!Subroutine for write output files:
!W_OUTPUT1    - Write output files as original code
!W_OUTPUT0     - Write output files for look at Grads or Ferret (or similar)
!W_RESTART     - Write restart files - it is not working yet

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! WRITE OUTPUT AS ORIGINAL CODE
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

      SUBROUTINE W_OUTPUT1!(iwmon, incf, inrf, along, alatg, sflux, heightm, &
!                          velocity, tmpdir, vollm, waream)
      
      use comvar
      implicit none
      include 'netcdf.inc'

      INTEGER nlevel, ntime

      nlevel = 1
      alev   = 1.
      uidim = 'degrees_east'
      ujdim = 'degrees_north'
      ukdim = 'level'
      uldim = 'months since 1990'

      IF (iwmon .eq. 1)then

         ntime = 1
         atim  = float(ntime)

! dimension variable units

         filen = 'output/sflux.ctl.nc'
         title = 'river discharge'
         units = 'm3/s'
         aname = 'sflux$'
         CALL ini4dnc (sflux, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )

         filen = 'output/height.ctl.nc'
         title = 'water elevation'
         units = 'm'
         aname = 'height$'
         CALL ini4dnc (heightm, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )

         filen = 'output/velocity.ctl.nc'
         title = 'river velocity'
         units = 'm/s'
         aname = 'veloc$'
         CALL ini4dnc (velocity, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )

         filen = 'output/tmpdir.ctl.nc'
         title = 'flood flow direction'
         units = ''
         aname = 'tmpdir$'
         CALL ini4dnc (tmpdir, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )

         filen = 'output/vollm.ctl.nc'
         title = 'flood water volume'
         units = 'm3'
         aname = 'vollm$'
         CALL ini4dnc (vollm, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )

         filen = 'output/warea.ctl.nc'
         title = 'water indicator'
         units = 'fraction'
         aname = 'warea$'
         CALL ini4dnc (waream, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )

       ELSE

         ntime = iwmon
         atim = real(ntime)

         filen = 'output/sflux.ctl.nc'
         aname = 'sflux$'
         CALL wr4dnc (sflux, incf*inrf, incf, inrf, nlevel, &
                      ntime, atim, aname, filen )

         filen = 'output/height.ctl.nc'
         aname = 'height$'
         CALL wr4dnc (heightm, incf*inrf, incf, inrf, nlevel, &
                      ntime, atim, aname, filen )

         filen = 'output/velocity.ctl.nc'
         aname = 'veloc$'
         CALL wr4dnc (velocity, incf*inrf, incf, inrf, nlevel, &
                      ntime, atim, aname, filen )

         filen = 'output/tmpdir.ctl.nc'
         aname = 'tmpdir$'
         CALL wr4dnc (tmpdir, incf*inrf, incf, inrf, nlevel, &
                      ntime, atim, aname, filen )

         filen = 'output/vollm.ctl.nc'
         aname = 'vollm$'
         CALL wr4dnc (vollm, incf*inrf, incf, inrf, nlevel, &
                      ntime, atim, aname, filen )

         filen = 'output/warea.ctl.nc'
         aname = 'warea$'
         CALL wr4dnc (waream, incf*inrf, incf, inrf, nlevel, &
                      ntime, atim, aname, filen )

       ENDIF

      print *, ' ----------------------------------------'
      print *, '    write month OK! ::: next step        '
      print *, ' ----------------------------------------'

      END SUBROUTINE W_OUTPUT1


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! WRITE OUTPUT FOR GRADS, FERRET OR SIMILAR
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

      SUBROUTINE W_OUTPUT0!(imon,wday,inity,startyear,wyear,icmon, iday,ndaypm, & 
!                    incf, inrf, along, alatg, loni, lati, &
!                    sflux, heightm, velocity, tmpdir, vollm, waream)

      use comvar, only: imon,      &
                        wday,      &
                        startyear, &
                        wyear,     &
!                        icmon, &
!                        iday, &
                        ndaypm,    &
                        incf,      &
                        inrf,      &
                        along,     &
                        alatg,     &
