      implicit none

      integer,parameter :: mx=360
      integer,parameter :: my=181

      real :: results(mx,my),days(12)
      integer :: vari(6),fid,leap,leapday
      integer :: iyear,imonth,iday,ihour,id,t,i,j
      character(len=200) :: snd
      character(len=70) :: sno(2),input,model
      character(len=3) :: seasons(4)

      data sno /'_SLP_','.txt'/
      data days /31,28,31,30,31,30,31,31,30,31,30,31/
      data seasons /'jfm','amj','jas','ond'/

      call get_command_argument(1,input)
      read(input,*) model
      call get_command_argument(2,input)
      read(input,*) vari(1)
      call get_command_argument(3,input)
      read(input,*) vari(2)
      call get_command_argument(4,input)
      read(input,*) vari(3)
      call get_command_argument(5,input)
      read(input,*) vari(4)
      call get_command_argument(6,input)
      read(input,*) vari(5)
      call get_command_argument(7,input)
      read(input,*) snd

      open(3,file=trim(snd),
     & form='unformatted',access='direct',recl=mx*my*4)

      id=0
      print*,trim(snd)

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

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

      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
                     print*,iyear
                     leap=leapday(iyear)
                  endif
               endif
            endif
         endif

         read(3,rec=id)((results(i,j),i=1,mx),j=1,my)

         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

         do i=1,mx
            do j=1,my
               if(results(i,j)/100..gt.9000) results(i,j)=-999.99
            enddo
         enddo

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

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

      FUNCTION leapday(year)

      integer :: leapday
      integer, intent(in) :: year

      leapday=0
      if(mod(year,4).eq.0) then
         leapday=1
         if(mod(year,100).eq.0) then
            leapday=0
            if(mod(year,400).eq.0) then
               leapday=1
            endif
         endif
      endif

      END FUNCTION leapday
