SUBROUTINE SCATRA_GEMS(PTAMOY,PTRAY,PTODAER,PTODRAY,PIZMOY,PRM,PGB,PXMUS,PXMUV,PBETAL,PPSL,  &
&                                                     PDDIRTT,PDDIFTT,PUDIRTT,PUDIFTT,PSPHALBT)

!****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
!          A.BENEDETTI - ECMWF 06.08.2007

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

!     MODEL PARAMETERS
!     ----------------
!--------------------------------------------------------------------------------------

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

USE RT6SVALUES

IMPLICIT NONE

REAL(KIND=JPRB), INTENT(IN) :: PXMUS, PXMUV
REAL(KIND=JPRB), INTENT(IN) :: PTODAER, PTODRAY
REAL(KIND=JPRB), INTENT(IN) :: PTAMOY(0:KNT), PTRAY(0:KNT)
REAL(KIND=JPRB), INTENT(IN) :: PGB(-KMU:KMU)
REAL(KIND=JPRB), INTENT(IN) :: PIZMOY(0:KNT)
REAL(KIND=JPRB), INTENT(IN) :: PBETAL(0:KNT,0:KNANG)
REAL(KIND=JPRB), INTENT(IN) :: PPSL(0:KNANG,-1:KNANG,-KMU:KMU)
REAL(KIND=JPRB), INTENT(INOUT) :: PRM(-KMU:KMU)
REAL(KIND=JPRB), INTENT(OUT) :: PDDIRTT
REAL(KIND=JPRB), INTENT(OUT) :: PDDIFTT
REAL(KIND=JPRB), INTENT(OUT) :: PUDIRTT
REAL(KIND=JPRB), INTENT(OUT) :: PUDIFTT
REAL(KIND=JPRB), INTENT(OUT) :: PSPHALBT

! LOCAL VARIABLES
! ---------------

!INTEGER(KIND=JPIM),PARAMETER  :: KMU=25, KMU2=48, KNP=49, KNANG=80, KNBMU=83, KNT=20

REAL(KIND=JPRB) :: z_PTRANS(-1:1)

REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "iso_gems.intfb.h"

IF (LHOOK) CALL DR_HOOK('SCATRA_GEMS',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
PRM(-KMU)=-PXMUV
PRM(KMU)=PXMUV
PRM(0)=PXMUS
CALL ISO_GEMS(PTAMOY,PTRAY,PIZMOY,PBETAL,PRM,PGB,PPSL,PTODAER,PTODRAY,z_PTRANS)

PUDIRTT=EXP(-(PTODAER+PTODRAY)/PXMUV)
PUDIFTT=z_PTRANS(-1)-PUDIRTT
! DOWNWARD TRANSMITTANCE 
PRM(-KMU)=-PXMUS
PRM(KMU)=PXMUS
PRM(0)=PXMUS
CALL ISO_GEMS(PTAMOY,PTRAY,PIZMOY,PBETAL,PRM,PGB,PPSL,PTODAER,PTODRAY,z_PTRANS)

PDDIRTT=EXP(-(PTODAER+PTODRAY)/PXMUS)
PDDIFTT=z_PTRANS(1)-PDDIRTT
PSPHALBT=z_PTRANS(0)*2.0_JPRB
  
IF (LHOOK) CALL DR_HOOK('SCATRA_GEMS',1,ZHOOK_HANDLE)
END SUBROUTINE SCATRA_GEMS
