SUBROUTINE DIAG_CLOUDS&
 & ( KIDIA,  KFDIA,  KLON,   KLEV,  &
 &   LDCUM,  KCBOT,  PGEO,   PGEOH,&   
 &   PT,     PL,     PI,     PA,&
 &   PCBASE, P0DEGL )

!    P.BECHTOLD     E.C.M.W.F.     02/2010

!    PURPOSE
!    -------

!    TO PROVIDE DIAGNOSTICS FOR CLOUD BASE
!    AND ZERO DEGREE LEVEL

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

!    METHOD.
!    --------
!    CLOUD BASE COMPUTATIONS TAKE INTO ACCOUNT "STRATIFORM" AND
!    CONVECTIVE CLOUD BASE
!    ZERO DEGREE LEVEL DETERMINATIONSEARCHES FOR FIRST SWAP FROM POSITIVE
!    TO NEGATIVE TEMPERATURES, CORRESPONDING HEIGHT IS SET TO MID-LEVEL VALUE

!    CLOUD BASE AND ZERO DEGREE LEVEL ARE GIVEN IN (M) ABOVE GROUND

!    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)

!    *PGEO*         GEOPOTENTIAL                                  M2/S2
!    *PGEOH*        GEOPOTENTIAL ON HALF LEVELS                   M2/S2
!    *PT*           TEMPERATURE                                    K
!    *PL*           LIQUID WATER CONTENT                          KG/KG
!    *PI*           ICE WATER CONTENT                             KG/KG
!    *PA*           CLOUD FRACTION

!    OUTPUT PARAMETERS (REAL) 

!    *PCBASE*      CLOUD BASE                                       M
!    *P0DEGL*      0 DEGREE CELSIUS LEVEL                           M 

!          MODIFICATIONS
!          -------------

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

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

USE YOMCST   , ONLY : RG, RTT
USE YOECUMF  , ONLY : NJKT2

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLON 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KCBOT(KLON)

LOGICAL,           INTENT(IN)    :: LDCUM(KLON)

REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEO(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOH(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PL(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PI(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PA(KLON,KLEV) 

REAL(KIND=JPRB)   ,INTENT(OUT)   :: PCBASE(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: P0DEGL(KLON) 

LOGICAL :: LLSTB(KLON), LL0L(KLON)
INTEGER(KIND=JPIM) :: JL, JK
REAL(KIND=JPRB) ::     ZRG, ZBASE0, ZBASEC(KLON), ZBASES(KLON)

REAL(KIND=JPRB) :: ZHOOK_HANDLE
!----------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('DIAG_CLOUDS',0,ZHOOK_HANDLE)

ZRG=1.0_JPRB/RG
ZBASE0=20.E3_JPRB

! Initialisations
DO JL=KIDIA,KFDIA
  PCBASE(JL)=ZBASE0
  ZBASEC(JL)=ZBASE0 ! convective cloud base
  ZBASES(JL)=ZBASE0 ! "stratiform" or cloud scheme based cloud base
  P0DEGL(JL)=0.0_JPRB

  LLSTB(JL)=.TRUE.
  LL0L(JL)=.TRUE.
ENDDO

! Convective Cloud Base 
DO JL=KIDIA,KFDIA
  IF (LDCUM(JL)) THEN
    JK=KCBOT(JL)
    ZBASEC(JL)=PGEOH(JL,JK)*ZRG
  ENDIF
ENDDO

! Vertical Search loop 
DO JK=KLEV-1,NJKT2,-1
! cloud base defined by liquid water/cloud fraction criterion
  DO JL=KIDIA,KFDIA
    IF(LLSTB(JL)) THEN
       IF(PA(JL,JK)>1.E-2_JPRB.AND.(PL(JL,JK)+PI(JL,JK))>1.E-6_JPRB) THEN
         ZBASES(JL)=PGEO(JL,JK)*ZRG
         LLSTB(JL)=.FALSE.
       ENDIF
    ENDIF
  ENDDO
! zero degree level, take mid-level value
  DO JL=KIDIA,KFDIA
    IF(LL0L(JL)) THEN
       IF(PT(JL,JK+1)>RTT.AND.PT(JL,JK)<=RTT) THEN
         P0DEGL(JL)=PGEOH(JL,JK+1)*ZRG
         LL0L(JL)=.FALSE.
       ENDIF
    ENDIF
  ENDDO
ENDDO

DO JL=KIDIA,KFDIA
  PCBASE(JL)=ZBASES(JL)
  IF(ZBASEC(JL)<ZBASE0.AND.ZBASES(JL)<ZBASE0) PCBASE(JL)=MIN(ZBASEC(JL),ZBASES(JL))
ENDDO

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

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