MODULE FLUXES 36,2
!@sum  FLUXES contains the fluxes between various components
!@auth Gavin Schmidt
!@ver  1.0
      USE MODEL_COM, only : im,jm,lm
      USE DOMAIN_DECOMP, ONLY : grid



      IMPLICIT NONE

!@var RUNOSI run off from sea/lake ice after surface (kg/m^2)
!@var ERUNOSI energy of run off from sea/lake ice after surface (J/m^2)
!@var SRUNOSI salt in run off from sea/lake ice after surface (kg/m^2)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: RUNOSI, ERUNOSI, SRUNOSI
!@var RUNPSI run off from sea/lake ice after precip (kg/m^2)
!@var ERUNPSI energy of run off from sea/lake ice after precip (J/m^2)
!@var SRUNPSI salt in run off from sea/lake ice after precip (kg/m^2)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: RUNPSI, SRUNPSI   ! ,ERUNPSI (not yet)
!@var RUNOE run off from earth (kg/m^2)
!@var ERUNOE energy of run off from earth (J/m^2)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: RUNOE, ERUNOE
C**** DMSI,DHSI,DSSI are fluxes for ice formation within water column
!@var DMSI mass flux of sea ice 1) open water and 2) under ice (kg/m^2)
!@var DHSI energy flux of sea ice 1) open water and 2) under ice (J/m^2)
!@var DSSI salt flux in sea ice 1) open water and 2) under ice (kg/m^2)
      REAL*8, ALLOCATABLE, DIMENSION(:,:,:) :: DMSI, DHSI, DSSI
!@var fmsi_io,fhsi_io,fssi_io basal ice-ocean fluxes (kg or J/m^2)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: fmsi_io,fhsi_io,fssi_io
!@var RUNOLI run off from land ice (kg/m^2) (Energy always=0)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: RUNOLI

C**** surface energy fluxes defined over type
!@param NSTYPE number of surface types for radiation purposes
      INTEGER, PARAMETER :: NSTYPE=4
!@var E0 net energy flux at surface for each type (J/m^2)
!@var E1 net energy flux at layer 1 for each type (J/m^2)
      REAL*8, ALLOCATABLE, DIMENSION(:,:,:) :: E0,E1
!@var EVAPOR evaporation over each type (kg/m^2) 
      REAL*8, ALLOCATABLE, DIMENSION(:,:,:) :: EVAPOR
!@var SOLAR absorbed solar radiation (J/m^2)
!@+   SOLAR(1)  absorbed by open water
!@+   SOLAR(2)  absorbed by ice
!@+   SOLAR(3)  absorbed by water under the ice
      REAL*8, ALLOCATABLE, DIMENSION(:,:,:) :: SOLAR

C**** Momemtum stresses are calculated as if they were over whole box
!@var DMUA,DMVA momentum flux from atmosphere for each type (kg/m s) 
!@+   On atmospheric A grid (tracer point)
      REAL*8, ALLOCATABLE, DIMENSION(:,:,:) :: DMUA,DMVA
!@var DMUI,DMVI momentum flux from sea ice to ocean (kg/m s)
!@+   On atmospheric C grid 
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: DMUI,DMVI
!@var UI2rho Ustar*2*rho ice-ocean friction velocity on atmospheric grid
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: UI2rho
!@var OGEOZA ocean surface height geopotential (m^2/s^2) (on ATM grid)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: OGEOZA

C**** currently saved - should be replaced by fluxed quantities
!@var DTH1,DQ1 heat/water flux from atmos. summed over type (C, kg/kg)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: DTH1,DQ1

!@var uflux1 surface turbulent u-flux (=-<uw>) 
!@var vflux1 surface turbulent v-flux (=-<vw>)
!@var tflux1 surface turbulent t-flux (=-<tw>)
!@var qflux1 surface turbulent q-flux (=-<qw>)
      real*8, allocatable, dimension(:,:) :: 
     &        uflux1,vflux1,tflux1,qflux1

C**** The E/FLOWO, E/S/MELTI, E/GMELT arrays are used to flux quantities 
C**** to the ocean that are not tied to the open water/ice covered 
C**** fractions. This is done separately for river flow, complete
C**** sea ice melt and iceberg/glacial melt.
!@var FLOWO,EFLOWO mass, energy from rivers into ocean (kg, J)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: FLOWO,EFLOWO
!@var MELTI,EMELTI,SMELTI mass,energy,salt from simelt into ocean (kg,J)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: MELTI,EMELTI,SMELTI
!@var GMELT,EGMELT mass,energy from glacial melt into ocean (kg,J)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: GMELT,EGMELT

!@var PREC precipitation (kg/m^2)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: PREC
!@var EPREC energy of preciptiation (J/m^2)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: EPREC
!@var PRECSS precipitation from super-saturation (kg/m^2)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: PRECSS

!@var GTEMP ground temperature (upper two levels) over surface type (C)
      REAL*8, ALLOCATABLE, DIMENSION(:,:,:,:) :: GTEMP
!@var SSS sea surface salinity on atmospheric grid (ppt)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: SSS
!@var MLHC ocean mixed layer heat capacity (J/m^2 C) 
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: MLHC
!@var UOSURF, VOSURF ocean surface velocity (Atm C grid) (m/s)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: UOSURF,VOSURF
!@var UISURF, VISURF dynamic ice surface velocity (Atm C grid) (m/s)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: UISURF,VISURF
!@var APRESS total atmos + sea ice pressure (at base of sea ice) (Pa)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: APRESS
!@var FWSIM fresh water sea ice mass (kg/m^2) (used for qflux model)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: FWSIM
!@var MSICNV fresh water sea ice mass convergence after advsi (kg/m^2)
!@+   (used for qflux model)
      REAL*8, ALLOCATABLE, DIMENSION(:,:) :: MSICNV





      END MODULE FLUXES


      SUBROUTINE ALLOC_FLUXES(grd_dum) 1,2
