! Library:phys $Id: hlcondfc.F90 5126 2007-05-11 16:21:54Z towil $

!option xopt(noeval)
subroutine hlcondfc (                                           &
!     integers
     &                  khor,         klev,          kstart,    &
     &                  kstop,        kstep,                    &
!     Prognostic variables
     &                  pt,           pq,                       &
!     Tendencies/fluxes
     &                  pdtdtin,      pdqdtin,       pdcwdin,   &
     &                  pdicedin,                               &
     &                  pdiftql,      pdiftqn,       pdiftq,    &
     &                  pdifts,                                 &
!     other parameters
     &                  pdpf                                    )

!     ===========================================================
!     PURPOSE:
!     Compute forcing to condensation processes 
!     due to dynamical advection and turbulence 
!     -------
!     METHOD: 
!     -----------------------------------------------------------
!     Fluxes of enthalpy and humidities are converted
!     to tendencies of temperature, specific humidity,
!     cloud water and cloud ice due to dynamics plus 
!     turbulence. This is done via local variable computations
!     in order not to modify turbulence fluxes needed later.  
!     -----------------------------------------------------------
!     AUTHOR: Bent H. Sass, DMI.

!     MODIFICATIONS: 
!     Original version 2005-03-22.
!     ===========================================================
!     1. DECLARATIONS.
 
!     1.1 MODULES USED:

use parkind1  ,only : jpim ,jprb
use yomhook   ,only : lhook,   dr_hook
use yhlconst  ,only : cpair, gravit, ccpq
use yhloption, only : lhlorig

!     1.2 ARGUMENT DECLARATIONS:
     
implicit none

integer(kind=jpim),intent(in)    ::  khor
integer(kind=jpim),intent(in)    ::  klev
integer(kind=jpim),intent(in)    ::  kstart
integer(kind=jpim),intent(in)    ::  kstop
integer(kind=jpim),intent(in)    ::  kstep

real(kind=jprb),   intent(in)    ::  pt(khor,klev)
real(kind=jprb),   intent(in)    ::  pq(khor,klev)
real(kind=jprb),   intent(inout) ::  pdtdtin(khor,klev)
real(kind=jprb),   intent(inout) ::  pdqdtin(khor,klev)
real(kind=jprb),   intent(inout) ::  pdcwdin(khor,klev)
real(kind=jprb),   intent(inout) ::  pdicedin(khor,klev)
real(kind=jprb),   intent(in)    ::  pdiftql(khor,0:klev)
real(kind=jprb),   intent(in)    ::  pdiftqn(khor,0:klev)
real(kind=jprb),   intent(in)    ::  pdiftq(khor,0:klev)
real(kind=jprb),   intent(in)    ::  pdifts(khor,0:klev)
real(kind=jprb),   intent(in)    ::  pdpf(khor,klev)


!     1.3 Other declarations 

real(kind=jprb)      :: zhook_handle
real(kind=jprb)      :: zdiftql(khor,klev)
real(kind=jprb)      :: zdiftqn(khor,klev)
real(kind=jprb)      :: zdifts(khor,klev)
real(kind=jprb)      :: zrk1(khor),zrk2(khor)
real(kind=jprb)      :: zdqcr
real(kind=jprb)      :: zindica
integer(kind=jpim)   :: jl,jk

      if(lhook) call dr_hook('hlcondfc',0,zhook_handle)

!     2.  Do the needed transformation related to the forcing of
!         condensation processes due to dynamics and turbulence. 

!     Fluxes to tendencies using input variables 
!     ( pdiftql, pdiftqn, pdifts, pdiftq ). For "pdifts" 
!     the output tendency is a temperature tendency.
!     The index of the output will be tendencies 
!     (1:nlev). 

!     Temperature tendency via enthalpy flux and specific 
!     humidity flux, and then subtract adjacent layer values 
!     to convert into temperature tendency. The uppermost 
!     temperature tendency must first be temporarily 
!     stored as another variable (zrk1).

