SUBROUTINE CLOUDSTTL ( KIDIA , KFDIA , KLON , KTDIA , KLEV, LDRAIN1D,&
 & PTSPHY,&
 & PAPHP15 , PAPP15  , PQM15   , PQS5   , PTM15,&
 & PLUDE5  , PLU5,&
 & PTENT5  , PTENQ5,&
 & PCLC5   , PQIWP5  , PQLWP5 ,PFPLSL5 , PFPLSN5,&
 & PFHPSL5 , PFHPSN5, PCOVPTOT5, &
 & PAPHP1 , PAPP1    , PQM1   , PQS   , PTM1,&
 & PLUDE  , PLU,&
 & PTENT  , PTENQ,&
 & PCLC   , PQIWP  , PQLWP ,PFPLSL , PFPLSN,&
 & PFHPSL , PFHPSN, PCOVPTOT )  

!**** *CLOUDSTTL*  - COMPUTES CLOUD COVER AND LIQUID WATER PATH
!                    AND LARGE-SCALE CONDENSATION
!                    (tangent linear)

!     PURPOSE.
!     --------
!         THIS ROUTINE COMPUTES CLOUD AMOUNTS WHICH ARE REQUIRED BY THE
!     RADIATION SCHEME FOR COMPUTATION OF THE FLUXES AND HE PHYSICAL
!     TENDENCIES OF THE TWO PROGNOSTIC VARIABLES T AND Q DUE TO
!     THE WATER PHASE CHANGES

!**   INTERFACE.
!     ----------
!              *CLOUDSTTL* IS CALLED FROM *CALLPARTL*

!     PARAMETER     DESCRIPTION                                   UNITS
!     ---------     -----------                                   -----

!     INPUT PARAMETERS (INTEGER):

!    *KIDIA*        START POINT
!    *KFDIA*        END POINT
!    *KLON*         NUMBER OF GRID POINTS PER PACKET
!    *KTDIA*        START OF THE VERTICAL LOOP
!    *KLEV*         NUMBER OF LEVELS

!    INPUT PARAMETERS (REAL):

!    *PTSPHY*       TIME STEP FOR THE PHYSICS                     S
!    *PQM15*        SPECIFIC HUMIDITY AT T-1                  (Trajectory)
!    *PQS5*         SATURATION SPECIFIC HUMIDITY              (Trajectory)
!    *PTM15*        TEMPERATURE AT T-1                        (Trajectory)
!    *PAPP15*       PROVISIONAL PRESSURE ON FULL LEVELS       (Trajectory)
!    *PAPHP15*      PRESSURE AT HALF LEVELS AT T-1            (Trajectory)
!    *PLUDE5*       DETRAINED LIQUID WATER                    (Trajectory)
!    *PLU5*         LIQUID WATER CONTENT IN UPDRAFTS          (Trajectory)
!    *PQM1*         SPECIFIC HUMIDITY AT T-1                     KG/KG
!    *PQS*          SATURATION SPECIFIC HUMIDITY                 KG/KG
!    *PTM1*         TEMPERATURE AT T-1                             K
!    *PAPHP1*       PRESSURE AT HALF LEVELS AT T-1                PA   
!    *PAPP1*        PROVISIONAL PRESSURE ON FULL LEVELS           PA
!    *PLUDE*        DETRAINED LIQUID WATER                       KG/(M3*S)
!    *PLU*          LIQUID WATER CONTENT IN UPDRAFTS             KG/KG

!    UPDATED PARAMETERS (REAL):
!    *PTENT5*       TEMPERATURE TENDENCY                      (Trajectory)
!    *PTENQ5*       MOISTURE TENDENCY                         (Trajectory)
!    *PTENT*        TEMPERATURE TENDENCY                          K/S
!    *PTENQ*        MOISTURE TENDENCY                            KG/(KG S)

!    OUTPUT PARAMETERS (REAL):

!    *PCLC5*        CLOUD COVER OF INDIVIDUAL LAYER
!    *PQIWP5*       ICE WATER CONTENT                         (Trajectory)    
!    *PQLWP5*       LIQUID WATER CONTENT                      (Trajectory)
!    *PFHPSL5*      ENTHALPY FLUX DUE TO LARGE SCALE RAIN     (Trajectory) 
!    *PFHPSN5*      ENTHALPY FLUX DUE TO LARGE SCALE SNOW     (Trajectory) 
!    *PFPLSL5*      LARGE SCALE RAIN FLUX                     (Trajectory)
!    *PFPLSN5*      LARGE SCALE SNOW FLUX                     (Trajectory)  
!    *PCLC*         CLOUD COVER OF INDIVIDUAL LAYER
!    *PQIWP*        ICE WATER CONTENT                             KG/KG
!    *PQLWP*        LIQUID WATER CONTENT                          KG/KG
!    *PFHPSL*       ENTHALPY FLUX DUE TO LARGE SCALE RAIN         J/(M2*S)
!    *PFHPSN*       ENTHALPY FLUX DUE TO LARGE SCALE SNOW         J/(M2*S)
!    *PFPLSL*       LARGE SCALE RAIN FLUX                        KG/(M2*S)
!    *PFPLSN*       LARGE SCALE SNOW FLUX                        KG/(M2*S)
!    *PCOVPTOT*     PRECIPITATION FRACTION IN EACH LAYER

!     METHOD.
!     -------

!     EXTERNALS.
!     ----------

!          NONE

!     REFERENCE.
!     ----------

!     AUTHOR.
!     -------
!     2002-10-01  M. Janiskova

!     MODIFICATIONS.
!     --------------
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Janiskova   13-Apr-2005 modified regularization
!        P.Lopez       21-Mar-2007 modified regularization of PCLC
!        P.Lopez       22-Oct-2007 Retuning of autoconversion coefficient
!        A.Geer        01-Oct-2007 LDRAIN1D name change to reflect usage
!        A.Geer        13-Apr-2010 2D precip fraction output for all-sky radiances 

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

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

USE YOMCST   , ONLY : RETV, RG  ,RCPD     ,&
 & RLVTT    ,RLSTT    ,RLMLT    ,RTT  
USE YOETHF   , ONLY : R2ES     ,R3LES    ,R3IES    ,R4LES    ,&
 & R4IES    ,R5LES    ,R5IES    ,R5ALVCP  ,R5ALSCP ,&
 & RALVDCP  ,RALSDCP  ,RTWAT    ,RTICE    ,RTICECU ,&
 & RTWAT_RTICE_R      ,RTWAT_RTICECU_R    ,RVTMP2 
