!        Generated by TAPENADE     (INRIA, Tropics team)
!  Version 2.1 - (Id: 1.33 vmp Stable - Wed Jan 12 13:35:07 MET 2005)
!  
! $Id: ice_flux.F,v 1.15 2004/02/25 17:35:13 eclare Exp $
!=======================================================================
!BOP
!
! !MODULE: ice_flux - flux variable declarations: coupler, diagnostic and internal
!
! !DESCRIPTION:
!
! Flux variable declarations; these include fields sent from the coupler
! ("in"), sent to the coupler ("out"), written to diagnostic history files
! ("diagnostic"), and used internally ("internal").
!
! !REVISION HISTORY:
!
! author Elizabeth C. Hunke, LANL
!
! !INTERFACE:
!
MODULE ICE_FLUX
  USE ice_constants
  USE ice_domain
  USE ice_kinds_mod
  USE ice_model_size
  USE ice_state
  REAL(KIND=DBL_KIND) :: evap(ilo:ihi, jlo:jhi), fhnet(ilo:ihi, jlo:jhi)&
&  , flat(ilo:ihi, jlo:jhi), flwout(ilo:ihi, jlo:jhi), fresh(ilo:ihi, &
&  jlo:jhi), fsalt(ilo:ihi, jlo:jhi), fsens(ilo:ihi, jlo:jhi), fswabs(&
&  ilo:ihi, jlo:jhi), fswthru(ilo:ihi, jlo:jhi), qref(ilo:ihi, jlo:jhi)&
&  , tref(ilo:ihi, jlo:jhi)
  REAL(KIND=DBL_KIND) :: congel(ilo:ihi, jlo:jhi), daidtt(ilo:ihi, jlo:&
&  jhi), dvidtt(ilo:ihi, jlo:jhi), fhnet_hist(ilo:ihi, jlo:jhi), frazil(&
&  ilo:ihi, jlo:jhi), fresh_hist(ilo:ihi, jlo:jhi), frz_onset(ilo:ihi, &
&  jlo:jhi), fsalt_hist(ilo:ihi, jlo:jhi), fswthru_hist(ilo:ihi, jlo:jhi&
&  ), meltb(ilo:ihi, jlo:jhi), meltl(ilo:ihi, jlo:jhi), meltt(ilo:ihi, &
&  jlo:jhi), mlt_onset(ilo:ihi, jlo:jhi), snoice(ilo:ihi, jlo:jhi)
  REAL(KIND=DBL_KIND) :: fsw(ilo:ihi, jlo:jhi), lhcoef(ilo:ihi, jlo:jhi)&
&  , shcoef(ilo:ihi, jlo:jhi), wind(ilo:ihi, jlo:jhi)
  REAL(KIND=DBL_KIND) :: flw(ilo:ihi, jlo:jhi), frain(ilo:ihi, jlo:jhi)&
&  , frzmlt(ilo:ihi, jlo:jhi), fsnow(ilo:ihi, jlo:jhi), pott(ilo:ihi, &
&  jlo:jhi), qa(ilo:ihi, jlo:jhi), rhoa(ilo:ihi, jlo:jhi), sss(ilo:ihi, &
&  jlo:jhi), sst(ilo:ihi, jlo:jhi), swidf(ilo:ihi, jlo:jhi), swidr(ilo:&
&  ihi, jlo:jhi), swvdf(ilo:ihi, jlo:jhi), swvdr(ilo:ihi, jlo:jhi), tair&
&  (ilo:ihi, jlo:jhi), tf(ilo:ihi, jlo:jhi), uatm(ilo:ihi, jlo:jhi), &
&  vatm(ilo:ihi, jlo:jhi), zlvl(ilo:ihi, jlo:jhi)
  REAL(KIND=DBL_KIND) :: daidtd(ilo:ihi, jlo:jhi), dvidtd(ilo:ihi, jlo:&
&  jhi), ss_tltx(ilo:ihi, jlo:jhi), ss_tlty(ilo:ihi, jlo:jhi), strairx(&
&  ilo:ihi, jlo:jhi), strairxt(ilo:ihi, jlo:jhi), strairy(ilo:ihi, jlo:&
&  jhi), strairyt(ilo:ihi, jlo:jhi), strocnx(ilo:ihi, jlo:jhi), strocnxt&
&  (ilo:ihi, jlo:jhi), strocny(ilo:ihi, jlo:jhi), strocnyt(ilo:ihi, jlo:&
&  jhi), strtltx(ilo:ihi, jlo:jhi), strtlty(ilo:ihi, jlo:jhi), uocn(ilo:&
&  ihi, jlo:jhi), vocn(ilo:ihi, jlo:jhi)

