SUBROUTINE AER_BDGTMSS &
 & ( KIDIA  , KFDIA   , KLON   , KLEV, KACTAERO, KWHAT, KNWAVL, KTWAVL, KSTART, KSTEP, &
 &   PAPH   , PAEROK  , PCAERO , PDZ     , PO3     , PRHCL   , PT      , &
 &   PAERMSS, PAERMSST, PAERTAU, PAERTAUT, PAERTAUI, PAERTAUB, PAERTAUA, PAERTAUF, PAERTAUO, &
 &   PAEROMGI,PAERASYI, PLISIS , PLISIT  )

!*** * AER_BDGTMSS* - COMPUTATION OF AEROSOL MASS(ES) AND OPTICAL DEPTH(S)

!**   INTERFACE.
!     ----------
!          *AER_BDGTMSS* IS CALLED FROM *CALLPAR*.

!     AUTHOR.
!     -------
!        JEAN-JACQUES MORCRETTE  *ECMWF*

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 2005-03-30
!     A. Benedetti: 2005-04-27 to compute aerosol optical mass from 
!                              optical depth
!     JJMorcrette : 20060721  16 wavelengths
!     JJMorcrette : 20070124  multi-wavelengths implemented
!     JJMorcrette : 20071101  17 wavelengths + absorption AOD
!     JJMorcrette : 20080407  total omega and g
!     JJMorcrette : 20080508  so2/so4
!     JJMorcrette : 20090311  19 wavelengths (+2 for lidar comparisons)
!     JJMorcrette : 20090706  Lidar simulator

!     PURPOSE.
!     --------
!     Depending on the value of KWHAT, either process:
!     - optical thickness from mass mixing ratio (KWHAT =0)
!     - mass mixing ratio from optical thickness (KWHAT/=0)

!     End products are:
!     - a total optical depth for each aerosol type separately        (PAERTAUT) 
!       and a total aerosol optical depth for all aerosols            (PAERTAUI)
!     - an absorption optical depth for each aerosol type separately  (PAERTAUB) 
!       and an absorption aerosol optical depth for all aerosols      (PAERTAUA)
!       and a total aerosol optical depth for all aerosols < 0.5 um   (PAERTAUF)
!     - a vertically integrated mass of aerosol for each aerosol type (PAERMSST)

!     Depending on the value of KWAVL, the optical depths are produced
!     for different wavelengths (in nm)
!         KWVAL=  340,  355,  380,  400,  440,  469,  500,  532,  555, 
!                 645,  670,  800,  858,  865, 1020, 1064, 1240, 1640, 2130

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

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

USE YOMCST    ,ONLY : RG
USE YOEAERATM ,ONLY : LUVINDX, LAERLISI
USE YOEAERLID ,ONLY : NWLID
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 
USE YOEDBUG   ,ONLY : KSTPDBG, NSTPDBG

IMPLICIT NONE

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

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

INTEGER(KIND=JPIM),INTENT(IN)  :: KLON 
INTEGER(KIND=JPIM),INTENT(IN)  :: KIDIA 
INTEGER(KIND=JPIM),INTENT(IN)  :: KFDIA 
INTEGER(KIND=JPIM),INTENT(IN)  :: KLEV
INTEGER(KIND=JPIM),INTENT(IN)  :: KACTAERO  ! Number of active aerosol species
INTEGER(KIND=JPIM),INTENT(IN)  :: KWHAT, KSTART, KSTEP
INTEGER(KIND=JPIM),INTENT(IN)  :: KNWAVL, KTWAVL(19)

REAL(KIND=JPRB)   ,INTENT(IN)  :: PAPH(KLON,0:KLEV)    , PT(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)  :: PAEROK(KLON,KLEV,KACTAERO), PCAERO(KLON,KLEV,KACTAERO)
REAL(KIND=JPRB)   ,INTENT(IN)  ::  PDZ(KLON,KLEV)           , PO3(KLON,KLEV)            , PRHCL(KLON,KLEV)

