SUBROUTINE CPUTQYSAD ( 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 )  

!     ------------------------------------------------------
!**** *CPUTQYSAD*  ADJOINT 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-11, M. Janiskova
!   Modifications
!   -------------
!      99-01 - code optimization, M. Janiskova
!        M.Hamrud      01-Oct-2003 CY28 Cleaning

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

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(INOUT) :: PTENDH(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENDQ(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENDU(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENDV(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCP9(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDELP9(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTT9(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUT9(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVT9(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFDIS(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTT1(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQT1(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUT1(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVT1(KPROMA,KFLEV) 

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

REAL(KIND=JPRB) :: ZTDCP5(KPROMA,KFLEV), ZCPF5(KPROMA,KFLEV),&
 & ZDEC5 (KPROMA,KFLEV), ZH95 (KPROMA,KFLEV)  

INTEGER(KIND=JPIM) :: JLEV, JROF

REAL(KIND=JPRB) :: ZCPF, ZDEC, ZFLAG, ZH9, ZRDT, ZRRG, ZTDCP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!  ------------------------------------------------------------
!   0. 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 JLEV = 1, KFLEV
  DO JROF = KSTART, KPROF

    ZTDCP5(JROF,JLEV)=(RCPV - RCPD)*PTENDQ5(JROF,JLEV)
    ZCPF5(JROF,JLEV)=1.0_JPRB/(PCP95(JROF,JLEV)+PDT*ZTDCP5(JROF,JLEV))

    ZDEC5(JROF,JLEV)=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))  
    ZH95(JROF,JLEV)=PCP95(JROF,JLEV)*PTT95(JROF,JLEV)

  ENDDO
ENDDO

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

DO JLEV = KFLEV, 1, -1
  DO JROF = KSTART, KPROF
    ZTDCP=0.0_JPRB
    ZCPF=0.0_JPRB
    ZDEC=0.0_JPRB
    ZH9=0.0_JPRB

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

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

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

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

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

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

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

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

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

  ENDDO
ENDDO

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

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

