SUBROUTINE AER_OPT_PROP_AD &
 & ( KDLEN  , KLEN  , KMXCOUNT, & 
 &   PQF, PTF, PPRESH, PPRESF, &
 &   YDGEMSPROF, &  
 &   PAEREXT,PAEROM, PAERASY, &
 &   PQF5, PTF5, PPRESH5, PPRESF5, &
 &   YDGEMSPROF5, &
 &   PAEREXT5,PAEROM5, PAERASY5)

!*** *AER_OPT_PROP_AD * Adjoint of routine for aerosol optical properties calculations.

!**   INTERFACE.
!     ----------
!          *AER_OPT_PROP_AD * IS CALLED FROM *AER_REFL_AD*.
!     CALL AER_OPT_PROP_AD(KDLEN,ILEN,IMXCOUNT, 
!                      &   PQF, PTF, PPRESH, PPRESF, YDGEMSPROF, &
!                      &   PAEREXT,PAEROM, PAERASY, &
!                      &   PQF5, PTF5, PPRESH5, PPRESF5, YDGEMSPROF5, &
!                      &   PAEREXT5,PAEROM5, PAERASY5 )
!     WHERE KDLEN      = First dimension: Max number of observations, this set (INPUT)
!           KLEN       = Number of aerosol optical depth obs (INPUT)
!           IMXCOUNT   = Number of channels (INPUT)
!           PQF       =  Perturb Specific humidity at observation points, model levels (INPUT)
!           PTF       =  Perturb Temperature at observation points, model levels (INPUT)
!           PPRESH,PPRESF =  Perturb  Half/Full level pressure values at obs points, model levels (INPUT)
!           PGEMSPROF     = Perturb GEMS variables (INPUT)
!           PAEREXT       = Perturb Aerosol extinction profile at different locations and wavelengths (OUTPUT)
!           PAEROM        = Perturb Aerosol ssa profile at different locations and wavelengths (OUTPUT)
!           PAERASY       = Perturb Aerosol asymmetri parameter profile at different locations and wavelengths (OUTPUT)
!           PQF5       =  Traj Specific humidity at observation points, model levels (INPUT)
!           PTF5       =  Traj Temperature at observation points, model levels (INPUT)
!           PPRESH5,PPRESF5 =  Traj Half/Full level pressure values at obs points, model levels (INPUT)
!           PGEMSPROF5     = Traj GEMS variables (INPUT)
!           PAEREXT5       = Traj Aerosol extinction profile at different locations and wavelengths (OUTPUT)
!           PAEROM5        = Traj Aerosol ssa profile at different locations and wavelengths (OUTPUT)
!           PAERASY5       = Traj Aerosol asymmetri parameter profile at different locations and wavelengths
!                            (OUTPUT)


!**   AUTHOR.
!     -------
!        ANGELA BENEDETTI        *ECMWF*

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 2008-08-12 (adapted from aod_op.F90)

!     PURPOSE.
!     --------
!     - derive aerosol optical thickness, single scattering albedo and asymmetry parameter of aerosols
!       from profiles of mass mixing ratio (tangent linear)

! Optical properties in su_aerop.F90 avaliable at following wavelenghts
! NOTE that number of wavelengths has gone up from 16 to 17!
!         WVAL=   1 (340),  2 (380),  3 (400),  4 (440),  5 (469),  6 (500), 7 (532), 8 (555), 
!                 9 (645), 10 (670), 11 (800), 12 (858), 13 (865), 14 (1020), 15 (1240), 16 (1640), 17 (2130)
!     'MODIS Channels' (channels from other instruments can be added) 
!      -         470 nm, 550 nm, 660 nm, 870 nm, 1240 nm, 1630 nm, 2130 nm
! Corresponding IWAVL
!      IWAVL = [ 5, 8, 10, 13, 15, 16, 17] 
! **** DEPENDS ON J.-J.'s choice of numbering in su_aerop.F90 - please double check!!!!!!
!-----------------------------------------------------------------------

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

