SUBROUTINE CUININTL &
 & ( KIDIA,    KFDIA,    KLON,    KLEV,&
 & PTEN5,    PQEN5,    PQSEN5,   PUEN5,    PVEN5,&
 & PVERVEL5, PGEO5,    PAPH5,&
 & KLWMIN,   KLAB,&
 & PTENH5,   PQENH5,   PQSENH5,  PGEOH5,&
 & PTU5,     PQU5,     PTD5,     PQD5,&
 & PUU5,     PVU5,     PUD5,     PVD5,&
 & PLU5,&
 & PTEN,     PQEN,     PQSEN,    PUEN,     PVEN,&
 & PVERVEL,  PGEO,     PAPH,&
 & PTENH,    PQENH,    PQSENH,   PGEOH,&
 & PTU,      PQU,      PTD,      PQD,&
 & PUU,      PVU,      PUD,      PVD,&
 & PLU  )  

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

!          Adapted from 

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

!          PURPOSE
!          -------

!          THIS ROUTINE INTERPOLATES LARGE-SCALE FIELDS OF T,Q ETC.
!          TO HALF LEVELS (I.E. GRID FOR MASSFLUX SCHEME),
!          DETERMINES LEVEL OF MAXIMUM VERTICAL VELOCITY
!          AND INITIALIZES VALUES FOR UPDRAFTS AND DOWNDRAFTS
!          (Tangent linear)

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

!          METHOD.
!          --------
!          FOR EXTRAPOLATION TO HALF LEVELS SEE 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 

!    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)
!    *PUEN5*        PROVISIONAL ENVIRONMENT U-VELOCITY (T+1)     (Trajectory)
!    *PVEN5*        PROVISIONAL ENVIRONMENT V-VELOCITY (T+1)     (Trajectory) 
!    *PVERVEL5*     VERTICAL VELOCITY                            (Trajectory) 
!    *PGEO5*        GEOPOTENTIAL                                 (Trajectory) 
!    *PGEOH5*       GEOPOTENTIAL ON HALF LEVELS                  (Trajectory) 
!    *PAPH5*        PROVISIONAL PRESSURE ON HALF LEVELS          (Trajectory) 

!    *PTEN*         PROVISIONAL ENVIRONMENT TEMPERATURE (T+1)       K 
!    *PQEN*         PROVISIONAL ENVIRONMENT SPEC. HUMIDITY (T+1)  KG/KG 
!    *PQSEN*        ENVIRONMENT SPEC. SATURATION 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
!    *PGEO*         GEOPOTENTIAL                                  M2/S2
!    *PGEOH*        GEOPOTENTIAL ON HALF LEVELS                   M2/S2
!    *PAPH*         PROVISIONAL PRESSURE ON HALF LEVELS             PA

!    OUTPUT PARAMETERS (INTEGER):

!    *KLWMIN*       LEVEL OF MAXIMUM VERTICAL VELOCITY 
!    *KLAB*         FLAG KLAB=1 FOR SUBCLOUD LEVELS
!                        KLAB=2 FOR CONDENSATION LEVEL

!    OUTPUT PARAMETERS (REAL):

!    *PTENH5*       ENV. TEMPERATURE (T+1) ON HALF LEVELS     (Trajectory)
!    *PQENH5*       ENV. SPEC. HUMIDITY (T+1) ON HALF LEVELS  (Trajectory)
!    *PQSENH5*      ENV. SPEC. SATURATION HUMIDITY (T+1)      (Trajectory)
!                   ON HALF LEVELS                       
!    *PTU5*         TEMPERATURE IN UPDRAFTS                   (Trajectory)
!    *PQU5*         SPEC. HUMIDITY IN UPDRAFTS                (Trajectory)
!    *PTD5*         TEMPERATURE IN DOWNDRAFTS                 (Trajectory)
!    *PQU5*         SPEC. HUMIDITY IN DOWNDRAFTS              (Trajectory)
!    *PUU5*         U-VELOCITY IN UPDRAFTS                    (Trajectory)
!    *PVU5*         V-VELOCITY IN UPDRAFTS                    (Trajectory)
!    *PUD5*         U-VELOCITY IN DOWNDRAFTS                  (Trajectory)
!    *PVD5*         V-VELOCITY IN DOWNDRAFTS                  (Trajectory)  
!    *PLU5*         LIQUID WATER CONTENT IN UPDRAFTS          (Trajectory)   

!    *PTENH*        ENV. TEMPERATURE (T+1) ON HALF LEVELS         K
!    *PQENH*        ENV. SPEC. HUMIDITY (T+1) ON HALF LEVELS    KG/KG
!    *PQSENH*       ENV. SPEC. SATURATION HUMIDITY (T+1)
!                   ON HALF LEVELS                              KG/KG
!    *PTU*          TEMPERATURE IN UPDRAFTS                       K
!    *PQU*          SPEC. HUMIDITY IN UPDRAFTS                  KG/KG
!    *PTD*          TEMPERATURE IN DOWNDRAFTS                     K
!    *PQU*          SPEC. HUMIDITY IN DOWNDRAFTS                KG/KG
!    *PUU*          U-VELOCITY IN UPDRAFTS                       M/S
!    *PVU*          V-VELOCITY IN UPDRAFTS                       M/S
!    *PUD*          U-VELOCITY IN DOWNDRAFTS                     M/S
!    *PVD*          V-VELOCITY IN DOWNDRAFTS                     M/S
!    *PLU*          LIQUID WATER CONTENT IN UPDRAFTS            KG/KG