USE YOECLD   , ONLY : CETA     
USE YOECLDP  , ONLY : RCLCRIT,  RKCONV   ,RLMIN, RPECONS
USE YOEPHLI  , ONLY : RLPTRC   
USE YOPHNC   , ONLY : LEVAPLS2
USE YOMNCL   , ONLY : LREGCL

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KLON 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KTDIA 
LOGICAL           ,INTENT(IN)    :: LDRAIN1D
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSPHY 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHP15(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPP15(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQM15(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQS5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTM15(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLUDE5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENT5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENQ5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PCLC5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQIWP5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQLWP5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFPLSL5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFPLSN5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFHPSL5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFHPSN5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PCOVPTOT5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHP1(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPP1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQS(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLUDE(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENT(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENQ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PCLC(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQIWP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQLWP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFPLSL(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFPLSN(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFHPSL(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFHPSN(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PCOVPTOT(KLON,KLEV) 
!     -----------------------------------------------------------------

!*       0.1   ARGUMENTS.
!              ----------

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

!*       0.2   LOCAL ARRAYS.
!              -------------

INTEGER(KIND=JPIM) :: JK, JL

!=======================================================================
!     TUNABLE CONSTANTS (to be moved to include files later)
!=======================================================================

! zscal is a scale factor that linearly reduces the variance between 
! qv=qv-crit and qv-qsat
! 0 = No scaling
! 1 = full scaling (i.e. variance=0 when qv=qsat)

REAL(KIND=JPRB) :: ZSCAL=0.9_JPRB

!=======================================================================

REAL(KIND=JPRB) :: ZTP15(KLON,KLEV), ZQP15(KLON,KLEV)
REAL(KIND=JPRB) :: ZLUDE5(KLON,KLEV), ZQLWC5(KLON,KLEV)
REAL(KIND=JPRB) :: ZRFL5(KLON), ZSFL5(KLON), ZDP5(KLON,KLEV), ZDR5(KLON)
REAL(KIND=JPRB) :: ZLSDCP5(KLON,KLEV), ZLFDCP5(KLON,KLEV), ZLVDCP5(KLON,KLEV)
REAL(KIND=JPRB) :: ZRFLN5(KLON), ZSFLN5(KLON),ZGDP5(KLON)
REAL(KIND=JPRB) :: ZFWAT5(KLON,KLEV), ZDQDT5(KLON), ZDTDT5(KLON)
REAL(KIND=JPRB) :: ZQCRIT5(KLON), ZCOVPCLR5(KLON)
REAL(KIND=JPRB) :: ZDQSDTEMP5(KLON), ZCORQS5(KLON), ZDTGDP5(KLON)
REAL(KIND=JPRB) :: ZQOLD5(KLON),ZPP5(KLON),ZDQ5(KLON), ZQLIM5(KLON)
REAL(KIND=JPRB) :: ZRFREEZE5(KLON,KLEV)

REAL(KIND=JPRB) :: ZTP1(KLON,KLEV), ZQP1(KLON,KLEV)
REAL(KIND=JPRB) :: ZLUDE(KLON,KLEV), ZQLWC(KLON,KLEV)
REAL(KIND=JPRB) :: ZRFL(KLON), ZSFL(KLON), ZDP(KLON,KLEV), ZDR(KLON)
REAL(KIND=JPRB) :: ZLSDCP(KLON,KLEV), ZLFDCP(KLON,KLEV), ZLVDCP(KLON,KLEV)
REAL(KIND=JPRB) :: ZRFLN(KLON), ZSFLN(KLON),ZGDP(KLON)
REAL(KIND=JPRB) :: ZFWAT(KLON,KLEV), ZDQDT(KLON), ZDTDT(KLON)
REAL(KIND=JPRB) :: ZQCRIT(KLON), ZCOVPCLR(KLON)
REAL(KIND=JPRB) :: ZDQSDTEMP(KLON), ZCORQS(KLON), ZDTGDP(KLON)
REAL(KIND=JPRB) :: ZQOLD(KLON),ZPP(KLON),ZDQ(KLON), ZQLIM(KLON)
REAL(KIND=JPRB) :: ZRFREEZE(KLON,KLEV)

REAL(KIND=JPRB) :: ZCRH(KLEV),ZSCALM(KLEV)

REAL(KIND=JPRB) :: ZOEALFA5, ZCLD5, ZLNEW5, ZPR5, ZD5, ZZ2S5
REAL(KIND=JPRB) :: ZCONS5, ZSNMLT5, ZZZ5, ZRN5, ZSN5
REAL(KIND=JPRB) :: ZOEALFAW5, ZALFAW5, ZFAC5, ZFACW5, ZFACI5, ZESDP5, ZCOR5
REAL(KIND=JPRB) :: ZFOEEW5, ZPRTOT5, ZDPR5, ZDPRSFL5, ZDPSSFL5, ZPRECLR5
REAL(KIND=JPRB) :: ZQE5, ZBETA5, ZB5, ZQPD5, ZQCD5, ZSQRT5, ZFWATR5
REAL(KIND=JPRB) :: ZRFREEZE25

REAL(KIND=JPRB) :: ZOEALFA, ZCLD, ZLNEW, ZPR, ZLCRIT, ZD, ZZ2S
REAL(KIND=JPRB) :: ZCONS, ZSNMLT, ZZZ, ZRN, ZSN
REAL(KIND=JPRB) :: ZOEALFAW, ZALFAW, ZFAC, ZFACW, ZFACI, ZESDP, ZCOR
REAL(KIND=JPRB) :: ZFOEEW, ZPRTOT, ZDPR, ZDPRSFL, ZDPSSFL, ZPRECLR
REAL(KIND=JPRB) :: ZQE, ZBETA, ZB, ZQPD, ZQCD, ZFWATR, ZRFREEZE2

REAL(KIND=JPRB) :: ZCKCODT, ZCONS2, ZCONS3, ZMELTP2, Z3ES, Z4ES, ZQMAX
REAL(KIND=JPRB) :: ZEPS1,ZEPS2,ZHUCOE,ZHUTIL,ZCKCODTA

REAL(KIND=JPRB) :: ZYYY, ZGAMMA

REAL(KIND=JPRB) :: ZCOVABOVE, ZCOVABOVE5

INTEGER(KIND=JPIM) :: INPCLO1,INPCLO2,IK,ICALL

LOGICAL :: LLO1, LLO2, LLFLAG(KLON)
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "cuadjtqstl.intfb.h"

!     ------------------------------------------------------------------
#include "fcttre.h"
!     ------------------------------------------------------------------

!*         1.     SET-UP INPUT QUANTITIES
!                 -----------------------

!*         1.1    Set-up tunning parameters

IF (LHOOK) CALL DR_HOOK('CLOUDSTTL',0,ZHOOK_HANDLE)

ZHUCOE=0.7_JPRB
ZHUTIL=0.9_JPRB

INPCLO1=1
INPCLO2=1

! set up constants required

!LLL ZCKCODT=3._JPRB*RKCONV*PTSPHY
ZCKCODT=2.0_JPRB*RKCONV*PTSPHY
ZCKCODTA=ZCKCODT/100._JPRB
ZCONS2 =1.0_JPRB/(PTSPHY*RG)
ZCONS3 =2.5e6_JPRB/1005._JPRB
ZMELTP2=RTT+2.0_JPRB

ZQMAX=0.5_JPRB
ZEPS1=1.E-12_JPRB
ZEPS2=1.E-10_JPRB

ZGAMMA=0.1_JPRB/SQRT(1.0_JPRB-0.95_JPRB)

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

!*         2.1    COMPUTE CRITICAL RELATIVE HUMIDITY AND RELATIVE HUMIDITY
!                 --------------------------------------------------------

DO JK=1,KLEV

! Critical relative humidity

  ZCRH(JK) = 0.85_JPRB-MAX( ZHUCOE*CETA(JK)**INPCLO1*&
   & (1.0_JPRB-CETA(JK))**INPCLO2*&
   & (1.85_JPRB+SQRT(ZHUTIL)*(CETA(JK)-0.5_JPRB)),ZEPS1)  
  ZSCALM(JK)=ZSCAL*MAX((CETA(JK)-0.2_JPRB),ZEPS1)**(0.2_JPRB)

  DO JL=KIDIA,KFDIA

! thermodynamic constants

    ZDP (JL,JK) = PAPHP1(JL,JK+1)-PAPHP1(JL,JK)
    ZDP5(JL,JK) = PAPHP15(JL,JK+1)-PAPHP15(JL,JK)
    ZZZ  = -RCPD*RVTMP2*PQM1(JL,JK)/(RCPD+RCPD*RVTMP2*PQM15(JL,JK))**2
    ZZZ5 =1.0_JPRB/(RCPD+RCPD*RVTMP2*PQM15(JL,JK))
    ZLFDCP (JL,JK) = RLMLT*ZZZ
    ZLFDCP5(JL,JK) = RLMLT*ZZZ5
    ZLSDCP (JL,JK) = RLSTT*ZZZ
    ZLSDCP5(JL,JK) = RLSTT*ZZZ5
    ZLVDCP (JL,JK) = RLVTT*ZZZ
    ZLVDCP5(JL,JK) = RLVTT*ZZZ5
    LLFLAG(JL)=.TRUE.
  ENDDO
ENDDO

! first guess values for T and q

DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    ZTP1 (JL,JK) = PTM1 (JL,JK) + PTSPHY*PTENT(JL,JK)
    ZTP15(JL,JK) = PTM15(JL,JK) + PTSPHY*PTENT5(JL,JK)
    ZQP1 (JL,JK) = PQM1 (JL,JK) + PTSPHY*PTENQ(JL,JK)
    ZQP15(JL,JK) = PQM15(JL,JK) + PTSPHY*PTENQ5(JL,JK)
  ENDDO
ENDDO

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

!*         2.2    INITIALIZATION OF CLOUD AND PRECIPITATION ARRAYS
!                 ------------------------------------------------

!       Clear cloud and freezing arrays

DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    PCLC (JL,JK) = 0.0_JPRB
    PCLC5(JL,JK) = 0.0_JPRB
    PQIWP (JL,JK) = 0.0_JPRB
    PQIWP5(JL,JK) = 0.0_JPRB
    PQLWP (JL,JK) = 0.0_JPRB
    PQLWP5(JL,JK) = 0.0_JPRB
    ZQLWC (JL,JK) = 0.0_JPRB
    ZQLWC5(JL,JK) = 0.0_JPRB
    ZRFREEZE (JL,JK)=0.0_JPRB
    ZRFREEZE5(JL,JK)=0.0_JPRB
    PCOVPTOT (JL,JK)=0.0_JPRB
    PCOVPTOT5(JL,JK)=0.0_JPRB
  ENDDO
ENDDO

!       Set to zero precipitation fluxes at the top

DO JL=KIDIA,KFDIA
  ZRFL (JL) = 0.0_JPRB
  ZRFL5(JL) = 0.0_JPRB
  ZSFL (JL) = 0.0_JPRB
  ZSFL5(JL) = 0.0_JPRB
  PFPLSL (JL,1) = 0.0_JPRB
  PFPLSL5(JL,1) = 0.0_JPRB
  PFPLSN (JL,1) = 0.0_JPRB
  PFPLSN5(JL,1) = 0.0_JPRB
  ZCOVPCLR (JL) = 0.0_JPRB
  ZCOVPCLR5(JL) = 0.0_JPRB
ENDDO

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

!*        3. COMPUTE LAYER CLOUD AMOUNTS
!            ---------------------------

! Large loop over KLEV 
! Calculates 
!   1. diagnostic CC and QL
!   2. Convective CC and QL
!   3. Rainfall 

DO JK=KTDIA,KLEV

!       3.1   INITIALIZATION

  DO JL=KIDIA,KFDIA

    ZDR (JL) = 0.0_JPRB
    ZDR5(JL) = 0.0_JPRB

!-----------------------------------
! calculate dqs/dT correction factor
!-----------------------------------

    ZOEALFAW  = 0.545_JPRB*0.17_JPRB*ZTP1(JL,JK) &
     & / COSH(0.17_JPRB*(ZTP15(JL,JK)-RLPTRC))**2  
    ZOEALFAW5 = 0.545_JPRB*(TANH(0.17_JPRB*(ZTP15(JL,JK)-RLPTRC))+1.0_JPRB)
    IF (ZTP15(JL,JK) < RTT) THEN
      ZALFAW  = ZOEALFAW
      ZALFAW5 = ZOEALFAW5
      Z3ES=R3IES
      Z4ES=R4IES
    ELSE
      ZALFAW  = 0.0_JPRB
      ZALFAW5 = 1.0_JPRB
      Z3ES=R3LES
      Z4ES=R4LES
    ENDIF
    ZFOEEW5 = R2ES*EXP(Z3ES*(ZTP15(JL,JK)-RTT)/(ZTP15(JL,JK)-Z4ES))
    ZFOEEW  = Z3ES*(RTT-Z4ES)*ZTP1(JL,JK)*ZFOEEW5 &
     & / (ZTP15(JL,JK)-Z4ES)**2  
    ZESDP  = ZFOEEW /PAPP15(JL,JK) - PAPP1(JL,JK) &
     & * ZFOEEW5/PAPP15(JL,JK)**2  
    ZESDP5 = ZFOEEW5/PAPP15(JL,JK)
    IF (ZESDP5 > ZQMAX) THEN
      ZESDP  = 0.0_JPRB
      ZESDP5 = ZQMAX
    ENDIF

    ZFACW  = -2.0_JPRB*R5LES*ZTP1(JL,JK)/(ZTP15(JL,JK)-R4LES)**3
    ZFACW5 = R5LES/((ZTP15(JL,JK)-R4LES)**2)
    ZFACI  = -2.0_JPRB*R5IES*ZTP1(JL,JK)/(ZTP15(JL,JK)-R4IES)**3
    ZFACI5 = R5IES/((ZTP15(JL,JK)-R4IES)**2)
    ZFAC  = ZALFAW5*ZFACW+ZFACW5*ZALFAW+(1.0_JPRB-ZALFAW5)*ZFACI &
     & - ZFACI5*ZALFAW   
    ZFAC5 = ZALFAW5*ZFACW5+(1.0_JPRB-ZALFAW5)*ZFACI5
    ZCOR  = RETV*ZESDP/(1.0_JPRB-RETV*ZESDP5)**2
    ZCOR5 = 1.0_JPRB/(1.0_JPRB-RETV*ZESDP5)
    ZDQSDTEMP (JL) = ZFAC5*ZCOR5*PQS(JL,JK)+ZFAC5*PQS5(JL,JK)*ZCOR &
     & + ZCOR5*PQS5(JL,JK)*ZFAC  
    ZDQSDTEMP5(JL) = ZFAC5*ZCOR5*PQS5(JL,JK)
    ZCORQS (JL) = ZCONS3*ZDQSDTEMP(JL)
    ZCORQS5(JL) = 1.0_JPRB+ZCONS3*ZDQSDTEMP5(JL)

! use clipped state

    IF (ZQP15(JL,JK)>PQS5(JL,JK)) THEN
      ZQLIM (JL) = PQS (JL,JK)
      ZQLIM5(JL) = PQS5(JL,JK)
    ELSE
      ZQLIM (JL) = ZQP1(JL,JK)
      ZQLIM5(JL) = ZQP15(JL,JK)
    ENDIF

! set up critical value of humidity

    ZQCRIT (JL) = ZCRH(JK)*PQS (JL,JK)
    ZQCRIT5(JL) = ZCRH(JK)*PQS5(JL,JK)
  ENDDO

! Replace BETA distribution with simple UNIFORM from Letreut & Li (90)

  DO JL=KIDIA,KFDIA
    IF (ZQP15(JL,JK) <= ZQCRIT5(JL)) THEN
      PCLC (JL,JK) = 0.0_JPRB
      PCLC5(JL,JK) = 0.0_JPRB
      ZQLWC (JL,JK) = 0.0_JPRB
      ZQLWC5(JL,JK) = 0.0_JPRB
    ELSEIF (ZQP15(JL,JK) >= PQS5(JL,JK)) THEN
      PCLC (JL,JK) = 0.0_JPRB
      PCLC5(JL,JK) = 1.0_JPRB
      ZQLWC (JL,JK) = (1.0_JPRB-ZSCALM(JK))*(PQS(JL,JK)-ZQCRIT(JL))
      ZQLWC5(JL,JK) = (1.0_JPRB-ZSCALM(JK))*(PQS5(JL,JK)-ZQCRIT5(JL))
    ELSE
      ZQPD  = PQS(JL,JK)-ZQP1(JL,JK)
      ZQPD5 = PQS5(JL,JK)-ZQP15(JL,JK)
      ZQCD  = PQS(JL,JK)-ZQCRIT(JL)
      ZQCD5 = PQS5(JL,JK)-ZQCRIT5(JL)

      ZSQRT5 = SQRT(ZQPD5/(ZQCD5-ZSCALM(JK)*(ZQP15(JL,JK) &
       & -ZQCRIT5(JL))))  
      PCLC5(JL,JK) = 1.0_JPRB-ZSQRT5
      PCLC (JL,JK) = -(0.5_JPRB/ZSQRT5) &
       & * (ZQPD*(ZQCD5-ZSCALM(JK)*(ZQP15(JL,JK)-ZQCRIT5(JL))) &
       & - ZQPD5*(ZQCD-ZSCALM(JK)*(ZQP1(JL,JK)-ZQCRIT(JL)))) &
       & / (ZQCD5-ZSCALM(JK)*(ZQP15(JL,JK)-ZQCRIT5(JL)))**2  

! Regularization of the linearized code:

      IF (LREGCL) THEN
        IF (PCLC5 (JL,JK)>0.2_JPRB .AND. PCLC5 (JL,JK)<0.7_JPRB) THEN
          ZYYY = -1.2_JPRB*PCLC5(JL,JK)+0.94_JPRB
          PCLC (JL,JK) = ZYYY * PCLC (JL,JK)
        ELSEIF (PCLC5 (JL,JK)>=0.7_JPRB .AND. PCLC5 (JL,JK)<0.95_JPRB) THEN
          PCLC (JL,JK) = 0.1_JPRB * PCLC (JL,JK)
        ELSEIF (PCLC5 (JL,JK)>=0.95_JPRB) THEN
          ZYYY = ZGAMMA * SQRT(1.0_JPRB - PCLC5(JL,JK))
          PCLC (JL,JK) = ZYYY * PCLC (JL,JK)
        ENDIF
      ENDIF
! end of regularization
     
      ZQLWC (JL,JK) = (ZSCALM(JK)*ZQPD+(1.0_JPRB-ZSCALM(JK))*ZQCD) &
       & * PCLC5(JL,JK)**2 &
       & + (ZSCALM(JK)*ZQPD5+(1.0_JPRB-ZSCALM(JK))*ZQCD5) &
       & * 2.0_JPRB*PCLC5(JL,JK)*PCLC(JL,JK)  
      ZQLWC5(JL,JK) = (ZSCALM(JK)*ZQPD5+(1.0_JPRB-ZSCALM(JK))*ZQCD5) &
       & * PCLC5(JL,JK)**2  
    ENDIF
  ENDDO

! Add convective component

  DO JL=KIDIA,KFDIA
    ZGDP (JL) = - RG*(PAPHP1(JL,JK+1)-PAPHP1(JL,JK)) &
     & / (PAPHP15(JL,JK+1)-PAPHP15(JL,JK))**2  
    ZGDP5(JL) = RG/(PAPHP15(JL,JK+1)-PAPHP15(JL,JK))
    ZLUDE (JL,JK) = PTSPHY*ZGDP5(JL)*PLUDE(JL,JK) &
     & + PTSPHY*PLUDE5(JL,JK)*ZGDP(JL)  
    ZLUDE5(JL,JK) = PLUDE5(JL,JK)*PTSPHY*ZGDP5(JL)
    IF (JK<KLEV) THEN
      LLO1=ZLUDE5(JL,JK)>=RLMIN.AND.PLU5(JL,JK+1)>=ZEPS2
    ELSE
      LLO1=.FALSE.
    ENDIF
    IF (LLO1) THEN
      PCLC (JL,JK) = PCLC(JL,JK) &
       & - PCLC(JL,JK)*(1.0_JPRB-EXP(-ZLUDE5(JL,JK)/PLU5(JL,JK+1))) &
       & + ((1.0_JPRB-PCLC5(JL,JK))/PLU5(JL,JK+1)) &
       & * EXP(-ZLUDE5(JL,JK)/PLU5(JL,JK+1))*ZLUDE(JL,JK) &
       & - ((1.0_JPRB-PCLC5(JL,JK))*ZLUDE5(JL,JK)/PLU5(JL,JK+1)**2) &
       & * EXP(-ZLUDE5(JL,JK)/PLU5(JL,JK+1))*PLU(JL,JK+1)  
      PCLC5(JL,JK) = PCLC5(JL,JK) &
       & + (1.0_JPRB-PCLC5(JL,JK)) &
       & * (1.0_JPRB-EXP(-ZLUDE5(JL,JK)/PLU5(JL,JK+1)))  
      ZQLWC (JL,JK) = ZQLWC (JL,JK)+ZLUDE (JL,JK)
      ZQLWC5(JL,JK) = ZQLWC5(JL,JK)+ZLUDE5(JL,JK)
    ENDIF
  ENDDO

! Calculate precipitation overlap. 
! Simple form based on Maximum Overlap.

  DO JL=KIDIA,KFDIA  
    IF (JK > KTDIA) THEN
      ZCOVABOVE5 = PCOVPTOT5(JL,JK-1) 
      ZCOVABOVE  = PCOVPTOT (JL,JK-1) 
    ELSE
      ZCOVABOVE5 = 0.0_JPRB
      ZCOVABOVE  = 0.0_JPRB
    ENDIF 
    IF (PCLC5(JL,JK) > ZCOVABOVE5) THEN ! total rain frac
      PCOVPTOT (JL,JK) = PCLC (JL,JK)
      PCOVPTOT5(JL,JK) = PCLC5(JL,JK)
    ELSE 
      PCOVPTOT (JL,JK) = ZCOVABOVE
      PCOVPTOT5(JL,JK) = ZCOVABOVE5
    ENDIF
    ZCOVPCLR (JL) = PCOVPTOT(JL,JK)-PCLC(JL,JK)
    ZCOVPCLR5(JL) = PCOVPTOT5(JL,JK)-PCLC5(JL,JK)
    IF (ZCOVPCLR5(JL) < 0.0_JPRB) THEN
      ZCOVPCLR (JL) = 0.0_JPRB
      ZCOVPCLR5(JL) = 0.0_JPRB
    ENDIF
  ENDDO

!*         3.3    CALCULATE PRECIPITATION

!    Melting of incoming snow

  DO JL=KIDIA,KFDIA
    IF (ZSFL5(JL) /= 0.0_JPRB) THEN
      ZCONS  = ZCONS2*(ZDP(JL,JK)*ZLFDCP5(JL,JK) &
       & - ZDP5(JL,JK)*ZLFDCP(JL,JK))/ZLFDCP5(JL,JK)**2  
      ZCONS5 = ZCONS2*ZDP5(JL,JK)/ZLFDCP5(JL,JK)
      IF ((ZTP15(JL,JK)-ZMELTP2) > 0.0_JPRB) THEN
        ZZ2S  = ZCONS5*ZTP1 (JL,JK)+ZCONS*(ZTP15(JL,JK)-ZMELTP2)
        ZZ2S5 = ZCONS5*(ZTP15(JL,JK)-ZMELTP2)
      ELSE
        ZZ2S  = 0.0_JPRB
        ZZ2S5 = 0.0_JPRB
      ENDIF
      IF (ZSFL5(JL) <= ZZ2S5) THEN
        ZSNMLT  = ZSFL (JL)
        ZSNMLT5 = ZSFL5(JL)
      ELSE
        ZSNMLT  = ZZ2S
        ZSNMLT5 = ZZ2S5
      ENDIF
      ZRFLN (JL) = ZRFL (JL)+ZSNMLT
      ZRFLN5(JL) = ZRFL5(JL)+ZSNMLT5
      ZSFLN (JL) = ZSFL (JL)-ZSNMLT
      ZSFLN5(JL) = ZSFL5(JL)-ZSNMLT5
      ZTP1 (JL,JK) = ZTP1 (JL,JK)-(ZSNMLT*ZCONS5-ZCONS*ZSNMLT5)/ZCONS5**2
      ZTP15(JL,JK) = ZTP15(JL,JK)-ZSNMLT5/ZCONS5
    ELSE
      ZRFLN (JL) = ZRFL (JL)
      ZRFLN5(JL) = ZRFL5(JL)
      ZSFLN (JL) = ZSFL (JL)
      ZSFLN5(JL) = ZSFL5(JL)
    ENDIF
  ENDDO

!    Diagnostic calculation of rain production

  DO JL=KIDIA,KFDIA
    IF (PCLC5(JL,JK) > ZEPS2) THEN
      IF (LEVAPLS2 .OR. LDRAIN1D) THEN
        ZLCRIT=RCLCRIT
      ELSE
        ZLCRIT = RCLCRIT*2._JPRB
      ENDIF
      ZCLD  = ZQLWC (JL,JK)/PCLC5(JL,JK) &
       & - ZQLWC5(JL,JK)*PCLC(JL,JK)/PCLC5(JL,JK)**2  
      ZCLD5 = ZQLWC5(JL,JK)/PCLC5(JL,JK)                ! in-cloud liquid 

      ZD5 = ZCKCODT*(1.0_JPRB-EXP(-(ZCLD5/ZLCRIT)**2))

!    Regularization of the linearized code:

      IF (LREGCL) THEN
        ZD  = (2.0_JPRB*ZCKCODTA/ZLCRIT**2)*EXP(-(ZCLD5/ZLCRIT)**2) &
         & * ZCLD5*ZCLD  
      ELSE
        ZD  = (2.0_JPRB*ZCKCODT/ZLCRIT**2)*EXP(-(ZCLD5/ZLCRIT)**2) &
         & * ZCLD5*ZCLD  
      ENDIF
!    end of regularization

      ZLNEW  = ZCLD5*EXP(-ZD5)*PCLC(JL,JK) &
       & + PCLC5(JL,JK)*EXP(-ZD5)*ZCLD &
       & - PCLC5(JL,JK)*ZCLD5*EXP(-ZD5)*ZD  
      ZLNEW5 = PCLC5(JL,JK)*ZCLD5*EXP(-ZD5)
      ZPR  = ZQLWC (JL,JK)-ZLNEW
      ZPR5 = ZQLWC5(JL,JK)-ZLNEW5
      ZQLWC (JL,JK) = ZQLWC (JL,JK)-ZPR
      ZQLWC5(JL,JK) = ZQLWC5(JL,JK)-ZPR5
    ELSE
      ZPR  = 0.0_JPRB
      ZPR5 = 0.0_JPRB
    ENDIF

!    New precipitation

    ZDR (JL) = ZDR(JL)+ZCONS2*ZDP5(JL,JK)*ZPR &
     & + ZCONS2*ZPR5*ZDP(JL,JK)  
    ZDR5(JL) = ZDR5(JL)+ZCONS2*ZDP5(JL,JK)*ZPR5

    ZOEALFA  = 0.545_JPRB*0.17_JPRB*ZTP1(JL,JK) &
     & / COSH(0.17_JPRB*(ZTP15(JL,JK)-RLPTRC))**2  
    ZOEALFA5 = 0.545_JPRB*(TANH(0.17_JPRB*(ZTP15(JL,JK)-RLPTRC))+1.0_JPRB)
    IF (ZTP15(JL,JK) < RTT) THEN
      ZFWAT (JL,JK) = ZOEALFA
      ZFWAT5(JL,JK) = ZOEALFA5
    ELSE
      ZFWAT (JL,JK) = 0.0_JPRB
      ZFWAT5(JL,JK) = 1.0_JPRB
    ENDIF

! Rain fraction (different from cloud liquid water fraction!)
    IF (ZTP15(JL,JK) < RTT) THEN
      ZRFREEZE5(JL,JK)=ZFWAT5(JL,JK)*ZDR5(JL)
      ZRFREEZE (JL,JK)=ZFWAT (JL,JK)*ZDR5(JL) + ZFWAT5(JL,JK)*ZDR (JL)
      ZFWATR5=0.0_JPRB
      ZFWATR =0.0_JPRB
    ELSE
      ZFWATR5=1.0_JPRB
      ZFWATR =0.0_JPRB
    ENDIF 

    ZRN  = ZFWATR5*ZDR(JL) + ZDR5(JL)*ZFWATR
    ZRN5 = ZFWATR5*ZDR5(JL)
    ZSN  = -ZDR5(JL)*ZFWATR + (1.0_JPRB-ZFWATR5)*ZDR(JL)
    ZSN5 = (1.0_JPRB-ZFWATR5)*ZDR5(JL)
    ZRFLN (JL) = ZRFLN (JL)+ZRN
    ZRFLN5(JL) = ZRFLN5(JL)+ZRN5
    ZSFLN (JL) = ZSFLN (JL)+ZSN
    ZSFLN5(JL) = ZSFLN5(JL)+ZSN5

!   Precip evaporation

    ZPRTOT  = ZRFLN (JL)+ZSFLN (JL)
    ZPRTOT5 = ZRFLN5(JL)+ZSFLN5(JL)
    LLO2=ZPRTOT5>ZEPS2 .AND. ZCOVPCLR5(JL)>ZEPS2 .AND. (LEVAPLS2 .OR. LDRAIN1D)
    IF (LLO2) THEN
      ZPRECLR  = (ZPRTOT5*ZCOVPCLR(JL)+ZCOVPCLR5(JL)*ZPRTOT) &
       & / PCOVPTOT5(JL,JK) &
       & - ZPRTOT5*ZCOVPCLR5(JL)*PCOVPTOT(JL,JK) &
       & / PCOVPTOT5(JL,JK)**2  
      ZPRECLR5 = ZPRTOT5*ZCOVPCLR5(JL)/PCOVPTOT5(JL,JK)

!     This is the humidity in the moistest zcovpclr region

      ZQE  = PQS(JL,JK) - ((PQS5(JL,JK)-ZQLIM5(JL))*ZCOVPCLR(JL) &
       & + ZCOVPCLR5(JL)*PQS(JL,JK)-ZCOVPCLR5(JL)*ZQLIM(JL)) &
       & / (1.0_JPRB-PCLC5(JL,JK))**2 &
       & - 2.0_JPRB*(PQS5(JL,JK)-ZQLIM5(JL))*ZCOVPCLR5(JL) &
       & * PCLC(JL,JK)/(1.0_JPRB-PCLC5(JL,JK))**3  
      ZQE5 = PQS5(JL,JK)-(PQS5(JL,JK)-ZQLIM5(JL))*ZCOVPCLR5(JL)&
       & / (1.0_JPRB-PCLC5(JL,JK))**2  

      ZBETA  = 0.5777_JPRB*(RG*RPECONS/5.09E-3_JPRB) &
       & * (5.09E-3_JPRB*ZCOVPCLR5(JL)/(ZPRECLR5 &
       & * SQRT(PAPP15(JL,JK)/PAPHP15(JL,KLEV+1))))**0.4223_JPRB &
       & * ((SQRT(PAPP15(JL,JK)/PAPHP15(JL,KLEV+1))*ZPRECLR &
       & + 0.5_JPRB*ZPRECLR5*PAPP1(JL,JK) &
       & / SQRT(PAPP15(JL,JK)*PAPHP15(JL,KLEV+1)) &
       & - 0.5_JPRB*ZPRECLR5  &
       & * SQRT(PAPP15(JL,JK)/PAPHP15(JL,KLEV+1)) &
       & * PAPHP1(JL,KLEV+1)/PAPHP15(JL,KLEV+1)) &
       & / ZCOVPCLR5(JL) &
       & - ZPRECLR5*SQRT(PAPP15(JL,JK)/PAPHP15(JL,KLEV+1)) &
       & * ZCOVPCLR(JL) / ZCOVPCLR5(JL)**2)  

      ZBETA5=RG*RPECONS*(SQRT(PAPP15(JL,JK) &
       & /PAPHP15(JL,KLEV+1))/5.09E-3_JPRB*ZPRECLR5 &
       & /ZCOVPCLR5(JL))**0.5777_JPRB  

!     implicit solution:

      ZB  = PTSPHY*((PQS5(JL,JK)-ZQE5)*ZBETA+ZBETA5*PQS(JL,JK) &
       & - ZBETA5*ZQE)/(1.0_JPRB+ZBETA5*PTSPHY*ZCORQS5(JL)) &
       & - (PTSPHY**2)*ZBETA5*(PQS5(JL,JK)-ZQE5) &
       & * (ZBETA5*ZCORQS(JL)+ZCORQS5(JL)*ZBETA) &
       & / (1.0_JPRB+ZBETA5*PTSPHY*ZCORQS5(JL))**2  
      ZB5 = PTSPHY*ZBETA5*(PQS5(JL,JK)-ZQE5) &
       & / (1.0_JPRB+ZBETA5*PTSPHY*ZCORQS5(JL))  

!     exact solution:

!     ZEXP5 = EXP(-ZBETA5*ZCORQS5(JL)*PTSPHY)
!     ZB  = (ZEXP5*(-ZCORQS5(JL)*(PQS(JL,JK)-ZQE) &
!      &  + (PQS5(JL,JK)-ZQE5) &
!      &  * (PTSPHY*ZCORQS5(JL)*ZBETA5*ZCORQS(JL) &
!      &  + PTSPHY*ZCORQS5(JL)*ZCORQS5(JL)*ZBETA + ZCORQS(JL))) &
!      &  + ZCORQS5(JL)*(PQS(JL,JK)-ZQE) &
!      &  - (PQS5(JL,JK)-ZQE5)*ZCORQS(JL)) &
!      &  / ZCORQS5(JL)**2
!     ZB5 =(PQS5(JL,JK)-ZQE5)*(_ONE_-ZEXP5)/ZCORQS5(JL)

      ZDTGDP (JL) = - PTSPHY*RG*(PAPHP1(JL,JK+1)-PAPHP1(JL,JK)) &
       & / (PAPHP15(JL,JK+1)-PAPHP15(JL,JK))**2  
      ZDTGDP5(JL) =PTSPHY*RG/(PAPHP15(JL,JK+1)-PAPHP15(JL,JK))
      ZDPR  = (ZCOVPCLR5(JL)*ZB+ZB5*ZCOVPCLR(JL)) &
       & / ZDTGDP5(JL) &
       & - ZCOVPCLR5(JL)*ZB5*ZDTGDP(JL)/ZDTGDP5(JL)**2  
      ZDPR5 = ZCOVPCLR5(JL)*ZB5/ZDTGDP5(JL)
      IF (ZDPR5 > ZPRECLR5) THEN
        ZDPR  = ZPRECLR
        ZDPR5 = ZPRECLR5
      ENDIF
      ZPRECLR  = ZPRECLR-ZDPR  ! take away from clr sky flux
      ZPRECLR5 = ZPRECLR5-ZDPR5
      IF (ZPRECLR5 <= 0.0_JPRB) THEN !reset
        PCOVPTOT (JL,JK) = PCLC(JL,JK)
        PCOVPTOT5(JL,JK) = PCLC5(JL,JK)
      ENDIF

! warm proportion
      ZDPRSFL  = (ZDPR5*ZRFLN(JL)+ZRFLN5(JL)*ZDPR)/ZPRTOT5 &
       & - ZDPR5*ZRFLN5(JL)*ZPRTOT/ZPRTOT5**2  
      ZDPRSFL5 = ZDPR5*ZRFLN5(JL)/ZPRTOT5     
      ZRFLN (JL) = ZRFLN (JL)-ZDPRSFL
      ZRFLN5(JL) = ZRFLN5(JL)-ZDPRSFL5
     
! ice proportion
      ZDPSSFL  = (ZDPR5*ZSFLN(JL)+ZSFLN5(JL)*ZDPR)/ZPRTOT5 &
       & - ZDPR5*ZSFLN5(JL)*ZPRTOT/ZPRTOT5**2  
      ZDPSSFL5 = ZDPR5*ZSFLN5(JL)/ZPRTOT5        
      ZSFLN (JL) = ZSFLN (JL)-ZDPSSFL
      ZSFLN5(JL) = ZSFLN5(JL)-ZDPSSFL5
    ENDIF

!   Partitioning for cloud liquid and ice water content

    PQLWP (JL,JK) = ZFWAT5(JL,JK)*ZQLWC(JL,JK) &
     & + ZQLWC5(JL,JK)*ZFWAT(JL,JK)  
    PQLWP5(JL,JK) = ZFWAT5(JL,JK)*ZQLWC5(JL,JK)
    PQIWP (JL,JK) = -ZQLWC5(JL,JK)*ZFWAT(JL,JK) &
     & + (1.0_JPRB-ZFWAT5(JL,JK))*ZQLWC(JL,JK)  
    PQIWP5(JL,JK) = (1.0_JPRB-ZFWAT5(JL,JK))*ZQLWC5(JL,JK)
  ENDDO

!   Incrementation of T and Q and fluxes' swap

  DO JL=KIDIA,KFDIA
    ZDQDT (JL) = -RG*(((ZRFLN(JL)-ZRFL(JL))+(ZSFLN(JL)-ZSFL(JL))) &
     & * ZDP5(JL,JK)-ZDP(JL,JK)*((ZRFLN5(JL)-ZRFL5(JL)) &
     & + (ZSFLN5(JL)-ZSFL5(JL))))/ZDP5(JL,JK)**2 &
     & + PLUDE5(JL,JK)*ZGDP(JL) + ZGDP5(JL)*PLUDE(JL,JK)  
    ZDQDT5(JL) = -((ZRFLN5(JL)-ZRFL5(JL))+(ZSFLN5(JL)-ZSFL5(JL)))&
     & * (RG/ZDP5(JL,JK))+PLUDE5(JL,JK)*ZGDP5(JL)  

    ZDTDT (JL) = ((ZLVDCP5(JL,JK)*(ZRFLN(JL)-ZRFL(JL))+ZLSDCP5(JL,JK) &
     & * (ZSFLN(JL)-ZSFL(JL)) &
     & + ZLVDCP(JL,JK)*(ZRFLN5(JL)-ZRFL5(JL))+ZLSDCP(JL,JK) &
     & * (ZSFLN5(JL)-ZSFL5(JL)))*ZDP5(JL,JK)-ZDP(JL,JK) &
     & * (ZLVDCP5(JL,JK)*(ZRFLN5(JL)-ZRFL5(JL))+ZLSDCP5(JL,JK) &
     & * (ZSFLN5(JL)-ZSFL5(JL))))*RG/ZDP5(JL,JK)**2 &
     & - ZGDP5(JL)*(ZFWAT5(JL,JK)*ZLVDCP5(JL,JK) &
     & + (1.0_JPRB-ZFWAT5(JL,JK))*ZLSDCP5(JL,JK))*PLUDE(JL,JK) &
     & - PLUDE5(JL,JK)*(ZFWAT5(JL,JK)*ZLVDCP5(JL,JK) &
     & + (1.0_JPRB-ZFWAT5(JL,JK))*ZLSDCP5(JL,JK))*ZGDP(JL) &
     & - PLUDE5(JL,JK)*ZGDP5(JL) &
     & * (ZFWAT5(JL,JK)*ZLVDCP(JL,JK) &
     & + (ZLVDCP5(JL,JK)-ZLSDCP5(JL,JK))*ZFWAT(JL,JK) &
     & + (1.0_JPRB-ZFWAT5(JL,JK))*ZLSDCP(JL,JK)) &
     & + (ZLSDCP (JL,JK)-ZLVDCP (JL,JK))*ZRFREEZE5(JL,JK)*ZGDP5(JL) & 
     & + (ZLSDCP5(JL,JK)-ZLVDCP5(JL,JK))*ZRFREEZE (JL,JK)*ZGDP5(JL) & 
     & + (ZLSDCP5(JL,JK)-ZLVDCP5(JL,JK))*ZRFREEZE5(JL,JK)*ZGDP (JL)
    ZDTDT5(JL) = (ZLVDCP5(JL,JK)*(ZRFLN5(JL)-ZRFL5(JL))+ZLSDCP5(JL,JK) &
     & * (ZSFLN5(JL)-ZSFL5(JL)))*(RG/ZDP5(JL,JK)) &
     & - PLUDE5(JL,JK)*ZGDP5(JL) &
     & * (ZFWAT5(JL,JK)*ZLVDCP5(JL,JK) &
     & + (1.0_JPRB-ZFWAT5(JL,JK))*ZLSDCP5(JL,JK)) &  
     & + (ZLSDCP5(JL,JK)-ZLVDCP5(JL,JK))*ZRFREEZE5(JL,JK)*ZGDP5(JL)

! first guess T and Q
    ZTP1 (JL,JK) = ZTP1 (JL,JK) + PTSPHY*ZDTDT (JL)
    ZTP15(JL,JK) = ZTP15(JL,JK) + PTSPHY*ZDTDT5(JL)
    ZQP1 (JL,JK) = ZQP1 (JL,JK) + PTSPHY*ZDQDT (JL)
    ZQP15(JL,JK) = ZQP15(JL,JK) + PTSPHY*ZDQDT5(JL)

    ZPP (JL) = PAPP1 (JL,JK)
    ZPP5(JL) = PAPP15(JL,JK)
    ZQOLD (JL) = ZQP1 (JL,JK)
    ZQOLD5(JL) = ZQP15(JL,JK)
  ENDDO

! clipping of final qv

  IK=JK
  ICALL=0
  CALL CUADJTQSTL ( KIDIA, KFDIA, KLON, KLEV, IK,&
   & ZPP5 , ZTP15 , ZQP15,&
   & ZPP  , ZTP1  , ZQP1 , LLFLAG, ICALL  )  

  DO JL=KIDIA,KFDIA
    IF ((ZQOLD5(JL)-ZQP15(JL,JK)) >= 0.0_JPRB) THEN
      ZDQ (JL) = ZQOLD (JL)-ZQP1 (JL,JK)
      ZDQ5(JL) = ZQOLD5(JL)-ZQP15(JL,JK) 
    ELSE
      ZDQ (JL) = 0.0_JPRB
      ZDQ5(JL) = 0.0_JPRB
    ENDIF
    ZDR (JL) = ZCONS2*(ZDP5(JL,JK)*ZDQ(JL)+ZDQ5(JL)*ZDP(JL,JK))
    ZDR5(JL) = ZCONS2*ZDP5(JL,JK)*ZDQ5(JL)

! Update rain fraction and freezing.
! Note: impact of new temperature ZTP1 on ZFWAT is neglected here.
    IF (ZTP15(JL,JK) < RTT) THEN
      ZRFREEZE25=ZFWAT5(JL,JK)*ZDR5(JL)
      ZRFREEZE2 =ZFWAT (JL,JK)*ZDR5(JL) + ZFWAT5(JL,JK)*ZDR (JL)
      ZFWATR5=0.0_JPRB
      ZFWATR =0.0_JPRB
    ELSE
      ZRFREEZE25=0.0_JPRB
      ZRFREEZE2 =0.0_JPRB
      ZFWATR5=1.0_JPRB
      ZFWATR =0.0_JPRB
    ENDIF 

    ZRN  = ZFWATR5*ZDR (JL)+ZDR5(JL)*ZFWATR
    ZRN5 = ZFWATR5*ZDR5(JL)
    ZSN  = (1.0_JPRB-ZFWATR5)*ZDR (JL)-ZDR5(JL)*ZFWATR
    ZSN5 = (1.0_JPRB-ZFWATR5)*ZDR5(JL)
    
    ZRFLN (JL) = ZRFLN (JL)+ZRN
    ZRFLN5(JL) = ZRFLN5(JL)+ZRN5
    ZSFLN (JL) = ZSFLN (JL)+ZSN
    ZSFLN5(JL) = ZSFLN5(JL)+ZSN5
    ZRFREEZE5(JL,JK)=ZRFREEZE5(JL,JK)+ZRFREEZE25
    ZRFREEZE (JL,JK)=ZRFREEZE (JL,JK)+ZRFREEZE2
  ENDDO

  DO JL=KIDIA,KFDIA
    ZDQDT (JL) = -RG*(((ZRFLN(JL)-ZRFL(JL))+(ZSFLN(JL)-ZSFL(JL))) &
     & * ZDP5(JL,JK)-ZDP(JL,JK)*((ZRFLN5(JL)-ZRFL5(JL)) &
     & + (ZSFLN5(JL)-ZSFL5(JL))))/ZDP5(JL,JK)**2 &
     & + PLUDE5(JL,JK)*ZGDP(JL) + ZGDP5(JL)*PLUDE(JL,JK)  
    ZDQDT5(JL) = -((ZRFLN5(JL)-ZRFL5(JL))+(ZSFLN5(JL)-ZSFL5(JL)))&
     & * (RG/ZDP5(JL,JK))+PLUDE5(JL,JK)*ZGDP5(JL)  

    ZDTDT (JL) = ((ZLVDCP5(JL,JK)*(ZRFLN(JL)-ZRFL(JL))+ZLSDCP5(JL,JK) &
     & * (ZSFLN(JL)-ZSFL(JL)) &
     & + ZLVDCP(JL,JK)*(ZRFLN5(JL)-ZRFL5(JL))+ZLSDCP(JL,JK) &
     & * (ZSFLN5(JL)-ZSFL5(JL)))*ZDP5(JL,JK)-ZDP(JL,JK) &
     & * (ZLVDCP5(JL,JK)*(ZRFLN5(JL)-ZRFL5(JL))+ZLSDCP5(JL,JK) &
     & * (ZSFLN5(JL)-ZSFL5(JL))))*RG/ZDP5(JL,JK)**2 &
     & - ZGDP5(JL)*(ZFWAT5(JL,JK)*ZLVDCP5(JL,JK) &
     & + (1.0_JPRB-ZFWAT5(JL,JK))*ZLSDCP5(JL,JK))*PLUDE(JL,JK) &
     & - PLUDE5(JL,JK)*(ZFWAT5(JL,JK)*ZLVDCP5(JL,JK) &
     & + (1.0_JPRB-ZFWAT5(JL,JK))*ZLSDCP5(JL,JK))*ZGDP(JL) &
     & - PLUDE5(JL,JK)*ZGDP5(JL) &
     & * (ZFWAT5(JL,JK)*ZLVDCP(JL,JK) &
     & + (ZLVDCP5(JL,JK)-ZLSDCP5(JL,JK))*ZFWAT(JL,JK) &
     & + (1.0_JPRB-ZFWAT5(JL,JK))*ZLSDCP(JL,JK))  &
     & + (ZLSDCP (JL,JK)-ZLVDCP (JL,JK))*ZRFREEZE5(JL,JK)*ZGDP5(JL) & 
     & + (ZLSDCP5(JL,JK)-ZLVDCP5(JL,JK))*ZRFREEZE (JL,JK)*ZGDP5(JL) & 
     & + (ZLSDCP5(JL,JK)-ZLVDCP5(JL,JK))*ZRFREEZE5(JL,JK)*ZGDP (JL) 
    ZDTDT5(JL) = (ZLVDCP5(JL,JK)*(ZRFLN5(JL)-ZRFL5(JL))+ZLSDCP5(JL,JK) &
     & * (ZSFLN5(JL)-ZSFL5(JL)))*(RG/ZDP5(JL,JK)) &
     & - PLUDE5(JL,JK)*ZGDP5(JL) &
     & * (ZFWAT5(JL,JK)*ZLVDCP5(JL,JK) &
     & + (1.0_JPRB-ZFWAT5(JL,JK))*ZLSDCP5(JL,JK)) &  
     & + (ZLSDCP5(JL,JK)-ZLVDCP5(JL,JK))*ZRFREEZE5(JL,JK)*ZGDP5(JL)  

    PTENQ (JL,JK) = PTENQ(JL,JK)+ZDQDT(JL)
    PTENQ5(JL,JK) = PTENQ5(JL,JK)+ZDQDT5(JL)
    PTENT (JL,JK) = PTENT(JL,JK)+ZDTDT(JL) 
    PTENT5(JL,JK) = PTENT5(JL,JK)+ZDTDT5(JL)

    PFPLSL (JL,JK+1) = ZRFLN (JL)
    PFPLSL5(JL,JK+1) = ZRFLN5(JL)
    PFPLSN (JL,JK+1) = ZSFLN (JL)
    PFPLSN5(JL,JK+1) = ZSFLN5(JL)
  ENDDO

  DO JL=KIDIA,KFDIA
    ZRFL (JL) = ZRFLN (JL)
    ZRFL5(JL) = ZRFLN5(JL)
    ZSFL (JL) = ZSFLN (JL)
    ZSFL5(JL) = ZSFLN5(JL)
  ENDDO

ENDDO  !jk

!*     ENTHALPY FLUXES DUE TO PRECIPITATION
!      ------------------------------------

DO JK=1,KLEV+1
  DO JL=KIDIA,KFDIA
    PFHPSL (JL,JK) = -PFPLSL (JL,JK)*RLVTT
    PFHPSL5(JL,JK) = -PFPLSL5(JL,JK)*RLVTT
    PFHPSN (JL,JK) = -PFPLSN (JL,JK)*RLSTT
    PFHPSN5(JL,JK) = -PFPLSN5(JL,JK)*RLSTT
  ENDDO
ENDDO

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

IF (LHOOK) CALL DR_HOOK('CLOUDSTTL',1,ZHOOK_HANDLE)
END SUBROUTINE CLOUDSTTL

