SUBROUTINE CUBASEN2AD &
 & ( KIDIA,    KFDIA,    KLON,    KLEV,&
 & LDRAIN1D,   LDCVOPT,&
 & PTENH5,   PQENH5,   PGEOH5,   PAP5,     PAPH5,&
 & PQHFL5,   PAHFS5,&
 & PTEN5,    PQEN5,    PGEO5,&
 & PTU5,     PQU5,     PLU5,&
 & PBUOH5,   PWUBASE5, PLGLAC5,  PQPRCV5,&
 & KTYPE,    KLAB,     LDCUM,    LDSC,&
 & KCBOT,    KBOTSC,   KCTOP,    KSTUP,&
 & PTU55,    PQU55,    PLU55,&
 & PBUOH55,  PWU2H55,  PSUH55,   PWU2H155,&
 & PTVENH55, PTVUH55,  PSUH255,  PQU255,&
 & PFACT355, PFACT455, PFACT555, PWS55,&   
 & PKHVFL55, PUST55,   PWUBASE55, PWORK155, PWORK155T,&
 & KCBOT55,  KLAB55,   LDCUM55,  LDTEST155,&
 & LDGO_ON55, LDMIDTEST55,&
 & PTENH,    PQENH,    PGEOH,    PAP,      PAPH,&
 & PQHFL,    PAHFS,&
 & PTEN,     PQEN,     PGEO,&
 & PTU,      PQU,      PLU,&
 & PBUOH,    PWUBASE,  PLGLAC,   PQPRCV )  

!          THIS ROUTINE CALCULATES CLOUD BASE FIELDS
!          CLOUD BASE HEIGHT AND CLOUD TOP HEIGHT
!          (ADJOINT VERSION)

!          Ph LOPEZ (ECMWF) (07/2002) 
!          Inspired from A. Pier Siebesma  (KNMI)
!          and C. Jakob (ECMWF) (01/2001)

!          PURPOSE.
!          --------
!          TO PRODUCE CLOUD BASE AND CLOUD TOP VALUES FOR 
!          SIMPLIFIED CONVECTIVE PARAMETRIZATION

!          INTERFACE
!          ---------
!          THIS ROUTINE IS CALLED FROM *CUMASTRN2AD*.
!          INPUT ARE ENVIRONM. VALUES OF T,Q,P,PHI AT HALF LEVELS.
!          IT RETURNS CLOUD FIELDS VALUES AND FLAGS AS FOLLOWS;
!                 KLAB=0 FOR STABLE LAYERS
!                 KLAB=1 FOR SUBCLOUD LEVELS
!                 KLAB=2 FOR CLOUD LEVELS LEVEL

!          METHOD.
!          --------
!          LIFT SURFACE AIR DRY-ADIABATICALLY TO CLOUD TOP.

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

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

!    INPUT PARAMETERS (REAL):

!    *PTENH5*       ENV. TEMPERATURE (T+1) ON HALF LEVELS         (Trajectory)
!    *PQENH5*       ENV. SPEC. HUMIDITY (T+1) ON HALF LEVELS      (Trajectory)                
!    *PQHFL5*       MOISTURE FLUX (EXCEPT FROM SNOW EVAP.)        (Trajectory)
!    *PAHFS5*       SENSIBLE HEAT FLUX                            (Trajectory)
!    *PSSTRU5*      KINEMATIC surface U-MOMENTUM FLUX             (Trajectory)
!    *PSSTRV5*      KINEMATIC surface V-MOMENTUM FLUX             (Trajectory)
!    *PGEOH5*       GEOPOTENTIAL ON HALF LEVELS                   (Trajectory)
!    *PAPH5*        PROVISIONAL PRESSURE ON HALF LEVELS           (Trajectory)
!    *PTEN5*        PROVISIONAL ENVIRONMENT TEMPERATURE (T+1)     (Trajectory)
!    *PQEN5*        PROVISIONAL ENVIRONMENT SPEC. HUMIDITY (T+1)  (Trajectory)
!    *PGEO5*        GEOPOTENTIAL                                  (Trajectory)
!    *PQHFL5*       MOISTURE FLUX (EXCEPT FROM SNOW EVAP.)        (Trajectory)
!    *PAHFS5*       SENSIBLE HEAT FLUX                            (Trajectory)

!    *PTENH*        ENV. TEMPERATURE (T+1) ON HALF LEVELS           K
!    *PQENH*        ENV. SPEC. HUMIDITY (T+1) ON HALF LEVELS      KG/KG
!    *PQHFL*        MOISTURE FLUX (EXCEPT FROM SNOW EVAP.)        KG/(SM2)
!    *PAHFS*        SENSIBLE HEAT FLUX                            W/M2
!    *PSSTRU*       KINEMATIC surface U-MOMENTUM FLUX             (M/S)^2
!    *PSSTRV*       KINEMATIC surface V-MOMENTUM FLUX             (M/S)^2
!    *PGEOH*        GEOPOTENTIAL ON HALF LEVELS                   M2/S2
!    *PAPH*         PROVISIONAL PRESSURE ON HALF LEVELS             PA
!    *PTEN*         PROVISIONAL ENVIRONMENT TEMPERATURE (T+1)       K
!    *PQEN*         PROVISIONAL ENVIRONMENT SPEC. HUMIDITY (T+1)  KG/KG
!    *PGEO*         GEOPOTENTIAL                                  M2/S2
!    *PQHFL*        MOISTURE FLUX (EXCEPT FROM SNOW EVAP.)        KG/(SM2)
!    *PAHFS*        SENSIBLE HEAT FLUX                            W/M2

!    UPDATED PARAMETERS (REAL):

!    *PTU5*         TEMPERATURE IN UPDRAFTS                       (Trajectory)
!    *PQU5*         SPEC. HUMIDITY IN UPDRAFTS                    (Trajectory)
!    *PLU5*         LIQUID WATER CONTENT IN UPDRAFTS              (Trajectory)

!    *PTU*          TEMPERATURE IN UPDRAFTS                         K
!    *PQU*          SPEC. HUMIDITY IN UPDRAFTS                    KG/KG
!    *PLU*          LIQUID WATER CONTENT IN UPDRAFTS              KG/KG

!    UPDATED PARAMETERS (INTEGER):

!    *KLAB*         FLAG KLAB=1 FOR SUBCLOUD LEVELS
!                        KLAB=2 FOR CLOUD LEVELS

!    OUTPUT PARAMETERS (LOGICAL):

!    *LDCUM*        FLAG: .TRUE. FOR CONVECTIVE POINTS 
!    *LDSC*         FLAG: .TRUE. IF BL-CLOUDS EXIST

!    OUTPUT PARAMETERS (INTEGER):

!    *KTYPE*       TYPE OF CONVECTION    
!    *KSTUP*       LEVEL AT WHICH THE UPDRAFT STARTS
!    *KCBOT*       CLOUD BASE LEVEL !    
!    *KCTOP*       CLOUD TOP LEVEL = HEIGHEST HALF LEVEL 
!                     WITH A NON-ZERO CLOUD UPDRAFT.
!    *KBOTSC*      CLOUD BASE LEVEL OF BL-CLOUDS

!    OUTPUT PARAMETERS (REAL):

!    *PBUOH5*      BUOYANCY ON HALF LEVELS                      (Trajectory)
!    *PLGLAC5*     FROZEN CLOUD WATER CONTENT                   (Trajectory)
!    *PQPRCV5*     CONVECTIVE PRECIPITATION CONTENT             (Trajectory)

!    *PBUOH*       BUOYANCY ON HALF LEVELS                       M/S2
!    *PLGLAC*      FROZEN CLOUD WATER CONTENT                   KG/KG 
!    *PQPRCV*      CONVECTIVE PRECIPITATION CONTENT             KG/KG 

