!OPTIONS XOPT(HSFUN)
SUBROUTINE AER_CLIMG &
 &( KIDIA , KFDIA, KLON , KLEV , KAERO, &
 &  PAPRS , PSIN , PCLON, PSLON, PTH  , &
 &  PAERCLIM  &
 &)  

!***********************************************************************
! CAUTION: THIS ROUTINE WORKS ONLY ON A NON-ROTATED, UNSTRETCHED GRID
!***********************************************************************

!**** *AER_CLIMG - MONTHLY MEAN CLIMATOLOGY OF AEROSOLS
!                  ADAPTED FROM *RADACT*

!     PURPOSE.
!     --------

!**   INTERFACE.
!     ----------
!        CALL *AER_CLIMG* FROM *CALLPAR*

!        EXPLICIT ARGUMENTS :
!        --------------------
!     ==== INPUTS ===
!     ==== OUTPUTS ===

!        IMPLICIT ARGUMENTS :   NONE
!        --------------------

!     METHOD.
!     -------

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

!          NONE

!     REFERENCE.
!     ----------

!        SEE RADIATION'S PART OF THE MODEL'S DOCUMENTATION AND
!        ECMWF RESEARCH DEPARTMENT DOCUMENTATION OF THE "I.F.S"

!     AUTHOR.
!     -------
!     J.-J. MORCRETTE  E.C.M.W.F.    20/04/2005

!     MODIFICATIONS.
!     --------------
!     J.-J. MORCRETTE  E.C.M.W.F.    93/03/15   OPERATIONAL CLIMATOLOGY
!     JJMorcrette 98-12-21 GISS volcanic aerosol climatology
!     JJMorcrette 99-09    monthly climatology of tropospheric aerosols
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!-----------------------------------------------------------------------

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

USE YOMCST   , ONLY :         RPI
USE YOEAERD  , ONLY : CVDAES   ,CVDAEL   ,CVDAEU   ,CVDAED   ,&
 & RCAEOPS  ,RCAEOPL  ,RCAEOPU  ,RCAEOPD  ,&
 & RCVOBGA  ,RCTRPT   ,RAESC    ,RAESS    ,&
 & RAELC    ,RAELS    ,RAEUC    ,RAEUS    ,RAEDC    ,&
 & RAEDS  
USE YOEAERSRC ,ONLY : NTYPAER
USE YOERAD   , ONLY : LHVOLCA  ,LNEWAER
USE YOEAERC  , ONLY : RTAEBC  ,RTAEOR   ,RTAESD   ,RTAESS   ,RTAESU   , &
 & RTAEVO  