CONTAINS
  SUBROUTINE INIT_FLUX_ATM()
    IMPLICIT NONE
    INCLUDE 'DIFFSIZES.inc'
!  Hint: nbdirsmax should be the maximum number of differentiation directions
    INTEGER(KIND=INT_KIND) :: i, j
!
! !DESCRIPTION:
!
! Initialize all fluxes sent to coupler for use by the atm model
! and a few state quantities
!
! !REVISION HISTORY:
!
! author: Elizabeth C. Hunke, LANL
!
! !USES:
!
! !INPUT/OUTPUT PARAMETERS:
!
!
!EOP
!
! horizontal indices
!-----------------------------------------------------------------
! fluxes sent
!-----------------------------------------------------------------
!
!
    DO j=jlo,jhi
      DO i=ilo,ihi
!
! wind stress, T grid
        strairxt(i, j) = c0
        strairyt(i, j) = c0
        fsens(i, j) = c0
        flat(i, j) = c0
        fswabs(i, j) = c0
        flwout(i, j) = c0
        evap(i, j) = c0
!-----------------------------------------------------------------
! other miscellaneous fields
!-----------------------------------------------------------------
        IF (aice(i, j) .GT. puny) THEN
          tref(i, j) = c0
          qref(i, j) = c0
        ELSE
! over land and ice-free ocean
          tref(i, j) = tair(i, j)
          qref(i, j) = qa(i, j)
        END IF
!
! wind stress, U grid
        strairx(i, j) = c0
        strairy(i, j) = c0
      END DO
    END DO
  END SUBROUTINE INIT_FLUX_ATM
  SUBROUTINE INIT_FLUX_OCN()
    IMPLICIT NONE
    INCLUDE 'DIFFSIZES.inc'
!  Hint: nbdirsmax should be the maximum number of differentiation directions
    INTEGER(KIND=INT_KIND) :: i, j
!
! !DESCRIPTION:
!
! Initialize fluxes sent to coupler for use by the ocean model
!
! !REVISION HISTORY:
!
! author: Elizabeth C. Hunke, LANL
!
! !USES:
!
! !INPUT/OUTPUT PARAMETERS:
!
!
!EOP
!
! horizontal indices
!-----------------------------------------------------------------
! fluxes sent
!-----------------------------------------------------------------
!
!
    DO j=jlo,jhi
      DO i=ilo,ihi
        fresh(i, j) = c0
        fsalt(i, j) = c0
        fhnet(i, j) = c0
        fswthru(i, j) = c0
      END DO
    END DO
  END SUBROUTINE INIT_FLUX_OCN
  SUBROUTINE INIT_DIAGNOSTICS()
    IMPLICIT NONE
    INCLUDE 'DIFFSIZES.inc'
!  Hint: nbdirsmax should be the maximum number of differentiation directions
    INTEGER(KIND=INT_KIND) :: i, j
!
! !DESCRIPTION:
!
! Initialize diagnostic fields written to history files.
!
! !REVISION HISTORY:
!
! author: William H. Lipscomb, LANL
!
! !USES:
!
! !INPUT/OUTPUT PARAMETERS:
!
!
!
!
! horizontal indices
!
    DO j=jlo,jhi
      DO i=ilo,ihi
        congel(i, j) = c0
        frazil(i, j) = c0
        snoice(i, j) = c0
        meltt(i, j) = c0
        meltb(i, j) = c0
        meltl(i, j) = c0
! temporarily used for initial area
        daidtt(i, j) = aice(i, j)
! temporarily used for initial volume
        dvidtt(i, j) = vice(i, j)
        daidtd(i, j) = c0
        dvidtd(i, j) = c0
        fresh_hist(i, j) = c0
        fsalt_hist(i, j) = c0
        fhnet_hist(i, j) = c0
        fswthru_hist(i, j) = c0
      END DO
    END DO
  END SUBROUTINE INIT_DIAGNOSTICS
  SUBROUTINE MERGE_FLUXES(n, strxn, stryn, fsensn, flatn, fswabsn, &
&    flwoutn, evapn, trefn, qrefn, freshn, fsaltn, fhnetn, fswthrun)
    IMPLICIT NONE
    INCLUDE 'DIFFSIZES.inc'
!  Hint: nbdirsmax should be the maximum number of differentiation directions
    REAL(KIND=DBL_KIND), DIMENSION(ilo:ihi, jlo:jhi),INTENT(IN) :: evapn
    REAL(KIND=DBL_KIND), DIMENSION(ilo:ihi, jlo:jhi),INTENT(IN) :: &
&    fhnetn
    REAL(KIND=DBL_KIND), DIMENSION(ilo:ihi, jlo:jhi),INTENT(IN) :: flatn
    REAL(KIND=DBL_KIND), DIMENSION(ilo:ihi, jlo:jhi),INTENT(IN) :: &
