!---------------------------------------------------------------
SUBROUTINE CPTENDSMAT( KPROMA, KSTART, KPROF, KFLEV,&
 !---------------------------------------------------------------      
 !  INPUT 2D 
 & PDIFCQ5, PDIFCS5, PDIFTQ5, PDIFTS5,&
 & PFCCQL5, PFCCQN5, PFCSQL5, PFCSQN5,&
 & PFPLCL5, PFPLCN5, PFPLSL5, PFPLSN5,&
 & PFRSO5 , PFRTH5 ,&
 & PSTRCU5, PSTRCV5, PSTRDU5, PSTRDV5,&
 & PSTRTU5, PSTRTV5, &
 & PSTRMU5, PSTRMV5, PFRMH5 ,&
 & PRDELP5, PAPHIF5, &
 & PU5    , PV5    , PT5    , PQ5    , &
 !  INPUT 1D 
 & PQS    , PTS    , POROG  ,      &
 !  OUTPUT 2D 
 & PTENDU5, PTENDV5, PTENDH5, PTENDQ5)     

!**** *CPTENDSMAT* - COMPUTATION OF TRAJECTORIES OF PHYSICAL TENDENCIES
!                    FOR THE SIMPLIFIED PHYSICS

!    Subject.
!    --------
!    Computation of trajectories for:
!     - COMPUTATION OF THE FLUXES OF ENTHALPY AND SENSIBLE HEAT FOR
!       PRECIPITATION AND CONDENSATION
!     - COMPUTATION OF PHYSICAL TENDENCIES FOR U, V, ENTHALPY H, 
!       SPECIFIC HUMIDITY Q AND FOR TEMPERATURE

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

! -   ARGUMENTS FOR INPUT
!     -------------------

! - DIMENSIONAL PARAMETERS

!     KSTART : START OF HORIZONTAL LOOP.
!     KPROF  : END OF HORIZONTAL LOOP.
!     KPROMA : HORIZONTAL DIMENSION.
!     KFLEV  : VERTICAL DIMENSION (FULL LEVELS).

! - 2D (0:KFLEV).
!   -------------
!   For trajectory
!   --------------
!     PDIFCQ5    : CONVECTIVE FLUX OF SPECIFIC HUMIDITY (NOT RAIN/SNOW).
!     PDIFCS5    : CONVECTIVE FLUX OF ENTHALPY (NOT RAIN/SNOW).
!     PDIFTQ5    : TURBULENT FLUX (INC. Q NEGATIVE) OF SPECIFIC HUMIDITY.
!     PDIFTS5    : TURBULENT FLUX OF ENTHALPY (OR DRY STATIC ENERGY).
!     PFCCQL5    : CONVECTIVE CONDENSATION FLUX FOR LIQUID WATER.
!     PFCCQN5    : CONVECTIVE CONDENSATION FLUX FOR ICE. 
!     PFCSQL5    : STRATIFORM CONDENSATION FLUX FOR LIQUID WATER.
!     PFCSQN5    : STRATIFORM CONDENSATION FLUX FOR ICE.
!     PFPLCL5    : CONVECTIVE PRECIPITATION AS RAIN.
!     PFPLCN5    : CONVECTIVE PRECIPITATION AS SNOW.
!     PFPLSL5    : STRATIFORM PRECIPITATION AS RAIN.
!     PFPLSN5    : STRATIFORM PRECIPITATION AS SNOW.
!     PFRSO5     : SHORTWAVE RADIATIVE FLUX.
!     PFRTH5     : LONGWAVE RADIATIVE FLUX.
!     PSTRCU5    : CONVECTIVE FLUX OF MOMENTUM "U".
!     PSTRCV5    : CONVECTIVE FLUX OF MOMENTUM "V".
!     PSTRDU5    : GRAVITY WAVE DRAG FLUX "U".
!     PSTRDV5    : GRAVITY WAVE DRAG FLUX "V".
!     PSTRTU5    : TURBULENT FLUX OF MOMENTUM "U".
!     PSTRTV5    : TURBULENT FLUX OF MOMENTUM "V".
!     PSTRMU5    : MESOSPHERIC FLUX FOR "U"-MOMENTUM.
!     PSTRMV5    : MESOSPHERIC FLUX FOR "V"-MOMENTUM.
!     PFRMH5     : MESOSPHERIC ENTHALPY FLUX.