REAL(KIND=JPRB)   ,INTENT(OUT) :: PAERMSS(KLON,KLEV,KACTAERO), PAERTAU(KLON,KLEV,KACTAERO), PAERTAUO(KLON,KLEV,3)
REAL(KIND=JPRB)   ,INTENT(OUT) :: PAERMSST(KLON,KACTAERO)    
REAL(KIND=JPRB)   ,INTENT(OUT) :: PAERTAUI(KLON,19), PAEROMGI(KLON,19), PAERASYI(KLON,19), PAERTAUT(KLON,KACTAERO,19)
REAL(KIND=JPRB)   ,INTENT(OUT) :: PAERTAUA(KLON,19), PAERTAUF(KLON,19), PAERTAUB(KLON,KACTAERO,19)
REAL(KIND=JPRB)   ,INTENT(OUT) :: PLISIS(KLON,NWLID,0:KLEV), PLISIT(KLON,NWLID,0:KLEV)


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

INTEGER(KIND=JPIM) :: IAER, IBIN, ICAER, ITAER, ITYP, IWAVL
INTEGER(KIND=JPIM) :: JAER, JK, JL, JTAB, JWAVL
INTEGER(KIND=JPIM) :: INAER(15), IRH(KLON,KLEV)
LOGICAL :: LLPRINT

REAL(KIND=JPRB) :: ZALF(KACTAERO), ZAERMSS, ZASY(KACTAERO), ZOMG(KACTAERO), ZETA(KLON,KLEV)
REAL(KIND=JPRB) :: ZAEROMGT(KLON,KACTAERO,19)  , ZAERASYT(KLON,KACTAERO,19)

REAL(KIND=JPRB) :: ZAP(KLON,KLEV), ZCLD(KLON,KLEV), ZCO2(KLON,KLEV)
REAL(KIND=JPRB) :: ZDP(KLON,KLEV), ZNO2(KLON,KLEV)

!!!#include "aer_lidsim.intfb.h"

REAL(KIND=JPRB) :: ZHOOK_HANDLE
!-----------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('AER_BDGTMSS',0,ZHOOK_HANDLE)

LLPRINT=.FALSE.
DO JL=1,NSTPDBG
  IF (KSTEP == KSTART .OR. KSTEP == KSTPDBG(JL)) THEN
    LLPRINT=.TRUE.
  ENDIF
ENDDO

!-- units:
!   ------
! ZALF is the extinction coefficient           m2 g-1
! PAEROK is the aerosol mass mixing ratio      kg kg-1 
! PCAERO is the aerosol density                kg cm-3 (KWHAT=0)
! PCAERO is the optical thickness              ND     (KWHAT=2)

ICAER=0
DO JAER=1,NMAXTAER
  IF (NTYPAER(JAER) /= 0) THEN
    ITAER=NTYPAER(JAER)
    DO IAER=1,ITAER
      ICAER=ICAER+1
      INAER(ICAER)=JAER*10+IAER
    ENDDO
  ENDIF
ENDDO

IRH(:,:)=1
PAERTAUI(KIDIA:KFDIA,:)=0._JPRB
PAEROMGI(KIDIA:KFDIA,:)=0._JPRB
PAERASYI(KIDIA:KFDIA,:)=0._JPRB
PAERTAUA(KIDIA:KFDIA,:)=0._JPRB
PAERTAUF(KIDIA:KFDIA,:)=0._JPRB
PAERMSS(KIDIA:KFDIA,1:KLEV,1:KACTAERO)= 0.0_JPRB
PAERTAUO(KIDIA:KFDIA,1:KLEV,1:3)=0._JPRB

IF (KWHAT == 0) THEN 
!-- inputs PCAERO is aerosol density in kg cm-3 

!-- define RH index from "clear-sky" (not yet!) relative humidity
  DO JK=1,KLEV
    DO JL=KIDIA,KFDIA
      ZETA(JL,JK)= (PAPH(JL,JK-1)+PAPH(JL,JK))/(2._JPRB*PAPH(JL,KLEV))
      DO JTAB=1,12
        IF (PRHCL(JL,JK)*100._JPRB > RRHTAB(JTAB)) THEN
          IRH(JL,JK)=JTAB
        ENDIF
      ENDDO
    ENDDO
  ENDDO

!-- mass of aerosols in each layer

  DO JAER=1,KACTAERO
    PAERMSST(KIDIA:KFDIA,JAER)=0._JPRB
    DO JK=1,KLEV
      DO JL=KIDIA,KFDIA
        ZAERMSS = PAEROK(JL,JK,JAER)*(PAPH(JL,JK)-PAPH(JL,JK-1))/RG 
        PAERMSS(JL,JK,JAER)= ZAERMSS
