c     gt_pc_get.f
c
c     Get principal component of Gt
c
      use commondata, only : ensmem,mx,my,ngmax,seasons,area,loc,gt_eof,
     & boxcox_sq,sur,tmp_path,sur_path,lambda_path,eof_path,mean_path,
     & date,time

      implicit none

      real :: values(20000),pc(30)
      real :: ave(20000),eof(10000,45)
      integer :: iyear,imonth,iday,ihour,valid,i,j,k,t,z,mem,runs
      character(len=50) sne(4),snd(4),sno(4),smem,input,model,shmodel,a

      data sne /'_hist_r1','_trGt_Mean1981-2000_','_','_SQ.txt'/
      data snd /'_r','_trGt_','_','_SQ.txt'/
      data sno /'_r','_trGt_PCs_','_','_SQ.txt'/

c      call idate(date)
c      call itime(time)
c      write(*,1000) date(2),date(1),date(3)+2000,time
c1000  format('gt_pc_get start at: Date ', i2.2, '/', i2.2, '/', i4.4,
c     &         '; time ',i2.2, ':', i2.2, ':', i2.2 )

      call get_command_argument(1,input)
      read(input,*) model
      call get_command_argument(2,input)
      read(input,*) runs

      shmodel=model(1:index(model,'_')-1)

      do z=1,13
      do k=1,4

         do mem=runs,runs
         smem=ensmem(mem)

         open(1,file=trim(tmp_path)//trim(model)//trim(snd(1))//
     & trim(smem)//trim(snd(2))//trim(area(z))//trim(snd(3))//
     & trim(seasons(k))//trim(snd(4)))
         read(1,*) iyear,imonth,iday,ihour,valid
         close(1)
C        print*,trim(model)//trim(snd(1))//trim(smem)//trim(snd(2))//
C    & trim(area(z))//trim(snd(3))//trim(seasons(k))//trim(snd(4))

         open(33,file=trim(eof_path)//trim(area(z))//trim(gt_eof(1))//
     & trim(seasons(k))//trim(gt_eof(2)))
         do j=1,valid
            read(33,*) (eof(j,i),i=1,45)
         enddo
         close(33)

         open(34,file=trim(mean_path)//trim(shmodel)//trim(sne(1))//
     & trim(sne(2))//trim(area(z))//trim(sne(3))//
     & trim(seasons(k))//trim(sne(4)))
         read(34,*) (ave(i),i=1,valid)
         close(34)

         open(2,file=trim(tmp_path)//trim(model)//trim(snd(1))//
     & trim(smem)//trim(snd(2))//trim(area(z))//trim(snd(3))//
     & trim(seasons(k))//trim(snd(4)))
         open(3,file=trim(tmp_path)//trim(model)//trim(sno(1))//
     & trim(smem)//trim(sno(2))//trim(area(z))//trim(sno(3))//
     & trim(seasons(k))//trim(sno(4)))

         do
            read(2,*,end=99) iyear,imonth,iday,ihour
            read(2,'(10f9.3)') (values(i),i=1,valid)

            do i=1,30
               pc(i)=0.
               do j=1,valid
                  pc(i)=pc(i)+(values(j)-ave(j))*eof(j,i)
                  if(values(j).lt.-899.99) then
                     print*,'missing data',z,k
                     stop
                  endif
               enddo 
            enddo 

            write(3,'(i6,3i3,60f9.3)') iyear,imonth,iday,ihour,
     & (pc(i),i=1,30)
         enddo
99       close(2)
         close(3)
         enddo

      enddo
      enddo

c      call idate(date)
c      call itime(time)
c      write(*,2000) date(2),date(1),date(3)+2000,time
c2000  format('gt_pc_get end at: Date ', i2.2, '/', i2.2, '/', i4.4,
c     &         '; time ',i2.2, ':', i2.2, ':', i2.2 )

      end
