SUBROUTINE m7_averageproperties &
  &( KIDIA , KFDIA , KLON, KLEV, &
  &  paernl, paerml, &
  &  pttn  , pm6rp , prhop)
  !
  !  Author:
  !  --------
  !  E. Vignati, JRC/EI (original source)                10/2000
  !  P. Stier, MPI      (f90-version, changes, comments)    2001

  !  JJMorcrette  20100115  Adaptation to ECMWF-IFS
  !
  !  Purpose:                                                           
  !  ---------                                                           
  !  Calculation of the mean particle mass (pttn).
  !     [molecules cm-3] for the sulphate mass
  !     [ug m-3] for the other compounds
  !
  !  Calculation of the (dry) radius and the density 
  !  of the particles of the insoluble modes.
  !
  !  Interface:
  !  ----------
  !  m7_averageproperties is called from m7
  !
  !  Externals:
  !  ----------
  !  none
  !
!-----------------------------------------------------------------------

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

USE YO_AERO_M7, ONLY : dbc,    doc,     ddust,           &  
                     & critn,  ram2cmr, nmod,   naermod, &
                     & ibcks,  ibcas,   ibccs,  ibcki,   &
                     & iocks,  iocas,   ioccs,  iocki,   &
                     & issas,  isscs,                    &
                     & iduas,  iducs,   iduai,  iduci,   &
                     & iaiti,  iacci,   icoai,           &
                     & nsol

USE YOMCST    ,ONLY : RPI

  IMPLICIT NONE
  ! 
  !--- Parameter list:
  !
  ! paerml(klon ,klev,naermod) = total aerosol mass for each compound 
  !                             [molec. cm-3 for sulfate and ug m-3 for others]
  ! paernl(klon ,klev,nmod)    = aerosol number for each mode [cm-3]
  ! pttn(klon ,klev,naermod)   = average mass for single compound in each mode 
  !                             [in molec. for sulphate and in ug for others]
  ! pm6rp(klon ,klev,nmod)     = mean mode actual radius (wet radius for soluble
  !                             modes and dry radius for insoluble modes) [cm]
  ! prhop(klon ,klev,nmod)     = mean mode particle density [g cm-3]
  !
  !--- Local variables:
  !
  ! zinsvol                   = average volume for single particle in the 
  !                             insolulbe mode [cm3]
  ! zinsmas                   = average mass for single particle in the 
  !                             insolulbe mode [g]

  !--- Parameters:

INTEGER(KIND=JPIM),INTENT(IN) :: KIDIA, KFDIA, KLON, KLEV

REAL(KIND=JPRB),INTENT(IN)  :: paerml(klon ,klev,naermod), paernl(klon ,klev,nmod)
  
REAL(KIND=JPRB),INTENT(OUT) :: pttn(klon ,klev,naermod),   pm6rp(klon ,klev,nmod),  & 
                             & prhop(klon ,klev,nmod)
  
  !--- Local variables:

INTEGER(KIND=JPIM) :: jmod,         jk,          jl
  
REAL(KIND=JPRB) :: zinsvol,      zinsmas,     zeps

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

  !--- 0) Initialization:

