!OPTIONS XOPT(NOEVAL)
SUBROUTINE ACUPM( KIDIA,KFDIA,KLON,KTDIA,KLEV,&
! ----------------------------------------------------------------------
! - INPUT  2D .
                &PCP, PLHS, PLHV,&
                &PEVEL0, PIPOI, PPOID, PQU,PTU,PUU,PVU,PUDAL, PUDOM,& 
                &PFPEVPL,PFPEVPN,&
! - OUTPUT 2D
                &POME,& 
                &PFHP, &
! - INPUT/OUTPUT  2D .
                &PQV,PQW,PT,PTW, PU, PV,&
! - LOCK ARGUMENTS .
                &CDLOCK)
! **** *ACUPM * - PREPARE INTERNAL STATE BEFORE DOWNDRAUGHT
!           Prepare Latent heat flux for dd closure
!               + Correspondent update of PT
!           (Following LUDEN switch, adapt to ud envt)
!                 
! **   Interface.
!      ----------
! *CALL* *ACUPM*
! ----------------------------------------------------------------------
! - INPUT ARGUMENTS
! -------------------

! - DIMENSIONING PARAMETERS FOR PHYSICS

! KIDIA      : INDICE DE DEPART DES BOUCLES VECTORISEES SUR L'HORIZONT..
! KFDIA      : INDICE DE FIN DES BOUCLES VECTORISEES SUR L'HORIZONTALE.
! KLON       : DIMENSION HORIZONTALE DES TABLEAUX.
! KTDIA      : INDICE DE DEPART DES BOUCLES VERTICALES (NIVEL DU SOMMET
!            : DU NUAGE).
! KLEV       : DIMENSION VERTICALE DES TABLEAUX "FULL LEVEL".


! - PHYSICAL VARIABLES

! - 2D (1:KLEV) .
! PCP
! PLHS
! PLHV
! PEVEL0    : ETA_dot*d(Pi)/d(ETA)at full levels
! PIPOI     : g*dt/delta p
! PPOID     : delta p/g*dt
! PUDAL     : updraught mesh fraction
! PUDOM     : relative updraught vertical velocity
! PQU       : updraught water vapour
! PTU       : updraught temperature
! PUU       : updraught zonal wind
! PVU       : updraught meridian wind

! 
! - 2D FLUXES (0:KLEV) .
! PFPEVPL
! PFPEVPN

! ----------------------------------------------------------------------
! -   OUTPUT ARGUMENTS 
!     ----------------
! - 1D
! - 2D
! POME   : Vertical velocity *dt in updraught environment (Pa)
! PFHP   : Latent heat flux associated to precipitation
!           (limited "downdraught closure" edition)
! ----------------------------------------------------------------------
! -   INPUT/OUTPUT ARGUMENTS 
!     ----------------------
! - 2D (1:KLEV)
! PQV  : Internal state water vapour
! PT   : Internal state temperature
! PQW  : Internal state Wet bulb moisture
! PTW  : Internal state Wet bulb temperatore
! 
! - 2D FLUXES (0:KLEV)
! 
! ----------------------------------------------------------------------
!     Author.
!     -------
!     01-2007, L. Gerard 

!     Modifications.
!     --------------
!     04-2007, L. Gerard 
!     A.Bogatchev : 26-Oct-2007 removing of severe breaks of coding rools
! ----------------------------------------------------------------------

USE PARKIND1 , ONLY: JPIM, JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMPHY   , ONLY : LCDDEVPRO, LUDEN
USE YOMPHY0  , ONLY : GDDEVF, GCVALMX
USE YOMPHY2  , ONLY : TSPHY
IMPLICIT NONE

! ----------------------------------------------------------------------
! SUBROUTINE ARGUMENTS
INTEGER(KIND=JPIM),INTENT(IN) :: KFDIA
INTEGER(KIND=JPIM),INTENT(IN) :: KIDIA
INTEGER(KIND=JPIM),INTENT(IN) :: KLEV
INTEGER(KIND=JPIM),INTENT(IN) :: KLON
INTEGER(KIND=JPIM),INTENT(IN) :: KTDIA

CHARACTER (LEN=5),INTENT(IN) :: CDLOCK