USE YOMDIM   , ONLY : NFLEVG
USE YOMGEM   , ONLY : VBH      ,VAH      ,VP00

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KLON 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KAERO
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPRS(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSIN(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCLON(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSLON(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTH(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PAERCLIM(KLON,KLEV,KAERO) 
!     -----------------------------------------------------------------

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

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

!*       0.2   LOCAL ARRAYS.
!              -------------

INTEGER(KIND=JPIM) :: IRINT, ISHIFT, ICF 

INTEGER(KIND=JPIM) :: IINLA1(KLON), IINLA2(KLON)
INTEGER(KIND=JPIM) :: IINLO1(KLON), IINLO2(KLON)

REAL(KIND=JPRB) :: ZAED  (KLON), ZAEL  (KLON), ZAES  (KLON), ZAEU  (KLON)
REAL(KIND=JPRB) :: ZAEQDN(KLON), ZAEQDO(KLON), ZAEQLN(KLON), ZAEQLO(KLON)
REAL(KIND=JPRB) :: ZAEQSN(KLON), ZAEQSO(KLON), ZAEQUN(KLON), ZAEQUO(KLON)

REAL(KIND=JPRB) :: ZAERBC(KLON), ZAEROR(KLON), ZAERSD(KLON)
REAL(KIND=JPRB) :: ZAERSS(KLON), ZAERSU(KLON), ZAERVO(KLON)

REAL(KIND=JPRB) :: ZAETRN(KLON),ZAETRO(KLON)
REAL(KIND=JPRB) :: ZSTRDEP(KLON)

REAL(KIND=JPRB) :: ZAER1(KLON,KLEV), ZAER2(KLON,KLEV), ZAER3(KLON,KLEV), &
  & ZAER4(KLON,KLEV), ZAER5(KLON,KLEV), ZAER6(KLON,KLEV), ZAER7(KLON,KLEV)   

REAL(KIND=JPRB) :: ZALP(66)
REAL(KIND=JPRB) :: ZDPN(KLON)  , ZDPO(KLON)
REAL(KIND=JPRB) :: ZFAED(21)    , ZFAEL(21)    , ZFAES(21)    , ZFAEU(21)
REAL(KIND=JPRB) :: ZGRTH(KLON)
REAL(KIND=JPRB) :: ZLON(KLON)  , ZLONR(72)    , ZNLO1(KLON) , ZNLO2(KLON)

REAL(KIND=JPRB) :: ZSILAT(KLON), ZSINR(46)    , ZGELAM(KLON)

INTEGER(KIND=JPIM) :: IMM, IMNC, IMNS, INLA, INLA1, INLA2, INLO1, INLO2, &
 & JK, JL, JLR, JMM, &
 & JNN, I_NLATR, I_NLONR, IAER  

REAL(KIND=JPRB) :: ZAETR, ZCOS1, ZCOS10, ZCOS2, ZCOS3, ZCOS4,&
 & ZCOS5, ZCOS6, ZCOS7, ZCOS8, ZCOS9, ZCPHN3, &
 & ZDPNMO, ZGRIDR, ZLATR, ZSDPN3, &
 & ZSIN, ZSIN1, ZSIN10, ZSIN2, ZSIN3, ZSIN4, &
 & ZSIN5, ZSIN6, ZSIN7, ZSIN8, ZSIN9  
REAL(KIND=JPRB) :: ZAERBC1, ZAERBC2, ZAEROR1, ZAEROR2, ZAERSD1, ZAERSD2, &
 & ZAERSS1, ZAERSS2, ZAERSU1, ZAERSU2  
REAL(KIND=JPRB) :: ZDLONR, ZCLO, ZSLO, ZEPSAER

!-- security related quantities
REAL(KIND=JPRB) :: ZEPSBGA, ZCTRBGA, ZCSTBGA
REAL(KIND=JPRB) :: ZETAC(KLEV)    , ZETAHC(0:KLEV)    , ZDPRC(KLEV)
REAL(KIND=JPRB) :: ZPRES(KLEV)    , ZPRESH(0:KLEV)    , ZEPSBG(KLEV)

REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
#include "legtri.intfb.h"
#include "gppre.intfb.h"

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

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

!*         0.     SECURITY BACKGROUND AEROSOLS

ZEPSBGA=0.001_JPRB/101325._JPRB
ZCTRBGA=0.03_JPRB/(101325._JPRB-19330._JPRB)
ZCSTBGA=0.045_JPRB/19330._JPRB
ZPRESH(KLEV)=VP00

CALL GPPRE ( 1, 1, 1, NFLEVG, VAH, VBH, ZPRESH, ZPRES )

DO JK=0,KLEV
  ZETAHC(JK)=ZPRESH(JK)/ZPRESH(KLEV)
ENDDO
DO JK=1,KLEV
  ZETAC(JK)=ZPRES(JK)/ZPRESH(KLEV)
  ZDPRC(JK)=ZPRES(JK)-ZPRES(JK-1)
  ZEPSBG(JK)=ZDPRC(JK)*ZEPSBGA
ENDDO
DO JL=KIDIA,KFDIA
 ZCLO=PCLON(JL)
 ZSLO=PSLON(JL)
 ZGELAM(JL) = SIGN(1.0_JPRB,ZCLO) * ASIN(ZSLO) - RPI *&
       & ( MIN(SIGN(1.0_JPRB,ZCLO),0.0_JPRB) + MIN(SIGN(2.0_JPRB,ZSLO),0.0_JPRB)&
       & +MIN(SIGN(1.0_JPRB,ZCLO),0.0_JPRB) * MIN(SIGN(2.0_JPRB,ZSLO),0.0_JPRB) )  
ENDDO

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

!*         1.     "NEW AEROSOL DISTRIBUTION" PARAMETERS COMPUTATIONS
!                 --------------------------------------------------

!*         1.1    VOLCANIC AEROSOL DISTRIBUTION PARAMETERS
!                 ----------------------------------------

!                 GISS CLIMATOLOGY, APPLIED TO SULPHATE AEROSOLS
!                 ----------------------------------------------

IRINT=1
ISHIFT=0
ICF=0

IF (LHVOLCA) THEN
  I_NLATR=46
  ZGRIDR=180._JPRB/(I_NLATR-1)
  DO JLR=1,I_NLATR
    ZLATR=90._JPRB-(JLR-1)*ZGRIDR
    ZSINR(JLR)=SIN(ZLATR*RPI/180._JPRB)
  ENDDO

  DO JL=KIDIA,KFDIA
    INLA=0
    ZSILAT(JL)=-9999._JPRB
    ZSIN=PSIN(JL)
    DO JLR=1,I_NLATR-1
      IF (ZSIN <= ZSINR(JLR) .AND. ZSIN > ZSINR(JLR+1)) THEN
        INLA=JLR
        ZSILAT(JL)=(ZSIN-ZSINR(INLA))/(ZSINR(INLA+1)-ZSINR(INLA))
        ZAERVO(JL)=RTAEVO(INLA)+ZSILAT(JL)*(RTAEVO(INLA+1)-RTAEVO(INLA))
      ENDIF
    ENDDO
    IF (ZSIN <= ZSINR(I_NLATR-1) .AND. ZSIN >= ZSINR(I_NLATR))THEN
      INLA=I_NLATR
      ZSILAT(JL)=(ZSIN-ZSINR(INLA-1))/(ZSINR(INLA)-ZSINR(INLA-1))
      ZAERVO(JL)=RTAEVO(INLA-1)&
       & +ZSILAT(JL)*(RTAEVO(INLA)-RTAEVO(INLA-1))  
    ENDIF
    IF (INLA == 0) THEN
      CALL ABOR1(' Problem with lat. interpolation in radact!')
    ENDIF
    ZDPO(JL)=PAPRS(JL,1)
    ZAETRO(JL)=1.0_JPRB
    ZSTRDEP(JL)=0.0_JPRB
  ENDDO

!-- define how thick the stratosphere is in pressure units
  DO JK=1,KLEV
    DO JL=KIDIA,KFDIA
      ZDPN(JL)=PAPRS(JL,JK+1)
      IF (0.5_JPRB*(PAPRS(JL,JK)+PAPRS(JL,JK+1)) < 999._JPRB) THEN
! for models with top above 10hPa
        ZAETRN(JL)=1.0_JPRB
        ZAETRO(JL)=1.0_JPRB
      ELSE
        ZAETRN(JL)=ZAETRO(JL)*(MIN(1.0_JPRB,PTH(JL,JK)/PTH(JL,JK+1)))**RCTRPT
      ENDIF
      ZAETR=SQRT(ZAETRN(JL)*ZAETRO(JL))
      ZDPNMO=ZDPN(JL)-ZDPO(JL)
      ZSTRDEP(JL)=ZSTRDEP(JL)+ZAETR*ZDPNMO
    ENDDO
    DO JL=KIDIA,KFDIA
      ZDPO(JL)  =ZDPN(JL)
      ZAETRO(JL)=ZAETRN(JL)
    ENDDO
  ENDDO
!--
  DO JL=KIDIA,KFDIA
    ZAERVO(JL)=ZAERVO(JL)/ZSTRDEP(JL)
  ENDDO

!                 TANRE ET AL. CLIMATOLOGY 
!                 ------------------------

ELSE
  DO JL=KIDIA,KFDIA
    ZAERVO(JL)=ZCSTBGA
  ENDDO
ENDIF

!*         1.2    TROPOSPHERIC AEROSOL DISTRIBUTION PARAMETERS
!                 --------------------------------------------

IF (LNEWAER) THEN
!  print *,'LNEWAER= ',LNEWAER
!  print *,' inputs        SINLAT            LONGITUDE'
!  DO JL=KIDIA,KFDIA
!    print 9001,JL,PSIN(JL),ZGELAM(JL)
  9001    format(1x,'AER_CLIMG',1x,I5,1x,2E15.8)
!  ENDDO         

!-- latitude       
  I_NLATR=46
  ZGRIDR=180._JPRB/(I_NLATR-1)
  DO JLR=1,I_NLATR
    ZLATR=90._JPRB-(JLR-1)*ZGRIDR
    ZSINR(JLR)=SIN(ZLATR*RPI/180._JPRB)
  ENDDO
  I_NLONR=72
  ZDLONR=2._JPRB*RPI/I_NLONR
  DO JLR=1,I_NLONR
    ZLONR(JLR)=(JLR-1)*ZDLONR
  ENDDO

!  print *,'Reference grid for Tegen climatology'        
!  print 9121,(ZSINR(JLR),JLR=1,NLATR)
  9121     format(1x,'ZSINR ',8E15.7)
!  print 9122,(ZLONR(JLR),JLR=1,NLONR)
  9122     format(1x,'ZLONR ',8E15.7)
           
    DO JL=KIDIA,KFDIA         
    IINLA1(JL)=0
    IINLA2(JL)=0
    ZSILAT(JL)=-9999._JPRB
    ZSIN=PSIN(JL)
    DO JLR=1,I_NLATR-1
      IF (ZSIN <= ZSINR(JLR) .AND. ZSIN > ZSINR(JLR+1)) THEN
        INLA=JLR
        IINLA1(JL)=JLR
        IINLA2(JL)=JLR+1
        ZSILAT(JL)=(ZSIN-ZSINR(INLA))/(ZSINR(INLA+1)-ZSINR(INLA))
      ENDIF
    ENDDO
    IF (ZSIN <= ZSINR(I_NLATR-1) .AND. ZSIN >= ZSINR(I_NLATR))THEN
      INLA=I_NLATR
      IINLA1(JL)=I_NLATR-1
      IINLA2(JL)=I_NLATR
      ZSILAT(JL)=(ZSIN-ZSINR(INLA-1))/(ZSINR(INLA)-ZSINR(INLA-1))
    ENDIF    
    IF (INLA == 0) THEN 
      CALL ABOR1(' Problem with lat. interpolation in radact!')
    ENDIF
!    print 9123,JL,PSIN(JL),INLA,ZSINR(INLA),ZSILAT(JL)
    9123     format(1x,'Interp.Latit.',I4,F10.7,I4,2F10.7)          
  ENDDO  

!-- longitude
  DO JL=KIDIA,KFDIA
    IINLO1(JL)=0
    IINLO2(JL)=0
    ZLON(JL)=-9999.
    DO JLR=1,71
      IF (ZGELAM(JL) < ZLONR(JLR+1) .AND. ZGELAM(JL) >= ZLONR(JLR)) &
         & THEN  
        IINLO1(JL)=JLR
        IINLO2(JL)=JLR+1
        ZNLO1(JL)=ZLONR(JLR)
        ZNLO2(JL)=ZLONR(JLR+1)
      ENDIF
    ENDDO      
    IF (ZGELAM(JL) >= ZLONR(72)) THEN
      IINLO1(JL)=72
      IINLO2(JL)= 1
      ZNLO1(JL)=ZLONR(72)
!      ZNLO2(JL)=ZLONR(72)+2.*RPI
      ZNLO2(JL)=ZLONR(72)+ZDLONR
    ENDIF
!    print 9124,JL,ZGELAM(JL),IINLO1(JL),IINLO2(JL),ZNLO1(JL),ZNLO2(JL)
    9124     format(1x,'Interp.Longit0.',I4,F10.7,2I5,2F10.7)          
  ENDDO  
 
  DO JL=KIDIA,KFDIA       
    IF (IINLO1(JL) == 0 .OR. IINLO2(JL) == 0) THEN 
      CALL ABOR1(' Problem with long. interpolation in radact!')
    ENDIF
    ZLON(JL)=(ZGELAM(JL)-ZNLO1(JL))/(ZNLO2(JL)-ZNLO1(JL))
    INLO1=IINLO1(JL)
    INLO2=IINLO2(JL)
    INLA1=IINLA1(JL)
    INLA2=IINLA2(JL)
    
    ZAERBC1=RTAEBC(INLO1,INLA1) &
     & +ZSILAT(JL)*(RTAEBC(INLO1,INLA2)-RTAEBC(INLO1,INLA1))    
    ZAERBC2=RTAEBC(INLO2,INLA1) &
     & +ZSILAT(JL)*(RTAEBC(INLO2,INLA2)-RTAEBC(INLO2,INLA1))    
    ZAERBC(JL)=ZAERBC1+ZLON(JL)*(ZAERBC2-ZAERBC1)
        
    ZAEROR1=RTAEOR(INLO1,INLA1) &
     & +ZSILAT(JL)*(RTAEOR(INLO1,INLA2)-RTAEOR(INLO1,INLA1))    
    ZAEROR2=RTAEOR(INLO2,INLA1) &
     & +ZSILAT(JL)*(RTAEOR(INLO2,INLA2)-RTAEOR(INLO2,INLA1))    
    ZAEROR(JL)=ZAEROR1+ZLON(JL)*(ZAEROR2-ZAEROR1)
        
    ZAERSD1=RTAESD(INLO1,INLA1) &
     & +ZSILAT(JL)*(RTAESD(INLO1,INLA2)-RTAESD(INLO1,INLA1))    
    ZAERSD2=RTAESD(INLO2,INLA1) &
     & +ZSILAT(JL)*(RTAESD(INLO2,INLA2)-RTAESD(INLO2,INLA1))    
    ZAERSD(JL)=ZAERSD1+ZLON(JL)*(ZAERSD2-ZAERSD1)
        
    ZAERSS1=RTAESS(INLO1,INLA1) &
     & +ZSILAT(JL)*(RTAESS(INLO1,INLA2)-RTAESS(INLO1,INLA1))    
    ZAERSS2=RTAESS(INLO2,INLA1) &
     & +ZSILAT(JL)*(RTAESS(INLO2,INLA2)-RTAESS(INLO2,INLA1))    
    ZAERSS(JL)=ZAERSS1+ZLON(JL)*(ZAERSS2-ZAERSS1)
        
    ZAERSU1=RTAESU(INLO1,INLA1) &
     & +ZSILAT(JL)*(RTAESU(INLO1,INLA2)-RTAESU(INLO1,INLA1))    
    ZAERSU2=RTAESU(INLO2,INLA1) &
     & +ZSILAT(JL)*(RTAESU(INLO2,INLA2)-RTAESU(INLO2,INLA1))    
    ZAERSU(JL)=ZAERSU1+ZLON(JL)*(ZAERSU2-ZAERSU1)
          
!    print 9125,JL,PSIN(JL),ZGELAM(JL),ZSILAT(JL) &
!     &      ,RTAESU(INLO1,INLA2),RTAESU(INLO1,INLA1),ZAERSU1 &
!     &      ,RTAESU(INLO2,INLA2),RTAESU(INLO2,INLA1),ZAERSU2 &
!     &                    ,INLA1,INLO1,INLO2,INLA2
!9125     format(1x,'Interp.Longit1.',I4,9F10.7,4I5)          
!    print 9126,JL,PSIN(JL),ZGELAM(JL),ZSILAT(JL),ZLON(JL) &
!     &      ,ZNLO1(JL),ZNLO2(JL),INLA1,INLO1,INLO2,INLA2
!9126     format(1x,'Interp.Longit2.',I4,6F10.7,4I5)          
!    print 9127,JL,ZAERBC(JL),ZAEROR(JL),ZAERSD(JL),ZAERSS(JL) &
!     &      ,ZAERSU(JL)
!9127     format(1x,'Interp.Longit3.',I4,5F10.7)          
  ENDDO       
ENDIF

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

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

!       3.     AEROSOLS
!              --------
!***
!       3.1     CALL TO LEGTRI

!***
CALL LEGTRI (ZSIN,11,66,ZALP)
!***

!       3.2     LEGENDRE TRANSFORM FOR AEROSOLS
!               -------------------------------

DO JMM=1,21
  ZFAES(JMM) = 0.0_JPRB
  ZFAEL(JMM) = 0.0_JPRB
  ZFAEU(JMM) = 0.0_JPRB
  ZFAED(JMM) = 0.0_JPRB
ENDDO
IMM  = 0
IMNC = 0
IMNS = 0
DO JMM=1,11
  IMM  = IMM+1
  DO JNN=JMM,11
    IMNC = IMNC+1
    ZFAES(IMM) = ZFAES(IMM)+ZALP(IMNC)*RAESC(IMNC)
    ZFAEL(IMM) = ZFAEL(IMM)+ZALP(IMNC)*RAELC(IMNC)
    ZFAEU(IMM) = ZFAEU(IMM)+ZALP(IMNC)*RAEUC(IMNC)
    ZFAED(IMM) = ZFAED(IMM)+ZALP(IMNC)*RAEDC(IMNC)
  ENDDO
  IF(JMM /= 1) THEN
    IMM  = IMM+1
    DO JNN=JMM,11
      IMNS = IMNS+1
      ZFAES(IMM) = ZFAES(IMM)+ZALP(IMNS+11)*RAESS(IMNS)
      ZFAEL(IMM) = ZFAEL(IMM)+ZALP(IMNS+11)*RAELS(IMNS)
      ZFAEU(IMM) = ZFAEU(IMM)+ZALP(IMNS+11)*RAEUS(IMNS)
      ZFAED(IMM) = ZFAED(IMM)+ZALP(IMNS+11)*RAEDS(IMNS)
    ENDDO
  ENDIF
ENDDO

!       3.3     FOURIER TRANSFORM FOR AEROSOLS
!               ------------------------------

DO JL=KIDIA,KFDIA
  ZCOS1    = PCLON(JL)
  ZSIN1    = PSLON(JL)
  ZCOS2    = ZCOS1*ZCOS1-ZSIN1*ZSIN1
  ZSIN2    = ZSIN1*ZCOS1+ZCOS1*ZSIN1
  ZCOS3    = ZCOS2*ZCOS1-ZSIN2*ZSIN1
  ZSIN3    = ZSIN2*ZCOS1+ZCOS2*ZSIN1
  ZCOS4    = ZCOS3*ZCOS1-ZSIN3*ZSIN1
  ZSIN4    = ZSIN3*ZCOS1+ZCOS3*ZSIN1
  ZCOS5    = ZCOS4*ZCOS1-ZSIN4*ZSIN1
  ZSIN5    = ZSIN4*ZCOS1+ZCOS4*ZSIN1
  ZCOS6    = ZCOS5*ZCOS1-ZSIN5*ZSIN1
  ZSIN6    = ZSIN5*ZCOS1+ZCOS5*ZSIN1
  ZCOS7    = ZCOS6*ZCOS1-ZSIN6*ZSIN1
  ZSIN7    = ZSIN6*ZCOS1+ZCOS6*ZSIN1
  ZCOS8    = ZCOS7*ZCOS1-ZSIN7*ZSIN1
  ZSIN8    = ZSIN7*ZCOS1+ZCOS7*ZSIN1
  ZCOS9    = ZCOS8*ZCOS1-ZSIN8*ZSIN1
  ZSIN9    = ZSIN8*ZCOS1+ZCOS8*ZSIN1
  ZCOS10   = ZCOS9*ZCOS1-ZSIN9*ZSIN1
  ZSIN10   = ZSIN9*ZCOS1+ZCOS9*ZSIN1
  ZAES(JL) = ZFAES(1) + 2.0_JPRB*&
   & ( ZFAES(2)*ZCOS1  + ZFAES(3)*ZSIN1  + ZFAES(4)*ZCOS2 &
   & + ZFAES(5)*ZSIN2  + ZFAES(6)*ZCOS3  + ZFAES(7)*ZSIN3 &
   & + ZFAES(8)*ZCOS4  + ZFAES(9)*ZSIN4  + ZFAES(10)*ZCOS5 &
   & + ZFAES(11)*ZSIN5 + ZFAES(12)*ZCOS6 + ZFAES(13)*ZSIN6 &
   & + ZFAES(14)*ZCOS7 + ZFAES(15)*ZSIN7 + ZFAES(16)*ZCOS8 &
   & + ZFAES(17)*ZSIN8 + ZFAES(18)*ZCOS9 + ZFAES(19)*ZSIN9 &
   & + ZFAES(20)*ZCOS10+ ZFAES(21)*ZSIN10                 )  
  ZAEL(JL) = ZFAEL(1) + 2.0_JPRB*&
   & ( ZFAEL(2)*ZCOS1  + ZFAEL(3)*ZSIN1  + ZFAEL(4)*ZCOS2 &
   & + ZFAEL(5)*ZSIN2  + ZFAEL(6)*ZCOS3  + ZFAEL(7)*ZSIN3 &
   & + ZFAEL(8)*ZCOS4  + ZFAEL(9)*ZSIN4  + ZFAEL(10)*ZCOS5 &
   & + ZFAEL(11)*ZSIN5 + ZFAEL(12)*ZCOS6 + ZFAEL(13)*ZSIN6 &
   & + ZFAEL(14)*ZCOS7 + ZFAEL(15)*ZSIN7 + ZFAEL(16)*ZCOS8 &
   & + ZFAEL(17)*ZSIN8 + ZFAEL(18)*ZCOS9 + ZFAEL(19)*ZSIN9 &
   & + ZFAEL(20)*ZCOS10+ ZFAEL(21)*ZSIN10                 )  
  ZAEU(JL) = ZFAEU(1) + 2.0_JPRB*&
   & ( ZFAEU(2)*ZCOS1  + ZFAEU(3)*ZSIN1  + ZFAEU(4)*ZCOS2 &
   & + ZFAEU(5)*ZSIN2  + ZFAEU(6)*ZCOS3  + ZFAEU(7)*ZSIN3 &
   & + ZFAEU(8)*ZCOS4  + ZFAEU(9)*ZSIN4  + ZFAEU(10)*ZCOS5 &
   & + ZFAEU(11)*ZSIN5 + ZFAEU(12)*ZCOS6 + ZFAEU(13)*ZSIN6 &
   & + ZFAEU(14)*ZCOS7 + ZFAEU(15)*ZSIN7 + ZFAEU(16)*ZCOS8 &
   & + ZFAEU(17)*ZSIN8 + ZFAEU(18)*ZCOS9 + ZFAEU(19)*ZSIN9 &
   & + ZFAEU(20)*ZCOS10+ ZFAEU(21)*ZSIN10                 )  
  ZAED(JL) = ZFAED(1) + 2.0_JPRB*&
   & ( ZFAED(2)*ZCOS1  + ZFAED(3)*ZSIN1  + ZFAED(4)*ZCOS2 &
   & + ZFAED(5)*ZSIN2  + ZFAED(6)*ZCOS3  + ZFAED(7)*ZSIN3 &
   & + ZFAED(8)*ZCOS4  + ZFAED(9)*ZSIN4  + ZFAED(10)*ZCOS5 &
   & + ZFAED(11)*ZSIN5 + ZFAED(12)*ZCOS6 + ZFAED(13)*ZSIN6 &
   & + ZFAED(14)*ZCOS7 + ZFAED(15)*ZSIN7 + ZFAED(16)*ZCOS8 &
   & + ZFAED(17)*ZSIN8 + ZFAED(18)*ZCOS9 + ZFAED(19)*ZSIN9 &
   & + ZFAED(20)*ZCOS10+ ZFAED(21)*ZSIN10                 )  
ENDDO

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

!*       4.      VERTICAL DISTRIBUTION
!*               ---------------------

DO JL=KIDIA,KFDIA
  ZDPO(JL)=PAPRS (JL,1)
  IF (LNEWAER) THEN
    ZAEQSO(JL)= ZAERSS(JL)*CVDAES(1)
    ZAEQLO(JL)=(ZAEROR(JL)+ZAERSU(JL))*CVDAEL(1)
    ZAEQUO(JL)= ZAERBC(JL)*CVDAEU(1)
    ZAEQDO(JL)= ZAERSD(JL)*CVDAED(1)
  ELSE
    ZAEQSO(JL)=RCAEOPS*ZAES(JL)*CVDAES(1)
    ZAEQLO(JL)=RCAEOPL*ZAEL(JL)*CVDAEL(1)
    ZAEQUO(JL)=RCAEOPU*ZAEU(JL)*CVDAEU(1)
    ZAEQDO(JL)=RCAEOPD*ZAED(JL)*CVDAED(1)
  ENDIF  
  ZAETRO(JL)=1.0_JPRB
ENDDO

DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    ZGRTH(JL)= PTH(JL,JK)/PTH(JL,JK+1)
    ZDPN(JL)=PAPRS (JL,JK+1)
    ZCPHN3=PAPRS (JL,JK+1)**3
    ZSDPN3=SQRT  (ZCPHN3)
    IF (LNEWAER) THEN
!-- new clim: sea-salt
      ZAEQSN(JL)= ZAERSS(JL)*CVDAES(JK+1)
!-- new clim: organic + sulfate
      ZAEQLN(JL)=(ZAEROR(JL)+ZAERSU(JL))*CVDAEL(JK+1)
!-- new clim: black carbon
      ZAEQUN(JL)= ZAERBC(JL)*CVDAEU(JK+1)
!-- new clim: sand dust
      ZAEQDN(JL)= ZAERSD(JL)*CVDAED(JK+1)
    ELSE
!-- old clim: maritime
      ZAEQSN(JL)=RCAEOPS*ZAES(JL)*CVDAES(JK+1)
!-- old clim: continental
      ZAEQLN(JL)=RCAEOPL*ZAEL(JL)*CVDAEL(JK+1)
!-- old clim: urban
      ZAEQUN(JL)=RCAEOPU*ZAEU(JL)*CVDAEU(JK+1)
!-- old clim: desert
      ZAEQDN(JL)=RCAEOPD*ZAED(JL)*CVDAED(JK+1)
    ENDIF  

    IF (0.5_JPRB*(PAPRS(JL,JK)+PAPRS(JL,JK+1)) < 999._JPRB) THEN
! for models with top above 10hPa
      ZAETRN(JL)=1.0_JPRB
      ZAETRO(JL)=1.0_JPRB
      ZEPSAER=ZEPSBG(JK)
    ELSE
      ZAETRN(JL)=ZAETRO(JL)*(MIN(1.0_JPRB, ZGRTH(JL) ))**RCTRPT
      ZEPSAER=1.E-10_JPRB
      ZEPSAER=ZEPSBG(JK)
    ENDIF

    ZAETR=SQRT  (ZAETRN(JL)*ZAETRO(JL))
    ZDPNMO    =ZDPN(JL)-ZDPO(JL)
! continental % organic+sulfate
!    ZAER1(JL,JK)=(1.0_JPRB-ZAETR)*(RCTRBGA*ZDPNMO+ ZAEQLN(JL)-ZAEQLO(JL))
!-- NB: tropospheric background not included in ZAER1 but in ZAER5
    ZAER1(JL,JK)=(1.0_JPRB-ZAETR)*(ZAEQLN(JL)-ZAEQLO(JL))
! maritime % sea salt
    ZAER2(JL,JK)=(1.0_JPRB-ZAETR)*(ZAEQSN(JL)-ZAEQSO(JL))
! desert dust
    ZAER3(JL,JK)=(1.0_JPRB-ZAETR)*(ZAEQDN(JL)-ZAEQDO(JL))
! urban % black carbon
    ZAER4(JL,JK)=(1.0_JPRB-ZAETR)*(ZAEQUN(JL)-ZAEQUO(JL))
! time variation applied to volcanic sulphate aerosols only - ash is background value
    ZAER7(JL,JK)=   ZAETR * RCVOBGA * ZDPNMO
!-- NB: instead of volcanic aerosols, tropospheric background not included in ZAER1
    ZAER5(JL,JK)=(1.0_JPRB-ZAETR)*ZCTRBGA*ZDPNMO
! stratospheric sulphate aerosols
    ZAER6(JL,JK)=   ZAETR * ZAERVO(JL)*ZDPNMO
!old RH dependence          
!         AADS(JL,JK)=MAX(RCAEADM, (RCAEADK(1)*PAERCLIM(JL,1,JK)
!           + RCAEADK(2)*PAERCLIM(JL,2,JK)+RCAEADK(3)*PAERCLIM(JL,3,JK))/ZDPNMO)
!**** **************************************************
!**** **************************************************
  ENDDO
  DO JL=KIDIA,KFDIA
    ZDPO(JL)  =ZDPN(JL)
    ZAEQSO(JL)=ZAEQSN(JL)
    ZAEQLO(JL)=ZAEQLN(JL)
    ZAEQUO(JL)=ZAEQUN(JL)
    ZAEQDO(JL)=ZAEQDN(JL)
    ZAETRO(JL)=ZAETRN(JL)
  ENDDO
  
!-- filling up the output arrays
  IAER=0
  IF (NTYPAER(1) /= 0) THEN
! maritime % sea salt
    IAER=IAER+1
    DO JL=KIDIA,KFDIA
      PAERCLIM(JL,JK,IAER)=MAX(ZEPSAER, ZAER2(JL,JK))
    ENDDO
! BEN - set to zero sea-salt concentration above level 10
      IF(JK < 10) then
       PAERCLIM(JL,JK,IAER)=0.0_JPRB
      ENDIF
  ENDIF
  IF (NTYPAER(2) /= 0) THEN
! desert dust
    IAER=IAER+1
    DO JL=KIDIA,KFDIA
      PAERCLIM(JL,JK,IAER)=MAX(ZEPSAER, ZAER3(JL,JK))
    ENDDO
  ENDIF
  IF (NTYPAER(3) /= 0) THEN
! continental % organic+sulfate
    IAER=IAER+1
    DO JL=KIDIA,KFDIA
      PAERCLIM(JL,JK,IAER)=MAX(ZEPSAER, ZAER1(JL,JK))
    ENDDO
  ENDIF
  IF (NTYPAER(4) /= 0) THEN
! urban % black carbon
    IAER=IAER+1
    DO JL=KIDIA,KFDIA
      PAERCLIM(JL,JK,IAER)=MAX(ZEPSAER, ZAER4(JL,JK))
    ENDDO
  ENDIF
  IF (NTYPAER(5) /= 0) THEN
! tropospheric background not included in ZAER1
    IAER=IAER+1
    DO JL=KIDIA,KFDIA
      PAERCLIM(JL,JK,IAER)=MAX(ZEPSAER, ZAER5(JL,JK))
    ENDDO
  ENDIF

  IF (NTYPAER(7) /= 0) THEN
! stratospheric background
    IAER=IAER+1
    DO JL=KIDIA,KFDIA
      PAERCLIM(JL,JK,IAER)=MAX(ZEPSAER, ZAER6(JL,JK))
    ENDDO
  ENDIF
  IF (NTYPAER(8) /= 0) THEN
!old-style volcanic 
    IAER=IAER+1
    DO JL=KIDIA,KFDIA
      PAERCLIM(JL,JK,IAER)=MAX(ZEPSAER, ZAER7(JL,JK))
    ENDDO
  ENDIF

!-- diagnostics in case of problem  
!  DO JAER=1,6
!    DO JL=KIDIA,KFDIA
!      PAERCLIM(JL,JAER,JK)=MAX(PAERCLIM(JL,JAER,JK),REPAER)
!    ENDDO
!    itot=il
!  ENDDO   
!--
   
ENDDO

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

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