SUBROUTINE GPMKTEND(KST,KEND,PTSPHY,&
 & PUT1,PVT1 ,PTT1,&
 & PGFLT1,&
 & PUT9,PVT9 ,PTT9,&
 & PGFL,&
 & PL,PI,&
 & PR,PS)  

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

USE YOMDIM   , ONLY : NFLEVG   ,NPROMA
USE YOM_YGFL , ONLY : YGFL     ,YGFLC, YL, YI, YR, YS
USE YOETHF   , ONLY : R2ES     ,R3LES    ,R3IES    ,R4LES    ,&
 & R4IES    ,R5LES    ,R5IES    ,R5ALVCP  ,R5ALSCP  ,&
 & RALVDCP  ,RALSDCP  ,RTWAT    ,RTICE    ,RTICECU  ,&
 & RTWAT_RTICE_R      ,RTWAT_RTICECU_R
USE YOMCST   , ONLY : RLVTT    ,RLSTT    ,RTT
USE YOMSLPHY , ONLY : LSLPHY

!**** *GPMKTEND - Convert t+dt values to tendencies 

!     Purpose. Convert t+dt values to tendencies before calling
!     -------- ECMWF physics

!**   Interface.
!     ----------
!        *CALL* *GPMKTEND(...)*

!        Explicit arguments : see CPGLAG, all names the same
!        -------------------- except for DOCTOR norm modifications

!        Implicit arguments :
!        --------------------

!     Method.
!     -------
!        See documentation

!     Externals.
!     ----------

!     Reference.
!     ----------
!        None

!     Author.
!     -------
!        Mats Hamrud  *ECMWF*

!     Modifications.
!     --------------
!      Original : 95-03-02 (From CPGLAG)
!      Modified 96-03 by A. Untch : grid-point prognostic ozone (EC)
!      Modified 98-01-09 by E. Holm : Introduced spectral ozone
!      M.Hamrud      01-Oct-2003 CY28 Cleaning
!      K. Yessad and D. Salmond (Feb 2006): adapt to LPC_FULL.
!      N. Wedi and D. Salmond 06-05-01 : liquid water and ice split moved from callpar
!      K. Yessad (Dec 2008): remove dummy CDLOCK
!      A. Tompkins 2007-15-06 : remove liquid water and ice split 
!     ------------------------------------------------------------------

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KEND 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSPHY 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUT1(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVT1(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTT1(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGFLT1(NPROMA,NFLEVG,YGFL%NDIM1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUT9(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVT9(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTT9(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGFL(NPROMA,NFLEVG,YGFL%NDIM) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PL(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PI(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PR(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PS(NPROMA,NFLEVG) 

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

INTEGER(KIND=JPIM) :: JLEV, JROF, JGFL
REAL(KIND=JPRB) :: ZTS_R
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "fcttre.h"

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

IF (LHOOK) CALL DR_HOOK('GPMKTEND',0,ZHOOK_HANDLE)

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

ZTS_R=1.0_JPRB/PTSPHY
DO JLEV=1,NFLEVG
  DO JROF=KST,KEND
    PUT1(JROF,JLEV)=(PUT1(JROF,JLEV)-PUT9(JROF,JLEV))*ZTS_R
    PVT1(JROF,JLEV)=(PVT1(JROF,JLEV)-PVT9(JROF,JLEV))*ZTS_R
    PTT1(JROF,JLEV)=(PTT1(JROF,JLEV)-PTT9(JROF,JLEV))*ZTS_R
  ENDDO
ENDDO

DO JGFL=1,YGFL%NUMFLDS
  IF(YGFLC(JGFL)%LT1) THEN
    DO JLEV=1,NFLEVG
      DO JROF=KST,KEND
        PGFLT1(JROF,JLEV,YGFLC(JGFL)%MP1)=(PGFLT1(JROF,JLEV,YGFLC(JGFL)%MP1)-&
         & PGFL(JROF,JLEV,YGFLC(JGFL)%MP9_PH))*ZTS_R  
      ENDDO
    ENDDO
  ENDIF
ENDDO

!-----------------------------------------------------------------------
! Overwrite PGFLT tendencies with calculation using linear interpolation
! Done for cloud variables only - would be much better to implement an
! attribute to demark cloud variables in GFL that are treated specially
! in the semi-Lagrangian advection scheme
!-----------------------------------------------------------------------
IF ( LSLPHY ) THEN
  DO JLEV=1,NFLEVG
    DO JROF=KST,KEND
      IF (YL%LT1) PGFLT1(JROF,JLEV,YL%MP1) = (PL(JROF,JLEV)-PGFL(JROF,JLEV,YL%MP9_PH))*ZTS_R
      IF (YI%LT1) PGFLT1(JROF,JLEV,YI%MP1) = (PI(JROF,JLEV)-PGFL(JROF,JLEV,YI%MP9_PH))*ZTS_R
      IF (YR%LT1) PGFLT1(JROF,JLEV,YR%MP1) = (PR(JROF,JLEV)-PGFL(JROF,JLEV,YR%MP9_PH))*ZTS_R
      IF (YS%LT1) PGFLT1(JROF,JLEV,YS%MP1) = (PS(JROF,JLEV)-PGFL(JROF,JLEV,YS%MP9_PH))*ZTS_R
    ENDDO
  ENDDO
ENDIF

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

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