&    flwoutn
    REAL(KIND=DBL_KIND), DIMENSION(ilo:ihi, jlo:jhi),INTENT(IN) :: &
&    freshn
    REAL(KIND=DBL_KIND), DIMENSION(ilo:ihi, jlo:jhi),INTENT(IN) :: &
&    fsaltn
    REAL(KIND=DBL_KIND), DIMENSION(ilo:ihi, jlo:jhi),INTENT(IN) :: &
&    fsensn
    REAL(KIND=DBL_KIND), DIMENSION(ilo:ihi, jlo:jhi),INTENT(IN) :: &
&    fswabsn
    REAL(KIND=DBL_KIND), DIMENSION(ilo:ihi, jlo:jhi),INTENT(IN) :: &
&    fswthrun
    INTEGER(KIND=INT_KIND),INTENT(IN) :: n
    REAL(KIND=DBL_KIND), DIMENSION(ilo:ihi, jlo:jhi),INTENT(IN) :: qrefn
    REAL(KIND=DBL_KIND), DIMENSION(ilo:ihi, jlo:jhi),INTENT(IN) :: strxn
    REAL(KIND=DBL_KIND), DIMENSION(ilo:ihi, jlo:jhi),INTENT(IN) :: stryn
    REAL(KIND=DBL_KIND), DIMENSION(ilo:ihi, jlo:jhi),INTENT(IN) :: trefn
    INTEGER(KIND=INT_KIND) :: i, j
!
! !DESCRIPTION:
!
! Aggregates flux information from all ice thickness categories
!
! !REVISION HISTORY:
!
! author: Elizabeth C. Hunke, LANL
!
! !USES:
!
!
! !INPUT/OUTPUT PARAMETERS:
!
! thickness category index
!
! air/ice zonal  strss,           (N/m**2)
! air/ice merdnl strss,           (N/m**2)
! sensible heat flx               (W/m**2)
! latent   heat flx               (W/m**2)
! shortwave absorbed heat flx     (W/m**2)
! upwd lw emitted heat flx        (W/m**2)
! evaporation                     (kg/m2/s)
! air tmp reference level         (K)
! air sp hum reference level      (kg/kg)
! fresh water flux to ocean       (kg/m2/s)
! salt flux to ocean              (kg/m2/s)
! actual ocn/ice heat flx         (W/m**2)
! sw radiation through ice bot    (W/m**2)
!
!EOP
!
! horizontal indices
!
    DO j=jlo,jhi
! atmo fluxes
      DO i=ilo,ihi
!
!
        strairxt(i, j) = strairxt(i, j) + strxn(i, j)*aicen(i, j, n)
        strairyt(i, j) = strairyt(i, j) + stryn(i, j)*aicen(i, j, n)
        fsens(i, j) = fsens(i, j) + fsensn(i, j)*aicen(i, j, n)
        flat(i, j) = flat(i, j) + flatn(i, j)*aicen(i, j, n)
        fswabs(i, j) = fswabs(i, j) + fswabsn(i, j)*aicen(i, j, n)
        flwout(i, j) = flwout(i, j) + (flwoutn(i, j)-(c1-emissivity)*flw&
&          (i, j))*aicen(i, j, n)
        evap(i, j) = evap(i, j) + evapn(i, j)*aicen(i, j, n)
        tref(i, j) = tref(i, j) + trefn(i, j)*aicen(i, j, n)
! ocean fluxes: update both coupler and history variables
        qref(i, j) = qref(i, j) + qrefn(i, j)*aicen(i, j, n)
!
!
        fresh(i, j) = fresh(i, j) + freshn(i, j)*aicen(i, j, n)
        fresh_hist(i, j) = fresh_hist(i, j) + freshn(i, j)*aicen(i, j, n&
&          )
        fsalt(i, j) = fsalt(i, j) + fsaltn(i, j)*aicen(i, j, n)
        fsalt_hist(i, j) = fsalt_hist(i, j) + fsaltn(i, j)*aicen(i, j, n&
&          )
        fhnet(i, j) = fhnet(i, j) + fhnetn(i, j)*aicen(i, j, n)
        fhnet_hist(i, j) = fhnet_hist(i, j) + fhnetn(i, j)*aicen(i, j, n&
&          )
        fswthru(i, j) = fswthru(i, j) + fswthrun(i, j)*aicen(i, j, n)
        fswthru_hist(i, j) = fswthru_hist(i, j) + fswthrun(i, j)*aicen(i&
&          , j, n)
      END DO
    END DO
  END SUBROUTINE MERGE_FLUXES
END MODULE ICE_FLUX