c     pt_pc_get.f
c
c     Get principal component of Pt
c
      use commondata, only : leapday,ensmem,threesixty,mx,my,days,loc,
     & area,pt_eof,loc_sq,seasons,ngmax,tmp_path,sur_path,eof_path,
     & date,time

      implicit none

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

      data snd /'_r','_SLPanom_Adjusted.dat'/
      data sno /'_r','_Pt_PCs_','_','.txt'/

c      call idate(date)
c      call itime(time)
c      write(*,1000) date(2),date(1),date(3)+2000,time
c1000  format('pt_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
      call get_command_argument(3,input)
      read(input,*) vari(1)
      call get_command_argument(4,input)
      read(input,*) vari(2)
      call get_command_argument(5,input)
      read(input,*) vari(3)
      call get_command_argument(6,input)
      read(input,*) vari(4)
      call get_command_argument(7,input)
      read(input,*) vari(5)
      call get_command_argument(8,input)
      read(input,*) vari(6)

      if(vari(6).eq.2) days=threesixty()

      do z=1,13
      valid=0
      open(9,file=trim(sur_path)//trim(loc_sq(1))//trim(area(z))//
     & trim(loc_sq(2)))
      do
         read(9,*,end=77) a
         valid=valid+1
      enddo
77    close(9)
      open(9,file=trim(sur_path)//trim(loc_sq(1))//trim(area(z))//
     & trim(loc_sq(2)))
      read(9,*) a
      valid=valid-1
      do i=1,valid
         read(9,*) num(i)
      enddo
      close(9)
      do k=1,4
      open(20+k,file=trim(eof_path)//trim(area(z))//trim(pt_eof(1))//
     & trim(seasons(k))//trim(pt_eof(2)))
      do j=1,valid
         read(20+k,*) (eof(k,j,i),i=1,45)
      enddo
      close(20+k)
      enddo

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

      do k=1,4
      open(10+k,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)))
      enddo
      open(2,file=trim(tmp_path)//trim(model)//trim(snd(1))//
     & trim(smem)//trim(snd(2)),
     & form='unformatted',access='direct',recl=mx*(my-1)*4)

      id=0

      iyear=vari(2)
      imonth=vari(3)
      iday=vari(4)
      ihour=vari(5)-6
      leap=leapday(iyear,vari(6))

      do t=1,vari(1)
         id=id+1
         ihour=ihour+6
         if(ihour.gt.18) then
            ihour=0
            iday=iday+1
            if(leap.eq.1.and.imonth.eq.2) then
               if(iday.gt.days(imonth)+1) then
                  iday=1
                  imonth=imonth+1
               endif
            else
               if(iday.gt.days(imonth)) then
                  iday=1
                  imonth=imonth+1
                  if(imonth.gt.12) then
                     imonth=1
                     iyear=iyear+1
                     leap=leapday(iyear,vari(6))
                  endif
               endif
            endif
         endif

         if(imonth.lt.4) then
            k=1
         else if(imonth.lt.7) then
            k=2
         else if(imonth.lt.10) then
            k=3
         else
            k=4
         endif

         read(2,rec=id) (values(i),i=1,mx*(my-1))
         do i=1,30
            pc(i)=0.
            do j=1,valid
               pc(i)=pc(i)+values(num(j))*eof(k,j,i)
               if(values(num(j)).lt.-899.99) then
                  print*,'missing data',z,k
                  stop
               endif
            enddo
         enddo

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

99    close(11)
      close(12)
      close(13)
      close(14)
      close(2)
      enddo
      enddo

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

      end