!-PAERMSST in kg m-2 
        PAERMSST(JL,JAER)  = PAERMSST(JL,JAER)+PAERMSS(JL,JK,JAER)
      ENDDO
    ENDDO
  ENDDO
      
  DO JWAVL=1,KNWAVL
    IWAVL=KTWAVL(JWAVL)

    DO JAER=1,KACTAERO
      PAERTAUT(KIDIA:KFDIA,JAER,JWAVL)=0._JPRB
      ZAEROMGT(KIDIA:KFDIA,JAER,JWAVL)=0._JPRB
      ZAERASYT(KIDIA:KFDIA,JAER,JWAVL)=0._JPRB
      PAERTAUB(KIDIA:KFDIA,JAER,JWAVL)=0._JPRB
      ITYP=INAER(JAER)/10
      IBIN=INAER(JAER)-ITYP*10

!-- ITYP is the aerosol type 1:SS,   2:DD,   3:OM    4:BC,   5:SU,   6:FA,   7:BS,   8=VS,
!   IBIN is the bin index: 1-3:SS, 1-3:DD,   2:OM,   2:BC,   2:SU,   1:FA,   1:SB    1=VS,
!   N.B.: extinction coefficients are in m2 g-1

      DO JK=1,KLEV
        DO JL=KIDIA,KFDIA
          IF (ITYP == 1) THEN
            ZALF(JAER)=ALF_SS(IRH(JL,JK),IWAVL,IBIN)
            ZOMG(JAER)=OMG_SS(IRH(JL,JK),IWAVL,IBIN)
            ZASY(JAER)=ASY_SS(IRH(JL,JK),IWAVL,IBIN)
          ELSEIF (ITYP == 2) THEN
            ZALF(JAER)=ALF_DD(IBIN,IWAVL)
            ZOMG(JAER)=OMG_DD(IBIN,IWAVL)
            ZASY(JAER)=ASY_DD(IBIN,IWAVL)
          ELSEIF (ITYP == 3) THEN
            ZALF(JAER)=ALF_OM(IRH(JL,JK),IWAVL)
            ZOMG(JAER)=OMG_OM(IRH(JL,JK),IWAVL)
            ZASY(JAER)=ASY_OM(IRH(JL,JK),IWAVL)
          ELSEIF (ITYP == 4 .OR. ITYP == 6) THEN
            ZALF(JAER)=ALF_BC(IWAVL)
            ZOMG(JAER)=OMG_BC(IWAVL)
            ZASY(JAER)=ASY_BC(IWAVL)
          ELSEIF (ITYP == 5 .OR. ITYP == 7 .OR. ITYP == 8) THEN
            ZALF(JAER)=ALF_SU(IRH(JL,JK),IWAVL)
            ZOMG(JAER)=OMG_SU(IRH(JL,JK),IWAVL)
            ZASY(JAER)=ASY_SU(IRH(JL,JK),IWAVL)
!-- SO2 does not contribute to optical depth, only SO4 does.
            IF (ITYP == 5 .AND. IBIN == 2) THEN
              ZALF(JAER)=0._JPRB
              ZOMG(JAER)=0._JPRB
              ZASY(JAER)=0._JPRB
            ENDIF
          ENDIF


!- PAERTAU (ND = g m-2 * m2 g-1), PAERTAUT (ND)
          PAERTAU(JL,JK,JAER)     = PAERMSS(JL,JK,JAER) * 1.E+03_JPRB * ZALF(JAER)
          PAERTAUT(JL,JAER,JWAVL) = PAERTAUT(JL,JAER,JWAVL)+PAERTAU(JL,JK,JAER)
          ZAEROMGT(JL,JAER,JWAVL) = ZAEROMGT(JL,JAER,JWAVL)+PAERTAU(JL,JK,JAER)*ZOMG(JAER)
          ZAERASYT(JL,JAER,JWAVL) = ZAERASYT(JL,JAER,JWAVL)+PAERTAU(JL,JK,JAER)*ZOMG(JAER)*ZASY(JAER)