!          EXTERNALS
!          ---------
!          *CUADJTQTL* TO SPECIFY QS AT HALF LEVELS

!          MODIFICATIONS
!          -------------
!             92-09-21 : Update to Cy44      J.-J. MORCRETTE
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!             05-02-11 : Optimisation (NJKT2) P. BECHTOLD

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

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

USE YOMCST   , ONLY : RCPD
USE YOECUMF  , ONLY : NJKT2

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)    :: PTEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQSEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVERVEL5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEO5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPH5(KLON,KLEV+1) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KLWMIN(KLON) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KLAB(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENH5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQENH5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQSENH5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOH5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTD5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQD5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PUU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PVU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PUD5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PVD5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PLU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTEN(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQEN(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQSEN(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(IN)    :: PGEO(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPH(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENH(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQENH(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQSENH(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOH(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTD(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQD(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PUU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PVU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PUD(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PVD(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PLU(KLON,KLEV) 
REAL(KIND=JPRB) ::     ZWMAX5(KLON)
REAL(KIND=JPRB) ::     ZPH5(KLON)

REAL(KIND=JPRB) ::     ZWMAX(KLON)
REAL(KIND=JPRB) ::     ZPH(KLON)
LOGICAL ::  LLFLAG(KLON)

INTEGER(KIND=JPIM) :: ICALL, IK, JK, JL

REAL(KIND=JPRB) :: Z1S, Z1S5, Z3S, Z3S5, ZALFA, ZZS, ZZS5, ZORCPD
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "cuadjtqstl.intfb.h"

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

!*    1.           SPECIFY LARGE SCALE PARAMETERS AT HALF LEVELS
!*                 ADJUST TEMPERATURE FIELDS IF STATICLY UNSTABLE
!*                 FIND LEVEL OF MAXIMUM VERTICAL VELOCITY
!                  ----------------------------------------------

IF (LHOOK) CALL DR_HOOK('CUININTL',0,ZHOOK_HANDLE)
ZALFA=LOG(2.0_JPRB)
ZORCPD=1._JPRB/RCPD
DO JK=2,KLEV
  DO JL=KIDIA,KFDIA
!jfm      PTENH5(JL,JK)=(MAX(RCPD*PTEN5(JL,JK-1)+PGEO5(JL,JK-1),
!jfm     1             RCPD*PTEN5(JL,JK)+PGEO5(JL,JK))-PGEOH5(JL,JK))*ZORCPD
    IF ((RCPD*PTEN5(JL,JK-1)+PGEO5(JL,JK-1)) >&
       & (RCPD*PTEN5(JL,JK  )+PGEO5(JL,JK  ))) THEN  
      Z1S  = RCPD*PTEN (JL,JK-1)+PGEO (JL,JK-1)
      Z1S5 = RCPD*PTEN5(JL,JK-1)+PGEO5(JL,JK-1)
    ELSE
      Z1S  = RCPD*PTEN (JL,JK  )+PGEO (JL,JK  )
      Z1S5 = RCPD*PTEN5(JL,JK  )+PGEO5(JL,JK  )
    ENDIF
    PTENH (JL,JK)=(Z1S -PGEOH (JL,JK))*ZORCPD
    PTENH5(JL,JK)=(Z1S5-PGEOH5(JL,JK))*ZORCPD
    PQENH (JL,JK)=PQEN (JL,JK-1)
    PQENH5(JL,JK)=PQEN5(JL,JK-1)
    PQSENH (JL,JK)=PQSEN (JL,JK-1)
    PQSENH5(JL,JK)=PQSEN5(JL,JK-1)
    ZPH (JL)=PAPH (JL,JK)
    ZPH5(JL)=PAPH5(JL,JK)
    LLFLAG(JL)=.TRUE.
  ENDDO

  IF(JK >= KLEV-1 .OR. JK<NJKT2) CYCLE
  IK=JK
  ICALL=0
  CALL CUADJTQSTL &
   & ( KIDIA,    KFDIA,    KLON,    KLEV,&
   & IK,&
   & ZPH5,     PTENH5,   PQSENH5,&
   & ZPH,      PTENH,    PQSENH,   LLFLAG,   ICALL)  

  DO JL=KIDIA,KFDIA
!jfm      PQENH5(JL,JK)=MIN(PQEN5(JL,JK-1),PQSEN5(JL,JK-1))
!jfm     1            +(PQSENH5(JL,JK)-PQSEN5(JL,JK-1))
!jfm      PQENH5(JL,JK)=MAX(PQENH5(JL,JK),0.)
    IF (PQEN5(JL,JK-1) > PQSEN5(JL,JK-1)) THEN
      Z3S  = PQSEN (JL,JK-1)
      Z3S5 = PQSEN5(JL,JK-1)
    ELSE
      Z3S  = PQEN (JL,JK-1)
      Z3S5 = PQEN5(JL,JK-1)
    ENDIF
    PQENH (JL,JK)=Z3S +(PQSENH (JL,JK)-PQSEN (JL,JK-1))
    PQENH5(JL,JK)=Z3S5+(PQSENH5(JL,JK)-PQSEN5(JL,JK-1))
    IF (PQENH5(JL,JK) < 0.0_JPRB) THEN
      PQENH (JL,JK) = 0.0_JPRB
      PQENH5(JL,JK) = 0.0_JPRB
    ENDIF
  ENDDO
ENDDO

DO JL=KIDIA,KFDIA
  PTENH (JL,KLEV)=(RCPD*PTEN (JL,KLEV)+PGEO (JL,KLEV)-PGEOH (JL,KLEV))*ZORCPD
  PTENH5(JL,KLEV)=(RCPD*PTEN5(JL,KLEV)+PGEO5(JL,KLEV)-PGEOH5(JL,KLEV))*ZORCPD
  PQENH (JL,KLEV)=PQEN (JL,KLEV)
  PQENH5(JL,KLEV)=PQEN5(JL,KLEV)
  PTENH (JL,1)=PTEN (JL,1)
  PTENH5(JL,1)=PTEN5(JL,1)
  PQENH (JL,1)=PQEN (JL,1)
  PQENH5(JL,1)=PQEN5(JL,1)
  KLWMIN(JL)=KLEV
  ZWMAX (JL)=0.0_JPRB
  ZWMAX5(JL)=0.0_JPRB
ENDDO

DO JK=KLEV-1,2,-1
  DO JL=KIDIA,KFDIA
!jfm      ZZS5=MAX(RCPD*PTENH5(JL,JK)+PGEOH5(JL,JK),
!jfm     1        RCPD*PTENH5(JL,JK+1)+PGEOH5(JL,JK+1))
    IF ((RCPD*PTENH5(JL,JK  )+PGEOH5(JL,JK  )) >&
       & (RCPD*PTENH5(JL,JK+1)+PGEOH5(JL,JK+1))) THEN  
      ZZS  = RCPD*PTENH (JL,JK  )+PGEOH (JL,JK  )
      ZZS5 = RCPD*PTENH5(JL,JK  )+PGEOH5(JL,JK  )
    ELSE
      ZZS  = RCPD*PTENH (JL,JK+1)+PGEOH (JL,JK+1)
      ZZS5 = RCPD*PTENH5(JL,JK+1)+PGEOH5(JL,JK+1)
    ENDIF
    PTENH (JL,JK)=(ZZS -PGEOH (JL,JK))*ZORCPD
    PTENH5(JL,JK)=(ZZS5-PGEOH5(JL,JK))*ZORCPD
  ENDDO
ENDDO

DO JK=KLEV,3,-1
!DIR$ IVDEP
!OCL NOVREC
  DO JL=KIDIA,KFDIA
    IF(PVERVEL5(JL,JK) < ZWMAX5(JL)) THEN
      ZWMAX (JL)=PVERVEL (JL,JK)
      ZWMAX5(JL)=PVERVEL5(JL,JK)
      KLWMIN(JL)=JK
    ENDIF
  ENDDO
ENDDO

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

!*    2.0          INITIALIZE VALUES FOR UPDRAFTS AND DOWNDRAFTS
!*                 ---------------------------------------------

DO JK=1,KLEV
  IK=JK-1
  IF(JK == 1) IK=1
  DO JL=KIDIA,KFDIA
    PTU (JL,JK)=PTENH (JL,JK)
    PTU5(JL,JK)=PTENH5(JL,JK)
    PTD (JL,JK)=PTENH (JL,JK)
    PTD5(JL,JK)=PTENH5(JL,JK)
    PQU (JL,JK)=PQENH (JL,JK)
    PQU5(JL,JK)=PQENH5(JL,JK)
    PQD (JL,JK)=PQENH (JL,JK)
    PQD5(JL,JK)=PQENH5(JL,JK)
    PLU (JL,JK)=0.0_JPRB
    PLU5(JL,JK)=0.0_JPRB
    PUU (JL,JK)=PUEN (JL,IK)
    PUU5(JL,JK)=PUEN5(JL,IK)
    PUD (JL,JK)=PUEN (JL,IK)
    PUD5(JL,JK)=PUEN5(JL,IK)
    PVU (JL,JK)=PVEN (JL,IK)
    PVU5(JL,JK)=PVEN5(JL,IK)
    PVD (JL,JK)=PVEN (JL,IK)
    PVD5(JL,JK)=PVEN5(JL,IK)
    KLAB(JL,JK)=0
  ENDDO
ENDDO

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