USE YOM_YGFL , ONLY : NAERO, NACTAERO, JPAERO
USE YOMDIM, ONLY : NFLEVG
USE YOMDB

USE YOMCST    ,ONLY : RG
USE YOMLUN    ,ONLY : NULERR
USE YOEAEROP  ,ONLY : ALF_SU, ALF_OM, ALF_DD, ALF_SS, ALF_BC &
                  & , ASY_SU, ASY_OM, ASY_DD, ASY_SS, ASY_BC &
                  & , OMG_SU, OMG_OM, OMG_DD, OMG_SS, OMG_BC
USE YOEAERSNK ,ONLY : RRHTAB
USE YOEAERSRC ,ONLY : NMAXTAER, NTYPAER, NBIN, NTYP 

USE TYPE_GEMS_PROFILES

IMPLICIT NONE

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

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

INTEGER(KIND=JPIM),INTENT(IN)    :: KDLEN, KLEN, KMXCOUNT

REAL(KIND=JPRB)   ,INTENT(INOUT)    :: PPRESH(KDLEN,0:NFLEVG) ! PERT/GRAD PRESSURE ON HALF LEVELS 
REAL(KIND=JPRB)   ,INTENT(INOUT)    :: PPRESF(KDLEN,NFLEVG)   ! PERT/GRAD PRESSURE ON FULL LEVELS 
REAL(KIND=JPRB)   ,INTENT(INOUT)    :: PTF(KDLEN,0:NFLEVG)    ! PERT/GRAD temperature 
REAL(KIND=JPRB)   ,INTENT(INOUT)    :: PQF(KDLEN,0:NFLEVG)    ! PERT/GRAD specific humidity 

TYPE (TYPE_GEMSPROF)   ,INTENT(INOUT) :: YDGEMSPROF(KDLEN,NGEMS) ! PERT/GRAD GEMS variables

REAL(KIND=JPRB)   ,INTENT(INOUT) :: PAEREXT(KDLEN,NFLEVG,KMXCOUNT) ! PERT/GRAD AEROSOL EXTINCTION 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PAEROM(KDLEN,NFLEVG,KMXCOUNT)  ! PERT/GRAD AEROSOL SINGLE SCATTERING ALBEDO 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PAERASY(KDLEN,NFLEVG,KMXCOUNT) ! PERT/GRAD AEROSOL ASYMMETRY PARAMETER

REAL(KIND=JPRB)   ,INTENT(IN)    :: PPRESH5(KDLEN,0:NFLEVG) ! PRESSURE ON HALF LEVELS FROM TRAJ
REAL(KIND=JPRB)   ,INTENT(IN)    :: PPRESF5(KDLEN,NFLEVG)   ! PRESSURE ON FULL LEVELS FROM TRAJ
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTF5(KDLEN,0:NFLEVG)    ! temperature FROM TRAJ
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQF5(KDLEN,0:NFLEVG)    ! specific humidity FROM TRAJ

TYPE (TYPE_GEMSPROF)   ,INTENT(IN) :: YDGEMSPROF5(KDLEN,NGEMS) ! GEMS variables from TRAJ

REAL(KIND=JPRB)   ,INTENT(OUT) :: PAEREXT5(KDLEN,NFLEVG,KMXCOUNT) ! Traj AEROSOL EXTINCTION
REAL(KIND=JPRB)   ,INTENT(OUT) :: PAEROM5(KDLEN,NFLEVG,KMXCOUNT)  ! Traj AEROSOL SINGLE SCATTERING ALBEDO
REAL(KIND=JPRB)   ,INTENT(OUT) :: PAERASY5(KDLEN,NFLEVG,KMXCOUNT) ! Traj AEROSOL ASYMMETRY PARAMETER