! - 2D (1:KFLEV).
!   --------------
!   For trajectory
!   ---------------
!     PRDELP5    : INVERSE OF LAYER THICKNESS IN PRESSURE UNITS.
!     PAPHIF5    : GEOPOTENTIAL ON FULL LEVELS.
!     PU5        : X COMPONENT OF WIND.
!     PV5        : Y COMPONENT OF WIND.
!     PT5        : TEMPERATURE.
!     PQ5        : SPECIFIC HUMIDITY OF WATER VAPOUR.
!---------------------------
! --- INPUT 1D.
!     ----------
!     PTS        : SURFACE TEMPERATURE. 
!     PQS        : SURFACE SPECIFIC HUMIDITY. 
!     POROG      : SURFACE GEOPOTENTIAL.

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

! -   ARGUMENTS FOR OUTPUT.
!     ----------------------

! --- 2D (1,KFLEV).
!     -------------
!   For trajectory
!   ----------------
!     PTENDU5    : TENDENCY OF U COMPONENT OF WIND.
!     PTENDV5    : TENDENCY OF V COMPONENT OF WIND.
!     PTENDH5    : TENDENCY OF ENTHALPY. 
!     PTENDQ5    : TENDENCY OF HUMIDITY. 

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

! -  IMPLICIT ARGUMENTS.
!    -------------------

!     COMMON YOMCST : RG,RCPD,RCPV,RCW,RCS,RLVZER,RLSZER
!     COMMON YOMPHY : NDPSFI,LCONDWT

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

!     Methode.
!     --------

!     Author.
!     -------
!        96-01-24, M. Janiskova

!     Modifications.
!     --------------
!        01-03, mesospheric drag - F. Bouyssel
!        M.Hamrud      01-Oct-2003 CY28 Cleaning

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

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

USE YOMCST   , ONLY : RG       ,RCPD     ,RCPV     ,RCW      ,&
 & RCS      ,RLVZER   ,RLSZER  