!                        loni, &
!                        lati, &
                        sflux,     &
                        heightm,   &
                        velocity,  &
                        tmpdir,    &
                        vollm,     &
                        waream,    &
                        mstep2,    &
                        voll,      &
                        dvoll,     &
                        fluxout,   &
                        sfluxin
                        
      implicit none
      include 'netcdf.inc'
      
      INTEGER idies, istat, ndim, mstep, nlevel, iyear

      INTEGER, DIMENSION(4) :: istart, icount
      REAL ocean, tweight, nday, alev
      CHARACTER*80 :: cdate, tunits
      CHARACTER*21 :: calendar, source, aname
      CHARACTER*10 :: dimnames(3), tdate
      character*3  chmon(12)
      CHARACTER*80 :: filen,units, title,  name3d


      mstep=12*(wyear-startyear+1)+imon
      nday=wday-ndaypm(imon)+1
      iyear=startyear-1
      
      data chmon / 'JAN','FEB','MAR','APR','MAY','JUN', &
                   'JUL','AUG','SEP','OCT','NOV','DEC' /

      tunits = 'days since 1990-01-15'

      tdate=chmon(imon)//'0000'//char(0)//char(0)//char(0)

      if (wyear .ge. 1000) then
         write(tdate(4:7),'(i4)') wyear
      else if (wyear .lt. 10) then
         write(tdate(7:7),'(i1)') wyear
      else if (wyear .lt. 100) then
         write(tdate(6:7),'(i2)') wyear
      else
         write(tdate(5:7),'(i3)') wyear
      end if

      source = 'THMB wmonthly'
      cdate = 'created in 31/08/2020'
      dimnames(1) = 'lon'
      dimnames(2) = 'lat'
      dimnames(3) = 'time'
      ocean = 9.e+20
      ndim = 3
      nlevel = 1
      alev   = 1.
      calendar = 'gregorian'

      istart(1)=1
      istart(2)=1
      istart(3)=mstep2
      istart(4)=1
      icount(1)=incf
      icount(2)=inrf
      icount(3)=1
      icount(4)=1
      tweight=ndaypm(imon)
      
      PRINT *
      PRINT *, ' ----------------------------------------'
      PRINT *, '                                         '
      PRINT *, '       write output to GrADS  ::: OK!    '
      PRINT *, '                                         '
      PRINT *, ' ----------------------------------------'
      PRINT *
!      print *, 'No write do THMB!!!! ', mstep2, incf, inrf, nday
!      print *, 'No write do THMB!!!! ', cdate, tunits
      print *

      IF (mstep2 .eq. 1)then
         filen = 'output/sflux.ctl.nc'
         title = 'river discharge'
         units = 'm3/s'
         aname = 'sflux'