!*       0.5   LOCAL VARIABLES
!              ---------------
INTEGER(KIND=JPIM), PARAMETER :: IWAVLMX=7 ! Max number of MODIS channels
INTEGER(KIND=JPIM), DIMENSION(IWAVLMX) :: IWAVL=(/5, 8, 10, 13, 14, 15, 16/) ! Index for MODIS channels
INTEGER(KIND=JPIM) :: IACTAEROP ! Number of optically active aerosol species (NACTAERO-1, for now)
INTEGER(KIND=JPIM) :: IAER, ITYP, IBIN, IFLAG, ISUM, IWAVLINDEX
INTEGER(KIND=JPIM) :: JAER, JK, JL, JJ, JF, JP, JLEN, JTAB
INTEGER(KIND=JPIM), PARAMETER  :: JTABMX=12
INTEGER(KIND=JPIM), ALLOCATABLE :: ITYPE(:) !index to determine aerosol species
INTEGER(KIND=JPIM), DIMENSION(KLEN,NFLEVG) :: IRH

REAL(KIND=JPRB), DIMENSION(KLEN,NFLEVG)   :: ZQSAT5, ZT5, ZPF5
REAL(KIND=JPRB), ALLOCATABLE   :: ZAERTAU(:,:)
REAL(KIND=JPRB), ALLOCATABLE   :: ZAERTAU5(:,:)
REAL(KIND=JPRB), ALLOCATABLE :: ZAERF(:,:,:) ! pert aer mixing ratios (interpolated to obs locations)
REAL(KIND=JPRB), ALLOCATABLE :: ZAERF5(:,:,:) ! aer mixing ratios from traj (interpolated to obs locations)
REAL(KIND=JPRB), DIMENSION(KLEN,NFLEVG) :: ZAERTMR5 ! total aer mixing ratios from traj 
REAL(KIND=JPRB), ALLOCATABLE :: ZALF5(:), ZOMG5(:), ZASY5(:) ! optical properties of aerosol species from traj
REAL(KIND=JPRB)    :: ZRHCL5(KLEN,NFLEVG), ZDP5, ZDP
REAL(KIND=JPRB), ALLOCATABLE :: ZEXTTMP(:,:,:,:), ZOMTMP(:,:,:,:),  &
     &             ZASYTMP(:,:,:,:)
REAL(KIND=JPRB), ALLOCATABLE :: ZEXTTMP5(:,:,:,:), ZOMTMP5(:,:,:,:),  &
     &             ZASYTMP5(:,:,:,:)

REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
#include "satur.intfb.h"
!-----------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('AER_OPT_PROP_AD',0,ZHOOK_HANDLE)

!-----------------------------------------------------------------------
! Check that total number of aerosol bins is not larger than NACTAERO
ISUM=0
DO IAER=1,NMAXTAER
  ISUM=ISUM+NTYPAER(IAER)
ENDDO
IF (ISUM > NACTAERO) THEN
  CALL ABOR1('AOD_OP:TOTAL NUMBER OF REQUESTED BINS PER TYPE EXCEEDS NACTAERO')
ENDIF

! Remember to put in a check for maximum IBIN allowed per aerosol type

IACTAEROP=NACTAERO-1 ! Number of optically active aerosol species

ALLOCATE(ZAERF5(KLEN,NFLEVG,IACTAEROP))
ALLOCATE(ZAERF(KLEN,NFLEVG,IACTAEROP))
ALLOCATE(ITYPE(IACTAEROP))
ALLOCATE(ZALF5(IACTAEROP))
ALLOCATE(ZASY5(IACTAEROP))
ALLOCATE(ZOMG5(IACTAEROP))
ALLOCATE(ZAERTAU(KLEN,IACTAEROP))
ALLOCATE(ZAERTAU5(KLEN,IACTAEROP))


ALLOCATE(ZEXTTMP(KLEN,NFLEVG,IACTAEROP,17))
ALLOCATE(ZOMTMP(KLEN,NFLEVG,IACTAEROP,17))
ALLOCATE(ZASYTMP(KLEN,NFLEVG,IACTAEROP,17))
ALLOCATE(ZEXTTMP5(KLEN,NFLEVG,IACTAEROP,17))
ALLOCATE(ZOMTMP5(KLEN,NFLEVG,IACTAEROP,17))
ALLOCATE(ZASYTMP5(KLEN,NFLEVG,IACTAEROP,17))

