SUBROUTINE CUMASTRNTL &
 & (  KIDIA,    KFDIA,    KLON,    KLEV,&
 & LDLAND,   PTSPHY,&
 & PTEN5,    PQEN5,    PUEN5,    PVEN5,    PLITOT5,&
 & PVERVEL5, PQSEN5,   PQHFL5,   PAHFS5,&
 & PAP5,     PAPH5,    PGEO5,    PGEOH5,&
 & PTENT5,   PTENQ5,   PTENU5,   PTENV5,&
 & LDCUM,    KTYPE,    KCBOT,    KCTOP,&
 & KBOTSC,   LDSC,&
 & PTU5,     PQU5,     PLU5,     PLUDE5,&
 & PENTH5,   PMFLXR5,  PMFLXS5,  PRAIN5,&
 & PMFU5,    PMFD5,&
 & KTRAC,    PCEN5,    PTENC5,   PSCAV5,&
 & PTEN,     PQEN,     PUEN,     PVEN,&
 & PVERVEL,  PQSEN,    PQHFL,    PAHFS,&
 & PAP,      PAPH,     PGEO,     PGEOH,&
 & PTENT,    PTENQ,    PTENU,    PTENV,&
 & PTU,      PQU,      PLU,      PLUDE,&
 & PENTH,    PMFLXR,   PMFLXS,   PRAIN,&
 & PMFU,     PMFD,&
 & PCEN,     PTENC )

!**** *CUMASTRNTL*  MASTER ROUTINE FOR CUMULUS MASSFLUX-SCHEME
!                   (Tangent linear)

!     J.F. MAHFOUF   E.C.M.W.F      06/12/96

!     Adapted from

!     M.TIEDTKE      E.C.M.W.F.     1986/1987/1989

!     PURPOSE
!     -------

!          THIS ROUTINE COMPUTES THE PHYSICAL TENDENCIES OF THE
!     PROGNOSTIC VARIABLES T,Q,U AND V DUE TO CONVECTIVE PROCESSES.
!     PROCESSES CONSIDERED ARE: CONVECTIVE FLUXES, FORMATION OF
!     PRECIPITATION, EVAPORATION OF FALLING RAIN BELOW CLOUD BASE,
!     SATURATED CUMULUS DOWNDRAFTS.

!**   INTERFACE.
!     ----------

!          *CUMASTRNTL* IS CALLED FROM *CUCALLNTL*
!     THE ROUTINE TAKES ITS INPUT FROM THE LONG-TERM STORAGE
!     T,Q,U,V,PHI AND P AND MOISTURE TENDENCIES.
!     IT RETURNS ITS OUTPUT TO THE SAME SPACE
!      1.MODIFIED TENDENCIES OF MODEL VARIABLES
!      2.RATES OF CONVECTIVE PRECIPITATION
!        (USED IN SUBROUTINE SURF)
!      3.CLOUD BASE, CLOUD TOP AND PRECIP FOR RADIATION
!        (USED IN SUBROUTINE CLOUD)

!     METHOD
!     -------

!     PARAMETERIZATION IS DONE USING A MASSFLUX-SCHEME.
!        (1) DEFINE CONSTANTS AND PARAMETERS
!        (2) SPECIFY VALUES (T,Q,QS...) AT HALF LEVELS AND
!            INITIALIZE UPDRAFT- AND DOWNDRAFT-VALUES IN 'CUINI'
!        (3) CALCULATE CLOUD BASE IN 'CUBASE'
!            AND SPECIFY CLOUD BASE MASSFLUX FROM PBL MOISTURE BUDGET
!        (4) DO CLOUD ASCENT IN 'CUASC' IN ABSENCE OF DOWNDRAFTS
!        (5) DO DOWNDRAFT CALCULATIONS:
!              (A) DETERMINE VALUES AT LFS IN 'CUDLFS'
!              (B) DETERMINE MOIST DESCENT IN 'CUDDRAF'
!              (C) RECALCULATE CLOUD BASE MASSFLUX CONSIDERING THE
!                  EFFECT OF CU-DOWNDRAFTS
!        (6) DO FINAL CLOUD ASCENT IN 'CUASC'
!        (7) DO FINAL ADJUSMENTS TO CONVECTIVE FLUXES IN 'CUFLX',
!            DO EVAPORATION IN SUBCLOUD LAYER
!        (8) CALCULATE INCREMENTS OF T AND Q IN 'CUDTDQ'
!        (9) CALCULATE INCREMENTS OF U AND V IN 'CUDUDV'

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

!    *KIDIA*        START POINT
!    *KFDIA*        END POINT
!    *KLON*         NUMBER OF GRID POINTS PER PACKET
!    *KLEV*         NUMBER OF LEVELS
!    *KTRAC*        NUMBER OF TRACERS

!     INPUT PARAMETERS (LOGICAL)

!    *LDLAND*       LAND SEA MASK (.TRUE. FOR LAND)

!     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)
!    *PUEN5*        PROVISIONAL ENVIRONMENT U-VELOCITY (T+1)     (Trajectory)
!    *PVEN5*        PROVISIONAL ENVIRONMENT V-VELOCITY (T+1)     (Trajectory)
!    *PLITOT5*      GRID MEAN LIQUID WATER+ICE CONTENT           (Trajectory)
!    *PVERVEL5*     VERTICAL VELOCITY                            (Trajectory)
!    *PQSEN5*       ENVIRONMENT SPEC. SATURATION HUMIDITY (T+1)  (Trajectory)
!    *PQHFL5*       MOISTURE FLUX (EXCEPT FROM SNOW EVAP.)       (Trajectory) 
!    *PAHFS5*       SENSIBLE HEAT FLUX                           (Trajectory)
!    *PAP5*         PROVISIONAL PRESSURE ON FULL LEVELS          (Trajectory)
!    *PAPH5*        PROVISIONAL PRESSURE ON HALF LEVELS          (Trajectory)
!    *PGEO5*        GEOPOTENTIAL                                 (Trajectory) 
!    *PGEOH5*       GEOPOTENTIAL ON HALF LEVELS                  (Trajectory) 
!    *PCEN5*        CHEMICAL TRACERS                             (Trajectory) 
!    *PSCAV5*       SCAVENGING COEFFICIENT                       (Trajectory)

!    *PTEN*         PROVISIONAL ENVIRONMENT TEMPERATURE (T+1)       K
!    *PQEN*         PROVISIONAL ENVIRONMENT SPEC. HUMIDITY (T+1)  KG/KG
!    *PUEN*         PROVISIONAL ENVIRONMENT U-VELOCITY (T+1)       M/S
!    *PVEN*         PROVISIONAL ENVIRONMENT V-VELOCITY (T+1)       M/S
!    *PVERVEL*      VERTICAL VELOCITY                             PA/S
!    *PQSEN*        ENVIRONMENT SPEC. SATURATION HUMIDITY (T+1)   KG/KG
!    *PQHFL*        MOISTURE FLUX (EXCEPT FROM SNOW EVAP.)        KG/(SM2)
!    *PAHFS*        SENSIBLE HEAT FLUX                            W/M2
!    *PAP*          PROVISIONAL PRESSURE ON FULL LEVELS             PA
!    *PAPH*         PROVISIONAL PRESSURE ON HALF LEVELS             PA
!    *PGEO*         GEOPOTENTIAL                                  M2/S2
!    *PGEOH*        GEOPOTENTIAL ON HALF LEVELS                   M2/S2
!    *PCEN*         CHEMICAL TRACERS                             

!    UPDATED PARAMETERS (REAL):

!    *PTENT5*       TEMPERATURE TENDENCY                         (Trajectory)     
!    *PTENQ5*       MOISTURE TENDENCY                            (Trajectory)  
!    *PTENU5*       TENDENCY OF U-COMP. OF WIND                  (Trajectory)  
!    *PTENV5*       TENDENCY OF V-COMP. OF WIND                  (Trajectory)  
!    *PTENC5*       TRACER TENDENCY                              (Trajectory)

!    *PTENT*        TEMPERATURE TENDENCY                           K/S
!    *PTENQ*        MOISTURE TENDENCY                             KG/(KG S)
!    *PTENU*        TENDENCY OF U-COMP. OF WIND                    M/S2
!    *PTENV*        TENDENCY OF V-COMP. OF WIND                    M/S2
!    *PTENC*        TRACER TENDENCY                                1/S

!    OUTPUT PARAMETERS (LOGICAL):

!    *LDCUM*        FLAG: .TRUE. FOR CONVECTIVE POINTS 
!    *LDSC*         FLAG: .TRUE. FOR SC-POINTS

!    OUTPUT PARAMETERS (INTEGER):

!    *KTYPE*        TYPE OF CONVECTION
!                       1 = PENETRATIVE CONVECTION
!                       2 = SHALLOW CONVECTION
!                       3 = MIDLEVEL CONVECTION
!    *KCBOT*        CLOUD BASE LEVEL
!    *KCTOP*        CLOUD TOP LEVEL
!    *KBOTSC*       CLOUD BASE LEVEL FOR SC-CLOUDS

!    OUTPUT PARAMETERS (REAL):

!    *PTU5*         TEMPERATURE IN UPDRAFTS                      (Trajectory)    
!    *PQU5*         SPEC. HUMIDITY IN UPDRAFTS                   (Trajectory)   
!    *PLU5*         LIQUID WATER CONTENT IN UPDRAFTS             (Trajectory)   
!    *PLUDE5*       DETRAINED LIQUID WATER                       (Trajectory)    
!    *PENTH5*       INCREMENT OF DRY STATIC ENERGY               (Trajectory)    
!    *PMFLXR5*      CONVECTIVE RAIN FLUX                         (Trajectory)    
!    *PMFLXS5*      CONVECTIVE SNOW FLUX                         (Trajectory)    
!    *PRAIN5*       TOTAL PRECIP. PRODUCED IN CONV. UPDRAFTS     (Trajectory)    
!                   (NO EVAPORATION IN DOWNDRAFTS)
!    *PMFU*         MASSFLUX UPDRAFTS                            (Trajectory)
!    *PMFD*         MASSFLUX DOWNDRAFTS                          (Trajectory)

