!  Differentiation of scatra_gems in forward (tangent) mode:
!   variations  of output variables: pudiftt pudirtt pddiftt psphalbt
!                pddirtt
!   with respect to input variables: pizmoy pbetal ptodaer ptamoy

SUBROUTINE SCATRA_GEMS_TL(ptamoy, ptamoyd, ptray, ptodaer, ptodaerd, &
&  ptodray, pizmoy, pizmoyd, prm, pgb, pxmus, pxmuv, pbetal, pbetald, &
&  ppsl, pddirtt, pddirttd, pddiftt, pddifttd, pudirtt, pudirttd, &
&  pudiftt, pudifttd, psphalbt, psphalbtd)

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

USE rt6svalues

IMPLICIT NONE

!****SCATRA*** COMPUTES SCATTERING TRANSMISSION FUNCTIONS FOR THE THREE ATMOPHERIC MODELS,
!              RAYLEIGH, AEROSOL AND MIXTURE


!     INTERFACE
!     ---------
! SCATRA IS CALLED BY DISCOM

!     INPUTS
!     ------

! PXMUS                     : COSINE OF SOLAR ZENITH ANGLE
! PXMUV                     : COSINE OF VIEWING ZENITH ANGLE
! PRM(-KMU:KMU)             : GAUSSIAN ANGLES FOR THE ZENITH
! PGB(-KMU:KMU)             : GAUSSIAN WEIGHTS FOR THE ZENITH
! PIZMOY(KNT)               : TRUNCATED AEROSOL SINGLE SCATTERING ALBEDO PROFILE.
! PTODAER                   : TOTAL AEROSOL OPTICAL DEPTH
! PTODRAY                   : TOTAL MOLECULAR OPTICAL DEPTH
! PTAMOY(0:KNT)             : TRUNCATED AEROSOL OPTICAL THICKNESS PROFILE.
! PTRAY(0:KNT)              : MOLECULAR OPTICAL THICKNESS PROFILE.
! PBETAL(KNT,KNANG+1) : COEFFICIENTS OF LEGENDRE POLYNOM
! PPSL(0:KNANG,-1:KNANG,-KMU:KMU)

!     OUTPUTS
!     -------

! PDDIRTT : TOTAL DIRECT DOWNWARD TRANSMITTANCE
! PDDIFTT : TOTAL DIFUSE DOWNWARD TRANSMITTANCE
! PUDIRTT : TOTAL DIRECT UPWARD TRANSMITTANCE
! PUDIFTT : TOTAL DIFUSE UPWARD TRANSMITTANCE
! PSPHALBT : TOTAL SPHERICAL ALBEDO

!     EXTERNALS.
!     ----------
!          ISO & CSALBR

!     MODIFICATIONS.
!     -------------
!          N.HUNEEUS 02.05.2007

!     SOURCE
!     ------
! ADAPTED FROM SCATRA.f FROM ORIGINAL 6S CODE, SEE VERMOTTE ET AL. (1997)

!     MODEL PARAMETERS
!     ----------------
!--------------------------------------------------------------------------------------
  REAL(KIND=JPRB), INTENT(IN) :: pxmus, pxmuv
  REAL(KIND=JPRB), INTENT(IN) :: ptodaer, ptodray
  REAL(KIND=JPRB), INTENT(IN) :: ptodaerd
  REAL(KIND=JPRB), INTENT(IN) :: ptamoy(0:knt), ptray(0:knt)
  REAL(KIND=JPRB), INTENT(IN) :: ptamoyd(0:knt)
  REAL(KIND=JPRB), INTENT(IN) :: pgb(-kmu:kmu)
  REAL(KIND=JPRB), INTENT(IN) :: pizmoy(0:knt)
  REAL(KIND=JPRB), INTENT(IN) :: pizmoyd(0:knt)
  REAL(KIND=JPRB), INTENT(INOUT) :: prm(-kmu:kmu)
  REAL(KIND=JPRB), INTENT(IN) :: pbetal(0:knt, 0:knang)
  REAL(KIND=JPRB), INTENT(IN) :: pbetald(0:knt, 0:knang)
  REAL(KIND=JPRB), INTENT(IN) :: ppsl(0:knang, -1:knang, -kmu:kmu)
  REAL(KIND=JPRB), INTENT(OUT) :: pddirtt
  REAL(KIND=JPRB), INTENT(OUT) :: pddirttd
  REAL(KIND=JPRB), INTENT(OUT) :: pddiftt
  REAL(KIND=JPRB), INTENT(OUT) :: pddifttd
  REAL(KIND=JPRB), INTENT(OUT) :: pudirtt
  REAL(KIND=JPRB), INTENT(OUT) :: pudirttd
  REAL(KIND=JPRB), INTENT(OUT) :: pudiftt
  REAL(KIND=JPRB), INTENT(OUT) :: pudifttd
  REAL(KIND=JPRB), INTENT(OUT) :: psphalbt
  REAL(KIND=JPRB), INTENT(OUT) :: psphalbtd
! LOCAL VARIABLES
! ---------------
!INTEGER(KIND=JPIM),PARAMETER  :: KMU=25, KMU2=48, KNP=49, KNANG=80, KNBMU=83, KNT=20
  REAL(KIND=JPRB) :: z_prmd(-kmu:kmu)
  REAL(KIND=JPRB) :: z_ptrans(-1:1)
  REAL(KIND=JPRB) :: z_ptransd(-1:1)
  INTRINSIC EXP

  REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "iso_gems_tl.intfb.h"

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

! INITIALIZING VARIABLES
! ----------------------
  pddirtt = 1.0_JPRB
  pddiftt = 0.0_JPRB
  pudirtt = 1.0_JPRB
  pudiftt = 0.0_JPRB
  psphalbt = 0.0_JPRB
! UPWARD TRANSMITTANCE
  z_prmd(-kmu) = 0._JPRB
  prm(-kmu) = -pxmuv
  z_prmd(kmu) = 0._JPRB
  prm(kmu) = pxmuv
  z_prmd(0) = 0._JPRB
  prm(0) = pxmus
  CALL ISO_GEMS_TL(ptamoy, ptamoyd, ptray, pizmoy, pizmoyd, pbetal, &
&            pbetald, prm, pgb, ppsl, ptodaer, ptodaerd, ptodray, z_ptrans, &
&            z_ptransd)
  pudirttd = -(ptodaerd*EXP(-((ptodaer+ptodray)/pxmuv))/pxmuv)
  pudirtt = EXP(-((ptodaer+ptodray)/pxmuv))
  pudifttd = z_ptransd(-1) - pudirttd
  pudiftt = z_ptrans(-1) - pudirtt
! DOWNWARD TRANSMITTANCE 
  z_prmd(-kmu) = 0._JPRB
  prm(-kmu) = -pxmus
  z_prmd(kmu) = 0._JPRB
  prm(kmu) = pxmus
  z_prmd(0) = 0._JPRB
  prm(0) = pxmus
  CALL ISO_GEMS_TL(ptamoy, ptamoyd, ptray, pizmoy, pizmoyd, pbetal, &
&            pbetald, prm, pgb, ppsl, ptodaer, ptodaerd, ptodray, z_ptrans, &
&            z_ptransd)
  pddirttd = -(ptodaerd*EXP(-((ptodaer+ptodray)/pxmus))/pxmus)
  pddirtt = EXP(-((ptodaer+ptodray)/pxmus))
  pddifttd = z_ptransd(1) - pddirttd
  pddiftt = z_ptrans(1) - pddirtt
  psphalbtd = 2.0_JPRB*z_ptransd(0)
  psphalbt = z_ptrans(0)*2.0_JPRB

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