!          EXTERNALS
!          ---------
!          *CUPDRA*    FOR COMPUTING CHARACTERISTICS OF UPDRAFT
!          *CUPDRAAD*  FOR COMPUTING CHARACTERISTICS OF UPDRAFT

!          MODIFICATIONS
!          -------------
!          P.Lopez      01-Oct-2005 New version of linearized convection
!          P.Lopez      11-01-2007  Added LDRAIN switch       
!          P.Lopez      11-Apr-2007 Optimization (LDCVOPT)
!          P.Lopez      19-Oct-2007 Revised version to improve match to Tiedtke scheme
!          A.Geer       01-Oct-2008 LDRAIN1D name change to reflect usage

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

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

USE YOMCST   , ONLY : RCPD, RETV, RD, RG
USE YOEVDF   , ONLY : RKAP
USE YOECUMF2 , ONLY : RDEPTHS2, NJKT12
USE YOMCUMFS , ONLY : LREGCV

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
LOGICAL            ,INTENT(IN)    :: LDRAIN1D 
LOGICAL            ,INTENT(IN)    :: LDCVOPT
REAL(KIND=JPRB)    ,INTENT(IN)    :: PTENH5(KLON,KLEV) 
REAL(KIND=JPRB)    ,INTENT(IN)    :: PQENH5(KLON,KLEV) 
REAL(KIND=JPRB)    ,INTENT(IN)    :: PGEOH5(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)    :: PQHFL5(KLON,KLEV+1) 
REAL(KIND=JPRB)    ,INTENT(IN)    :: PAHFS5(KLON,KLEV+1) 
REAL(KIND=JPRB)    ,INTENT(IN)    :: PTEN5(KLON,KLEV) 
REAL(KIND=JPRB)    ,INTENT(IN)    :: PQEN5(KLON,KLEV) 
REAL(KIND=JPRB)    ,INTENT(IN)    :: PGEO5(KLON,KLEV) 
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(OUT)   :: PBUOH5(KLON,KLEV) 
REAL(KIND=JPRB)    ,INTENT(OUT)   :: PWUBASE5(KLON) 
REAL(KIND=JPRB)    ,INTENT(OUT)   :: PLGLAC5(KLON,KLEV) 
REAL(KIND=JPRB)    ,INTENT(OUT)   :: PQPRCV5(KLON,KLEV) 
INTEGER(KIND=JPIM) ,INTENT(OUT)   :: KTYPE(KLON) 
INTEGER(KIND=JPIM) ,INTENT(OUT)   :: KLAB(KLON,KLEV) 
LOGICAL            ,INTENT(OUT)   :: LDCUM(KLON) 
LOGICAL            ,INTENT(OUT)   :: LDSC(KLON) 
INTEGER(KIND=JPIM) ,INTENT(OUT)   :: KCBOT(KLON) 
INTEGER(KIND=JPIM) ,INTENT(OUT)   :: KBOTSC(KLON) 
INTEGER(KIND=JPIM) ,INTENT(OUT)   :: KCTOP(KLON) 
INTEGER(KIND=JPIM) ,INTENT(OUT)   :: KSTUP(KLON) 
REAL(KIND=JPRB)    ,INTENT(INOUT) :: PTENH(KLON,KLEV) 
REAL(KIND=JPRB)    ,INTENT(INOUT) :: PQENH(KLON,KLEV) 
REAL(KIND=JPRB)    ,INTENT(INOUT) :: PGEOH(KLON,KLEV+1)
REAL(KIND=JPRB)    ,INTENT(INOUT) :: PAP(KLON,KLEV) 
REAL(KIND=JPRB)    ,INTENT(INOUT) :: PAPH(KLON,KLEV+1) 
REAL(KIND=JPRB)    ,INTENT(INOUT) :: PQHFL(KLON,KLEV+1) 
REAL(KIND=JPRB)    ,INTENT(INOUT) :: PAHFS(KLON,KLEV+1) 
REAL(KIND=JPRB)    ,INTENT(INOUT) :: PTEN(KLON,KLEV) 
REAL(KIND=JPRB)    ,INTENT(INOUT) :: PQEN(KLON,KLEV) 
REAL(KIND=JPRB)    ,INTENT(INOUT) :: PGEO(KLON,KLEV) 
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) :: PBUOH(KLON,KLEV) 
REAL(KIND=JPRB)    ,INTENT(INOUT) :: PWUBASE(KLON) 
REAL(KIND=JPRB)    ,INTENT(INOUT) :: PLGLAC(KLON,KLEV) 
REAL(KIND=JPRB)    ,INTENT(INOUT) :: PQPRCV(KLON,KLEV) 
! arrays for optimization (trajectory for adjoint saved in CUBASEN2)
REAL(KIND=JPRB)    ,INTENT(IN)    :: PTU55   (KLON,KLEV)   
REAL(KIND=JPRB)    ,INTENT(IN)    :: PQU55   (KLON,KLEV)    
REAL(KIND=JPRB)    ,INTENT(IN)    :: PLU55   (KLON,KLEV)    
REAL(KIND=JPRB)    ,INTENT(IN)    :: PBUOH55 (KLON,KLEV)
REAL(KIND=JPRB)    ,INTENT(IN)    :: PWU2H55 (KLON,KLEV)
REAL(KIND=JPRB)    ,INTENT(IN)    :: PSUH55  (KLON,KLEV)
REAL(KIND=JPRB)    ,INTENT(IN)    :: PWU2H155(KLON)
REAL(KIND=JPRB)    ,INTENT(IN)    :: PTVENH55(KLON,KLEV)    
REAL(KIND=JPRB)    ,INTENT(IN)    :: PTVUH55 (KLON,KLEV)    
REAL(KIND=JPRB)    ,INTENT(IN)    :: PSUH255 (KLON,KLEV)    
REAL(KIND=JPRB)    ,INTENT(IN)    :: PQU255  (KLON,KLEV)    
REAL(KIND=JPRB)    ,INTENT(IN)    :: PFACT355(KLON)    
REAL(KIND=JPRB)    ,INTENT(IN)    :: PFACT455(KLON)    
REAL(KIND=JPRB)    ,INTENT(IN)    :: PFACT555(KLON)    
REAL(KIND=JPRB)    ,INTENT(IN)    :: PWS55   (KLON)    
REAL(KIND=JPRB)    ,INTENT(IN)    :: PKHVFL55(KLON)    
REAL(KIND=JPRB)    ,INTENT(IN)    :: PUST55  (KLON)    
REAL(KIND=JPRB)    ,INTENT(IN)    :: PWUBASE55(KLON)    
REAL(KIND=JPRB)    ,INTENT(IN)    :: PWORK155(KLON,KLEV)    
REAL(KIND=JPRB)    ,INTENT(IN)    :: PWORK155T(KLON,3,KLEV)    
INTEGER(KIND=JPIM) ,INTENT(IN)    :: KCBOT55 (KLON)
INTEGER(KIND=JPIM) ,INTENT(IN)    :: KLAB55  (KLON,KLEV)
LOGICAL            ,INTENT(IN)    :: LDCUM55 (KLON)
LOGICAL            ,INTENT(IN)    :: LDTEST155(KLON,KLEV)
LOGICAL            ,INTENT(IN)    :: LDGO_ON55(KLON,KLEV)
LOGICAL            ,INTENT(IN)    :: LDMIDTEST55(KLON,KLEV)

!             LOCAL STORAGE
!             ----- -------

