SUBROUTINE CPHFLUX ( KLON, KIDIA, KFDIA, KLEV,&
 & PDT,&
 & LD_LCTEMP, LD_LCTOP,&
 !  Variables 2D Input
 & PTEND,PTENDQ,PTENDU,PTENDV, PT, PQ, PU, PV, PDELP,&
 !  Variables 2D In/Output
 & PFLUX )  

! *  ROUTINE COMPUTES FLUXES FROM TENDENCIES

!    PDT    = TIME STEP
!    LCTEMP = LOGICAL = TRUE IF TEMPERATURE TENDENCIES WHICH COMPUTE  FL
!                       OF THE ENTHALPY
!    LCTOP  = LOGICAL = TRUE IF WE BEGIN THE INTEGRATION FROM TOP
!    PTEND  = TENDENCIES WICH CALCULATE THE FLUXES
!    PTENDQ = HUMIDITY ONLY USED IF LCTEMP = TRUE
!    PTENDU = U WIND ONLY USED IF LCTEMP = TRUE
!    PTENDV = V WIND ONLY USED IF LCTEMP = TRUE
!    PT     = TEMPERATURE
!    PQ     = HUMIDITY
!    PU     = U WIND
!    PV     = V WIND
!    PDELP  = DELTA P FULL LEVEL

!    PFLUX  = MUST CONTAIN BOUNDARIE VALUES EITHER AT TOP OR BOTTOM DEPE

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

USE YOMCST  , ONLY : RCPD , RCPV , RG

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)    :: PDT 
LOGICAL           ,INTENT(IN)    :: LD_LCTEMP 
LOGICAL           ,INTENT(IN)    :: LD_LCTOP 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTEND(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDQ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDV(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PV(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDELP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFLUX(KLON,0:KLEV) 
INTEGER(KIND=JPIM) :: JLEV, JLON
REAL(KIND=JPRB) ::ZCOEF1,ZCOEF2,ZCOEF3,ZCP1,ZCP9
REAL(KIND=JPRB) :: ZHOOK_HANDLE

IF (LHOOK) CALL DR_HOOK('CPHFLUX',0,ZHOOK_HANDLE)
IF (LD_LCTEMP) THEN
  ZCOEF1 = RCPD
  ZCOEF2 = RCPV - RCPD
  ZCOEF3 = 1.0_JPRB
ELSE
  ZCOEF1 = 1.0_JPRB
  ZCOEF2 = 0.0_JPRB
  ZCOEF3 = 0.0_JPRB
ENDIF

IF (LD_LCTOP) THEN
  DO JLEV = 1,KLEV
    DO JLON = KIDIA,KFDIA
      ZCP9=ZCOEF1+ZCOEF2*PQ(JLON,JLEV)
      ZCP1=ZCOEF1+ZCOEF2*(PQ(JLON,JLEV)+PTENDQ(JLON,JLEV)*PDT)
      PFLUX(JLON,JLEV)=PFLUX(JLON,JLEV-1) - PDELP(JLON,JLEV)/RG &
       & * (   PTEND(JLON,JLEV) * ZCP1 +  ZCOEF3 *&
       & ( 0.5_JPRB*PDT*((PTENDU(JLON,JLEV))**2 + (PTENDV(JLON,JLEV))**2)&
       & +   ( PU(JLON,JLEV)*PTENDU(JLON,JLEV)&
       & +         PV(JLON,JLEV)*PTENDV(JLON,JLEV) )&
       & +   PT(JLON,JLEV) * (ZCP1-ZCP9)/PDT  )  )  
    ENDDO
  ENDDO
ELSE
  DO JLEV = KLEV,1,-1
    DO JLON = KIDIA,KFDIA
      ZCP9=ZCOEF1+ZCOEF2*PQ(JLON,JLEV)
      ZCP1 = ZCOEF1+ZCOEF2*(PQ(JLON,JLEV)+PTENDQ(JLON,JLEV)*PDT)
      PFLUX(JLON,JLEV-1)=PFLUX(JLON,JLEV) + PDELP(JLON,JLEV)/RG &
       & * (   PTEND(JLON,JLEV) * ZCP1 +  ZCOEF3 *&
       & ( 0.5_JPRB*PDT*((PTENDU(JLON,JLEV))**2 + (PTENDV(JLON,JLEV))**2)&
       & +   ( PU(JLON,JLEV)*PTENDU(JLON,JLEV)&
       & +         PV(JLON,JLEV)*PTENDV(JLON,JLEV) )&
       & +   PT(JLON,JLEV) * (ZCP1-ZCP9)/PDT  )   )  
    ENDDO
  ENDDO
ENDIF

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