    MODULE comvar
!
    IMPLICIT NONE
!      include '/usr/include/netcdf.inc'
    PUBLIC
    SAVE
!
! ------
! com1d
! ------
!
      !INTEGER, PARAMETER :: nmons = 252 !number of timestep for the run
      !INTEGER :: nmons !number of timestep for the run
      INTEGER :: nmons2, nmons3 !number of timestep for the run

! Parameters
      REAL, PARAMETER :: circ=4.0024E+7 ! Earth circunference at equator
      REAL, PARAMETER :: pi = 3.1415926536
      REAL, PARAMETER :: rad = pi/180.

      REAL, DIMENSION(:,:,:), ALLOCATABLE :: drainin, & ! subsurface runoff
                                             runin, &   ! surface runoff
                                             prcpi, &   ! rain
                                             evapi      ! evaporation
      REAL, DIMENSION(:), ALLOCATABLE :: area !area of the gridpoint
      REAL, DIMENSION(801) :: cdf !  Cumulative Distribuction Function
      INTEGER :: iini,iend,jini,jend ! number of the first and last land point for the run area
      
! Allocated as (incf,inrf)
      REAL, DIMENSION(:,:), ALLOCATABLE :: sfluxin,   & !
                                           fluxout,   & !
                                           laream,    & !
                                           volb,      & !
                                           volr,      & 
                                           fluxoutw,  &
                                           sfluxinw,  &
                                           volt,      &
                                           basin2,    &
                                           areat,     &
                                           vollm,     &
                                           dvollm,    &
                                           grid2,     &
                                           stddev,    &
                                           velocity,  &
                                           temp,      &
                                           tempdl,    &
                                           tempdw,    &
                                           tempdr,    &
                                           af,        &
                                           ddist,     &
                                           larea,     &
                                           sflux,     &
                                           height,    &
                                           warea,     &
                                           waream,    &
                                           outdir,    &
                                           heightm,   &
                                           tmpdir,    &
                                           sinuos,    &
                                           dummy,     &
                                           uparea,    &
                                           wetmask,   &
                                           vollir,    &			!Height Correction
                                           warear,    &			!Height Correction
                                           dummy1,    &
                                           dummy2,    &
                                           prec


      REAL(KIND=8), DIMENSION(:,:), ALLOCATABLE :: dvoll,  &
                                                   outelv, &
                                                   voll,   &
                                                   dvolw,  &
                                                   volw !,   &
                                                   !grid2 !RACOSTA


!       REAL(KIND=4), DIMENSION(:,:), ALLOCATABLE :: dvoll, &
!                                                    outelv, &
!                                                    voll, &
!                                                    dvolw, &
!                                                    volw



      REAL, DIMENSION(:), ALLOCATABLE :: alat
      REAL, DIMENSION(:), ALLOCATABLE :: alon
      REAL, DIMENSION(:), ALLOCATABLE :: alatg 
      REAL, DIMENSION(:), ALLOCATABLE :: along 

! Area
      INTEGER :: inc, &
                 inr, &
                 incf, &
                 inrf

      REAL :: nmin, &
              dgper, &
              dy, &
              scale2

! input resolution
      REAL :: lati, latf, loni, lonf
      REAL :: longi, longf, latii, latif

! Parameters
!      REAL(KIND=8), PARAMETER :: grideps = 1.e-10
!      REAL(KIND=8), PARAMETER :: dveps = 1.e-10
!      REAL, PARAMETER :: circ=4.0024E+7
!      REAL, PARAMETER :: pi = 3.1415926536
!      REAL, PARAMETER :: rad = pi/180.


      INTEGER :: itime, iyear,imon,iday,icmon,iwmon, iwmon2
      INTEGER :: laket,cday,wday,restart,spin
      INTEGER :: wrtrestart, getrestart
      CHARACTER*8 :: startinput, finalinput, startrun, finalrun
      INTEGER :: tyear,wyear, wett, outfile
      REAL :: dx,phi,delt,ic,io,ioo, a, b, c, d
      REAL :: basarea
      
! Area for 5° 
      INTEGER :: j2start, j2end, i2start, i2end, nr, nc
! Area for 5'
      INTEGER:: j3start , j3end, i3start, i3end, nrf, ncf

!  prcpl     = rainfall onto lake or wetland surface m**3/s
!  evapl     = evaporation from lake or wetland surface m**3/s
!  rin       = runoff into runoff reservoir m**3/s
!  rout      = flow out of runoff reservoir m**3/s
!  bin       = drainage into baseflow reservoir m**3/s
!  bout      = drainage out of baseflow reservoir m**3/s
!  prcpi     = precip at 0.5 degree resolution m**3/s
!  evapi     = evap at 0.5 degree resolution m**3/s
!  runin     = surface runoff at 0.5 degree resolution m**3/s
!  drainin   = sub-surface runoff at 0.5 degree resolution m**3/s
!  area      = total surface area of each grid cell in m**2
!  grid2     = land elevation
!  stddev    = standard deviation of topo in cell from the mean (grid2)
!  larea     = prescribed surface water area (unitless, 1/0)
!  sfluxin   = flux into each cell at that timestep
!  fluxout   = flux out of a cell at that timestep
!  sfluxout  = mean monthly flux out of a cell (last year of run)
!  voll      = volume of water in river flow reservoir
!  volb      = volume of water in baseflow reservoir
!  effvel    = effective velocity calculated for the river
!  effvelf   = effective velocity calculated for the floodplain

! Readed in params.hyd

!  timed     = residence time of baseflow reservoir
!  timer     = residence time of runoff reservoir
!  effref    = reference effective velocity for the river
!  refper    = reference perimeter
!  effrefl   = reference effective velocity for the floodplain
!  flref     = tunable parameter for the speed of the water on the floodplain
!  irrout    = irrigation within the basin m**3/s


      REAL :: nspday
      REAL :: timed,timer,effref,effrefl,flref,refper
      REAL :: volchk,heightl,preh
      REAL :: xt,upref,z0,pz0,interval,pdf


      REAL, DIMENSION(:,:), ALLOCATABLE ::  sillh, outi, outj

      INTEGER ktstep, nyrs, startyear !k1,km,
      INTEGER, DIMENSION(8)  :: ioff,joff 
      INTEGER, DIMENSION(12) :: ndaypm
      REAL,    DIMENSION(12) :: irrwgt

! variables and constants for defining lat and lon values on output files
      REAL, DIMENSION(:), ALLOCATABLE ::latscale
      REAL, DIMENSION(:), ALLOCATABLE ::lonscale
      REAL lonfst,latfst,latend,lonend

! NetCDf variables
!      INTEGER nlevel,ntime,ierr
      INTEGER, DIMENSION(4) :: istart, nstart,icount
      INTEGER, DIMENSION(12) :: times
      CHARACTER*80 :: filen,units, title,uidim,ujdim,ukdim,uldim, name3d
      CHARACTER*21 :: aname
      REAL :: alev, atim
      INTEGER :: irrig
      INTEGER :: monthini, monthend
      INTEGER :: mstep2
      
      
!--------------------------------------------------------------
! set values for searching all gridcells around a given gridcell
! 8 possible directions
!
      DATA ioff /0,1,1,1,0,-1,-1,-1/ ! traditional way
      DATA joff /-1,-1,0,1,1,1,0,-1/
      DATA ndaypm /31,28,31,30,31,30,31,31,30,31,30,31/
      DATA irrwgt /.092,.07,.038,.054,.075,.145,.104,.081,.103,.083,.081,.074/
!      print *, 'NMONS no COMVAR: ', nmons

      END MODULE comvar