LOGICAL ::   LLGO_ON(KLON),  LLMID(KLON)
LOGICAL ::   LLGO_ON2(KLON,KLEV), LL_LDO_ASC(KLEV)
LOGICAL ::   LL_LMIDTEST(KLON,KLEV)
LOGICAL ::   LLTEST1(KLON,KLEV)

INTEGER(KIND=JPIM) :: I_KLAB2(KLON,KLEV)
INTEGER(KIND=JPIM) :: IKSTART, JK, JL, IKB, JKK, IKP1, JKT1

REAL(KIND=JPRB)    :: ZSENH5(KLON,KLEV+1),   ZSUH5(KLON,KLEV),&
                    & ZWU2H5(KLON,KLEV)  
REAL(KIND=JPRB)    :: ZSENH(KLON,KLEV+1),    ZSUH(KLON,KLEV),&
                    & ZWU2H(KLON,KLEV)  

REAL(KIND=JPRB)    :: ZTVENH15(KLON,KLEV),   ZTVUH15(KLON,KLEV)

REAL(KIND=JPRB)    :: ZDEPTH5, ZUST5(KLON),&
                &     ZKHVFL5(KLON), ZWS5(KLON),   ZFACT5(KLON),&
                &     ZFACT3(KLON),  ZFACT4(KLON) 
 
REAL(KIND=JPRB)     :: ZTU5(KLON,KLEV) 
REAL(KIND=JPRB)     :: ZQU5(KLON,KLEV) 
REAL(KIND=JPRB)     :: ZLU5(KLON,KLEV) 
REAL(KIND=JPRB)     :: ZBUOH5(KLON,KLEV) 
REAL(KIND=JPRB)     :: ZLGLAC5(KLON,KLEV) 
REAL(KIND=JPRB)     :: ZQPRCV5(KLON,KLEV)
REAL(KIND=JPRB)     :: ZZWU2H5(KLON,KLEV)
REAL(KIND=JPRB)     :: ZZSUH5(KLON,KLEV)  
REAL(KIND=JPRB)     :: ZTU05(KLON,KLEV)
REAL(KIND=JPRB)     :: ZQU05(KLON,KLEV)

REAL(KIND=JPRB)     :: ZTU (KLON,KLEV) 
REAL(KIND=JPRB)     :: ZQU (KLON,KLEV) 
REAL(KIND=JPRB)     :: ZLU (KLON,KLEV) 
REAL(KIND=JPRB)     :: ZBUOH (KLON,KLEV) 
REAL(KIND=JPRB)     :: ZLGLAC (KLON,KLEV) 
REAL(KIND=JPRB)     :: ZQPRCV (KLON,KLEV)
REAL(KIND=JPRB)     :: ZZWU2H (KLON,KLEV)
REAL(KIND=JPRB)     :: ZZSUH (KLON,KLEV)  
REAL(KIND=JPRB)     :: ZTU0 (KLON,KLEV)
REAL(KIND=JPRB)     :: ZQU0 (KLON,KLEV)

INTEGER(KIND=JPIM)  :: ILAB(KLON,KLEV) 
INTEGER(KIND=JPIM)  :: ICBOT(KLON) 
INTEGER(KIND=JPIM)  :: IBOTSC(KLON) 
INTEGER(KIND=JPIM)  :: ICTOP(KLON) 
INTEGER(KIND=JPIM)  :: ISTUP(KLON)
LOGICAL             :: LLCUM(KLON) 
LOGICAL             :: LLSC(KLON) 
LOGICAL             :: LLFOUND_CV(KLON) 

