PROGRAM ARGOS

!GUSTAVO      parameter  (NETA=50, NLEV=13)
!GUSTAVO        parameter  (IGRTBL=52)
!GUSTAVO      parameter (IMAX=im,JMAX=jm,NVAR=5)
!GUSTAVO      parameter (IX=1,IY=im,JX=1,JY=jm)
!GUSTAVO      INTEGER i,j,n,k,v,irec,reclen
!GUSTAVO      INTEGER NPBL,C(NLEV)
!GUSTAVO                                                INTEGER kgtype,imdlty !GUSTAVO
!GUSTAVO                                                REAL dx,lonw,alatvt,lats,dy !GUSTAVO
!GUSTAVO      REAL peta(IMAX,JMAX,NETA), psigma(IMAX,JMAX,NLEV)
!GUSTAVO      REAL a(NLEV),b(NLEV)
!GUSTAVO      REAL vareta(IMAX,JMAX,NETA,NVAR), varsigma(IMAX,JMAX,NLEV,NVAR)
!GUSTAVO      REAL LON(IMAX,JMAX),LAT(IMAX,JMAX),PRESSFC(IMAX,JMAX),PREC(IMAX,JMAX)
!GUSTAVO      REAL FSS(IMAX,JMAX),FMS(IMAX,JMAX),CRG(IMAX,JMAX),MASK(IMAX,JMAX)
!GUSTAVO      REAL HGT(IMAX,JMAX,NETA),POT(IMAX,JMAX,NETA),TKE(IMAX,JMAX,NETA),PRES(IMAX,JMAX,NETA)
!GUSTAVO      REAL SPFH(IMAX,JMAX,NETA),UGRD(IMAX,JMAX,NETA),VGRD(IMAX,JMAX,NETA)
!GUSTAVO      REAL dlnpm(IMAX,JMAX,NLEV),dlnpo(IMAX,JMAX,NETA),swind(imax,jmax,nlev),dwind(imax,jmax,nlev),dir(imax,jmax,nlev)
!GUSTAVO      REAL HPBL(imax,jmax),TKEMIN(imax,jmax)

!     parameter  (NETA=50, NLEV=13,NVAR=5)
      parameter  (NETA=50, NLEV=21,NVAR=5)  !DIÊGO
      parameter  (IGRTBL=52)
      INTEGER i,j,n,k,v,irec,reclen,p1,p2
      INTEGER NPBL,C(NLEV)
      INTEGER kgtype,imdlty !GUSTAVO
      REAL dx,lonw,alatvt,lats,dy !GUSTAVO
      REAL a(NLEV),b(NLEV)
      REAL,ALLOCATABLE,DIMENSION (:,:,:)       :: peta
      REAL,ALLOCATABLE,DIMENSION (:,:,:)       :: psigma
      REAL,ALLOCATABLE,DIMENSION (:,:,:,:)     :: vareta
      REAL,ALLOCATABLE,DIMENSION (:,:,:,:)     :: varsigma
      REAL,ALLOCATABLE,DIMENSION (:,:)         :: LON,LAT,PRESSFC,PREC,ZS,TP2M,theta2
      REAL,ALLOCATABLE,DIMENSION (:,:)         :: FSS,FMS,CRG,MASK,DP2M,U10M,V10M
      REAL,ALLOCATABLE,DIMENSION (:,:)         :: swind10,dwind10,dir10,q2m
      REAL,ALLOCATABLE,DIMENSION (:,:,:)       :: HGT,POT,PRES
      REAL,ALLOCATABLE,DIMENSION (:,:,:)       :: TKE
      REAL,ALLOCATABLE,DIMENSION (:,:,:)       :: SPFH,UGRD,VGRD,dlnpo
      REAL,ALLOCATABLE,DIMENSION (:,:,:)       :: dlnpm,swind,dwind,dir
      REAL,ALLOCATABLE,DIMENSION (:,:)         :: HPBL,TKEMIN

      CHARACTER          INPUT*35, OUTPUT*200, IGRDTBL*52
      CHARACTER(LEN=6  ) FCTN
      CHARACTER(LEN=2  ) HORAA,MINUTEA
      CHARACTER(LEN=12 ) DATAA
      CHARACTER(LEN=6  ) nufile,outype,proj,readco,readll,dataset,itag,datset
      logical north