! Extract aerosol mixing ratios from GEMS profiles structure

ZAERF(:,:,:)=0.0
ZAERF5(:,:,:)=0.0
ITYPE(:)=0
IF(NAERO > 0) THEN
  DO JLEN=1,KLEN
    IAER=0
    DO JF=1,NGEMS
      JP=0
      IF(YDGEMSPROF5(JLEN,JF)%IGRIB == 210001 .OR.  &
       &   YDGEMSPROF5(JLEN,JF)%IGRIB == 210002 .OR.  &
       &   YDGEMSPROF5(JLEN,JF)%IGRIB == 210003) JP=1 !SS 3 bins
      IF(YDGEMSPROF5(JLEN,JF)%IGRIB == 210004 .OR.  &
       &   YDGEMSPROF5(JLEN,JF)%IGRIB == 210005 .OR.  &
       &   YDGEMSPROF5(JLEN,JF)%IGRIB == 210006 ) JP=2 !DD 3 bins
      IF(YDGEMSPROF5(JLEN,JF)%IGRIB == 210007 .OR. &
       &   YDGEMSPROF5(JLEN,JF)%IGRIB == 210008) JP=3 ! ORGANIC MATTER - 2 bins
      IF(YDGEMSPROF5(JLEN,JF)%IGRIB == 210009 .OR. &
       &   YDGEMSPROF5(JLEN,JF)%IGRIB == 210010) JP=4 ! Black Carbon - 2 bins
      IF(YDGEMSPROF5(JLEN,JF)%IGRIB == 210011) JP=5 ! Sulphates
      IF(YDGEMSPROF5(JLEN,JF)%IGRIB == 210013) JP=7 ! Stratospheric background
      
      IF(JP > 0) THEN
        IAER=IAER+1
        ZAERF5(JLEN,:,IAER)=YDGEMSPROF5(JLEN,JF)%MODELPROF(:)
        ITYPE(IAER)=JP
        IF(IAER > JPAERO) THEN
          CALL ABOR1('AOD_OP:NOT ENOUGH AEROSOL SPECIES ALLOCATED')
        ENDIF
      ENDIF
      IF(YDGEMSPROF(JLEN,JF)%IGRIB == 210048) THEN ! Total aerosol mixing ratio
        ZAERTMR5(JLEN,:)=YDGEMSPROF5(JLEN,JF)%MODELPROF(:)
      ENDIF
    ENDDO
  ENDDO
ENDIF


! TRAJECTORY CALCULATIONS
! Compute relative humidity at observation points, on model levels

ZT5(1:KLEN,:) = PTF5(1:KLEN,1:NFLEVG) ! trajectory
ZPF5(1:KLEN,:)= PPRESF5(1:KLEN,:)     ! trajectory

IFLAG=2
CALL SATUR (1 , KLEN , KLEN  , 1 , NFLEVG,&
  & ZPF5, ZT5 , ZQSAT5 , IFLAG)

!-- define RH index from "clear-sky" relative humidity
IRH(:,:)=1
DO JK=1,NFLEVG
  DO JL=1,KLEN
    ZRHCL5(JL,JK)=(PQF5(JL,JK)/ZQSAT5(JL,JK))*100._JPRB
    DO JTAB=1,JTABMX
      IF (ZRHCL5(JL,JK) > RRHTAB(JTAB)) THEN
        IRH(JL,JK)=JTAB
      ENDIF
    ENDDO
  ENDDO
ENDDO

!*************************************************************************************************
!TRAJECTORY CALCULATIONS
!*************************************************************************************************

 PAEREXT5(:,:,:) = 0.0000001_JPRB !Traj
 PAEROM5(:,:,:)  = 0.0000001_JPRB
 PAERASY5(:,:,:) = 0.0000001_JPRB