!         print *, 'testando dentro: ', incf, along, inrf, alatg
!         print *, 'testando dentro: ', along, alatg
         CALL inifile(idies,filen,title,source,cdate,incf,along,inrf,alatg, &
                      '','','',nlevel,alev,'',tunits,'gregorian',istat)
         CALL inivar(idies,aname,title,units,ndim,dimnames,ocean,istat)
         CALL endini(idies,istat)

         filen = 'output/height.ctl.nc'
         title = 'water elevation'
         units = 'm'
         aname = 'height'
         CALL inifile(idies,filen,title,source,cdate,incf,along,inrf,alatg, &
                      '','','',nlevel,alev,'',tunits,'gregorian',istat)
         CALL inivar(idies,aname,title,units,ndim,dimnames,ocean,istat)
         CALL endini(idies,istat)

         filen = 'output/velocity.ctl.nc'
         title = 'river velocity'
         units = 'm/s'
         aname = 'veloc'
         CALL inifile(idies,filen,title,source,cdate,incf,along,inrf,alatg, &
                      '','','',nlevel,alev,'',tunits,'gregorian',istat)
         CALL inivar(idies,aname,title,units,ndim,dimnames,ocean,istat)
         CALL endini(idies,istat)

         filen = 'output/tmpdir.ctl.nc'
         title = 'flood flow direction'
         units = ''
         aname = 'tmpdir'
         CALL inifile(idies,filen,title,source,cdate,incf,along,inrf,alatg, &
                      '','','',nlevel,alev,'',tunits,'gregorian',istat)
         CALL inivar(idies,aname,title,units,ndim,dimnames,ocean,istat)
         CALL endini(idies,istat)

         filen = 'output/vollm.ctl.nc'
         title = 'flood water volume'
         units = 'm3'
         aname = 'vollm'
         CALL inifile(idies,filen,title,source,cdate,incf,along,inrf,alatg, &
                      '','','',nlevel,alev,'',tunits,'gregorian',istat)
         CALL inivar(idies,aname,title,units,ndim,dimnames,ocean,istat)
         CALL endini(idies,istat)

         filen = 'output/warea.ctl.nc'
         title = 'water indicator'
         units = 'fraction'
         aname = 'warea'
         CALL inifile(idies,filen,title,source,cdate,incf,along,inrf,alatg, &
                      '','','',nlevel,alev,'',tunits,'gregorian',istat)
         CALL inivar(idies,aname,title,units,ndim,dimnames,ocean,istat)
         CALL endini(idies,istat)
         
         !print *, 'passei pelo iniciar VARS output GrADS!!!!!!', istart, icount
         !print *, 'ainda testando o inicial', nday, tweight, tdate, istat

         ! ------------- ESCREVER MENSALMENTE DVOLL -----------------------
         filen = 'output/dvoll.ctl.nc'
         title = 'delta voll'
         units = 'm3'
         aname = 'dvoll'
         CALL inifile(idies,filen,title,source,cdate,incf,along,inrf,alatg, &
                      '','','',nlevel,alev,'',tunits,'gregorian',istat)
         CALL inivar(idies,aname,title,units,ndim,dimnames,ocean,istat)
         CALL endini(idies,istat)

         filen = 'output/voll.ctl.nc'
         title = 'volume voll'
         units = 'm3'
         aname = 'voll'
         CALL inifile(idies,filen,title,source,cdate,incf,along,inrf,alatg, &
                      '','','',nlevel,alev,'',tunits,'gregorian',istat)
         CALL inivar(idies,aname,title,units,ndim,dimnames,ocean,istat)
         CALL endini(idies,istat)

         filen = 'output/fluxout.ctl.nc'
         title = 'fluxout voll'
         units = 'm3/s'
         aname = 'fluxout'
         CALL inifile(idies,filen,title,source,cdate,incf,along,inrf,alatg, &
                      '','','',nlevel,alev,'',tunits,'gregorian',istat)
         CALL inivar(idies,aname,title,units,ndim,dimnames,ocean,istat)
         CALL endini(idies,istat)

         filen = 'output/sfluxin.ctl.nc'
         title = 'sfluxin voll'
         units = 'm3/s'
         aname = 'sfluxin'
         CALL inifile(idies,filen,title,source,cdate,incf,along,inrf,alatg, &
                      '','','',nlevel,alev,'',tunits,'gregorian',istat)
         CALL inivar(idies,aname,title,units,ndim,dimnames,ocean,istat)
         CALL endini(idies,istat)
