SUBROUTINE CPUTQYSTL ( KPROMA, KSTART, KPROF, KFLEV, PDT,&
 !  Variables 2D Input
 & PTENDH5, PTENDQ5, PTENDU5, PTENDV5,&
 & PCP95, PDELP95, PTT95, PUT95, PVT95,&
 & PTENDH, PTENDQ, PTENDU, PTENDV,&
 & PCP9, PDELP9, PTT9, PUT9, PVT9,&
 !  Variables 2D In/Out
 & PFDIS,PTT1, PQT1, PUT1, PVT1 )  

!     ------------------------------------------------------
!**** *CPUTQYSTL*  TANGENT LINEAR OF EVOLUTION OF T, WIND, Q 
!     ------------------------------------------------------

!     ARGUMENTS FOR INPUT
!     --------------------
!       KPROMA : HORIZONTAL DIMENSION.
!       KSTART : START OF HORIZONTAL LOOP.
!       KPROF : END OF HORIZONTAL LOOP.
!       KFLEV : VERTICAL DIMENSION (FULL LEVELS).
!       PDT : PAS DE TEMPS EFFECTIF

! - 2D (KFLEV)
!   ----------
!       PTENDH  : TENDENCY OF ENTHALPY
!       PTENDQ  : TENDENCY OF HUMIDITY
!       PTENDU  : TENDENCY OF U COMPONENT OF WIND
!       PTENDV  : TENDENCY OF V COMPONENT OF WIND
!       PCP9    : SPECIFIC HEAT AT CONST.PRESSURE FOR AIR AT T=9
!       PDELP9  : LAYER THICKNESS IN PRESSURE UNITS AT T=9
!       PTT9    : TEMPERATURE AT T=9
!       PUT9    : X-COMPONENT OF WIND AT T=9
!       PVT9    : Y-COMPONENT OF WIND AT T=9

!   The same but for trajectory
!   ---------------------------
!       PTENDH5 : TENDENCY OF ENTHALPY
!       PTENDQ5 : TENDENCY OF HUMIDITY
!       PTENDU5 : TENDENCY OF U COMPONENT OF WIND
!       PTENDV5 : TENDENCY OF V COMPONENT OF WIND
!       PCP95   : SPECIFIC HEAT AT CONST.PRESSURE FOR AIR AT T=9
!       PDELP95 : LAYER THICKNESS IN PRESSURE UNITS AT T=9
!       PTT95   : TEMPERATURE AT T=9
!       PUT95   : X-COMPONENT OF WIND AT T=9
!       PVT95   : Y-COMPONENT OF WIND AT T=9
!     IMPLICITE ARGUMENTS
!     --------------------
!       UNIVERSAL CONSTANTS  = COMMON /YOMCST/:  RG,RCPD,RCPV,RCS,RCW

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

!  - 2D (KFLEV)

!       PTT1  : TEMPERATURE AT T=1
!       PQT1  : SPECIFIC HUMIDITY AT T=1
!       PUT1  : X-COMPONENT OF WIND AT T=1 
!       PVT1  : Y-COMPONENT OF WIND AT T=1
!       PFDIS : FLUX ENTHALPIE DU A LA DISSIPATION EC

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

!   Author
!   ------
!         95-12-05, M. Janiskova

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

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

USE YOMCST   , ONLY : RG       ,RCPD     ,RCPV

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

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)    :: PDT 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDH5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDQ5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDU5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDV5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCP95(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDELP95(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTT95(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUT95(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVT95(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDH(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDQ(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDU(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDV(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCP9(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDELP9(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTT9(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUT9(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVT9(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFDIS(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTT1(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQT1(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUT1(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVT1(KPROMA,KFLEV) 

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

INTEGER(KIND=JPIM) :: JLEV, JROF

REAL(KIND=JPRB) :: ZCPF, ZCPF5, ZDEC, ZDEC5, ZFLAG, ZH9, ZH95,&
 & ZRDT, ZRRG, ZTDCP, ZTDCP5  
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!  ------------------------------------------------------------
!   1. CALCULATION OF EVOLUTION OF WIND, Q AND TEMPERATURE
!  ------------------------------------------------------------

IF (PDT <= 0.0_JPRB) THEN
  ZFLAG=0.0_JPRB
  ZRDT= 0.0_JPRB
ELSE
  ZFLAG=1.0_JPRB
  ZRDT= 1.0_JPRB/PDT
ENDIF
ZRRG   = 1.0_JPRB/RG

DO JROF=KSTART,KPROF
  PFDIS(JROF,0)=0.0_JPRB
ENDDO

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

    PUT1(JROF,JLEV)=PUT1(JROF,JLEV)+PDT*PTENDU(JROF,JLEV)

    PVT1(JROF,JLEV) = PVT1(JROF,JLEV) + PDT * PTENDV(JROF,JLEV)

    PQT1(JROF,JLEV) = PQT1(JROF,JLEV) + PDT * PTENDQ(JROF,JLEV)

    ZTDCP5=(RCPV - RCPD)*PTENDQ5(JROF,JLEV)
    ZTDCP =(RCPV - RCPD)*PTENDQ(JROF,JLEV)

    ZCPF5=1.0_JPRB/(PCP95(JROF,JLEV)+PDT*ZTDCP5)
    ZCPF=-PCP9(JROF,JLEV)/(PCP95(JROF,JLEV)+PDT*ZTDCP5)**2 &
     & -PDT*ZTDCP/(PCP95(JROF,JLEV)+PDT*ZTDCP5)**2  

    ZDEC5=PDT*(PUT95(JROF,JLEV)*PTENDU5(JROF,JLEV)&
     & +PVT95(JROF,JLEV)*PTENDV5(JROF,JLEV)+0.5_JPRB*PDT &
     & *((PTENDU5(JROF,JLEV))**2+(PTENDV5(JROF,JLEV))**2))  
    ZDEC=(PDT*PUT95(JROF,JLEV)+PDT**2*PTENDU5(JROF,JLEV))&
     & *PTENDU(JROF,JLEV)&
     & +PDT*PTENDU5(JROF,JLEV)*PUT9(JROF,JLEV)&
     & +(PDT*PVT95(JROF,JLEV)+PDT**2*PTENDV5(JROF,JLEV))&
     & *PTENDV(JROF,JLEV)&
     & +PDT*PTENDV5(JROF,JLEV)*PVT9(JROF,JLEV)  

    ZH95=PCP95(JROF,JLEV)*PTT95(JROF,JLEV)
    ZH9=PCP95(JROF,JLEV)*PTT9(JROF,JLEV)+PTT95(JROF,JLEV)*PCP9(JROF,JLEV)

    PTT1(JROF,JLEV)=PTT1(JROF,JLEV)&
     & +ZFLAG*(ZH95-ZDEC5+PDT*PTENDH5(JROF,JLEV))*ZCPF &
     & +ZFLAG*ZCPF5*ZH9 &
     & -ZFLAG*ZCPF5*ZDEC &
     & +ZFLAG*PDT*ZCPF5*PTENDH(JROF,JLEV)&
     & -ZFLAG*PTT9(JROF,JLEV)  

    PFDIS(JROF,JLEV)=PFDIS(JROF,JLEV-1)&
     & +ZRDT*ZRRG*PDELP95(JROF,JLEV)*ZDEC &
     & +ZRDT*ZRRG*ZDEC5*PDELP9(JROF,JLEV)  
  ENDDO
ENDDO

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

