!OPTIONS XOPT(HSFUN)
SUBROUTINE CUFLX2TL &
 & (  KIDIA,    KFDIA,    KLON,   KLEV,&
 & PTSPHY,&
 & PTEN5,    PQEN5,    PQSEN5,   PTENH5,   PQENH5,&
 & PAPH5,    PAP5,     LDLAND,   LDCUM,&
 & KCBOT,    KCTOP,    KDTOP,    KTOPM2,&
 & KTYPE,    LDDRAF,&
 & PMFU5,    PMFD5,    PMFUS5,   PMFDS5,&
 & PMFUQ5,   PMFDQ5,   PMFUL5,   PLUDE5,&
 & PDMFUP5,  PDMFDP5,  PDPMEL5,  PLGLAC5,&
 & PMFLXR5,  PMFLXS5,  PRAIN5,&
 & PTEN,     PQEN,     PQSEN,    PTENH,    PQENH,&
 & PAPH,     PAP,&
 & PMFU,     PMFD,     PMFUS,    PMFDS,&
 & PMFUQ,    PMFDQ,    PMFUL,    PLUDE,&
 & PDMFUP,   PDMFDP,   PDPMEL,   PLGLAC,&
 & PMFLXR,   PMFLXS,   PRAIN )  

!          Ph LOPEZ          E.C.M.W.F.     22/05/2002

!          Adapted from

!          M.TIEDTKE         E.C.M.W.F.     7/86 MODIF. 12/89

!          PURPOSE
!          -------

!          THIS ROUTINE DOES THE FINAL CALCULATION OF CONVECTIVE
!          FLUXES IN THE CLOUD LAYER AND IN THE SUBCLOUD LAYER
!          IN THE CASE OF THE SIMPLIFIED CONVECTION SCHEME
!          (Tangent linear)

!          INTERFACE
!          ---------
!          THIS ROUTINE IS CALLED FROM *CUMASTRNTL*.

!     PARAMETER     DESCRIPTION                                   UNITS
!     ---------     -----------                                   -----
!     INPUT PARAMETERS (INTEGER):

!    *KIDIA*        START POINT
!    *KFDIA*        END POINT
!    *KLON*         NUMBER OF GRID POINTS PER PACKET
!    *KLEV*         NUMBER OF LEVELS
!    *KCBOT*        CLOUD BASE LEVEL
!    *KCTOP*        CLOUD TOP LEVEL
!    *KDTOP*        TOP LEVEL OF DOWNDRAFTS

!    INPUT PARAMETERS (LOGICAL):

!    *LDLAND*       LAND SEA MASK (.TRUE. FOR LAND)
!    *LDCUM*        FLAG: .TRUE. FOR CONVECTIVE POINTS

!    INPUT PARAMETERS (REAL):

!    *PTSPHY*       TIME STEP FOR THE PHYSICS                       S
!    *PTEN5*        PROVISIONAL ENVIRONMENT TEMPERATURE (T+1)    (Trajectory)
!    *PQEN5*        PROVISIONAL ENVIRONMENT SPEC. HUMIDITY (T+1) (Trajectory)
!    *PQSEN5*       ENVIRONMENT SPEC. SATURATION HUMIDITY (T+1)  (Trajectory)
!    *PTENH5*       ENV. TEMPERATURE (T+1) ON HALF LEVELS        (Trajectory)
!    *PQENH5*       ENV. SPEC. HUMIDITY (T+1) ON HALF LEVELS     (Trajectory)
!    *PAPH5*        PROVISIONAL PRESSURE ON HALF LEVELS          (Trajectory)
!    *PAP5*         PROVISIONAL PRESSURE ON FULL LEVELS          (Trajectory)

!    *PTEN*         PROVISIONAL ENVIRONMENT TEMPERATURE (T+1)       K
!    *PQEN*         PROVISIONAL ENVIRONMENT SPEC. HUMIDITY (T+1)  KG/KG
!    *PQSEN*        ENVIRONMENT SPEC. SATURATION HUMIDITY (T+1)   KG/KG
!    *PTENH*        ENV. TEMPERATURE (T+1) ON HALF LEVELS           K
!    *PQENH*        ENV. SPEC. HUMIDITY (T+1) ON HALF LEVELS      KG/KG
!    *PAPH*         PROVISIONAL PRESSURE ON HALF LEVELS            PA
!    *PAP*          PROVISIONAL PRESSURE ON FULL LEVELS            PA

!    UPDATED PARAMETERS (INTEGER):

!    *KTYPE*        SET TO ZERO IF LDCUM=.FALSE.

!    UPDATED PARAMETERS (LOGICAL):

!    *LDDRAF*       SET TO .FALSE. IF LDCUM=.FALSE. OR KDTOP<KCTOP   

!    UPDATED PARAMETERS (REAL):