!
!--------------------------------------
! AFTER FIRST MONTH, JUST WRITE
!--------------------------------------
!
      ENDIF
      
         filen = 'output/sflux.ctl.nc'
         aname = 'sflux'
      CALL writevar(filen,aname,istart,icount,sflux,nday, &
                  tweight,tdate,istat)

         filen = 'output/height.ctl.nc'
         aname = 'height'
       CALL writevar(filen,aname,istart,icount,heightm,nday, &
                  tweight,tdate,istat)


         filen = 'output/velocity.ctl.nc'
         aname = 'veloc'
      CALL writevar(filen,aname,istart,icount,velocity,nday, &
                  tweight,tdate,istat)


         filen = 'output/tmpdir.ctl.nc'
         aname = 'tmpdir'
      CALL writevar(filen,aname,istart,icount,tmpdir,nday, &
                  tweight,tdate,istat)


         filen = 'output/vollm.ctl.nc'
         aname = 'vollm'
      CALL writevar(filen,aname,istart,icount,vollm,nday, &
                  tweight,tdate,istat)

         filen = 'output/warea.ctl.nc'
         aname = 'warea'
      CALL writevar(filen,aname,istart,icount,waream,nday, &
                  tweight,tdate,istat)

      ! ----- ESCREVER DVOLL E VOLL ------
         filen = 'output/dvoll.ctl.nc'
         aname = 'dvoll'
      CALL writevar(filen,aname,istart,icount,dvoll,nday, &
                  tweight,tdate,istat)

         filen = 'output/voll.ctl.nc'
         aname = 'voll'
      CALL writevar(filen,aname,istart,icount,voll,nday, &
                  tweight,tdate,istat)

         filen = 'output/fluxout.ctl.nc'
         aname = 'fluxout'
      CALL writevar(filen,aname,istart,icount,fluxout,nday, &
                  tweight,tdate,istat)

         filen = 'output/sfluxin.ctl.nc'
         aname = 'sfluxin'
      CALL writevar(filen,aname,istart,icount,fluxout,nday, &
                  tweight,tdate,istat)

      PRINT *
      PRINT *, ' ----------------------------------------'
      PRINT *, '    write monthly OK! ::: next step      '
      PRINT *, ' ----------------------------------------'
      PRINT *
      
      IF (istat .ne. 0) then
         WRITE(*,*) 'ERROR in wmonthly', istart, icount, nday
         stop 1
      ENDIF


      END SUBROUTINE W_OUTPUT0
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!---------------------------------------------------
!           WRITE RESTART
!---------------------------------------------------
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      SUBROUTINE W_RESTART(iwmon, incf, inrf, along, alatg, sflux, height, &
                          velocity, tmpdir, voll, warea, sfluxin, fluxout, dvoll)
      
      include 'netcdf.inc'

      INTEGER iwmon, incf, inrf, nlevel, ntime
      INTEGER i, j, jstart, jend, istart, iend, ii, jj
      REAL alev, atim
      REAL, DIMENSION(inrf) :: alatg 
      REAL, DIMENSION(incf) :: along 
      CHARACTER*80 :: filen,units, title,uidim,ujdim,ukdim,uldim
      CHARACTER*21 :: aname
      REAL, DIMENSION(incf,inrf) :: heightm, sflux, velocity, grid2, fluxout
      REAL, DIMENSION(incf,inrf) :: tmpdir, waream, sfluxin
      REAL(KIND=8), DIMENSION(incf,inrf) :: voll, dvoll 
      REAL, DIMENSION(incf,inrf) :: dummy1, dummy2

      nlevel = 1
      alev   = 1.
      uidim = 'degrees_east'
      ujdim = 'degrees_north'
      ukdim = 'level'
      uldim = 'days since 1990-01-15'
      ntime = 1
      atim  = float(ntime)


!
! WRITE instantaneous values to restart file. These can be used with restart
! turned on to get a seamless restart.

          WRITE(*,*) 'jstart/jend & istart/iend: '
          WRITE(*,*) jstart, jend, istart, iend

          WRITE(*,*) 'inrf/incf : '
          WRITE(*,*) inrf, incf
!----------------------------------------------------
          WRITE(*,*)
          WRITE(*,*)
          WRITE(*,*) '---   escrevendo GRID2  ---- '

         filen = 'output/restart/grid2.nc'
         title = 'land elevation msl'
         units = 'm'
         aname = 'topo$'
         CALL ini4dnc (grid2, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )
         WRITE(*,*)
         WRITE(*,*) '---   escrevendo GRID2 OK  ---- '
!----------------------------------------------------

          WRITE(*,*)
          WRITE(*,*)
          WRITE(*,*) '---   escrevendo HEIGTH  ---- '

         filen = 'output/restart/height.nc'
         title = 'river water height'
         units = 'm'
         aname = 'height$'
         CALL ini4dnc (height, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )
         WRITE(*,*)
         WRITE(*,*) '---   escrevendo HEIGHT OK  ---- '
!----------------------------------------------------

          WRITE(*,*)
          WRITE(*,*)
          WRITE(*,*) '---   escrevendo WAREA  ---- '

         filen = 'output/restart/warea.nc'
         title = 'flood area'
         units = 'fraction'
         aname = 'warea$'
         CALL ini4dnc (warea, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )
         WRITE(*,*)
         WRITE(*,*) '---   escrevendo WAREA OK  ---- '
!----------------------------------------------------

          WRITE(*,*)
          WRITE(*,*)
          WRITE(*,*) '---   escrevendo SFLUX  ---- '

         filen = 'output/restart/sflux.nc'
         title = 'river discharge'
         units = 'm3/s'
         aname = 'sflux$'
         CALL ini4dnc (sflux, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )
         WRITE(*,*)
         WRITE(*,*) '---   escrevendo SFLUX OK  ---- '