!@sum   Initializes FLUXES's arrays
!@auth  Rosalinda de Fainchtein
!@ver  1.0

      USE DOMAIN_DECOMP, ONLY : DYN_GRID
      USE FLUXES
      IMPLICIT NONE
      TYPE (DYN_GRID), INTENT(IN) :: grd_dum

      INTEGER :: I_0H, I_1H, J_1H, J_0H
      INTEGER :: IER

      INTEGER :: I,J,L


      I_0H = grd_dum%I_STRT_HALO
      I_1H = grd_dum%I_STOP_HALO
      J_0H = grd_dum%J_STRT_HALO
      J_1H = grd_dum%J_STOP_HALO

      !I-J arrays
      ALLOCATE( RUNOSI  ( I_0H:I_1H , J_0H:J_1H ), 
     &          ERUNOSI ( I_0H:I_1H , J_0H:J_1H ), 
     &          SRUNOSI ( I_0H:I_1H , J_0H:J_1H ),
     &          RUNPSI  ( I_0H:I_1H , J_0H:J_1H ), 
     &          SRUNPSI ( I_0H:I_1H , J_0H:J_1H ),
     &          RUNOE   ( I_0H:I_1H , J_0H:J_1H ), 
     &          ERUNOE  ( I_0H:I_1H , J_0H:J_1H ),
     &          fmsi_io ( I_0H:I_1H , J_0H:J_1H ),
     &          fhsi_io ( I_0H:I_1H , J_0H:J_1H ),
     &          fssi_io ( I_0H:I_1H , J_0H:J_1H ),
     &          RUNOLI  ( I_0H:I_1H , J_0H:J_1H ),
     &          DMUI    ( I_0H:I_1H , J_0H:J_1H ),
     &          DMVI    ( I_0H:I_1H , J_0H:J_1H ),
     &          UI2rho  ( I_0H:I_1H , J_0H:J_1H ),
     &          OGEOZA  ( I_0H:I_1H , J_0H:J_1H ),
     &          DTH1    ( I_0H:I_1H , J_0H:J_1H ),
     &          DQ1     ( I_0H:I_1H , J_0H:J_1H ),
     &   STAT=IER )
      ALLOCATE( uflux1  ( I_0H:I_1H , J_0H:J_1H ),
     &          vflux1  ( I_0H:I_1H , J_0H:J_1H ),
     &          tflux1  ( I_0H:I_1H , J_0H:J_1H ),
     &          qflux1  ( I_0H:I_1H , J_0H:J_1H ),
     &          FLOWO   ( I_0H:I_1H , J_0H:J_1H ),
     &          EFLOWO  ( I_0H:I_1H , J_0H:J_1H ),
     &          MELTI   ( I_0H:I_1H , J_0H:J_1H ),
     &          EMELTI  ( I_0H:I_1H , J_0H:J_1H ),
     &          SMELTI  ( I_0H:I_1H , J_0H:J_1H ),
     &          GMELT   ( I_0H:I_1H , J_0H:J_1H ),
     &          EGMELT  ( I_0H:I_1H , J_0H:J_1H ),
     &          PREC    ( I_0H:I_1H , J_0H:J_1H ),
     &          EPREC   ( I_0H:I_1H , J_0H:J_1H ),
     &          PRECSS  ( I_0H:I_1H , J_0H:J_1H ),
     &          SSS     ( I_0H:I_1H , J_0H:J_1H ),
     &          MLHC    ( I_0H:I_1H , J_0H:J_1H ),
     &          UOSURF  ( I_0H:I_1H , J_0H:J_1H ),
     &          VOSURF  ( I_0H:I_1H , J_0H:J_1H ),
     &          UISURF  ( I_0H:I_1H , J_0H:J_1H ),
     &          VISURF  ( I_0H:I_1H , J_0H:J_1H ),
     &          APRESS  ( I_0H:I_1H , J_0H:J_1H ),
     &          FWSIM   ( I_0H:I_1H , J_0H:J_1H ),
     &          MSICNV  ( I_0H:I_1H , J_0H:J_1H ),
     &   STAT=IER)


       !n-I-J arrays
       ALLOCATE( DMSI    (  2  , I_0H:I_1H , J_0H:J_1H ), 
     &           DHSI    (  2  , I_0H:I_1H , J_0H:J_1H ), 
     &           DSSI    (  2  , I_0H:I_1H , J_0H:J_1H ),
     &           SOLAR   (  3  , I_0H:I_1H , J_0H:J_1H ),
     &   STAT = IER)


      !I-J-: arrays
      ALLOCATE( E0      ( I_0H:I_1H , J_0H:J_1H , NSTYPE ),
     &          E1      ( I_0H:I_1H , J_0H:J_1H , NSTYPE ),
     &          EVAPOR  ( I_0H:I_1H , J_0H:J_1H , NSTYPE ),
     &          DMUA    ( I_0H:I_1H , J_0H:J_1H , NSTYPE ),
     &          DMVA    ( I_0H:I_1H , J_0H:J_1H , NSTYPE ),
     &   STAT = IER)


       !:,:,I,J array
       ALLOCATE( GTEMP( 2 , NSTYPE, I_0H:I_1H , J_0H:J_1H ),
     &   STAT = IER)
       GTEMP=0.   ! initialize at 0

!TRACERS_ON**********************







      END SUBROUTINE ALLOC_FLUXES