USE YOMPHY   , ONLY : NDPSFI

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KPROMA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTART 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROF 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFCQ5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFCS5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFTQ5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFTS5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFCCQL5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFCCQN5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFCSQL5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFCSQN5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPLCL5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPLCN5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPLSL5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPLSN5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFRSO5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFRTH5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRCU5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRCV5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRDU5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRDV5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRTU5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRTV5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRMU5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRMV5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFRMH5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRDELP5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHIF5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PU5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PV5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQ5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQS(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTS(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POROG(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENDU5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENDV5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENDH5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTENDQ5(KPROMA,KFLEV) 

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

REAL(KIND=JPRB) :: ZCPS (KPROMA)

!     ZH : ENTHALPIE = CpT + Phi + Ec
REAL(KIND=JPRB) :: ZH5(KPROMA,KFLEV),ZGSDP5(KPROMA,KFLEV),ZCP5(KPROMA,KFLEV)

REAL(KIND=JPRB) :: ZFHSCL5(KPROMA,0:KFLEV),ZFHSCN5(KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFHSSL5(KPROMA,0:KFLEV),ZFHSSN5(KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFHPCL5(KPROMA,0:KFLEV),ZFHPCN5(KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFHPSL5(KPROMA,0:KFLEV),ZFHPSN5(KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFHP5  (KPROMA,0:KFLEV),ZFP5   (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFEPFP5(KPROMA,0:KFLEV)

INTEGER(KIND=JPIM) :: JLEV, JLON, JROF

REAL(KIND=JPRB) :: ZCPI5, ZCVMCD, ZCVMCS, ZCVMCW, ZDPSFI, ZFPDP5, ZTI5
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "abor1.intfb.h"

!------------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('CPTENDSMAT',0,ZHOOK_HANDLE)
!------------------------------------------------------------------------

!*
!     ---------------------------------------------------
!     I - AUXILIARY CONSTANTS

ZDPSFI = REAL( NDPSFI ,JPRB)
ZCVMCD = RCPV - RCPD
ZCVMCW = RCPV - RCW
ZCVMCS = RCPV - RCS

!     -----------------------------------------------------
!     0. 1 - Test for deltam=1.
!     -----------------------------------------------------
IF (NDPSFI==1_JPIM) THEN
  CALL ABOR1('deltam=1 not properly coded in cptendsmat')
 ENDIF

!*
!     -----------------------------------------------------
!     II - COMPUTATION OF Cp.

! - TEMPORARY 1D

!     ZCPS  : SPECIFIC HEAT AT THE SURFACE

! For trajectory
! ---------------
! - TEMPORARY 2D

!     ZCP5  : VALUE OF ATMOSPHERIC Cp

DO JLON=KSTART,KPROF

  ZCPS(JLON)=RCPD*(1.0_JPRB-PQS(JLON))+RCPV*PQS(JLON)
  DO JLEV=1,KFLEV

    ZCP5(JLON,JLEV)=RCPD*(1.0_JPRB-PQ5(JLON,JLEV))+RCPV*PQ5(JLON,JLEV)
  ENDDO
ENDDO

!*
!     ----------------------------------------------------------
!     III - COMPUTATION OF THE FLUXES OF ENTHALPY AND SENSIBLE HEAT FOR
!           PRECIPITATION AND CONDENSATION

! - TEMPORARY 2D (0:KFLEV).

! For trajectory
! ---------------
!     ZFHSCL5 : CONVECTIVE SENSIBLE HEAT FLUX FOR LIQUID WATER.
!     ZFHSCN5 : CONVECTIVE SENSIBLE HEAT FLUX FOR SNOW.
!     ZFHSSL5 : STRATIFORM SENSIBLE HEAT FLUX FOR LIQUID WATER.
!     ZFHSSN5 : STRATIFORM SENSIBLE HEAT FLUX FOR SNOW.
!     ZFHPCL5 : FLUX OF ENTHALPY FOR LIQUID CONVECTIVE CONDENSATION.
!     ZFHPCN5 : FLUX OF ENTHALPY FOR SNOW CONVECTIVE CONDENSATION.
!     ZFHPSL5 : FLUX OF ENTHALPY FOR LIQUID STRATIFORM CONDENSATION.
!     ZFHPSN5 : FLUX OF ENTHALPY FOR SNOW STRATIFORM CONDENSATION.
!     ZFHP5   : TOTAL FLUX OF ENTHALPY + SENSIBLE HEAT.
!     ZFP5    : TOTAL FLUX OF PRECIPITATIONS.
!     ZFEPFP5 : PSEUDO-FLUX OF ENTHALPY FOR ENERGY OF PRECIPITATIONS.

! --- COMPUTATION AT THE TOP.
!     -----------------------

DO JLON=KSTART,KPROF
  ZFHSCL5(JLON,0)=PFPLCL5(JLON,0)*PT5(JLON,1)*(RCW-RCPD*(1.0_JPRB-ZDPSFI))
  ZFHSCN5(JLON,0)=PFPLCN5(JLON,0)*PT5(JLON,1)*(RCS-RCPD*(1.0_JPRB-ZDPSFI))
  ZFHSSL5(JLON,0)=PFPLSL5(JLON,0)*PT5(JLON,1)*(RCW-RCPD*(1.0_JPRB-ZDPSFI))
  ZFHSSN5(JLON,0)=PFPLSN5(JLON,0)*PT5(JLON,1)*(RCS-RCPD*(1.0_JPRB-ZDPSFI))
  ZFHPCL5(JLON,0)= - RLVZER * PFCCQL5(JLON,0)
  ZFHPCN5(JLON,0)= - RLSZER * PFCCQN5(JLON,0)
  ZFHPSL5(JLON,0)= - RLVZER * PFCSQL5(JLON,0)
  ZFHPSN5(JLON,0)= - RLSZER * PFCSQN5(JLON,0)

  ZFP5(JLON,0)= PFPLCL5(JLON,0)+PFPLCN5(JLON,0)&
   & + PFPLSL5(JLON,0)+PFPLSN5(JLON,0)  
  ZFEPFP5(JLON,0)= - ZDPSFI * ZCP5(JLON,1) * PT5(JLON,1) *   ZFP5(JLON,0)
  ZFHP5(JLON,0) = ZFHSCL5(JLON,0) + ZFHSCN5(JLON,0)&
   & + ZFHSSL5(JLON,0) + ZFHSSN5(JLON,0) &
   & + ZFHPCL5(JLON,0) + ZFHPCN5(JLON,0) &
   & + ZFHPSL5(JLON,0) + ZFHPSN5(JLON,0) &
   & + ZFEPFP5(JLON,0)  
ENDDO

! --- CALCULATION THE FLUXES AT THE LEVELS
!     -------------------------------------

DO JLEV = 1 , KFLEV-1
  DO JLON = KSTART , KPROF
    ZTI5 = 0.5_JPRB * ( PT5(JLON,JLEV) + PT5(JLON,JLEV+1) )
    ZCPI5= 0.5_JPRB * ( ZCP5(JLON,JLEV) + ZCP5(JLON,JLEV+1) )

    ZFHSCL5(JLON,JLEV)=PFPLCL5(JLON,JLEV)*ZTI5*(RCW-RCPD*(1.0_JPRB-ZDPSFI))
    ZFHSCN5(JLON,JLEV)=PFPLCN5(JLON,JLEV)*ZTI5*(RCS-RCPD*(1.0_JPRB-ZDPSFI))
    ZFHSSL5(JLON,JLEV)=PFPLSL5(JLON,JLEV)*ZTI5*(RCW-RCPD*(1.0_JPRB-ZDPSFI))
    ZFHSSN5(JLON,JLEV)=PFPLSN5(JLON,JLEV)*ZTI5*(RCS-RCPD*(1.0_JPRB-ZDPSFI))

    ZFHPCL5(JLON,JLEV)= - RLVZER * PFCCQL5(JLON,JLEV)
    ZFHPCN5(JLON,JLEV)= - RLSZER * PFCCQN5(JLON,JLEV)
    ZFHPSL5(JLON,JLEV)= - RLVZER * PFCSQL5(JLON,JLEV)
    ZFHPSN5(JLON,JLEV)= - RLSZER * PFCSQN5(JLON,JLEV)

    ZFP5(JLON,JLEV)= PFPLCL5(JLON,JLEV)+PFPLCN5(JLON,JLEV)&
     & + PFPLSL5(JLON,JLEV)+PFPLSN5(JLON,JLEV)  
    ZFEPFP5(JLON,JLEV)= - ZDPSFI * ZCPI5 * ZTI5 * ZFP5(JLON,JLEV)
    ZFHP5(JLON,JLEV) = ZFHSCL5(JLON,JLEV) + ZFHSCN5(JLON,JLEV)&
     & + ZFHSSL5(JLON,JLEV) + ZFHSSN5(JLON,JLEV) &
     & + ZFHPCL5(JLON,JLEV) + ZFHPCN5(JLON,JLEV) &
     & + ZFHPSL5(JLON,JLEV) + ZFHPSN5(JLON,JLEV) &
     & + ZFEPFP5(JLON,JLEV)  
  ENDDO
ENDDO

! --- SURFACE CALCULATIONS.
!     ----------------------

DO JLON=KSTART,KPROF
  ZFHSCL5(JLON,KFLEV)=PFPLCL5(JLON,KFLEV)*PTS(JLON)*(RCW-RCPD*(1.0_JPRB-ZDPSFI))
  ZFHSCN5(JLON,KFLEV)=PFPLCN5(JLON,KFLEV)*PTS(JLON)*(RCS-RCPD*(1.0_JPRB-ZDPSFI))
  ZFHSSL5(JLON,KFLEV)=PFPLSL5(JLON,KFLEV)*PTS(JLON)*(RCW-RCPD*(1.0_JPRB-ZDPSFI))
  ZFHSSN5(JLON,KFLEV)=PFPLSN5(JLON,KFLEV)*PTS(JLON)*(RCS-RCPD*(1.0_JPRB-ZDPSFI))

  ZFHPCL5(JLON,KFLEV)= - RLVZER * PFCCQL5(JLON,KFLEV)
  ZFHPCN5(JLON,KFLEV)= - RLSZER * PFCCQN5(JLON,KFLEV)
  ZFHPSL5(JLON,KFLEV)= - RLVZER * PFCSQL5(JLON,KFLEV)
  ZFHPSN5(JLON,KFLEV)= - RLSZER * PFCSQN5(JLON,KFLEV)

  ZFP5(JLON,KFLEV)= PFPLCL5(JLON,KFLEV)+PFPLCN5(JLON,KFLEV)&
   & + PFPLSL5(JLON,KFLEV)+PFPLSN5(JLON,KFLEV)  
  ZFEPFP5(JLON,KFLEV)= - ZDPSFI * ZCPS(JLON) * PTS(JLON) * ZFP5(JLON,KFLEV)
  ZFHP5(JLON,KFLEV) = ZFHSCL5(JLON,KFLEV) + ZFHSCN5(JLON,KFLEV)&
   & + ZFHSSL5(JLON,KFLEV) + ZFHSSN5(JLON,KFLEV) &
   & + ZFHPCL5(JLON,KFLEV) + ZFHPCN5(JLON,KFLEV) &
   & + ZFHPSL5(JLON,KFLEV) + ZFHPSN5(JLON,KFLEV) &
   & + ZFEPFP5(JLON,KFLEV)  
ENDDO

!*
!     ---------------------------------------------------------------
!     IV - CALCULATION OF THE EVOLUTION Q AND dU/dT AND dH/dt
!          (WITH H = CpT + Phi + Ec )

DO JLEV = 1, KFLEV
  DO JROF = KSTART, KPROF

    ZGSDP5(JROF,JLEV)=RG*PRDELP5(JROF,JLEV)

    PTENDU5(JROF,JLEV)=ZGSDP5(JROF,JLEV)&
     & *((PSTRCU5(JROF,JLEV-1) - PSTRCU5(JROF,JLEV)) &
     & + (PSTRDU5(JROF,JLEV-1) - PSTRDU5(JROF,JLEV)) &
     & + (PSTRMU5(JROF,JLEV-1) - PSTRMU5(JROF,JLEV)) &
     & + (PSTRTU5(JROF,JLEV-1) - PSTRTU5(JROF,JLEV)))  

    PTENDV5(JROF,JLEV)=ZGSDP5(JROF,JLEV)&
     & *((PSTRCV5(JROF,JLEV-1) - PSTRCV5(JROF,JLEV)) &
     & + (PSTRDV5(JROF,JLEV-1) - PSTRDV5(JROF,JLEV)) &
     & + (PSTRMV5(JROF,JLEV-1) - PSTRMV5(JROF,JLEV)) &
     & + (PSTRTV5(JROF,JLEV-1) - PSTRTV5(JROF,JLEV)))  

    PTENDQ5(JROF,JLEV)=ZGSDP5(JROF,JLEV)&
     & *((PDIFTQ5(JROF,JLEV-1) - PDIFTQ5(JROF,JLEV)) &
     & + (PDIFCQ5(JROF,JLEV-1) - PDIFCQ5(JROF,JLEV)) &
     & + (PFCCQL5(JROF,JLEV-1) - PFCCQL5(JROF,JLEV)) &
     & + (PFCCQN5(JROF,JLEV-1) - PFCCQN5(JROF,JLEV)) &
     & + (PFCSQL5(JROF,JLEV-1) - PFCSQL5(JROF,JLEV)) &
     & + (PFCSQN5(JROF,JLEV-1) - PFCSQN5(JROF,JLEV)))  

    PTENDH5(JROF,JLEV)=ZGSDP5(JROF,JLEV)&
     & *((PFRSO5 (JROF,JLEV-1) - PFRSO5 (JROF,JLEV)) &
     & + (PFRTH5 (JROF,JLEV-1) - PFRTH5 (JROF,JLEV)) &
     & + (PFRMH5 (JROF,JLEV-1) - PFRMH5 (JROF,JLEV)) &
     & + (PDIFCS5(JROF,JLEV-1) - PDIFCS5(JROF,JLEV)) &
     & + (PDIFTS5(JROF,JLEV-1) - PDIFTS5(JROF,JLEV)) &
     & + (ZFHP5  (JROF,JLEV-1) - ZFHP5  (JROF,JLEV)))  

  ENDDO
ENDDO

!*
!------------------------------------------------------------------------
!     V - EVENTUAL TRANSPORT OF DRY STATIC ENERGY, THE QUANTITY OF
!         MOVEMENT BY RAIN 

IF ( NDPSFI == 1 ) THEN

! - CALCULATION OF THE ENTHALPY.
!-------------------------------

  DO JLEV = 1, KFLEV
    DO JROF = KSTART, KPROF
      ZH5(JROF,JLEV) = ZCP5(JROF,JLEV)*PT5(JROF,JLEV)&
       & + PAPHIF5(JROF,JLEV)+0.5_JPRB*(PU5(JROF,JLEV)*PU5(JROF,JLEV)&
       & + PV5(JROF,JLEV)*PV5(JROF,JLEV))  

      PTENDQ5(JROF,JLEV)=PTENDQ5(JROF,JLEV)&
       & -ZGSDP5 (JROF,JLEV)*PQ5 (JROF,JLEV)&
       & *(ZFP5 (JROF,JLEV-1) - ZFP5 (JROF,JLEV))  

      ZGSDP5(JROF,JLEV)= - 0.5_JPRB * ZGSDP5(JROF,JLEV)
    ENDDO
  ENDDO

! - COMPUTATION AT THE TOP.
!--------------------------

  DO JROF = KSTART, KPROF
    ZFPDP5 = ZGSDP5(JROF,1) * ZFP5(JROF,1)

    PTENDU5(JROF,1)=PTENDU5(JROF,1)+ZFPDP5*(PU5(JROF,2)-PU5(JROF,1))
    PTENDV5(JROF,1)=PTENDV5(JROF,1)+ZFPDP5*(PV5(JROF,2)-PV5(JROF,1))
    PTENDH5(JROF,1)=PTENDH5(JROF,1)+ZFPDP5*(ZH5(JROF,2)-ZH5(JROF,1))
  ENDDO

! - COMPUTATION IN THE MIDDLE.
!------------------------------

  DO JLEV = 2, KFLEV-1
    DO JROF = KSTART, KPROF

      PTENDU5(JROF,JLEV) = PTENDU5(JROF,JLEV)+ZGSDP5(JROF,JLEV)&
       & *(ZFP5(JROF,JLEV-1)*(PU5(JROF,JLEV)-PU5(JROF,JLEV-1)) &
       & +ZFP5(JROF,JLEV)*(PU5(JROF,JLEV+1)-PU5(JROF,JLEV)))  
      PTENDV5(JROF,JLEV) = PTENDV5(JROF,JLEV)+ZGSDP5(JROF,JLEV)&
       & *(ZFP5(JROF,JLEV-1)*(PV5(JROF,JLEV)-PV5(JROF,JLEV-1)) &
       & +ZFP5(JROF,JLEV)*(PV5(JROF,JLEV+1)-PV5(JROF,JLEV)))  
      PTENDH5(JROF,JLEV) = PTENDH5(JROF,JLEV)+ZGSDP5(JROF,JLEV)&
       & *(ZFP5(JROF,JLEV-1)*(ZH5(JROF,JLEV)-ZH5(JROF,JLEV-1)) &
       & +ZFP5(JROF,JLEV)*(ZH5(JROF,JLEV+1)-ZH5(JROF,JLEV)))  

    ENDDO
  ENDDO

! - CONDITION AT THE BOTTOM: OROGRAPHY.
!--------------------------------------
  DO JROF = KSTART, KPROF

    PTENDU5(JROF,KFLEV) = PTENDU5(JROF,KFLEV)+ZGSDP5(JROF,KFLEV)&
     & *(ZFP5(JROF,KFLEV-1)*(PU5(JROF,KFLEV)-PU5(JROF,KFLEV-1))&
     & -2.0_JPRB*ZFP5(JROF,KFLEV)*PU5(JROF,KFLEV))  
    PTENDV5(JROF,KFLEV) = PTENDV5(JROF,KFLEV)+ZGSDP5(JROF,KFLEV)&
     & *(ZFP5(JROF,KFLEV-1)*(PV5(JROF,KFLEV)-PV5(JROF,KFLEV-1))&
     & -2.0_JPRB*ZFP5(JROF,KFLEV)*PV5(JROF,KFLEV))  
    PTENDH5(JROF,KFLEV) = PTENDH5(JROF,KFLEV)+ZGSDP5(JROF,KFLEV)&
     & *(ZFP5(JROF,KFLEV-1)*(ZH5(JROF,KFLEV)-ZH5(JROF,KFLEV-1)) &
     & + 2.0_JPRB*ZFP5(JROF,KFLEV)*((RCPD + ZCVMCD*PQS(JROF)) &
     & * PTS(JROF) + POROG(JROF)-ZH5(JROF,KFLEV)))  
  ENDDO

ENDIF

!------------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('CPTENDSMAT',1,ZHOOK_HANDLE)
END SUBROUTINE CPTENDSMAT