IF(KMXCOUNT ==1) IWAVLINDEX=8   ! 550nm

DO JJ=1,KMXCOUNT

!   IWAVLINDEX is the wavelength index: for now the choice of channels is tailored to MODIS optical
!         depth retrievals (Remer et al. 2005 and Benedetti, 2005, ECMWF Tech. Memo
!                                                 "Requirements for AEROSOL-related observations").
! *** to be activated, for now assume AOD is only defined at 550nm , i.e. IWAVLINDEX=8 always
     IWAVLINDEX=IWAVL(JJ)

  DO JL=1,KLEN
   DO JAER=1,IACTAEROP

!-- ITYP is the aerosol type 1:SeaSalt,   2:DesertDust,   3:OrganicMatter, 4: BlackCarbon,
!                            5:SUlphates, 6:FlyingAshes
!   IBIN is the bin index: 1-3:SS, 1-3:DD, 1:OM, 1:BC, 1:SU, 1:FA
! This indeces are defined in su_aerw.F90

         ITYP=NTYP(JAER)
         IBIN=NBIN(JAER)
         IF(ITYP /= 0 .AND. IBIN == 0) THEN
          WRITE(NULERR,*)'AOD_OP:AT LEAST ONE BIN NEEDS TO BE ALLOCATED FOR TYPE ', ITYP
          WRITE(NULERR,*) 'PROGRAM WILL STOP'
          CALL ABOR1('')
         ENDIF
         IF(ITYP /= ITYPE(JAER)) THEN
          WRITE(NULERR,*)'AOD_OP: AEROSOL TYPE IS NOT MATCHING', ITYP
          WRITE(NULERR,*) 'PROGRAM WILL STOP'
          CALL ABOR1('')
         ENDIF

      ZAERTAU5(JL,JAER)=0.0_JPRB

      DO JK=1,NFLEVG

! Always double check dimensioning of arrays ALF_** in yoeaerop.F90 !!!!!!!!!
! It has changed a number of times!!!!!!!!
         IF (ITYP == 1) THEN
          ZALF5(JAER)=ALF_SS(IRH(JL,JK),IWAVLINDEX,IBIN)
          ZOMG5(JAER)=OMG_SS(IRH(JL,JK),IWAVLINDEX,IBIN)
          ZASY5(JAER)=ASY_SS(IRH(JL,JK),IWAVLINDEX,IBIN)
         ELSEIF (ITYP == 2) THEN
          ZALF5(JAER)=ALF_DD(IBIN,IWAVLINDEX)
          ZOMG5(JAER)=OMG_DD(IBIN,IWAVLINDEX)
          ZASY5(JAER)=ASY_DD(IBIN,IWAVLINDEX)
         ELSEIF (ITYP == 3) THEN
          ZALF5(JAER)=ALF_OM(IRH(JL,JK),IWAVLINDEX)
          ZOMG5(JAER)=OMG_OM(IRH(JL,JK),IWAVLINDEX)
          ZASY5(JAER)=ASY_OM(IRH(JL,JK),IWAVLINDEX)
         ELSEIF (ITYP == 4 .OR. ITYP == 6) THEN
          ZALF5(JAER)=ALF_BC(IWAVLINDEX)
          ZOMG5(JAER)=OMG_BC(IWAVLINDEX)
          ZASY5(JAER)=ASY_BC(IWAVLINDEX)
         ELSEIF (ITYP == 5 .OR. ITYP == 7 .OR. ITYP == 8 .OR. ITYP == 9) THEN
          ZALF5(JAER)=ALF_SU(IRH(JL,JK),IWAVLINDEX)
          ZOMG5(JAER)=OMG_SU(IRH(JL,JK),IWAVLINDEX)
          ZASY5(JAER)=ASY_SU(IRH(JL,JK),IWAVLINDEX)
         ENDIF

