 SUBROUTINE GRG_TENDCTM &
 &    (LDSTART, KIDIA  , KFDIA  , KLON  , KLEV , KTRAC, KGRG,  &
 &     PTSTEP ,PDELP,  PRS1, PRSF1, PTP, PTENDGRG, PCEN ,PTENC , & 
 &     PTENGFL )

!**  GRG_TENDCTM - converts CTM tendecy information to GRG
!**   INTERFACE.
!     ----------
!          *GRG_TENDCTM* IS CALLED FROM *CALLPAR*.

! INPUTS:
! -------
! PTENDGRG (KLON,KLEV,NGRG)   : CTM TENDENCY RATE  OF GRG TRACERS in kg/kg/s 
! PCEN    (KLON,KLEV,KTRAC)    : CONCENTRATION OF TRACERS           (kg/kg)
! PTENC    (KLON,KLEV,KTRAC)   : TOTAL TENDENCY OF CONCENTRATION OF TRACERS BEFORE(kg/kg s-1)
! PDELP(KLON,KLEV)             : PRESSURE DELTA in PRESSURE UNITES      (Pa)
! PRS1 (KLON,0:KLEV)  : HALF-LEVEL PRESSURE           (Pa)
! PRSF1(KLON,KLEV)             : FULL-LEVEL PRESSURE           (Pa)
! PTP(KLON,KLEV)               : FULL-LEVEL TEMPERATURE (W. DYN.TEND.) (K)
! PTSTEP                       : TIME-STEP LENGTH                   (s)

! OUTPUTS
! -------
! PTENC  (KLON,KLEV,KTRAC)     : TENDENCY OF CONCENTRATION OF TRACERS (kg/kg s-1)
! PTCGRG  (KLON,KTRAC)         : TOTAL COLUMNS OF CTM TRACERS (kg/m2)
!     AUTHOR.
!     -------
!        JOHANNES FLEMMING  *ECMWF*

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 2005-12-10

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

USE YOM_YGFL , ONLY : NGRG, NGRG_CPLO4, YGRG, YGFL 
USE YOMCOUPLO4, ONLY : LCOUPLO4_CTM

IMPLICIT NONE

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

!*       0.1  ARGUMENTS
!             ---------

INTEGER(KIND=JPIM),INTENT(IN) :: KIDIA , KFDIA , KLON , KLEV
INTEGER(KIND=JPIM),INTENT(IN) :: KTRAC
INTEGER(KIND=JPIM),INTENT(IN)  :: KGRG(NGRG)

REAL(KIND=JPRB),INTENT(IN)    :: PTSTEP
REAL(KIND=JPRB),INTENT(IN)    :: PDELP(KLON,KLEV)
REAL(KIND=JPRB),INTENT(IN)    :: PRSF1(KLON,KLEV)
REAL(KIND=JPRB),INTENT(IN)    :: PRS1(KLON,0:KLEV)
REAL(KIND=JPRB),INTENT(IN)    :: PTP(KLON,KLEV)   
REAL(KIND=JPRB),INTENT(IN)    :: PTENDGRG(KLON,KLEV,NGRG_CPLO4)
REAL(KIND=JPRB),INTENT(INOUT) :: PTENC(KLON,KLEV,KTRAC)
REAL(KIND=JPRB),INTENT(IN)    :: PCEN(KLON,KLEV,KTRAC) 
REAL(KIND=JPRB),INTENT(IN)    :: PTENGFL(KLON,KLEV,YGFL%NDIM1)

!*       0.5   LOCAL VARIABLES
!              ---------------

REAL(KIND=JPRB) :: ZTDECAY_CO, ZMIN, ZCENTEST
INTEGER(KIND=JPIM) :: JK, JL, JT, JT1
LOGICAL, INTENT(IN) :: LDSTART 
REAL(KIND=JPRB) :: ZHOOK_HANDLE


#include "abor1.intfb.h"

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

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

IF (.NOT. LCOUPLO4_CTM)  THEN
  IF (LHOOK) CALL DR_HOOK('GRG_TENDCTM',1,ZHOOK_HANDLE)
  RETURN
ENDIF

!*       1.     CALCULATE TENDECIES 
!              -------------------
!  currently we assume chemical production and loss only 
!  no rates,  meaning the concentrations itself do not matter


IF ( LCOUPLO4_CTM ) THEN 
 JT1=0 
 DO JT=1,NGRG
  IF (YGRG(JT)%NCOUPLO4 >= 1 ) THEN 
    JT1=JT1+1
! If Diffusion or Convection is already included in CTM tendency by reset to dynamicl tendency 
     IF (.NOT. YGRG(JT)%LDIFF) THEN
       IF (YGRG(JT)%LCONV) CALL ABOR1(" LDIFF and LCONV have to both false ")
        DO JK=1,KLEV
         DO JL=KIDIA,KFDIA
          PTENC(JL,JK,KGRG(JT)) = PTENGFL(JL,JK,YGRG(JT)%MP1)   
        ENDDO
      ENDDO 
    ENDIF

! add CTM tendency
    DO JK=1,KLEV
      DO JL=KIDIA,KFDIA
        PTENC(JL,JK,KGRG(JT))  =  PTENC(JL,JK,KGRG(JT)) + &
        &                  (PTENDGRG (JL,JK,JT1))   
      ENDDO
    ENDDO
  ELSE
! htap CO lifetime of 50 days for uncoupled GRG tracer 
   ZTDECAY_CO=(EXP(-1.0_JPRB*PTSTEP/(50.0_JPRB*86400.0_JPRB))-1.0_JPRB)/PTSTEP
    DO JK=1,KLEV
      DO JL=KIDIA,KFDIA
        PTENC(JL,JK,KGRG(JT)) =  PTENC(JL,JK,KGRG(JT)) + &
     &    PCEN(JL,JK,KGRG(JT))*ZTDECAY_CO  
      ENDDO
    ENDDO
  ENDIF
 ENDDO
ENDIF


! cut tendencies leading to negative values
ZMIN= 1.0E-15 
 DO JT=1,NGRG
   DO JK=1,KLEV
     DO JL=KIDIA,KFDIA
      ZCENTEST = PCEN(JL,JK,KGRG(JT))
      IF (( PTENC(JL,JK,KGRG(JT))*PTSTEP  + ZCENTEST )  < ZMIN ) THEN 
       PTENC(JL,JK,KGRG(JT)) =  (ZMIN - ZCENTEST ) / PTSTEP 
      ENDIF
     ENDDO
   ENDDO
 ENDDO

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