!    *PTU*          TEMPERATURE IN UPDRAFTS                         K
!    *PQU*          SPEC. HUMIDITY IN UPDRAFTS                    KG/KG
!    *PLU*          LIQUID WATER CONTENT IN UPDRAFTS              KG/KG
!    *PLUDE*        DETRAINED LIQUID WATER                        KG/(M2*S)
!    *PENTH*        INCREMENT OF DRY STATIC ENERGY                 J/(KG*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)
!    *PMFU*         MASSFLUX UPDRAFTS                             KG/(M2*S)
!    *PMFD*         MASSFLUX DOWNDRAFTS                           KG/(M2*S)

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

!       CUININTL:  INITIALIZES VALUES AT VERTICAL GRID USED IN CU-PARAMETR.
!       CUBASENTL: CLOUD BASE CALCULATION FOR PENETR.AND SHALLOW CONVECTION
!       CUASCNTL:  CLOUD ASCENT FOR ENTRAINING PLUME
!       CUDLFSNTL: DETERMINES VALUES AT LFS FOR DOWNDRAFTS
!       CUDDRAFNTL:DOES MOIST DESCENT FOR CUMULUS DOWNDRAFTS
!       CUFLXNTL:  FINAL ADJUSTMENTS TO CONVECTIVE FLUXES (ALSO IN PBL)
!       CUDQDTNTL: UPDATES TENDENCIES FOR T AND Q
!       CUDUDVTL: UPDATES TENDENCIES FOR U AND V

!     SWITCHES.
!     --------

!          LMFPEN=.TRUE.   PENETRATIVE CONVECTION IS SWITCHED ON
!          LMFSCV=.TRUE.   SHALLOW CONVECTION IS SWITCHED ON
!          LMFMID=.TRUE.   MIDLEVEL CONVECTION IS SWITCHED ON
!          LMFDD=.TRUE.    CUMULUS DOWNDRAFTS SWITCHED ON
!          LMFIT=.TRUE.    UPDRAUGHT ITERATION
!          LMFDUDV=.TRUE.  CUMULUS FRICTION SWITCHED ON

!     MODEL PARAMETERS (DEFINED IN SUBROUTINE CUPARAM)
!     ------------------------------------------------
!     ENTRPEN    ENTRAINMENT RATE FOR PENETRATIVE CONVECTION
!     ENTRSCV    ENTRAINMENT RATE FOR SHALLOW CONVECTION
!     ENTRMID    ENTRAINMENT RATE FOR MIDLEVEL CONVECTION
!     ENTRDD     ENTRAINMENT RATE FOR CUMULUS DOWNDRAFTS
!     RMFCMAX    MAXIMUM MASSFLUX VALUE ALLOWED FOR
!     RMFCMIN    MINIMUM MASSFLUX VALUE (FOR SAFETY)
!     RMFDEPS    FRACTIONAL MASSFLUX FOR DOWNDRAFTS AT LFS
!     RPRCON     COEFFICIENT FOR CONVERSION FROM CLOUD WATER TO RAIN

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

!          PAPER ON MASSFLUX SCHEME (TIEDTKE,1989)

!          MODIFICATIONS
!          -------------
!             92-09-21 : Update to Cy44      J.-J. MORCRETTE
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        P. Lopez      27-Feb-2006 Added default downdraft initializations
!             06-02-11 : Enable implicit TQ     P. BECHTOLD
!                        add Tracers 
!             07-10-09 : Added KE dissipation   P.BECHTOLD
!----------------------------------------------------------------------

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

USE YOMCST   , ONLY : RG       ,RD       ,RCPD     ,RETV     ,&
 & RLVTT    ,RLSTT    ,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 YOECUMF  , ONLY : ENTRPEN  ,ENTRSCV  ,LMFDD    ,LMFDUDV  ,&
 & RTAU      ,RDEPTHS ,LMFSCV  ,LMFPEN   ,LMFIT    ,NJKT2    ,&
 & RMFCFL    ,RMFLIC  ,RMFLIA  ,RMFSOLUV ,&
 & RUVPER    ,RMFSOLTQ,RMFSOLCT,RMFCMIN  ,LMFSMOOTH,LMFWSTAR ,&
 & LMFUVDIS