! convert units from m^2/g to m^2/kg
            ZALF5(JAER)=ZALF5(JAER)*1000._JPRB ! conversion from m^2/g to m^2/kg
            ZDP5=PPRESH5(JL,JK)-PPRESH5(JL,JK-1)

            ZAERTAU5(JL,JAER)=ZAERTAU5(JL,JAER)+ ZAERF5(JL,JK,JAER)*ZALF5(JAER)*(ZDP5/RG)

            ZEXTTMP5(JL,JK,JAER,JJ) = ZAERTAU5(JL,JAER)
            ZOMTMP5(JL,JK,JAER,JJ) =  ZOMG5(JAER) * ZAERTAU5(JL,JAER) ! equals the scattering cross section
            ZASYTMP5(JL,JK,JAER,JJ) = ZASY5(JAER) * ZOMG5(JAER) * ZAERTAU5(JL,JAER)

      ENDDO ! loop over model vertical levels

! BEN - optical properties as sum over all individual species

         DO JK=1,NFLEVG
           PAEREXT5(JL,JK,JJ) =   PAEREXT5(JL,JK,JJ) + ZEXTTMP5(JL,JK,JAER,JJ)
           PAEROM5(JL,JK,JJ)  =   PAEROM5(JL,JK,JJ)  + ZOMTMP5(JL,JK,JAER,JJ)
           PAERASY5(JL,JK,JJ) =   PAERASY5(JL,JK,JJ) + ZASYTMP5(JL,JK,JAER,JJ)
        ENDDO


    ENDDO ! loop over aerosol type

  ENDDO ! loop over horizontal observational points

! BEN - normalizations

        DO JL=1,KLEN
         DO JK=1,NFLEVG
          IF(PAEREXT5(JL,JK,JJ) > 0.0_JPRB) THEN
           PAEROM5(JL,JK,JJ) = PAEROM5(JL,JK,JJ)/PAEREXT5(JL,JK,JJ)

          ENDIF
          IF(PAEREXT5(JL,JK,JJ) > 0.0_JPRB .AND. PAEROM5(JL,JK,JJ) > 0.0_JPRB) THEN
           PAERASY5(JL,JK,JJ)= PAERASY5(JL,JK,JJ)/ ( PAEROM5(JL,JK,JJ)*PAEREXT5(JL,JK,JJ) )

          ENDIF
         ENDDO
        ENDDO

ENDDO ! loop over MODIS channels


!*************************************************************************************************
! ADJOINT CALCULATIONS
!*************************************************************************************************

 ZEXTTMP(:,:,:,:) = 0.0_JPRB
 ZOMTMP(:,:,:,:) = 0.0_JPRB
 ZASYTMP(:,:,:,:) = 0.0_JPRB

IF(KMXCOUNT ==1) IWAVLINDEX=8   ! 550nm

DO JJ=KMXCOUNT,1,-1  ! Loop on MODIS channels

!   IWAVLINDEX is the wavelength index: for now the choice of channels is tailored to MODIS optical
!         depth retrievals (Remer et al. 2005 and Benedetti, 2005, ECMWF Tech. Memo
!                                                 "Requirements for AEROSOL-related observations").
! *** to be activated, for now assume AOD is only defined at 550nm , i.e. IWAVLINDEX=8 always
!     IWAVLINDEX=IWAVL(JJ)