REAL(KIND=JPRB) ,INTENT(IN) :: PCP(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(IN) :: PLHS(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(IN) :: PLHV(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(IN) :: PEVEL0(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(IN) :: PIPOI(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(IN) :: PPOID(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(IN) :: PUDAL(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(IN) :: PUDOM(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(IN) :: PQU(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(IN) :: PTU(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(IN) :: PUU(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(IN) :: PVU(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(IN) :: PFPEVPL(KLON,0:KLEV)
REAL(KIND=JPRB) ,INTENT(IN) :: PFPEVPN(KLON,0:KLEV)
! - OUTPUT  2D .
REAL(KIND=JPRB) ,INTENT(OUT) :: POME(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(OUT) :: PFHP(KLON,0:KLEV)
! - INPUT/OUTPUT  2D .
REAL(KIND=JPRB) ,INTENT(INOUT) :: PQV(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(INOUT) :: PQW(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(INOUT) :: PT(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(INOUT) :: PTW(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(INOUT) :: PU(KLON,KLEV)
REAL(KIND=JPRB) ,INTENT(INOUT) :: PV(KLON,KLEV)

! ----------------------------------------------------------------------
! LOCALS
INTEGER (KIND=JPIM) :: JLEV, JLON
REAL(KIND=JPRB)  :: ZDDEVC,  ZZ, ZUDAL
 REAL(KIND=JPRB) :: ZHOOK_HANDLE
! ----------------------------------------------------------------------
#include "abor1.intfb.h"
! ----------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('ACUPM',0,ZHOOK_HANDLE)

!     CHECK RELIABILITY OF INPUT ARGUMENTS.
IF(CDLOCK /= 'ACUPM') THEN
  WRITE(0,*) 'CDLOCK=',CDLOCK
  CALL ABOR1('ACUPM BAD NUMBER OF ARGUMENTS!')
ENDIF
! *
! ------------------------------------------------------------------
!     I - INITIALISATIONS


! EFFECT OF GDDEVF on INITIAL DD T-PROFILE:
IF (LCDDEVPRO) THEN
   ZDDEVC=1.0_JPRB-GDDEVF
ELSE
   ZDDEVC=0.0_JPRB
ENDIF
! ------------------------------------------------------------------
!     II - CALCULATIONS

PFHP(KIDIA:KFDIA,KTDIA-1)=0._JPRB
DO JLEV=KTDIA,KLEV
  DO JLON=KIDIA,KFDIA
    !
    !  ------------------------------------------------------------
    !   COMPUTE THE TEMPERATURE SINK SOURCE FOR DOWNDRAUGHT CLOSURE
    !  ------------------------------------------------------------
    !
    PFHP(JLON,JLEV)= PFHP(JLON,JLEV-1) + ( &
      &  PLHV(JLON,JLEV)*(PFPEVPL(JLON,JLEV)-PFPEVPL(JLON,JLEV-1)) &
      & +PLHS(JLON,JLEV)*(PFPEVPN(JLON,JLEV)-PFPEVPN(JLON,JLEV-1)) )
! Effect of 1-gddevf on profile
! if PFHP is bigger at level l than l-1, there is residual cooling of layer l
    PT(JLON,JLEV)= PT(JLON,JLEV)-ZDDEVC*(PIPOI(JLON,JLEV)/PCP(JLON,JLEV))*&
      &(PFHP(JLON,JLEV)-PFHP(JLON,JLEV-1))
    !
    ! Adapt values to updraught environment
    ! according to LUDEN switch
    !
    ZUDAL=MIN(GCVALMX,PUDAL(JLON,JLEV))
! Mean Updraught environment vertical velocity:
    POME(JLON,JLEV)=(PEVEL0(JLON,JLEV)&
         &   -ZUDAL*PUDOM(JLON,JLEV))*TSPHY
    IF (LUDEN) THEN
      ZZ=1.0_JPRB-ZUDAL
      ZZ=1.0_JPRB/ZZ
      PQV(JLON,JLEV)=(PQV(JLON,JLEV)-PUDAL(JLON,JLEV)*PQU(JLON,JLEV))*ZZ
      PT(JLON,JLEV)=(PT(JLON,JLEV)-PUDAL(JLON,JLEV)*PTU(JLON,JLEV))*ZZ
      PU(JLON,JLEV)=(PU(JLON,JLEV)-PUDAL(JLON,JLEV)*PUU(JLON,JLEV))*ZZ
      PV(JLON,JLEV)=(PV(JLON,JLEV)-PUDAL(JLON,JLEV)*PVU(JLON,JLEV))*ZZ
! Remark; the cloud condensates have only a passive role in acmodo
!         and we keep them as mean grid-box values.

! Normally PTW and PQW should be re-computed based on PT, PQV !
! Do we leave them unchanged ??
    ENDIF
  ENDDO
ENDDO
! *
! ------------------------------------------------------------------
!     I - INITIALISATIONS


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