REAL(KIND=JPRB) :: ZRHO    ! DENSITY AT SURFACE                             (KG/M^3) 
REAL(KIND=JPRB) :: ZUST    ! U_STAR                                         (M/S)
REAL(KIND=JPRB) :: ZKHVFL  ! SURFACE BUOYANCY FLUX                          (K M/S)
REAL(KIND=JPRB) :: ZWS     ! SIGMA_W            AT LOWEST MODEL HALFLEVEL   (M/S)
REAL(KIND=JPRB) :: ZQEXC   ! HUMIDITY    EXCESS AT LOWEST MODEL HALFLEVEL   (#)
REAL(KIND=JPRB) :: ZTEXC   ! TEMPERATURE EXCESS AT LOWEST MODEL HALFLEVEL   (K)
REAL(KIND=JPRB) :: ZTVENH  ! ENVIRONMENT VIRTUAL TEMPERATURE AT HALF LEVELS (K)  
REAL(KIND=JPRB) :: ZTVUH   ! UPDRAFT VIRTUAL TEMPERATURE AT HALF LEVELS     (K)
REAL(KIND=JPRB) :: ZQEXC5, ZTEXC5, ZFACT6, ZFACT7, ZFACT8, ZRHO5I, ZWORK25
REAL(KIND=JPRB) :: ZWORK1, ZWORK2, ZRHO5
REAL(KIND=JPRB) :: ZRCPD

REAL(KIND=JPRB) :: ZWORK15 (KLON,KLEV), ZWORK15T(KLON,1:3,KLEV)
REAL(KIND=JPRB) :: ZTU15(KLON,KLEV),    ZQU15(KLON,KLEV)
REAL(KIND=JPRB) :: ZLU15(KLON,KLEV)
REAL(KIND=JPRB) :: ZSUH15(KLON,KLEV),   ZWU2H15(KLON,KLEV)
REAL(KIND=JPRB) :: ZSUH25(KLON,KLEV),   ZQU25(KLON,KLEV)
REAL(KIND=JPRB) :: ZBUOH15(KLON,KLEV)

REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "cupdra.intfb.h"
#include "cupdraad.intfb.h"

!----------------------------------------------------------------------
!     0.           INITIALIZE CONSTANTS AND FIELDS
!                  -------------------------------
!----------------------------------------------------------------------

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

JKT1=NJKT12
ZRCPD=1.0_JPRB/RCPD

LLMID(:)=.FALSE.
LLGO_ON(:)=.FALSE.
LLGO_ON2(:,:)=.FALSE.
LL_LDO_ASC(:)=.FALSE.
LL_LMIDTEST(:,:)=.FALSE.
LLTEST1(:,:)=.FALSE.

PWUBASE5(:)=0.0_JPRB

DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    PBUOH5 (JL,JK)=0.0_JPRB
    PQPRCV5(JL,JK)=0.0_JPRB
    PLGLAC5(JL,JK)=0.0_JPRB
  ENDDO
ENDDO

!----------------------------------------------------------------------
!       ---------------------------
!       1.1  INITIALIZE FIELDS
!       ---------------------------

DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    ZSUH5(JL,JK)=0.0_JPRB
    ZWU2H5(JL,JK)=0.0_JPRB
    ZSENH5(JL,JK)= RCPD*PTENH5(JL,JK)+PGEOH5(JL,JK)
    ZTU05(JL,JK)=PTU5(JL,JK)
    ZQU05(JL,JK)=PQU5(JL,JK)
  ENDDO
ENDDO

DO JKK=1,KLEV
  DO JL=KIDIA,KFDIA
    ZWORK15T(JL,1:3,JKK)=0.0_JPRB
    ZWORK15(JL,JKK)=0.0_JPRB
  ENDDO
ENDDO

KCBOT  (:)  =-1      ! cloud base level for convection, (-1 if not found)
KSTUP  (:)  =-1      ! level at which the updraft starts (-1 if not found)
KBOTSC (:)  =-1      ! sc    base level for sc-clouds , (-1 if not found)
KCTOP  (:)  =-1      ! cloud top for convection (-1 if not found)
KTYPE  (:)  =0       ! convection type (0 if no convection)
LDCUM  (:)  =.FALSE. ! on exit: true if convection on
LDSC   (:)  =.FALSE. ! on exit: true if cloudbase=found

IF (LDCVOPT) THEN
! Get trajectory for adjoint computations from input arrays
! that were stored in CUBASEN2.
  DO JK=1,KLEV
    DO JL=KIDIA,KFDIA
      I_KLAB2 (JL,JK) = KLAB55   (JL,JK)
      ZTU15   (JL,JK) = PTU55    (JL,JK) 
      ZQU15   (JL,JK) = PQU55    (JL,JK) 
      ZLU15   (JL,JK) = PLU55    (JL,JK) 
    ENDDO
  ENDDO
  DO JK=1,KLEV
    DO JL=KIDIA,KFDIA
      ZSUH15  (JL,JK) = PSUH55   (JL,JK) 
      ZWU2H15 (JL,JK) = PWU2H55  (JL,JK) 
      ZBUOH15 (JL,JK) = PBUOH55  (JL,JK)
    ENDDO
  ENDDO
  DO JK=1,KLEV
    DO JL=KIDIA,KFDIA
      ZQU25   (JL,JK) = PQU255   (JL,JK)
      ZSUH25  (JL,JK) = PSUH255  (JL,JK)
      ZWORK15 (JL,JK) = PWORK155 (JL,JK)
      ZTVENH15(JL,JK) = PTVENH55 (JL,JK)
    ENDDO
  ENDDO
  DO JK=1,KLEV
    DO JL=KIDIA,KFDIA
      ZTVUH15 (JL,JK) = PTVUH55  (JL,JK)
      LLTEST1 (JL,JK) = LDTEST155(JL,JK)
      LLGO_ON2(JL,JK) = LDGO_ON55(JL,JK)
      LL_LMIDTEST(JL,JK) = LDMIDTEST55(JL,JK)
    ENDDO
  ENDDO
  DO JK=1,KLEV
    DO JL=KIDIA,KFDIA
      DO JKK=1,3
        ZWORK15T(JL,JKK,JK) = PWORK155T(JL,JKK,JK)
      ENDDO
    ENDDO
  ENDDO
  DO JL=KIDIA,KFDIA
    LDCUM(JL)  = LDCUM55(JL)
    KCBOT(JL)  = KCBOT55(JL)
    ZFACT3(JL) = PFACT355(JL)
    ZFACT4(JL) = PFACT455(JL)
    ZFACT5(JL) = PFACT555(JL)
    ZWS5(JL)   = PWS55(JL)
    ZUST5(JL)  = PUST55(JL)
    ZKHVFL5(JL)= PKHVFL55(JL)
    IF (LDCUM(JL)) THEN
      IKB = KCBOT(JL)
      ZWU2H5(JL,IKB) = PWU2H155(JL)
      PWUBASE5(JL) = PWUBASE55(JL)
    ENDIF
  ENDDO

ELSE

  LLFOUND_CV(:)=.FALSE.

  DO JK=KLEV-1,JKT1,-1

! Initialize local arrays
    ICBOT(:)=-1
    ISTUP(:)=-1
    IBOTSC(:)=-1
    ICTOP(:)=-1
    LLCUM(:)=.FALSE.
    LLSC(:)=.FALSE.

    DO JKK=1,KLEV
      DO JL=KIDIA,KFDIA
        ZBUOH5 (JL,JKK)=0.0_JPRB
        ZQPRCV5(JL,JKK)=0.0_JPRB
        ZLGLAC5(JL,JKK)=0.0_JPRB  
      ENDDO
    ENDDO
    DO JKK=1,KLEV
      DO JL=KIDIA,KFDIA
        ZTU5   (JL,JKK)=ZTU05(JL,JKK)
        ZQU5   (JL,JKK)=ZQU05(JL,JKK)
      ENDDO
    ENDDO
    DO JKK=1,KLEV
      DO JL=KIDIA,KFDIA
        ZLU5   (JL,JKK)=0.0_JPRB
        ZZWU2H5(JL,JKK)=0.0_JPRB
        ZZSUH5 (JL,JKK)=0.0_JPRB
        ILAB(JL,JKK)=0
      ENDDO
    ENDDO

    IF (JK == KLEV-1) THEN

!        ---------------------------------------------------------
!        1.2    INITIALISE FIELDS AT LOWEST HALF MODEL LEVEL
!        ---------------------------------------------------------

!LOP    LL_LDO_ASC(JK)=.TRUE.

      DO JL=KIDIA,KFDIA

        ZRHO5 = PAPH5(JL,KLEV+1)/(RD*(PTEN5(JL,KLEV)*(1.0_JPRB+RETV*PQEN5(JL,KLEV))))

!LOP    ZSQRTUV5(JL)=SQRT(PSSTRU5(JL)**2 + PSSTRV5(JL)**2)
!LOP    IF (ZSQRTUV5(JL) > REPUST) THEN
!LOP      ZUST5(JL) = ZSQRTUV5(JL)
!LOP    ELSE
!LOP      ZUST5(JL) = REPUST
!LOP    ENDIF
        ZUST5(JL) = 0.1_JPRB

        ZKHVFL5(JL) = (PAHFS5(JL,KLEV+1)*ZRCPD &
         & + RETV*PTEN5(JL,KLEV)*PQHFL5(JL,KLEV+1))/ZRHO5  

        ZFACT3(JL) = ZUST5(JL)**3 &
         & - 1.5_JPRB*RKAP*ZKHVFL5(JL)*PGEOH5(JL,KLEV)/PTEN5(JL,KLEV)  
        ZWS5(JL) = 1.2_JPRB * MAX(ZFACT3(JL),0.0_JPRB)**(1.0_JPRB/3._JPRB)

        IF (ZKHVFL5(JL) < 0.0_JPRB) THEN
          LLGO_ON(JL)=.TRUE.
          LLGO_ON2(JL,JK)=LLGO_ON(JL)
          ILAB(JL,KLEV)= 1
          ZFACT4(JL)=-1.5_JPRB*PAHFS5(JL,KLEV+1)/(ZRHO5*ZWS5(JL)*RCPD)
          ZTEXC5 = MAX(ZFACT4(JL),0.0_JPRB)
          ZFACT5(JL)=-1.5_JPRB*PQHFL5(JL,KLEV+1)/(ZRHO5*ZWS5(JL))
          ZQEXC5 = MAX(ZFACT5(JL),0.0_JPRB)
          ZQU5(JL,KLEV) = PQENH5(JL,KLEV) + ZQEXC5
          ZZSUH5(JL,KLEV)= ZSENH5(JL,KLEV) + RCPD*ZTEXC5
          ZTU5(JL,KLEV) = (ZSENH5(JL,KLEV)-PGEOH5(JL,KLEV))*ZRCPD + ZTEXC5
          ZZWU2H5(JL,KLEV)= ZWS5(JL)*ZWS5(JL)

!  determine buoyancy at lowest half level

          ZTVENH15(JL,JK)=(1.0_JPRB+RETV*PQENH5(JL,KLEV)) &
           & * (ZSENH5(JL,KLEV)-PGEOH5(JL,KLEV))*ZRCPD  
          ZTVUH15(JL,JK) = (1.0_JPRB+RETV*ZQU5(JL,KLEV))*ZTU5(JL,KLEV)
          ZBUOH5(JL,KLEV) = (ZTVUH15(JL,JK)-ZTVENH15(JL,JK))*RG/ZTVENH15(JL,JK)  
        ELSE
          LLGO_ON(JL)=.FALSE.           ! non-convective point
          LLGO_ON2(JL,JK)=LLGO_ON(JL)
        ENDIF
      ENDDO

    ELSE

!        -------------------------------------------------
!        1.3    CHECK FOR "MID-LEVEL" CONVECTION 
!               AND INITIALISE FIELDS BEFORE ASCENT 
!               (SIMPLIFIED SCHEME)
!        -------------------------------------------------
 
!LOP    LL_LDO_ASC(JK)=.TRUE.

      DO JL=KIDIA,KFDIA
 
        LL_LMIDTEST(JL,JK)=(.NOT.LLFOUND_CV(JL) .AND. PGEOH5(JL,JK+1) < 1.5E+05_JPRB)

        IF (LL_LMIDTEST(JL,JK)) THEN

          LLGO_ON(JL)=.TRUE.
          LLGO_ON2(JL,JK)=LLGO_ON(JL)
 
          ZTEXC5=0.2_JPRB
          ZQEXC5=1.E-04_JPRB
          ZQU5(JL,JK+1)=PQENH5(JL,JK+1) + ZQEXC5
          ZZSUH5(JL,JK+1)=ZSENH5(JL,JK+1) + RCPD*ZTEXC5
          ZTU5(JL,JK+1)=(ZSENH5(JL,JK+1)-PGEOH5(JL,JK+1))*ZRCPD + ZTEXC5
          ZLU5(JL,JK+1)=0.0_JPRB
          ZZWU2H5(JL,JK+1)=1.0_JPRB
          ILAB(JL,JK+1)=1

! construct mixed layer for parcels emanating in lowest 60 hPa
          IF (PAPH5(JL,KLEV+1)-PAPH5(JL,JK)<60.E2_JPRB) THEN
            ZQU5(JL,JK+1)=0.0_JPRB
            ZZSUH5(JL,JK+1)=0.0_JPRB
            DO JKK=JK+2,JK,-1
              ZWORK15T(JL,JKK-JK+1,JK)=ZWORK15(JL,JK)
              IF( ZWORK15(JL,JK) < 50.E2_JPRB ) THEN
                ZWORK25=PAPH5(JL,JKK)-PAPH5(JL,JKK-1)
                ZWORK15(JL,JK)=ZWORK15(JL,JK)+ZWORK25
                ZQU5(JL,JK+1)=ZQU5(JL,JK+1)+PQENH5(JL,JKK)*ZWORK25
                ZZSUH5(JL,JK+1)=ZZSUH5(JL,JK+1)+ZSENH5(JL,JKK)*ZWORK25
             ENDIF
            ENDDO
! Store trajectory
            ZSUH25(JL,JK)=ZZSUH5(JL,JK+1)
            ZQU25(JL,JK)=ZQU5(JL,JK+1)

            ZQU5(JL,JK+1)  = ZQU5(JL,JK+1)  /ZWORK15(JL,JK) + ZQEXC5
            ZZSUH5(JL,JK+1)= ZZSUH5(JL,JK+1)/ZWORK15(JL,JK) + RCPD*ZTEXC5
            ZTU5(JL,JK+1)  =(ZZSUH5(JL,JK+1)-PGEOH5(JL,JK+1))*ZRCPD + ZTEXC5
          ENDIF
 
!  determine buoyancy 
 
          ZTVENH15(JL,JK) = (1.0_JPRB+RETV*PQENH5(JL,JK+1)) &
                        & * (ZSENH5(JL,JK+1)-PGEOH5(JL,JK+1))*ZRCPD  
          ZTVUH15(JL,JK) = (1.0_JPRB+RETV*ZQU5(JL,JK+1))*ZTU5(JL,JK+1)
          ZBUOH5(JL,JK+1) = (ZTVUH15(JL,JK)-ZTVENH15(JL,JK))*RG/ZTVENH15(JL,JK)

        ELSE
          LLGO_ON(JL)=.FALSE.
          LLGO_ON2(JL,JK)=LLGO_ON(JL)
        ENDIF

      ENDDO

    ENDIF

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

!     2.0          DO ASCENT IN SUBCLOUD AND LAYER,
!                  CHECK FOR EXISTENCE OF CONDENSATION LEVEL,
!                  ADJUST T,Q AND L ACCORDINGLY IN *CUADJTQ*,
!                  CHECK FOR BUOYANCY AND SET FLAGS
!                  -------------------------------------
!       ------------------------------------------------------------
!        2.1  DO THE VERTICAL ASCENT UNTIL VELOCITY BECOMES NEGATIVE
!       ------------------------------------------------------------

!LOP  IF (LL_LDO_ASC(JK)) THEN

      IKSTART=JK

! Store trajectory
      I_KLAB2(:,JK)=ILAB(:,IKSTART+1)
      ZTU15(:,JK)  =ZTU5(:,IKSTART+1) 
      ZQU15(:,JK)  =ZQU5(:,IKSTART+1) 
      ZLU15(:,JK)  =ZLU5(:,IKSTART+1) 
      ZSUH15(:,JK) =ZZSUH5(:,IKSTART+1) 
      ZWU2H15(:,JK)=ZZWU2H5(:,IKSTART+1) 
      ZBUOH15(:,JK)=ZBUOH5(:,IKSTART+1)

      CALL CUPDRA &
       & ( KIDIA,    KFDIA,    KLON,    KLEV,&
       & IKSTART,  LLGO_ON,  LDRAIN1D,&
       & PQENH5,   PGEOH5,   PAPH5,    PGEO5,&
       & ZTU5,     ZQU5,     ZLU5,&
       & ZSENH5,   ZZWU2H5,  ZZSUH5,&
       & ZBUOH5,   ZLGLAC5,  ZQPRCV5,&
       & ILAB,     LLCUM,    LLSC,     ICBOT,&
       & IBOTSC,   ICTOP,    ISTUP )  

      DO JL=KIDIA,KFDIA
        IF (LLCUM(JL)) THEN
          ZDEPTH5=(PAPH5(JL,ICBOT(JL))-PAPH5(JL,ICTOP(JL)))
          LLTEST1(JL,JK)=((IKSTART == KLEV-1 .AND. ZDEPTH5 <  RDEPTHS2) .OR. &
                       &  (IKSTART  < KLEV-1 .AND. ZDEPTH5 >= RDEPTHS2))
          IF (LLTEST1(JL,JK)) THEN
            DO JKK=1,KLEV
              PTU5(JL,JKK)=ZTU5(JL,JKK)
              PQU5(JL,JKK)=ZQU5(JL,JKK)
              PLU5(JL,JKK)=ZLU5(JL,JKK)
              ZWU2H5(JL,JKK)=ZZWU2H5(JL,JKK)
              ZSUH5(JL,JKK)=ZZSUH5(JL,JKK)
              PBUOH5(JL,JKK)=ZBUOH5(JL,JKK)
              PLGLAC5(JL,JKK)=ZLGLAC5(JL,JKK)
              PQPRCV5(JL,JKK)=ZQPRCV5(JL,JKK)
              KLAB(JL,JKK)=ILAB(JL,JKK)
            ENDDO
            LDCUM(JL)=LLCUM(JL)
            LDSC(JL)=LLSC(JL)
            KCBOT(JL)=ICBOT(JL)
            KBOTSC(JL)=IBOTSC(JL)
            KCTOP(JL)=ICTOP(JL)
            KSTUP(JL)=ISTUP(JL)
            IF (IKSTART < KLEV-1) LLFOUND_CV(JL)=.TRUE.
          ENDIF
        ENDIF
      ENDDO

!LOP    LL_LDO_ASC(JK-1)=.FALSE.

!LOP  ENDIF

  ENDDO

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

! Set flag for mid-level convection in simplified scheme

!LOP IF (LMFMID2) THEN
!LOP   DO JL=KIDIA,KFDIA
!LOP     IF (LDCUM(JL).AND.LLMID(JL)) KTYPE(JL)=3
!LOP   ENDDO
!LOP ENDIF

! Set updraft vertical velocity at cloud base 

  DO JL=KIDIA,KFDIA
    IF (LDCUM(JL)) THEN
      IKB=KCBOT(JL)
      IF (ZWU2H5(JL,IKB) > 0.0_JPRB) THEN 
        PWUBASE5(JL)=SQRT(ZWU2H5(JL,IKB))
      ELSE
        PWUBASE5(JL)=0.0_JPRB
      ENDIF 
    ENDIF
  ENDDO

! endif .NOT.LDCVOPT
ENDIF

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

!     *****               ADJOINT CALCULATIONS             ******

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

!*         0.     INITIALIZATIONS
!                 ---------------

ZSENH(:,:)=0.0_JPRB
ZSUH(:,:) =0.0_JPRB
ZWU2H(:,:)=0.0_JPRB
ZQU0(:,:)=0.0_JPRB
ZTU0(:,:)=0.0_JPRB

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

! Set updraft vertical velocity at cloud base

DO JL=KIDIA,KFDIA
  IF (LDCUM(JL)) THEN
    IKB=KCBOT(JL)
    IF (ZWU2H5(JL,IKB) > 0.0_JPRB) THEN
      ZWU2H(JL,IKB)=ZWU2H(JL,IKB)+0.5_JPRB*PWUBASE(JL) &
       & /PWUBASE5(JL)  
    ENDIF
    PWUBASE(JL)=0.0_JPRB
  ENDIF
ENDDO

!LOP ZLU is initialized here and not inside the JK loop 
!LOP because it is not reset to zero in the forward part.
ZLU(:,:)=0.0_JPRB

DO JK=JKT1,KLEV-1

  ZQPRCV(:,:)=0.0_JPRB
  ZLGLAC(:,:)=0.0_JPRB
  ZBUOH(:,:)=0.0_JPRB
  ZZSUH(:,:)=0.0_JPRB
  ZZWU2H(:,:)=0.0_JPRB
  ZQU(:,:)=0.0_JPRB
  ZTU(:,:)=0.0_JPRB

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

!     2.0          DO ASCENT IN SUBCLOUD AND LAYER,
!                  CHECK FOR EXISTENCE OF CONDENSATION LEVEL,
!                  ADJUST T,Q AND L ACCORDINGLY IN *CUADJTQ*,
!                  CHECK FOR BUOYANCY AND SET FLAGS
!                  -------------------------------------
!       ------------------------------------------------------------
!        2.1  DO THE VERTICAL ASCENT UNTIL VELOCITY BECOMES NEGATIVE
!       ------------------------------------------------------------

!LOP  IF (LL_LDO_ASC(JK)) THEN

    DO JL=KIDIA,KFDIA
      IF (LLTEST1(JL,JK)) THEN
        DO JKK=1,KLEV
          ZQPRCV(JL,JKK)=ZQPRCV(JL,JKK)+PQPRCV(JL,JKK)
          PQPRCV(JL,JKK)=0.0_JPRB
          ZLGLAC(JL,JKK)=ZLGLAC(JL,JKK)+PLGLAC(JL,JKK)
          PLGLAC(JL,JKK)=0.0_JPRB
          ZBUOH(JL,JKK)=ZBUOH(JL,JKK)+PBUOH(JL,JKK)
          PBUOH(JL,JKK)=0.0_JPRB
          ZZSUH(JL,JKK)=ZZSUH(JL,JKK)+ZSUH(JL,JKK)
          ZSUH(JL,JKK)=0.0_JPRB
          ZZWU2H(JL,JKK)=ZZWU2H(JL,JKK)+ZWU2H(JL,JKK)
          ZWU2H(JL,JKK)=0.0_JPRB
          ZLU(JL,JKK)=ZLU(JL,JKK)+PLU(JL,JKK)
          PLU(JL,JKK)=0.0_JPRB
          ZQU(JL,JKK)=ZQU(JL,JKK)+PQU(JL,JKK)
          PQU(JL,JKK)=0.0_JPRB
          ZTU(JL,JKK)=ZTU(JL,JKK)+PTU(JL,JKK)
          PTU(JL,JKK)=0.0_JPRB
        ENDDO
      ENDIF
    ENDDO

    IKSTART=JK
    LLGO_ON(:)=LLGO_ON2(:,JK)

! Use proper trajectory
    ICBOT(:)=-1
    ISTUP(:)=-1
    IBOTSC(:)=-1
    ICTOP(:)=-1
    LLCUM(:)=.FALSE.
    LLSC(:)=.FALSE.

    DO JKK=1,KLEV
      DO JL=KIDIA,KFDIA
        ZBUOH5 (JL,JKK)=0.0_JPRB
        ZQPRCV5(JL,JKK)=0.0_JPRB
        ZLGLAC5(JL,JKK)=0.0_JPRB  
      ENDDO
    ENDDO
    DO JKK=1,KLEV
      DO JL=KIDIA,KFDIA
        ZTU5(JL,JKK)=ZTU05(JL,JKK)
        ZQU5(JL,JKK)=ZQU05(JL,JKK)
        ZLU5(JL,JKK)=0.0_JPRB
       ENDDO
    ENDDO
    DO JKK=1,KLEV
      DO JL=KIDIA,KFDIA
        ZZWU2H5(JL,JKK)=0.0_JPRB
        ZZSUH5 (JL,JKK)=0.0_JPRB
        ILAB(JL,JKK)=0
      ENDDO
    ENDDO

    DO JL=KIDIA,KFDIA
      IF (LLGO_ON(JL)) THEN
        IKP1=IKSTART+1
        ILAB(JL,IKP1)=I_KLAB2(JL,JK)
        ZTU5(JL,IKP1)=ZTU15(JL,JK)
        ZQU5(JL,IKP1)=ZQU15(JL,JK)
        ZLU5(JL,IKP1)=ZLU15(JL,JK)
        ZZWU2H5(JL,IKP1)=ZWU2H15(JL,JK)
        ZZSUH5(JL,IKP1)=ZSUH15(JL,JK)
        ZBUOH5(JL,IKP1)=ZBUOH15(JL,JK)
      ENDIF
    ENDDO

    CALL CUPDRAAD &
     & ( KIDIA,    KFDIA,    KLON,    KLEV,&
     & IKSTART,  LLGO_ON,&
     & PQENH5,   PGEOH5,   PAPH5,    PGEO5,&
     & ZTU5,     ZQU5,     ZLU5,&
     & ZSENH5,   ZZWU2H5,  ZZSUH5,&
     & ZBUOH5,   ZLGLAC5,  ZQPRCV5,&
     & ILAB,     LLCUM,    LLSC,     ICBOT,&
     & IBOTSC,   ICTOP,    ISTUP,&
     & PQENH,    PGEOH,    PAPH,     PGEO,&
     & ZTU,      ZQU,      ZLU,&
     & ZSENH,    ZZWU2H,   ZZSUH,&
     & ZBUOH,    ZLGLAC,   ZQPRCV )  

!LOP  ENDIF

  IF (JK == KLEV-1) THEN

!        ---------------------------------------------------------
!        1.2    INITIALISE FIELDS AT LOWEST HALF MODEL LEVEL
!        ---------------------------------------------------------

    DO JL=KIDIA,KFDIA

      ZRHO5 = PAPH5(JL,KLEV+1)/(RD*(PTEN5(JL,KLEV)*(1.0_JPRB+RETV*PQEN5(JL,KLEV))))
      ZRHO5I=1.0_JPRB/ZRHO5

      ZWS=0.0_JPRB
      ZUST=0.0_JPRB
      ZKHVFL=0.0_JPRB
      ZRHO=0.0_JPRB

      IF (ZKHVFL5(JL) < 0.0_JPRB) THEN

!  determine buoyancy at lowest half level

        ZTVUH=0.0_JPRB
        ZTVENH=0.0_JPRB
        ZTEXC=0.0_JPRB
        ZQEXC=0.0_JPRB

! Reduce buoyancy perturbation if required
        IF (LREGCV) THEN
          ZBUOH(JL,KLEV) = ZBUOH(JL,KLEV)*0.35_JPRB
        ENDIF

        ZTVUH=ZTVUH+RG*ZBUOH(JL,KLEV)/ZTVENH15(JL,JK)
        ZTVENH=ZTVENH-RG*ZBUOH(JL,KLEV)*ZTVUH15(JL,JK)/(ZTVENH15(JL,JK)**2)     
        ZBUOH(JL,KLEV)=0.0_JPRB
      
        ZQU(JL,KLEV)=ZQU(JL,KLEV)+ZTVUH*RETV*ZTU15(JL,JK)
        ZTU(JL,KLEV)=ZTU(JL,KLEV)+ZTVUH*(1.0_JPRB+RETV*ZQU15(JL,JK))
        ZTVUH=0.0_JPRB

        PQENH(JL,KLEV)=PQENH(JL,KLEV)+ZTVENH*RETV*ZRCPD &
         & *(ZSENH5(JL,KLEV)-PGEOH5(JL,KLEV))  
        ZSENH(JL,KLEV)=ZSENH(JL,KLEV)+ZTVENH &
         & *(1.0_JPRB+RETV*PQENH5(JL,KLEV))*ZRCPD  
        PGEOH(JL,KLEV)=PGEOH(JL,KLEV)-ZTVENH &
         & *(1.0_JPRB+RETV*PQENH5(JL,KLEV))*ZRCPD  
        ZTVENH=0.0_JPRB

        ZWS=ZWS+2.0_JPRB*ZZWU2H(JL,KLEV)*ZWS5(JL)
        ZZWU2H(JL,KLEV)=0.0_JPRB

        ZSENH(JL,KLEV)=ZSENH(JL,KLEV)+ZTU(JL,KLEV)*ZRCPD
        PGEOH(JL,KLEV)=PGEOH(JL,KLEV)-ZTU(JL,KLEV)*ZRCPD
        ZTEXC=ZTEXC+ZTU(JL,KLEV)
        ZTU(JL,KLEV)=0.0_JPRB

        ZSENH(JL,KLEV)=ZSENH(JL,KLEV)+ZZSUH(JL,KLEV)
        ZTEXC=ZTEXC+RCPD*ZZSUH(JL,KLEV)
        ZZSUH(JL,KLEV)=0.0_JPRB
 
        PQENH(JL,KLEV)=PQENH(JL,KLEV)+ZQU(JL,KLEV)
        ZQEXC=ZQEXC+ZQU(JL,KLEV)
        ZQU(JL,KLEV)=0.0_JPRB

        IF (ZFACT5(JL) > 0.0_JPRB) THEN
          PQHFL(JL,KLEV+1)=PQHFL(JL,KLEV+1)+ZQEXC*ZFACT5(JL) &
           & /PQHFL5(JL,KLEV+1)  
          ZRHO=ZRHO-ZQEXC*ZFACT5(JL)*ZRHO5I
          ZWS=ZWS-ZQEXC*ZFACT5(JL)/ZWS5(JL)
        ENDIF
        ZQEXC=0.0_JPRB

        IF (ZFACT4(JL) > 0.0_JPRB) THEN
          PAHFS(JL,KLEV+1)=PAHFS(JL,KLEV+1)+ZTEXC*ZFACT4(JL) &
           & /PAHFS5(JL,KLEV+1)  
          ZRHO=ZRHO-ZTEXC*ZFACT4(JL)*ZRHO5I
          ZWS=ZWS-ZTEXC*ZFACT4(JL)/ZWS5(JL)  
        ENDIF
        ZTEXC=0.0_JPRB

      ENDIF

      IF (ZFACT3(JL) > 0.0_JPRB) THEN
! Reduce perturbation of w* if required
        IF (LREGCV .AND. ABS(ZWS5(JL)) < 0.5_JPRB) THEN
          ZWS=0.0_JPRB
        ENDIF

        ZFACT7=1.0_JPRB/(ZWS5(JL)*ZWS5(JL))
        ZFACT6=1.728_JPRB*0.5_JPRB*RKAP/PTEN5(JL,KLEV)*ZFACT7
        ZUST=ZUST+ZWS*1.728_JPRB*ZFACT7*ZUST5(JL)*ZUST5(JL)
        ZKHVFL=ZKHVFL-ZWS*ZFACT6*PGEOH5(JL,KLEV)
        PGEOH(JL,KLEV)=PGEOH(JL,KLEV)-ZWS*ZFACT6*ZKHVFL5(JL)
        PTEN(JL,KLEV)=PTEN(JL,KLEV)+ZWS*ZFACT6*ZKHVFL5(JL) &
         & *PGEOH5(JL,KLEV)/PTEN5(JL,KLEV)   
      ENDIF
      ZWS=0.0_JPRB

      PAHFS(JL,KLEV+1)=PAHFS(JL,KLEV+1)+ZKHVFL*ZRCPD*ZRHO5I
      PTEN(JL,KLEV)=PTEN(JL,KLEV)+ZKHVFL*RETV*PQHFL5(JL,KLEV+1)*ZRHO5I  
      PQHFL(JL,KLEV+1)=PQHFL(JL,KLEV+1)+ZKHVFL*RETV*PTEN5(JL,KLEV)*ZRHO5I  
      ZRHO=ZRHO-ZKHVFL*ZKHVFL5(JL)*ZRHO5I
      ZKHVFL=0.0_JPRB
    
!LOP    IF (ZSQRTUV5(JL) > REPUST) THEN
!LOP      PSSTRU(JL)=PSSTRU(JL)+ZUST*PSSTRU5(JL)/ZUST5(JL)
!LOP      PSSTRV(JL)=PSSTRV(JL)+ZUST*PSSTRV5(JL)/ZUST5(JL)
!LOP    ENDIF
      ZUST=0.0_JPRB
    
      PAPH(JL,KLEV+1)=PAPH(JL,KLEV+1)+ZRHO*ZRHO5/PAPH5(JL,KLEV+1)
      PTEN(JL,KLEV)=PTEN(JL,KLEV)-ZRHO*ZRHO5/PTEN5(JL,KLEV)
      PQEN(JL,KLEV)=PQEN(JL,KLEV)-ZRHO*RETV*ZRHO5 &
       & /(1.0_JPRB+RETV*PQEN5(JL,KLEV))  
      ZRHO=0.0_JPRB

    ENDDO

  ELSE

!        -------------------------------------------------
!        1.3    CHECK FOR "MID-LEVEL" CONVECTION
!               AND INITIALISE FIELDS BEFORE ASCENT
!               (SIMPLIFIED SCHEME)
!        -------------------------------------------------

    DO JL=KIDIA,KFDIA

     IF (LL_LMIDTEST(JL,JK)) THEN

        ZTVUH=0.0_JPRB
        ZTVENH=0.0_JPRB
        ZTEXC=0.0_JPRB
        ZQEXC=0.0_JPRB
        ZWORK1=0.0_JPRB
        ZWORK2=0.0_JPRB

! Reduce buoyancy perturbation if required
        IF (LREGCV) THEN
          ZBUOH(JL,JK+1) = ZBUOH(JL,JK+1)*0.35_JPRB
        ENDIF

!  determine buoyancy

        ZTVUH=ZTVUH+ZBUOH(JL,JK+1)*RG/ZTVENH15(JL,JK)
        ZTVENH=ZTVENH-ZBUOH(JL,JK+1)*RG*ZTVUH15(JL,JK)/(ZTVENH15(JL,JK)**2)  
        ZBUOH(JL,JK+1)=0.0_JPRB

        ZQU(JL,JK+1)=ZQU(JL,JK+1)+ZTVUH*RETV*ZTU15(JL,JK) 
        ZTU(JL,JK+1)=ZTU(JL,JK+1)+ZTVUH*(1.0_JPRB+RETV*ZQU15(JL,JK))
        ZTVUH=0.0_JPRB

        PQENH(JL,JK+1)=PQENH(JL,JK+1)+ZTVENH*RETV*ZRCPD &
         & *(ZSENH5(JL,JK+1)-PGEOH5(JL,JK+1))  
        ZSENH(JL,JK+1)=ZSENH(JL,JK+1)+ZTVENH &
         & *(1.0_JPRB+RETV*PQENH5(JL,JK+1))*ZRCPD  
        PGEOH(JL,JK+1)=PGEOH(JL,JK+1)-ZTVENH &
         & *(1.0_JPRB+RETV*PQENH5(JL,JK+1))*ZRCPD  
        ZTVENH=0.0_JPRB

! construct mixed layer for parcels emanating in lowest 60 hPa
        IF (PAPH5(JL,KLEV+1)-PAPH5(JL,JK)<60.E2_JPRB) THEN

          ZZSUH(JL,JK+1)= ZZSUH(JL,JK+1) + ZTU(JL,JK+1)*ZRCPD
          PGEOH(JL,JK+1)= PGEOH(JL,JK+1) - ZTU(JL,JK+1)*ZRCPD
          ZTEXC         = ZTEXC          + ZTU(JL,JK+1)
          ZTU(JL,JK+1)=0.0_JPRB

          ZFACT8 = 1.0_JPRB/ZWORK15(JL,JK)
          ZWORK1 = ZWORK1 - ZZSUH(JL,JK+1)*ZSUH25(JL,JK)*(ZFACT8**2.0_JPRB)
          ZTEXC  = ZTEXC  + ZZSUH(JL,JK+1)*RCPD
          ZZSUH(JL,JK+1)=ZZSUH(JL,JK+1)*ZFACT8

          ZWORK1 = ZWORK1 - ZQU(JL,JK+1)*ZQU25(JL,JK)*(ZFACT8**2.0_JPRB)
          ZQEXC  = ZQEXC  + ZQU(JL,JK+1)
          ZQU(JL,JK+1)=ZQU(JL,JK+1)*ZFACT8 

          DO JKK=JK,JK+2
            IF (ZWORK15T(JL,JKK-JK+1,JK) < 50.E2_JPRB) THEN
              ZWORK25=PAPH5(JL,JKK)-PAPH5(JL,JKK-1)

              ZSENH(JL,JKK)=ZSENH(JL,JKK)+ZZSUH(JL,JK+1)*ZWORK25
              ZWORK2       =ZWORK2       +ZZSUH(JL,JK+1)*ZSENH5(JL,JKK)
      
              PQENH(JL,JKK)=PQENH(JL,JKK)+ZQU(JL,JK+1)*ZWORK25
              ZWORK2       =ZWORK2       +ZQU(JL,JK+1)*PQENH5(JL,JKK)

              ZWORK2=ZWORK2+ZWORK1

              PAPH(JL,JKK  )=PAPH(JL,JKK  )+ZWORK2
              PAPH(JL,JKK-1)=PAPH(JL,JKK-1)-ZWORK2
              ZWORK2=0.0_JPRB
            ENDIF
          ENDDO           
          ZZSUH(JL,JK+1)=0.0_JPRB
          ZQU (JL,JK+1)=0.0_JPRB           
        ENDIF             

        ZZWU2H(JL,JK+1)=0.0_JPRB
        ZLU  (JL,JK+1)=0.0_JPRB

        ZSENH(JL,JK+1)=ZSENH(JL,JK+1)+ZTU(JL,JK+1)*ZRCPD
        PGEOH(JL,JK+1)=PGEOH(JL,JK+1)-ZTU(JL,JK+1)*ZRCPD
        ZTEXC         =ZTEXC         +ZTU(JL,JK+1)
        ZTU(JL,JK+1)=0.0_JPRB

        ZSENH(JL,JK+1)=ZSENH(JL,JK+1)+ZZSUH(JL,JK+1)
        ZTEXC         =ZTEXC         +ZZSUH(JL,JK+1)*RCPD
        ZZSUH(JL,JK+1)=0.0_JPRB

        PQENH(JL,JK+1)=PQENH(JL,JK+1)+ZQU(JL,JK+1)
        ZQEXC         =ZQEXC         +ZQU(JL,JK+1) 
        ZQU(JL,JK+1)=0.0_JPRB   
    
      ENDIF

    ENDDO

  ENDIF

  DO JKK=1,KLEV
    DO JL=KIDIA,KFDIA
      ZZSUH(JL,JKK)=0.0_JPRB
      ZZWU2H(JL,JKK)=0.0_JPRB
      ZLU(JL,JKK)=0.0_JPRB
    ENDDO
  ENDDO
  DO JKK=1,KLEV
    DO JL=KIDIA,KFDIA
      ZQU0(JL,JKK)=ZQU0(JL,JKK)+ZQU(JL,JKK)
      ZQU(JL,JKK)=0.0_JPRB
      ZTU0(JL,JKK)=ZTU0(JL,JKK)+ZTU(JL,JKK)
      ZTU(JL,JKK)=0.0_JPRB
    ENDDO
  ENDDO
  DO JKK=1,KLEV
    DO JL=KIDIA,KFDIA
      ZLGLAC(JL,JKK)=0.0_JPRB
      ZQPRCV(JL,JKK)=0.0_JPRB
      ZBUOH(JL,JKK)=0.0_JPRB
    ENDDO
  ENDDO

ENDDO

!----------------------------------------------------------------------
!       -----------------------------------------------------------
!       1.1  PREPARE FIELDS ON HALF LEVELS BY LINEAR INTERPOLATION
!             OF SPECIFIC HUMIDITY AND STATIC ENERGY
!       -----------------------------------------------------------

DO JK=KLEV,1,-1
  DO JL=KIDIA,KFDIA
    PQU(JL,JK)=PQU(JL,JK)+ZQU0(JL,JK)
    ZQU0(JL,JK)=0.0_JPRB
    PTU(JL,JK)=PTU(JL,JK)+ZTU0(JL,JK)
    ZTU0(JL,JK)=0.0_JPRB

    PTENH(JL,JK)=PTENH(JL,JK)+RCPD*ZSENH(JL,JK)
    PGEOH(JL,JK)=PGEOH(JL,JK)+ZSENH(JL,JK)
    ZSENH(JL,JK)=0.0_JPRB
    ZWU2H(JL,JK)=0.0_JPRB
    ZSUH(JL,JK)=0.0_JPRB
  ENDDO
ENDDO

!----------------------------------------------------------------------
!     0.           INITIALIZE CONSTANTS AND FIELDS
!                  -------------------------------
!----------------------------------------------------------------------

DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    PLGLAC(JL,JK)=0.0_JPRB
    PQPRCV(JL,JK)=0.0_JPRB
    PBUOH(JL,JK)=0.0_JPRB
  ENDDO
ENDDO

DO JL=KIDIA,KFDIA
  PWUBASE(JL)=0.0_JPRB
ENDDO

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