!GUSTAVO

!      DATA A /0.0000, 0.0000, 0.0000, 7.2673, 109.2023, 307.6248, &
!      618.0159, 1048.745, 1601.891, 2274.187, 3057.763,3940.954, 4909.016/
!      !, 5944.805, 7029.453, 8142.773/

!      DATA B /1.000000, 0.9924415, 0.9836503, 0.9730898, 0.9603294,0.9450387 &
!      , 0.9269804, 0.9060047, 0.8820428,0.8551004, 0.8252521, 0.7926340, 0.7574387/
!      !, 0.7199079, 0.6803269, 0.6390179/


!DIÊGO

      DATA A /0.000000, 0.003160, 6.575628, 54.208336, 162.043427, 336.772369, &
      576.314148, 895.193542, 1297.656128, 1784.854614, 2356.202637, 3010.146973, 3743.464355, &
      4550.215820, 5422.802734, 6353.920898, 7335.164551, 8356.252930, 9405.222656, 10471.310547, 11543.166992/


      DATA B /1.000000, 0.997630, 0.994204, 0.989153, 0.982238, 0.973466, &
      0.963007, 0.950274, 0.935157, 0.917651, 0.897767, 0.875518, 0.850950, &
      0.824185, 0.795385, 0.764679, 0.732224, 0.698224, 0.662934, 0.626559, 0.589317/

!DIÊGO (data A e B aproximados - 9 algarismos no máximo)

!      DATA A /0.000000, 0.003160, 6.575628, 54.20834, 162.0434, 336.7724, &
!      576.3142, 895.1935, 1297.656, 1784.855, 2356.203, 3010.147, 3743.464, &
!      , 4550.216, 5422.803, 6353.921, 7335.165, 8356.253, 9405.223, 10471.311, 11543.167/
!
!
!      DATA B /1.000000, 0.997630, 0.994204, 0.989153, 0.982238, 0.973466 &
!      , 0.963007, 0.950274, 0.935157,0.917651, 0.897767, 0.875518, 0.850950, &
!      , 0.824185, 0.795385, 0.764679, 0.732224, 0.698224, 0.662934, 0.626559, 0.589317/


!GUSTAVO
      read(5,*)ITAG,NRSTRT,NPINCR,dataset
!GSM      dataset=TRIM(dataset)
      write(6,*)ITAG,NRSTRT,NPINCR,dataset
      
!     READ DOMAINS.
      open(1,file='./namelist_medium_domain',form='formatted',status='old')
      rewind 1
      read(1,*)ixxx,iyyy,jxxx,jyyy

      IMXXX=(iyyy-ixxx)+1
      JMXXX=(jyyy-jxxx)+1

      print*,"NUM. DE PTS DO RECORTE",ixxx,iyyy,jxxx,jyyy