!!  zeps=EPSILON(1.0_JPRB)
!  zeps=100._JPRB*TINY(1.0_JPRB)
  zeps=1.E-32_JPRB

  !--- 1) Calculate mean particle masses at start of timestep: ---------------------------
  !
  !       To be able to compute a intra-modal coagulation coefficient for the nucleation
  !       mode for the case of no pre-existing particles but coagulation of freshly formed
  !       particles during the timestep, pttn is set to the mass of the critical cluster
  !       for this case. This allows to calculate an ambient radius of the 
  !       freshly formed particles and subsequently the calculation of the coagulation 
  !       coefficient. This mass is "virtual" as it is not added to the mode but used 
  !       only for the described computation of the coagulation coefficient. 
  !       !@@@ Check whether this is always fulfilled. 
  
  DO jmod=1,nsol
     DO jk=1,klev
        DO jl=KIDIA,KFDIA
           IF (paernl(jl,jk,jmod)  >  1.e-10_JPRB .AND. paerml(jl,jk,jmod)  >  1.e-15_JPRB) THEN

              pttn(jl,jk,jmod)=paerml(jl,jk,jmod)/paernl(jl,jk,jmod)

           ELSEIF (jmod == 1 .AND. paernl(jl,jk,jmod) <= 1.e-10_JPRB .AND. paerml(jl,jk,jmod) <= 1.e-15_JPRB) THEN

              pttn(jl,jk,jmod)=critn

           ENDIF
        ENDDO
     ENDDO
  ENDDO
  !
  !--- 3) Calculation of the mean mass pttn [ug] for each compound in the modes: ---------
  !       [Factor 1.E-6 to convert(ug m-3)/cm-3 into ug]
  !
  DO jmod=2,nmod
     DO jk=1,klev
        DO jl=KIDIA,KFDIA
           IF (jmod == 2) THEN
              IF (paernl(jl,jk,jmod)  >  1.e-10_JPRB .AND. paerml(jl,jk,ibcks)  >  1.e-15_JPRB) THEN
                 pttn(jl,jk,ibcks)=paerml(jl,jk,ibcks)/paernl(jl,jk,jmod)*1.E-6_JPRB
              ELSE
                 pttn(jl,jk,ibcks)=0._JPRB
              ENDIF
              IF (paernl(jl,jk,jmod)  >  1.e-10_JPRB .AND. paerml(jl,jk,iocks)  >  1.e-15_JPRB) THEN
                 pttn(jl,jk,iocks)=paerml(jl,jk,iocks)/paernl(jl,jk,jmod)*1.E-6_JPRB
              ELSE
                 pttn(jl,jk,iocks)=0._JPRB
              ENDIF
           ENDIF
           !	    
           IF (jmod == 3) THEN
              IF (paernl(jl,jk,jmod)  >  1.e-10_JPRB .AND. paerml(jl,jk,ibcas)  >  1.e-15_JPRB) THEN
                 pttn(jl,jk,ibcas)=paerml(jl,jk,ibcas)/paernl(jl,jk,jmod)*1.E-6_JPRB
              ELSE
                 pttn(jl,jk,ibcas)=0._JPRB
              ENDIF
              IF (paernl(jl,jk,jmod)  >  1.e-10_JPRB .AND. paerml(jl,jk,iocas)  >  1.e-15_JPRB) THEN
                 pttn(jl,jk,iocas)=paerml(jl,jk,iocas)/paernl(jl,jk,jmod)*1.E-6_JPRB
              ELSE
                 pttn(jl,jk,iocas)=0._JPRB
              ENDIF
              IF (paernl(jl,jk,jmod)  >  1.e-10_JPRB .AND. paerml(jl,jk,issas)  >  1.e-15_JPRB) THEN
                 pttn(jl,jk,issas)=paerml(jl,jk,issas)/paernl(jl,jk,jmod)*1.E-6_JPRB
              ELSE
                 pttn(jl,jk,issas)=0._JPRB
              ENDIF
              IF (paernl(jl,jk,jmod)  >  1.e-10_JPRB .AND. paerml(jl,jk,iduas)  >  1.e-15_JPRB) THEN
                 pttn(jl,jk,iduas)=paerml(jl,jk,iduas)/paernl(jl,jk,jmod)*1.E-6_JPRB
              ELSE
                 pttn(jl,jk,iduas)=0._JPRB
              ENDIF
           ENDIF
           !	    
           IF (jmod == 4) THEN
              IF (paernl(jl,jk,jmod)  >  1.e-10_JPRB .AND. paerml(jl,jk,ibccs)  >  1.e-15_JPRB) THEN
                 pttn(jl,jk,ibccs)=paerml(jl,jk,ibccs)/paernl(jl,jk,jmod)*1.E-6_JPRB
              ELSE
                 pttn(jl,jk,ibccs)=0._JPRB
              ENDIF
              IF (paernl(jl,jk,jmod)  >  1.e-10_JPRB .AND. paerml(jl,jk,ioccs)  >  1.e-15_JPRB) THEN
                 pttn(jl,jk,ioccs)=paerml(jl,jk,ioccs)/paernl(jl,jk,jmod)*1.E-6_JPRB
              ELSE
                 pttn(jl,jk,ioccs)=0._JPRB
              ENDIF
              IF (paernl(jl,jk,jmod)  >  1.e-10_JPRB .AND. paerml(jl,jk,isscs)  >  1.e-15_JPRB) THEN
                 pttn(jl,jk,isscs)=paerml(jl,jk,isscs)/paernl(jl,jk,jmod)*1.E-6_JPRB
              ELSE
                 pttn(jl,jk,isscs)=0._JPRB
              ENDIF
              IF (paernl(jl,jk,jmod)  >  1.e-10_JPRB .AND. paerml(jl,jk,iducs)  >  1.e-15_JPRB) THEN
                 pttn(jl,jk,iducs)=paerml(jl,jk,iducs)/paernl(jl,jk,jmod)*1.E-6_JPRB
              ELSE
                 pttn(jl,jk,iducs)=0._JPRB
              ENDIF
           ENDIF
           !	    
           IF (jmod == 5) THEN
              IF (paernl(jl,jk,jmod)  >  1.e-10_JPRB .AND. paerml(jl,jk,ibcki)  >  1.e-15_JPRB) THEN
                 pttn(jl,jk,ibcki)=paerml(jl,jk,ibcki)/paernl(jl,jk,jmod)*1.E-6_JPRB
              ELSE
                 pttn(jl,jk,ibcki)=0._JPRB
              ENDIF
              IF (paernl(jl,jk,jmod)  >  1.e-10_JPRB .AND. paerml(jl,jk,iocki)  >  1.e-15_JPRB) THEN
                 pttn(jl,jk,iocki)=paerml(jl,jk,iocki)/paernl(jl,jk,jmod)*1.E-6_JPRB
              ELSE
                 pttn(jl,jk,iocki)=0._JPRB
              ENDIF
           ENDIF
           !	    
           IF (jmod == 6) THEN
              IF (paernl(jl,jk,jmod)  >  1.e-10_JPRB .AND. paerml(jl,jk,iduai)  >  1.e-15_JPRB) THEN
                 pttn(jl,jk,iduai)=paerml(jl,jk,iduai)/paernl(jl,jk,jmod)*1.E-6_JPRB
              ELSE
                 pttn(jl,jk,iduai)=0._JPRB
              ENDIF
           ENDIF
           !
           IF (jmod == 7) THEN
              IF (paernl(jl,jk,jmod)  >  1.e-10_JPRB .AND. paerml(jl,jk,iduci)  >  1.e-15_JPRB) THEN
                 pttn(jl,jk,iduci)=paerml(jl,jk,iduci)/paernl(jl,jk,jmod)*1.E-6_JPRB
              ELSE
                 pttn(jl,jk,iduci)=0._JPRB
              ENDIF
           ENDIF
        ENDDO
     ENDDO
  ENDDO

  !
  !--- 4) Calculate count median radii for lognormal distribution from -------------------
  !       mass for insoluble modes:

  DO jk=1,klev
     DO jl=KIDIA,KFDIA

        !--- 4.1) Aitken mode insoluble:

        zinsmas=1.e-6_JPRB*(pttn(jl,jk,ibcki)+pttn(jl,jk,iocki))
        zinsvol=1.e-6_JPRB*(pttn(jl,jk,ibcki)/dbc+pttn(jl,jk,iocki)/doc)
        IF (zinsvol > zeps) THEN
           prhop(jl,jk,iaiti)=zinsmas/zinsvol
           pm6rp(jl,jk,iaiti)=(0.75_JPRB/RPI*1.e-6_JPRB* &
                            &  (pttn(jl,jk,ibcki)/dbc+pttn(jl,jk,iocki)/doc))**(1._JPRB/3._JPRB)*ram2cmr(iaiti)
        ELSE
           prhop(jl,jk,iaiti)=0._JPRB
           pm6rp(jl,jk,iaiti)=0._JPRB
        ENDIF

        !--- 4.2) Accumulation mode insoluble:

        IF (pttn(jl,jk,iduai) > zeps) THEN 
           prhop(jl,jk,iacci)=ddust
           pm6rp(jl,jk,iacci)=(0.75_JPRB/RPI*1.e-6_JPRB*pttn(jl,jk,iduai)/ddust)**(1._JPRB/3._JPRB)*ram2cmr(iacci)
        ELSE
           prhop(jl,jk,iacci)=0._JPRB
           pm6rp(jl,jk,iacci)=0._JPRB  
        ENDIF

        !--- 4.3) Coarse mode insoluble:

        IF (pttn(jl,jk,iduci) > zeps) THEN
           prhop(jl,jk,icoai)=ddust
           pm6rp(jl,jk,icoai)=(0.75_JPRB/RPI*1.e-6_JPRB*pttn(jl,jk,iduci)/ddust)**(1._JPRB/3._JPRB)*ram2cmr(icoai)
        ELSE
           prhop(jl,jk,icoai)=0._JPRB
           pm6rp(jl,jk,icoai)=0._JPRB
        ENDIF

     ENDDO
  ENDDO

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