c     pt_get.f
c
c     Get Pt from adjusted SLP
c
      use commondata, only : leapday,ensmem,threesixty,mx,my,days,
     & seasons,tmp_path,date,time

      implicit none

      real :: p(200,100),q(200,100)
      integer :: vari(6),fid,mem,runs,leap
      integer :: iyear,imonth,iday,ihour,id,t,i,j
      character(len=50) :: snd(2),sno(3),smem,input,model

      data snd /'_r','_SLP_Adjusted.dat'/
      data sno /'_r','_SLP_','.txt'/

c      call idate(date)
c      call itime(time)
c      write(*,1000) date(2),date(1),date(3)+2000,time
c1000  format('pt_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 mem=runs,runs
      smem=ensmem(mem)

      id=0

      open(3,file=trim(tmp_path)//trim(model)//trim(snd(1))//
     & trim(smem)//trim(snd(2)),
     & form='unformatted',access='direct',recl=mx*(my+1)*4)

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

      open(21,file=trim(tmp_path)//trim(model)//trim(sno(1))//
     & trim(smem)//trim(sno(2))//trim(seasons(1))//trim(sno(3)))
      open(22,file=trim(tmp_path)//trim(model)//trim(sno(1))//
     & trim(smem)//trim(sno(2))//trim(seasons(2))//trim(sno(3)))
      open(23,file=trim(tmp_path)//trim(model)//trim(sno(1))//
     & trim(smem)//trim(sno(2))//trim(seasons(3))//trim(sno(3)))
      open(24,file=trim(tmp_path)//trim(model)//trim(sno(1))//
     & trim(smem)//trim(sno(2))//trim(seasons(4))//trim(sno(3)))

      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

         read(3,rec=id)((p(i,j),i=1,mx),j=1,my+1)
         do i=1,mx
            do j=1,my-1
               q(i,j)=p(i,j+1)
               if(q(i,j).lt.-899.99) then
                  q(i,j)=-900.99
               endif
            enddo
         enddo

         if(imonth.lt.4) then
            fid=21
         else if(imonth.lt.7) then
            fid=22
         else if(imonth.lt.10) then
            fid=23
         else
            fid=24
         endif

         write(fid,'(i6,3i4.2)') iyear,imonth,iday,ihour
         write(fid,'(10f9.3)') ((q(i,j),i=1,mx),j=1,(my-1))
      enddo

      close(3)
      close(21)
      close(22)
      close(23)
      close(24)
      enddo

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

      end
