SUBROUTINE VDFDPBLS(KIDIA,KFDIA,KLON,KLEV,&
 & PTM1,PQM1,PGEOM1,PKMFL,PKHFL,PKQFL,PDHPBL)  
!     ------------------------------------------------------------------

!**   *VDFDPBL* - VDFDPBL (Diagnostic PBL height) determines  
!                 simple PBL height for simplified physics

!     Original  M. Ko"hler       E.C.M.W.F.    09/2007.
!     (based on vdfexcu.F90 cy28r1)

!     PURPOSE
!     -------

!     Determine PBL height

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

!     *VDFDPBLS* IS CALLED BY *VDFMAINS*

!     INPUT PARAMETERS (INTEGER):

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

!     INPUT PARAMETERS (REAL):

!     *PTM1*         TEMPERATURE AT T-1
!     *PQM1*         SPECIFIC HUMUDITY AT T-1
!     *PGEOM1*       GEOPOTENTIAL AT T-1
!     *PKMFL*        KINEMATIC MOMENTUM FLUX 
!     *PKHFL*        KINEMATIC HEAT FLUX     
!     *PKQFL*        KINEMATIC MOISTURE FLUX  

!     OUTPUT PARAMETERS (REAL):

!     *PDHPBL*        Boundary layer height                  m

!     METHOD
!     ------

!     SEE DOCUMENTATION

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

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

USE YOMCST   , ONLY : RG, RCPD,RETV
USE YOEVDF   , ONLY : RPAR1

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KLON 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PKMFL(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PKHFL(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PKQFL(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDHPBL(KLON) 

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

LOGICAL            :: LLDONE(KLON)
INTEGER(KIND=JPIM) :: JK, JL
REAL(KIND=JPRB)    :: ZUST(KLON), ZWS(KLON), &
 & ZSVBOT(KLON), ZSVINC(KLON), ZKHVFL(KLON), &
 & ZCONS13, ZCONS14, ZCONS15, ZIPBL, ZSV(KLON,KLEV),&
 & ZREPUST, ZSVREF

REAL(KIND=JPRB)    :: ZHOOK_HANDLE

INTERFACE
#include "surf_inq.h"
END INTERFACE

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

!*       1.     INITIALIZE CONSTANTS
!               --------------------

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

ZIPBL  = 1000._JPRB         ! RPARZI
ZCONS13= 1.0_JPRB/3._JPRB
ZCONS14= RCPD*9.0_JPRB
ZCONS15= RPAR1*RG

CALL SURF_INQ(PREPUST=ZREPUST)

!        2.     PREPARE FOR MIXED LAYER DEPTH
!               -----------------------------

DO JL=KIDIA,KFDIA
  ZUST(JL)=MAX(SQRT(PKMFL(JL)),ZREPUST)
  ZKHVFL(JL)=PKHFL(JL)+RETV*PTM1(JL,KLEV)*PKQFL(JL)
  PDHPBL(JL)=0.0_JPRB
  IF (ZKHVFL(JL)  <  0.0_JPRB) THEN
    ZWS(JL)=(ZUST(JL)**3-ZCONS15*ZKHVFL(JL)*ZIPBL/PTM1(JL,KLEV))**ZCONS13
    ZSVBOT(JL)=RCPD*PTM1(JL,KLEV)*(1.0_JPRB+RETV*PQM1(JL,KLEV))+PGEOM1(JL,KLEV)
    ZSVINC(JL)=-ZCONS14*ZKHVFL(JL)/ZWS(JL)
    LLDONE(JL)=.FALSE.
    ZSV(JL,KLEV)=ZSVBOT(JL)
  ELSE
    LLDONE(JL)=.TRUE.
  ENDIF
ENDDO

!        3.     VERTICAL SCAN TO DETERMINE MIXED LAYER DEPTH
!               --------------------------------------------

DO JK=KLEV-1,1,-1
  DO JL=KIDIA,KFDIA
    IF (.NOT. LLDONE(JL)) THEN
      ZSV(JL,JK)=RCPD*PTM1(JL,JK)*(1.0_JPRB+RETV*PQM1(JL,JK))+PGEOM1(JL,JK)
      ZSVREF=ZSVBOT(JL)+ZSVINC(JL)
      IF ( ZSV(JL,JK) > ZSVREF ) THEN
        PDHPBL(JL)=( ( ZSV(JL,JK) - ZSVREF )   * PGEOM1(JL,JK+1)  &
                & +  ( ZSVREF - ZSV(JL,JK+1) ) * PGEOM1(JL,JK)  ) &
                & /  ( ZSV(JL,JK) - ZSV(JL,JK+1) ) / RG  
        LLDONE(JL)=.TRUE.
      ELSE
        IF ( ZSV(JL,JK) <= ZSVBOT(JL) )  ZSVBOT(JL)=ZSV(JL,JK)
      ENDIF
    ENDIF
  ENDDO
ENDDO


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