! Normalizations 
        DO JL=KLEN,1,-1
         DO JK=NFLEVG,1,-1

          IF(PAEREXT5(JL,JK,JJ) > 0.0_JPRB .AND. PAEROM5(JL,JK,JJ) > 0.0_JPRB) THEN
 
           PAERASY(JL,JK,JJ) = PAERASY(JL,JK,JJ) + PAERASY(JL,JK,JJ) / (PAEROM5(JL,JK,JJ)*PAEREXT5(JL,JK,JJ))
           PAEREXT(JL,JK,JJ) = PAEREXT(JL,JK,JJ) - PAERASY5(JL,JK,JJ) * PAEROM5(JL,JK,JJ) * PAERASY(JL,JK,JJ) &
      &                                           /( PAEROM5(JL,JK,JJ)*PAEREXT5(JL,JK,JJ) )**2 
           PAEROM(JL,JK,JJ) = PAEROM(JL,JK,JJ) - PAERASY5(JL,JK,JJ) * PAEREXT5(JL,JK,JJ) * PAERASY(JL,JK,JJ) &
      &                                           /( PAEROM5(JL,JK,JJ)*PAEREXT5(JL,JK,JJ) )**2

         ENDIF

         IF(PAEREXT5(JL,JK,JJ) > 0.0_JPRB) THEN

           PAEROM(JL,JK,JJ) = PAEROM(JL,JK,JJ) + PAEROM(JL,JK,JJ)/PAEREXT5(JL,JK,JJ)
           PAEREXT(JL,JK,JJ) = PAEREXT(JL,JK,JJ) + (PAEROM(JL,JK,JJ) * PAEROM5(JL,JK,JJ)) &
     &                        /PAEREXT5(JL,JK,JJ)**2

         ENDIF
        ENDDO
       ENDDO

  DO JL=KLEN,1,-1
   DO JAER=IACTAEROP,1,-1

    ! BEN - optical properties as sum over all individual species
         DO JK=NFLEVG,1,-1
           ZASYTMP(JL,JK,JAER,JJ) = ZASYTMP(JL,JK,JAER,JJ) + PAERASY(JL,JK,JJ)
           ZOMTMP(JL,JK,JAER,JJ) = ZOMTMP(JL,JK,JAER,JJ) + PAEROM(JL,JK,JJ)
           ZEXTTMP(JL,JK,JAER,JJ) = ZEXTTMP(JL,JK,JAER,JJ) + PAEREXT(JL,JK,JJ)
        ENDDO

! Initialize local variables
      ZDP     = 0.0_JPRB
      ZAERTAU(JL,JAER)=0.0_JPRB


!-- ITYP is the aerosol type 1:SeaSalt,   2:DesertDust,   3:OrganicMatter, 4: BlackCarbon,
!                            5:SUlphates, 6:FlyingAshes
!   IBIN is the bin index: 1-3:SS, 1-3:DD, 1:OM, 1:BC, 1:SU, 1:FA
! This indeces are defined in su_aerw.F90 

         ITYP=NTYP(JAER)
         IBIN=NBIN(JAER)
         IF(ITYP /= 0 .AND. IBIN == 0) THEN
          WRITE(NULERR,*)'AOD_OP:AT LEAST ONE BIN NEEDS TO BE ALLOCATED FOR TYPE ', ITYP
          WRITE(NULERR,*) 'PROGRAM WILL STOP'
          CALL ABOR1('') 
         ENDIF
         IF(ITYP /= ITYPE(JAER)) THEN
          WRITE(NULERR,*)'AOD_OP: AEROSOL TYPE IS NOT MATCHING', ITYP
          WRITE(NULERR,*) 'PROGRAM WILL STOP'
          CALL ABOR1('')
         ENDIF

      ZAERTAU5(JL,JAER)=0.0_JPRB
        
      DO JK=NFLEVG,1,-1

