SUBROUTINE CUBASMCNTL &
 & (KIDIA,    KFDIA,    KLON,     KLEV,&
 & KK,&
 & PTEN5,    PQEN5,    PQSEN5,&
 & PVERVEL5, PGEO5,    PGEOH5,   LDCUM,    KTYPE,    KLAB,&
 & KCBOT,    PMFU5,    PMFUB5,   PENTR5,   PLRAIN5,&
 & PTU5,     PQU5,     PLU5,&
 & PMFUS5,   PMFUQ5,   PMFUL5,   PDMFUP5 )  

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

!          Adapted from

!          M.TIEDTKE         E.C.M.W.F.     12/89

!          PURPOSE.
!          --------
!          THIS ROUTINE CALCULATES CLOUD BASE VALUES
!          FOR MIDLEVEL CONVECTION
!          (Tangent linear)

!          INTERFACE
!          ---------

!          THIS ROUTINE IS CALLED FROM *CUASCNTL*.
!          INPUT ARE ENVIRONMENTAL VALUES T,Q ETC
!          IT RETURNS CLOUDBASE VALUES FOR MIDLEVEL CONVECTION

!          METHOD.
!          --------
!          M. TIEDTKE (1989)

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

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

!    INPUT PARAMETERS (REAL):

!    *PTEN5*        PROVISIONAL ENVIRONMENT TEMPERATURE (T+1)    (Trajectory) 
!    *PQEN5*        PROVISIONAL ENVIRONMENT SPEC. HUMIDITY (T+1) (Trajectory)
!    *PQSEN5*       ENVIRONMENT SPEC. SATURATION HUMIDITY (T+1)  (Trajectory)
!    *PVERVEL5*     VERTICAL VELOCITY                            (Trajectory)
!    *PGEO5*        GEOPOTENTIAL                                 (Trajectory)
!    *PGEOH5*       GEOPOTENTIAL ON HALF LEVELS                  (Trajectory)
!    *PLRAIN5*      RAIN WATER CONTENT IN UPDRAFTS               (Trajectory)

!    INPUT PARAMETERS (LOGICAL):

!    *LDCUM*        FLAG: .TRUE. FOR CONVECTIVE POINTS 

!    UPDATED PARAMETERS (INTEGER):

!    *KTYPE*        TYPE OF CONVECTION
!                       1 = PENETRATIVE CONVECTION
!                       2 = SHALLOW CONVECTION
!                       3 = MIDLEVEL CONVECTION
!    *KLAB*         FLAG KLAB=1 FOR SUBCLOUD LEVELS
!                        KLAB=2 FOR CLOUD LEVELS
!    *KCBOT*        CLOUD BASE LEVEL

!    OUTPUT PARAMETERS (REAL):

!    *PMFU5*        MASSFLUX IN UPDRAFTS                    (Trajectory)
!    *PMFUB5*       MASSFLUX IN UPDRAFTS AT CLOUD BASE      (Trajectory)
!    *PENTR5*       FRACTIONAL MASS ENTRAINMENT RATE        (Trajectory)
!    *PTU5*         TEMPERATURE IN UPDRAFTS                 (Trajectory)
!    *PQU5*         SPEC. HUMIDITY IN UPDRAFTS              (Trajectory)
!    *PLU5*         LIQUID WATER CONTENT IN UPDRAFTS        (Trajectory)
!    *PMFUS5*       FLUX OF DRY STATIC ENERGY IN UPDRAFTS   (Trajectory)
!    *PMFUQ5*       FLUX OF SPEC. HUMIDITY IN UPDRAFTS      (Trajectory)
!    *PMFUL5*       FLUX OF LIQUID WATER IN UPDRAFTS        (Trajectory)
!    *PDMFUP5*      FLUX DIFFERENCE OF PRECIP. IN UPDRAFTS  (Trajectory)

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

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

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

USE YOMCST   , ONLY : RG       ,RCPD
USE YOECUMF  , ONLY : ENTRMID  ,RMFCMAX  ,RMFCMIN  ,LMFMID

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)    :: KK 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQSEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVERVEL5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEO5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOH5(KLON,KLEV+1) 
LOGICAL           ,INTENT(IN)    :: LDCUM(KLON) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KTYPE(KLON) 
INTEGER(KIND=JPIM),INTENT(INOUT) :: KLAB(KLON,KLEV) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KCBOT(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PMFU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFUB5(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PENTR5(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PLRAIN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PLU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PMFUS5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PMFUQ5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PMFUL5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDMFUP5(KLON,KLEV) 
INTEGER(KIND=JPIM) :: JL

REAL(KIND=JPRB) :: ZZZMB5
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!*    1.           CALCULATE ENTRAINMENT AND DETRAINMENT RATES
!                  -------------------------------------------

!DIR$ IVDEP
!OCL NOVREC
IF (LHOOK) CALL DR_HOOK('CUBASMCNTL',0,ZHOOK_HANDLE)
DO JL=KIDIA,KFDIA
  IF(LMFMID.AND.PGEO5(JL,KK) > 5000.0_JPRB.AND.PGEO5(JL,KK)<1.E5_JPRB) THEN
    IF(.NOT.LDCUM(JL).AND.KLAB(JL,KK+1) == 0.0_JPRB &
       & .AND.PQEN5(JL,KK) > 0.80_JPRB*PQSEN5(JL,KK)) THEN  
      PTU5(JL,KK+1)=(RCPD*PTEN5(JL,KK)+PGEO5(JL,KK)-PGEOH5(JL,KK+1))/RCPD
      PQU5(JL,KK+1)=PQEN5(JL,KK)
      PLU5(JL,KK+1)=0.0_JPRB
!jfm            ZZZMB5=MAX(RMFCMIN,-PVERVEL5(JL,KK)/RG)
!jfm            ZZZMB5=MIN(ZZZMB5,RMFCMAX)
      IF (RMFCMIN > (-PVERVEL5(JL,KK)/RG)) THEN
        ZZZMB5=RMFCMIN
      ELSE
        ZZZMB5=-PVERVEL5(JL,KK)/RG
      ENDIF
      IF (ZZZMB5 > RMFCMAX) THEN
        ZZZMB5=RMFCMAX
      ENDIF
      PMFUB5(JL)=ZZZMB5
      PMFU5(JL,KK+1)=PMFUB5(JL)
      PMFUS5(JL,KK+1)=PMFUB5(JL)*(RCPD*PTU5(JL,KK+1)+PGEOH5(JL,KK+1))
      PMFUQ5(JL,KK+1)=PMFUB5(JL)*PQU5(JL,KK+1)
      PMFUL5(JL,KK+1)=0.0_JPRB
      PDMFUP5(JL,KK+1)=0.0_JPRB
      PLRAIN5(JL,KK+1)=0.0_JPRB
      KCBOT(JL)=KK
      KLAB(JL,KK+1)=1
      KTYPE(JL)=3
      PENTR5(JL)=ENTRMID
    ENDIF
  ENDIF
ENDDO

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