!    *PMFU5*        MASSFLUX IN UPDRAFTS                        (Trajectory)  
!    *PMFD5*        MASSFLUX IN DOWNDRAFTS                      (Trajectory)  
!    *PMFUS5*       FLUX OF DRY STATIC ENERGY IN UPDRAFTS       (Trajectory)  
!    *PMFDS5*       FLUX OF DRY STATIC ENERGY IN DOWNDRAFTS     (Trajectory)
!    *PMFUQ5*       FLUX OF SPEC. HUMIDITY IN UPDRAFTS          (Trajectory)  
!    *PMFDQ5*       FLUX OF SPEC. HUMIDITY IN DOWNDRAFTS        (Trajectory)  
!    *PMFUL5*       FLUX OF LIQUID WATER IN UPDRAFTS            (Trajectory)  
!    *PLUDE5*       DETRAINED LIQUID WATER                      (Trajectory)  
!    *PDMFUP5*      FLUX DIFFERENCE OF PRECIP. IN UPDRAFTS      (Trajectory)  
!    *PDMFDP5*      FLUX DIFFERENCE OF PRECIP. IN DOWNDRAFTS    (Trajectory)  

!    *PMFU*         MASSFLUX IN UPDRAFTS                          KG/(M2*S)
!    *PMFD*         MASSFLUX IN DOWNDRAFTS                        KG/(M2*S)
!    *PMFUS*        FLUX OF DRY STATIC ENERGY IN UPDRAFTS          J/(M2*S)
!    *PMFDS*        FLUX OF DRY STATIC ENERGY IN DOWNDRAFTS        J/(M2*S)
!    *PMFUQ*        FLUX OF SPEC. HUMIDITY IN UPDRAFTS            KG/(M2*S)
!    *PMFDQ*        FLUX OF SPEC. HUMIDITY IN DOWNDRAFTS          KG/(M2*S)
!    *PMFUL*        FLUX OF LIQUID WATER IN UPDRAFTS              KG/(M2*S)
!    *PLUDE*        DETRAINED LIQUID WATER                        KG/(M3*S)
!    *PDMFUP*       FLUX DIFFERENCE OF PRECIP. IN UPDRAFTS        KG/(M2*S)
!    *PDMFDP*       FLUX DIFFERENCE OF PRECIP. IN DOWNDRAFTS      KG/(M2*S)

!    OUTPUT PARAMETERS (REAL):

!    *PDPMEL5*      CHANGE IN PRECIP.-FLUXES DUE TO MELTING     (Trajectory) 
!    *PLGLAC5*      FLUX OF FROZEN CLOUD WATER IN UPDRAFTS      (Trajectory) 
!    *PMFLXR5*      CONVECTIVE RAIN FLUX                        (Trajectory) 
!    *PMFLXS5*      CONVECTIVE SNOW FLUX                        (Trajectory) 
!    *PRAIN5*       TOTAL PRECIP. PRODUCED IN CONV. UPDRAFTS    (Trajectory) 
!                   (NO EVAPORATION IN DOWNDRAFTS)       

!    *PDPMEL*       CHANGE IN PRECIP.-FLUXES DUE TO MELTING       KG/(M2*S)
!    *PLGLAC*       FLUX OF FROZEN CLOUD WATER IN UPDRAFTS        KG/(M2*S)
!    *PMFLXR*       CONVECTIVE RAIN FLUX                          KG/(M2*S)
!    *PMFLXS*       CONVECTIVE SNOW FLUX                          KG/(M2*S)
!    *PRAIN*        TOTAL PRECIP. PRODUCED IN CONV. UPDRAFTS      KG/(M2*S)
!                   (NO EVAPORATION IN DOWNDRAFTS)

!          EXTERNALS
!          ---------
!          NONE

!       Modifications
!       -------------
!        P. Lopez      11-01-2007  Removed switch LPHYLIN
!        P. Lopez      04-09-2007  Revision to match Tiedtke scheme

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

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