! Always double check dimensioning of arrays ALF_** in yoeaerop.F90 !!!!!!!!!
! It has changed a number of times!!!!!!!!
         IF (ITYP == 1) THEN
          ZALF5(JAER)=ALF_SS(IRH(JL,JK),IWAVLINDEX,IBIN)
          ZOMG5(JAER)=OMG_SS(IRH(JL,JK),IWAVLINDEX,IBIN)
          ZASY5(JAER)=ASY_SS(IRH(JL,JK),IWAVLINDEX,IBIN)
         ELSEIF (ITYP == 2) THEN
          ZALF5(JAER)=ALF_DD(IBIN,IWAVLINDEX)
          ZOMG5(JAER)=OMG_DD(IBIN,IWAVLINDEX)
          ZASY5(JAER)=ASY_DD(IBIN,IWAVLINDEX)
         ELSEIF (ITYP == 3) THEN
          ZALF5(JAER)=ALF_OM(IRH(JL,JK),IWAVLINDEX)
          ZOMG5(JAER)=OMG_OM(IRH(JL,JK),IWAVLINDEX)
          ZASY5(JAER)=ASY_OM(IRH(JL,JK),IWAVLINDEX)
         ELSEIF (ITYP == 4 .OR. ITYP == 6) THEN
          ZALF5(JAER)=ALF_BC(IWAVLINDEX)
          ZOMG5(JAER)=OMG_BC(IWAVLINDEX)
          ZASY5(JAER)=ASY_BC(IWAVLINDEX)
         ELSEIF (ITYP == 5 .OR. ITYP == 7 .OR. ITYP == 8 .OR. ITYP == 9) THEN
          ZALF5(JAER)=ALF_SU(IRH(JL,JK),IWAVLINDEX)
          ZOMG5(JAER)=OMG_SU(IRH(JL,JK),IWAVLINDEX)
          ZASY5(JAER)=ASY_SU(IRH(JL,JK),IWAVLINDEX)
         ENDIF
               
! convert units from m^2/g to m^2/kg
          ZALF5(JAER)=ZALF5(JAER)*1000._JPRB ! conversion from m^2/g to m^2/kg

          ZAERTAU5(JL,JAER)=ZAERTAU5(JL,JAER)+ ZAERF5(JL,JK,JAER)*ZALF5(JAER)*(ZDP5/RG)

          ZAERTAU(JL,JAER) = ZAERTAU(JL,JAER) + ZASY5(JAER) * ZOMG5(JAER) * ZASYTMP(JL,JK,JAER,JJ)
          ZASYTMP(JL,JK,JAER,JJ) = 0.0_JPRB
          ZAERTAU(JL,JAER) = ZAERTAU(JL,JAER) + ZOMG5(JAER) * ZOMTMP(JL,JK,JAER,JJ)
          ZOMTMP(JL,JK,JAER,JJ) = 0.0_JPRB
          ZAERTAU(JL,JAER) = ZAERTAU(JL,JAER) + ZEXTTMP(JL,JK,JAER,JJ)
          ZEXTTMP(JL,JK,JAER,JJ) = 0.0_JPRB

          ZAERF(JL,JK,JAER) = ZAERF(JL,JK,JAER) + ZAERTAU(JL,JAER)*ZALF5(JAER)*(ZDP5/RG) 
          ZDP = ZDP  + ZAERTAU(JL,JAER) * ( ZAERF5(JL,JK,JAER)*ZALF5(JAER) /RG)  

           PPRESH(JL,JK) = PPRESH(JL,JK) + ZDP
           PPRESH(JL,JK-1) = PPRESH(JL,JK-1) - ZDP
           ZDP = 0.0

      ENDDO ! loop over model vertical levels
        ZAERTAU(JL,JAER) = 0.0_JPRB

    ENDDO ! loop over aerosol type
    
 PAERASY(JL,:,JJ) = 0.0_JPRB
 PAEROM(JL,:,JJ)  = 0.0_JPRB
 PAEREXT(JL,:,JJ) = 0.0_JPRB


  ENDDO ! loop over horizontal observational points

ENDDO ! loop over MODIS channels

DEALLOCATE(ZAERF5)
DEALLOCATE(ZAERF)
DEALLOCATE(ITYPE)
DEALLOCATE(ZALF5)
DEALLOCATE(ZASY5)
DEALLOCATE(ZOMG5)
DEALLOCATE(ZAERTAU)
DEALLOCATE(ZAERTAU5)

DEALLOCATE(ZEXTTMP)
DEALLOCATE(ZOMTMP)
DEALLOCATE(ZASYTMP)
DEALLOCATE(ZEXTTMP5)
DEALLOCATE(ZOMTMP5)
DEALLOCATE(ZASYTMP5)


!-----------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('AER_OPT_PROP_AD',1,ZHOOK_HANDLE)
END SUBROUTINE AER_OPT_PROP_AD