!- PAERTAUB is the absorption optical thickness 
!- to compare to UV Index, should it be weighted by (1.-eta) ?
          IF (LUVINDX) THEN  
            PAERTAUB(JL,JAER,JWAVL) = PAERTAUB(JL,JAER,JWAVL)+PAERTAU(JL,JK,JAER)*(1._JPRB-ZOMG(JAER))
          ELSE
            PAERTAUB(JL,JAER,JWAVL) = PAERTAUB(JL,JAER,JWAVL)+PAERTAU(JL,JK,JAER)*(1._JPRB-ZOMG(JAER))*(1._JPRB-ZETA(JL,JK))
          ENDIF
        ENDDO
      ENDDO

!-- total optical thickness: as sum over all individual aerosol optical thicknesses

      DO JL=KIDIA,KFDIA
        PAERTAUI(JL,JWAVL)=PAERTAUI(JL,JWAVL)+PAERTAUT(JL,JAER,JWAVL)
        PAERTAUA(JL,JWAVL)=PAERTAUA(JL,JWAVL)+PAERTAUB(JL,JAER,JWAVL)
        PAEROMGI(JL,JWAVL)=PAEROMGI(JL,JWAVL)+ZAEROMGT(JL,JAER,JWAVL)
        PAERASYI(JL,JWAVL)=PAERASYI(JL,JWAVL)+ZAERASYT(JL,JAER,JWAVL)
      ENDDO
      IF ( (ITYP == 1 .AND. IBIN == 1) .OR. (ITYP == 2 .AND. IBIN == 1) .OR. ITYP >= 3 ) THEN
        DO JL=KIDIA,KFDIA
          PAERTAUF(JL,JWAVL)=PAERTAUF(JL,JWAVL)+PAERTAUT(JL,JAER,JWAVL)
        ENDDO
      ENDIF

!-- for 532 nm, store the profile of optical thickness for the sum of aerosols, 
!   and separately for the natural (SS+DU) and anthropogenic (OM+BC+SU) aerosols
      IF (JWAVL == 8) THEN
        DO JK=1,KLEV
          DO JL=KIDIA,KFDIA
            PAERTAUO(JL,JK,1)=PAERTAUO(JL,JK,1)+PAERTAU(JL,JK,JAER)
          ENDDO
        ENDDO

!-- ITYP = 1: SS   ; = 2: DU
        IF (ITYP < 3) THEN
          DO JK=1,KLEV
            DO JL=KIDIA,KFDIA
              PAERTAUO(JL,JK,2)=PAERTAUO(JL,JK,2)+PAERTAU(JL,JK,JAER)
            ENDDO
          ENDDO
        ELSE
!-- ITYP = 3: OM   ; = 4: BC   ; = 5: SU
          DO JK=1,KLEV
            DO JL=KIDIA,KFDIA
              PAERTAUO(JL,JK,3)=PAERTAUO(JL,JK,3)+PAERTAU(JL,JK,JAER)
            ENDDO
          ENDDO
        ENDIF
      ENDIF

    ENDDO

    DO JL=KIDIA,KFDIA
      IF( PAERTAUI(JL,JWAVL) > 0._JPRB .AND. PAEROMGI(JL,JWAVL) > 0._JPRB) THEN
        PAERASYI(JL,JWAVL)=PAERASYI(JL,JWAVL)/PAEROMGI(JL,JWAVL)
        PAEROMGI(JL,JWAVL)=PAEROMGI(JL,JWAVL)/PAERTAUI(JL,JWAVL)
      ELSE
        PAERASYI(JL,JWAVL)=0._JPRB
        PAEROMGI(JL,JWAVL)=0._JPRB
      ENDIF
    ENDDO

  ENDDO
ENDIF


! Watch out the optical properties for this inversion below from optical thickness to mmr might not be 
! correct anymore

! BEN
IF (KWHAT == 2) THEN 
!-- inputs PCAERO are optical thicknesses at 0.55 microns. From those
! get aerosol mass in kg/kg

