!  Differentiation of atmref_gems in forward (tangent) mode:
!   variations  of output variables: promix
!   with respect to input variables: pizmoy pbetal ptamoy

SUBROUTINE ATMREF_GEMS_TL(ptamoy, ptamoyd, ptray, pizmoy, pizmoyd, phirad, &
&  pxmus, pxmuv, prm, pgb, prp, pbetal, pbetald, ppsl, ptodaer, &
&  ptodray, promix, promixd)

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

  USE rt6svalues, ONLY : kmu, knp, knt, knang

  IMPLICIT NONE

!****ATMREF*** COMPUTES AEROSOL, RAYLEIGH AND MIXTURE REFLECTANCE


!     INTERFACE
!     ---------
! ATMREF IS CALLED BY DISCOM

!     INPUTS
!     ------

! PHIRAD          : DIFFERENCE IN AZIMUTAL ANGLE [RAD]
! PXMUS           : COSINE OF SOLAR ZENITH ANGLE
! PXMUV           : COSINE OF VIEWING ZENITH ANGLE
! PRP(KNP)        : GAUSSIAN ANGLES FOR AZIMUTH
! PRM(-KMU:KMU) : GAUSSIAN ANGLES FOR THE ZENITH
! PGB(-KMU:KMU) : GAUSSIAN WEIGHTS FOR THE ZENITH
! PTAMOY(KNT)     : TRUNCATED AEROSOL OPTICAL THICKNESS PROFILE.
! PTRAY(KNT)      : MOLECULAR OPTICAL THICKNESS PROFILE.
! PIZMOY(KNT)     : TRUNCATED AEROSOL SINGLE SCATTERING ALBEDO PROFILE.
! PBETAL(KNT,KNANG+1) : COEFFICIENTS OF LEGENDRE POLYNOM
! PTODAER               : TOTAL AEROSOL OPTICAL THICKNESS
! PTODRAY               : TOTAL MOLECULAR OPTICAL THICKNESS

!     OUTPUTS
!     -------
! PROMIX  : ATMOSPHERIC REFLECTANCE OF MIXTURE

!     EXTERNALS.
!     ----------
!          OS

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

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

!     MODEL PARAMETERS
!     ----------------
!--------------------------------------------------------------------------------------
!INTEGER(KIND=JPIM),PARAMETER  :: KMU=25, KMU2=48, KNP=49, KNANG=80, KNBMU=83, KNT=20
  REAL(KIND=JPRB), INTENT(IN) :: pgb(-kmu:kmu), prp(knp)
  REAL(KIND=JPRB), INTENT(IN) :: phirad, pxmus, pxmuv
  REAL(KIND=JPRB), INTENT(IN) :: ppsl(0:knang, -1:knang, -kmu:kmu)
  REAL(KIND=JPRB), INTENT(IN) :: ptamoy(0:knt), ptray(0:knt), pizmoy(0:&
&  knt)
  REAL(KIND=JPRB), INTENT(IN) :: ptamoyd(0:knt), pizmoyd(0:knt)
  REAL(KIND=JPRB), INTENT(IN) :: pbetal(0:knt, 0:knang), ptodaer, &
&  ptodray
  REAL(KIND=JPRB), INTENT(IN) :: pbetald(0:knt, 0:knang)
  REAL(KIND=JPRB), INTENT(OUT) :: promix
  REAL(KIND=JPRB), INTENT(OUT) :: promixd
  REAL(KIND=JPRB), INTENT(INOUT) :: prm(-kmu:kmu)
! LOCAL VARIABLES
! ---------------
  REAL(KIND=JPRB) :: z_pxlm2(-kmu:kmu, knp)
  REAL(KIND=JPRB) :: z_pxlm2d(-kmu:kmu, knp)

  REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "os_gems_tl.intfb.h"

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

! MIXTURE REFLECTANCE

  CALL OS_GEMS_TL(ptamoy, ptamoyd, ptray, pizmoy, pizmoyd, phirad, pbetal, &
&           pbetald, prm, pgb, prp, ppsl, ptodaer, ptodray, z_pxlm2, &
&           z_pxlm2d)

  promixd = z_pxlm2d(kmu, 1)/pxmus
  promix = z_pxlm2(kmu, 1)/pxmus
  PRINT*, 'PROMIX,PXMUS = ', promix, pxmus

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