!     READ OUTPUT GRID SPECIFICATIONS.
!
      OPEN(18,FILE='cntrl.parm_'//dataset,status='old')
      rewind(18)
      read(18,1000) kgtype
      read(18,1000) imdlty
      read(18,1030) datset
      read(18,1030) outype
      read(18,1030) nufile
      read(18,1030) proj
      read(18,1010) north
      read(18,1000) im
      read(18,1000) jm
      read(18,1020) dx
      read(18,1020) lonw
      read(18,1020) alatvt
      read(18,1020) lats
      read(18,1020) dy
      read(18,1030) readll
      read(18,1030) readco
 1000 format(T28,I5)
 1010 format(T28,L1)
 1020 format(T28,F11.6)
 1030 format(T28,A6)
      print*,"im: ",im," jm: ",jm


      IMAX=im
      JMAX=jm
      IX=1
      IY=im
      JX=1
      JY=jm

      ixrec=ixxx
      iyrec=iyyy

      jxrec=jxxx
      jyrec=jyyy

! Allocate variables
      ALLOCATE(peta(IMAX,JMAX,NETA))
      ALLOCATE(psigma(IMAX,JMAX,NLEV))
      ALLOCATE(vareta(IMAX,JMAX,NETA,NVAR))
      ALLOCATE(varsigma(IMAX,JMAX,NLEV,NVAR))
      ALLOCATE(LON(IMAX,JMAX))
      ALLOCATE(LAT(IMAX,JMAX))
!!!GUSTAVO
      ALLOCATE(ZS(IMAX,JMAX))
      ALLOCATE(TP2M(IMAX,JMAX))
      ALLOCATE(DP2M(IMAX,JMAX))
      ALLOCATE(U10M(IMAX,JMAX))
      ALLOCATE(V10M(IMAX,JMAX))
      ALLOCATE(swind10(IMAX,JMAX))
      ALLOCATE(dwind10(IMAX,JMAX))
      ALLOCATE(dir10(IMAX,JMAX))
      ALLOCATE(q2m(IMAX,JMAX))
      ALLOCATE(theta2(IMAX,JMAX))
!!!GUSTAVO						
      ALLOCATE(PRESSFC(IMAX,JMAX))
      ALLOCATE(PREC(IMAX,JMAX))
      ALLOCATE(FSS(IMAX,JMAX))
      ALLOCATE(FMS(IMAX,JMAX))
      ALLOCATE(CRG(IMAX,JMAX))
      ALLOCATE(MASK(IMAX,JMAX))
      ALLOCATE(HGT(IMAX,JMAX,NETA))
      ALLOCATE(POT(IMAX,JMAX,NETA))
      ALLOCATE(PRES(IMAX,JMAX,NETA))
      ALLOCATE(TKE(IMAX,JMAX,NETA))
      ALLOCATE(SPFH(IMAX,JMAX,NETA))
      ALLOCATE(UGRD(IMAX,JMAX,NETA))
      ALLOCATE(VGRD(IMAX,JMAX,NETA))
      ALLOCATE(dlnpo(IMAX,JMAX,NETA))
      ALLOCATE(dlnpm(IMAX,JMAX,NLEV))
      ALLOCATE(swind(IMAX,JMAX,NLEV))
      ALLOCATE(dwind(IMAX,JMAX,NLEV))
      ALLOCATE(dir(IMAX,JMAX,NLEV))
      ALLOCATE(HPBL(IMAX,JMAX))
      ALLOCATE(TKEMIN(IMAX,JMAX))

!GUSTAVO
      INQUIRE (IOLENGTH = reclen) PRESSFC

      CALL GETARG(1,INPUT)
      CALL GETARG(2,FCTN)
      OPEN (10, FILE=TRIM(INPUT),ACCESS='DIRECT',RECL=reclen,FORM='unformatted')

      CALL TIME(DATAA)
      HORAA=DATAA(1:2)
      MINUTEA=DATAA(4:5)

      WRITE (*,*)"LENDO: "//INPUT
      WRITE (*,*)INPUT(12:13)//" "//INPUT(14:15)//" "//INPUT(16:17)//" "//HORAA//" "//MINUTEA
      WRITE (*,*)INPUT(12:13)//" "//INPUT(14:15)//" "//INPUT(16:17)//" "//INPUT(18:19)//" "//FCTN(5:6)

!chou      OUTPUT=INPUT(13:14)//INPUT(15:16)//INPUT(17:18)//INPUT(19:20)//FCTN(2:3)//"_52"IGRDTBL
      OUTPUT=INPUT(12:13)//INPUT(14:15)//INPUT(16:17)//INPUT(18:19)//FCTN(5:6)//"_01"
      WRITE (*,*)"ESCREVENDO: "//OUTPUT

! Read 2d variables
      irec=1
      READ(10,rec=irec) LON(:,:)
!      print*,LON
      irec=irec+1
      READ(10,rec=irec) LAT(:,:)

      irec=irec+1
      READ(10,rec=irec) ZS(:,:)

      irec=irec+1
      READ(10,rec=irec) TP2M(:,:)

      irec=irec+1
      READ(10,rec=irec) DP2M(:,:)

      irec=irec+1
      READ(10,rec=irec) U10M(:,:)

      irec=irec+1
      READ(10,rec=irec) V10M(:,:)

      irec=irec+1
      READ(10,rec=irec) MASK(:,:)

      irec=irec+1
      READ(10,rec=irec) PRESSFC(:,:)

      irec=irec+1
      READ(10,rec=irec) PREC(:,:)

      irec=irec+1
      READ(10,rec=irec) FSS(:,:)

      irec=irec+1
      READ(10,rec=irec) FMS(:,:)

      irec=irec+1
      READ(10,rec=irec) CRG(:,:)

! Read 3d variables
      DO k=1,neta
        irec=irec+1
        READ(10,rec=irec) PRES(:,:,k)
      END DO

      DO k=1,neta
       irec=irec+1
       READ(10,rec=irec) HGT(:,:,k)

      END DO

      DO k=1,neta
        irec=irec+1
        READ(10,rec=irec) POT(:,:,k)
      END DO

      DO k=1,neta
        irec=irec+1
        READ(10,rec=irec) SPFH(:,:,k)
      END DO

      DO k=1,neta
        irec=irec+1
        READ(10,rec=irec) UGRD(:,:,k)
      END DO

      DO k=1,neta
        irec=irec+1
        READ(10,rec=irec) VGRD(:,:,k)
      END DO

      DO k=1,neta
        irec=irec+1
        READ(10,rec=irec) TKE(:,:,k)
      END DO


      CLOSE(10,STATUS='DELETE')

 ! Calculation of pressure for interpolation

      DO i=1,imax
       DO j=1,jmax
        DO k=1,neta
         peta(i,j,k)=PRES(i,j,k)
        END DO
       END DO
      END DO


      DO i=1,imax
       DO j=1,jmax
        DO n=1,nlev
         psigma(i,j,n)=a(n)+b(n)*PRESSFC(i,j)
        END DO
       END DO
      END DO


      DO i=1,imax
       DO j=1,jmax
        DO k=1,neta
         vareta(i,j,k,1)=HGT(i,j,k)
         vareta(i,j,k,2)=POT(i,j,k)
         vareta(i,j,k,3)=SPFH(i,j,k)
         vareta(i,j,k,4)=UGRD(i,j,k)
         vareta(i,j,k,5)=VGRD(i,j,k)
        END DO
       END DO
      END DO

!  Interpolation from eta vertical coordinate to sigma vertical coordinate

      DO v=1,nvar
       DO i=1,imax
        DO j=1,jmax
         DO n=1,nlev
          DO k=1,neta-1
           IF ((psigma(i,j,n).LE.peta(i,j,k)) .and. (psigma(i,j,n).GE.peta(i,j,k+1))) THEN
             p1=k
             p2=k+1
             dlnpm(i,j,n)=alog(psigma(i,j,n))-alog(peta(i,j,p1))
             dlnpo(i,j,p1)=alog(peta(i,j,p2))-alog(peta(i,j,p1))
             varsigma(i,j,n,v)=(vareta(i,j,p2,v)-vareta(i,j,p1,v))*dlnpm(i,j,n)/dlnpo(i,j,p1) +vareta(i,j,p1,v)

           ELSE

               IF(psigma(i,j,1).GT.peta(i,j,1)) THEN
                varsigma(i,j,1,v)= vareta(i,j,1,v)
               END IF

           END IF
          END DO
         END DO
        END DO
       END DO
      END DO


!################################################
! Change the unit of precipitacion from m for mm
      DO i=1,imax
       DO j=1,jmax
        PREC(i,j)=PREC(i,j)*1000
       END DO
      END DO
!##################################################
! Calculate Planetary Boundary Layer height

        NPBL=1
        DO i=1,imax
        DO j=1,jmax
        DO k=2,NETA/2   ! Chou: check up to half of number layers
         IF (TKE(i,j,k).LE.0.202) THEN
          NPBL=k-1
          GO TO 300
         END IF
        END DO
         300 HPBL(i,j)=HGT(i,j,NPBL)
        END DO
        END DO

!####################################################
!Calculate the wind speed in m/s

      DO i=1,imax
       DO j=1,jmax
       swind10(i,j)=sqrt(u10m(i,j)**2+v10m(i,j)**2) !CHOU
        DO n=1,nlev
         swind(i,j,n)=sqrt(varsigma(i,j,n,4)**2+varsigma(i,j,n,5)**2)
        END DO
       END DO
      END DO

!calculate the wind direction in degrees

      DO i=1,imax
      DO j=1,jmax
      DO n=1,nlev
        IF (varsigma(i,j,n,4).eq.0) THEN
           IF (varsigma(i,j,n,5).GT.0) THEN
             dir(i,j,n)=90.
           else
             dir(i,j,n)=360.
           END IF
        else
           dir(i,j,n)=atan(varsigma(i,j,n,5)/varsigma(i,j,n,4))*180./3.14159
           IF (varsigma(i,j,n,4).LT.0) THEN
             dir(i,j,n)=dir(i,j,n)+180.
           END IF
           IF (dir(i,j,n).LT.0) THEN
             dir(i,j,n)=dir(i,j,n)+360.
           END IF
        END IF

          dwind(i,j,n)=270.-dir(i,j,n)
          IF (dwind(i,j,n).LT.0) THEN
           dwind(i,j,n)=dwind(i,j,n)+360.
          END IF
          IF (dwind(i,j,n).GE.360.) THEN
           dwind(i,j,n)=dwind(i,j,n)-360.
          END IF

      END DO
      END DO
      END DO

      DO i=1,imax
      DO j=1,jmax
        IF (u10m(i,j).eq.0) THEN
           IF (v10m(i,j).GT.0) THEN
             dir10(i,j)=90.
           else
             dir10(i,j)=360.
           END IF
        else
           dir10(i,j)=atan(v10m(i,j)/u10m(i,j))*180./3.14159
           IF (u10m(i,j).LT.0)  dir10(i,j)=dir10(i,j)+180.
           IF (dir10(i,j).LT.0) dir10(i,j)=dir10(i,j)+360.
        END IF
        dwind10(i,j)=270.-dir10(i,j)
        IF (dwind10(i,j).LT.0)  dwind10(i,j)=dwind10(i,j)+360.
        IF (dwind10(i,j).GE.360.) dwind10(i,j)=dwind10(i,j)-360.
      END DO
      END DO

! Calculate specific humidity from T and Td
!0. calculate es and e, 1. calculate RH , 2. calculate qs
      DO i=1,imax
      DO j=1,jmax
       Tc =Tp2m(i,j)-273.15
       Tdc=Dp2m(i,j)-273.15
       Es=6.11*10.0**(7.5*Tc/(237.7+Tc))
       E =6.11*10.0**(7.5*Tdc/(237.7+Tdc))
       qs= 0.622*Es/((pressfc(i,j)/100)-Es)
       RH=E/Es
       q2m(i,j)=RH*qs
      ENDDO
      ENDDO

!####################################################
! Calculate virtual potential temperature

      DO i=1,imax
       DO j=1,jmax
          theta2(i,j)=tp2m(i,j)*(1.0+0.61*q2m(i,j))
        DO n=1,nlev
          varsigma(i,j,n,2)=varsigma(i,j,n,2)*(1.0+0.61*varsigma(i,j,n,3))
        END DO
       END DO
      END DO
!###################################################

! Write DMI-HIRLAM text format
      OPEN (UNIT=20,FILE=TRIM(OUTPUT))

      write(20,40)
      write(20,41)INPUT(12:13)//" "//INPUT(14:15)//" "//INPUT(16:17)//" "//HORAA//" "//MINUTEA
      write(20,42)INPUT(12:13)//" "//INPUT(14:15)//" "//INPUT(16:17)//" "//INPUT(18:19)//" "//FCTN(5:6)
      write(20,'(10x,I3,10x,I3)') IMXXX,JMXXX
      write(20,44)
      write(20,45)
      write(20,46)
      write(20,47)
      write(20,48)
      write(20,49)
!!!      write(20,'(10I8.5)')((NINT((LON(i,j)-360)*1000),i=ix,iy),j=jx,jy)
      write(20,'(10I8.5)')((NINT((LON(i,j)-360)*1000),i=ixrec,iyrec),j=jxrec,jyrec)
      write(20,50)
      write(20,51)
!!!      write(20,'(10I8.5)')((NINT(LAT(i,j)*1000),i=ix,iy),j=jx,jy)
      write(20,'(10I8.5)')((NINT(LAT(i,j)*1000),i=ixrec,iyrec),j=jxrec,jyrec)
      write(20,52)
      write(20,53)
!!      write(20,'(10I8.5)') ((NINT(PREC(i,j)*1000),i=ix,iy),j=jx,jy)
      write(20,'(10I8.5)') ((NINT(PREC(i,j)*1000),i=ixrec,iyrec),j=jxrec,jyrec)
      write(20,54)
      write(20,55)
!1      write(20,'(10I8.5)') ((NINT(HPBL(i,j)),i=ix,iy),j=jx,jy)
      write(20,'(10I8.5)') ((NINT(HPBL(i,j)),i=ixrec,iyrec),j=jxrec,jyrec)
      write(20,56)
      write(20,57)
!!     write(20,'(10I8.5)') ((NINT(FSS(i,j)*1000),i=ix,iy),j=jx,jy)
      write(20,'(10I8.5)') ((NINT(FSS(i,j)*1000),i=ixrec,iyrec),j=jxrec,jyrec)
      write(20,58)
      write(20,59)
!!      write(20,'(10I8.5)') ((NINT(FMS(i,j)*10),i=ix,iy),j=jx,jy)
      write(20,'(10I8.5)') ((NINT(FMS(i,j)*10),i=ixrec,iyrec),j=jxrec,jyrec)
      write(20,60)
      write(20,61)
!!      write(20,'(10I8.5)') ((NINT(MASK(i,j)*1000),i=ix,iy),j=jx,jy)
      write(20,'(10I8.5)') ((NINT(MASK(i,j)*1000),i=ixrec,iyrec),j=jxrec,jyrec)
      write(20,62)
      write(20,63)
!      write(20,'(10I8.5)')((NINT(CRG(i,j)*1000),i=ix,iy),j=jx,jy)
      write(20,'(10I8.5)')((NINT(CRG(i,j)*1000),i=ixrec,iyrec),j=jxrec,jyrec)


!      DATA C /00, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20/


!DIÊGO

       DATA C / 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71/

        N0=0
        write(20,64)
      !Geopotential
        write(20,65)
        write(20,66)
        write(20,'(i2.2)') N0
!        write(20,'(10I8.5)') ((NINT(zs(i,j)),i=ix,iy),j=jx,jy)
        write(20,'(10I8.5)') ((NINT(zs(i,j)),i=ixrec,iyrec),j=jxrec,jyrec)   
      DO 5 n=2,nlev
        write(20,65)
        write(20,66)
        write(20,'(i2.2)') c(n)
!!        write(20,'(10I8.5)')((NINT(varsigma(i,j,n,1)),i=ix,iy),j=jx,jy)
         write(20,'(10I8.5)')((NINT(varsigma(i,j,n,1)),i=ixrec,iyrec),j=jxrec,jyrec)
      5 CONTINUE


      !wind speed (m/s)
        write(20,67)
        write(20,68)
        write(20,'(i2.2)') N0
!!        write(20,'(10I8.5)')((NINT(swind10(i,j)*1000),i=ix,iy),j=jx,jy)
        write(20,'(10I8.5)')((NINT(swind10(i,j)*1000),i=ixrec,iyrec),j=jxrec,jyrec)
      DO 6 n=2,nlev
        write(20,67)
        write(20,68)
        write(20,'(i2.2)') c(n)
!!        write(20,'(10I8.5)')((NINT(swind(i,j,n)*1000),i=ix,iy),j=jx,jy)
        write(20,'(10I8.5)')((NINT(swind(i,j,n)*1000),i=ixrec,iyrec),j=jxrec,jyrec)
      6 CONTINUE


      !wind direction
        write(20,69)
        write(20,70)
        write(20,'(i2.2)') N0
!!        write(20,'(10I8.5)')((NINT(dwind10(i,j)),i=ix,iy),j=jx,jy)
        write(20,'(10I8.5)')((NINT(dwind10(i,j)),i=ixrec,iyrec),j=jxrec,jyrec)
      DO 7 n=2,nlev
        write(20,69)
        write(20,70)
        write(20,'(i2.2)') c(n)
!!        write(20,'(10I8.5)')((NINT(dwind(i,j,n)),i=ix,iy),j=jx,jy)
        write(20,'(10I8.5)')((NINT(dwind(i,j,n)),i=ixrec,iyrec),j=jxrec,jyrec)
      7 CONTINUE


      !virtual potential temperature
        write(20,71)
        write(20,72)
        write(20,'(i2.2)') N0
!!        write(20,'(10I8.5)')((NINT(theta2(i,j)*100),i=ix,iy),j=jx,jy)
        write(20,'(10I8.5)')((NINT(theta2(i,j)*100),i=ixrec,iyrec),j=jxrec,jyrec)
      DO 8 n=2,nlev
        write(20,71)
        write(20,72)
        write(20,'(i2.2)') c(n)
!!        write(20,'(10I8.5)')((NINT(varsigma(i,j,n,2)*100),i=ix,iy),j=jx,jy)
        write(20,'(10I8.5)')((NINT(varsigma(i,j,n,2)*100),i=ixrec,iyrec),j=jxrec,jyrec)
      8 CONTINUE



      !specific humidity
        write(20,73)
        write(20,74)
        write(20,'(i2.2)') N0
!!        write(20,'(10I8.5)')((NINT(q2m(i,j)*10000),i=ix,iy),j=jx,jy)
        write(20,'(10I8.5)')((NINT(q2m(i,j)*10000),i=ixrec,iyrec),j=jxrec,jyrec)
      DO 9 n=2,nlev
        write(20,73)
        write(20,74)
        write(20,'(i2.2)') c(n)
!!        write(20,'(10I8.5)')((NINT(varsigma(i,j,n,3)*10000),i=ix,iy),j=jx,jy)
        write(20,'(10I8.5)')((NINT(varsigma(i,j,n,3)*10000),i=ixrec,iyrec),j=jxrec,jyrec)
      9 CONTINUE

      CLOSE(20)

40 format ("HEADER")
41 format (A14)
42 format (A14)
43 format ("33 33")
!44 format ("13  00 31 30 29 28 27 26 25 24 23 22 21 20")
!44 format ("22  00 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71")   !DIÊGO
44 format ("21  00 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71")   !GUSTAVO
45 format ("1 centered")
46 format ("80.0 0.0")
47 format ("SINGLE-LEVEL FIELDS")
48 format ("longitude (decimal deg.)")
49 format ("1.00E-03")
50 format ("latitude (decimal deg.)")
51 format ("1.00E-03")
52 format ("precipitation intensity (mm/hour)")
53 format ("1.00E-03")
54 format ("ABL height (m)")
55 format ("1.00")
56 format ("surface sensible heat flux (W/m^2)")
57 format ("1.00E-03")
58 format ("surface momentum flux (kg/(m*s^2))")
59 format ("1.00E-01")
60 format ("fraction of land")
61 format ("1.00E-03")
62 format ("roughness (m)")
63 format ("1.00E-03")
64 format ("MULTI-LEVEL FIELDS")
65 format ("geopotential height (m)")
66 format ("1.00")
67 format ("wind speed (m/s)")
68 format ("1.00E-03")
69 format ("wind direction (decimal deg.)")
70 format ("1.00")
71 format ("virtual potential temperature (K)")
72 format ("1.00E-02")
73 format ("specific humidity (dimensionless)")
74 format ("1.00E-04")



!#######################################################################


end PROGRAM ARGOS