!
! place double precision variable into single precision for netcdf WRITE
!
!----------------------------------------------------
          WRITE(*,*)
          WRITE(*,*)
          WRITE(*,*) '---   escrevendo VOLL SIMPLE PRECISION  ---- '

          DO j = 1,inrf
              DO i = 1,incf
                  dummy1(i,j) = sngl(voll(i,j))
             ENDDO
          ENDDO

         filen = 'output/restart/voll.nc'
         title = 'river water volume'
         units = 'm3'
         aname = 'voll$'
         CALL ini4dnc (dummy1, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )
         WRITE(*,*)
         WRITE(*,*) '---   escrevendo VOLL OK  ---- '
!
! place double precision variable into single precision for netcdf WRITE
!----------------------------------------------------
          WRITE(*,*)
          WRITE(*,*)
          WRITE(*,*) '---   escrevendo DVOLL SIMPLE PRECISION  ---- '

          DO j = 1,inrf
              DO i = 1,incf
                 dummy2(i,j) = sngl(dvoll(i,j))
               ENDDO
          ENDDO
!
         filen = 'output/restart/dvoll.nc'
         title = 'river water change in volume'
         units = 'm3'
         aname = 'dvoll$'
         CALL ini4dnc (dummy2, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )
         WRITE(*,*)
         WRITE(*,*) '---   escrevendo DVOLL OK  ---- '
!----------------------------------------------------
        WRITE(*,*)
        WRITE(*,*)
        WRITE(*,*) '---   escrevendo SFLUXIN  ---- '

        filen = 'output/restart/sfluxin.nc'
        title = 'river flux into grid cell'
        units = 'm3/s'
        aname = 'sfluxin$'
        CALL ini4dnc (sfluxin, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )
        WRITE(*,*)
        WRITE(*,*) '---   escrevendo SFLUXIN OK  ---- '
!----------------------------------------------------
          WRITE(*,*)
          WRITE(*,*)
          WRITE(*,*) '---   escrevendo FLUXOUT  ---- '


        filen = 'output/restart/fluxout.nc'
        title = 'river flux OUT grid cell'
        units = 'm3/s'
        aname = 'fluxout$'
        CALL ini4dnc (fluxout, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )

        WRITE(*,*)
        WRITE(*,*) '---   escrevendo FLUXOUT OK  ---- '

      END SUBROUTINE W_RESTART
!############# IN TEST - NOT WORKING     ##########

      SUBROUTINE W_RESTART01!(imon,wday,inity,startyear,wyear,icmon, iday,ndaypm, & 
!                    incf, inrf, along, alatg, loni, lati, &
!                    sflux, heightm, velocity, tmpdir, vollm, waream)

      use comvar, only: iwmon,     &
                        incf,      &
                        inrf,      &
                        along,     &
                        alatg,     &
                        sflux,     &
                        heightm,   &
                        velocity,  &
                        tmpdir,    &
                        voll,      &
                        waream,    &
                        dvoll,     &
                        sfluxin,   &
                        fluxout,   &
                        mstep2

      
      include 'netcdf.inc'

      INTEGER nlevel, ntime
      INTEGER i, j, jstart, jend, istart, iend, ii, jj
      REAL alev, atim
      !REAL, DIMENSION(inrf) :: alatg 
      !REAL, DIMENSION(incf) :: along 
      CHARACTER*80 :: filen,units, title,uidim,ujdim,ukdim,uldim
      CHARACTER*21 :: aname
      !REAL, DIMENSION(incf,inrf) :: heightm, sflux, velocity, grid2
      !REAL, DIMENSION(incf,inrf) :: tmpdir, vollm, waream, dummy, sfluxin, dvollm
      !REAL(KIND=8), DIMENSION(incf,inrf) :: voll, dvoll, fluxout
      !REAL, DIMENSION(incf,inrf) :: voll, dvoll, fluxout
      REAL, DIMENSION (incf, inrf) :: dummy
            nlevel = 1
      alev   = 1.
      uidim = 'degrees_east'
      ujdim = 'degrees_north'
      ukdim = 'level'
      uldim = 'days since 1990-01-15'
      ntime = 1
      atim  = float(ntime)


!
! WRITE instantaneous values to restart file. These can be used with restart
! turned on to get a seamless restart.

          WRITE(*,*) 'jstart/jend & istart/iend: '
          WRITE(*,*) jstart, jend, istart, iend

          WRITE(*,*) 'inrf/incf : '
          WRITE(*,*)  inrf, incf