USE YOMCST   , ONLY : 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  
USE YOEPHLI  , ONLY : RLPTRC
USE YOECUMF2 , ONLY : RCUCOV2   ,RCPECONS2 ,RTAUMEL2

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 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSPHY 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQSEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENH5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQENH5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPH5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAP5(KLON,KLEV) 
LOGICAL           ,INTENT(IN)    :: LDLAND(KLON)
LOGICAL           ,INTENT(IN)    :: LDCUM(KLON) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KCBOT(KLON) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KCTOP(KLON) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDTOP(KLON) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KTOPM2 
INTEGER(KIND=JPIM),INTENT(INOUT) :: KTYPE(KLON) 
LOGICAL           ,INTENT(INOUT) :: LDDRAF(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFD5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFUS5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFDS5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFUQ5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFDQ5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFUL5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLUDE5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDMFUP5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDMFDP5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDPMEL5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLGLAC5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFLXR5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFLXS5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PRAIN5(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTEN(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQEN(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQSEN(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENH(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQENH(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPH(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFD(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFUS(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFDS(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFUQ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFDQ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFUL(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLUDE(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDMFUP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDMFDP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDPMEL(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLGLAC(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFLXR(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFLXS(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PRAIN(KLON) 
INTEGER(KIND=JPIM) :: IK, IKB, ITOP, JK, JL

REAL(KIND=JPRB) :: ZRHEBC(KLON)

REAL(KIND=JPRB) :: Z1S5, Z2S5, ZALFAW5, ZCONS15, ZCONS1A,&
 & ZCONS2, ZDENOM5, ZDRFL15, &
 & ZDRFL5, ZFAC5, ZFOEEWI5, ZFOEEWL5, ZOEALFA5, &
 & ZOEEWM5, ZOELHM5, ZPDR5, ZPDS5, ZRFL5, ZRFLN5, &
 & ZRMIN5, ZRNEW5, ZSNMLT5, ZTARG5, &
 & ZTMST, ZZP5, ZP15, ZP25, ZFACT25, ZFACT35, &
 & ZCONS3, ZSQRP5  

REAL(KIND=JPRB) :: Z1S, Z2S, ZALFAW, ZDENOM, ZDRFL1, ZCONS1, &
 & ZDRFL, ZFAC, ZFOEEWI, ZFOEEWL, ZOEALFA, &
 & ZOEEWM, ZOELHM, ZPDR, ZPDS, ZRFL, ZRFLN, &
 & ZRMIN, ZRNEW, ZSNMLT, ZTARG, ZZP, ZP1, ZP2, &
 & ZFACT1, ZFACT2, ZFACT3  
REAL(KIND=JPRB) :: ZHOOK_HANDLE

!DIR$ VFUNCTION EXPHF
#include "fcttre.h"
#include "fcttretl.h"

!--------------------------------------------------------------------
!*             SPECIFY CONSTANTS

IF (LHOOK) CALL DR_HOOK('CUFLX2TL',0,ZHOOK_HANDLE)
ZTMST=PTSPHY
ZCONS1A=RCPD/(RLMLT*RG*RTAUMEL2)
ZCONS2=1.0_JPRB/(RG*ZTMST)

!*    1.0          DETERMINE FINAL CONVECTIVE FLUXES
!                  ---------------------------------

ITOP=KLEV
DO JL=KIDIA,KFDIA
  PRAIN5(JL)=0.0_JPRB
  PRAIN (JL)=0.0_JPRB
  ITOP=MIN(ITOP,KCTOP(JL))
  IF(.NOT.LDCUM(JL).OR.KDTOP(JL) < KCTOP(JL)) LDDRAF(JL)=.FALSE.
  IF(.NOT.LDCUM(JL)) KTYPE(JL)=0
  IF(LDLAND(JL)) THEN
    ZRHEBC(JL)=0.7_JPRB
  ELSE
    ZRHEBC(JL)=0.9_JPRB
  ENDIF
ENDDO
KTOPM2=MAX(2,ITOP-2)
! TO GET IDENTICAL RESULTS FOR DIFFERENT NPROMA FORCE KTOPM2 TO 2
KTOPM2=2
DO JK=KTOPM2,KLEV
!DIR$ IVDEP
!OCL NOVREC
  DO JL=KIDIA,KFDIA
    PMFLXR5(JL,JK)=0.0_JPRB
    PMFLXR (JL,JK)=0.0_JPRB
    PMFLXS5(JL,JK)=0.0_JPRB
    PMFLXS (JL,JK)=0.0_JPRB
    PDPMEL5(JL,JK)=0.0_JPRB
    PDPMEL (JL,JK)=0.0_JPRB
    IF(LDCUM(JL).AND.JK >= KCTOP(JL)-1) THEN
      PMFUS (JL,JK)=RCPD*(PMFU (JL,JK)*(PMFUS5(JL,JK)-PTENH5(JL,JK)) &
                 & +      PMFU5(JL,JK)*(PMFUS (JL,JK)-PTENH (JL,JK)))
      PMFUS5(JL,JK)=RCPD*PMFU5(JL,JK)*(PMFUS5(JL,JK)-PTENH5(JL,JK))

      PMFUQ (JL,JK)=PMFU (JL,JK)*(PMFUQ5(JL,JK)-PQENH5(JL,JK)) &
                 & +PMFU5(JL,JK)*(PMFUQ (JL,JK)-PQENH (JL,JK))
      PMFUQ5(JL,JK)=PMFU5(JL,JK)*(PMFUQ5(JL,JK)-PQENH5(JL,JK))

      PMFUL (JL,JK)=PMFU (JL,JK)*PMFUL5(JL,JK) + PMFU5(JL,JK)*PMFUL (JL,JK)
      PMFUL5(JL,JK)=PMFU5(JL,JK)*PMFUL5(JL,JK)

      PLGLAC(JL,JK)=PMFU (JL,JK)*PLGLAC5(JL,JK) &
       & +PMFU5(JL,JK)*PLGLAC (JL,JK)  
      PLGLAC5(JL,JK)=PMFU5(JL,JK)*PLGLAC5(JL,JK)

      IF (JK < KLEV) THEN
        PLUDE (JL,JK)= PMFU (JL,JK+1)*PLUDE5(JL,JK) &
                   & + PMFU5(JL,JK+1)*PLUDE (JL,JK)
        PLUDE5(JL,JK)=PMFU5(JL,JK+1)*PLUDE5(JL,JK)
      ELSE
        PLUDE5(JL,JK)=0.0_JPRB
        PLUDE (JL,JK)=0.0_JPRB
      ENDIF
      
      IF(LDDRAF(JL).AND.JK >= KDTOP(JL)) THEN
        PMFDS (JL,JK)=RCPD*(PMFD (JL,JK)*(PMFDS5(JL,JK)-PTENH5(JL,JK)) &
                   & +      PMFD5(JL,JK)*(PMFDS (JL,JK)-PTENH (JL,JK)))
        PMFDS5(JL,JK)=RCPD*PMFD5(JL,JK)*(PMFDS5(JL,JK)-PTENH5(JL,JK))

        PMFDQ (JL,JK)=PMFD (JL,JK)*(PMFDQ5(JL,JK)-PQENH5(JL,JK)) &
                   & +PMFD5(JL,JK)*(PMFDQ (JL,JK)-PQENH (JL,JK))
        PMFDQ5(JL,JK)=PMFD5(JL,JK)*(PMFDQ5(JL,JK)-PQENH5(JL,JK))
      ELSE
        PMFD5(JL,JK)=0.0_JPRB
        PMFD (JL,JK)=0.0_JPRB
        PMFDS5(JL,JK)=0.0_JPRB
        PMFDS (JL,JK)=0.0_JPRB
        PMFDQ5(JL,JK)=0.0_JPRB
        PMFDQ (JL,JK)=0.0_JPRB
        PDMFDP5(JL,JK-1)=0.0_JPRB
        PDMFDP (JL,JK-1)=0.0_JPRB
      ENDIF
    ELSE
      PMFU5(JL,JK)=0.0_JPRB
      PMFU (JL,JK)=0.0_JPRB
      PMFD5(JL,JK)=0.0_JPRB
      PMFD (JL,JK)=0.0_JPRB
      PMFUS5(JL,JK)=0.0_JPRB
      PMFUS (JL,JK)=0.0_JPRB
      PMFDS5(JL,JK)=0.0_JPRB
      PMFDS (JL,JK)=0.0_JPRB
      PMFUQ5(JL,JK)=0.0_JPRB
      PMFUQ (JL,JK)=0.0_JPRB
      PMFDQ5(JL,JK)=0.0_JPRB
      PMFDQ (JL,JK)=0.0_JPRB
      PMFUL5(JL,JK)=0.0_JPRB
      PMFUL (JL,JK)=0.0_JPRB
      PLGLAC5(JL,JK)=0.0_JPRB
      PLGLAC (JL,JK)=0.0_JPRB
      PDMFUP5(JL,JK-1)=0.0_JPRB
      PDMFUP (JL,JK-1)=0.0_JPRB
      PDMFDP5(JL,JK-1)=0.0_JPRB
      PDMFDP (JL,JK-1)=0.0_JPRB
      PLUDE5(JL,JK-1)=0.0_JPRB
      PLUDE (JL,JK-1)=0.0_JPRB
    ENDIF
  ENDDO
ENDDO
PMFLXR5(:,KLEV+1)=0.0_JPRB
PMFLXR (:,KLEV+1)=0.0_JPRB
PMFLXS5(:,KLEV+1)=0.0_JPRB
PMFLXS (:,KLEV+1)=0.0_JPRB

!*    1.5          SCALE FLUXES BELOW CLOUD BASE
!                  LINEAR DCREASE
!                  -----------------------------

!DIR$ IVDEP
!OCL NOVREC
DO JL=KIDIA,KFDIA
  IF(LDCUM(JL)) THEN
    IKB=KCBOT(JL)
    IK=IKB+1
    ZP15=PAPH5(JL,KLEV+1)-PAPH5(JL,IK)
    ZP1 =PAPH (JL,KLEV+1)-PAPH (JL,IK)
    ZP25=PAPH5(JL,KLEV+1)-PAPH5(JL,IKB)
    ZP2 =PAPH (JL,KLEV+1)-PAPH (JL,IKB)
    ZZP5=ZP15/ZP25
    ZZP=(ZP1*ZP25-ZP15*ZP2)/(ZP25*ZP25)

    IF(KTYPE(JL) == 3) THEN
      ZZP=2.0_JPRB*ZZP*ZZP5
      ZZP5=ZZP5*ZZP5
    ENDIF

    PMFU (JL,IK)=PMFU(JL,IKB)*ZZP5+PMFU5(JL,IKB)*ZZP
    PMFU5(JL,IK)=PMFU5(JL,IKB)*ZZP5

    ZTARG5=PTENH5(JL,IKB)
    ZTARG =PTENH (JL,IKB)
    ZOEALFA5=0.545_JPRB*(TANH(0.17_JPRB*(ZTARG5-RLPTRC))+1.0_JPRB)
    ZOEALFA=0.545_JPRB*(1.0_JPRB-TANH(0.17_JPRB*(ZTARG5-RLPTRC))**2) &
     & *0.17_JPRB*ZTARG  
    ZOELHM5=ZOEALFA5*RLVTT+(1.0_JPRB-ZOEALFA5)*RLSTT
    ZOELHM=ZOEALFA *RLVTT-ZOEALFA*RLSTT
    PMFUS(JL,IK)=(PMFUS(JL,IKB)&
     & -ZOELHM*PMFUL5(JL,IKB)-ZOELHM5*PMFUL(JL,IKB))*ZZP5 &
     & +(PMFUS5(JL,IKB)-ZOELHM5*PMFUL5(JL,IKB))*ZZP  
    PMFUS5(JL,IK)=(PMFUS5(JL,IKB)-ZOELHM5*PMFUL5(JL,IKB))*ZZP5
    PMFUQ (JL,IK)=(PMFUQ (JL,IKB)+PMFUL (JL,IKB))*ZZP5 &
     & +(PMFUQ5(JL,IKB)+PMFUL5(JL,IKB))*ZZP  
    PMFUQ5(JL,IK)=(PMFUQ5(JL,IKB)+PMFUL5(JL,IKB))*ZZP5
    PMFUL5(JL,IK)=0.0_JPRB
    PMFUL (JL,IK)=0.0_JPRB
  ENDIF
ENDDO

DO JK=KTOPM2,KLEV
!DIR$ IVDEP
!OCL NOVREC
  DO JL=KIDIA,KFDIA
    IF(LDCUM(JL).AND.JK > KCBOT(JL)+1) THEN
      IKB=KCBOT(JL)+1
      ZP15=PAPH5(JL,KLEV+1)-PAPH5(JL,JK)
      ZP1 =PAPH (JL,KLEV+1)-PAPH (JL,JK)
      ZP25=PAPH5(JL,KLEV+1)-PAPH5(JL,IKB)
      ZP2 =PAPH (JL,KLEV+1)-PAPH (JL,IKB)
      ZZP5=ZP15/ZP25
      ZZP=(ZP1*ZP25-ZP15*ZP2)/(ZP25**2)

      IF(KTYPE(JL) == 3) THEN
        ZZP=2.0_JPRB*ZZP*ZZP5
        ZZP5=ZZP5**2
      ENDIF
      PMFU (JL,JK)=PMFU(JL,IKB)*ZZP5+PMFU5(JL,IKB)*ZZP
      PMFU5(JL,JK)=PMFU5(JL,IKB)*ZZP5
      PMFUS (JL,JK)=PMFUS(JL,IKB)*ZZP5+PMFUS5(JL,IKB)*ZZP
      PMFUS5(JL,JK)=PMFUS5(JL,IKB)*ZZP5
      PMFUQ (JL,JK)=PMFUQ(JL,IKB)*ZZP5+PMFUQ5(JL,IKB)*ZZP
      PMFUQ5(JL,JK)=PMFUQ5(JL,IKB)*ZZP5
      PMFUL5(JL,JK)=0.0_JPRB
      PMFUL (JL,JK)=0.0_JPRB
      IF(LDDRAF(JL).AND.PMFD5(JL,IKB+1) == 0.0_JPRB) THEN
        PMFD (JL,JK)=PMFD(JL,IKB)*ZZP5+PMFD5(JL,IKB)*ZZP
        PMFD5(JL,JK)=PMFD5(JL,IKB)*ZZP5
        PMFDS (JL,JK)=PMFDS(JL,IKB)*ZZP5+PMFDS5(JL,IKB)*ZZP
        PMFDS5(JL,JK)=PMFDS5(JL,IKB)*ZZP5
        PMFDQ (JL,JK)=PMFDQ(JL,IKB)*ZZP5+PMFDQ5(JL,IKB)*ZZP
        PMFDQ5(JL,JK)=PMFDQ5(JL,IKB)*ZZP5
      ENDIF
    ENDIF
  ENDDO
ENDDO

!*    2.            CALCULATE RAIN/SNOW FALL RATES
!*                  CALCULATE MELTING OF SNOW
!*                  CALCULATE EVAPORATION OF PRECIP
!                   -------------------------------

DO JK=KTOPM2,KLEV
  DO JL=KIDIA,KFDIA
    IF(LDCUM(JL).AND.JK >= KCTOP(JL)-1) THEN
      PRAIN5(JL)=PRAIN5(JL)+PDMFUP5(JL,JK)
      PRAIN (JL)=PRAIN (JL)+PDMFUP (JL,JK)
      IF(PMFLXS5(JL,JK) > 0.0_JPRB.AND.PTEN5(JL,JK) > RTT) THEN
        ZCONS15=ZCONS1A*(1.0_JPRB+0.5_JPRB*(PTEN5(JL,JK)-RTT))
        ZCONS1 =ZCONS1A*0.5_JPRB*PTEN(JL,JK)
        ZFAC5=ZCONS15*(PAPH5(JL,JK+1)-PAPH5(JL,JK))
        ZFAC =ZCONS1 *(PAPH5(JL,JK+1)-PAPH5(JL,JK)) &
         & +ZCONS15*(PAPH (JL,JK+1)-PAPH (JL,JK))  
        IF (PMFLXS5(JL,JK) < ZFAC5*(PTEN5(JL,JK)-RTT)) THEN
          ZSNMLT5 = PMFLXS5(JL,JK)
          ZSNMLT  = PMFLXS (JL,JK)
        ELSE
          ZSNMLT5 = ZFAC5*(PTEN5(JL,JK)-RTT)
          ZSNMLT  = ZFAC *(PTEN5(JL,JK)-RTT)+ZFAC5*PTEN(JL,JK)
        ENDIF
        PDPMEL5(JL,JK)=ZSNMLT5
        PDPMEL (JL,JK)=ZSNMLT 

        ZTARG5=PTEN5(JL,JK)-ZSNMLT5/ZFAC5
        ZTARG =PTEN(JL,JK)-ZSNMLT/ZFAC5+ZSNMLT5*ZFAC/(ZFAC5**2)

        ZOEALFA5=0.545_JPRB*(TANH(0.17_JPRB*(ZTARG5-RLPTRC))+1.0_JPRB)
        ZOEALFA=0.545_JPRB*(1.0_JPRB-TANH(0.17_JPRB*(ZTARG5-RLPTRC))**2) &
         & *0.17_JPRB*ZTARG  
        ZFOEEWL5=R2ES*EXP(R3LES*(ZTARG5-RTT)/(ZTARG5-R4LES))
        ZFOEEWL =ZFOEEWL5*R3LES*ZTARG*(RTT-R4LES)/(ZTARG5-R4LES)**2
        ZFOEEWI5=R2ES*EXP(R3IES*(ZTARG5-RTT)/(ZTARG5-R4IES))
        ZFOEEWI =ZFOEEWI5*R3IES*ZTARG*(RTT-R4IES)/(ZTARG5-R4IES)**2
        ZOEEWM5=ZOEALFA5*ZFOEEWL5 + (1.0_JPRB-ZOEALFA5)*ZFOEEWI5
        ZOEEWM =ZOEALFA5*ZFOEEWL+ZOEALFA*ZFOEEWL5 &
         & +(1.0_JPRB-ZOEALFA5)*ZFOEEWI-ZOEALFA*ZFOEEWI5  
        PQSEN5(JL,JK)=ZOEEWM5/PAP5(JL,JK)
        PQSEN (JL,JK)=(ZOEEWM-ZOEEWM5*PAP(JL,JK)/PAP5(JL,JK)) &
         & /PAP5(JL,JK)  
      ENDIF
      ZALFAW5=0.545_JPRB*(TANH(0.17_JPRB*(PTEN5(JL,JK)-RLPTRC))+1.0_JPRB)
      ZALFAW =0.545_JPRB*(1.0_JPRB-TANH(0.17_JPRB*(PTEN5(JL,JK)-RLPTRC))**2) &
       & *0.17_JPRB*PTEN(JL,JK)  
!no liquid precipitation above melting level
      IF(PTEN5(JL,JK) < RTT .AND. ZALFAW5 > 0.0_JPRB) THEN
        PLGLAC5(JL,JK)=PLGLAC5(JL,JK)+ZALFAW5*(PDMFUP5(JL,JK)+PDMFDP5(JL,JK))
        PLGLAC (JL,JK)=PLGLAC (JL,JK)+ZALFAW *(PDMFUP5(JL,JK)+PDMFDP5(JL,JK)) &
                    & +               ZALFAW5*(PDMFUP (JL,JK)+PDMFDP (JL,JK))
        ZALFAW5=0.0_JPRB
        ZALFAW =0.0_JPRB
      ENDIF

      PMFLXR5(JL,JK+1)=PMFLXR5(JL,JK)+ZALFAW5*&
       & (PDMFUP5(JL,JK)+PDMFDP5(JL,JK))+PDPMEL5(JL,JK)  
      PMFLXR (JL,JK+1)=PMFLXR (JL,JK) &
       & +ZALFAW *(PDMFUP5(JL,JK)+PDMFDP5(JL,JK)) &
       & +ZALFAW5*(PDMFUP (JL,JK)+PDMFDP (JL,JK)) &
       & +PDPMEL(JL,JK)  
      PMFLXS5(JL,JK+1)=PMFLXS5(JL,JK)+(1.0_JPRB-ZALFAW5)*&
       & (PDMFUP5(JL,JK)+PDMFDP5(JL,JK))-PDPMEL5(JL,JK)  
      PMFLXS (JL,JK+1)=PMFLXS (JL,JK) &
       & -ZALFAW*(PDMFUP5(JL,JK)+PDMFDP5(JL,JK)) &
       & +(1.0_JPRB-ZALFAW5)*(PDMFUP(JL,JK)+PDMFDP(JL,JK)) &
       & -PDPMEL(JL,JK)  
      IF(PMFLXR5(JL,JK+1)+PMFLXS5(JL,JK+1) < 0.0_JPRB) THEN
        PDMFDP5(JL,JK)=-(PMFLXR5(JL,JK)+PMFLXS5(JL,JK)+PDMFUP5(JL,JK))
        PDMFDP (JL,JK)=-(PMFLXR (JL,JK)+PMFLXS (JL,JK)+PDMFUP (JL,JK))
        PMFLXR5(JL,JK+1)=0.0_JPRB
        PMFLXR (JL,JK+1)=0.0_JPRB
        PMFLXS5(JL,JK+1)=0.0_JPRB
        PMFLXS (JL,JK+1)=0.0_JPRB
        PDPMEL5(JL,JK)  =0.0_JPRB
        PDPMEL (JL,JK)  =0.0_JPRB
      ELSEIF(PMFLXR5(JL,JK+1) < 0.0_JPRB ) THEN
        PMFLXS5(JL,JK+1)=PMFLXS5(JL,JK+1)+PMFLXR5(JL,JK+1)
        PMFLXS (JL,JK+1)=PMFLXS (JL,JK+1)+PMFLXR (JL,JK+1)
        PMFLXR5(JL,JK+1)=0.0_JPRB
        PMFLXR (JL,JK+1)=0.0_JPRB
      ELSEIF(PMFLXS5(JL,JK+1) < 0.0_JPRB ) THEN
        PMFLXR5(JL,JK+1)=PMFLXR5(JL,JK+1)+PMFLXS5(JL,JK+1)
        PMFLXR (JL,JK+1)=PMFLXR (JL,JK+1)+PMFLXS (JL,JK+1)
        PMFLXS5(JL,JK+1)=0.0_JPRB
        PMFLXS (JL,JK+1)=0.0_JPRB
      ENDIF
    ENDIF
  ENDDO
ENDDO

! Reminder for conservation:
!    pdmfup(jl,jk)+pdmfdp(jl,jk)=pmflxr(jl,jk+1)+pmflxs(jl,jk+1)-pmflxr(jl,jk)-pmflxs(jl,jk)

DO JK=KTOPM2,KLEV
  DO JL=KIDIA,KFDIA
    IF(LDCUM(JL).AND.JK >= KCBOT(JL)) THEN 
      ZRFL5=PMFLXR5(JL,JK)+PMFLXS5(JL,JK)
      ZRFL =PMFLXR (JL,JK)+PMFLXS (JL,JK)
      IF(ZRFL5 > 1.E-20_JPRB) THEN
!jfm            ZDRFL15=RCPECONS2*MAX(0.,PQSEN5(JL,JK)-PQEN5(JL,JK))*RCUCOV2*
!jfm     1             (SQRT(PAPH5(JL,JK)/PAPH5(JL,KLEV+1))/5.09E-3*
!jfm     2             ZRFL5/RCUCOV2)**0.5777*
!jfm     3             (PAPH5(JL,JK+1)-PAPH5(JL,JK))
        IF (PQSEN5(JL,JK)-PQEN5(JL,JK) > 0.0_JPRB) THEN
          Z1S5 = PQSEN5(JL,JK)-PQEN5(JL,JK)
          Z1S  = PQSEN (JL,JK)-PQEN (JL,JK)
        ELSE
          Z1S5 = 0.0_JPRB 
          Z1S  = 0.0_JPRB 
        ENDIF
        ZCONS3=RCPECONS2*RCUCOV2
        ZSQRP5=SQRT(PAPH5(JL,JK)/PAPH5(JL,KLEV+1))
        ZFACT1=1.0_JPRB/(5.09E-3_JPRB*RCUCOV2)
        ZFACT25=ZSQRP5*ZRFL5*ZFACT1
        ZFACT2 =ZSQRP5*(ZRFL+ZRFL5*( PAPH(JL,JK)/PAPH5(JL,JK) &
         & -PAPH(JL,KLEV+1)/PAPH5(JL,KLEV+1) ))*ZFACT1  
        ZFACT35=ZFACT25**0.5777_JPRB
        ZFACT3 =0.5777_JPRB*ZFACT2*ZFACT35/ZFACT25
        ZDRFL15=ZCONS3*Z1S5*ZFACT35*(PAPH5(JL,JK+1)-PAPH5(JL,JK))
        ZDRFL1 =ZCONS3 &
         & *(Z1S *ZFACT35*(PAPH5(JL,JK+1)-PAPH5(JL,JK)) &
         & + Z1S5*ZFACT3 *(PAPH5(JL,JK+1)-PAPH5(JL,JK)) &
         & + Z1S5*ZFACT35*(PAPH (JL,JK+1)-PAPH (JL,JK)))  

        ZRNEW5=ZRFL5-ZDRFL15
        ZRNEW =ZRFL -ZDRFL1 
!jfm            ZRMIN5=ZRFL5-RCUCOV2*MAX(0.,0.8*PQSEN5(JL,JK)-PQEN5(JL,JK))
!jfm     1            *ZCONS2*(PAPH5(JL,JK+1)-PAPH5(JL,JK))
!jfm            ZRNEW5=MAX(ZRNEW5,ZRMIN5)
!jfm            ZRFLN5=MAX(ZRNEW5,0.)
!jfm            ZDRFL5=MIN(0.,ZRFLN5-ZRFL5)
!jfm            ZDENOM5=1./MAX(1.E-20,PMFLXR5(JL,JK)+PMFLXS5(JL,JK))
        IF (ZRHEBC(JL)*PQSEN5(JL,JK)-PQEN5(JL,JK) > 0.0_JPRB) THEN
          Z2S5 = ZRHEBC(JL)*PQSEN5(JL,JK)-PQEN5(JL,JK)
          Z2S  = ZRHEBC(JL)*PQSEN (JL,JK)-PQEN (JL,JK)
        ELSE
          Z2S5 = 0.0_JPRB 
          Z2S  = 0.0_JPRB 
        ENDIF
        ZRMIN5=ZRFL5-RCUCOV2*Z2S5*ZCONS2*(PAPH5(JL,JK+1)-PAPH5(JL,JK))
        ZRMIN =ZRFL -RCUCOV2*ZCONS2 &
         & *(Z2S *(PAPH5(JL,JK+1)-PAPH5(JL,JK)) &
         & + Z2S5*(PAPH (JL,JK+1)-PAPH (JL,JK)))  
        IF (ZRNEW5 < ZRMIN5) THEN
          ZRNEW5 = ZRMIN5
          ZRNEW  = ZRMIN 
        ENDIF
        IF (ZRNEW5 > 0.0_JPRB) THEN
          ZRFLN5 = ZRNEW5
          ZRFLN  = ZRNEW 
        ELSE
          ZRFLN5 = 0.0_JPRB
          ZRFLN  = 0.0_JPRB
        ENDIF
        IF ((ZRFLN5-ZRFL5) < 0.0_JPRB) THEN
          ZDRFL5 = ZRFLN5-ZRFL5
          ZDRFL  = ZRFLN -ZRFL 
        ELSE
          ZDRFL5 = 0.0_JPRB
          ZDRFL  = 0.0_JPRB
        ENDIF
        ZALFAW5=0.545_JPRB*(TANH(0.17_JPRB*(PTEN5(JL,JK)-RLPTRC))+1.0_JPRB)
        ZALFAW =0.545_JPRB*(1.0_JPRB-TANH(0.17_JPRB*(PTEN5(JL,JK)-RLPTRC))**2) &
         & *0.17_JPRB*PTEN(JL,JK)  
        IF(PTEN5(JL,JK) < RTT) THEN
          ZALFAW5=0.0_JPRB
          ZALFAW =0.0_JPRB
        ENDIF
        ZPDR5=ZALFAW5*PDMFDP5(JL,JK)
        ZPDR =ZALFAW5*PDMFDP(JL,JK)+ZALFAW*PDMFDP5(JL,JK)
        ZPDS5=(1.0_JPRB-ZALFAW5)*PDMFDP5(JL,JK)
        ZPDS =(1.0_JPRB-ZALFAW5)*PDMFDP(JL,JK)-ZALFAW*PDMFDP5(JL,JK)
        IF ((PMFLXR5(JL,JK)+PMFLXS5(JL,JK)) > 1.E-20_JPRB) THEN
          ZDENOM5 = 1.0_JPRB/(PMFLXR5(JL,JK)+PMFLXS5(JL,JK))
          ZDENOM  = -(PMFLXR(JL,JK)+PMFLXS(JL,JK))*(ZDENOM5**2)
        ELSE
          ZDENOM5 = 1.0_JPRB/1.E-20_JPRB
          ZDENOM  = 0.0_JPRB
        ENDIF
        PMFLXR5(JL,JK+1)=PMFLXR5(JL,JK)+ZPDR5 &
         & +PDPMEL5(JL,JK)+ZDRFL5*PMFLXR5(JL,JK)*ZDENOM5  
        PMFLXR (JL,JK+1)=PMFLXR (JL,JK)+ZPDR+PDPMEL(JL,JK) &
         & +ZDRFL *PMFLXR5(JL,JK)*ZDENOM5 &
         & +ZDRFL5*PMFLXR (JL,JK)*ZDENOM5 &
         & +ZDRFL5*PMFLXR5(JL,JK)*ZDENOM   
        PMFLXS5(JL,JK+1)=PMFLXS5(JL,JK)+ZPDS5 &
         & -PDPMEL5(JL,JK)+ZDRFL5*PMFLXS5(JL,JK)*ZDENOM5  
        PMFLXS (JL,JK+1)=PMFLXS (JL,JK)+ZPDS-PDPMEL(JL,JK) &
         & +ZDRFL *PMFLXS5(JL,JK)*ZDENOM5 &
         & +ZDRFL5*PMFLXS (JL,JK)*ZDENOM5 &
         & +ZDRFL5*PMFLXS5(JL,JK)*ZDENOM   
        PDMFUP5(JL,JK)=PDMFUP5(JL,JK)+ZDRFL5
        PDMFUP (JL,JK)=PDMFUP (JL,JK)+ZDRFL
        IF(PMFLXR5(JL,JK+1)+PMFLXS5(JL,JK+1) < 0.0_JPRB) THEN
          PDMFUP5(JL,JK)=PDMFUP5(JL,JK)-(PMFLXR5(JL,JK+1)+PMFLXS5(JL,JK+1))
          PDMFUP (JL,JK)=PDMFUP (JL,JK)-(PMFLXR (JL,JK+1)+PMFLXS (JL,JK+1))
          PMFLXR5(JL,JK+1)=0.0_JPRB
          PMFLXR (JL,JK+1)=0.0_JPRB
          PMFLXS5(JL,JK+1)=0.0_JPRB
          PMFLXS (JL,JK+1)=0.0_JPRB
          PDPMEL5(JL,JK)  =0.0_JPRB
          PDPMEL (JL,JK)  =0.0_JPRB
        ELSEIF(PMFLXR5(JL,JK+1) < 0.0_JPRB ) THEN
          PMFLXS5(JL,JK+1)=PMFLXS5(JL,JK+1)+PMFLXR5(JL,JK+1)
          PMFLXS (JL,JK+1)=PMFLXS (JL,JK+1)+PMFLXR (JL,JK+1)
          PMFLXR5(JL,JK+1)=0.0_JPRB
          PMFLXR (JL,JK+1)=0.0_JPRB
        ELSEIF(PMFLXS5(JL,JK+1) < 0.0_JPRB ) THEN
          PMFLXR5(JL,JK+1)=PMFLXR5(JL,JK+1)+PMFLXS5(JL,JK+1)
          PMFLXR (JL,JK+1)=PMFLXR (JL,JK+1)+PMFLXS (JL,JK+1)
          PMFLXS5(JL,JK+1)=0.0_JPRB
          PMFLXS (JL,JK+1)=0.0_JPRB
        ENDIF
      ELSE
        PMFLXR5(JL,JK+1)=0.0_JPRB
        PMFLXR (JL,JK+1)=0.0_JPRB
        PMFLXS5(JL,JK+1)=0.0_JPRB
        PMFLXS (JL,JK+1)=0.0_JPRB
        PDMFDP5(JL,JK)=0.0_JPRB
        PDMFDP (JL,JK)=0.0_JPRB
        PDPMEL5(JL,JK)=0.0_JPRB
        PDPMEL (JL,JK)=0.0_JPRB
      ENDIF
    ENDIF
  ENDDO
ENDDO

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