USE YOEPHY   , ONLY : LMFTRAC  ,LMFSCAV
 
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)    :: KTRAC
LOGICAL           ,INTENT(IN)    :: LDLAND(KLON) 
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(IN)    :: PUEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLITOT5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVERVEL5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQSEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQHFL5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAHFS5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAP5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPH5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEO5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOH5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCEN5(KLON,KLEV,KTRAC) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSCAV5(KTRAC) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENT5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENQ5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENV5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENC5(KLON,KLEV,KTRAC)
LOGICAL           ,INTENT(INOUT) :: LDCUM(KLON) 
INTEGER(KIND=JPIM),INTENT(INOUT) :: KTYPE(KLON) 
INTEGER(KIND=JPIM),INTENT(INOUT) :: KCBOT(KLON) 
INTEGER(KIND=JPIM),INTENT(INOUT) :: KCTOP(KLON) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KBOTSC(KLON) 
LOGICAL           ,INTENT(OUT)   :: LDSC(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLUDE5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PENTH5(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(INOUT) :: PMFU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFD5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTEN(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQEN(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUEN(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVEN(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVERVEL(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQSEN(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQHFL(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAHFS(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPH(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEO(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOH(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCEN(KLON,KLEV,KTRAC) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENT(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENQ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENV(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENC(KLON,KLEV,KTRAC) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLUDE(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PENTH(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) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFD(KLON,KLEV) 
REAL(KIND=JPRB) ::     ZTENH5(KLON,KLEV),      ZQENH5(KLON,KLEV),&
 & ZQSENH5(KLON,KLEV),&
 & ZTD5(KLON,KLEV),        ZQD5(KLON,KLEV),&
 & ZMFUS5(KLON,KLEV),      ZMFDS5(KLON,KLEV),&
 & ZMFUQ5(KLON,KLEV),      ZMFDQ5(KLON,KLEV),&
 & ZDMFUP5(KLON,KLEV),     ZDMFDP5(KLON,KLEV),&
 & ZDMFUPC5(KLON,KLEV),    ZDMFDPC5(KLON,KLEV),&
 & ZMFUL5(KLON,KLEV),      ZRFL5(KLON),&
 & ZUU5(KLON,KLEV),        ZVU5(KLON,KLEV),&
 & ZUD5(KLON,KLEV),        ZVD5(KLON,KLEV),&
 & ZKINEU5(KLON,KLEV),     ZKINED5(KLON,KLEV)
REAL(KIND=JPRB) ::     ZENTR5(KLON),           ZHCBASE5(KLON),&
 & ZMFUB5(KLON),           ZMFUB15(KLON),&
 & ZDQCV5(KLON)  
REAL(KIND=JPRB) ::                 ZDPMEL5(KLON,KLEV),ZLGLAC5(KLON,KLEV)
REAL(KIND=JPRB) ::     ZDHPBL5(KLON),      zwubase5(klon)

REAL(KIND=JPRB) ::     ZDMFEN5(KLON,KLEV),     ZDMFDE5(KLON,KLEV),&
 & ZMFUDE_RATE5(KLON,KLEV), ZMFDDE_RATE5(KLON,KLEV)

REAL(KIND=JPRB) ::     ZTENH(KLON,KLEV),       ZQENH(KLON,KLEV),&
 & ZQSENH(KLON,KLEV),&
 & ZTD(KLON,KLEV),         ZQD(KLON,KLEV),&
 & ZMFUS(KLON,KLEV),       &
 & ZMFUQ(KLON,KLEV),       &
 & ZDMFUP(KLON,KLEV),      &
 & ZMFUL(KLON,KLEV),       &
 & ZUU(KLON,KLEV),         ZVU(KLON,KLEV),&
 & ZUD(KLON,KLEV),         ZVD(KLON,KLEV)  
REAL(KIND=JPRB) ::     ZENTR(KLON),&
 & ZMFUB(KLON)  

INTEGER(KIND=JPIM) ::  ILAB(KLON,KLEV),        IDTOP(KLON),ICTOP0(KLON),ILWMIN(KLON)
INTEGER(KIND=JPIM) ::  IDPL(KLON) ! departure level for convection

REAL(KIND=JPRB) ::     zcape5(klon),                   zheat5(klon)

LOGICAL ::  LLDDRAF(KLON), LLDDRAF3(KLON), LLDCUM(KLON)
LOGICAL ::  LLO1, LLO2(KLON)

INTEGER(KIND=JPIM) :: IKB, ITOPM2, JK, IK, JL

REAL(KIND=JPRB) ::  Z3S5,&
 & ZCONS2, ZCONS, ZDH5, ZDQMIN5, ZDZ5, ZEPS5, ZFAC5, ZMFMAX5, ZPBMPT5, ZQUMQE5, ZRO5,&
 & ZMFA5,  ZERATE5, ZDERATE5, ZORCPD5,&
 & ZDUTEN5,ZDVTEN5, ZTDIS5

! for w Trigger
REAL(KIND=JPRB) :: ZTAU5(KLON)   ! adjustment time
REAL(KIND=JPRB) :: ZWMEAN5(KLON) ! mean updraught velocity

! scaling factor and momentum massflux
REAL(KIND=JPRB) :: ZMFS5(KLON), ZMFUUS5(KLON,KLEV), ZMFDUS5(KLON,KLEV) ,&
                 & ZMFUUB5(KLON), ZMFUVB5(KLON),&
                 & ZTENU5(KLON,KLEV), ZTENV5(KLON,KLEV),&
                 & ZTENU (KLON,KLEV), ZTENV (KLON,KLEV),&
                 & ZMF_SHAL5(KLON),&
                 & ZUV25(KLON,KLEV), ZSUM125(KLON), ZSUM225(KLON)

! Allocatable arrays for Tracers
REAL(KIND=JPRB)::ZSCAV5(KTRAC)
REAL(KIND=JPRB), DIMENSION(:,:)  ,ALLOCATABLE  :: ZMFUUC5, ZMFDUC5, ZMFUDR5, ZMFDDR5,&
                                                 &ZMFUUC,  ZMFDUC,  ZMFUDR,  ZMFDDR,&
                                                 &ZDMFUPC, ZDMFDPC

REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "cuascntl.intfb.h"
#include "cubasentl.intfb.h"
#include "cuddrafntl.intfb.h"
#include "cudlfsntl.intfb.h"
#include "cudtdqntl.intfb.h"
#include "cududvtl.intfb.h"
#include "cuflxntl.intfb.h"
#include "cuinintl.intfb.h"
#include "cuctracertl.intfb.h"

#include "fcttre.h"

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

!     1.           SPECIFY CONSTANTS AND PARAMETERS
!                  --------------------------------

IF (LHOOK) CALL DR_HOOK('CUMASTRNTL',0,ZHOOK_HANDLE)
ZCONS2=RMFCFL/(RG*PTSPHY)
ZCONS =1.0_JPRB/(RG*PTSPHY)
ZORCPD5=1.0_JPRB/RCPD
!----------------------------------------------------------------------

!*    2.           INITIALIZE VALUES AT VERTICAL GRID POINTS IN 'CUINITL'
!                  -----------------------------------------------------

CALL CUININTL &
 & ( KIDIA,    KFDIA,    KLON,    KLEV,&
 & PTEN5,    PQEN5,    PQSEN5,   PUEN5,    PVEN5,&
 & PVERVEL5, PGEO5,    PAPH5,&
 & ILWMIN,   ILAB,&
 & ZTENH5,   ZQENH5,   ZQSENH5,  PGEOH5,&
 & PTU5,     PQU5,     ZTD5,     ZQD5,&
 & ZUU5,     ZVU5,     ZUD5,     ZVD5,&
 & PLU5,&
 & PTEN,     PQEN,     PQSEN,    PUEN,     PVEN,&
 & PVERVEL,  PGEO,     PAPH,&
 & ZTENH,    ZQENH,    ZQSENH,   PGEOH,&
 & PTU,      PQU,      ZTD,      ZQD,&
 & ZUU,      ZVU,      ZUD,      ZVD,&
 & PLU     )  

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

!*    3.0          CLOUD BASE CALCULATIONS
!                  -----------------------

!*             (A) DETERMINE CLOUD BASE VALUES IN 'CUBASENTL'
!                  -----------------------------------------

CALL CUBASENTL &
 & ( KIDIA,    KFDIA,    KLON,     KLEV,&
 & ZTENH5,   ZQENH5,   PGEOH5,   PAPH5,&
 & pqhfl5,   pahfs5,&
 & PTEN5,    PQEN5,    PGEO5,&
 & PUEN5,    PVEN5,&
 & PTU5,     PQU5,     PLU5,     ZUU5,     ZVU5,   zwubase5,&
 & ILAB,     LDCUM,    LDSC,     KCBOT,    KBOTSC,&
 & ictop0,   idpl )  


!*             (B) DETERMINE TOTAL MOISTURE CONVERGENCE AND
!*                 THEN DECIDE ON TYPE OF CUMULUS CONVECTION 
!                  -----------------------------------------

DO JL=KIDIA,KFDIA
  ZDQCV5(JL) =0.0_JPRB
  ZDHPBL5(JL)=0.0_JPRB
  IDTOP(JL)=0
ENDDO
DO JK=NJKT2,KLEV
  DO JL=KIDIA,KFDIA
    ZDQCV5(JL)=ZDQCV5(JL)+MAX(0.0_JPRB,PTENQ5(JL,JK))*(PAPH5(JL,JK+1)-PAPH5(JL,JK))
    IF(LDCUM(JL).and.JK >= KCBOT(JL)) THEN
      ZDHPBL5(JL)=ZDHPBL5(JL)+(RLVTT*PTENQ5(JL,JK)+&
       & RCPD*PTENT5(JL,JK))&
       & *(PAPH5(JL,JK+1)-PAPH5(JL,JK))  
    ENDIF
  ENDDO
ENDDO

!*             (A) ESTIMATE CLOUD HEIGHT FOR ENTRAINMENT/DETRAINMENT
!*                 CALCULATIONS IN CUASC (MAX.POSSIBLE CLOUD HEIGHT
!*                 FOR NON-ENTRAINING PLUME, FOLLOWING A.-S.,1974)
!                  -------------------------------------------------

!DIR$ IVDEP
!OCL NOVREC      
DO JL=KIDIA,KFDIA
  IF (LDCUM(JL)) then
    IKB=KCBOT(JL)
    ZHCBASE5(JL)=RCPD*PTU5(JL,IKB)+PGEOH5(JL,IKB)+RLVTT*PQU5(JL,IKB)
    IF(.NOT.LDCUM(JL)) ICTOP0(JL)=-1
  ELSE
    ZHCBASE5(JL)=0.0_JPRB
  ENDIF
ENDDO

!*                 SPECIFY INITIAL CLOUD TYPE
!*

DO JL=KIDIA,KFDIA
  IF (LDCUM(JL)) THEN
    IKB=KCBOT(JL)
    ITOPM2=ICTOP0(JL)
    ZPBMPT5=PAPH5(JL,IKB)-PAPH5(JL,ITOPM2)
    IF (ZPBMPT5 >= RDEPTHS) THEN
      KTYPE(JL)=1
    ELSE
      KTYPE(JL)=2
    ENDIF
  ELSE
    KTYPE(JL)=0
  ENDIF
ENDDO

!*             (C) DETERMINE MOISTURE SUPPLY FOR BOUNDARY LAYER
!*                 AND DETERMINE CLOUD BASE MASSFLUX IGNORING
!*                 THE EFFECTS OF DOWNDRAFTS AT THIS STAGE
!                  ------------------------------------------

!DIR$ IVDEP
!OCL NOVREC
IF (LMFWSTAR) THEN
  DO JL=KIDIA,KFDIA
    IF (LDCUM(JL)) THEN
      IKB=KCBOT(JL)
      ZDZ5=MAX(0.0_JPRB,MIN(1.5E3_JPRB,(PGEOH5(JL,IKB)-PGEOH5(JL,KLEV+1))/RG))
      ZMF_SHAL5(JL)=0.07_JPRB*(RG/PTEN5(JL,KLEV)*ZDZ5*&
        &MAX(0.0_JPRB,-PAHFS5(JL,KLEV+1)*ZORCPD5-RETV*PTEN5(JL,KLEV)*PQHFL5(JL,KLEV+1)))**.3333
      ZMFMAX5=(PAPH5(JL,IKB)-PAPH5(JL,IKB-1))*ZCONS2*RMFLIC+RMFLIA
      ZMF_SHAL5(JL)=MIN(ZMF_SHAL5(JL),ZMFMAX5)
    ENDIF
  ENDDO
ENDIF

DO JL=KIDIA,KFDIA
  IF (LDCUM(JL)) THEN
    IKB=KCBOT(JL)
    ZMFMAX5=(PAPH5(JL,IKB)-PAPH5(JL,IKB-1))*ZCONS2*RMFLIC+RMFLIA

! deep convection

    IF (KTYPE(JL) == 1) THEN
      ZMFUB5(JL)=ZMFMAX5*0.1_JPRB
      ZENTR5(JL)=ENTRPEN

    ELSEIF (KTYPE(JL) == 2) THEN

! shallow convection

      ZQUMQE5=PQU5(JL,IKB)+PLU5(JL,IKB)-ZQENH5(JL,IKB)
      ZDQMIN5=MAX(0.01_JPRB*ZQENH5(JL,IKB),1.E-10_JPRB)
      ZDH5=RCPD*(PTU5(JL,IKB)-ZTENH5(JL,IKB))+RLVTT*ZQUMQE5
      ZDH5=RG*MAX(ZDH5,1.E5_JPRB*ZDQMIN5)
      IF (ZDHPBL5(JL) > 0.0_JPRB) THEN
        ZMFUB5(JL)=ZDHPBL5(JL)/ZDH5
      !EPS: temporary solution for explicit
        if(ptsphy>1800._jprb.and.rmfcfl==1.0_jprb) then
          ZMFUB5(JL)=MIN(ZMFUB5(JL),3._JPRB*ZMFMAX5)
        else
          ZMFUB5(JL)=MIN(ZMFUB5(JL),ZMFMAX5)
        endif
      ELSE
        ZMFUB5(JL)=ZMFMAX5*0.1_JPRB
        LDCUM(JL)=.FALSE.
      ENDIF
      IF(LMFWSTAR) ZMFUB5(JL)=ZMF_SHAL5(JL)
      ZENTR5(JL)=ENTRSCV
    ENDIF

  ELSE

! no buoyancy cloud base from surface
! set cloud base mass flux and mixing rate
! to default value for safety

    ZMFUB5(JL)=0.0_JPRB
    ZENTR5(JL)=ENTRSCV
  ENDIF
ENDDO

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

!*    4.0          DETERMINE CLOUD ASCENT FOR ENTRAINING PLUME
!                  -------------------------------------------

!*             (A) ESTIMATE CLOUD HEIGHT FOR ENTRAINMENT/DETRAINMENT
!*                 CALCULATIONS IN CUASC (MAX.POSSIBLE CLOUD HEIGHT
!*                 FOR NON-ENTRAINING PLUME, FOLLOWING A.-S.,1974)
!                  -------------------------------------------------

! CALCULATIONS NOW DONE IS SECTION 3 ABOVE SO THAT
! INITIAL CLOUD DEPTH CAN BE USED TO SPECIFY
! THE TYPE OF CONVECTION

!*             (B) DO ASCENT IN 'CUASCNTL'IN ABSENCE OF DOWNDRAFTS
!                  -----------------------------------------------

CALL CUASCNTL &
 & ( KIDIA,    KFDIA,    KLON,    KLEV,&
 & PTSPHY,&
 & ZTENH5,   ZQENH5,   PUEN5,    PVEN5,&
 & PTEN5,    PQEN5,    PQSEN5,   PLITOT5,&
 & PGEO5,    PGEOH5,   PAP5,     PAPH5,&
 & PVERVEL5, zwubase5, ILWMIN,&
 & LDLAND,   LDCUM,    KTYPE,    ILAB,&
 & PTU5,     PQU5,     PLU5,&
 & PMFU5,    ZMFUB5,   ZENTR5,   ZLGLAC5,&
 & ZMFUS5,   ZMFUQ5,   ZMFUL5,   PLUDE5,   ZDMFUP5,&
 & ZDMFEN5,  ZMFUDE_RATE5,       ZKINEU5,  ZWMEAN5,&
 & KCBOT,    KCTOP,    ICTOP0,   idpl,&
 & ZTENH,    ZQENH,    PUEN,     PVEN,&
 & PTEN,     PQEN,     PQSEN,&
 & PGEO,     PGEOH,    PAPH,&
 & PVERVEL,&
 & PTU,      PQU,      PLU,&
 & PMFU,     ZMFUB,    ZENTR,&
 & ZMFUS,    ZMFUQ,    ZMFUL,    ZDMFUP  )  

!*         (C) CHECK CLOUD DEPTH AND CHANGE ENTRAINMENT RATE ACCORDINGLY
!              CALCULATE PRECIPITATION RATE (FOR DOWNDRAFT CALCULATION)
!              -----------------------------------------------------

!DIR$ IVDEP
!OCL NOVREC
DO JL=KIDIA,KFDIA
  IF (LDCUM(JL)) THEN
    IKB=KCBOT(JL)
    ITOPM2=KCTOP(JL)
    ZPBMPT5=PAPH5(JL,IKB)-PAPH5(JL,ITOPM2)
    IF(KTYPE(JL) == 1.AND.ZPBMPT5 < RDEPTHS) KTYPE(JL)=2
    IF(KTYPE(JL) == 2.AND.ZPBMPT5 >= RDEPTHS) KTYPE(JL)=1
    ICTOP0(JL)=KCTOP(JL)
    IF(KTYPE(JL) == 1) ZENTR5(JL)=ENTRPEN
    IF(KTYPE(JL) == 2) ZENTR5(JL)=ENTRSCV
  ENDIF
  ZRFL5(JL)=ZDMFUP5(JL,1)
ENDDO
DO JK=2,KLEV
  DO JL=KIDIA,KFDIA
    ZRFL5(JL)=ZRFL5(JL)+ZDMFUP5(JL,JK)
  ENDDO
ENDDO
DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    PMFD5(JL,JK)=0.0_JPRB
    ZMFDS5(JL,JK)=0.0_JPRB
    ZMFDQ5(JL,JK)=0.0_JPRB
    ZDMFDP5(JL,JK)=0.0_JPRB
    ZDPMEL5(JL,JK)=0.0_JPRB
    ZDMFDE5(JL,JK)=0.0_JPRB
    ZMFDDE_RATE5(JL,JK)=0.0_JPRB
  ENDDO
ENDDO
IF(LMFUVDIS) THEN
  DO JK=1,KLEV
    DO JL=KIDIA,KFDIA
      ZTENU5(JL,JK)=PTENU5(JL,JK)
      ZTENV5(JL,JK)=PTENV5(JL,JK)
      ZTENU (JL,JK)=PTENU (JL,JK)
      ZTENV (JL,JK)=PTENV (JL,JK)
    ENDDO
  ENDDO
ENDIF

DO JL=KIDIA,KFDIA
  LLDDRAF(JL)=.FALSE.
ENDDO

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

!*    5.0          CUMULUS DOWNDRAFT CALCULATIONS
!                  ------------------------------

IF(LMFDD) THEN

!*             (A) DETERMINE LFS IN 'CUDLFSNTL'
!                  ----------------------------

  CALL CUDLFSNTL &
   & ( KIDIA,    KFDIA,    KLON,    KLEV,&
   & KCBOT,    KCTOP,    LDCUM,&
   & ZTENH5,   ZQENH5,&
   & PTEN5,    PQSEN5,   PGEO5,&
   & PGEOH5,   PAPH5,    PTU5,     PQU5,&
   & ZMFUB5,   ZRFL5,&
   & ZTD5,     ZQD5,&
   & PMFD5,    ZMFDS5,   ZMFDQ5,   ZDMFDP5,&
   & IDTOP,    LLDDRAF )  

!*            (B)  DETERMINE DOWNDRAFT T,Q AND FLUXES IN 'CUDDRAFNTL'
!                  --------------------------------------------------

  CALL CUDDRAFNTL &
   & ( KIDIA,    KFDIA,    KLON,    KLEV,&
   & LLDDRAF,&
   & ZTENH5,   ZQENH5,&
   & PGEO5,    PGEOH5,   PAPH5,    ZRFL5,&
   & ZTD5,     ZQD5,     PMFU5,&
   & PMFD5,    ZMFDS5,   ZMFDQ5,   ZDMFDP5,&
   & ZDMFDE5,  ZMFDDE_RATE5,       ZKINED5 )  

ENDIF

!*                (C)  RECALCULATE CLOUD BASE MASSFLUX FROM A
!*                 CAPE CLOSURE FOR DEEP CONVECTION (KTYPE=1)
!*                 AND BY PBL EQUILIBRUM TAKING DOWNDRAFTS INTO
!*                 ACCOUNT FOR SHALLOW CONVECTION (KTYPE=2)          
!                  --------------------------------------------

!   DEEP CONVECTION

DO JL=KIDIA,KFDIA
  ZHEAT5(JL)=0.0_JPRB
  ZCAPE5(JL)=0.0_JPRB
  ZMFUB15(JL)=ZMFUB5(JL)
ENDDO

DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    LLO1=LDCUM(JL).AND.KTYPE(JL) == 1
    IF(LLO1.AND.JK <= KCBOT(JL).AND.JK > KCTOP(JL)) THEN
      IKB=KCBOT(JL)
      ZRO5=PAPH5(JL,JK)/(RD*ZTENH5(JL,JK)*(1.0_JPRB+RETV*ZQENH5(JL,JK)))
      ZDZ5=(PGEOH5(JL,JK-1)-PGEOH5(JL,JK))
      ZHEAT5(JL)=ZHEAT5(JL) +&
       & (  (PTEN5(JL,JK-1)-PTEN5(JL,JK) + ZDZ5*ZORCPD5)/ZTENH5(JL,JK)&
       & +  RETV*(PQEN5(JL,JK-1)-PQEN5(JL,JK))  ) *&
       & (RG*(PMFU5(JL,JK)+PMFD5(JL,JK)))/ZRO5  
      ZCAPE5(JL)=ZCAPE5(JL) +&
       & ((PTU5(JL,JK)-ZTENH5(JL,JK))/ZTENH5(JL,JK)&
       & +RETV*(PQU5(JL,JK)-ZQENH5(JL,JK))&
       & -PLU5(JL,JK) ) * ZDZ5  
    ENDIF
  ENDDO
ENDDO

DO JL=KIDIA,KFDIA
  IF(LDCUM(JL).AND.KTYPE(JL) == 1) THEN
    IKB=KCBOT(JL)
    IK=KCTOP(JL)
    ZCAPE5(JL)=MAX(0.0_JPRB,MIN(ZCAPE5(JL),5000.0_JPRB))
    ZHEAT5(JL)=MAX(1.E-4_JPRB,ZHEAT5(JL))
    ZTAU5(JL)=(PGEOH5(JL,IK)-PGEOH5(JL,IKB))/((2.0_JPRB+MIN(15.0_JPRB,ZWMEAN5(JL)))*RG)*RTAU
    ZTAU5(JL)=MAX(PTSPHY,MIN(10800._JPRB,ZTAU5(JL)))
    ZTAU5(JL)=MAX(720._JPRB,ZTAU5(JL))
    ZMFUB15(JL)=(ZCAPE5(JL)*ZMFUB5(JL))/(ZHEAT5(JL)*ZTAU5(JL))
    ZMFUB15(JL)=MAX(ZMFUB15(JL),0.001_JPRB)
    ZMFMAX5=(PAPH5(JL,IKB)-PAPH5(JL,IKB-1))*ZCONS2*RMFLIC+RMFLIA
    ZMFUB15(JL)=MIN(ZMFUB15(JL),ZMFMAX5)
  ENDIF
ENDDO

!  SHALLOW CONVECTION AND MID_LEVEL

!DIR$ IVDEP
!OCL NOVREC
DO JL=KIDIA,KFDIA
  IF ( LDCUM(JL) .AND. (KTYPE(JL) == 2.OR. KTYPE(JL) == 3) ) THEN
    IKB=KCBOT(JL)
    IF(PMFD5(JL,IKB) < 0.0_JPRB) THEN
      ZEPS5=-PMFD5(JL,IKB)/MAX(ZMFUB5(JL),1.E-10_JPRB)
    ELSE
      ZEPS5=0.0_JPRB
    ENDIF
    ZQUMQE5=PQU5(JL,IKB)+PLU5(JL,IKB)-&
     & ZEPS5*ZQD5(JL,IKB)-(1.0_JPRB-ZEPS5)*ZQENH5(JL,IKB)  
    ZDQMIN5=MAX(0.01_JPRB*ZQENH5(JL,IKB),1.E-10_JPRB)
! maximum permisable value of ud base mass flux
    ZMFMAX5=(PAPH5(JL,IKB)-PAPH5(JL,IKB-1))*ZCONS2*RMFLIC+RMFLIA

! shallow convection

    IF(KTYPE(JL) == 2) THEN
      ZDH5=RCPD*(PTU5(JL,IKB)-ZEPS5*ZTD5(JL,IKB)-&
       & (1.0_JPRB-ZEPS5)*ZTENH5(JL,IKB))+RLVTT*ZQUMQE5  
      ZDH5=RG*MAX(ZDH5,1.E5_JPRB*ZDQMIN5)
      IF(ZDHPBL5(JL) > 0.0_JPRB) THEN
        ZMFUB15(JL)=ZDHPBL5(JL)/ZDH5
      ELSE
        ZMFUB15(JL)=ZMFUB5(JL)
      ENDIF
   !EPS: temporary solution for explicit
      if(ptsphy>1800._jprb.and.rmfcfl==1.0_jprb) then
        zmfub15(jl)=min(zmfub15(jl),3._jprb*zmfmax5)
      else
        zmfub15(jl)=min(zmfub15(jl),zmfmax5)
      endif
      IF(LMFWSTAR) ZMFUB15(JL)=ZMF_SHAL5(JL)
    ENDIF

! mid-level convection

    IF(KTYPE(JL) == 3)THEN
      ZMFUB15(JL)=ZMFUB5(JL)*(1.0_JPRB+ZEPS5)
      ZMFUB15(JL)=MIN(ZMFUB15(JL),ZMFMAX5)
    ENDIF

  ENDIF
ENDDO

! rescale DD fluxes if deep and shallow convection

DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    if ( LLDDRAF(JL) .and.( ktype(jl) == 1.or. ktype(jl) == 2 ) ) THEN
!jfm            ZFAC5=ZMFUB15(JL)/MAX(ZMFUB5(JL),1.E-10)
      IF (ZMFUB5(JL) < 1.E-10_JPRB) THEN
        Z3S5=1.E-10_JPRB
      ELSE
        Z3S5=ZMFUB5(JL)
      ENDIF
      ZFAC5=ZMFUB15(JL)/Z3S5
      PMFD5(JL,JK)=PMFD5(JL,JK)*ZFAC5
      ZMFDS5(JL,JK)=ZMFDS5(JL,JK)*ZFAC5
      ZMFDQ5(JL,JK)=ZMFDQ5(JL,JK)*ZFAC5
      ZDMFDP5(JL,JK)=ZDMFDP5(JL,JK)*ZFAC5
      ZMFDDE_RATE5(JL,JK)=ZMFDDE_RATE5(JL,JK)*ZFAC5
    ENDIF
  ENDDO
ENDDO

IF(LMFIT) THEN

  DO JK=2,KLEV-1
    DO JL=KIDIA,KFDIA
      ZUU5(JL,JK)=PUEN5(JL,JK-1)
      ZVU5(JL,JK)=PVEN5(JL,JK-1)
    ENDDO
  ENDDO
  
  ! reset updraught mass flux at cloud base
  
  DO JL=KIDIA,KFDIA
    ZMFUB5(JL)=ZMFUB15(JL)
  ENDDO

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

!*    6.0          DETERMINE FINAL CLOUD ASCENT FOR ENTRAINING PLUME
!*                 FOR PENETRATIVE CONVECTION (TYPE=1),
!*                 FOR SHALLOW TO MEDIUM CONVECTION (TYPE=2)
!*                 AND FOR MID-LEVEL CONVECTION (TYPE=3).
!                  -------------------------------------------------

  CALL CUASCNTL &
   & ( KIDIA,    KFDIA,    KLON,    KLEV,&
   & PTSPHY,&
   & ZTENH5,   ZQENH5,   PUEN5,    PVEN5,&
   & PTEN5,    PQEN5,    PQSEN5,   PLITOT5,&
   & PGEO5,    PGEOH5,   PAP5,     PAPH5,&
   & PVERVEL5, zwubase5, ILWMIN,&
   & LDLAND,   LDCUM,    KTYPE,    ILAB,&
   & PTU5,     PQU5,     PLU5,&
   & PMFU5,    ZMFUB5,   ZENTR5,   ZLGLAC5,&
   & ZMFUS5,   ZMFUQ5,   ZMFUL5,   PLUDE5,   ZDMFUP5,&
   & ZDMFEN5,  ZMFUDE_RATE5,       ZKINEU5,  ZWMEAN5,&
   & KCBOT,    KCTOP,    ICTOP0,   idpl,&
   & ZTENH,    ZQENH,    PUEN,     PVEN,&
   & PTEN,     PQEN,     PQSEN,&
   & PGEO,     PGEOH,    PAPH,&
   & PVERVEL,&
   & PTU,      PQU,      PLU,&
   & PMFU,     ZMFUB,    ZENTR,&
   & ZMFUS,    ZMFUQ,    ZMFUL,    ZDMFUP )  
  
  DO JL=KIDIA,KFDIA
    IF (LDCUM(JL)) THEN
      IKB=KCBOT(JL)
      ITOPM2=KCTOP(JL)
      ZPBMPT5=PAPH5(JL,IKB)-PAPH5(JL,ITOPM2)
      IF(KTYPE(JL) == 1.AND.ZPBMPT5 < RDEPTHS) KTYPE(JL)=2
      IF(KTYPE(JL) == 2.AND.ZPBMPT5 >= RDEPTHS) KTYPE(JL)=1
    ENDIF
  ENDDO

ELSE

  DO JL=KIDIA,KFDIA
    IF(LDCUM(JL)) THEN
      ZMFS5(JL)=ZMFUB15(JL)/MAX(RMFCMIN,ZMFUB5(JL))
    ENDIF
  ENDDO
  DO JK=2,KLEV
    DO JL=KIDIA,KFDIA
      IF(LDCUM(JL).AND.JK>=KCTOP(JL)-1) THEN
        IKB=KCBOT(JL)
        IF(JK>IKB) THEN
          ZDZ5=((PAPH5(JL,KLEV+1)-PAPH5(JL,JK))/(PAPH5(JL,KLEV+1)-PAPH5(JL,IKB)))
          PMFU5(JL,JK)=PMFU5(JL,IKB)*ZDZ5
        ENDIF
        ZMFMAX5=(PAPH5(JL,JK)-PAPH5(JL,JK-1))*ZCONS2*RMFLIC+RMFLIA
        IF(PMFU5(JL,JK)*ZMFS5(JL)>ZMFMAX5) &
           & ZMFS5(JL)=MIN(ZMFS5(JL),ZMFMAX5/PMFU5(JL,JK))  
      ENDIF
    ENDDO
  ENDDO
  DO JK=2,KLEV
    DO JL=KIDIA,KFDIA
      IF(LDCUM(JL).AND.JK<=KCBOT(JL).AND.JK>=KCTOP(JL)-1) THEN
        PMFU5(JL,JK)=PMFU5(JL,JK)*ZMFS5(JL)
        ZMFUS5(JL,JK)=ZMFUS5(JL,JK)*ZMFS5(JL)
        ZMFUQ5(JL,JK)=ZMFUQ5(JL,JK)*ZMFS5(JL)
        ZMFUL5(JL,JK)=ZMFUL5(JL,JK)*ZMFS5(JL)
        ZDMFUP5(JL,JK)=ZDMFUP5(JL,JK)*ZMFS5(JL)
        ZDMFEN5(JL,JK)=ZDMFEN5(JL,JK)*ZMFS5(JL)
        PLUDE5(JL,JK)=PLUDE5(JL,JK)*ZMFS5(JL)
        ZMFUDE_RATE5(JL,JK)=ZMFUDE_RATE5(JL,JK)*ZMFS5(JL)
      ENDIF
    ENDDO
  ENDDO

ENDIF
!-----------------------------------------------------------------------

!*    6.5          IN CASE THAT EITHER DEEP OR SHALLOW IS SWITCHED OFF
!                  RESET LDCUM TO FALSE-> FLUXES SET TO ZERO IN CUFLXN
!                  ---------------------------------------------------

!                 exclude pathological convective points
DO JL=KIDIA,KFDIA
  IF(KTYPE(JL)==2.AND.KCBOT(JL)==KCTOP(JL).AND.KCBOT(JL)>=KLEV-1) THEN
    LDCUM(JL)=.FALSE.
    KTYPE(JL)=0
  ENDIF
ENDDO

IF (.NOT.LMFSCV .OR. .NOT.LMFPEN) THEN
  DO JL=KIDIA,KFDIA
    LLO2(JL)=.FALSE.
    IF((.NOT.LMFSCV .AND. KTYPE(JL)==2).OR.(.NOT.LMFPEN .AND. KTYPE(JL)==1)) THEN
      LLO2(JL)=.TRUE.
      LDCUM(JL)=.FALSE.
    ENDIF
  ENDDO
ENDIF
!-----------------------------------------------------------------------

!*    7.0          DETERMINE FINAL CONVECTIVE FLUXES IN 'CUFLXNTL'
!                  -----------------------------------------------

!- set DD mass fluxes to zero if downdraft top higher than cloud top
!  (because of inconsistency with second updraught)
DO JL=KIDIA,KFDIA
  IF(LLDDRAF(JL).AND.IDTOP(JL)<=KCTOP(JL)) THEN
    IDTOP(JL)=KCTOP(JL)+1
  ENDIF
ENDDO

DO JK=2,KLEV
  DO JL=KIDIA,KFDIA
    IF (LLDDRAF(JL)) THEN
      IF (JK<IDTOP(JL)) THEN
        PMFD5(JL,JK)=0.0_JPRB
        ZMFDS5(JL,JK)=0.0_JPRB
        ZMFDQ5(JL,JK)=0.0_JPRB
        ZMFDDE_RATE5(JL,JK)=0.0_JPRB
        ZDMFDP5(JL,JK)=0.0_JPRB
      ELSEIF (JK==IDTOP(JL)) THEN
        ZMFDDE_RATE5(JL,JK)=0.0_JPRB
      ENDIF
    ENDIF
  ENDDO
ENDDO

CALL CUFLXNTL &
 & ( KIDIA,    KFDIA,    KLON,    KLEV,&
 & PTSPHY,&
 & PTEN5,    PQEN5,    PQSEN5,   ZTENH5,   ZQENH5,&
 & PAPH5,    PAP5,     PGEOH5,   LDLAND,   LDCUM,&
 & KCBOT,    KCTOP,    IDTOP,    ITOPM2,&
 & KTYPE,    LLDDRAF,&
 & PMFU5,    PMFD5,    ZMFUS5,   ZMFDS5,&
 & ZMFUQ5,   ZMFDQ5,   ZMFUL5,   PLUDE5,&
 & ZDMFUP5,  ZDMFDP5,  ZDPMEL5,  ZLGLAC5,&
 & PMFLXR5,  PMFLXS5,  PRAIN5,   ZMFDDE_RATE5 )  

!- rescale DD fluxes if total mass flux becomes negative
!- correct DD detrainment rates if entrainment becomes negative
!- correct UD detrainment rates if entrainment becomes negative
!- conservation correction for precip

ZMFS5(:)=1.0_JPRB
!DO JK=2,KLEV-1
DO JK=2,KLEV  ! change for stability
   DO JL=KIDIA,KFDIA
      IF ( LLDDRAF(JL) .AND. JK>=IDTOP(JL)-1 ) THEN
         ZMFMAX5=PMFU5(JL,JK)*0.98_JPRB
         IF(PMFD5(JL,JK)+ZMFMAX5+1.E-15_JPRB<0._JPRB) THEN
            IF(ZMFS5(JL)>-ZMFMAX5/PMFD5(JL,JK)) THEN
              ZMFS5(JL)=-ZMFMAX5/PMFD5(JL,JK)
            ELSE
              ZMFS5(JL)=ZMFS5(JL)
            ENDIF
         ENDIF
      ENDIF
   ENDDO
ENDDO
ZMFUUB5(:)=0.0_JPRB
DO JK=2,KLEV
  DO JL=KIDIA,KFDIA
    IF ( ZMFS5(JL)<1.0_JPRB .AND. JK>=IDTOP(JL)-1 ) THEN
      PMFD5(JL,JK)=PMFD5(JL,JK)*ZMFS5(JL)
      ZMFDS5(JL,JK)=ZMFDS5(JL,JK)*ZMFS5(JL)
      ZMFDQ5(JL,JK)=ZMFDQ5(JL,JK)*ZMFS5(JL)
      ZMFDDE_RATE5(JL,JK)=ZMFDDE_RATE5(JL,JK)*ZMFS5(JL)
      ZMFUUB5(JL)=ZMFUUB5(JL)-(1.0_JPRB-ZMFS5(JL))*ZDMFDP5(JL,JK)
      PMFLXR5(JL,JK+1)=PMFLXR5(JL,JK+1)+ZMFUUB5(JL)
      ZDMFDP5(JL,JK)=ZDMFDP5(JL,JK)*ZMFS5(JL)
    ENDIF
   !ZDMFUPC5(JL,JK)=ZDMFUP5(JL,JK)
   !ZDMFDPC5(JL,JK)=ZDMFDP5(JL,JK)
  ENDDO
ENDDO

DO JK=2,KLEV-1
   DO JL=KIDIA,KFDIA
      IF ( LLDDRAF(JL) .AND. JK>=IDTOP(JL)-1 ) THEN
         ZERATE5=-PMFD5(JL,JK)+PMFD5(JL,JK-1)+ZMFDDE_RATE5(JL,JK)
         IF(ZERATE5<0.0_JPRB) THEN
           ZMFDDE_RATE5(JL,JK)=ZMFDDE_RATE5(JL,JK)-ZERATE5
         ENDIF
      ENDIF
      IF ( LDCUM(JL) .AND. JK>=KCTOP(JL)-1 ) THEN
         ZERATE5=PMFU5(JL,JK)-PMFU5(JL,JK+1)+ZMFUDE_RATE5(JL,JK)
         IF(ZERATE5<0.0_JPRB) THEN
           ZMFUDE_RATE5(JL,JK)=ZMFUDE_RATE5(JL,JK)-ZERATE5
         ENDIF
       ! ZDMFUP5(JL,JK)=ZDMFUP5(JL,JK)+ZDMFDP5(JL,JK)
         ZDMFUP5(JL,JK)=PMFLXR5(JL,JK+1)+PMFLXS5(JL,JK+1)&
                     &-PMFLXR5(JL,JK)-PMFLXS5(JL,JK)
         ZDMFDP5(JL,JK)=0.0_JPRB
      ENDIF
   ENDDO
ENDDO

! avoid negative humidities at ddraught top
DO JL=KIDIA,KFDIA
 IF(LLDDRAF(JL)) THEN
   JK=IDTOP(JL)
   IK=MIN(JK+1,KLEV)
   IF(ZMFDQ5(JL,JK)<0.3_JPRB*ZMFDQ5(JL,IK)) THEN
     IF(RMFSOLTQ==0.0_JPRB) THEN
       ZMFDQ5(JL,JK)=0.3_JPRB*ZMFDQ5(JL,IK)
     ELSE
       PMFD5(JL,JK)=0.3_JPRB*PMFD5(JL,IK)
     ENDIF
   ENDIF
 ENDIF
ENDDO

! avoid negative humidities near cloud top because gradient of precip flux
! and detrainment / liquid water flux too large
DO JK=2,KLEV
  DO JL=KIDIA,KFDIA
    IF(LDCUM(JL).AND.JK>=KCTOP(JL)-1.AND.JK<KCBOT(JL)) THEN
      ZDZ5=PTSPHY*RG/(PAPH5(JL,JK+1)-PAPH5(JL,JK))
      ZMFA5=ZMFUQ5(JL,JK+1)+ZMFDQ5(JL,JK+1)-ZMFUQ5(JL,JK)-ZMFDQ5(JL,JK)+&
      &ZMFUL5(JL,JK+1)-ZMFUL5(JL,JK)+ZDMFUP5(JL,JK)
      ZMFA5=(ZMFA5-PLUDE5(JL,JK))*ZDZ5
      IF(PQEN5(JL,JK)+ZMFA5<0.0_JPRB) THEN
        PLUDE5(JL,JK)=PLUDE5(JL,JK)+2.0_JPRB*(PQEN5(JL,JK)+ZMFA5)/ZDZ5
      ENDIF
      IF(PLUDE5(JL,JK)<0.0_JPRB) THEN
        PLUDE5(JL,JK)=0.0_JPRB
      ENDIF
    ENDIF
  ENDDO
ENDDO

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

!*    8.0          UPDATE TENDENCIES FOR T AND Q IN SUBROUTINE CUDTDQNTL
!                  -----------------------------------------------------

IF( RMFSOLTQ>0.0_JPRB) THEN
! derive updraught properties for implicit

  DO JK=2,KLEV
    DO JL=KIDIA,KFDIA
      IF(LDCUM(JL)) THEN
        IF(JK>KCBOT(JL)) THEN
          ZMFA5=1.0_JPRB/MAX(1.E-15_JPRB,PMFU5(JL,JK))
          PQU5(JL,JK)=ZQENH5(JL,JK)+ZMFUQ5(JL,JK)*ZMFA5
          PTU5(JL,JK)=ZTENH5(JL,JK)+ZMFUS5(JL,JK)*ZMFA5*ZORCPD5
          ZMFUS5(JL,JK)=PMFU5(JL,JK)*(RCPD*PTU5(JL,JK)+PGEOH5(JL,JK))
          ZMFUQ5(JL,JK)=PMFU5(JL,JK)*PQU5(JL,JK)
          IF(LLDDRAF(JL)) THEN
            ZMFA5=1.0_JPRB/MIN(-1.E-15_JPRB,PMFD5(JL,JK))
            ZQD5(JL,JK)=ZQENH5(JL,JK)+ZMFDQ5(JL,JK)*ZMFA5
            ZTD5(JL,JK)=ZTENH5(JL,JK)+ZMFDS5(JL,JK)*ZMFA5*ZORCPD5
            ZMFDQ5(JL,JK)=PMFD5(JL,JK)*ZQD5(JL,JK)
            ZMFDS5(JL,JK)=PMFD5(JL,JK)*(RCPD*ZTD5(JL,JK)+PGEOH5(JL,JK))
          ENDIF
        ELSEIF(JK<=KCBOT(JL).AND.JK>=KCTOP(JL)) THEN
          ZMFUS5(JL,JK)=PMFU5(JL,JK)*(RCPD*PTU5(JL,JK)+PGEOH5(JL,JK))
          ZMFUQ5(JL,JK)=PMFU5(JL,JK)*PQU5(JL,JK)
          ZMFDS5(JL,JK)=PMFD5(JL,JK)*(RCPD*ZTD5(JL,JK)+PGEOH5(JL,JK))
          ZMFDQ5(JL,JK)=PMFD5(JL,JK)*ZQD5(JL,JK)
        ENDIF
      ENDIF
    ENDDO
  ENDDO

ENDIF

CALL CUDTDQNTL &
 & ( KIDIA,    KFDIA,    KLON,    KLEV,&
 & ITOPM2,   KTYPE,    KCBOT,    KCTOP,    IDTOP,&
 & LDCUM,    LLDDRAF,  PTSPHY,&
 & PAPH5,    PGEOH5,   PGEO5,&
 & PTEN5,    ZTENH5,   PQEN5,    ZQENH5,   PQSEN5,&
 & ZLGLAC5,  PLUDE5,&
 & ZMFUS5,   ZMFDS5,   ZMFUQ5,   ZMFDQ5,&
 & ZMFUL5,   ZDMFUP5,  ZDPMEL5,&
 & PTENT5,   PTENQ5,   PENTH5,&
 & PMFU5,    PMFD5,&
 & ZDMFEN5,  ZDMFDE5,&
 & PGEOH,    PGEO,&
 & PTEN,     ZTENH,    PQEN,     ZQENH,    PQSEN,&
 & PTENT,    PTENQ   )  

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

!*    9.0          COMPUTE MOMENTUM IN UPDRAUGHT AND DOWNDRAUGHT
!                  ---------------------------------------------

IF(LMFDUDV) THEN

  DO JK=KLEV-1,2,-1
    IK=JK+1
    DO JL=KIDIA,KFDIA
      IF(LDCUM(JL)) THEN
        IF(JK==KCBOT(JL).AND.KTYPE(JL)<3) THEN
           IKB=IDPL(JL)
           ZUU5(JL,JK)=PUEN5(JL,IKB-1)
           ZVU5(JL,JK)=PVEN5(JL,IKB-1)
        ELSEIF(JK==KCBOT(JL).AND.KTYPE(JL)==3) THEN
           ZUU5(JL,JK)=PUEN5(JL,JK-1)
           ZVU5(JL,JK)=PVEN5(JL,JK-1)
        ENDIF
        IF( JK<KCBOT(JL).AND.JK>=KCTOP(JL)) THEN
          ZFAC5=0.0_JPRB
          IF(KTYPE(JL)==1.OR.KTYPE(JL)==3) ZFAC5=2.0_JPRB
          if(ktype(jl)==1.and.jk<=kctop(jl)+2) zfac5=3.0_jprb
          ZERATE5=PMFU5(JL,JK)-PMFU5(JL,IK)+(1.0_JPRB+ZFAC5)*ZMFUDE_RATE5(JL,JK)
          ZDERATE5=(1.0_JPRB+ZFAC5)*ZMFUDE_RATE5(JL,JK)
          ZMFA5=1.0_JPRB/MAX(RMFCMIN,PMFU5(JL,JK))
          ZUU5(JL,JK)=(ZUU5(JL,IK)*PMFU5(JL,IK)+ZERATE5*PUEN5(JL,JK)-&
                     &ZDERATE5*ZUU5(JL,IK))*ZMFA5
          ZVU5(JL,JK)=(ZVU5(JL,IK)*PMFU5(JL,IK)+ZERATE5*PVEN5(JL,JK)-&
                     &ZDERATE5*ZVU5(JL,IK))*ZMFA5
        ENDIF
      ENDIF
    ENDDO
  ENDDO
  DO JK=3,KLEV
    IK=JK-1
    DO JL=KIDIA,KFDIA
      IF( LDCUM(JL)) THEN
        IF(JK==IDTOP(JL)) THEN
          ZUD5(JL,JK)=0.5_JPRB*(ZUU5(JL,JK)+PUEN5(JL,IK))
          ZVD5(JL,JK)=0.5_JPRB*(ZVU5(JL,JK)+PVEN5(JL,IK))
        ELSEIF(JK>IDTOP(JL)) THEN
          ZERATE5=-PMFD5(JL,JK)+PMFD5(JL,IK)+ZMFDDE_RATE5(JL,JK)
          ZMFA5=1.0_JPRB/MIN(-RMFCMIN,PMFD5(JL,JK))
          ZUD5(JL,JK)=(ZUD5(JL,IK)*PMFD5(JL,IK)-ZERATE5*PUEN5(JL,IK)+&
                     &ZMFDDE_RATE5(JL,JK)*ZUD5(JL,IK))*ZMFA5
          ZVD5(JL,JK)=(ZVD5(JL,IK)*PMFD5(JL,IK)-ZERATE5*PVEN5(JL,IK)+&
                     &ZMFDDE_RATE5(JL,JK)*ZVD5(JL,IK))*ZMFA5
        ENDIF
      ENDIF
    ENDDO
  ENDDO


!*    9.1          UPDATE TENDENCIES FOR U AND V IN SUBROUTINE CUDUDVTL
!                  ----------------------------------------------------

! EPS: Begin
! rescale massfluxes for explicit/semi-implicit for stability in Momentum
!------------------------------------------------------------------------

  ZMFS5(:)=1.0_JPRB
! IF(RMFSOLUV<=0.5_JPRB) THEN
  IF(RMFSOLUV<=1.0_JPRB) THEN
    DO JK=2,KLEV
      DO JL=KIDIA,KFDIA
        IF(LDCUM(JL).AND.JK>=KCTOP(JL)-1) THEN
          ZMFMAX5=(PAPH5(JL,JK)-PAPH5(JL,JK-1))*ZCONS
          IF(PMFU5(JL,JK)>ZMFMAX5.AND.JK>=KCTOP(JL)) &
           & ZMFS5(JL)=MIN(ZMFS5(JL),ZMFMAX5/PMFU5(JL,JK))  
        ENDIF
      ENDDO
    ENDDO
  ENDIF
  DO JK=1,KLEV
    DO JL=KIDIA,KFDIA
      ZMFUUS5(JL,JK)=PMFU5(JL,JK)
      ZMFDUS5(JL,JK)=PMFD5(JL,JK)
      IF(LDCUM(JL).AND.JK>=KCTOP(JL)-1) THEN
        ZMFUUS5(JL,JK)=PMFU5(JL,JK)*ZMFS5(JL)
        ZMFDUS5(JL,JK)=PMFD5(JL,JK)*ZMFS5(JL)
      ENDIF
    ENDDO
  ENDDO

! recompute Draught properties below for Implicit
! based on linear flux profiles

  IF(RMFSOLUV>0.0_JPRB) THEN

    DO JL=KIDIA,KFDIA
      IF(LDCUM(JL)) THEN
        JK=KCBOT(JL)
        IK=JK-1
        ZMFUUB5(JL)=ZMFUUS5(JL,JK)*(ZUU5(JL,JK)-PUEN5(JL,IK))
        ZMFUVB5(JL)=ZMFUUS5(JL,JK)*(ZVU5(JL,JK)-PVEN5(JL,IK))
      ENDIF
    ENDDO

    DO JK=2,KLEV
      IK=JK-1
      DO JL=KIDIA,KFDIA
        IF ( LDCUM(JL).AND.JK>KCBOT(JL) ) THEN
          IKB=KCBOT(JL)
          ZDZ5=((PAPH5(JL,KLEV+1)-PAPH5(JL,JK))/(PAPH5(JL,KLEV+1)-PAPH5(JL,IKB)))
          IF(KTYPE(JL) == 3) THEN
            ZDZ5=ZDZ5*ZDZ5
          ENDIF
          ZMFA5=1.0_JPRB/MAX(RMFCMIN,ZMFUUS5(JL,JK))
          ZUU5(JL,JK)=PUEN5(JL,IK)+ZMFUUB5(JL)*ZDZ5*ZMFA5
          ZVU5(JL,JK)=PVEN5(JL,IK)+ZMFUVB5(JL)*ZDZ5*ZMFA5
          ZMFDUS5(JL,JK)=ZMFDUS5(JL,IKB)*ZDZ5
          ZUD5(JL,JK)=PUEN5(JL,IK)+ZUD5(JL,IKB)-PUEN5(JL,IKB-1)
          ZVD5(JL,JK)=PVEN5(JL,IK)+ZVD5(JL,IKB)-PVEN5(JL,IKB-1)
        ENDIF
    ! add UV perturb to correct wind bias
        IF ( LDCUM(JL).AND.JK>=KCTOP(JL) ) THEN
          ZUU5(JL,JK)=ZUU5(JL,JK)-RUVPER*SIGN(1.0_JPRB,ZUU5(JL,JK))
          ZVU5(JL,JK)=ZVU5(JL,JK)-RUVPER*SIGN(1.0_JPRB,ZVU5(JL,JK))
        ENDIF
      ENDDO
    ENDDO

  ENDIF
!-------------------------------------------------------------------
! End
! Intermediate Solution for stability in EPS:
! For original code replace line
!  &, PUEN,     PVEN,     ZMFUUS,   ZMFDUS &
!by
!  &, PUEN,     PVEN,     PMFU,     PMFD

  CALL CUDUDVTL ( &
   & KIDIA,    KFDIA,    KLON,    KLEV,&
   & ITOPM2,   KTYPE,    KCBOT,    KCTOP,    LDCUM,    PTSPHY,&
   & PAPH5,    PUEN5,    PVEN5,    ZMFUUS5,  ZMFDUS5,&
   & ZUU5,     ZUD5,     ZVU5,     ZVD5,&
   & PTENU5,   PTENV5,&
   & ZDMFEN5,  ZDMFDE5,&
   & PUEN,     PVEN,&
   & PTENU,    PTENV     )  

  IF(LMFUVDIS) THEN
! add KE dissipation
    DO JL=KIDIA,KFDIA
      ZSUM125(JL)=0.0_JPRB
      ZSUM225(JL)=0.0_JPRB
    ENDDO
    DO JK=1,KLEV
      DO JL=KIDIA,KFDIA
        ZUV25(JL,JK)=0.0_JPRB
        IF (LDCUM(JL).AND.JK>=KCTOP(JL)-1) THEN
          ZDZ5=(PAPH5(JL,JK+1)-PAPH5(JL,JK))
          ZDUTEN5=PTENU5(JL,JK)-ZTENU5(JL,JK)
          ZDVTEN5=PTENV5(JL,JK)-ZTENV5(JL,JK)
          ZUV25(JL,JK)=SQRT(ZDUTEN5**2+ZDVTEN5**2)*ZDZ5
          ZSUM225(JL)=ZSUM225(JL)+ZUV25(JL,JK)
          ZSUM125(JL)=ZSUM125(JL)-(PUEN5(JL,JK)*ZDUTEN5+PVEN5(JL,JK)*ZDVTEN5)*ZDZ5
        ENDIF
      ENDDO
    ENDDO
    DO JK=1,KLEV
      DO JL=KIDIA,KFDIA
        IF (LDCUM(JL).AND.JK>=KCTOP(JL)-1) THEN
          ZDZ5=(PAPH5(JL,JK+1)-PAPH5(JL,JK))
          ZTDIS5=ZORCPD5*ZSUM125(JL)/ZDZ5*&
         &             ZUV25(JL,JK)/MAX(1.E-15_JPRB,ZSUM225(JL))
          PTENT5(JL,JK)=PTENT5(JL,JK)+ZTDIS5
        ENDIF
      ENDDO
    ENDDO
  ENDIF

ENDIF

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

!*   10.           IN CASE THAT EITHER DEEP OR SHALLOW IS SWITCHED OFF
!                  NEED TO SET SOME VARIABLES A POSTERIORI TO ZERO
!                  ---------------------------------------------------

IF (.NOT.LMFSCV .OR. .NOT.LMFPEN) THEN
  DO JK=2,KLEV
    DO JL=KIDIA,KFDIA
      IF(LLO2(JL).AND.JK>=KCTOP(JL)-1) THEN
        PTU5(JL,JK)  =PTEN5(JL,JK)
        PQU5(JL,JK)  =PQEN5(JL,JK)
        PLU5(JL,JK)  =0.0_JPRB
        PENTH5(JL,JK) =0.0_JPRB
        ZMFUDE_RATE5(JL,JK) =0.0_JPRB
        ZMFDDE_RATE5(JL,JK) =0.0_JPRB
      ENDIF
    ENDDO
  ENDDO
!  DO JL=KIDIA,KFDIA
!     IF(LLO2(JL)) THEN
!       KCTOP(JL)=KLEV-1
!       KCBOT(JL)=KLEV-1
!     ENDIF
!  ENDDO
ENDIF

! further clean up
DO JK=2,KLEV
  DO JL=KIDIA,KFDIA
    IF(LDCUM(JL).AND.JK<KCTOP(JL)) THEN
      PTU5(JL,JK)=PTEN5(JL,JK)
      PQU5(JL,JK)=PQEN5(JL,JK)
      PLU5(JL,JK)=0.0_JPRB
    ENDIF
  ENDDO
ENDDO


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

!*   11.0          CHEMICAL TRACER TRANSPORT
!                  -------------------------

IF ( LMFTRAC .AND. KTRAC>0 ) THEN

  ALLOCATE(ZMFUUC5(KLON,KLEV))
  ALLOCATE(ZMFDUC5(KLON,KLEV))
  ALLOCATE(ZMFUDR5(KLON,KLEV))
  ALLOCATE(ZMFDDR5(KLON,KLEV))

  ALLOCATE(ZMFUUC(KLON,KLEV))
  ALLOCATE(ZMFDUC(KLON,KLEV))
  ALLOCATE(ZMFUDR(KLON,KLEV))
  ALLOCATE(ZMFDDR(KLON,KLEV))
  ALLOCATE(ZDMFUPC(KLON,KLEV))
  ALLOCATE(ZDMFDPC(KLON,KLEV))

! transport switched off for mid-level convection
  DO JL=KIDIA,KFDIA
    !IF( LDCUM(JL).AND.KTYPE(JL)/=3 ) THEN
     IF( LDCUM(JL).AND.KTYPE(JL)/=3.and.kcbot(jl)-kctop(jl)>=1 ) THEN
         LLDCUM(JL)=.TRUE.
         LLDDRAF3(JL)=LLDDRAF(JL)
     ELSE
         LLDCUM(JL)=.FALSE.
         LLDDRAF3(JL)=.FALSE.
     ENDIF
  ENDDO
  
! check and correct mass fluxes for CFL criterium

  ZMFS5(:)=1.0_JPRB
  IF(RMFSOLCT<=3.0_JPRB) THEN
    DO JK=2,KLEV
      DO JL=KIDIA,KFDIA
        IF(LLDCUM(JL).AND.JK>=KCTOP(JL)) THEN
          ZMFMAX5=(PAPH5(JL,JK)-PAPH5(JL,JK-1))*0.8*ZCONS
          IF(PMFU5(JL,JK)>ZMFMAX5) &
           & ZMFS5(JL)=MIN(ZMFS5(JL),ZMFMAX5/PMFU5(JL,JK))  
        ENDIF
      ENDDO
    ENDDO
  ENDIF
  DO JK=1,KLEV
    DO JL=KIDIA,KFDIA
      IF(LLDCUM(JL).AND.JK>=KCTOP(JL)-1) THEN
        ZMFUUC5(JL,JK)=PMFU5(JL,JK)*ZMFS5(JL)
        ZMFUDR5(JL,JK)=ZMFUDE_RATE5(JL,JK)*ZMFS5(JL)
        ZDMFUPC5(JL,JK)=(PMFLXR5(JL,JK)+PMFLXS5(JL,JK))*ZMFS5(JL)
        ikb=kcbot(jl)
        if(jk>ikb) zdmfupc5(jl,jk)=zdmfupc5(jl,ikb)
      ELSE
        ZMFUUC5(JL,JK)=0._JPRB
        ZMFUDR5(JL,JK)=0._JPRB
      ENDIF
      IF ( LLDDRAF3(JL) .AND. JK>=IDTOP(JL)-1) THEN
        ZMFDUC5(JL,JK)=PMFD5(JL,JK)*ZMFS5(JL)
        ZMFDDR5(JL,JK)=ZMFDDE_RATE5(JL,JK)*ZMFS5(JL)
       !ZDMFDPC5(JL,JK)=ZDMFDPC5(JL,JK)*ZMFS5(JL)
        ZDMFDPC5(JL,JK)=0.0_JPRB
      ELSE
        ZMFDUC5(JL,JK)=0._JPRB
        ZMFDDR5(JL,JK)=0._JPRB
      ENDIF
      ZMFUUC(JL,JK)=0._JPRB
      ZMFDUC(JL,JK)=0._JPRB
      ZMFUDR(JL,JK)=0._JPRB
      ZMFDDR(JL,JK)=0._JPRB
      ZDMFUPC(JL,JK)=0._JPRB
      ZDMFDPC(JL,JK)=0._JPRB
    ENDDO
  ENDDO

  IF( LMFSMOOTH ) THEN
! smmoothing of mass fluxes (gradients) at top and bottom of draughts
    DO JK=2,KLEV-1
      DO JL=KIDIA,KFDIA
        IF(LLDDRAF3(JL).AND.ZMFDUC5(JL,JK)<0.0_JPRB .AND. ZMFDUC5(JL,JK+1)==0.0_JPRB) THEN
          ZERATE5=MIN(0._JPRB,ZMFDUC5(JL,JK)-0.5*ZMFDUC5(JL,JK-1))
          ZMFDUC5(JL,JK)=ZMFDUC5(JL,JK)-ZERATE5
          ZMFDDR5(JL,JK)=ZMFDDR5(JL,JK)-ZERATE5
          ZMFDDR5(JL,JK+1)=-ZMFDUC5(JL,JK)
        ENDIF
        IF(LLDCUM(JL).AND.JK==KCTOP(JL)) THEN
          ZERATE5=MAX(0.0_JPRB,ZMFUUC5(JL,JK)-0.5_JPRB*ZMFUUC5(JL,JK+1))
          ZMFUUC5(JL,JK)=ZMFUUC5(JL,JK)-ZERATE5
          ZMFUDR5(JL,JK)=ZMFUDR5(JL,JK)+ZERATE5
          ZMFUDR5(JL,JK-1)=ZMFUUC5(JL,JK)
        ENDIF
      ENDDO
    ENDDO
    DO JK=KLEV-1,2,-1
      DO JL=KIDIA,KFDIA
        IF(LLDCUM(JL)) THEN
          IF(ZMFUDR5(JL,JK)==0.0_JPRB.AND.ZMFUDR5(JL,JK-1)>0.0_JPRB) THEN
            ZMFUDR5(JL,JK)=0.5_JPRB*ZMFUDR5(JL,JK-1)
          ENDIF
        ENDIF
      ENDDO
    ENDDO
  ENDIF

  IF(LMFSCAV) THEN
    ZSCAV5(:)=PSCAV5(:)
  ELSE
    ZSCAV5(:)=0.0_JPRB
    ZDMFUPC5(:,:)=0.0_JPRB
    ZDMFUPC (:,:)=0.0_JPRB
  ENDIF

  CALL CUCTRACERTL &
   & ( KIDIA,    KFDIA,    KLON,     KLEV,     KTRAC,&
   & KCTOP,     IDTOP,&
   & LLDCUM,   LLDDRAF3,  PTSPHY,&
   & PAPH5,    ZMFUUC5,   ZMFDUC5,  ZMFUDR5,  ZMFDDR5,&
   & ZDMFUPC5, ZDMFDPC5,&
   & PCEN5,    PTENC5,    ZSCAV5,&
   & PAPH,     ZMFUUC,    ZMFDUC,   ZMFUDR,   ZMFDDR,&
   & ZDMFUPC,  ZDMFDPC,&
   & PCEN,     PTENC )

  DEALLOCATE(ZDMFDPC)
  DEALLOCATE(ZDMFUPC)
  DEALLOCATE(ZMFDDR)
  DEALLOCATE(ZMFUDR)
  DEALLOCATE(ZMFDUC)
  DEALLOCATE(ZMFUUC)

  DEALLOCATE(ZMFDDR5)
  DEALLOCATE(ZMFUDR5)
  DEALLOCATE(ZMFDUC5)
  DEALLOCATE(ZMFUUC5)

ENDIF

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

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