!     NB ! Local variables must be used in the sense that
!     the input turbulence fluxes must not be overwritten.

      do jl=kstart,kstop
        zrk1(jl)= &
     &  (-pdifts(jl,1) +cpair*ccpq*pt(jl,1)*pdiftq(jl,1))*gravit/  &
     &  (pdpf(jl,1)*(cpair*(1. + ccpq*pq(jl,1))))
      enddo 

      do jk=2,klev
        do jl=kstart,kstop
          zrk2(jl)=(pdifts(jl,jk-1) -cpair*ccpq*pt(jl,jk-1)* &
     &    pdiftq(jl,jk-1))/  &
     &    (cpair*(1. + ccpq*pq(jl,jk-1)))
          zdifts(jl,jk)= &
     &    ((-pdifts(jl,jk) +cpair*ccpq*pt(jl,jk)*pdiftq(jl,jk))/  &
     &    (cpair*(1. + ccpq*pq(jl,jk))) +zrk2(jl) )*gravit/ &
     &    pdpf(jl,jk)  
        enddo
      enddo 

!     Store saved value zrk1() from uppermost level

      do jl=kstart,kstop
        zdifts(jl,1)=zrk1(jl)
      enddo 

!     Modify now humidity fluxes to tendies

!     Tendencies of specific humidity and cloud water
!     due to turbulence.

      do jk=1,klev
        do jl=kstart,kstop
          zdiftql(jl,jk)=(-pdiftql(jl,jk) +pdiftql(jl,jk-1))*  &
     &                  gravit/pdpf(jl,jk)
          zdiftqn(jl,jk)=(-pdiftqn(jl,jk) +pdiftqn(jl,jk-1))*  & 
     &                  gravit/pdpf(jl,jk)
        enddo
      enddo

!     ===========================================================
!     Moisture budget considerations:
!     ----------------------------------------------------------- 
!     The input tendency "pdqdtin(jk)" already contains the 
!     sum of humidity tendencies from dynamics and turbulence.
!     The convection scheme has some closure assumptions implying 
!     that the transferred input moisture tendency profile will in 
!     many cases not be changed (convection scheme is not active).
!     When, on the other hand,  the convection scheme is active
!     it redistributes the humidity in the vertical, acting with 
!     equal vertically integrated moisture sink at low levels as 
!     that of moisture source at upper levels and thus distribu-
!     ting the gain to condensates and humidity moistening.

!     Additional remarks.
!     -------------------
!     Currently the "pdqdtin" can be constrained below a
!     critical value "zdqcr" in order to assure numerical stability.
!     The current choice of 'zdqcr' is so high that  
!     this poses no 'meteorological' limitation to the scheme.

!     Moreover , the current output tendencies ( "pdcwdin", "pdicedin" 
!     and "pdtdtin" ) are zero. This is because there are zero dynamics 
!     input in the ALADIN system (December 2005).
!     The normal HIRLAM practice is to add both the 
!     tendencies from dynamics and turbulence to achieve 
!     preliminary updates of variables as input to condensation scheme. 
!     This has already been done in the input to the STRACO scheme.
!     Hence there is no need to do it here. 

!     The turbulence tendencies can be added also in the ALADIN case
!     ("zdiftql", "zdiftqn","zdifts" ) but are currently put to zero
!     through the indicator switch "zindica". 
!     ---------------------------------------------------------------
 
      zdqcr   = 1.0E-5_jprb
      zindica = 0.0_jprb

      do jk=1,klev
        do jl=kstart,kstop 
          pdqdtin (jl,jk)  =  max( min( pdqdtin(jl,jk),zdqcr ),-zdqcr)
          if ( .not. lhlorig ) then 
            pdcwdin (jl,jk)  =  pdcwdin(jl,jk)  + zindica*zdiftql(jl,jk)
            pdicedin(jl,jk)  =  pdicedin(jl,jk) + zindica*zdiftqn(jl,jk)
            pdtdtin (jl,jk)  =  pdtdtin(jl,jk)  + zindica*zdifts(jl,jk)
          endif 
        enddo 
      enddo 

!     ----------------------------------------------------------------

     if (lhook) call dr_hook('hlcondfc',1,zhook_handle)

end subroutine hlcondfc
