SUBROUTINE m7_equil &
  & (kidia  , kfdia , klon  , klev, &
  &  prelhum, paerml, paernl, &
  &  pm6rp  , pm6dry, phplus, pww , prhop  , ptp1  )
  !
  !**** *m7_equil* calculates the ambient radii of accumulation 
  !                and coarse mode particles in presence of sea salt
  !     
  !
  !  Authors:
  !  --------
  !  E. Vignati, JRC/EI (original source)                    01/2000
  !  P. Stier, MPI      (f90-version, changes, comments)        2001
  !
  !  Purpose:
  !  --------
  !  This routine calculates the equilibrium radius of sea salt
  !  particles, and of sea salt particles mixed with sulphate for 
  !  accumulation and coarse modes. 
  !
  !  Method:
  !  -------
  !  from the mass of sea salt and sulphate (in ug/m3),
  !   
  !
  !**Interface:
  !  ----------
  !  *m7_equil* is called from *m7*
  !
  !  Externals:
  !  ----------
  !  none
  !
  !  References:
  !  -----------
  !  Jacobson, M.Z., Tabazadeh, A., Turco, R.P., (1996). Simulating
  !     equilibrium within aerosols and nonequilibrium between gases
  !     and aerosols. 
  !  Tang, I.N., (1997). Thermodynamic and optical properties of 
  !     mixed-salt aerosols of atmospheric importance. 
  !
  !@@@  ToDo:  Rewrite with identifiers of ions and ion pairs!!!!!
  !
  !--- Local variables: to be completed

  ! 
  !  Modifications:
  !  --------------
  !  20091112: JJMorcrette, Adaptation to ECMWF IFS 
  !


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

USE YO_AERO_M7 ,ONLY : naermod, nsol,    nss,     nmod,      &
  &                    wnacl,   wna2so4, wnahso4, wh2so4,    &
  &                    ddust,   dbc,     doc,     dnacl,     &
  &                    dna2so4, dnahso4, dh2so4,  dh2o,      &
  &                    ram2cmr, r_kcal,  crh,     avo

USE YOMCST    ,ONLY : RPI

  IMPLICIT NONE
  !
  !--- Parameter list:
  !
  ! phplus = molality of H+ [mol/kg(water)]
  !
  !
  !--- Local variables:
  ! 
  !  zna(:,:,i) = sodium concentration [ug m-3]
  !               i=1: accumulation mode; i=2: coarse mode
  !  zcl(:,:,i) = chlorine concentration [ug m-3]
  !               i=1: accumulation mode; i=2: coarse mode
  !
  ! !@@@ To be completed!
  !
  ! Indices for the arrays zmm and zmmr:
  !  -----------------------------------
  ! |     ions               ion pair   |
  ! |  (mole m-3)            (mole m-3) |
  ! | i   zmm(i)             zmmr(i)    |
  ! | 1   Na+                NaCl       |
  ! | 2   Cl-                NaHSO4     |
  ! | 3   SO4--              Na2SO4     |
  ! | 4   HSO4-              H2-SO4     |
  ! | 5   H+                            |
  !  -----------------------------------

  !--- Parameters:

INTEGER(KIND=JPIM),INTENT(IN) :: kidia, kfdia, klon, klev

REAL(KIND=JPRB),INTENT(IN) :: prelhum(klon ,klev),        ptp1(klon ,klev)       , &
  &                           paerml(klon ,klev,naermod), paernl(klon ,klev,nmod)

REAL(KIND=JPRB),INTENT(OUT) :: pm6rp(klon ,klev,nmod),     pm6dry(klon ,klev,nsol), &
  &                            prhop(klon ,klev,nmod),     pww(klon ,klev,nmod)

REAL(KIND=JPRB),INTENT(INOUT) :: phplus(klon ,klev,nss)       

  !--- Local variables:

INTEGER(KIND=JPIM) :: i,           jl,            jk,           jmod

REAL(KIND=JPRB) :: zaw,         zvolw,         zdryvol,      zdrymass,  &
  &                zkw,         zdryvol_mean,  zambvol_mean

REAL(KIND=JPRB) :: zmm(5),      zmmr(5),       zmol(5),      zmo(4),    &
  &                zmmt(4)
  !   
  !