!-- define RH index from "clear-sky" (not yet!) relative humidity
  DO JK=1,KLEV
    DO JL=KIDIA,KFDIA
      DO JTAB=1,12
        IF (PRHCL(JL,JK)*100. > RRHTAB(JTAB)) THEN
          IRH(JL,JK)=JTAB
        ENDIF
      ENDDO
    ENDDO
  ENDDO

  PAERTAUI(KIDIA:KFDIA,:)=0._JPRB
  PAEROMGI(KIDIA:KFDIA,:)=0._JPRB
  PAERASYI(KIDIA:KFDIA,:)=0._JPRB
  PAERTAUA(KIDIA:KFDIA,:)=0._JPRB
  IWAVL=KTWAVL(1)

  DO JAER=1,KACTAERO
    PAERMSST(KIDIA:KFDIA,JAER)=0._JPRB
    PAERTAUT(KIDIA:KFDIA,JAER,:)=0._JPRB
    PAERTAUB(KIDIA:KFDIA,JAER,:)=0._JPRB

    DO JK=1,KLEV
      DO JL=KIDIA,KFDIA
        
        ITYP=INAER(JAER)/10
        IBIN=INAER(JAER)-ITYP*10
!-- ITYP is the aerosol type 1:SS,   2:DD,   3:OM,   4:BC,   5:SU,   6:SU     
!   IBIN is the bin index: 1-3:SS, 1-3:DD,   1:OM
        IF (ITYP == 1) THEN
          ZALF(JAER)=ALF_SS(IRH(JL,JK),IWAVL,IBIN)
        ELSEIF (ITYP == 2) THEN
          ZALF(JAER)=ALF_DD(IBIN,IWAVL)
        ELSEIF (ITYP == 3) THEN
          ZALF(JAER)=ALF_OM(IRH(JL,JK),IWAVL)
        ELSEIF (ITYP == 4 .OR. ITYP == 6) THEN
          ZALF(JAER)=ALF_BC(IWAVL)
        ELSEIF (ITYP == 5 .OR. ITYP == 7 .OR. ITYP == 8) THEN
          ZALF(JAER)=ALF_SU(IRH(JL,JK),IWAVL)
          IF (ITYP == 5 .AND. IBIN == 2) THEN
            ZALF(JAER)=0._JPRB
          ENDIF
        ENDIF

        PAERMSS(JL,JK,JAER)=0._JPRB

        PAERTAU(JL,JK,JAER)=PCAERO(JL,JK,JAER)
        PAERTAUT(JL,JAER,1)=PAERTAUT(JL,JAER,1)+PAERTAU(JL,JK,JAER)
        
        ZDP(JL,JK)=PAPH(JL,JK)-PAPH(JL,JK-1)

        IF (ZALF(JAER) /= 0.0_JPRB .OR. ZDP(JL,JK) /=0.0_JPRB ) THEN 
          PAERMSS(JL,JK,JAER)=PAERTAU(JL,JK,JAER)*RG/(ZDP(JL,JK)*ZALF(JAER)*1000._JPRB)
        ENDIF

        PAERMSST(JL,JAER)  =PAERMSST(JL,JAER)+PAERMSS(JL,JK,JAER)
      ENDDO
    ENDDO
    DO JL=KIDIA,KFDIA
      PAERTAUI(JL,1)=PAERTAUI(JL,1)+PAERTAUT(JL,JAER,1)
      PAERTAUA(JL,1)=0._JPRB
    ENDDO
  ENDDO
ENDIF

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

!
!*         2.     LIDAR SIGNAL SIMULATIONS AT 355, 532 and 1064 nm
!                 ------------------------------------------------

DO JK=0,KLEV
  DO JL=KIDIA,KFDIA
    PLISIS(JL,1:NWLID,JK)=0._JPRB
    PLISIT(JL,1:NWLID,JK)=0._JPRB
  ENDDO
ENDDO

IF (LAERLISI) THEN
  DO JK=1,KLEV
    DO JL=KIDIA,KFDIA
      ZAP(JL,JK)=(PAPH(JL,JK)+PAPH(JL,JK-1))*0.5_JPRB
      ZCO2(JL,JK)=5.014E-06_JPRB
      ZCLD(JL,JK)=0._JPRB
      ZNO2(JL,JK)=1.E-07_JPRB
    ENDDO
  ENDDO
!
!  CALL AER_LIDSIM &
!    &( KIDIA , KFDIA, KLON, KLEV , KACTAERO, &
!    &  PAEROK, PAPH , ZAP , ZCO2 , ZCLD , &
!    &  ZDP   , ZNO2 , PO3 , PRHCL, PT   , &
!    &  PLISIS, PLISIT &
!    &)     
!
ENDIF

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