! #### --- WRITE RESTART GRID2

          WRITE(*,*)
          WRITE(*,*)
          WRITE(*,*) '---   escrevendo GRID2  ---- '

         filen = 'output/restart/grid2.nc'
         title = 'land elevation msl'
         units = 'm'
         aname = 'topo$'
         CALL ini4dnc (grid2, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )


! #### --- WRITE RESTART HEIGHT


          WRITE(*,*)
          WRITE(*,*)
          WRITE(*,*) '---   escrevendo HEIGHT  ---- '

         filen = 'output/restart/height.nc'
         title = 'river water height'
         units = 'm'
         aname = 'height$'
         CALL ini4dnc (height, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )

! #### --- WRITE RESTART WAREA

  
          WRITE(*,*)
          WRITE(*,*)
          WRITE(*,*) '---   escrevendo WAREA  ---- '

         filen = 'output/restart/warea.nc'
         title = 'flood area'
         units = 'fraction'
         aname = 'warea$'
         CALL ini4dnc (warea, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )
!
! #### --- WRITE RESTART SFLUX

          WRITE(*,*)
          WRITE(*,*)
          WRITE(*,*) '---   escrevendo SFLUX  ---- '

         filen = 'output/restart/sflux.nc'
         title = 'river discharge'
         units = 'm3/s'
         aname = 'sflux$'
         CALL ini4dnc (sflux, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )
!
! place double precision variable into single precision for netcdf WRITE
!
! #### --- WRITE RESTART VOLL

          WRITE(*,*)
          WRITE(*,*)
          WRITE(*,*) '---   escrevendo VOLL  ---- '
        DO j = 1,inrf
              DO i = 1,incf
                voll(ii,jj) = sngl(voll(ii,jj))
          ENDDO
        ENDDO


!         DO  j = jstart,jend
!           DO i = istart,iend
!             ii = i - (istart-1)
!           jj = j - (jstart-1)
!              voll(ii,jj) = sngl(voll(ii,jj))
!           ENDDO
!         ENDDO
!
         filen = 'output/restart/voll.nc'
         title = 'river water volume'
         units = 'm3'
         aname = 'voll$'
         CALL ini4dnc (voll, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )
!
! place double precision variable into single precision for netcdf WRITE
!
! #### --- WRITE RESTART DVOLL

          WRITE(*,*)
          WRITE(*,*)
          WRITE(*,*) '---   escrevendo DVOLL  ---- '
!
!         DO j = jstart,jend
!           DO i = istart,iend
!             ii = i - (istart-1)
!           jj = j - (jstart-1)
!              dvoll(ii,jj) = sngl(dvoll(ii,jj))
!           ENDDO
!         ENDDO
        DO j = 1,inrf
          DO i = 1,incf
                dvoll(ii,jj) = sngl(dvoll(ii,jj))
          ENDDO
        ENDDO
!
!          filen = 'output/restart/dvoll.nc'
!          title = 'river water change in volume'
!          units = 'm3'
!          aname = 'dvoll$'
!          CALL ini4dnc (dvoll, incf*inrf, incf, inrf, nlevel, units, &
!                        uidim, ujdim, ukdim, uldim, along, alatg, &
!                        alev, atim, aname, title, filen )
!
!
! #### --- WRITE RESTART SFLUXIN
!
          WRITE(*,*)
          WRITE(*,*)
          WRITE(*,*) '---   escrevendo SFLUXIN  ---- '

        filen = 'output/restart/sfluxin.nc'
        title = 'river flux into grid cell'
        units = 'm3/s'
        aname = 'sfluxin$'
        CALL ini4dnc (sfluxin, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )
!
! #### --- WRITE RESTART FLUXOUT
!
          WRITE(*,*)
          WRITE(*,*)
          WRITE(*,*) '---   escrevendo FLUXOUT  ---- '

        filen = 'output/restart/fluxout.nc'
        title = 'river flux OUT grid cell'
        units = 'm3/s'
        aname = 'fluxout$'
        CALL ini4dnc (fluxout, incf*inrf, incf, inrf, nlevel, units, &
                       uidim, ujdim, ukdim, uldim, along, alatg, &
                       alev, atim, aname, title, filen )

      END SUBROUTINE W_RESTART01
!######################################