REAL(KIND=JPRB) :: ZHOOK_HANDLE
!--------------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('M7_EQUIL',0,ZHOOK_HANDLE)

  DO jmod=1,nss
     DO jk=1,klev
        DO jl=kidia,kfdia

           IF ((paerml(jl,jk,jmod+12) > 1.E-15_JPRB) .AND. (paernl(jl,jk,jmod+2)>1.E-10_JPRB))  THEN
              !
              !--- 1) Dry Calculations: --------------------------------------------------
              !
              !--- 1.1) Calculate initial concentrations of the compounds in mole/m+3:
              !
              !--- Na, Cl:
              !
              zmm(1)=paerml(jl,jk,jmod+12)*1.E-6_JPRB / wnacl       ! n(Na)    [mole m-3]
              zmm(2)=paerml(jl,jk,jmod+12)*1.E-6_JPRB / wnacl       ! n(Cl)    [mole m-3]
              !      [         g m-3           ] / [g mole-1]           = [mole m-3]
              !
              !--- SO4--:
              !
              zmm(3)=paerml(jl,jk,jmod+2) *1.E+6_JPRB / avo         ! n(H2SO4) [mole m-3]
              !      [      m-3                ] / [mole-1]             = [mole m-3]
              !
              !--- HSO4-:
              !
              zmm(4)=0._JPRB                                        ! n(HSO4)  [mole m-3]
              !
              !--- 1.2) Calculation of the concentration of the different species:
              !         The ions are supposed to be arranged such that
              !         sodium is associated to sulphate first in
              !         Na2SO4, the remaining sodium is associated to Cl
              !         in form of NaCl.
              !
              zmmt(1)=zmm(1)                        ! n(Na)  
              zmmt(3)=zmm(3)                        ! n(SO4)    
              !
              zmmr(3)=MIN(zmmt(1)/2._JPRB , zmmt(3))     ! n(Na2SO4) 
              zmmt(1)=zmmt(1)-2._JPRB*zmmr(3)             ! Remaining n(Na) after association
                                                    ! with Na2SO4: n(Na)=n(Na)-2*n(Na2SO4)
              !
              zmmr(1)=MIN(zmm(2),zmmt(1))           ! n(NaCl) 
              !
              zmm(2)=zmmr(1)                        ! n(Cl) bound in NaCl, the rest is
                                                    ! assumed to evaporate in presence of SO4
              !
              zmmr(2)=0._JPRB                            ! n(NaHSO4)
              !
              zmmr(4)=zmm(3)-zmmr(2)-zmmr(3)        ! n(H2-SO4)(t)=n(H2SO4)(t0)-n(NaHSO4)-n(Na2SO4)
              !                                     ! as n(H2SO4)(t0)=n(SO4--)(t0)
              !
              !--- 1.3) Total aerosol dry volume [cm3/m3]:
              !
              zdryvol= zmmr(1)*wnacl/dnacl               + &
                &      zmmr(2)*wnahso4/dnahso4           + &
                &      zmmr(3)*wna2so4/dna2so4           + &
                &      zmmr(4)*wh2so4/dh2so4             + &
                &      paerml(jl,jk,jmod+14)*1.e-6_JPRB/ddust + &
                &      paerml(jl,jk,jmod+5) *1.e-6_JPRB/dbc   + &
                &      paerml(jl,jk,jmod+9) *1.e-6_JPRB/doc
              !
              !--- 1.4) Mean aerosol dry volume [cm+3]:
              zdryvol_mean = zdryvol    / (paernl(jl,jk,jmod+2)*1.E6_JPRB)
              ! [cm+3]     = [cm+3/m+3] /  [         m-3            ]
              !
              !--- 1.5) Dry radius [cm]:
              !
              pm6dry(jl,jk,jmod+2)=((3._JPRB/(4._JPRB*rpi))*zdryvol_mean)**(1._JPRB/3._JPRB) * ram2cmr(jmod+2)
              !
              !--- 1.6) Total aerosol dry mass [gr/m3]:
              !
              zdrymass= zmmr(1)*wnacl                    + &
                &       zmmr(2)*wnahso4                  + &
                &       zmmr(3)*wna2so4                  + &
                &       zmmr(4)*wh2so4                   + &
                &       paerml(jl,jk,jmod+14)*1.e-6_JPRB      + & ! Dust
                &       paerml(jl,jk,jmod+5)*1.e-6_JPRB       + & ! Black Carbon
                &       paerml(jl,jk,jmod+9)*1.e-6_JPRB           ! Organic Carbon
              !
              !
              !--- 2) Wet calculations: --------------------------------------------------
              !
              !--- Set threshold for relative humidity:
              !    If RH is smaller than the Critical Relative Humidity 
              !    (currently crh=0.45) the equilibrium radius is set to the dry radius:

              IF (prelhum(jl,jk) < crh) THEN
                 !
                 phplus(jl,jk,jmod)  = 0._JPRB 
                 !
                 pww(jl,jk,jmod+2)     = 0._JPRB
                 !
                 pm6rp(jl,jk,jmod+2) = pm6dry(jl,jk,jmod+2)
                 !
                 prhop(jl,jk,jmod+2) = zdrymass/zdryvol
                 !
              ELSE
                 !
                 !--- 2.1) Calculate thermodynamic properties under ambient conditions
                 !
                 !--- 2.1.1) Water activity:
                 !
                 zaw=prelhum(jl,jk)
                 !
                 !--- 2.1.2) Molality as function of the water activity:
                 !           Currently sulfate is assumed to be fully dissociated,
                 !           i.e. zmmr(2)=0. and zmo(2) is not calculated.
                 !
                 !           Changed reference to Jacobson et al. (1996):
                 !
                 !--- NaCl:
                 
                 zmo(1)=(-1.918004E2_JPRB+2.001540E3_JPRB*zaw-8.557205E3_JPRB*zaw**2           &
                   &     +1.987670E4_JPRB*zaw**3-2.717192E4_JPRB*zaw**4+2.187103E4_JPRB*zaw**5 &
                   &     -9.591577E3_JPRB*zaw**6+1.763672E3_JPRB*zaw**7                    )**2 

                 !--- NaHSO4:

                 zmo(2)=(+4.662777E0_JPRB-1.128472E1_JPRB*zaw+7.049464E1_JPRB*zaw**2           &
                   &     -2.788050E2_JPRB*zaw**3+6.103105E2_JPRB*zaw**4-7.409417E2_JPRB*zaw**5 & 
                   &     +4.614577E2_JPRB*zaw**6-1.150735E2_JPRB*zaw**7                    )**2 

                 !--- Na2SO4:

                 zmo(3)=(-3.295311E3_JPRB+3.188349E4_JPRB*zaw-1.305168E5_JPRB*zaw**2           &
                   &     +2.935608E5_JPRB*zaw**3-3.920423E5_JPRB*zaw**4+3.109519E5_JPRB*zaw**5 &
                   &     -1.356439E5_JPRB*zaw**6+2.510249E4_JPRB*zaw**7                    )**2

                 !--- H2-SO4:
                 
                 zmo(4)=(+5.611895_JPRB-1.387446E1_JPRB*zaw+1.750682E1_JPRB*zaw**2             &
                   &     +7.138146E1_JPRB*zaw**3-3.109173E2_JPRB*zaw**4+4.662288E2_JPRB*zaw**5 &
                   &     -3.128612E2_JPRB*zaw**6+7.76097E1_JPRB*zaw**7                     )**2
                 !
                 !
                 !--- 2.2) Calculation of the water content in kg water/m3 air:
                 !         (zmmr[mole/m3(air)]/zmo[mole/kg(water)] =zww[kg(water)/m3(air)]
                 !
                 pww(jl,jk,jmod+2)=zmmr(1)/zmo(1)+zmmr(2)/zmo(2)+zmmr(3)/zmo(3)+zmmr(4)/zmo(4)
                 !
                 !--- 2.3) Calculate the molality of the ions
                 !
                 !--- 2.3.1) For Na+, Cl-, SO4--, HSO4- :
                 !
                 DO i=1,4
                    zmol(i)=zmm(i)/pww(jl,jk,jmod+2)
                 ENDDO
                 !
                 !--- 2.3.2) For h+ :
                 !
                 !    [H+] = -[Na+] + [Cl-] + [OH-] + 2[SO4--] +[HSO4-]
                 !
                 !    with [OH-] = kw/[H+]
                 !
                 !--- Calculate autodissociation constant (kw) for water:
                 !    (Seinfeld &Pandis (1998): Eq. (6.5) + Table 6.5)

                 zkw=1.0E-14_JPRB*exp( (13.35_JPRB/r_kcal) * (1._JPRB/298._JPRB - 1._JPRB/ptp1(jl,jk)) )

                 !--- Calculate molality of H+:

                 zmol(5)=( (-zmol(1)+zmol(2)+2._JPRB*zmol(3)+zmol(4)) +                          &
                   &      SQRT( (zmol(1)-zmol(2)-2._JPRB*zmol(3)-zmol(4))**2 + 4._JPRB*zkw ) ) / 2._JPRB
                 !
                 !
                 zmm(5)=zmol(5)*pww(jl,jk,jmod+2)
                 !
                 phplus(jl,jk,jmod)=zmol(5)
                 !
                 !
                 !--- 2.4) Calculation of the wet radius
                 !
                 !--- 2.4.1) Total water volume  [cm3/m3]:
                 !
                 zvolw=pww(jl,jk,jmod+2)/(dh2o*1.E-3_JPRB)  ![cm3/m3]=[kg/m3]/([g/cm3]*[1.E-3 kg/g])
                 !
                 !
                 !--- 2.4.2) Mean aerosol ambient volume:
                 zambvol_mean = (zdryvol+zvolw) / (paernl(jl,jk,jmod+2)*1.E6_JPRB)
                 ! [cm+3]     = [  cm+3/m+3   ] / [         m-3             ]                 
                 
                 !--- 2.4.3) Equilibrium wet count mean radius [cm]:
                 !
                 pm6rp(jl,jk,jmod+2)=((3._JPRB/(4._JPRB*rpi))*zambvol_mean)**(1._JPRB/3._JPRB) * ram2cmr(jmod+2)
                 !
                 !--- 2.4.4) Calculation of the particle density (g cm-3):
                 !
                 prhop(jl,jk,jmod+2)=(zdrymass+zvolw*dh2o)/(zvolw+zdryvol)
                 !

              ENDIF !(prelhum(jl,jk) < crh)

           ENDIF !((paerml(jl,jk,jmod+12) > 1.E-15) .AND. (paernl(jl,jk,jmod+2)>1.E-10)) 

        ENDDO
     ENDDO
  ENDDO
  !
!--------------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('M7_EQUIL',1,ZHOOK_HANDLE)
END SUBROUTINE m7_equil
