source: palm/trunk/SOURCE/salsa_mod.f90 @ 3473

Last change on this file since 3473 was 3473, checked in by suehring, 7 years ago

Bugfix for previous commit

  • Property svn:keywords set to Id
File size: 509.6 KB
Line 
1!> @file salsa_mod.f90
2!--------------------------------------------------------------------------------!
3! This file is part of PALM-4U.
4!
5! PALM-4U is free software: you can redistribute it and/or modify it under the
6! terms of the GNU General Public License as published by the Free Software
7! Foundation, either version 3 of the License, or (at your option) any later
8! version.
9!
10! PALM-4U is distributed in the hope that it will be useful, but WITHOUT ANY
11! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13!
14! You should have received a copy of the GNU General Public License along with
15! PALM. If not, see <http://www.gnu.org/licenses/>.
16!
17! Copyright 1997-2018 Leibniz Universitaet Hannover
18!--------------------------------------------------------------------------------!
19!
20! Current revisions:
21! -----------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: salsa_mod.f90 3473 2018-10-30 20:50:15Z suehring $
27! NetCDF input routine renamed
28!
29! 3467 2018-10-30 19:05:21Z suehring
30! Initial revision
31!
32! 3412 2018-10-24 07:25:57Z monakurppa
33!
34! Authors:
35! --------
36! @author monakurppa
37!
38!
39! Description:
40! ------------
41!> Sectional aerosol module for large scale applications SALSA
42!> (Kokkola et al., 2008, ACP 8, 2469-2483). Solves the aerosol number and mass
43!> concentration as well as chemical composition. Includes aerosol dynamic
44!> processes: nucleation, condensation/evaporation of vapours, coagulation and
45!> deposition on tree leaves, ground and roofs.
46!> Implementation is based on formulations implemented in UCLALES-SALSA except
47!> for deposition which is based on parametrisations by Zhang et al. (2001,
48!> Atmos. Environ. 35, 549-560) or Petroff&Zhang (2010, Geosci. Model Dev. 3,
49!> 753-769)
50!>
51!> @todo Implement turbulent inflow of aerosols in inflow_turbulence.
52!> @todo Deposition on walls and horizontal surfaces calculated from the aerosol
53!>       dry radius, not wet
54!> @todo Deposition on subgrid scale vegetation
55!> @todo Deposition on vegetation calculated by default for deciduous broadleaf
56!>       trees
57!> @todo Revise masked data output. There is a potential bug in case of
58!>       terrain-following masked output, according to data_output_mask.
59!> @todo There are now improved interfaces for NetCDF data input which can be
60!>       used instead of get variable etc.
61!------------------------------------------------------------------------------!
62 MODULE salsa_mod
63
64    USE basic_constants_and_equations_mod,                                     &
65        ONLY:  c_p, g, p_0, pi, r_d
66 
67    USE chemistry_model_mod,                                                   &
68        ONLY:  chem_species, nspec, nvar, spc_names
69
70    USE chem_modules,                                                          &
71        ONLY:  call_chem_at_all_substeps, chem_gasphase_on
72
73    USE control_parameters
74       
75    USE indices,                                                               &
76        ONLY:  nbgp, nx, nxl, nxlg, nxr, nxrg, ny, nyn, nyng, nys, nysg, nzb,  &
77               nzb_s_inner, nz, nzt, wall_flags_0
78     
79    USE kinds
80   
81    USE pegrid
82   
83    USE salsa_util_mod
84
85    IMPLICIT NONE
86!
87!-- SALSA constants:
88!
89!-- Local constants:
90    INTEGER(iwp), PARAMETER ::  ngast   = 5 !< total number of gaseous tracers:
91                                            !< 1 = H2SO4, 2 = HNO3, 3 = NH3,
92                                            !< 4 = OCNV (non-volatile OC),
93                                            !< 5 = OCSV (semi-volatile) 
94    INTEGER(iwp), PARAMETER ::  nmod    = 7 !< number of modes for initialising
95                                            !< the aerosol size distribution                                             
96    INTEGER(iwp), PARAMETER ::  nreg    = 2 !< Number of main size subranges
97    INTEGER(iwp), PARAMETER ::  maxspec = 7 !< Max. number of aerosol species
98!   
99!-- Universal constants
100    REAL(wp), PARAMETER ::  abo    = 1.380662E-23_wp  !< Boltzmann constant (J/K)
101    REAL(wp), PARAMETER ::  alv    = 2.260E+6_wp      !< latent heat for H2O
102                                                      !< vaporisation (J/kg)
103    REAL(wp), PARAMETER ::  alv_d_rv  = 4896.96865_wp !< alv / rv
104    REAL(wp), PARAMETER ::  am_airmol = 4.8096E-26_wp !< Average mass of one air
105                                                      !< molecule (Jacobson,
106                                                      !< 2005, Eq. 2.3)                                                   
107    REAL(wp), PARAMETER ::  api6   = 0.5235988_wp     !< pi / 6   
108    REAL(wp), PARAMETER ::  argas  = 8.314409_wp      !< Gas constant (J/(mol K))
109    REAL(wp), PARAMETER ::  argas_d_cpd = 8.281283865E-3_wp !< argas per cpd
110    REAL(wp), PARAMETER ::  avo    = 6.02214E+23_wp   !< Avogadro constant (1/mol)
111    REAL(wp), PARAMETER ::  d_sa   = 5.539376964394570E-10_wp !< diameter of
112                                                      !< condensing sulphuric
113                                                      !< acid molecule (m) 
114    REAL(wp), PARAMETER ::  for_ppm_to_nconc =  7.243016311E+16_wp !<
115                                                 !< ppm * avo / R (K/(Pa*m3))
116    REAL(wp), PARAMETER ::  epsoc  = 0.15_wp          !< water uptake of organic
117                                                      !< material     
118    REAL(wp), PARAMETER ::  mclim  = 1.0E-23_wp    !< mass concentration min
119                                                   !< limit for aerosols (kg/m3)                                                   
120    REAL(wp), PARAMETER ::  n3     = 158.79_wp !< Number of H2SO4 molecules in
121                                               !< 3 nm cluster if d_sa=5.54e-10m
122    REAL(wp), PARAMETER ::  nclim  = 1.0_wp    !< number concentration min limit
123                                               !< for aerosols and gases (#/m3)
124    REAL(wp), PARAMETER ::  surfw0 = 0.073_wp  !< surface tension of pure water
125                                               !< at ~ 293 K (J/m2)   
126    REAL(wp), PARAMETER ::  vclim  = 1.0E-24_wp    !< volume concentration min
127                                                   !< limit for aerosols (m3/m3)                                           
128!-- Molar masses in kg/mol
129    REAL(wp), PARAMETER ::  ambc   = 12.0E-3_wp     !< black carbon (BC)
130    REAL(wp), PARAMETER ::  amdair = 28.970E-3_wp   !< dry air
131    REAL(wp), PARAMETER ::  amdu   = 100.E-3_wp     !< mineral dust
132    REAL(wp), PARAMETER ::  amh2o  = 18.0154E-3_wp  !< H2O
133    REAL(wp), PARAMETER ::  amh2so4  = 98.06E-3_wp  !< H2SO4
134    REAL(wp), PARAMETER ::  amhno3 = 63.01E-3_wp    !< HNO3
135    REAL(wp), PARAMETER ::  amn2o  = 44.013E-3_wp   !< N2O
136    REAL(wp), PARAMETER ::  amnh3  = 17.031E-3_wp   !< NH3
137    REAL(wp), PARAMETER ::  amo2   = 31.9988E-3_wp  !< O2
138    REAL(wp), PARAMETER ::  amo3   = 47.998E-3_wp   !< O3
139    REAL(wp), PARAMETER ::  amoc   = 150.E-3_wp     !< organic carbon (OC)
140    REAL(wp), PARAMETER ::  amss   = 58.44E-3_wp    !< sea salt (NaCl)
141!-- Densities in kg/m3
142    REAL(wp), PARAMETER ::  arhobc     = 2000.0_wp !< black carbon
143    REAL(wp), PARAMETER ::  arhodu     = 2650.0_wp !< mineral dust
144    REAL(wp), PARAMETER ::  arhoh2o    = 1000.0_wp !< H2O
145    REAL(wp), PARAMETER ::  arhoh2so4  = 1830.0_wp !< SO4
146    REAL(wp), PARAMETER ::  arhohno3   = 1479.0_wp !< HNO3
147    REAL(wp), PARAMETER ::  arhonh3    = 1530.0_wp !< NH3
148    REAL(wp), PARAMETER ::  arhooc     = 2000.0_wp !< organic carbon
149    REAL(wp), PARAMETER ::  arhoss     = 2165.0_wp !< sea salt (NaCl)
150!-- Volume of molecule in m3/#
151    REAL(wp), PARAMETER ::  amvh2o   = amh2o /avo / arhoh2o      !< H2O
152    REAL(wp), PARAMETER ::  amvh2so4 = amh2so4 / avo / arhoh2so4 !< SO4
153    REAL(wp), PARAMETER ::  amvhno3  = amhno3 / avo / arhohno3   !< HNO3
154    REAL(wp), PARAMETER ::  amvnh3   = amnh3 / avo / arhonh3     !< NH3 
155    REAL(wp), PARAMETER ::  amvoc    = amoc / avo / arhooc       !< OC
156    REAL(wp), PARAMETER ::  amvss    = amss / avo / arhoss       !< sea salt
157   
158!
159!-- SALSA switches:
160    INTEGER(iwp) ::  nj3 = 1 !< J3 parametrization (nucleation)
161                             !< 1 = condensational sink (Kerminen&Kulmala, 2002)
162                             !< 2 = coagulational sink (Lehtinen et al. 2007)
163                             !< 3 = coagS+self-coagulation (Anttila et al. 2010)                                       
164    INTEGER(iwp) ::  nsnucl = 0 !< Choice of the nucleation scheme:
165                                !< 0 = off   
166                                !< 1 = binary nucleation
167                                !< 2 = activation type nucleation
168                                !< 3 = kinetic nucleation
169                                !< 4 = ternary nucleation
170                                !< 5 = nucleation with ORGANICs
171                                !< 6 = activation type of nucleation with
172                                !<     H2SO4+ORG
173                                !< 7 = heteromolecular nucleation with H2SO4*ORG
174                                !< 8 = homomolecular nucleation of  H2SO4 +
175                                !<     heteromolecular nucleation with H2SO4*ORG
176                                !< 9 = homomolecular nucleation of  H2SO4 and ORG
177                                !<     +heteromolecular nucleation with H2SO4*ORG
178    LOGICAL ::  advect_particle_water = .TRUE.  !< advect water concentration of
179                                                !< particles                               
180    LOGICAL ::  decycle_lr            = .FALSE. !< Undo cyclic boundary
181                                                !< conditions: left and right
182    LOGICAL ::  decycle_ns            = .FALSE. !< north and south boundaries
183    LOGICAL ::  feedback_to_palm      = .FALSE. !< allow feedback due to
184                                                !< hydration and/or condensation
185                                                !< of H20
186    LOGICAL ::  no_insoluble          = .FALSE. !< Switch to exclude insoluble 
187                                                !< chemical components
188    LOGICAL ::  read_restart_data_salsa = .FALSE. !< read restart data for salsa
189    LOGICAL ::  salsa                 = .FALSE.   !< SALSA master switch
190    LOGICAL ::  salsa_gases_from_chem = .FALSE.   !< Transfer the gaseous
191                                                  !< components to SALSA from 
192                                                  !< from chemistry model
193    LOGICAL ::  van_der_waals_coagc   = .FALSE.   !< Enhancement of coagulation
194                                                  !< kernel by van der Waals and
195                                                  !< viscous forces
196    LOGICAL ::  write_binary_salsa    = .FALSE.   !< read binary for salsa
197!-- Process switches: nl* is read from the NAMELIST and is NOT changed.
198!--                   ls* is the switch used and will get the value of nl*
199!--                       except for special circumstances (spinup period etc.)
200    LOGICAL ::  nlcoag       = .FALSE. !< Coagulation master switch
201    LOGICAL ::  lscoag       = .FALSE. !<
202    LOGICAL ::  nlcnd        = .FALSE. !< Condensation master switch
203    LOGICAL ::  lscnd        = .FALSE. !<
204    LOGICAL ::  nlcndgas     = .FALSE. !< Condensation of precursor gases
205    LOGICAL ::  lscndgas     = .FALSE. !<
206    LOGICAL ::  nlcndh2oae   = .FALSE. !< Condensation of H2O on aerosol
207    LOGICAL ::  lscndh2oae   = .FALSE. !< particles (FALSE -> equilibrium calc.)
208    LOGICAL ::  nldepo       = .FALSE. !< Deposition master switch
209    LOGICAL ::  lsdepo       = .FALSE. !<
210    LOGICAL ::  nldepo_topo  = .FALSE. !< Deposition on vegetation master switch
211    LOGICAL ::  lsdepo_topo  = .FALSE. !<
212    LOGICAL ::  nldepo_vege  = .FALSE. !< Deposition on walls master switch
213    LOGICAL ::  lsdepo_vege  = .FALSE. !<
214    LOGICAL ::  nldistupdate = .TRUE.  !< Size distribution update master switch                                     
215    LOGICAL ::  lsdistupdate = .FALSE. !<                                     
216!
217!-- SALSA variables:
218    CHARACTER (LEN=20) ::  bc_salsa_b = 'neumann'   !< bottom boundary condition                                     
219    CHARACTER (LEN=20) ::  bc_salsa_t = 'neumann'   !< top boundary condition
220    CHARACTER (LEN=20) ::  depo_vege_type = 'zhang2001' !< or 'petroff2010'
221    CHARACTER (LEN=20) ::  depo_topo_type = 'zhang2001' !< or 'petroff2010'
222    CHARACTER (LEN=20), DIMENSION(4) ::  decycle_method = & 
223                             (/'dirichlet','dirichlet','dirichlet','dirichlet'/)
224                                 !< Decycling method at horizontal boundaries,
225                                 !< 1=left, 2=right, 3=south, 4=north
226                                 !< dirichlet = initial size distribution and
227                                 !< chemical composition set for the ghost and
228                                 !< first three layers
229                                 !< neumann = zero gradient
230    CHARACTER (LEN=3), DIMENSION(maxspec) ::  listspec = &  !< Active aerosols
231                                   (/'SO4','   ','   ','   ','   ','   ','   '/)
232    CHARACTER (LEN=20) ::  salsa_source_mode = 'no_source' 
233                                                    !< 'read_from_file',
234                                                    !< 'constant' or 'no_source'                                   
235    INTEGER(iwp) ::  dots_salsa = 0  !< starting index for salsa-timeseries
236    INTEGER(iwp) ::  fn1a = 1    !< last index for bin subranges:  subrange 1a
237    INTEGER(iwp) ::  fn2a = 1    !<                              subrange 2a
238    INTEGER(iwp) ::  fn2b = 1    !<                              subrange 2b
239    INTEGER(iwp), DIMENSION(ngast) ::  gas_index_chem = (/ 1, 1, 1, 1, 1/) !<
240                                 !< Index of gaseous compounds in the chemistry
241                                 !< model. In SALSA, 1 = H2SO4, 2 = HNO3,
242                                 !< 3 = NH3, 4 = OCNV, 5 = OCSV
243    INTEGER(iwp) ::  ibc_salsa_b !<
244    INTEGER(iwp) ::  ibc_salsa_t !<
245    INTEGER(iwp) ::  igctyp = 0  !< Initial gas concentration type
246                                 !< 0 = uniform (use H2SO4_init, HNO3_init,
247                                 !<     NH3_init, OCNV_init and OCSV_init)
248                                 !< 1 = read vertical profile from an input file 
249    INTEGER(iwp) ::  in1a = 1    !< start index for bin subranges: subrange 1a
250    INTEGER(iwp) ::  in2a = 1    !<                              subrange 2a
251    INTEGER(iwp) ::  in2b = 1    !<                              subrange 2b
252    INTEGER(iwp) ::  isdtyp = 0  !< Initial size distribution type
253                                 !< 0 = uniform
254                                 !< 1 = read vertical profile of the mode number
255                                 !<     concentration from an input file 
256    INTEGER(iwp) ::  ibc  = -1 !< Indice for: black carbon (BC)
257    INTEGER(iwp) ::  idu  = -1 !< dust
258    INTEGER(iwp) ::  inh  = -1 !< NH3
259    INTEGER(iwp) ::  ino  = -1 !< HNO3   
260    INTEGER(iwp) ::  ioc  = -1 !< organic carbon (OC)
261    INTEGER(iwp) ::  iso4 = -1 !< SO4 or H2SO4   
262    INTEGER(iwp) ::  iss  = -1 !< sea salt
263    INTEGER(iwp) ::  lod_aero = 0   !< level of detail for aerosol emissions
264    INTEGER(iwp) ::  lod_gases = 0  !< level of detail for gaseous emissions   
265    INTEGER(iwp), DIMENSION(nreg) ::  nbin = (/ 3, 7/)    !< Number of size bins
266                                               !< for each aerosol size subrange
267    INTEGER(iwp) ::  nbins = 1  !< total number of size bins
268    INTEGER(iwp) ::  ncc   = 1  !< number of chemical components used     
269    INTEGER(iwp) ::  ncc_tot = 1!< total number of chemical compounds (ncc+1
270                                !< if particle water is advected)
271    REAL(wp) ::  act_coeff = 1.0E-7_wp     !< Activation coefficient
272    REAL(wp) ::  aerosol_source = 0.0_wp   !< Constant aerosol flux (#/(m3*s))
273    REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::  emission_mass_fracs  !< array for
274                                    !< aerosol composition per emission category
275                                    !< 1:SO4 2:OC 3:BC 4:DU 5:SS 6:NO 7:NH 
276    REAL(wp) ::  dt_salsa  = 0.00001_wp    !< Time step of SALSA
277    REAL(wp) ::  H2SO4_init = nclim        !< Init value for sulphuric acid gas
278    REAL(wp) ::  HNO3_init  = nclim        !< Init value for nitric acid gas
279    REAL(wp) ::  last_salsa_time = 0.0_wp  !< time of the previous salsa
280                                           !< timestep
281    REAL(wp) ::  nf2a = 1.0_wp             !< Number fraction allocated to a-
282                                           !< bins in subrange 2
283                                           !< (b-bins will get 1-nf2a)   
284    REAL(wp) ::  NH3_init  = nclim         !< Init value for ammonia gas
285    REAL(wp) ::  OCNV_init = nclim         !< Init value for non-volatile
286                                           !< organic gases
287    REAL(wp) ::  OCSV_init = nclim         !< Init value for semi-volatile
288                                           !< organic gases
289    REAL(wp), DIMENSION(nreg+1) ::  reglim = & !< Min&max diameters of size subranges
290                                 (/ 3.0E-9_wp, 5.0E-8_wp, 1.0E-5_wp/)
291    REAL(wp) ::  rhlim = 1.20_wp    !< RH limit in %/100. Prevents
292                                    !< unrealistically high RH in condensation                           
293    REAL(wp) ::  skip_time_do_salsa = 0.0_wp !< Starting time of SALSA (s)
294!-- Initial log-normal size distribution: mode diameter (dpg, micrometres),
295!-- standard deviation (sigmag) and concentration (n_lognorm, #/cm3)
296    REAL(wp), DIMENSION(nmod) ::  dpg   = (/0.013_wp, 0.054_wp, 0.86_wp,       &
297                                            0.2_wp, 0.2_wp, 0.2_wp, 0.2_wp/) 
298    REAL(wp), DIMENSION(nmod) ::  sigmag  = (/1.8_wp, 2.16_wp, 2.21_wp,        &
299                                              2.0_wp, 2.0_wp, 2.0_wp, 2.0_wp/) 
300    REAL(wp), DIMENSION(nmod) ::  n_lognorm = (/1.04e+5_wp, 3.23E+4_wp, 5.4_wp,&
301                                                0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp/)
302!-- Initial mass fractions / chemical composition of the size distribution   
303    REAL(wp), DIMENSION(maxspec) ::  mass_fracs_a = & !< mass fractions between
304             (/1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0/) !< aerosol species for A bins
305    REAL(wp), DIMENSION(maxspec) ::  mass_fracs_b = & !< mass fractions between
306             (/0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0/) !< aerosol species for B bins
307             
308    REAL(wp), ALLOCATABLE, DIMENSION(:) ::  bin_low_limits  !< to deliver
309                                                            !< information about
310                                                            !< the lower
311                                                            !< diameters per bin                                       
312    REAL(wp), ALLOCATABLE, DIMENSION(:) ::  nsect     !< Background number
313                                                      !< concentration per bin
314    REAL(wp), ALLOCATABLE, DIMENSION(:) ::  massacc   !< Mass accomodation
315                                                      !< coefficients per bin                                             
316!
317!-- SALSA derived datatypes:
318!
319!-- Prognostic variable: Aerosol size bin information (number (#/m3) and
320!-- mass (kg/m3) concentration) and the concentration of gaseous tracers (#/m3).
321!-- Gas tracers are contained sequentially in dimension 4 as:
322!-- 1. H2SO4, 2. HNO3, 3. NH3, 4. OCNV (non-volatile organics),
323!-- 5. OCSV (semi-volatile)
324    TYPE salsa_variable
325       REAL(wp), POINTER, DIMENSION(:,:,:), CONTIGUOUS     ::  conc
326       REAL(wp), POINTER, DIMENSION(:,:,:), CONTIGUOUS     ::  conc_p
327       REAL(wp), POINTER, DIMENSION(:,:,:), CONTIGUOUS     ::  tconc_m
328       REAL(wp), ALLOCATABLE, DIMENSION(:,:)   ::  flux_s, diss_s
329       REAL(wp), ALLOCATABLE, DIMENSION(:,:,:) ::  flux_l, diss_l
330       REAL(wp), ALLOCATABLE, DIMENSION(:)     ::  init
331       REAL(wp), ALLOCATABLE, DIMENSION(:,:,:) ::  source
332       REAL(wp), ALLOCATABLE, DIMENSION(:,:)   ::  sums_ws_l
333    END TYPE salsa_variable
334   
335!-- Map bin indices between parallel size distributions   
336    TYPE t_parallelbin
337       INTEGER(iwp) ::  cur  ! Index for current distribution
338       INTEGER(iwp) ::  par  ! Index for corresponding parallel distribution
339    END TYPE t_parallelbin
340   
341!-- Datatype used to store information about the binned size distributions of
342!-- aerosols
343    TYPE t_section
344       REAL(wp) ::  vhilim   !< bin volume at the high limit
345       REAL(wp) ::  vlolim   !< bin volume at the low limit
346       REAL(wp) ::  vratiohi !< volume ratio between the center and high limit
347       REAL(wp) ::  vratiolo !< volume ratio between the center and low limit
348       REAL(wp) ::  dmid     !< bin middle diameter (m)
349       !******************************************************
350       ! ^ Do NOT change the stuff above after initialization !
351       !******************************************************
352       REAL(wp) ::  dwet    !< Wet diameter or mean droplet diameter (m)
353       REAL(wp), DIMENSION(maxspec+1) ::  volc !< Volume concentrations
354                            !< (m^3/m^3) of aerosols + water. Since most of
355                            !< the stuff in SALSA is hard coded, these *have to
356                            !< be* in the order
357                            !< 1:SO4, 2:OC, 3:BC, 4:DU, 5:SS, 6:NO, 7:NH, 8:H2O
358       REAL(wp) ::  veqh2o  !< Equilibrium H2O concentration for each particle
359       REAL(wp) ::  numc    !< Number concentration of particles/droplets (#/m3)
360       REAL(wp) ::  core    !< Volume of dry particle
361    END TYPE t_section 
362!
363!-- Local aerosol properties in SALSA
364    TYPE(t_section), ALLOCATABLE ::  aero(:)
365!
366!-- SALSA tracers:
367!-- Tracers as x = x(k,j,i,bin). The 4th dimension contains all the size bins
368!-- sequentially for each aerosol species  + water.
369!
370!-- Prognostic tracers:
371!
372!-- Number concentration (#/m3)
373    TYPE(salsa_variable), ALLOCATABLE, DIMENSION(:), TARGET ::  aerosol_number
374    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:), TARGET ::  nconc_1
375    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:), TARGET ::  nconc_2
376    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:), TARGET ::  nconc_3
377!
378!-- Mass concentration (kg/m3)
379    TYPE(salsa_variable), ALLOCATABLE, DIMENSION(:), TARGET ::  aerosol_mass
380    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:), TARGET ::  mconc_1
381    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:), TARGET ::  mconc_2
382    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:), TARGET ::  mconc_3
383!
384!-- Gaseous tracers (#/m3)
385    TYPE(salsa_variable), ALLOCATABLE, DIMENSION(:), TARGET ::  salsa_gas
386    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:), TARGET ::  gconc_1
387    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:), TARGET ::  gconc_2
388    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:), TARGET ::  gconc_3
389!
390!-- Diagnostic tracers
391    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:) ::  sedim_vd !< sedimentation
392                                                           !< velocity per size
393                                                           !< bin (m/s)
394    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:) ::  Ra_dry !< dry radius (m)
395   
396!-- Particle component index tables
397    TYPE(component_index) :: prtcl !< Contains "getIndex" which gives the index
398                                   !< for a given aerosol component name, i.e.
399                                   !< 1:SO4, 2:OC, 3:BC, 4:DU,
400                                   !< 5:SS, 6:NO, 7:NH, 8:H2O 
401!                                   
402!-- Data output arrays:
403!-- Gases:
404    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:), TARGET ::  g_H2SO4_av  !< H2SO4
405    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:), TARGET ::  g_HNO3_av   !< HNO3
406    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:), TARGET ::  g_NH3_av    !< NH3
407    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:), TARGET ::  g_OCNV_av   !< non-vola-
408                                                                    !< tile OC
409    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:), TARGET ::  g_OCSV_av   !< semi-vol.
410                                                                    !< OC
411!-- Integrated:                                                                   
412    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:) ::  LDSA_av  !< lung deposited
413                                                         !< surface area                                                   
414    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:) ::  Ntot_av  !< total number conc.
415    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:) ::  PM25_av  !< PM2.5
416    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:) ::  PM10_av  !< PM10
417!-- In the particle phase:   
418    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:), TARGET ::  s_BC_av  !< black carbon
419    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:), TARGET ::  s_DU_av  !< dust
420    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:), TARGET ::  s_H2O_av !< liquid water
421    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:), TARGET ::  s_NH_av  !< ammonia
422    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:), TARGET ::  s_NO_av  !< nitrates
423    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:), TARGET ::  s_OC_av  !< org. carbon
424    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:), TARGET ::  s_SO4_av !< sulphates
425    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:), TARGET ::  s_SS_av  !< sea salt
426!-- Bins:   
427    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:) ::  mbins_av  !< bin mass
428                                                            !< concentration
429    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:) ::  Nbins_av  !< bin number
430                                                            !< concentration 
431       
432!
433!-- PALM interfaces:
434!
435!-- Boundary conditions:
436    INTERFACE salsa_boundary_conds
437       MODULE PROCEDURE salsa_boundary_conds
438       MODULE PROCEDURE salsa_boundary_conds_decycle
439    END INTERFACE salsa_boundary_conds
440!   
441!-- Data output checks for 2D/3D data to be done in check_parameters
442    INTERFACE salsa_check_data_output
443       MODULE PROCEDURE salsa_check_data_output
444    END INTERFACE salsa_check_data_output
445   
446!
447!-- Input parameter checks to be done in check_parameters
448    INTERFACE salsa_check_parameters
449       MODULE PROCEDURE salsa_check_parameters
450    END INTERFACE salsa_check_parameters
451
452!
453!-- Averaging of 3D data for output
454    INTERFACE salsa_3d_data_averaging
455       MODULE PROCEDURE salsa_3d_data_averaging
456    END INTERFACE salsa_3d_data_averaging
457
458!
459!-- Data output of 2D quantities
460    INTERFACE salsa_data_output_2d
461       MODULE PROCEDURE salsa_data_output_2d
462    END INTERFACE salsa_data_output_2d
463
464!
465!-- Data output of 3D data
466    INTERFACE salsa_data_output_3d
467       MODULE PROCEDURE salsa_data_output_3d
468    END INTERFACE salsa_data_output_3d
469   
470!
471!-- Data output of 3D data
472    INTERFACE salsa_data_output_mask
473       MODULE PROCEDURE salsa_data_output_mask
474    END INTERFACE salsa_data_output_mask
475
476!
477!-- Definition of data output quantities
478    INTERFACE salsa_define_netcdf_grid
479       MODULE PROCEDURE salsa_define_netcdf_grid
480    END INTERFACE salsa_define_netcdf_grid
481   
482!
483!-- Output of information to the header file
484    INTERFACE salsa_header
485       MODULE PROCEDURE salsa_header
486    END INTERFACE salsa_header
487 
488!
489!-- Initialization actions 
490    INTERFACE salsa_init
491       MODULE PROCEDURE salsa_init
492    END INTERFACE salsa_init
493 
494!
495!-- Initialization of arrays
496    INTERFACE salsa_init_arrays
497       MODULE PROCEDURE salsa_init_arrays
498    END INTERFACE salsa_init_arrays
499
500!
501!-- Writing of binary output for restart runs  !!! renaming?!
502    INTERFACE salsa_wrd_local
503       MODULE PROCEDURE salsa_wrd_local
504    END INTERFACE salsa_wrd_local
505   
506!
507!-- Reading of NAMELIST parameters
508    INTERFACE salsa_parin
509       MODULE PROCEDURE salsa_parin
510    END INTERFACE salsa_parin
511
512!
513!-- Reading of parameters for restart runs
514    INTERFACE salsa_rrd_local
515       MODULE PROCEDURE salsa_rrd_local
516    END INTERFACE salsa_rrd_local
517   
518!
519!-- Swapping of time levels (required for prognostic variables)
520    INTERFACE salsa_swap_timelevel
521       MODULE PROCEDURE salsa_swap_timelevel
522    END INTERFACE salsa_swap_timelevel
523
524    INTERFACE salsa_driver
525       MODULE PROCEDURE salsa_driver
526    END INTERFACE salsa_driver
527
528    INTERFACE salsa_tendency
529       MODULE PROCEDURE salsa_tendency
530       MODULE PROCEDURE salsa_tendency_ij
531    END INTERFACE salsa_tendency
532   
533   
534   
535    SAVE
536
537    PRIVATE
538!
539!-- Public functions:
540    PUBLIC salsa_boundary_conds, salsa_check_data_output,                      &
541           salsa_check_parameters, salsa_3d_data_averaging,                    &
542           salsa_data_output_2d, salsa_data_output_3d, salsa_data_output_mask, &
543           salsa_define_netcdf_grid, salsa_diagnostics, salsa_driver,          &
544           salsa_header, salsa_init, salsa_init_arrays, salsa_parin,           &
545           salsa_rrd_local, salsa_swap_timelevel, salsa_tendency,              &
546           salsa_wrd_local
547!
548!-- Public parameters, constants and initial values
549    PUBLIC dots_salsa, dt_salsa, last_salsa_time, lsdepo, salsa,               &
550           salsa_gases_from_chem, skip_time_do_salsa
551!
552!-- Public prognostic variables
553    PUBLIC aerosol_mass, aerosol_number, fn2a, fn2b, gconc_2, in1a, in2b,      &
554           mconc_2, nbins, ncc, ncc_tot, nclim, nconc_2, ngast, prtcl, Ra_dry, &
555           salsa_gas, sedim_vd
556
557 CONTAINS
558
559!------------------------------------------------------------------------------!
560! Description:
561! ------------
562!> Parin for &salsa_par for new modules
563!------------------------------------------------------------------------------!
564 SUBROUTINE salsa_parin
565
566    IMPLICIT NONE
567
568    CHARACTER (LEN=80) ::  line   !< dummy string that contains the current line
569                                  !< of the parameter file
570                                 
571    NAMELIST /salsa_parameters/             &
572                          advect_particle_water, & ! Switch for advecting
573                                                ! particle water. If .FALSE.,
574                                                ! equilibration is called at
575                                                ! each time step.       
576                          bc_salsa_b,       &   ! bottom boundary condition
577                          bc_salsa_t,       &   ! top boundary condition
578                          decycle_lr,       &   ! decycle SALSA components
579                          decycle_method,   &   ! decycle method applied:
580                                                ! 1=left 2=right 3=south 4=north
581                          decycle_ns,       &   ! decycle SALSA components
582                          depo_vege_type,   &   ! Parametrisation type
583                          depo_topo_type,   &   ! Parametrisation type
584                          dpg,              &   ! Mean diameter for the initial
585                                                ! log-normal modes
586                          dt_salsa,         &   ! SALSA timestep in seconds
587                          feedback_to_palm, &   ! allow feedback due to
588                                                ! hydration / condensation
589                          H2SO4_init,       &   ! Init value for sulphuric acid
590                          HNO3_init,        &   ! Init value for nitric acid
591                          igctyp,           &   ! Initial gas concentration type
592                          isdtyp,           &   ! Initial size distribution type                                               
593                          listspec,         &   ! List of actived aerosols
594                                                ! (string list)
595                          mass_fracs_a,     &   ! Initial relative contribution 
596                                                ! of each species to particle 
597                                                ! volume in a-bins, 0 for unused
598                          mass_fracs_b,     &   ! Initial relative contribution 
599                                                ! of each species to particle
600                                                ! volume in b-bins, 0 for unused
601                          n_lognorm,        &   ! Number concentration for the
602                                                ! log-normal modes                                               
603                          nbin,             &   ! Number of size bins for
604                                                ! aerosol size subranges 1 & 2
605                          nf2a,             &   ! Number fraction of particles
606                                                ! allocated to a-bins in
607                                                ! subrange 2 b-bins will get
608                                                ! 1-nf2a                         
609                          NH3_init,         &   ! Init value for ammonia
610                          nj3,              &   ! J3 parametrization
611                                                ! 1 = condensational sink
612                                                !     (Kerminen&Kulmala, 2002)
613                                                ! 2 = coagulational sink
614                                                !     (Lehtinen et al. 2007)
615                                                ! 3 = coagS+self-coagulation
616                                                !     (Anttila et al. 2010)                                                   
617                          nlcnd,            &   ! Condensation master switch
618                          nlcndgas,         &   ! Condensation of gases
619                          nlcndh2oae,       &   ! Condensation of H2O                           
620                          nlcoag,           &   ! Coagulation master switch
621                          nldepo,           &   ! Deposition master switch
622                          nldepo_vege,      &   ! Deposition on vegetation
623                                                ! master switch
624                          nldepo_topo,      &   ! Deposition on topo master
625                                                ! switch                         
626                          nldistupdate,     &   ! Size distribution update
627                                                ! master switch
628                          nsnucl,           &   ! Nucleation scheme:
629                                                ! 0 = off,
630                                                ! 1 = binary nucleation
631                                                ! 2 = activation type nucleation
632                                                ! 3 = kinetic nucleation
633                                                ! 4 = ternary nucleation
634                                                ! 5 = nucleation with organics
635                                                ! 6 = activation type of
636                                                !     nucleation with H2SO4+ORG
637                                                ! 7 = heteromolecular nucleation
638                                                !     with H2SO4*ORG
639                                                ! 8 = homomolecular nucleation 
640                                                !     of H2SO4 + heteromolecular
641                                                !     nucleation with H2SO4*ORG
642                                                ! 9 = homomolecular nucleation
643                                                !     of H2SO4 and ORG + hetero-
644                                                !     molecular nucleation with
645                                                !     H2SO4*ORG
646                          OCNV_init,        &   ! Init value for non-volatile
647                                                ! organic gases
648                          OCSV_init,        &   ! Init value for semi-volatile
649                                                ! organic gases
650                          read_restart_data_salsa, & ! read restart data for
651                                                     ! salsa
652                          reglim,           &   ! Min&max diameter limits of
653                                                ! size subranges
654                          salsa,            &   ! Master switch for SALSA
655                          salsa_source_mode,&   ! 'read_from_file' or 'constant'
656                                                ! or 'no_source'
657                          sigmag,           &   ! stdev for the initial log-
658                                                ! normal modes                                               
659                          skip_time_do_salsa, & ! Starting time of SALSA (s)
660                          van_der_waals_coagc,& ! include van der Waals forces
661                          write_binary_salsa    ! Write binary for salsa
662                           
663       
664    line = ' '
665       
666!
667!-- Try to find salsa package
668    REWIND ( 11 )
669    line = ' '
670    DO WHILE ( INDEX( line, '&salsa_parameters' ) == 0 )
671       READ ( 11, '(A)', END=10 )  line
672    ENDDO
673    BACKSPACE ( 11 )
674
675!
676!-- Read user-defined namelist
677    READ ( 11, salsa_parameters )
678
679!
680!-- Set flag that indicates that the new module is switched on
681!-- Note that this parameter needs to be declared in modules.f90
682    salsa = .TRUE.
683
684 10 CONTINUE
685       
686 END SUBROUTINE salsa_parin
687
688 
689!------------------------------------------------------------------------------!
690! Description:
691! ------------
692!> Check parameters routine for salsa.
693!------------------------------------------------------------------------------!
694 SUBROUTINE salsa_check_parameters
695
696    USE control_parameters,                                                    &
697        ONLY:  message_string
698       
699    IMPLICIT NONE
700   
701!
702!-- Checks go here (cf. check_parameters.f90).
703    IF ( salsa  .AND.  .NOT.  humidity )  THEN
704       WRITE( message_string, * ) 'salsa = ', salsa, ' is ',                   &
705              'not allowed with humidity = ', humidity
706       CALL message( 'check_parameters', 'SA0009', 1, 2, 0, 6, 0 )
707    ENDIF
708   
709    IF ( bc_salsa_b == 'dirichlet' )  THEN
710       ibc_salsa_b = 0
711    ELSEIF ( bc_salsa_b == 'neumann' )  THEN
712       ibc_salsa_b = 1
713    ELSE
714       message_string = 'unknown boundary condition: bc_salsa_b = "'           &
715                         // TRIM( bc_salsa_t ) // '"'
716       CALL message( 'check_parameters', 'SA0011', 1, 2, 0, 6, 0 )                 
717    ENDIF
718   
719    IF ( bc_salsa_t == 'dirichlet' )  THEN
720       ibc_salsa_t = 0
721    ELSEIF ( bc_salsa_t == 'neumann' )  THEN
722       ibc_salsa_t = 1
723    ELSE
724       message_string = 'unknown boundary condition: bc_salsa_t = "'           &
725                         // TRIM( bc_salsa_t ) // '"'
726       CALL message( 'check_parameters', 'SA0012', 1, 2, 0, 6, 0 )                 
727    ENDIF
728   
729    IF ( nj3 < 1  .OR.  nj3 > 3 )  THEN
730       message_string = 'unknown nj3 (must be 1-3)'
731       CALL message( 'check_parameters', 'SA0044', 1, 2, 0, 6, 0 )
732    ENDIF
733           
734 END SUBROUTINE salsa_check_parameters
735
736!------------------------------------------------------------------------------!
737!
738! Description:
739! ------------
740!> Subroutine defining appropriate grid for netcdf variables.
741!> It is called out from subroutine netcdf.
742!> Same grid as for other scalars (see netcdf_interface_mod.f90)
743!------------------------------------------------------------------------------!
744 SUBROUTINE salsa_define_netcdf_grid( var, found, grid_x, grid_y, grid_z )
745   
746    IMPLICIT NONE
747
748    CHARACTER (LEN=*), INTENT(OUT) ::  grid_x   !<
749    CHARACTER (LEN=*), INTENT(OUT) ::  grid_y   !<
750    CHARACTER (LEN=*), INTENT(OUT) ::  grid_z   !<
751    CHARACTER (LEN=*), INTENT(IN)  ::  var      !<
752   
753    LOGICAL, INTENT(OUT) ::  found   !<
754   
755    found  = .TRUE.
756!
757!-- Check for the grid
758
759    IF ( var(1:2) == 'g_' )  THEN
760       grid_x = 'x' 
761       grid_y = 'y' 
762       grid_z = 'zu'   
763    ELSEIF ( var(1:4) == 'LDSA' )  THEN
764       grid_x = 'x' 
765       grid_y = 'y' 
766       grid_z = 'zu'
767    ELSEIF ( var(1:5) == 'm_bin' )  THEN
768       grid_x = 'x' 
769       grid_y = 'y' 
770       grid_z = 'zu'
771    ELSEIF ( var(1:5) == 'N_bin' )  THEN
772       grid_x = 'x' 
773       grid_y = 'y' 
774       grid_z = 'zu'
775    ELSEIF ( var(1:4) == 'Ntot' ) THEN
776       grid_x = 'x' 
777       grid_y = 'y' 
778       grid_z = 'zu'
779    ELSEIF ( var(1:2) == 'PM' )  THEN
780       grid_x = 'x' 
781       grid_y = 'y' 
782       grid_z = 'zu'
783    ELSEIF ( var(1:2) == 's_' )  THEN
784       grid_x = 'x' 
785       grid_y = 'y' 
786       grid_z = 'zu'
787    ELSE
788       found  = .FALSE.
789       grid_x = 'none'
790       grid_y = 'none'
791       grid_z = 'none'
792    ENDIF
793
794 END SUBROUTINE salsa_define_netcdf_grid
795
796 
797!------------------------------------------------------------------------------!
798! Description:
799! ------------
800!> Header output for new module
801!------------------------------------------------------------------------------!
802 SUBROUTINE salsa_header( io )
803
804    IMPLICIT NONE
805 
806    INTEGER(iwp), INTENT(IN) ::  io   !< Unit of the output file
807!
808!-- Write SALSA header
809    WRITE( io, 1 )
810    WRITE( io, 2 ) skip_time_do_salsa
811    WRITE( io, 3 ) dt_salsa
812    WRITE( io, 12 )  SHAPE( aerosol_number(1)%conc ), nbins
813    IF ( advect_particle_water )  THEN
814       WRITE( io, 16 )  SHAPE( aerosol_mass(1)%conc ), ncc_tot*nbins,          &
815                        advect_particle_water
816    ELSE
817       WRITE( io, 16 )  SHAPE( aerosol_mass(1)%conc ), ncc*nbins,              &
818                        advect_particle_water
819    ENDIF
820    IF ( .NOT. salsa_gases_from_chem )  THEN
821       WRITE( io, 17 )  SHAPE( aerosol_mass(1)%conc ), ngast,                  &
822                        salsa_gases_from_chem
823    ENDIF
824    WRITE( io, 4 ) 
825    IF ( nsnucl > 0 )  THEN
826       WRITE( io, 5 ) nsnucl, nj3
827    ENDIF
828    IF ( nlcoag )  THEN
829       WRITE( io, 6 ) 
830    ENDIF
831    IF ( nlcnd )  THEN
832       WRITE( io, 7 ) nlcndgas, nlcndh2oae
833    ENDIF
834    IF ( nldepo )  THEN
835       WRITE( io, 14 ) nldepo_vege, nldepo_topo
836    ENDIF
837    WRITE( io, 8 )  reglim, nbin, bin_low_limits
838    WRITE( io, 15 ) nsect
839    WRITE( io, 13 ) ncc, listspec, mass_fracs_a, mass_fracs_b
840    IF ( .NOT. salsa_gases_from_chem )  THEN
841       WRITE( io, 18 ) ngast, H2SO4_init, HNO3_init, NH3_init, OCNV_init,      &
842                       OCSV_init
843    ENDIF
844    WRITE( io, 9 )  isdtyp, igctyp
845    IF ( isdtyp == 0 )  THEN
846       WRITE( io, 10 )  dpg, sigmag, n_lognorm
847    ELSE
848       WRITE( io, 11 )
849    ENDIF
850   
851
8521   FORMAT (//' SALSA information:'/                                           &
853              ' ------------------------------'/)
8542   FORMAT   ('    Starts at: skip_time_do_salsa = ', F10.2, '  s')
8553   FORMAT  (/'    Timestep: dt_salsa = ', F6.2, '  s')
85612  FORMAT  (/'    Array shape (z,y,x,bins):'/                                 &
857              '       aerosol_number:  ', 4(I3)) 
85816  FORMAT  (/'       aerosol_mass:    ', 4(I3),/                              &
859              '       (advect_particle_water = ', L1, ')')
86017  FORMAT   ('       salsa_gas: ', 4(I3),/                                    &
861              '       (salsa_gases_from_chem = ', L1, ')')
8624   FORMAT  (/'    Aerosol dynamic processes included: ')
8635   FORMAT  (/'       nucleation (scheme = ', I1, ' and J3 parametrization = ',&
864               I1, ')')
8656   FORMAT  (/'       coagulation')
8667   FORMAT  (/'       condensation (of precursor gases = ', L1,                &
867              '          and water vapour = ', L1, ')' )
86814  FORMAT  (/'       dry deposition (on vegetation = ', L1,                   &
869              '          and on topography = ', L1, ')')             
8708   FORMAT  (/'    Aerosol bin subrange limits (in metres): ',  3(ES10.2E3) /  &
871              '    Number of size bins for each aerosol subrange: ', 2I3,/     &
872              '    Aerosol bin limits (in metres): ', *(ES10.2E3))
87315  FORMAT   ('    Initial number concentration in bins at the lowest level',  &
874              ' (#/m**3):', *(ES10.2E3))       
87513  FORMAT  (/'    Number of chemical components used: ', I1,/                 &
876              '       Species: ',7(A6),/                                       &
877              '    Initial relative contribution of each species to particle', & 
878              ' volume in:',/                                                  &
879              '       a-bins: ', 7(F6.3),/                                     &
880              '       b-bins: ', 7(F6.3))
88118  FORMAT  (/'    Number of gaseous tracers used: ', I1,/                     &
882              '    Initial gas concentrations:',/                              &
883              '       H2SO4: ',ES12.4E3, ' #/m**3',/                           &
884              '       HNO3:  ',ES12.4E3, ' #/m**3',/                           &
885              '       NH3:   ',ES12.4E3, ' #/m**3',/                           &
886              '       OCNV:  ',ES12.4E3, ' #/m**3',/                           &
887              '       OCSV:  ',ES12.4E3, ' #/m**3')
8889    FORMAT (/'   Initialising concentrations: ', /                            &
889              '      Aerosol size distribution: isdtyp = ', I1,/               &
890              '      Gas concentrations: igctyp = ', I1 )
89110   FORMAT ( '      Mode diametres: dpg(nmod) = ', 7(F7.3),/                  &
892              '      Standard deviation: sigmag(nmod) = ', 7(F7.2),/           &
893              '      Number concentration: n_lognorm(nmod) = ', 7(ES12.4E3) )
89411   FORMAT (/'      Size distribution read from a file.')
895
896 END SUBROUTINE salsa_header
897
898!------------------------------------------------------------------------------!
899! Description:
900! ------------
901!> Allocate SALSA arrays and define pointers if required
902!------------------------------------------------------------------------------!
903 SUBROUTINE salsa_init_arrays
904 
905    USE surface_mod,                                                           &
906        ONLY:  surf_def_h, surf_def_v, surf_lsm_h, surf_lsm_v, surf_usm_h,     &
907               surf_usm_v
908
909    IMPLICIT NONE
910   
911    INTEGER(iwp) ::  gases_available !< Number of available gas components in
912                                     !< the chemistry model
913    INTEGER(iwp) ::  i   !< loop index for allocating
914    INTEGER(iwp) ::  l   !< loop index for allocating: surfaces
915    INTEGER(iwp) ::  lsp !< loop index for chem species in the chemistry model
916   
917    gases_available = 0
918
919!
920!-- Allocate prognostic variables (see salsa_swap_timelevel)
921#if defined( __nopointer )
922    message_string = 'SALSA runs only with POINTER Version'
923    CALL message( 'salsa_mod: salsa_init_arrays', 'SA0023', 1, 2, 0, 6, 0 )
924#else         
925!
926!-- Set derived indices:
927!-- (This does the same as the subroutine salsa_initialize in SALSA/
928!-- UCLALES-SALSA)       
929    in1a = 1                ! 1st index of subrange 1a
930    in2a = in1a + nbin(1)   ! 1st index of subrange 2a
931    fn1a = in2a - 1         ! last index of subrange 1a
932    fn2a = fn1a + nbin(2)   ! last index of subrange 2a
933   
934!   
935!-- If the fraction of insoluble aerosols in subrange 2 is zero: do not allocate
936!-- arrays for them
937    IF ( nf2a > 0.999999_wp  .AND.  SUM( mass_fracs_b ) < 0.00001_wp )  THEN
938       no_insoluble = .TRUE.
939       in2b = fn2a+1    ! 1st index of subrange 2b
940       fn2b = fn2a      ! last index of subrange 2b
941    ELSE
942       in2b = in2a + nbin(2)   ! 1st index of subrange 2b
943       fn2b = fn2a + nbin(2)   ! last index of subrange 2b
944    ENDIF
945   
946   
947    nbins = fn2b   ! total number of aerosol size bins
948!   
949!-- Create index tables for different aerosol components
950    CALL component_index_constructor( prtcl, ncc, maxspec, listspec )
951   
952    ncc_tot = ncc
953    IF ( advect_particle_water )  ncc_tot = ncc + 1  ! Add water
954   
955!
956!-- Allocate:
957    ALLOCATE( aero(nbins), bin_low_limits(nbins), nsect(nbins), massacc(nbins) )
958    IF ( nldepo ) ALLOCATE( sedim_vd(nzb:nzt+1,nysg:nyng,nxlg:nxrg,nbins) )         
959    ALLOCATE( Ra_dry(nzb:nzt+1,nysg:nyng,nxlg:nxrg,nbins) )
960   
961!   
962!-- Aerosol number concentration
963    ALLOCATE( aerosol_number(nbins) )
964    ALLOCATE( nconc_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg,nbins),                    &
965              nconc_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg,nbins),                    &
966              nconc_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg,nbins) )
967    nconc_1 = 0.0_wp
968    nconc_2 = 0.0_wp
969    nconc_3 = 0.0_wp
970   
971    DO i = 1, nbins
972       aerosol_number(i)%conc(nzb:nzt+1,nysg:nyng,nxlg:nxrg)    => nconc_1(:,:,:,i)
973       aerosol_number(i)%conc_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg)  => nconc_2(:,:,:,i)
974       aerosol_number(i)%tconc_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg) => nconc_3(:,:,:,i)
975       ALLOCATE( aerosol_number(i)%flux_s(nzb+1:nzt,0:threads_per_task-1),     &
976                 aerosol_number(i)%diss_s(nzb+1:nzt,0:threads_per_task-1),     &
977                 aerosol_number(i)%flux_l(nzb+1:nzt,nys:nyn,0:threads_per_task-1),&
978                 aerosol_number(i)%diss_l(nzb+1:nzt,nys:nyn,0:threads_per_task-1),&
979                 aerosol_number(i)%init(nzb:nzt+1),                            &
980                 aerosol_number(i)%sums_ws_l(nzb:nzt+1,0:threads_per_task-1) )
981    ENDDO     
982   
983!   
984!-- Aerosol mass concentration   
985    ALLOCATE( aerosol_mass(ncc_tot*nbins) ) 
986    ALLOCATE( mconc_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg,ncc_tot*nbins),            &
987              mconc_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg,ncc_tot*nbins),            &
988              mconc_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg,ncc_tot*nbins) )
989    mconc_1 = 0.0_wp
990    mconc_2 = 0.0_wp
991    mconc_3 = 0.0_wp
992   
993    DO i = 1, ncc_tot*nbins
994       aerosol_mass(i)%conc(nzb:nzt+1,nysg:nyng,nxlg:nxrg)    => mconc_1(:,:,:,i)
995       aerosol_mass(i)%conc_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg)  => mconc_2(:,:,:,i)
996       aerosol_mass(i)%tconc_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg) => mconc_3(:,:,:,i)       
997       ALLOCATE( aerosol_mass(i)%flux_s(nzb+1:nzt,0:threads_per_task-1),       &
998                 aerosol_mass(i)%diss_s(nzb+1:nzt,0:threads_per_task-1),       &
999                 aerosol_mass(i)%flux_l(nzb+1:nzt,nys:nyn,0:threads_per_task-1),&
1000                 aerosol_mass(i)%diss_l(nzb+1:nzt,nys:nyn,0:threads_per_task-1),&
1001                 aerosol_mass(i)%init(nzb:nzt+1),                              &
1002                 aerosol_mass(i)%sums_ws_l(nzb:nzt+1,0:threads_per_task-1)  )
1003    ENDDO
1004   
1005!
1006!-- Surface fluxes: answs = aerosol number, amsws = aerosol mass
1007!
1008!-- Horizontal surfaces: default type
1009    DO  l = 0, 2   ! upward (l=0), downward (l=1) and model top (l=2)
1010       ALLOCATE( surf_def_h(l)%answs( 1:surf_def_h(l)%ns, nbins ) )
1011       ALLOCATE( surf_def_h(l)%amsws( 1:surf_def_h(l)%ns, nbins*ncc_tot ) )
1012       surf_def_h(l)%answs = 0.0_wp
1013       surf_def_h(l)%amsws = 0.0_wp
1014    ENDDO
1015!-- Horizontal surfaces: natural type   
1016    IF ( land_surface )  THEN
1017       ALLOCATE( surf_lsm_h%answs( 1:surf_lsm_h%ns, nbins ) )
1018       ALLOCATE( surf_lsm_h%amsws( 1:surf_lsm_h%ns, nbins*ncc_tot ) )
1019       surf_lsm_h%answs = 0.0_wp
1020       surf_lsm_h%amsws = 0.0_wp
1021    ENDIF
1022!-- Horizontal surfaces: urban type
1023    IF ( urban_surface )  THEN
1024       ALLOCATE( surf_usm_h%answs( 1:surf_usm_h%ns, nbins ) )
1025       ALLOCATE( surf_usm_h%amsws( 1:surf_usm_h%ns, nbins*ncc_tot ) )
1026       surf_usm_h%answs = 0.0_wp
1027       surf_usm_h%amsws = 0.0_wp
1028    ENDIF
1029!
1030!-- Vertical surfaces: northward (l=0), southward (l=1), eastward (l=2) and
1031!-- westward (l=3) facing
1032    DO  l = 0, 3   
1033       ALLOCATE( surf_def_v(l)%answs( 1:surf_def_v(l)%ns, nbins ) )
1034       surf_def_v(l)%answs = 0.0_wp
1035       ALLOCATE( surf_def_v(l)%amsws( 1:surf_def_v(l)%ns, nbins*ncc_tot ) )
1036       surf_def_v(l)%amsws = 0.0_wp
1037       
1038       IF ( land_surface)  THEN
1039          ALLOCATE( surf_lsm_v(l)%answs( 1:surf_lsm_v(l)%ns, nbins ) )
1040          surf_lsm_v(l)%answs = 0.0_wp
1041          ALLOCATE( surf_lsm_v(l)%amsws( 1:surf_lsm_v(l)%ns, nbins*ncc_tot ) )
1042          surf_lsm_v(l)%amsws = 0.0_wp
1043       ENDIF
1044       
1045       IF ( urban_surface )  THEN
1046          ALLOCATE( surf_usm_v(l)%answs( 1:surf_usm_v(l)%ns, nbins ) )
1047          surf_usm_v(l)%answs = 0.0_wp
1048          ALLOCATE( surf_usm_v(l)%amsws( 1:surf_usm_v(l)%ns, nbins*ncc_tot ) )
1049          surf_usm_v(l)%amsws = 0.0_wp
1050       ENDIF
1051    ENDDO   
1052   
1053!
1054!-- Concentration of gaseous tracers (1. SO4, 2. HNO3, 3. NH3, 4. OCNV, 5. OCSV)
1055!-- (number concentration (#/m3) )
1056!
1057!-- If chemistry is on, read gas phase concentrations from there. Otherwise,
1058!-- allocate salsa_gas array.
1059
1060    IF ( air_chemistry )  THEN   
1061       DO  lsp = 1, nvar
1062          IF ( TRIM( chem_species(lsp)%name ) == 'H2SO4' )  THEN
1063             gases_available = gases_available + 1
1064             gas_index_chem(1) = lsp
1065          ELSEIF ( TRIM( chem_species(lsp)%name ) == 'HNO3' )  THEN
1066             gases_available = gases_available + 1 
1067             gas_index_chem(2) = lsp
1068          ELSEIF ( TRIM( chem_species(lsp)%name ) == 'NH3' )  THEN
1069             gases_available = gases_available + 1
1070             gas_index_chem(3) = lsp
1071          ELSEIF ( TRIM( chem_species(lsp)%name ) == 'OCNV' )  THEN
1072             gases_available = gases_available + 1
1073             gas_index_chem(4) = lsp
1074          ELSEIF ( TRIM( chem_species(lsp)%name ) == 'OCSV' )  THEN
1075             gases_available = gases_available + 1
1076             gas_index_chem(5) = lsp
1077          ENDIF
1078       ENDDO
1079
1080       IF ( gases_available == ngast )  THEN
1081          salsa_gases_from_chem = .TRUE.
1082       ELSE
1083          WRITE( message_string, * ) 'SALSA is run together with chemistry '// &
1084                                     'but not all gaseous components are '//   &
1085                                     'provided by kpp (H2SO4, HNO3, NH3, '//   &
1086                                     'OCNV, OCSC)'
1087       CALL message( 'check_parameters', 'SA0024', 1, 2, 0, 6, 0 )
1088       ENDIF
1089
1090    ELSE
1091
1092       ALLOCATE( salsa_gas(ngast) ) 
1093       ALLOCATE( gconc_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg,ngast),                 &
1094                 gconc_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg,ngast),                 &
1095                 gconc_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg,ngast) )
1096       gconc_1 = 0.0_wp
1097       gconc_2 = 0.0_wp
1098       gconc_3 = 0.0_wp
1099       
1100       DO i = 1, ngast
1101          salsa_gas(i)%conc(nzb:nzt+1,nysg:nyng,nxlg:nxrg)    => gconc_1(:,:,:,i)
1102          salsa_gas(i)%conc_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg)  => gconc_2(:,:,:,i)
1103          salsa_gas(i)%tconc_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg) => gconc_3(:,:,:,i)
1104          ALLOCATE( salsa_gas(i)%flux_s(nzb+1:nzt,0:threads_per_task-1),       &
1105                    salsa_gas(i)%diss_s(nzb+1:nzt,0:threads_per_task-1),       &
1106                    salsa_gas(i)%flux_l(nzb+1:nzt,nys:nyn,0:threads_per_task-1),&
1107                    salsa_gas(i)%diss_l(nzb+1:nzt,nys:nyn,0:threads_per_task-1),&
1108                    salsa_gas(i)%init(nzb:nzt+1),                              &
1109                    salsa_gas(i)%sums_ws_l(nzb:nzt+1,0:threads_per_task-1) )
1110       ENDDO       
1111!
1112!--    Surface fluxes: gtsws = gaseous tracer flux
1113!
1114!--    Horizontal surfaces: default type
1115       DO  l = 0, 2   ! upward (l=0), downward (l=1) and model top (l=2)
1116          ALLOCATE( surf_def_h(l)%gtsws( 1:surf_def_h(l)%ns, ngast ) )
1117          surf_def_h(l)%gtsws = 0.0_wp
1118       ENDDO
1119!--    Horizontal surfaces: natural type   
1120       IF ( land_surface )  THEN
1121          ALLOCATE( surf_lsm_h%gtsws( 1:surf_lsm_h%ns, ngast ) )
1122          surf_lsm_h%gtsws = 0.0_wp
1123       ENDIF
1124!--    Horizontal surfaces: urban type         
1125       IF ( urban_surface )  THEN
1126          ALLOCATE( surf_usm_h%gtsws( 1:surf_usm_h%ns, ngast ) )
1127          surf_usm_h%gtsws = 0.0_wp
1128       ENDIF
1129!
1130!--    Vertical surfaces: northward (l=0), southward (l=1), eastward (l=2) and
1131!--    westward (l=3) facing
1132       DO  l = 0, 3     
1133          ALLOCATE( surf_def_v(l)%gtsws( 1:surf_def_v(l)%ns, ngast ) )
1134          surf_def_v(l)%gtsws = 0.0_wp
1135          IF ( land_surface )  THEN
1136             ALLOCATE( surf_lsm_v(l)%gtsws( 1:surf_lsm_v(l)%ns, ngast ) )
1137             surf_lsm_v(l)%gtsws = 0.0_wp
1138          ENDIF
1139          IF ( urban_surface )  THEN
1140             ALLOCATE( surf_usm_v(l)%gtsws( 1:surf_usm_v(l)%ns, ngast ) )
1141             surf_usm_v(l)%gtsws = 0.0_wp
1142          ENDIF
1143       ENDDO
1144    ENDIF
1145   
1146#endif
1147
1148 END SUBROUTINE salsa_init_arrays
1149
1150!------------------------------------------------------------------------------!
1151! Description:
1152! ------------
1153!> Initialization of SALSA. Based on salsa_initialize in UCLALES-SALSA.
1154!> Subroutines salsa_initialize, SALSAinit and DiagInitAero in UCLALES-SALSA are
1155!> also merged here.
1156!------------------------------------------------------------------------------!
1157 SUBROUTINE salsa_init
1158
1159    IMPLICIT NONE
1160   
1161    INTEGER(iwp) :: b
1162    INTEGER(iwp) :: c
1163    INTEGER(iwp) :: g
1164    INTEGER(iwp) :: i
1165    INTEGER(iwp) :: j
1166   
1167    bin_low_limits = 0.0_wp
1168    nsect          = 0.0_wp
1169    massacc        = 1.0_wp 
1170   
1171!
1172!-- Indices for chemical components used (-1 = not used)
1173    i = 0
1174    IF ( is_used( prtcl, 'SO4' ) )  THEN
1175       iso4 = get_index( prtcl,'SO4' )
1176       i = i + 1
1177    ENDIF
1178    IF ( is_used( prtcl,'OC' ) )  THEN
1179       ioc = get_index(prtcl, 'OC')
1180       i = i + 1
1181    ENDIF
1182    IF ( is_used( prtcl, 'BC' ) )  THEN
1183       ibc = get_index( prtcl, 'BC' )
1184       i = i + 1
1185    ENDIF
1186    IF ( is_used( prtcl, 'DU' ) )  THEN
1187       idu = get_index( prtcl, 'DU' )
1188       i = i + 1
1189    ENDIF
1190    IF ( is_used( prtcl, 'SS' ) )  THEN
1191       iss = get_index( prtcl, 'SS' )
1192       i = i + 1
1193    ENDIF
1194    IF ( is_used( prtcl, 'NO' ) )  THEN
1195       ino = get_index( prtcl, 'NO' )
1196       i = i + 1
1197    ENDIF
1198    IF ( is_used( prtcl, 'NH' ) )  THEN
1199       inh = get_index( prtcl, 'NH' )
1200       i = i + 1
1201    ENDIF
1202!   
1203!-- All species must be known
1204    IF ( i /= ncc )  THEN
1205       message_string = 'Unknown aerosol species/component(s) given in the' // &
1206                        ' initialization'
1207       CALL message( 'salsa_mod: salsa_init', 'SA0020', 1, 2, 0, 6, 0 )
1208    ENDIF
1209   
1210!
1211!-- Initialise
1212!
1213!-- Aerosol size distribution (TYPE t_section)
1214    aero(:)%dwet     = 1.0E-10_wp
1215    aero(:)%veqh2o   = 1.0E-10_wp
1216    aero(:)%numc     = nclim
1217    aero(:)%core     = 1.0E-10_wp
1218    DO c = 1, maxspec+1    ! 1:SO4, 2:OC, 3:BC, 4:DU, 5:SS, 6:NO, 7:NH, 8:H2O
1219       aero(:)%volc(c) = 0.0_wp
1220    ENDDO
1221   
1222    IF ( nldepo )  sedim_vd = 0.0_wp
1223!   
1224!-- Initilisation actions that are NOT conducted for restart runs
1225    IF ( .NOT. read_restart_data_salsa )  THEN   
1226   
1227       DO  b = 1, nbins
1228          aerosol_number(b)%conc      = nclim
1229          aerosol_number(b)%conc_p    = 0.0_wp
1230          aerosol_number(b)%tconc_m   = 0.0_wp
1231          aerosol_number(b)%flux_s    = 0.0_wp
1232          aerosol_number(b)%diss_s    = 0.0_wp
1233          aerosol_number(b)%flux_l    = 0.0_wp
1234          aerosol_number(b)%diss_l    = 0.0_wp
1235          aerosol_number(b)%init      = nclim
1236          aerosol_number(b)%sums_ws_l = 0.0_wp
1237       ENDDO
1238       DO  c = 1, ncc_tot*nbins
1239          aerosol_mass(c)%conc      = mclim
1240          aerosol_mass(c)%conc_p    = 0.0_wp
1241          aerosol_mass(c)%tconc_m   = 0.0_wp
1242          aerosol_mass(c)%flux_s    = 0.0_wp
1243          aerosol_mass(c)%diss_s    = 0.0_wp
1244          aerosol_mass(c)%flux_l    = 0.0_wp
1245          aerosol_mass(c)%diss_l    = 0.0_wp
1246          aerosol_mass(c)%init      = mclim
1247          aerosol_mass(c)%sums_ws_l = 0.0_wp
1248       ENDDO
1249       
1250       IF ( .NOT. salsa_gases_from_chem )  THEN
1251          DO  g = 1, ngast
1252             salsa_gas(g)%conc_p    = 0.0_wp
1253             salsa_gas(g)%tconc_m   = 0.0_wp
1254             salsa_gas(g)%flux_s    = 0.0_wp
1255             salsa_gas(g)%diss_s    = 0.0_wp
1256             salsa_gas(g)%flux_l    = 0.0_wp
1257             salsa_gas(g)%diss_l    = 0.0_wp
1258             salsa_gas(g)%sums_ws_l = 0.0_wp
1259          ENDDO
1260       
1261!
1262!--       Set initial value for gas compound tracers and initial values
1263          salsa_gas(1)%conc = H2SO4_init
1264          salsa_gas(1)%init = H2SO4_init
1265          salsa_gas(2)%conc = HNO3_init
1266          salsa_gas(2)%init = HNO3_init
1267          salsa_gas(3)%conc = NH3_init
1268          salsa_gas(3)%init = NH3_init
1269          salsa_gas(4)%conc = OCNV_init
1270          salsa_gas(4)%init = OCNV_init
1271          salsa_gas(5)%conc = OCSV_init
1272          salsa_gas(5)%init = OCSV_init     
1273       ENDIF
1274!
1275!--    Aerosol radius in each bin: dry and wet (m)
1276       Ra_dry = 1.0E-10_wp
1277!   
1278!--    Initialise aerosol tracers   
1279       aero(:)%vhilim   = 0.0_wp
1280       aero(:)%vlolim   = 0.0_wp
1281       aero(:)%vratiohi = 0.0_wp
1282       aero(:)%vratiolo = 0.0_wp
1283       aero(:)%dmid     = 0.0_wp
1284!
1285!--    Initialise the sectional particle size distribution
1286       CALL set_sizebins()
1287!
1288!--    Initialise location-dependent aerosol size distributions and
1289!--    chemical compositions:
1290       CALL aerosol_init 
1291!
1292!--    Initalisation run of SALSA
1293       DO  i = nxl, nxr
1294          DO  j = nys, nyn
1295             CALL salsa_driver( i, j, 1 )
1296             CALL salsa_diagnostics( i, j )
1297          ENDDO
1298       ENDDO 
1299    ENDIF
1300!
1301!-- Set the aerosol and gas sources
1302    IF ( salsa_source_mode == 'read_from_file' )  THEN
1303       CALL salsa_set_source
1304    ENDIF
1305   
1306 END SUBROUTINE salsa_init
1307
1308!------------------------------------------------------------------------------!
1309! Description:
1310! ------------
1311!> Initializes particle size distribution grid by calculating size bin limits
1312!> and mid-size for *dry* particles in each bin. Called from salsa_initialize
1313!> (only at the beginning of simulation).
1314!> Size distribution described using:
1315!>   1) moving center method (subranges 1 and 2)
1316!>      (Jacobson, Atmos. Env., 31, 131-144, 1997)
1317!>   2) fixed sectional method (subrange 3)
1318!> Size bins in each subrange are spaced logarithmically
1319!> based on given subrange size limits and bin number.
1320!
1321!> Mona changed 06/2017: Use geometric mean diameter to describe the mean
1322!> particle diameter in a size bin, not the arithmeric mean which clearly
1323!> overestimates the total particle volume concentration.
1324!
1325!> Coded by:
1326!> Hannele Korhonen (FMI) 2005
1327!> Harri Kokkola (FMI) 2006
1328!
1329!> Bug fixes for box model + updated for the new aerosol datatype:
1330!> Juha Tonttila (FMI) 2014
1331!------------------------------------------------------------------------------!
1332 SUBROUTINE set_sizebins
1333               
1334    IMPLICIT NONE
1335!   
1336!-- Local variables
1337    INTEGER(iwp) ::  cc
1338    INTEGER(iwp) ::  dd
1339    REAL(wp) ::  ratio_d !< ratio of the upper and lower diameter of subranges
1340!
1341!-- vlolim&vhilim: min & max *dry* volumes [fxm]
1342!-- dmid: bin mid *dry* diameter (m)
1343!-- vratiolo&vratiohi: volume ratio between the center and low/high limit
1344!
1345!-- 1) Size subrange 1:
1346    ratio_d = reglim(2) / reglim(1)   ! section spacing (m)
1347    DO  cc = in1a,fn1a
1348       aero(cc)%vlolim = api6 * ( reglim(1) * ratio_d **                       &
1349                                ( REAL( cc-1 ) / nbin(1) ) ) ** 3.0_wp
1350       aero(cc)%vhilim = api6 * ( reglim(1) * ratio_d **                       &
1351                                ( REAL( cc ) / nbin(1) ) ) ** 3.0_wp
1352       aero(cc)%dmid = SQRT( ( aero(cc)%vhilim / api6 ) ** ( 1.0_wp / 3.0_wp ) &
1353                           * ( aero(cc)%vlolim / api6 ) ** ( 1.0_wp / 3.0_wp ) )
1354       aero(cc)%vratiohi = aero(cc)%vhilim / ( api6 * aero(cc)%dmid ** 3.0_wp )
1355       aero(cc)%vratiolo = aero(cc)%vlolim / ( api6 * aero(cc)%dmid ** 3.0_wp )
1356    ENDDO
1357!
1358!-- 2) Size subrange 2:
1359!-- 2.1) Sub-subrange 2a: high hygroscopicity
1360    ratio_d = reglim(3) / reglim(2)   ! section spacing
1361    DO  dd = in2a, fn2a
1362       cc = dd - in2a
1363       aero(dd)%vlolim = api6 * ( reglim(2) * ratio_d **                       &
1364                                  ( REAL( cc ) / nbin(2) ) ) ** 3.0_wp
1365       aero(dd)%vhilim = api6 * ( reglim(2) * ratio_d **                       &
1366                                  ( REAL( cc+1 ) / nbin(2) ) ) ** 3.0_wp
1367       aero(dd)%dmid = SQRT( ( aero(dd)%vhilim / api6 ) ** ( 1.0_wp / 3.0_wp ) &
1368                           * ( aero(dd)%vlolim / api6 ) ** ( 1.0_wp / 3.0_wp ) )
1369       aero(dd)%vratiohi = aero(dd)%vhilim / ( api6 * aero(dd)%dmid ** 3.0_wp )
1370       aero(dd)%vratiolo = aero(dd)%vlolim / ( api6 * aero(dd)%dmid ** 3.0_wp )
1371    ENDDO
1372!         
1373!-- 2.2) Sub-subrange 2b: low hygroscopicity
1374    IF ( .NOT. no_insoluble )  THEN
1375       aero(in2b:fn2b)%vlolim   = aero(in2a:fn2a)%vlolim
1376       aero(in2b:fn2b)%vhilim   = aero(in2a:fn2a)%vhilim
1377       aero(in2b:fn2b)%dmid     = aero(in2a:fn2a)%dmid
1378       aero(in2b:fn2b)%vratiohi = aero(in2a:fn2a)%vratiohi
1379       aero(in2b:fn2b)%vratiolo = aero(in2a:fn2a)%vratiolo
1380    ENDIF
1381!         
1382!-- Initialize the wet diameter with the bin dry diameter to avoid numerical
1383!-- problems later
1384    aero(:)%dwet = aero(:)%dmid
1385!
1386!-- Save bin limits (lower diameter) to be delivered to the host model if needed
1387    DO cc = 1, nbins
1388       bin_low_limits(cc) = ( aero(cc)%vlolim / api6 )**( 1.0_wp / 3.0_wp )
1389    ENDDO   
1390   
1391 END SUBROUTINE set_sizebins
1392 
1393!------------------------------------------------------------------------------!
1394! Description:
1395! ------------
1396!> Initilize altitude-dependent aerosol size distributions and compositions.
1397!>
1398!> Mona added 06/2017: Correct the number and mass concentrations by normalizing
1399!< by the given total number and mass concentration.
1400!>
1401!> Tomi Raatikainen, FMI, 29.2.2016
1402!------------------------------------------------------------------------------!
1403 SUBROUTINE aerosol_init
1404 
1405    USE arrays_3d,                                                             &
1406        ONLY:  zu
1407 
1408    USE NETCDF
1409   
1410    USE netcdf_data_input_mod,                                                 &
1411        ONLY:  get_attribute, netcdf_data_input_get_dimension_length,          &
1412               get_variable, open_read_file
1413   
1414    IMPLICIT NONE
1415   
1416    INTEGER(iwp) ::  b          !< loop index: size bins
1417    INTEGER(iwp) ::  c          !< loop index: chemical components
1418    INTEGER(iwp) ::  ee         !< index: end
1419    INTEGER(iwp) ::  g          !< loop index: gases
1420    INTEGER(iwp) ::  i          !< loop index: x-direction
1421    INTEGER(iwp) ::  id_faero   !< NetCDF id of PIDS_SALSA
1422    INTEGER(iwp) ::  id_fchem   !< NetCDF id of PIDS_CHEM
1423    INTEGER(iwp) ::  j          !< loop index: y-direction
1424    INTEGER(iwp) ::  k          !< loop index: z-direction
1425    INTEGER(iwp) ::  kk         !< loop index: z-direction
1426    INTEGER(iwp) ::  nz_file    !< Number of grid-points in file (heights)                           
1427    INTEGER(iwp) ::  prunmode
1428    INTEGER(iwp) ::  ss !< index: start
1429    LOGICAL  ::  netcdf_extend = .FALSE. !< Flag indicating wether netcdf
1430                                         !< topography input file or not
1431    REAL(wp), DIMENSION(nbins) ::  core  !< size of the bin mid aerosol particle,
1432    REAL(wp) ::  flag           !< flag to mask topography grid points
1433    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  pr_gas !< gas profiles
1434    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  pr_mass_fracs_a !< mass fraction
1435                                                              !< profiles: a
1436    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  pr_mass_fracs_b !< and b
1437    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  pr_nsect !< sectional size
1438                                                       !< distribution profile
1439    REAL(wp), DIMENSION(nbins)            ::  nsect  !< size distribution (#/m3)
1440    REAL(wp), DIMENSION(0:nz+1,nbins)     ::  pndist !< size dist as a function
1441                                                     !< of height (#/m3)
1442    REAL(wp), DIMENSION(0:nz+1)           ::  pnf2a  !< number fraction: bins 2a
1443    REAL(wp), DIMENSION(0:nz+1,maxspec)   ::  pvf2a  !< mass distributions of 
1444                                                     !< aerosol species for a 
1445    REAL(wp), DIMENSION(0:nz+1,maxspec)   ::  pvf2b  !< and b-bins     
1446    REAL(wp), DIMENSION(0:nz+1)           ::  pvfOC1a !< mass fraction between
1447                                                     !< SO4 and OC in 1a
1448    REAL(wp), DIMENSION(:), ALLOCATABLE   ::  pr_z
1449
1450    prunmode = 1
1451!
1452!-- Bin mean aerosol particle volume (m3)
1453    core(:) = 0.0_wp
1454    core(1:nbins) = api6 * aero(1:nbins)%dmid ** 3.0_wp
1455!   
1456!-- Set concentrations to zero
1457    nsect(:)     = 0.0_wp
1458    pndist(:,:)  = 0.0_wp
1459    pnf2a(:)     = nf2a   
1460    pvf2a(:,:)   = 0.0_wp
1461    pvf2b(:,:)   = 0.0_wp
1462    pvfOC1a(:)   = 0.0_wp
1463
1464    IF ( isdtyp == 1 )  THEN
1465!
1466!--    Read input profiles from PIDS_SALSA   
1467!   
1468!--    Location-dependent size distributions and compositions.     
1469       INQUIRE( FILE='PIDS_SALSA'// TRIM( coupling_char ), EXIST=netcdf_extend )
1470       IF ( netcdf_extend )  THEN
1471!
1472!--       Open file in read-only mode 
1473          CALL open_read_file( 'PIDS_SALSA' // TRIM( coupling_char ), id_faero )
1474!
1475!--       Input heights   
1476          CALL netcdf_data_input_get_dimension_length( id_faero, nz_file, "profile_z" ) 
1477         
1478          ALLOCATE( pr_z(nz_file), pr_mass_fracs_a(maxspec,nz_file),           &
1479                    pr_mass_fracs_b(maxspec,nz_file), pr_nsect(nbins,nz_file) ) 
1480          CALL get_variable( id_faero, 'profile_z', pr_z ) 
1481!       
1482!--       Mass fracs profile: 1: H2SO4 (sulphuric acid), 2: OC (organic carbon),
1483!--                           3: BC (black carbon),      4: DU (dust), 
1484!--                           5: SS (sea salt),          6: HNO3 (nitric acid),
1485!--                           7: NH3 (ammonia)         
1486          CALL get_variable( id_faero, "profile_mass_fracs_a", pr_mass_fracs_a,&
1487                             0, nz_file-1, 0, maxspec-1 )
1488          CALL get_variable( id_faero, "profile_mass_fracs_b", pr_mass_fracs_b,&
1489                             0, nz_file-1, 0, maxspec-1 )
1490          CALL get_variable( id_faero, "profile_nsect", pr_nsect, 0, nz_file-1,&
1491                             0, nbins-1 )                   
1492         
1493          kk = 1
1494          DO  k = nzb, nz+1
1495             IF ( kk < nz_file )  THEN
1496                DO  WHILE ( pr_z(kk+1) <= zu(k) )
1497                   kk = kk + 1
1498                   IF ( kk == nz_file )  EXIT
1499                ENDDO
1500             ENDIF
1501             IF ( kk < nz_file )  THEN
1502!             
1503!--             Set initial value for gas compound tracers and initial values
1504                pvf2a(k,:) = pr_mass_fracs_a(:,kk) + ( zu(k) - pr_z(kk) ) / (  &
1505                            pr_z(kk+1) - pr_z(kk) ) * ( pr_mass_fracs_a(:,kk+1)&
1506                            - pr_mass_fracs_a(:,kk) )   
1507                pvf2b(k,:) = pr_mass_fracs_b(:,kk) + ( zu(k) - pr_z(kk) ) / (  &
1508                            pr_z(kk+1) - pr_z(kk) ) * ( pr_mass_fracs_b(:,kk+1)&
1509                            - pr_mass_fracs_b(:,kk) )             
1510                pndist(k,:) = pr_nsect(:,kk) + ( zu(k) - pr_z(kk) ) / (        &
1511                              pr_z(kk+1) - pr_z(kk) ) * ( pr_nsect(:,kk+1) -   &
1512                              pr_nsect(:,kk) )
1513             ELSE
1514                pvf2a(k,:) = pr_mass_fracs_a(:,kk)       
1515                pvf2b(k,:) = pr_mass_fracs_b(:,kk)
1516                pndist(k,:) = pr_nsect(:,kk)
1517             ENDIF
1518             IF ( iso4 < 0 )  THEN
1519                pvf2a(k,1) = 0.0_wp
1520                pvf2b(k,1) = 0.0_wp
1521             ENDIF
1522             IF ( ioc < 0 )  THEN
1523                pvf2a(k,2) = 0.0_wp
1524                pvf2b(k,2) = 0.0_wp
1525             ENDIF
1526             IF ( ibc < 0 )  THEN
1527                pvf2a(k,3) = 0.0_wp
1528                pvf2b(k,3) = 0.0_wp
1529             ENDIF
1530             IF ( idu < 0 )  THEN
1531                pvf2a(k,4) = 0.0_wp
1532                pvf2b(k,4) = 0.0_wp
1533             ENDIF
1534             IF ( iss < 0 )  THEN
1535                pvf2a(k,5) = 0.0_wp
1536                pvf2b(k,5) = 0.0_wp
1537             ENDIF
1538             IF ( ino < 0 )  THEN
1539                pvf2a(k,6) = 0.0_wp
1540                pvf2b(k,6) = 0.0_wp
1541             ENDIF
1542             IF ( inh < 0 )  THEN
1543                pvf2a(k,7) = 0.0_wp
1544                pvf2b(k,7) = 0.0_wp
1545             ENDIF
1546!
1547!--          Then normalise the mass fraction so that SUM = 1
1548             pvf2a(k,:) = pvf2a(k,:) / SUM( pvf2a(k,:) )
1549             IF ( SUM( pvf2b(k,:) ) > 0.0_wp ) pvf2b(k,:) = pvf2b(k,:) /       &
1550                                                            SUM( pvf2b(k,:) )
1551          ENDDO         
1552          DEALLOCATE( pr_z, pr_mass_fracs_a, pr_mass_fracs_b, pr_nsect )
1553       ELSE
1554          message_string = 'Input file '// TRIM( 'PIDS_SALSA' ) //             &
1555                           TRIM( coupling_char ) // ' for SALSA missing!'
1556          CALL message( 'salsa_mod: aerosol_init', 'SA0032', 1, 2, 0, 6, 0 )               
1557       ENDIF   ! netcdf_extend   
1558 
1559    ELSEIF ( isdtyp == 0 )  THEN
1560!
1561!--    Mass fractions for species in a and b-bins
1562       IF ( iso4 > 0 )  THEN
1563          pvf2a(:,1) = mass_fracs_a(iso4) 
1564          pvf2b(:,1) = mass_fracs_b(iso4)
1565       ENDIF
1566       IF ( ioc > 0 )  THEN
1567          pvf2a(:,2) = mass_fracs_a(ioc)
1568          pvf2b(:,2) = mass_fracs_b(ioc) 
1569       ENDIF
1570       IF ( ibc > 0 )  THEN
1571          pvf2a(:,3) = mass_fracs_a(ibc) 
1572          pvf2b(:,3) = mass_fracs_b(ibc)
1573       ENDIF
1574       IF ( idu > 0 )  THEN
1575          pvf2a(:,4) = mass_fracs_a(idu)
1576          pvf2b(:,4) = mass_fracs_b(idu) 
1577       ENDIF
1578       IF ( iss > 0 )  THEN
1579          pvf2a(:,5) = mass_fracs_a(iss)
1580          pvf2b(:,5) = mass_fracs_b(iss) 
1581       ENDIF
1582       IF ( ino > 0 )  THEN
1583          pvf2a(:,6) = mass_fracs_a(ino)
1584          pvf2b(:,6) = mass_fracs_b(ino)
1585       ENDIF
1586       IF ( inh > 0 )  THEN
1587          pvf2a(:,7) = mass_fracs_a(inh)
1588          pvf2b(:,7) = mass_fracs_b(inh)
1589       ENDIF
1590       DO  k = nzb, nz+1
1591          pvf2a(k,:) = pvf2a(k,:) / SUM( pvf2a(k,:) )
1592          IF ( SUM( pvf2b(k,:) ) > 0.0_wp ) pvf2b(k,:) = pvf2b(k,:) /          &
1593                                                         SUM( pvf2b(k,:) )
1594       ENDDO
1595       
1596       CALL size_distribution( n_lognorm, dpg, sigmag, nsect )
1597!
1598!--    Normalize by the given total number concentration
1599       nsect = nsect * SUM( n_lognorm ) * 1.0E+6_wp / SUM( nsect )     
1600       DO  b = in1a, fn2b
1601          pndist(:,b) = nsect(b)
1602       ENDDO
1603    ENDIF
1604   
1605    IF ( igctyp == 1 )  THEN
1606!
1607!--    Read input profiles from PIDS_CHEM   
1608!   
1609!--    Location-dependent size distributions and compositions.     
1610       INQUIRE( FILE='PIDS_CHEM' // TRIM( coupling_char ), EXIST=netcdf_extend )
1611       IF ( netcdf_extend  .AND.  .NOT. salsa_gases_from_chem )  THEN
1612!
1613!--       Open file in read-only mode     
1614          CALL open_read_file( 'PIDS_CHEM' // TRIM( coupling_char ), id_fchem )
1615!
1616!--       Input heights   
1617          CALL netcdf_data_input_get_dimension_length( id_fchem, nz_file, "profile_z" ) 
1618          ALLOCATE( pr_z(nz_file), pr_gas(ngast,nz_file) ) 
1619          CALL get_variable( id_fchem, 'profile_z', pr_z ) 
1620!       
1621!--       Gases:
1622          CALL get_variable( id_fchem, "profile_H2SO4", pr_gas(1,:) )
1623          CALL get_variable( id_fchem, "profile_HNO3", pr_gas(2,:) )
1624          CALL get_variable( id_fchem, "profile_NH3", pr_gas(3,:) )
1625          CALL get_variable( id_fchem, "profile_OCNV", pr_gas(4,:) )
1626          CALL get_variable( id_fchem, "profile_OCSV", pr_gas(5,:) )
1627         
1628          kk = 1
1629          DO  k = nzb, nz+1
1630             IF ( kk < nz_file )  THEN
1631                DO  WHILE ( pr_z(kk+1) <= zu(k) )
1632                   kk = kk + 1
1633                   IF ( kk == nz_file )  EXIT
1634                ENDDO
1635             ENDIF
1636             IF ( kk < nz_file )  THEN
1637!             
1638!--             Set initial value for gas compound tracers and initial values
1639                DO  g = 1, ngast
1640                   salsa_gas(g)%init(k) =  pr_gas(g,kk) + ( zu(k) - pr_z(kk) ) &
1641                                           / ( pr_z(kk+1) - pr_z(kk) ) *       &
1642                                           ( pr_gas(g,kk+1) - pr_gas(g,kk) )
1643                   salsa_gas(g)%conc(k,:,:) = salsa_gas(g)%init(k)
1644                ENDDO
1645             ELSE
1646                DO  g = 1, ngast
1647                   salsa_gas(g)%init(k) =  pr_gas(g,kk) 
1648                   salsa_gas(g)%conc(k,:,:) = salsa_gas(g)%init(k)
1649                ENDDO
1650             ENDIF
1651          ENDDO
1652         
1653          DEALLOCATE( pr_z, pr_gas )
1654       ELSEIF ( .NOT. netcdf_extend  .AND.  .NOT.  salsa_gases_from_chem )  THEN
1655          message_string = 'Input file '// TRIM( 'PIDS_CHEM' ) //              &
1656                           TRIM( coupling_char ) // ' for SALSA missing!'
1657          CALL message( 'salsa_mod: aerosol_init', 'SA0033', 1, 2, 0, 6, 0 )               
1658       ENDIF   ! netcdf_extend     
1659    ENDIF
1660
1661    IF ( ioc > 0  .AND.  iso4 > 0 )  THEN     
1662!--    Both are there, so use the given "massDistrA"
1663       pvfOC1a(:) = pvf2a(:,2) / ( pvf2a(:,2) + pvf2a(:,1) )  ! Normalize
1664    ELSEIF ( ioc > 0 )  THEN
1665!--    Pure organic carbon
1666       pvfOC1a(:) = 1.0_wp
1667    ELSEIF ( iso4 > 0 )  THEN
1668!--    Pure SO4
1669       pvfOC1a(:) = 0.0_wp   
1670    ELSE
1671       message_string = 'Either OC or SO4 must be active for aerosol region 1a!'
1672       CALL message( 'salsa_mod: aerosol_init', 'SA0021', 1, 2, 0, 6, 0 )
1673    ENDIF   
1674   
1675!
1676!-- Initialize concentrations
1677    DO  i = nxlg, nxrg 
1678       DO  j = nysg, nyng
1679          DO  k = nzb, nzt+1
1680!
1681!--          Predetermine flag to mask topography         
1682             flag = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i), 0 ) )
1683!         
1684!--          a) Number concentrations
1685!--           Region 1:
1686             DO  b = in1a, fn1a
1687                aerosol_number(b)%conc(k,j,i) = pndist(k,b) * flag
1688                IF ( prunmode == 1 )  THEN
1689                   aerosol_number(b)%init = pndist(:,b)
1690                ENDIF
1691             ENDDO
1692!             
1693!--           Region 2:
1694             IF ( nreg > 1 )  THEN
1695                DO  b = in2a, fn2a
1696                   aerosol_number(b)%conc(k,j,i) = MAX( 0.0_wp, pnf2a(k) ) *   &
1697                                                    pndist(k,b) * flag
1698                   IF ( prunmode == 1 )  THEN
1699                      aerosol_number(b)%init = MAX( 0.0_wp, nf2a ) * pndist(:,b)
1700                   ENDIF
1701                ENDDO
1702                IF ( .NOT. no_insoluble )  THEN
1703                   DO  b = in2b, fn2b
1704                      IF ( pnf2a(k) < 1.0_wp )  THEN             
1705                         aerosol_number(b)%conc(k,j,i) = MAX( 0.0_wp, 1.0_wp   &
1706                                               - pnf2a(k) ) * pndist(k,b) * flag
1707                         IF ( prunmode == 1 )  THEN
1708                            aerosol_number(b)%init = MAX( 0.0_wp, 1.0_wp -     &
1709                                                          nf2a ) * pndist(:,b)
1710                         ENDIF
1711                      ENDIF
1712                   ENDDO
1713                ENDIF
1714             ENDIF
1715!
1716!--          b) Aerosol mass concentrations
1717!--             bin subrange 1: done here separately due to the SO4/OC convention
1718!--          SO4:
1719             IF ( iso4 > 0 )  THEN
1720                ss = ( iso4 - 1 ) * nbins + in1a !< start
1721                ee = ( iso4 - 1 ) * nbins + fn1a !< end
1722                b = in1a
1723                DO  c = ss, ee
1724                   aerosol_mass(c)%conc(k,j,i) = MAX( 0.0_wp, 1.0_wp -         &
1725                                                  pvfOC1a(k) ) * pndist(k,b) * &
1726                                                  core(b) * arhoh2so4 * flag
1727                   IF ( prunmode == 1 )  THEN
1728                      aerosol_mass(c)%init = MAX( 0.0_wp, 1.0_wp - MAXVAL(     &
1729                                             pvfOC1a ) ) * pndist(:,b) *       &
1730                                             core(b) * arhoh2so4
1731                   ENDIF
1732                   b = b+1
1733                ENDDO
1734             ENDIF
1735!--          OC:
1736             IF ( ioc > 0 ) THEN
1737                ss = ( ioc - 1 ) * nbins + in1a !< start
1738                ee = ( ioc - 1 ) * nbins + fn1a !< end
1739                b = in1a
1740                DO  c = ss, ee 
1741                   aerosol_mass(c)%conc(k,j,i) = MAX( 0.0_wp, pvfOC1a(k) ) *   &
1742                                           pndist(k,b) * core(b) * arhooc * flag
1743                   IF ( prunmode == 1 )  THEN
1744                      aerosol_mass(c)%init = MAX( 0.0_wp, MAXVAL( pvfOC1a ) )  &
1745                                             * pndist(:,b) *  core(b) * arhooc
1746                   ENDIF
1747                   b = b+1
1748                ENDDO 
1749             ENDIF
1750             
1751             prunmode = 3  ! Init only once
1752 
1753          ENDDO !< k
1754       ENDDO !< j
1755    ENDDO !< i
1756   
1757!
1758!-- c) Aerosol mass concentrations
1759!--    bin subrange 2:
1760    IF ( nreg > 1 ) THEN
1761   
1762       IF ( iso4 > 0 ) THEN
1763          CALL set_aero_mass( iso4, pvf2a(:,1), pvf2b(:,1), pnf2a, pndist,     &
1764                              core, arhoh2so4 )
1765       ENDIF
1766       IF ( ioc > 0 ) THEN
1767          CALL set_aero_mass( ioc, pvf2a(:,2), pvf2b(:,2), pnf2a, pndist, core,&
1768                              arhooc )
1769       ENDIF
1770       IF ( ibc > 0 ) THEN
1771          CALL set_aero_mass( ibc, pvf2a(:,3), pvf2b(:,3), pnf2a, pndist, core,&
1772                              arhobc )
1773       ENDIF
1774       IF ( idu > 0 ) THEN
1775          CALL set_aero_mass( idu, pvf2a(:,4), pvf2b(:,4), pnf2a, pndist, core,&
1776                              arhodu )
1777       ENDIF
1778       IF ( iss > 0 ) THEN
1779          CALL set_aero_mass( iss, pvf2a(:,5), pvf2b(:,5), pnf2a, pndist, core,&
1780                              arhoss )
1781       ENDIF
1782       IF ( ino > 0 ) THEN
1783          CALL set_aero_mass( ino, pvf2a(:,6), pvf2b(:,6), pnf2a, pndist, core,&
1784                              arhohno3 )
1785       ENDIF
1786       IF ( inh > 0 ) THEN
1787          CALL set_aero_mass( inh, pvf2a(:,7), pvf2b(:,7), pnf2a, pndist, core,&
1788                              arhonh3 )
1789       ENDIF
1790
1791    ENDIF
1792   
1793 END SUBROUTINE aerosol_init
1794 
1795!------------------------------------------------------------------------------!
1796! Description:
1797! ------------
1798!> Create a lognormal size distribution and discretise to a sectional
1799!> representation.
1800!------------------------------------------------------------------------------!
1801 SUBROUTINE size_distribution( in_ntot, in_dpg, in_sigma, psd_sect )
1802   
1803    IMPLICIT NONE
1804   
1805!-- Log-normal size distribution: modes   
1806    REAL(wp), DIMENSION(:), INTENT(in) ::  in_dpg    !< geometric mean diameter
1807                                                     !< (micrometres)
1808    REAL(wp), DIMENSION(:), INTENT(in) ::  in_ntot   !< number conc. (#/cm3)
1809    REAL(wp), DIMENSION(:), INTENT(in) ::  in_sigma  !< standard deviation
1810    REAL(wp), DIMENSION(:), INTENT(inout) ::  psd_sect !< sectional size
1811                                                       !< distribution
1812    INTEGER(iwp) ::  b          !< running index: bin
1813    INTEGER(iwp) ::  ib         !< running index: iteration
1814    REAL(wp) ::  d1             !< particle diameter (m, dummy)
1815    REAL(wp) ::  d2             !< particle diameter (m, dummy)
1816    REAL(wp) ::  delta_d        !< (d2-d1)/10                                                     
1817    REAL(wp) ::  deltadp        !< bin width
1818    REAL(wp) ::  dmidi          !< ( d1 + d2 ) / 2
1819   
1820    DO  b = in1a, fn2b !< aerosol size bins
1821       psd_sect(b) = 0.0_wp
1822!--    Particle diameter at the low limit (largest in the bin) (m)
1823       d1 = ( aero(b)%vlolim / api6 ) ** ( 1.0_wp / 3.0_wp )
1824!--    Particle diameter at the high limit (smallest in the bin) (m)
1825       d2 = ( aero(b)%vhilim / api6 ) ** ( 1.0_wp / 3.0_wp )
1826!--    Span of particle diameter in a bin (m)
1827       delta_d = ( d2 - d1 ) / 10.0_wp
1828!--    Iterate:             
1829       DO  ib = 1, 10
1830          d1 = ( aero(b)%vlolim / api6 ) ** ( 1.0_wp / 3.0_wp ) + ( ib - 1)    &
1831               * delta_d
1832          d2 = d1 + delta_d
1833          dmidi = ( d1 + d2 ) / 2.0_wp
1834          deltadp = LOG10( d2 / d1 )
1835         
1836!--       Size distribution
1837!--       in_ntot = total number, total area, or total volume concentration
1838!--       in_dpg = geometric-mean number, area, or volume diameter
1839!--       n(k) = number, area, or volume concentration in a bin
1840!--       n_lognorm and dpg converted to units of #/m3 and m
1841          psd_sect(b) = psd_sect(b) + SUM( in_ntot * 1.0E+6_wp * deltadp /     &
1842                     ( SQRT( 2.0_wp * pi ) * LOG10( in_sigma ) ) *             &
1843                     EXP( -LOG10( dmidi / ( 1.0E-6_wp * in_dpg ) )**2.0_wp /   &
1844                     ( 2.0_wp * LOG10( in_sigma ) ** 2.0_wp ) ) )
1845 
1846       ENDDO
1847    ENDDO
1848   
1849 END SUBROUTINE size_distribution
1850
1851!------------------------------------------------------------------------------!
1852! Description:
1853! ------------
1854!> Sets the mass concentrations to aerosol arrays in 2a and 2b.
1855!>
1856!> Tomi Raatikainen, FMI, 29.2.2016
1857!------------------------------------------------------------------------------!
1858 SUBROUTINE set_aero_mass( ispec, ppvf2a, ppvf2b, ppnf2a, ppndist, pcore, prho )
1859   
1860    IMPLICIT NONE
1861
1862    INTEGER(iwp), INTENT(in) :: ispec  !< Aerosol species index
1863    REAL(wp), INTENT(in) ::  pcore(nbins) !< Aerosol bin mid core volume   
1864    REAL(wp), INTENT(in) ::  ppndist(0:nz+1,nbins) !< Aerosol size distribution
1865    REAL(wp), INTENT(in) ::  ppnf2a(0:nz+1) !< Number fraction for 2a   
1866    REAL(wp), INTENT(in) ::  ppvf2a(0:nz+1) !< Mass distributions for a
1867    REAL(wp), INTENT(in) ::  ppvf2b(0:nz+1) !< and b bins   
1868    REAL(wp), INTENT(in) ::  prho !< Aerosol density
1869    INTEGER(iwp) ::  b  !< loop index
1870    INTEGER(iwp) ::  c  !< loop index       
1871    INTEGER(iwp) ::  ee !< index: end
1872    INTEGER(iwp) ::  i  !< loop index
1873    INTEGER(iwp) ::  j  !< loop index
1874    INTEGER(iwp) ::  k  !< loop index
1875    INTEGER(iwp) ::  prunmode  !< 1 = initialise
1876    INTEGER(iwp) ::  ss !< index: start
1877    REAL(wp) ::  flag   !< flag to mask topography grid points
1878   
1879    prunmode = 1
1880   
1881    DO i = nxlg, nxrg 
1882       DO j = nysg, nyng
1883          DO k = nzb, nzt+1 
1884!
1885!--          Predetermine flag to mask topography
1886             flag = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i), 0 ) ) 
1887!             
1888!--          Regime 2a:
1889             ss = ( ispec - 1 ) * nbins + in2a
1890             ee = ( ispec - 1 ) * nbins + fn2a
1891             b = in2a
1892             DO c = ss, ee
1893                aerosol_mass(c)%conc(k,j,i) = MAX( 0.0_wp, ppvf2a(k) ) *       &
1894                               ppnf2a(k) * ppndist(k,b) * pcore(b) * prho * flag
1895                IF ( prunmode == 1 )  THEN
1896                   aerosol_mass(c)%init = MAX( 0.0_wp, MAXVAL( ppvf2a(:) ) ) * &
1897                                          MAXVAL( ppnf2a ) * pcore(b) * prho * &
1898                                          MAXVAL( ppndist(:,b) ) 
1899                ENDIF
1900                b = b+1
1901             ENDDO
1902!--          Regime 2b:
1903             IF ( .NOT. no_insoluble )  THEN
1904                ss = ( ispec - 1 ) * nbins + in2b
1905                ee = ( ispec - 1 ) * nbins + fn2b
1906                b = in2a
1907                DO c = ss, ee
1908                   aerosol_mass(c)%conc(k,j,i) = MAX( 0.0_wp, ppvf2b(k) ) * (  &
1909                                         1.0_wp - ppnf2a(k) ) * ppndist(k,b) * &
1910                                         pcore(b) * prho * flag
1911                   IF ( prunmode == 1 )  THEN
1912                      aerosol_mass(c)%init = MAX( 0.0_wp, MAXVAL( ppvf2b(:) ) )&
1913                                        * ( 1.0_wp - MAXVAL( ppnf2a ) ) *      &
1914                                        MAXVAL( ppndist(:,b) ) * pcore(b) * prho
1915                   ENDIF
1916                   b = b+1
1917                ENDDO
1918             ENDIF
1919             prunmode = 3  ! Init only once
1920          ENDDO
1921       ENDDO
1922    ENDDO
1923 END SUBROUTINE set_aero_mass
1924
1925!------------------------------------------------------------------------------!
1926! Description:
1927! ------------
1928!> Swapping of timelevels
1929!------------------------------------------------------------------------------!
1930 SUBROUTINE salsa_swap_timelevel( mod_count )
1931
1932    IMPLICIT NONE
1933
1934    INTEGER(iwp), INTENT(IN) ::  mod_count  !<
1935    INTEGER(iwp) ::  b  !<   
1936    INTEGER(iwp) ::  c  !<   
1937    INTEGER(iwp) ::  g  !<
1938
1939!
1940!-- Example for prognostic variable "prog_var"
1941#if defined( __nopointer )
1942    IF ( myid == 0 )  THEN
1943       message_string =  ' SALSA runs only with POINTER Version'
1944       CALL message( 'salsa_swap_timelevel', 'SA0022', 1, 2, 0, 6, 0 )
1945    ENDIF
1946#else
1947   
1948    SELECT CASE ( mod_count )
1949
1950       CASE ( 0 )
1951
1952          DO  b = 1, nbins
1953             aerosol_number(b)%conc(nzb:nzt+1,nysg:nyng,nxlg:nxrg)   =>        &
1954                nconc_1(:,:,:,b)
1955             aerosol_number(b)%conc_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg) =>        &
1956                nconc_2(:,:,:,b)
1957             DO  c = 1, ncc_tot
1958                aerosol_mass((c-1)*nbins+b)%conc(nzb:nzt+1,nysg:nyng,nxlg:nxrg)  &
1959                   => mconc_1(:,:,:,(c-1)*nbins+b)
1960                aerosol_mass((c-1)*nbins+b)%conc_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg)&
1961                   => mconc_2(:,:,:,(c-1)*nbins+b)
1962             ENDDO
1963          ENDDO
1964         
1965          IF ( .NOT. salsa_gases_from_chem )  THEN
1966             DO  g = 1, ngast
1967                salsa_gas(g)%conc(nzb:nzt+1,nysg:nyng,nxlg:nxrg)   =>          &
1968                   gconc_1(:,:,:,g)
1969                salsa_gas(g)%conc_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg) =>          &
1970                   gconc_2(:,:,:,g)
1971             ENDDO
1972          ENDIF
1973
1974       CASE ( 1 )
1975
1976          DO  b = 1, nbins
1977             aerosol_number(b)%conc(nzb:nzt+1,nysg:nyng,nxlg:nxrg)   =>        &
1978                nconc_2(:,:,:,b)
1979             aerosol_number(b)%conc_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg) =>        &
1980                nconc_1(:,:,:,b)
1981             DO  c = 1, ncc_tot
1982                aerosol_mass((c-1)*nbins+b)%conc(nzb:nzt+1,nysg:nyng,nxlg:nxrg)  &
1983                   => mconc_2(:,:,:,(c-1)*nbins+b)
1984                aerosol_mass((c-1)*nbins+b)%conc_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg)&
1985                   => mconc_1(:,:,:,(c-1)*nbins+b)
1986             ENDDO
1987          ENDDO
1988         
1989          IF ( .NOT. salsa_gases_from_chem )  THEN
1990             DO  g = 1, ngast
1991                salsa_gas(g)%conc(nzb:nzt+1,nysg:nyng,nxlg:nxrg)   =>          &
1992                   gconc_2(:,:,:,g)
1993                salsa_gas(g)%conc_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg) =>          &
1994                   gconc_1(:,:,:,g)
1995             ENDDO
1996          ENDIF
1997
1998    END SELECT
1999#endif
2000
2001 END SUBROUTINE salsa_swap_timelevel
2002
2003
2004!------------------------------------------------------------------------------!
2005! Description:
2006! ------------
2007!> This routine reads the respective restart data.
2008!------------------------------------------------------------------------------!
2009 SUBROUTINE salsa_rrd_local 
2010
2011   
2012    IMPLICIT NONE
2013   
2014    CHARACTER (LEN=20) :: field_char   !<
2015    INTEGER(iwp) ::  b  !<   
2016    INTEGER(iwp) ::  c  !<
2017    INTEGER(iwp) ::  g  !<
2018    INTEGER(iwp) ::  i  !<
2019    INTEGER(iwp) ::  j  !<
2020    INTEGER(iwp) ::  k  !<   
2021   
2022    IF ( read_restart_data_salsa )  THEN
2023       READ ( 13 )  field_char
2024
2025       DO  WHILE ( TRIM( field_char ) /= '*** end salsa ***' )
2026       
2027          DO b = 1, nbins
2028             READ ( 13 )  aero(b)%vlolim
2029             READ ( 13 )  aero(b)%vhilim
2030             READ ( 13 )  aero(b)%dmid
2031             READ ( 13 )  aero(b)%vratiohi
2032             READ ( 13 )  aero(b)%vratiolo
2033          ENDDO
2034
2035          DO  i = nxl, nxr
2036             DO  j = nys, nyn
2037                DO k = nzb+1, nzt
2038                   DO  b = 1, nbins
2039                      READ ( 13 )  aerosol_number(b)%conc(k,j,i)
2040                      DO  c = 1, ncc_tot
2041                         READ ( 13 )  aerosol_mass((c-1)*nbins+b)%conc(k,j,i)
2042                      ENDDO
2043                   ENDDO
2044                   IF ( .NOT. salsa_gases_from_chem )  THEN
2045                      DO  g = 1, ngast
2046                         READ ( 13 )  salsa_gas(g)%conc(k,j,i)
2047                      ENDDO 
2048                   ENDIF
2049                ENDDO
2050             ENDDO
2051          ENDDO
2052
2053          READ ( 13 )  field_char
2054
2055       ENDDO
2056       
2057    ENDIF
2058
2059 END SUBROUTINE salsa_rrd_local
2060   
2061
2062!------------------------------------------------------------------------------!
2063! Description:
2064! ------------
2065!> This routine writes the respective restart data.
2066!> Note that the following input variables in PARIN have to be equal between
2067!> restart runs:
2068!>    listspec, nbin, nbin2, nf2a, ncc, mass_fracs_a, mass_fracs_b
2069!------------------------------------------------------------------------------!
2070 SUBROUTINE salsa_wrd_local
2071
2072    IMPLICIT NONE
2073   
2074    INTEGER(iwp) ::  b  !<   
2075    INTEGER(iwp) ::  c  !<
2076    INTEGER(iwp) ::  g  !<
2077    INTEGER(iwp) ::  i  !<
2078    INTEGER(iwp) ::  j  !<
2079    INTEGER(iwp) ::  k  !<
2080   
2081    IF ( write_binary  .AND.  write_binary_salsa )  THEN
2082       
2083       DO b = 1, nbins
2084          WRITE ( 14 )  aero(b)%vlolim
2085          WRITE ( 14 )  aero(b)%vhilim
2086          WRITE ( 14 )  aero(b)%dmid
2087          WRITE ( 14 )  aero(b)%vratiohi
2088          WRITE ( 14 )  aero(b)%vratiolo
2089       ENDDO
2090       
2091       DO  i = nxl, nxr
2092          DO  j = nys, nyn
2093             DO  k = nzb+1, nzt
2094                DO  b = 1, nbins
2095                   WRITE ( 14 )  aerosol_number(b)%conc(k,j,i)
2096                   DO  c = 1, ncc_tot
2097                      WRITE ( 14 )  aerosol_mass((c-1)*nbins+b)%conc(k,j,i)
2098                   ENDDO
2099                ENDDO
2100                IF ( .NOT. salsa_gases_from_chem )  THEN
2101                   DO  g = 1, ngast
2102                      WRITE ( 14 )  salsa_gas(g)%conc(k,j,i)
2103                   ENDDO 
2104                ENDIF
2105             ENDDO
2106          ENDDO
2107       ENDDO
2108       
2109       WRITE ( 14 )  '*** end salsa ***   '
2110         
2111    ENDIF
2112       
2113 END SUBROUTINE salsa_wrd_local   
2114
2115
2116!------------------------------------------------------------------------------!
2117! Description:
2118! ------------
2119!> Performs necessary unit and dimension conversion between the host model and
2120!> SALSA module, and calls the main SALSA routine.
2121!> Partially adobted form the original SALSA boxmodel version.
2122!> Now takes masses in as kg/kg from LES!! Converted to m3/m3 for SALSA
2123!> 05/2016 Juha: This routine is still pretty much in its original shape.
2124!>               It's dumb as a mule and twice as ugly, so implementation of
2125!>               an improved solution is necessary sooner or later.
2126!> Juha Tonttila, FMI, 2014
2127!> Jaakko Ahola, FMI, 2016
2128!> Only aerosol processes included, Mona Kurppa, UHel, 2017
2129!------------------------------------------------------------------------------!
2130 SUBROUTINE salsa_driver( i, j, prunmode )
2131
2132    USE arrays_3d,                                                             &
2133        ONLY: pt_p, q_p, rho_air_zw, u, v, w
2134       
2135    USE plant_canopy_model_mod,                                                &
2136        ONLY: lad_s
2137       
2138    USE surface_mod,                                                           &
2139        ONLY:  surf_def_h, surf_def_v, surf_lsm_h, surf_lsm_v, surf_usm_h,     &
2140               surf_usm_v
2141 
2142    IMPLICIT NONE
2143   
2144    INTEGER(iwp), INTENT(in) ::  i   !< loop index
2145    INTEGER(iwp), INTENT(in) ::  j   !< loop index
2146    INTEGER(iwp), INTENT(in) ::  prunmode !< 1: Initialization call
2147                                          !< 2: Spinup period call
2148                                          !< 3: Regular runtime call
2149!-- Local variables
2150    TYPE(t_section), DIMENSION(fn2b) ::  aero_old !< helper array
2151    INTEGER(iwp) ::  bb     !< loop index
2152    INTEGER(iwp) ::  cc     !< loop index
2153    INTEGER(iwp) ::  endi   !< end index
2154    INTEGER(iwp) ::  k_wall !< vertical index of topography top
2155    INTEGER(iwp) ::  k      !< loop index
2156    INTEGER(iwp) ::  l      !< loop index
2157    INTEGER(iwp) ::  nc_h2o !< index of H2O in the prtcl index table
2158    INTEGER(iwp) ::  ss     !< loop index
2159    INTEGER(iwp) ::  str    !< start index
2160    INTEGER(iwp) ::  vc     !< default index in prtcl
2161    REAL(wp) ::  cw_old     !< previous H2O mixing ratio
2162    REAL(wp) ::  flag       !< flag to mask topography grid points
2163    REAL(wp), DIMENSION(nzb:nzt+1) ::  in_adn !< air density (kg/m3)   
2164    REAL(wp), DIMENSION(nzb:nzt+1) ::  in_cs  !< H2O sat. vapour conc.
2165    REAL(wp), DIMENSION(nzb:nzt+1) ::  in_cw  !< H2O vapour concentration
2166    REAL(wp) ::  in_lad                       !< leaf area density (m2/m3)
2167    REAL(wp), DIMENSION(nzb:nzt+1) ::  in_p   !< pressure (Pa)     
2168    REAL(wp) ::  in_rh                        !< relative humidity                     
2169    REAL(wp), DIMENSION(nzb:nzt+1) ::  in_t   !< temperature (K)
2170    REAL(wp), DIMENSION(nzb:nzt+1) ::  in_u   !< wind magnitude (m/s)
2171    REAL(wp), DIMENSION(nzb:nzt+1) ::  kvis   !< kinematic viscosity of air(m2/s)                                           
2172    REAL(wp), DIMENSION(nzb:nzt+1,fn2b) ::  Sc      !< particle Schmidt number   
2173    REAL(wp), DIMENSION(nzb:nzt+1,fn2b) ::  vd      !< particle fall seed (m/s,
2174                                                    !< sedimentation velocity)
2175    REAL(wp), DIMENSION(nzb:nzt+1) ::  ppm_to_nconc !< Conversion factor
2176                                                    !< from ppm to #/m3                                                     
2177    REAL(wp) ::  zgso4  !< SO4
2178    REAL(wp) ::  zghno3 !< HNO3
2179    REAL(wp) ::  zgnh3  !< NH3
2180    REAL(wp) ::  zgocnv !< non-volatile OC
2181    REAL(wp) ::  zgocsv !< semi-volatile OC
2182   
2183    aero_old(:)%numc = 0.0_wp
2184    in_adn           = 0.0_wp   
2185    in_cs            = 0.0_wp
2186    in_cw            = 0.0_wp 
2187    in_lad           = 0.0_wp
2188    in_rh            = 0.0_wp
2189    in_p             = 0.0_wp 
2190    in_t             = 0.0_wp 
2191    in_u             = 0.0_wp
2192    kvis             = 0.0_wp
2193    Sc               = 0.0_wp
2194    vd               = 0.0_wp
2195    ppm_to_nconc     = 1.0_wp
2196    zgso4            = nclim
2197    zghno3           = nclim
2198    zgnh3            = nclim
2199    zgocnv           = nclim
2200    zgocsv           = nclim
2201   
2202!       
2203!-- Aerosol number is always set, but mass can be uninitialized
2204    DO cc = 1, nbins
2205       aero(cc)%volc     = 0.0_wp
2206       aero_old(cc)%volc = 0.0_wp
2207    ENDDO 
2208!   
2209!-- Set the salsa runtime config (How to make this more efficient?)
2210    CALL set_salsa_runtime( prunmode )
2211!             
2212!-- Calculate thermodynamic quantities needed in SALSA
2213    CALL salsa_thrm_ij( i, j, p_ij=in_p, temp_ij=in_t, cw_ij=in_cw,            &
2214                        cs_ij=in_cs, adn_ij=in_adn )
2215!
2216!-- Magnitude of wind: needed for deposition
2217    IF ( lsdepo )  THEN
2218       in_u(nzb+1:nzt) = SQRT(                                                 &
2219                   ( 0.5_wp * ( u(nzb+1:nzt,j,i) + u(nzb+1:nzt,j,i+1) ) )**2 + & 
2220                   ( 0.5_wp * ( v(nzb+1:nzt,j,i) + v(nzb+1:nzt,j+1,i) ) )**2 + &
2221                   ( 0.5_wp * ( w(nzb:nzt-1,j,i) + w(nzb+1:nzt,j,  i) ) )**2 )
2222    ENDIF
2223!
2224!-- Calculate conversion factors for gas concentrations
2225    ppm_to_nconc = for_ppm_to_nconc * in_p / in_t
2226!
2227!-- Determine topography-top index on scalar grid
2228    k_wall = MAXLOC( MERGE( 1, 0, BTEST( wall_flags_0(:,j,i), 12 ) ),          &
2229                     DIM = 1 ) - 1     
2230               
2231    DO k = nzb+1, nzt
2232!
2233!--    Predetermine flag to mask topography
2234       flag = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i), 0 ) )
2235!       
2236!--    Do not run inside buildings       
2237       IF ( flag == 0.0_wp )  CYCLE   
2238!
2239!--    Wind velocity for dry depositon on vegetation   
2240       IF ( lsdepo_vege  .AND.  plant_canopy  )  THEN
2241          in_lad = lad_s(k-k_wall,j,i)
2242       ENDIF       
2243!
2244!--    For initialization and spinup, limit the RH with the parameter rhlim
2245       IF ( prunmode < 3 ) THEN
2246          in_cw(k) = MIN( in_cw(k), in_cs(k) * rhlim )
2247       ELSE
2248          in_cw(k) = in_cw(k)
2249       ENDIF
2250       cw_old = in_cw(k) !* in_adn(k)
2251!               
2252!--    Set volume concentrations:
2253!--    Sulphate (SO4) or sulphuric acid H2SO4
2254       IF ( iso4 > 0 )  THEN
2255          vc = 1
2256          str = ( iso4-1 ) * nbins + 1    ! start index
2257          endi = iso4 * nbins             ! end index
2258          cc = 1
2259          DO ss = str, endi
2260             aero(cc)%volc(vc) = aerosol_mass(ss)%conc(k,j,i) / arhoh2so4
2261             cc = cc+1
2262          ENDDO
2263          aero_old(1:nbins)%volc(vc) = aero(1:nbins)%volc(vc)
2264       ENDIF
2265       
2266!--    Organic carbon (OC) compounds
2267       IF ( ioc > 0 )  THEN
2268          vc = 2
2269          str = ( ioc-1 ) * nbins + 1
2270          endi = ioc * nbins
2271          cc = 1
2272          DO ss = str, endi
2273             aero(cc)%volc(vc) = aerosol_mass(ss)%conc(k,j,i) / arhooc 
2274             cc = cc+1
2275          ENDDO
2276          aero_old(1:nbins)%volc(vc) = aero(1:nbins)%volc(vc)
2277       ENDIF
2278       
2279!--    Black carbon (BC)
2280       IF ( ibc > 0 )  THEN
2281          vc = 3
2282          str = ( ibc-1 ) * nbins + 1 + fn1a
2283          endi = ibc * nbins
2284          cc = 1 + fn1a
2285          DO ss = str, endi
2286             aero(cc)%volc(vc) = aerosol_mass(ss)%conc(k,j,i) / arhobc 
2287             cc = cc+1
2288          ENDDO                   
2289          aero_old(1:nbins)%volc(vc) = aero(1:nbins)%volc(vc)
2290       ENDIF
2291
2292!--    Dust (DU)
2293       IF ( idu > 0 )  THEN
2294          vc = 4
2295          str = ( idu-1 ) * nbins + 1 + fn1a
2296          endi = idu * nbins
2297          cc = 1 + fn1a
2298          DO ss = str, endi
2299             aero(cc)%volc(vc) = aerosol_mass(ss)%conc(k,j,i) / arhodu 
2300             cc = cc+1
2301          ENDDO
2302          aero_old(1:nbins)%volc(vc) = aero(1:nbins)%volc(vc)
2303       ENDIF
2304
2305!--    Sea salt (SS)
2306       IF ( iss > 0 )  THEN
2307          vc = 5
2308          str = ( iss-1 ) * nbins + 1 + fn1a
2309          endi = iss * nbins
2310          cc = 1 + fn1a
2311          DO ss = str, endi
2312             aero(cc)%volc(vc) = aerosol_mass(ss)%conc(k,j,i) / arhoss 
2313             cc = cc+1
2314          ENDDO
2315          aero_old(1:nbins)%volc(vc) = aero(1:nbins)%volc(vc)
2316       ENDIF
2317
2318!--    Nitrate (NO(3-)) or nitric acid HNO3
2319       IF ( ino > 0 )  THEN
2320          vc = 6
2321          str = ( ino-1 ) * nbins + 1 
2322          endi = ino * nbins
2323          cc = 1
2324          DO ss = str, endi
2325             aero(cc)%volc(vc) = aerosol_mass(ss)%conc(k,j,i) / arhohno3 
2326             cc = cc+1
2327          ENDDO
2328          aero_old(1:nbins)%volc(vc) = aero(1:nbins)%volc(vc)
2329       ENDIF
2330
2331!--    Ammonium (NH(4+)) or ammonia NH3
2332       IF ( inh > 0 )  THEN
2333          vc = 7
2334          str = ( inh-1 ) * nbins + 1
2335          endi = inh * nbins
2336          cc = 1
2337          DO ss = str, endi
2338             aero(cc)%volc(vc) = aerosol_mass(ss)%conc(k,j,i) / arhonh3 
2339             cc = cc+1
2340          ENDDO
2341          aero_old(1:nbins)%volc(vc) = aero(1:nbins)%volc(vc)
2342       ENDIF
2343
2344!--    Water (always used)
2345       nc_h2o = get_index( prtcl,'H2O' )
2346       vc = 8
2347       str = ( nc_h2o-1 ) * nbins + 1
2348       endi = nc_h2o * nbins
2349       cc = 1
2350       IF ( advect_particle_water )  THEN
2351          DO ss = str, endi
2352             aero(cc)%volc(vc) = aerosol_mass(ss)%conc(k,j,i) / arhoh2o 
2353             cc = cc+1
2354          ENDDO
2355       ELSE
2356         aero(1:nbins)%volc(vc) = mclim 
2357       ENDIF
2358       aero_old(1:nbins)%volc(vc) = aero(1:nbins)%volc(vc)
2359!
2360!--    Number concentrations (numc) and particle sizes
2361!--    (dwet = wet diameter, core = dry volume)
2362       DO  bb = 1, nbins
2363          aero(bb)%numc = aerosol_number(bb)%conc(k,j,i) 
2364          aero_old(bb)%numc = aero(bb)%numc
2365          IF ( aero(bb)%numc > nclim )  THEN
2366             aero(bb)%dwet = ( SUM( aero(bb)%volc(:) ) / aero(bb)%numc / api6 )&
2367                                **( 1.0_wp / 3.0_wp )
2368             aero(bb)%core = SUM( aero(bb)%volc(1:7) ) / aero(bb)%numc 
2369          ELSE
2370             aero(bb)%dwet = aero(bb)%dmid
2371             aero(bb)%core = api6 * ( aero(bb)%dwet ) ** 3.0_wp
2372          ENDIF
2373       ENDDO
2374!       
2375!--    On EACH call of salsa_driver, calculate the ambient sizes of
2376!--    particles by equilibrating soluble fraction of particles with water
2377!--    using the ZSR method.
2378       in_rh = in_cw(k) / in_cs(k)
2379       IF ( prunmode==1  .OR.  .NOT. advect_particle_water )  THEN
2380          CALL equilibration( in_rh, in_t(k), aero, .TRUE. )
2381       ENDIF
2382!
2383!--    Gaseous tracer concentrations in #/m3
2384       IF ( salsa_gases_from_chem )  THEN       
2385!       
2386!--       Convert concentrations in ppm to #/m3
2387          zgso4  = chem_species(gas_index_chem(1))%conc(k,j,i) * ppm_to_nconc(k)
2388          zghno3 = chem_species(gas_index_chem(2))%conc(k,j,i) * ppm_to_nconc(k)
2389          zgnh3  = chem_species(gas_index_chem(3))%conc(k,j,i) * ppm_to_nconc(k)
2390          zgocnv = chem_species(gas_index_chem(4))%conc(k,j,i) * ppm_to_nconc(k)     
2391          zgocsv = chem_species(gas_index_chem(5))%conc(k,j,i) * ppm_to_nconc(k)                 
2392       ELSE
2393          zgso4  = salsa_gas(1)%conc(k,j,i) 
2394          zghno3 = salsa_gas(2)%conc(k,j,i) 
2395          zgnh3  = salsa_gas(3)%conc(k,j,i) 
2396          zgocnv = salsa_gas(4)%conc(k,j,i) 
2397          zgocsv = salsa_gas(5)%conc(k,j,i)
2398       ENDIF   
2399!
2400!--    ***************************************!
2401!--                   Run SALSA               !
2402!--    ***************************************!
2403       CALL run_salsa( in_p(k), in_cw(k), in_cs(k), in_t(k), in_u(k),          &
2404                       in_adn(k), in_lad, zgso4, zgocnv, zgocsv, zghno3, zgnh3,&
2405                       aero, prtcl, kvis(k), Sc(k,:), vd(k,:), dt_salsa )
2406!--    ***************************************!
2407       IF ( lsdepo ) sedim_vd(k,j,i,:) = vd(k,:)
2408!                           
2409!--    Calculate changes in concentrations
2410       DO bb = 1, nbins
2411          aerosol_number(bb)%conc(k,j,i) = aerosol_number(bb)%conc(k,j,i)      &
2412                                 +  ( aero(bb)%numc - aero_old(bb)%numc ) * flag
2413       ENDDO
2414       
2415       IF ( iso4 > 0 )  THEN
2416          vc = 1
2417          str = ( iso4-1 ) * nbins + 1
2418          endi = iso4 * nbins
2419          cc = 1
2420          DO ss = str, endi
2421             aerosol_mass(ss)%conc(k,j,i) = aerosol_mass(ss)%conc(k,j,i)       &
2422                               + ( aero(cc)%volc(vc) - aero_old(cc)%volc(vc) ) &
2423                               * arhoh2so4 * flag
2424             cc = cc+1
2425          ENDDO
2426       ENDIF
2427       
2428       IF ( ioc > 0 )  THEN
2429          vc = 2
2430          str = ( ioc-1 ) * nbins + 1
2431          endi = ioc * nbins
2432          cc = 1
2433          DO ss = str, endi
2434             aerosol_mass(ss)%conc(k,j,i) = aerosol_mass(ss)%conc(k,j,i)       &
2435                               + ( aero(cc)%volc(vc) - aero_old(cc)%volc(vc) ) &
2436                               * arhooc * flag
2437             cc = cc+1
2438          ENDDO
2439       ENDIF
2440       
2441       IF ( ibc > 0 )  THEN
2442          vc = 3
2443          str = ( ibc-1 ) * nbins + 1 + fn1a
2444          endi = ibc * nbins
2445          cc = 1 + fn1a
2446          DO ss = str, endi
2447             aerosol_mass(ss)%conc(k,j,i) = aerosol_mass(ss)%conc(k,j,i)       &
2448                               + ( aero(cc)%volc(vc) - aero_old(cc)%volc(vc) ) &
2449                               * arhobc * flag 
2450             cc = cc+1
2451          ENDDO
2452       ENDIF
2453       
2454       IF ( idu > 0 )  THEN
2455          vc = 4
2456          str = ( idu-1 ) * nbins + 1 + fn1a
2457          endi = idu * nbins
2458          cc = 1 + fn1a
2459          DO ss = str, endi
2460             aerosol_mass(ss)%conc(k,j,i) = aerosol_mass(ss)%conc(k,j,i)       &
2461                               + ( aero(cc)%volc(vc) - aero_old(cc)%volc(vc) ) &
2462                               * arhodu * flag
2463             cc = cc+1
2464          ENDDO
2465       ENDIF
2466       
2467       IF ( iss > 0 )  THEN
2468          vc = 5
2469          str = ( iss-1 ) * nbins + 1 + fn1a
2470          endi = iss * nbins
2471          cc = 1 + fn1a
2472          DO ss = str, endi
2473             aerosol_mass(ss)%conc(k,j,i) = aerosol_mass(ss)%conc(k,j,i)       &
2474                               + ( aero(cc)%volc(vc) - aero_old(cc)%volc(vc) ) &
2475                               * arhoss * flag
2476             cc = cc+1
2477          ENDDO
2478       ENDIF
2479       
2480       IF ( ino > 0 )  THEN
2481          vc = 6
2482          str = ( ino-1 ) * nbins + 1
2483          endi = ino * nbins
2484          cc = 1
2485          DO ss = str, endi
2486             aerosol_mass(ss)%conc(k,j,i) = aerosol_mass(ss)%conc(k,j,i)       &
2487                               + ( aero(cc)%volc(vc) - aero_old(cc)%volc(vc) ) &
2488                               * arhohno3 * flag
2489             cc = cc+1
2490          ENDDO
2491       ENDIF
2492       
2493       IF ( inh > 0 )  THEN
2494          vc = 7
2495          str = ( ino-1 ) * nbins + 1
2496          endi = ino * nbins
2497          cc = 1
2498          DO ss = str, endi
2499             aerosol_mass(ss)%conc(k,j,i) = aerosol_mass(ss)%conc(k,j,i)       &
2500                               + ( aero(cc)%volc(vc) - aero_old(cc)%volc(vc) ) &
2501                               * arhonh3 * flag
2502             cc = cc+1
2503          ENDDO
2504       ENDIF
2505       
2506       IF ( advect_particle_water )  THEN
2507          nc_h2o = get_index( prtcl,'H2O' )
2508          vc = 8
2509          str = ( nc_h2o-1 ) * nbins + 1
2510          endi = nc_h2o * nbins
2511          cc = 1
2512          DO ss = str, endi
2513             aerosol_mass(ss)%conc(k,j,i) = aerosol_mass(ss)%conc(k,j,i)       &
2514                               + ( aero(cc)%volc(vc) - aero_old(cc)%volc(vc) ) &
2515                               * arhoh2o * flag
2516             IF ( prunmode == 1 )  THEN
2517                aerosol_mass(ss)%init(k) = MAX( aerosol_mass(ss)%init(k),      &
2518                                               aerosol_mass(ss)%conc(k,j,i) )
2519             ENDIF
2520             cc = cc+1                             
2521          ENDDO
2522       ENDIF
2523
2524!--    Condensation of precursor gases
2525       IF ( lscndgas )  THEN
2526          IF ( salsa_gases_from_chem )  THEN         
2527!         
2528!--          SO4 (or H2SO4)
2529             chem_species( gas_index_chem(1) )%conc(k,j,i) =                &
2530                            chem_species( gas_index_chem(1) )%conc(k,j,i) + &
2531                                                  ( zgso4 / ppm_to_nconc(k) - &
2532                       chem_species( gas_index_chem(1) )%conc(k,j,i) ) * flag
2533!                           
2534!--          HNO3
2535             chem_species( gas_index_chem(2) )%conc(k,j,i) =                &
2536                            chem_species( gas_index_chem(2) )%conc(k,j,i) + &
2537                                                 ( zghno3 / ppm_to_nconc(k) - &
2538                       chem_species( gas_index_chem(2) )%conc(k,j,i) ) * flag
2539!                           
2540!--          NH3
2541             chem_species( gas_index_chem(3) )%conc(k,j,i) =                &
2542                            chem_species( gas_index_chem(3) )%conc(k,j,i) + &
2543                                                  ( zgnh3 / ppm_to_nconc(k) - &
2544                       chem_species( gas_index_chem(3) )%conc(k,j,i) ) * flag
2545!                           
2546!--          non-volatile OC
2547             chem_species( gas_index_chem(4) )%conc(k,j,i) =                &
2548                            chem_species( gas_index_chem(4) )%conc(k,j,i) + &
2549                                                 ( zgocnv / ppm_to_nconc(k) - &
2550                       chem_species( gas_index_chem(4) )%conc(k,j,i) ) * flag
2551!                           
2552!--          semi-volatile OC
2553             chem_species( gas_index_chem(5) )%conc(k,j,i) =                &
2554                            chem_species( gas_index_chem(5) )%conc(k,j,i) + &
2555                                                 ( zgocsv / ppm_to_nconc(k) - &
2556                       chem_species( gas_index_chem(5) )%conc(k,j,i) ) * flag                 
2557         
2558          ELSE
2559!         
2560!--          SO4 (or H2SO4)
2561             salsa_gas(1)%conc(k,j,i) = salsa_gas(1)%conc(k,j,i) + ( zgso4 -   &
2562                                          salsa_gas(1)%conc(k,j,i) ) * flag
2563!                           
2564!--          HNO3
2565             salsa_gas(2)%conc(k,j,i) = salsa_gas(2)%conc(k,j,i) + ( zghno3 -  &
2566                                          salsa_gas(2)%conc(k,j,i) ) * flag
2567!                           
2568!--          NH3
2569             salsa_gas(3)%conc(k,j,i) = salsa_gas(3)%conc(k,j,i) + ( zgnh3 -   &
2570                                          salsa_gas(3)%conc(k,j,i) ) * flag
2571!                           
2572!--          non-volatile OC
2573             salsa_gas(4)%conc(k,j,i) = salsa_gas(4)%conc(k,j,i) + ( zgocnv -  &
2574                                          salsa_gas(4)%conc(k,j,i) ) * flag
2575!                           
2576!--          semi-volatile OC
2577             salsa_gas(5)%conc(k,j,i) = salsa_gas(5)%conc(k,j,i) + ( zgocsv -  &
2578                                          salsa_gas(5)%conc(k,j,i) ) * flag
2579          ENDIF
2580       ENDIF
2581!               
2582!--    Tendency of water vapour mixing ratio is obtained from the
2583!--    change in RH during SALSA run. This releases heat and changes pt.
2584!--    Assumes no temperature change during SALSA run.
2585!--    q = r / (1+r), Euler method for integration
2586!
2587       IF ( feedback_to_palm )  THEN
2588          q_p(k,j,i) = q_p(k,j,i) + 1.0_wp / ( in_cw(k) * in_adn(k) + 1.0_wp ) &
2589                       ** 2.0_wp * ( in_cw(k) - cw_old ) * in_adn(k) 
2590          pt_p(k,j,i) = pt_p(k,j,i) + alv / c_p * ( in_cw(k) - cw_old ) *      &
2591                        in_adn(k) / ( in_cw(k) / in_adn(k) + 1.0_wp ) ** 2.0_wp&
2592                        * pt_p(k,j,i) / in_t(k)
2593       ENDIF
2594                         
2595    ENDDO   ! k
2596!   
2597!-- Set surfaces and wall fluxes due to deposition 
2598    IF ( lsdepo_topo  .AND.  prunmode == 3 )  THEN
2599       IF ( .NOT. land_surface  .AND.  .NOT. urban_surface )  THEN
2600          CALL depo_topo( i, j, surf_def_h(0), vd, Sc, kvis, in_u, rho_air_zw )
2601          DO  l = 0, 3
2602             CALL depo_topo( i, j, surf_def_v(l), vd, Sc, kvis, in_u,          &
2603                             rho_air_zw**0.0_wp )
2604          ENDDO
2605       ELSE
2606          CALL depo_topo( i, j, surf_usm_h, vd, Sc, kvis, in_u, rho_air_zw )
2607          DO  l = 0, 3
2608             CALL depo_topo( i, j, surf_usm_v(l), vd, Sc, kvis, in_u,          &
2609                             rho_air_zw**0.0_wp )
2610          ENDDO
2611          CALL depo_topo( i, j, surf_lsm_h, vd, Sc, kvis, in_u, rho_air_zw )
2612          DO  l = 0, 3
2613             CALL depo_topo( i, j, surf_lsm_v(l), vd, Sc, kvis, in_u,          &
2614                             rho_air_zw**0.0_wp )
2615          ENDDO
2616       ENDIF
2617    ENDIF
2618   
2619 END SUBROUTINE salsa_driver
2620
2621!------------------------------------------------------------------------------!
2622! Description:
2623! ------------
2624!> The SALSA subroutine
2625!> Modified for the new aerosol datatype,
2626!> Juha Tonttila, FMI, 2014.
2627!> Only aerosol processes included, Mona Kurppa, UHel, 2017
2628!------------------------------------------------------------------------------!   
2629 SUBROUTINE run_salsa( ppres, pcw, pcs, ptemp, mag_u, adn, lad, pc_h2so4,      &
2630                       pc_ocnv, pc_ocsv, pc_hno3, pc_nh3, paero, prtcl, kvis,  &
2631                       Sc, vc, ptstep )
2632
2633    IMPLICIT NONE
2634!
2635!-- Input parameters and variables
2636    REAL(wp), INTENT(in) ::  adn    !< air density (kg/m3)
2637    REAL(wp), INTENT(in) ::  lad    !< leaf area density (m2/m3)
2638    REAL(wp), INTENT(in) ::  mag_u  !< magnitude of wind (m/s)
2639    REAL(wp), INTENT(in) ::  ppres  !< atmospheric pressure at each grid
2640                                    !< point (Pa)
2641    REAL(wp), INTENT(in) ::  ptemp  !< temperature at each grid point (K)
2642    REAL(wp), INTENT(in) ::  ptstep !< time step of salsa processes (s)
2643    TYPE(component_index), INTENT(in) :: prtcl  !< part. component index table
2644!       
2645!-- Input variables that are changed within:
2646    REAL(wp), INTENT(inout) ::  kvis     !< kinematic viscosity of air (m2/s)
2647    REAL(wp), INTENT(inout) ::  Sc(:)    !< particle Schmidt number
2648    REAL(wp), INTENT(inout) ::  vc(:)    !< particle fall speed (m/s,
2649                                         !< sedimentation velocity)
2650!-- Gas phase concentrations at each grid point (#/m3)
2651    REAL(wp), INTENT(inout) ::  pc_h2so4 !< sulphuric acid
2652    REAL(wp), INTENT(inout) ::  pc_hno3  !< nitric acid
2653    REAL(wp), INTENT(inout) ::  pc_nh3   !< ammonia
2654    REAL(wp), INTENT(inout) ::  pc_ocnv  !< nonvolatile OC
2655    REAL(wp), INTENT(inout) ::  pc_ocsv  !< semivolatile OC
2656    REAL(wp), INTENT(inout) ::  pcs      !< Saturation concentration of water
2657                                         !< vapour (kg/m3)
2658    REAL(wp), INTENT(inout) ::  pcw      !< Water vapour concentration (kg/m3)                                                   
2659    TYPE(t_section), INTENT(inout) ::  paero(fn2b) 
2660!
2661!-- Coagulation
2662    IF ( lscoag )   THEN
2663       CALL coagulation( paero, ptstep, ptemp, ppres )
2664    ENDIF
2665!
2666!-- Condensation
2667    IF ( lscnd )   THEN
2668       CALL condensation( paero, pc_h2so4, pc_ocnv, pc_ocsv,  pc_hno3, pc_nh3, &
2669                          pcw, pcs, ptemp, ppres, ptstep, prtcl )
2670    ENDIF   
2671!   
2672!-- Deposition
2673    IF ( lsdepo )  THEN
2674       CALL deposition( paero, ptemp, adn, mag_u, lad, kvis, Sc, vc ) 
2675    ENDIF       
2676!
2677!-- Size distribution bin update
2678!-- Mona: why done 3 times in SALSA-standalone?
2679    IF ( lsdistupdate )   THEN
2680       CALL distr_update( paero )
2681    ENDIF
2682   
2683  END SUBROUTINE run_salsa 
2684 
2685!------------------------------------------------------------------------------!
2686! Description:
2687! ------------
2688!> Set logical switches according to the host model state and user-specified
2689!> NAMELIST options.
2690!> Juha Tonttila, FMI, 2014
2691!> Only aerosol processes included, Mona Kurppa, UHel, 2017
2692!------------------------------------------------------------------------------!
2693 SUBROUTINE set_salsa_runtime( prunmode )
2694 
2695    IMPLICIT NONE
2696   
2697    INTEGER(iwp), INTENT(in) ::  prunmode
2698   
2699    SELECT CASE(prunmode)
2700
2701       CASE(1) !< Initialization
2702          lscoag       = .FALSE.
2703          lscnd        = .FALSE.
2704          lscndgas     = .FALSE.
2705          lscndh2oae   = .FALSE.
2706          lsdepo       = .FALSE.
2707          lsdepo_vege  = .FALSE.
2708          lsdepo_topo  = .FALSE.
2709          lsdistupdate = .TRUE.
2710
2711       CASE(2)  !< Spinup period
2712          lscoag      = ( .FALSE. .AND. nlcoag   )
2713          lscnd       = ( .TRUE.  .AND. nlcnd    )
2714          lscndgas    = ( .TRUE.  .AND. nlcndgas )
2715          lscndh2oae  = ( .TRUE.  .AND. nlcndh2oae )
2716
2717       CASE(3)  !< Run
2718          lscoag       = nlcoag
2719          lscnd        = nlcnd
2720          lscndgas     = nlcndgas
2721          lscndh2oae   = nlcndh2oae
2722          lsdepo       = nldepo
2723          lsdepo_vege  = nldepo_vege
2724          lsdepo_topo  = nldepo_topo
2725          lsdistupdate = nldistupdate
2726
2727    END SELECT
2728
2729
2730 END SUBROUTINE set_salsa_runtime 
2731 
2732!------------------------------------------------------------------------------!
2733! Description:
2734! ------------
2735!> Calculates the absolute temperature (using hydrostatic pressure), saturation
2736!> vapour pressure and mixing ratio over water, relative humidity and air
2737!> density needed in the SALSA model.
2738!> NOTE, no saturation adjustment takes place -> the resulting water vapour
2739!> mixing ratio can be supersaturated, allowing the microphysical calculations
2740!> in SALSA.
2741!
2742!> Juha Tonttila, FMI, 2014 (original SALSAthrm)
2743!> Mona Kurppa, UHel, 2017 (adjustment for PALM and only aerosol processes)
2744!------------------------------------------------------------------------------!
2745 SUBROUTINE salsa_thrm_ij( i, j, p_ij, temp_ij, cw_ij, cs_ij, adn_ij )
2746 
2747    USE arrays_3d,                                                             &
2748        ONLY: p, pt, q, zu
2749       
2750    USE basic_constants_and_equations_mod,                                     &
2751        ONLY:  barometric_formula, exner_function, ideal_gas_law_rho, magnus 
2752       
2753    USE control_parameters,                                                    &
2754        ONLY: pt_surface, surface_pressure
2755       
2756    IMPLICIT NONE
2757   
2758    INTEGER(iwp), INTENT(in) ::  i
2759    INTEGER(iwp), INTENT(in) ::  j 
2760    REAL(wp), DIMENSION(:), INTENT(inout) ::  adn_ij
2761    REAL(wp), DIMENSION(:), INTENT(inout) ::  p_ij       
2762    REAL(wp), DIMENSION(:), INTENT(inout) ::  temp_ij
2763    REAL(wp), DIMENSION(:), INTENT(inout), OPTIONAL ::  cw_ij
2764    REAL(wp), DIMENSION(:), INTENT(inout), OPTIONAL ::  cs_ij 
2765    REAL(wp), DIMENSION(nzb:nzt+1) ::  e_s !< saturation vapour pressure
2766                                           !< over water (Pa)
2767    REAL(wp) ::  t_surface !< absolute surface temperature (K)
2768!
2769!-- Pressure p_ijk (Pa) = hydrostatic pressure + perturbation pressure (p)
2770    t_surface = pt_surface * exner_function( surface_pressure )
2771    p_ij(:) = 100.0_wp * barometric_formula( zu, t_surface, surface_pressure ) &
2772              + p(:,j,i)
2773!             
2774!-- Absolute ambient temperature (K)
2775    temp_ij(:) = pt(:,j,i) * exner_function( p_ij(:) )       
2776!
2777!-- Air density
2778    adn_ij(:) = ideal_gas_law_rho( p_ij(:), temp_ij(:) )
2779!
2780!-- Water vapour concentration r_v (kg/m3)
2781    IF ( PRESENT( cw_ij ) )  THEN
2782       cw_ij(:) = ( q(:,j,i) / ( 1.0_wp - q(:,j,i) ) ) * adn_ij(:) 
2783    ENDIF
2784!
2785!-- Saturation mixing ratio r_s (kg/kg) from vapour pressure at temp (Pa)
2786    IF ( PRESENT( cs_ij ) )  THEN
2787       e_s(:) = magnus( temp_ij(:) ) 
2788       cs_ij(:) = ( 0.622_wp * e_s / ( p_ij(:) - e_s(:) ) ) * adn_ij(:) 
2789    ENDIF
2790   
2791 END SUBROUTINE salsa_thrm_ij 
2792
2793!------------------------------------------------------------------------------!
2794! Description:
2795! ------------
2796!> Calculates ambient sizes of particles by equilibrating soluble fraction of
2797!> particles with water using the ZSR method (Stokes and Robinson, 1966).
2798!> Method:
2799!> Following chemical components are assumed water-soluble
2800!> - (ammonium) sulphate (100%)
2801!> - sea salt (100 %)
2802!> - organic carbon (epsoc * 100%)
2803!> Exact thermodynamic considerations neglected.
2804!> - If particles contain no sea salt, calculation according to sulphate
2805!>   properties
2806!> - If contain sea salt but no sulphate, calculation according to sea salt
2807!>   properties
2808!> - If contain both sulphate and sea salt -> the molar fraction of these
2809!>   compounds determines which one of them is used as the basis of calculation.
2810!> If sulphate and sea salt coexist in a particle, it is assumed that the Cl is
2811!> replaced by sulphate; thus only either sulphate + organics or sea salt +
2812!> organics is included in the calculation of soluble fraction.
2813!> Molality parameterizations taken from Table 1 of Tang: Thermodynamic and
2814!> optical properties of mixed-salt aerosols of atmospheric importance,
2815!> J. Geophys. Res., 102 (D2), 1883-1893 (1997)
2816!
2817!> Coded by:
2818!> Hannele Korhonen (FMI) 2005
2819!> Harri Kokkola (FMI) 2006
2820!> Matti Niskanen(FMI) 2012
2821!> Anton Laakso  (FMI) 2013
2822!> Modified for the new aerosol datatype, Juha Tonttila (FMI) 2014
2823!
2824!> fxm: should sea salt form a solid particle when prh is very low (even though
2825!> it could be mixed with e.g. sulphate)?
2826!> fxm: crashes if no sulphate or sea salt
2827!> fxm: do we really need to consider Kelvin effect for subrange 2
2828!------------------------------------------------------------------------------!     
2829 SUBROUTINE equilibration( prh, ptemp, paero, init )
2830     
2831    IMPLICIT NONE
2832!
2833!-- Input variables
2834    LOGICAL, INTENT(in) ::  init   !< TRUE: Initialization call
2835                                   !< FALSE: Normal runtime: update water
2836                                   !<        content only for 1a
2837    REAL(wp), INTENT(in) ::  prh   !< relative humidity [0-1]
2838    REAL(wp), INTENT(in) ::  ptemp !< temperature (K)
2839!
2840!-- Output variables
2841    TYPE(t_section), INTENT(inout) ::  paero(fn2b)     
2842!
2843!-- Local
2844    INTEGER(iwp) :: b      !< loop index
2845    INTEGER(iwp) :: counti  !< loop index
2846    REAL(wp) ::  zaw        !< water activity [0-1]       
2847    REAL(wp) ::  zbinmol(7) !< binary molality of each components (mol/kg)
2848    REAL(wp) ::  zcore      !< Volume of dry particle   
2849    REAL(wp) ::  zdold      !< Old diameter
2850    REAL(wp) ::  zdwet      !< Wet diameter or mean droplet diameter
2851    REAL(wp) ::  zke        !< Kelvin term in the Köhler equation
2852    REAL(wp) ::  zlwc       !< liquid water content [kg/m3-air]
2853    REAL(wp) ::  zrh        !< Relative humidity
2854    REAL(wp) ::  zvpart(7)  !< volume of chem. compounds in one particle
2855   
2856    zaw       = 0.0_wp
2857    zbinmol   = 0.0_wp
2858    zcore     = 0.0_wp
2859    zdold     = 0.0_wp
2860    zdwet     = 0.0_wp
2861    zlwc      = 0.0_wp
2862    zrh       = 0.0_wp
2863   
2864!               
2865!-- Relative humidity:
2866    zrh = prh
2867    zrh = MAX( zrh, 0.05_wp )
2868    zrh = MIN( zrh, 0.98_wp)   
2869!
2870!-- 1) Regime 1: sulphate and partly water-soluble OC. Done for every CALL
2871    DO  b = in1a, fn1a   ! size bin
2872         
2873       zbinmol = 0.0_wp
2874       zdold   = 1.0_wp 
2875       zke     = 1.02_wp
2876       
2877       IF ( paero(b)%numc > nclim )  THEN
2878!
2879!--       Volume in one particle
2880          zvpart = 0.0_wp
2881          zvpart(1:2) = paero(b)%volc(1:2) / paero(b)%numc
2882          zvpart(6:7) = paero(b)%volc(6:7) / paero(b)%numc
2883!               
2884!--       Total volume and wet diameter of one dry particle
2885          zcore = SUM( zvpart(1:2) )
2886          zdwet = paero(b)%dwet
2887         
2888          counti = 0
2889          DO  WHILE ( ABS( zdwet / zdold - 1.0_wp ) > 1.0E-2_wp ) 
2890         
2891             zdold = MAX( zdwet, 1.0E-20_wp )
2892             zaw = MAX( 1.0E-3_wp, zrh / zke ) ! To avoid underflow
2893!                   
2894!--          Binary molalities (mol/kg):
2895!--          Sulphate
2896             zbinmol(1) = 1.1065495E+2_wp - 3.6759197E+2_wp * zaw              &
2897                                          + 5.0462934E+2_wp * zaw**2.0_wp      &
2898                                          - 3.1543839E+2_wp * zaw**3.0_wp      &
2899                                          + 6.770824E+1_wp  * zaw**4.0_wp 
2900!--          Organic carbon                     
2901             zbinmol(2) = 1.0_wp / ( zaw * amh2o ) - 1.0_wp / amh2o 
2902!--          Nitric acid                             
2903             zbinmol(6) = 2.306844303E+1_wp - 3.563608869E+1_wp * zaw          &
2904                                            - 6.210577919E+1_wp * zaw**2.0_wp  &
2905                                            + 5.510176187E+2_wp * zaw**3.0_wp  &
2906                                            - 1.460055286E+3_wp * zaw**4.0_wp  &
2907                                            + 1.894467542E+3_wp * zaw**5.0_wp  &
2908                                            - 1.220611402E+3_wp * zaw**6.0_wp  &
2909                                            + 3.098597737E+2_wp * zaw**7.0_wp 
2910!
2911!--          Calculate the liquid water content (kg/m3-air) using ZSR (see e.g.
2912!--          Eq. 10.98 in Seinfeld and Pandis (2006))
2913             zlwc = ( paero(b)%volc(1) * ( arhoh2so4 / amh2so4 ) ) /           &
2914                    zbinmol(1) + epsoc * paero(b)%volc(2) * ( arhooc / amoc )  &
2915                    / zbinmol(2) + ( paero(b)%volc(6) * ( arhohno3/amhno3 ) )  &
2916                    / zbinmol(6)
2917!                           
2918!--          Particle wet diameter (m)
2919             zdwet = ( zlwc / paero(b)%numc / arhoh2o / api6 +                 &
2920                     ( SUM( zvpart(6:7) ) / api6 ) +      &
2921                       zcore / api6 )**( 1.0_wp / 3.0_wp )
2922!                             
2923!--          Kelvin effect (Eq. 10.85 in in Seinfeld and Pandis (2006)). Avoid
2924!--          overflow.
2925             zke = EXP( MIN( 50.0_wp,                                          &
2926                       4.0_wp * surfw0 * amvh2so4 / ( abo * ptemp *  zdwet ) ) )
2927             
2928             counti = counti + 1
2929             IF ( counti > 1000 )  THEN
2930                message_string = 'Subrange 1: no convergence!'
2931                CALL message( 'salsa_mod: equilibration', 'SA0042',            &
2932                              1, 2, 0, 6, 0 )
2933             ENDIF
2934          ENDDO
2935!               
2936!--       Instead of lwc, use the volume concentration of water from now on
2937!--       (easy to convert...)
2938          paero(b)%volc(8) = zlwc / arhoh2o
2939!               
2940!--       If this is initialization, update the core and wet diameter
2941          IF ( init )  THEN
2942             paero(b)%dwet = zdwet
2943             paero(b)%core = zcore
2944          ENDIF
2945         
2946       ELSE
2947!--       If initialization
2948!--       1.2) empty bins given bin average values 
2949          IF ( init )  THEN
2950             paero(b)%dwet = paero(b)%dmid
2951             paero(b)%core = api6 * paero(b)%dmid ** 3.0_wp
2952          ENDIF
2953         
2954       ENDIF
2955             
2956    ENDDO !< b
2957!
2958!-- 2) Regime 2a: sulphate, OC, BC and sea salt
2959!--    This is done only for initialization call, otherwise the water contents
2960!--    are computed via condensation
2961    IF ( init )  THEN
2962       DO  b = in2a, fn2b 
2963             
2964!--       Initialize
2965          zke     = 1.02_wp
2966          zbinmol = 0.0_wp
2967          zdold   = 1.0_wp
2968!               
2969!--       1) Particle properties calculated for non-empty bins
2970          IF ( paero(b)%numc > nclim )  THEN
2971!               
2972!--          Volume in one particle [fxm]
2973             zvpart = 0.0_wp
2974             zvpart(1:7) = paero(b)%volc(1:7) / paero(b)%numc
2975!
2976!--          Total volume and wet diameter of one dry particle [fxm]
2977             zcore = SUM( zvpart(1:5) )
2978             zdwet = paero(b)%dwet
2979
2980             counti = 0
2981             DO  WHILE ( ABS( zdwet / zdold - 1.0_wp ) > 1.0E-12_wp )
2982             
2983                zdold = MAX( zdwet, 1.0E-20_wp )
2984                zaw = zrh / zke
2985!                     
2986!--             Binary molalities (mol/kg):
2987!--             Sulphate
2988                zbinmol(1) = 1.1065495E+2_wp - 3.6759197E+2_wp * zaw           & 
2989                        + 5.0462934E+2_wp * zaw**2 - 3.1543839E+2_wp * zaw**3  &
2990                        + 6.770824E+1_wp  * zaw**4 
2991!--             Organic carbon                       
2992                zbinmol(2) = 1.0_wp / ( zaw * amh2o ) - 1.0_wp / amh2o 
2993!--             Nitric acid
2994                zbinmol(6) = 2.306844303E+1_wp - 3.563608869E+1_wp * zaw       &
2995                     - 6.210577919E+1_wp * zaw**2 + 5.510176187E+2_wp * zaw**3 &
2996                     - 1.460055286E+3_wp * zaw**4 + 1.894467542E+3_wp * zaw**5 &
2997                     - 1.220611402E+3_wp * zaw**6 + 3.098597737E+2_wp * zaw**7 
2998!--             Sea salt (natrium chloride)                                 
2999                zbinmol(5) = 5.875248E+1_wp - 1.8781997E+2_wp * zaw            &
3000                         + 2.7211377E+2_wp * zaw**2 - 1.8458287E+2_wp * zaw**3 &
3001                         + 4.153689E+1_wp  * zaw**4 
3002!                                 
3003!--             Calculate the liquid water content (kg/m3-air)
3004                zlwc = ( paero(b)%volc(1) * ( arhoh2so4 / amh2so4 ) ) /        &
3005                       zbinmol(1) + epsoc * ( paero(b)%volc(2) * ( arhooc /    &
3006                       amoc ) ) / zbinmol(2) + ( paero(b)%volc(6) * ( arhohno3 &
3007                       / amhno3 ) ) / zbinmol(6) + ( paero(b)%volc(5) *        &
3008                       ( arhoss / amss ) ) / zbinmol(5)
3009                       
3010!--             Particle wet radius (m)
3011                zdwet = ( zlwc / paero(b)%numc / arhoh2o / api6 +              &
3012                          ( SUM( zvpart(6:7) ) / api6 )  + &
3013                           zcore / api6 ) ** ( 1.0_wp / 3.0_wp )
3014!                               
3015!--             Kelvin effect (Eq. 10.85 in Seinfeld and Pandis (2006))
3016                zke = EXP( MIN( 50.0_wp,                                       &
3017                        4.0_wp * surfw0 * amvh2so4 / ( abo * zdwet * ptemp ) ) )
3018                         
3019                counti = counti + 1
3020                IF ( counti > 1000 )  THEN
3021                   message_string = 'Subrange 2: no convergence!'
3022                CALL message( 'salsa_mod: equilibration', 'SA0043',            &
3023                              1, 2, 0, 6, 0 )
3024                ENDIF
3025             ENDDO
3026!                   
3027!--          Liquid water content; instead of LWC use the volume concentration
3028             paero(b)%volc(8) = zlwc / arhoh2o
3029             paero(b)%dwet    = zdwet
3030             paero(b)%core    = zcore
3031             
3032          ELSE
3033!--          2.2) empty bins given bin average values
3034             paero(b)%dwet = paero(b)%dmid
3035             paero(b)%core = api6 * paero(b)%dmid ** 3.0_wp
3036          ENDIF
3037               
3038       ENDDO   ! b
3039    ENDIF
3040
3041 END SUBROUTINE equilibration 
3042 
3043!------------------------------------------------------------------------------!
3044!> Description:
3045!> ------------
3046!> Calculation of the settling velocity vc (m/s) per aerosol size bin and
3047!> deposition on plant canopy (lsdepo_vege).
3048!
3049!> Deposition is based on either the scheme presented in:
3050!> Zhang et al. (2001), Atmos. Environ. 35, 549-560 (includes collection due to
3051!> Brownian diffusion, impaction, interception and sedimentation)
3052!> OR
3053!> Petroff & Zhang (2010), Geosci. Model Dev. 3, 753-769 (includes also
3054!> collection due to turbulent impaction)
3055!
3056!> Equation numbers refer to equation in Jacobson (2005): Fundamentals of
3057!> Atmospheric Modeling, 2nd Edition.
3058!
3059!> Subroutine follows closely sedim_SALSA in UCLALES-SALSA written by Juha
3060!> Tonttila (KIT/FMI) and Zubair Maalick (UEF).
3061!> Rewritten to PALM by Mona Kurppa (UH), 2017.
3062!
3063!> Call for grid point i,j,k
3064!------------------------------------------------------------------------------!
3065
3066 SUBROUTINE deposition( paero, tk, adn, mag_u, lad, kvis, Sc, vc )
3067 
3068    USE plant_canopy_model_mod,                                                &
3069        ONLY: cdc
3070 
3071    IMPLICIT NONE
3072   
3073    REAL(wp), INTENT(in)    ::  adn    !< air density (kg/m3) 
3074    REAL(wp), INTENT(out)   ::  kvis   !< kinematic viscosity of air (m2/s)
3075    REAL(wp), INTENT(in) ::     lad    !< leaf area density (m2/m3)
3076    REAL(wp), INTENT(in)    ::  mag_u  !< wind velocity (m/s)
3077    REAL(wp), INTENT(out)   ::  Sc(:)  !< particle Schmidt number 
3078    REAL(wp), INTENT(in)    ::  tk     !< abs.temperature (K)   
3079    REAL(wp), INTENT(out)   ::  vc(:)  !< critical fall speed i.e. settling
3080                                       !< velocity of an aerosol particle (m/s)
3081    TYPE(t_section), INTENT(inout) ::  paero(fn2b)       
3082   
3083    INTEGER(iwp) ::  b      !< loop index
3084    INTEGER(iwp) ::  c      !< loop index
3085    REAL(wp) ::  avis       !< molecular viscocity of air (kg/(m*s))
3086    REAL(wp), PARAMETER ::  c_A = 1.249_wp !< Constants A, B and C for
3087    REAL(wp), PARAMETER ::  c_B = 0.42_wp  !< calculating  the Cunningham 
3088    REAL(wp), PARAMETER ::  c_C = 0.87_wp  !< slip-flow correction (Cc) 
3089                                           !< according to Jacobson (2005),
3090                                           !< Eq. 15.30
3091    REAL(wp) ::  Cc         !< Cunningham slip-flow correction factor     
3092    REAL(wp) ::  Kn         !< Knudsen number   
3093    REAL(wp) ::  lambda     !< molecular mean free path (m)
3094    REAL(wp) ::  mdiff      !< particle diffusivity coefficient   
3095    REAL(wp) ::  pdn        !< particle density (kg/m3)     
3096    REAL(wp) ::  ustar      !< friction velocity (m/s)   
3097    REAL(wp) ::  va         !< thermal speed of an air molecule (m/s)
3098    REAL(wp) ::  zdwet      !< wet diameter (m)                             
3099!
3100!-- Initialise
3101    Cc            = 0.0_wp
3102    Kn            = 0.0_wp
3103    mdiff         = 0.0_wp
3104    pdn           = 1500.0_wp    ! default value
3105    ustar         = 0.0_wp 
3106!
3107!-- Molecular viscosity of air (Eq. 4.54)
3108    avis = 1.8325E-5_wp * ( 416.16_wp / ( tk + 120.0_wp ) ) * ( tk /           &
3109           296.16_wp )**1.5_wp
3110!             
3111!-- Kinematic viscosity (Eq. 4.55)
3112    kvis =  avis / adn
3113!       
3114!-- Thermal velocity of an air molecule (Eq. 15.32)
3115    va = SQRT( 8.0_wp * abo * tk / ( pi * am_airmol ) ) 
3116!
3117!-- Mean free path (m) (Eq. 15.24)
3118    lambda = 2.0_wp * avis / ( adn * va )
3119   
3120    DO  b = 1, nbins
3121   
3122       IF ( paero(b)%numc < nclim )  CYCLE
3123       zdwet = paero(b)%dwet
3124!
3125!--    Knudsen number (Eq. 15.23)
3126       Kn = MAX( 1.0E-2_wp, lambda / ( zdwet * 0.5_wp ) ) ! To avoid underflow
3127!
3128!--    Cunningham slip-flow correction (Eq. 15.30)
3129       Cc = 1.0_wp + Kn * ( c_A + c_B * EXP( -c_C / Kn ) )
3130
3131!--    Particle diffusivity coefficient (Eq. 15.29)
3132       mdiff = ( abo * tk * Cc ) / ( 3.0_wp * pi * avis * zdwet )
3133!       
3134!--    Particle Schmidt number (Eq. 15.36)
3135       Sc(b) = kvis / mdiff       
3136!       
3137!--    Critical fall speed i.e. settling velocity  (Eq. 20.4)                 
3138       vc(b) = MIN( 1.0_wp, terminal_vel( 0.5_wp * zdwet, pdn, adn, avis, Cc) )
3139       
3140       IF ( lsdepo_vege  .AND.  plant_canopy  .AND.  lad > 0.0_wp )  THEN
3141!       
3142!--       Friction velocity calculated following Prandtl (1925):
3143          ustar = SQRT( cdc ) * mag_u
3144          CALL depo_vege( paero, b, vc(b), mag_u, ustar, kvis, Sc(b), lad )
3145       ENDIF
3146    ENDDO
3147 
3148 END SUBROUTINE deposition 
3149 
3150!------------------------------------------------------------------------------!
3151! Description:
3152! ------------
3153!> Calculate change in number and volume concentrations due to deposition on
3154!> plant canopy.
3155!------------------------------------------------------------------------------!
3156 SUBROUTINE depo_vege( paero, b, vc, mag_u, ustar, kvis_a, Sc, lad )
3157 
3158    IMPLICIT NONE
3159   
3160    INTEGER(iwp), INTENT(in) ::  b  !< loop index
3161    REAL(wp), INTENT(in) ::  kvis_a !< kinematic viscosity of air (m2/s)
3162    REAL(wp), INTENT(in) ::  lad    !< leaf area density (m2/m3)
3163    REAL(wp), INTENT(in) ::  mag_u  !< wind velocity (m/s)   
3164    REAL(wp), INTENT(in) ::  Sc     !< particle Schmidt number
3165    REAL(wp), INTENT(in) ::  ustar  !< friction velocity (m/s)                                   
3166    REAL(wp), INTENT(in) ::  vc     !< terminal velocity (m/s) 
3167    TYPE(t_section), INTENT(inout) ::  paero(fn2b) 
3168   
3169    INTEGER(iwp) ::  c      !< loop index
3170    REAL(wp), PARAMETER ::  c_A = 1.249_wp !< Constants A, B and C for
3171    REAL(wp), PARAMETER ::  c_B = 0.42_wp  !< calculating  the Cunningham 
3172    REAL(wp), PARAMETER ::  c_C = 0.87_wp  !< slip-flow correction (Cc) 
3173                                           !< according to Jacobson (2005),
3174                                           !< Eq. 15.30
3175    REAL(wp) ::  alpha       !< parameter, Table 3 in Zhang et al. (2001) 
3176    REAL(wp) ::  depo        !< deposition efficiency
3177    REAL(wp) ::  C_Br        !< coefficient for Brownian diffusion
3178    REAL(wp) ::  C_IM        !< coefficient for inertial impaction
3179    REAL(wp) ::  C_IN        !< coefficient for interception
3180    REAL(wp) ::  C_IT        !< coefficient for turbulent impaction   
3181    REAL(wp) ::  gamma       !< parameter, Table 3 in Zhang et al. (2001)   
3182    REAL(wp) ::  par_A       !< parameter A for the characteristic radius of
3183                             !< collectors, Table 3 in Zhang et al. (2001)   
3184    REAL(wp) ::  rt          !< the overall quasi-laminar resistance for
3185                             !< particles
3186    REAL(wp) ::  St          !< Stokes number for smooth surfaces or bluff
3187                             !< surface elements                                 
3188    REAL(wp) ::  tau_plus    !< dimensionless particle relaxation time   
3189    REAL(wp) ::  v_bd        !< deposition velocity due to Brownian diffusion
3190    REAL(wp) ::  v_im        !< deposition velocity due to impaction
3191    REAL(wp) ::  v_in        !< deposition velocity due to interception
3192    REAL(wp) ::  v_it        !< deposition velocity due to turbulent impaction                               
3193!
3194!-- Initialise
3195    depo     = 0.0_wp 
3196    rt       = 0.0_wp
3197    St       = 0.0_wp
3198    tau_plus = 0.0_wp
3199    v_bd     = 0.0_wp     
3200    v_im     = 0.0_wp       
3201    v_in     = 0.0_wp       
3202    v_it     = 0.0_wp         
3203       
3204    IF ( depo_vege_type == 'zhang2001' )  THEN
3205!       
3206!--    Parameters for the land use category 'deciduous broadleaf trees'(Table 3)     
3207       par_A = 5.0E-3_wp
3208       alpha = 0.8_wp
3209       gamma = 0.56_wp 
3210!       
3211!--    Stokes number for vegetated surfaces (Seinfeld & Pandis (2006): Eq.19.24) 
3212       St = vc * ustar / ( g * par_A )         
3213!         
3214!--    The overall quasi-laminar resistance for particles (Zhang et al., Eq. 5)       
3215       rt = MAX( EPSILON( 1.0_wp ), ( 3.0_wp * ustar * EXP( -St**0.5_wp ) *    &
3216                         ( Sc**( -gamma ) + ( St / ( alpha + St ) )**2.0_wp +  &
3217                           0.5_wp * ( paero(b)%dwet / par_A )**2.0_wp ) ) )
3218       depo = ( rt + vc ) * lad
3219       paero(b)%numc = paero(b)%numc - depo * paero(b)%numc * dt_salsa
3220       DO  c = 1, maxspec+1
3221          paero(b)%volc(c) = paero(b)%volc(c) - depo * paero(b)%volc(c) *      &
3222                             dt_salsa
3223       ENDDO
3224       
3225    ELSEIF ( depo_vege_type == 'petroff2010' )  THEN
3226!
3227!--    vd = v_BD + v_IN + v_IM + v_IT + vc
3228!--    Deposition efficiencies from Table 1. Constants from Table 2.
3229       C_Br  = 1.262_wp
3230       C_IM  = 0.130_wp
3231       C_IN  = 0.216_wp
3232       C_IT  = 0.056_wp
3233       par_A = 0.03_wp   ! Here: leaf width (m)     
3234!       
3235!--    Stokes number for vegetated surfaces (Seinfeld & Pandis (2006): Eq.19.24) 
3236       St = vc * ustar / ( g * par_A )         
3237!
3238!--    Non-dimensional relexation time of the particle on top of canopy
3239       tau_plus = vc * ustar**2.0_wp / ( kvis_a * g ) 
3240!
3241!--    Brownian diffusion
3242       v_bd = mag_u * C_Br * Sc**( -2.0_wp / 3.0_wp ) *                        &
3243              ( mag_u * par_A / kvis_a )**( -0.5_wp )
3244!
3245!--    Interception
3246       v_in = mag_u * C_IN * paero(b)%dwet / par_A * ( 2.0_wp + LOG( 2.0_wp *  &
3247              par_A / paero(b)%dwet ) )                     
3248!
3249!--    Impaction: Petroff (2009) Eq. 18
3250       v_im = mag_u * C_IM * ( St / ( St + 0.47_wp ) )**2.0_wp
3251       
3252       IF ( tau_plus < 20.0_wp )  THEN
3253          v_it = 2.5E-3_wp * C_IT * tau_plus**2.0_wp
3254       ELSE
3255          v_it = C_IT
3256       ENDIF
3257       depo = ( v_bd + v_in + v_im + v_it + vc ) * lad     
3258       paero(b)%numc = paero(b)%numc - depo * paero(b)%numc * dt_salsa     
3259       DO  c = 1, maxspec+1
3260          paero(b)%volc(c) = paero(b)%volc(c) - depo * paero(b)%volc(c) *      &
3261                             dt_salsa
3262       ENDDO
3263    ENDIF 
3264 
3265 END SUBROUTINE depo_vege
3266 
3267!------------------------------------------------------------------------------!
3268! Description:
3269! ------------ 
3270!> Calculate deposition on horizontal and vertical surfaces. Implement as
3271!> surface flux.
3272!------------------------------------------------------------------------------!
3273
3274 SUBROUTINE depo_topo( i, j, surf, vc, Sc, kvis, mag_u, norm )
3275 
3276    USE surface_mod,                                                           &
3277        ONLY:  surf_type
3278 
3279    IMPLICIT NONE
3280   
3281    INTEGER(iwp), INTENT(in) ::  i     !< loop index
3282    INTEGER(iwp), INTENT(in) ::  j     !< loop index
3283    REAL(wp), INTENT(in) ::  kvis(:)   !< kinematic viscosity of air (m2/s)
3284    REAL(wp), INTENT(in) ::  mag_u(:)  !< wind velocity (m/s)                                                 
3285    REAL(wp), INTENT(in) ::  norm(:)   !< normalisation (usually air density)
3286    REAL(wp), INTENT(in) ::  Sc(:,:)  !< particle Schmidt number
3287    REAL(wp), INTENT(in) ::  vc(:,:)  !< terminal velocity (m/s)   
3288    TYPE(surf_type), INTENT(inout) :: surf  !< respective surface type
3289    INTEGER(iwp) ::  b      !< loop index
3290    INTEGER(iwp) ::  c      !< loop index
3291    INTEGER(iwp) ::  k      !< loop index
3292    INTEGER(iwp) ::  m      !< loop index
3293    INTEGER(iwp) ::  surf_e !< End index of surface elements at (j,i)-gridpoint
3294    INTEGER(iwp) ::  surf_s !< Start index of surface elements at (j,i)-gridpoint
3295    REAL(wp) ::  alpha      !< parameter, Table 3 in Zhang et al. (2001)
3296    REAL(wp) ::  C_Br       !< coefficient for Brownian diffusion
3297    REAL(wp) ::  C_IM       !< coefficient for inertial impaction
3298    REAL(wp) ::  C_IN       !< coefficient for interception
3299    REAL(wp) ::  C_IT       !< coefficient for turbulent impaction
3300    REAL(wp) ::  depo       !< deposition efficiency
3301    REAL(wp) ::  gamma      !< parameter, Table 3 in Zhang et al. (2001)
3302    REAL(wp) ::  par_A      !< parameter A for the characteristic radius of
3303                            !< collectors, Table 3 in Zhang et al. (2001)
3304    REAL(wp) ::  rt         !< the overall quasi-laminar resistance for
3305                            !< particles
3306    REAL(wp) ::  St         !< Stokes number for bluff surface elements 
3307    REAL(wp) ::  tau_plus   !< dimensionless particle relaxation time   
3308    REAL(wp) ::  v_bd       !< deposition velocity due to Brownian diffusion
3309    REAL(wp) ::  v_im       !< deposition velocity due to impaction
3310    REAL(wp) ::  v_in       !< deposition velocity due to interception
3311    REAL(wp) ::  v_it       !< deposition velocity due to turbulent impaction 
3312!
3313!-- Initialise
3314    rt       = 0.0_wp
3315    St       = 0.0_wp
3316    tau_plus = 0.0_wp
3317    v_bd     = 0.0_wp     
3318    v_im     = 0.0_wp       
3319    v_in     = 0.0_wp       
3320    v_it     = 0.0_wp                                 
3321    surf_s   = surf%start_index(j,i)
3322    surf_e   = surf%end_index(j,i) 
3323   
3324    DO  m = surf_s, surf_e 
3325       k = surf%k(m)       
3326       DO  b = 1, nbins
3327          IF ( aerosol_number(b)%conc(k,j,i) <= nclim  .OR.                    &
3328               Sc(k+1,b) < 1.0_wp )  CYCLE   
3329                   
3330          IF ( depo_topo_type == 'zhang2001' )  THEN
3331!       
3332!--          Parameters for the land use category 'urban' in Table 3
3333             alpha = 1.5_wp
3334             gamma = 0.56_wp 
3335             par_A = 10.0E-3_wp
3336!       
3337!--          Stokes number for smooth surfaces or surfaces with bluff roughness
3338!--          elements (Seinfeld and Pandis, 2nd edition (2006): Eq. 19.23)       
3339             St = MAX( 0.01_wp, vc(k+1,b) * surf%us(m) ** 2.0_wp /             &
3340                       ( g * kvis(k+1)  ) ) 
3341!         
3342!--          The overall quasi-laminar resistance for particles (Eq. 5)       
3343             rt = MAX( EPSILON( 1.0_wp ), ( 3.0_wp * surf%us(m) * (            &
3344                       Sc(k+1,b)**( -gamma ) + ( St / ( alpha + St ) )**2.0_wp &
3345                        + 0.5_wp * ( Ra_dry(k,j,i,b) / par_A )**2.0_wp ) *     &
3346                       EXP( -St**0.5_wp ) ) ) 
3347             depo = vc(k+1,b) + rt
3348             
3349          ELSEIF ( depo_topo_type == 'petroff2010' )  THEN 
3350!
3351!--          vd = v_BD + v_IN + v_IM + v_IT + vc
3352!--          Deposition efficiencies from Table 1. Constants from Table 2.
3353             C_Br  = 1.262_wp
3354             C_IM  = 0.130_wp
3355             C_IN  = 0.216_wp
3356             C_IT  = 0.056_wp
3357             par_A = 0.03_wp   ! Here: leaf width (m) 
3358!       
3359!--          Stokes number for smooth surfaces or surfaces with bluff roughness
3360!--          elements (Seinfeld and Pandis, 2nd edition (2006): Eq. 19.23)       
3361             St = MAX( 0.01_wp, vc(k+1,b) * surf%us(m) ** 2.0_wp /             &
3362                       ( g *  kvis(k+1) ) )             
3363!
3364!--          Non-dimensional relexation time of the particle on top of canopy
3365             tau_plus = vc(k+1,b) * surf%us(m)**2.0_wp / ( kvis(k+1) * g ) 
3366!
3367!--          Brownian diffusion
3368             v_bd = mag_u(k+1) * C_Br * Sc(k+1,b)**( -2.0_wp / 3.0_wp ) *      &
3369                    ( mag_u(k+1) * par_A / kvis(k+1) )**( -0.5_wp )
3370!
3371!--          Interception
3372             v_in = mag_u(k+1) * C_IN * Ra_dry(k,j,i,b)/ par_A * ( 2.0_wp +    &
3373                    LOG( 2.0_wp * par_A / Ra_dry(k,j,i,b) ) )                     
3374!
3375!--          Impaction: Petroff (2009) Eq. 18
3376             v_im = mag_u(k+1) * C_IM * ( St / ( St + 0.47_wp ) )**2.0_wp
3377             
3378             IF ( tau_plus < 20.0_wp )  THEN
3379                v_it = 2.5E-3_wp * C_IT * tau_plus**2.0_wp
3380             ELSE
3381                v_it = C_IT
3382             ENDIF
3383             depo =  v_bd + v_in + v_im + v_it + vc(k+1,b)       
3384         
3385          ENDIF
3386          IF ( lod_aero == 3  .OR.  salsa_source_mode ==  'no_source' )  THEN
3387             surf%answs(m,b) = -depo * norm(k) * aerosol_number(b)%conc(k,j,i) 
3388             DO  c = 1, ncc_tot   
3389                surf%amsws(m,(c-1)*nbins+b) = -depo *  norm(k) *               &
3390                                         aerosol_mass((c-1)*nbins+b)%conc(k,j,i)
3391             ENDDO    ! c
3392          ELSE
3393             surf%answs(m,b) = SUM( aerosol_number(b)%source(:,j,i) ) -        &
3394                               MAX( 0.0_wp, depo * norm(k) *                   &
3395                               aerosol_number(b)%conc(k,j,i) )
3396             DO  c = 1, ncc_tot   
3397                surf%amsws(m,(c-1)*nbins+b) = SUM(                             &
3398                               aerosol_mass((c-1)*nbins+b)%source(:,j,i) ) -   &
3399                               MAX(  0.0_wp, depo *  norm(k) *                 &
3400                               aerosol_mass((c-1)*nbins+b)%conc(k,j,i) )
3401             ENDDO 
3402          ENDIF
3403       ENDDO    ! b
3404    ENDDO    ! m     
3405     
3406 END SUBROUTINE depo_topo
3407 
3408!------------------------------------------------------------------------------!
3409! Description:
3410! ------------
3411! Function for calculating terminal velocities for different particles sizes.
3412!------------------------------------------------------------------------------!
3413 REAL(wp) FUNCTION terminal_vel( radius, rhop, rhoa, visc, beta )
3414 
3415    IMPLICIT NONE
3416   
3417    REAL(wp), INTENT(in) ::  beta    !< Cunningham correction factor
3418    REAL(wp), INTENT(in) ::  radius  !< particle radius (m)
3419    REAL(wp), INTENT(in) ::  rhop    !< particle density (kg/m3)
3420    REAL(wp), INTENT(in) ::  rhoa    !< air density (kg/m3)
3421    REAL(wp), INTENT(in) ::  visc    !< molecular viscosity of air (kg/(m*s))
3422   
3423    REAL(wp), PARAMETER ::  rhoa_ref = 1.225_wp ! reference air density (kg/m3)
3424!
3425!-- Stokes law with Cunningham slip correction factor
3426    terminal_vel = ( 4.0_wp * radius**2.0_wp ) * ( rhop - rhoa ) * g * beta /  &
3427                   ( 18.0_wp * visc ) ! (m/s)
3428       
3429 END FUNCTION terminal_vel
3430 
3431!------------------------------------------------------------------------------!
3432! Description:
3433! ------------
3434!> Calculates particle loss and change in size distribution due to (Brownian)
3435!> coagulation. Only for particles with dwet < 30 micrometres.
3436!
3437!> Method:
3438!> Semi-implicit, non-iterative method: (Jacobson, 1994)
3439!> Volume concentrations of the smaller colliding particles added to the bin of
3440!> the larger colliding particles. Start from first bin and use the updated
3441!> number and volume for calculation of following bins. NB! Our bin numbering
3442!> does not follow particle size in subrange 2.
3443!
3444!> Schematic for bin numbers in different subranges:
3445!>             1                            2
3446!>    +-------------------------------------------+
3447!>  a | 1 | 2 | 3 || 4 | 5 | 6 | 7 |  8 |  9 | 10||
3448!>  b |           ||11 |12 |13 |14 | 15 | 16 | 17||
3449!>    +-------------------------------------------+
3450!
3451!> Exact coagulation coefficients for each pressure level are scaled according
3452!> to current particle wet size (linear scaling).
3453!> Bins are organized in terms of the dry size of the condensation nucleus,
3454!> while coagulation kernell is calculated with the actual hydrometeor
3455!> size.
3456!
3457!> Called from salsa_driver
3458!> fxm: Process selection should be made smarter - now just lots of IFs inside
3459!>      loops
3460!
3461!> Coded by:
3462!> Hannele Korhonen (FMI) 2005
3463!> Harri Kokkola (FMI) 2006
3464!> Tommi Bergman (FMI) 2012
3465!> Matti Niskanen(FMI) 2012
3466!> Anton Laakso  (FMI) 2013
3467!> Juha Tonttila (FMI) 2014
3468!------------------------------------------------------------------------------!
3469 SUBROUTINE coagulation( paero, ptstep, ptemp, ppres )
3470               
3471    IMPLICIT NONE
3472   
3473!-- Input and output variables
3474    TYPE(t_section), INTENT(inout) ::  paero(fn2b) !< Aerosol properties
3475    REAL(wp), INTENT(in) ::  ppres  !< ambient pressure (Pa)
3476    REAL(wp), INTENT(in) ::  ptemp  !< ambient temperature (K)
3477    REAL(wp), INTENT(in) ::  ptstep !< time step (s)
3478!-- Local variables
3479    INTEGER(iwp) ::  index_2a !< corresponding bin in subrange 2a
3480    INTEGER(iwp) ::  index_2b !< corresponding bin in subrange 2b
3481    INTEGER(iwp) ::  b !< loop index
3482    INTEGER(iwp) ::  ll !< loop index
3483    INTEGER(iwp) ::  mm !< loop index
3484    INTEGER(iwp) ::  nn !< loop index
3485    REAL(wp) ::  pressi !< pressure
3486    REAL(wp) ::  temppi !< temperature
3487    REAL(wp) ::  zcc(fn2b,fn2b)   !< updated coagulation coefficients (m3/s) 
3488    REAL(wp) ::  zdpart_mm        !< diameter of particle (m)
3489    REAL(wp) ::  zdpart_nn        !< diameter of particle (m)   
3490    REAL(wp) ::  zminusterm       !< coagulation loss in a bin (1/s)
3491    REAL(wp) ::  zplusterm(8)     !< coagulation gain in a bin (fxm/s)
3492                                  !< (for each chemical compound)
3493    REAL(wp) ::  zmpart(fn2b)     !< approximate mass of particles (kg)
3494   
3495    zcc       = 0.0_wp
3496    zmpart    = 0.0_wp
3497    zdpart_mm = 0.0_wp
3498    zdpart_nn = 0.0_wp
3499!
3500!-- 1) Coagulation to coarse mode calculated in a simplified way:
3501!--    CoagSink ~ Dp in continuum subrange, thus we calculate 'effective'
3502!--    number concentration of coarse particles
3503
3504!-- 2) Updating coagulation coefficients
3505!   
3506!-- Aerosol mass (kg). Density of 1500 kg/m3 assumed
3507    zmpart(1:fn2b) = api6 * ( MIN( paero(1:fn2b)%dwet, 30.0E-6_wp )**3.0_wp  ) &
3508                     * 1500.0_wp 
3509    temppi = ptemp
3510    pressi = ppres
3511    zcc    = 0.0_wp
3512!
3513!-- Aero-aero coagulation
3514    DO  mm = 1, fn2b   ! smaller colliding particle
3515       IF ( paero(mm)%numc < nclim )  CYCLE
3516       DO  nn = mm, fn2b   ! larger colliding particle
3517          IF ( paero(nn)%numc < nclim )  CYCLE
3518         
3519          zdpart_mm = MIN( paero(mm)%dwet, 30.0E-6_wp )     ! Limit to 30 um
3520          zdpart_nn = MIN( paero(nn)%dwet, 30.0E-6_wp )     ! Limit to 30 um
3521!             
3522!--       Coagulation coefficient of particles (m3/s)
3523          zcc(mm,nn) = coagc( zdpart_mm, zdpart_nn, zmpart(mm), zmpart(nn),    &
3524                              temppi, pressi )
3525          zcc(nn,mm) = zcc(mm,nn)
3526       ENDDO
3527    ENDDO
3528       
3529!   
3530!-- 3) New particle and volume concentrations after coagulation:
3531!--    Calculated according to Jacobson (2005) eq. 15.9
3532!
3533!-- Aerosols in subrange 1a:
3534    DO  b = in1a, fn1a
3535       IF ( paero(b)%numc < nclim )  CYCLE
3536       zminusterm   = 0.0_wp
3537       zplusterm(:) = 0.0_wp
3538!       
3539!--    Particles lost by coagulation with larger aerosols
3540       DO  ll = b+1, fn2b
3541          zminusterm = zminusterm + zcc(b,ll) * paero(ll)%numc
3542       ENDDO
3543!       
3544!--    Coagulation gain in a bin: change in volume conc. (cm3/cm3):
3545       DO ll = in1a, b-1
3546          zplusterm(1:2) = zplusterm(1:2) + zcc(ll,b) * paero(ll)%volc(1:2)
3547          zplusterm(6:7) = zplusterm(6:7) + zcc(ll,b) * paero(ll)%volc(6:7)
3548          zplusterm(8)   = zplusterm(8)   + zcc(ll,b) * paero(ll)%volc(8)
3549       ENDDO
3550!       
3551!--    Volume and number concentrations after coagulation update [fxm]
3552       paero(b)%volc(1:2) = ( paero(b)%volc(1:2) + ptstep * zplusterm(1:2) * &
3553                             paero(b)%numc ) / ( 1.0_wp + ptstep * zminusterm )
3554       paero(b)%volc(6:7) = ( paero(b)%volc(6:7) + ptstep * zplusterm(6:7) * &
3555                             paero(b)%numc ) / ( 1.0_wp + ptstep * zminusterm )
3556       paero(b)%volc(8)   = ( paero(b)%volc(8)   + ptstep * zplusterm(8) *   &
3557                             paero(b)%numc ) / ( 1.0_wp + ptstep * zminusterm )
3558       paero(b)%numc = paero(b)%numc / ( 1.0_wp + ptstep * zminusterm  +     &
3559                        0.5_wp * ptstep * zcc(b,b) * paero(b)%numc )               
3560    ENDDO
3561!             
3562!-- Aerosols in subrange 2a:
3563    DO  b = in2a, fn2a
3564       IF ( paero(b)%numc < nclim )  CYCLE
3565       zminusterm   = 0.0_wp
3566       zplusterm(:) = 0.0_wp
3567!       
3568!--    Find corresponding size bin in subrange 2b
3569       index_2b = b - in2a + in2b
3570!       
3571!--    Particles lost by larger particles in 2a
3572       DO  ll = b+1, fn2a
3573          zminusterm = zminusterm + zcc(b,ll) * paero(ll)%numc 
3574       ENDDO
3575!       
3576!--    Particles lost by larger particles in 2b
3577       IF ( .NOT. no_insoluble )  THEN
3578          DO  ll = index_2b+1, fn2b
3579             zminusterm = zminusterm + zcc(b,ll) * paero(ll)%numc
3580          ENDDO
3581       ENDIF
3582!       
3583!--    Particle volume gained from smaller particles in subranges 1, 2a and 2b
3584       DO  ll = in1a, b-1
3585          zplusterm(1:2) = zplusterm(1:2) + zcc(ll,b) * paero(ll)%volc(1:2)
3586          zplusterm(6:7) = zplusterm(6:7) + zcc(ll,b) * paero(ll)%volc(6:7)
3587          zplusterm(8)   = zplusterm(8)   + zcc(ll,b) * paero(ll)%volc(8)
3588       ENDDO 
3589!       
3590!--    Particle volume gained from smaller particles in 2a
3591!--    (Note, for components not included in the previous loop!)
3592       DO  ll = in2a, b-1
3593          zplusterm(3:5) = zplusterm(3:5) + zcc(ll,b)*paero(ll)%volc(3:5)             
3594       ENDDO
3595       
3596!       
3597!--    Particle volume gained from smaller (and equal) particles in 2b
3598       IF ( .NOT. no_insoluble )  THEN
3599          DO  ll = in2b, index_2b
3600             zplusterm(1:8) = zplusterm(1:8) + zcc(ll,b) * paero(ll)%volc(1:8)
3601          ENDDO
3602       ENDIF
3603!       
3604!--    Volume and number concentrations after coagulation update [fxm]
3605       paero(b)%volc(1:8) = ( paero(b)%volc(1:8) + ptstep * zplusterm(1:8) * &
3606                             paero(b)%numc ) / ( 1.0_wp + ptstep * zminusterm )
3607       paero(b)%numc = paero(b)%numc / ( 1.0_wp + ptstep * zminusterm +      &
3608                        0.5_wp * ptstep * zcc(b,b) * paero(b)%numc )
3609    ENDDO
3610!             
3611!-- Aerosols in subrange 2b:
3612    IF ( .NOT. no_insoluble )  THEN
3613       DO  b = in2b, fn2b
3614          IF ( paero(b)%numc < nclim )  CYCLE
3615          zminusterm   = 0.0_wp
3616          zplusterm(:) = 0.0_wp
3617!       
3618!--       Find corresponding size bin in subsubrange 2a
3619          index_2a = b - in2b + in2a
3620!       
3621!--       Particles lost to larger particles in subranges 2b
3622          DO  ll = b+1, fn2b
3623             zminusterm = zminusterm + zcc(b,ll) * paero(ll)%numc
3624          ENDDO
3625!       
3626!--       Particles lost to larger and equal particles in 2a
3627          DO  ll = index_2a, fn2a
3628             zminusterm = zminusterm + zcc(b,ll) * paero(ll)%numc
3629          ENDDO
3630!       
3631!--       Particle volume gained from smaller particles in subranges 1 & 2a
3632          DO  ll = in1a, index_2a-1
3633             zplusterm(1:8) = zplusterm(1:8) + zcc(ll,b) * paero(ll)%volc(1:8)
3634          ENDDO
3635!       
3636!--       Particle volume gained from smaller particles in 2b
3637          DO  ll = in2b, b-1
3638             zplusterm(1:8) = zplusterm(1:8) + zcc(ll,b) * paero(ll)%volc(1:8)
3639          ENDDO
3640!       
3641!--       Volume and number concentrations after coagulation update [fxm]
3642          paero(b)%volc(1:8) = ( paero(b)%volc(1:8) + ptstep * zplusterm(1:8)&
3643                           * paero(b)%numc ) / ( 1.0_wp + ptstep * zminusterm )
3644          paero(b)%numc = paero(b)%numc / ( 1.0_wp + ptstep * zminusterm  +  &
3645                           0.5_wp * ptstep * zcc(b,b) * paero(b)%numc )
3646       ENDDO
3647    ENDIF
3648
3649 END SUBROUTINE coagulation
3650
3651!------------------------------------------------------------------------------!
3652! Description:
3653! ------------
3654!> Calculation of coagulation coefficients. Extended version of the function
3655!> originally found in mo_salsa_init.
3656!
3657!> J. Tonttila, FMI, 05/2014
3658!------------------------------------------------------------------------------!
3659 REAL(wp) FUNCTION coagc( diam1, diam2, mass1, mass2, temp, pres )
3660 
3661    IMPLICIT NONE
3662!       
3663!-- Input and output variables
3664    REAL(wp), INTENT(in) ::  diam1 !< diameter of colliding particle 1 (m)
3665    REAL(wp), INTENT(in) ::  diam2 !< diameter of colliding particle 2 (m)
3666    REAL(wp), INTENT(in) ::  mass1 !< mass of colliding particle 1 (kg)
3667    REAL(wp), INTENT(in) ::  mass2 !< mass of colliding particle 2 (kg)
3668    REAL(wp), INTENT(in) ::  pres  !< ambient pressure (Pa?) [fxm]
3669    REAL(wp), INTENT(in) ::  temp  !< ambient temperature (K)       
3670!
3671!-- Local variables
3672    REAL(wp) ::  fmdist !< distance of flux matching (m)   
3673    REAL(wp) ::  knud_p !< particle Knudsen number
3674    REAL(wp) ::  mdiam  !< mean diameter of colliding particles (m) 
3675    REAL(wp) ::  mfp    !< mean free path of air molecules (m)   
3676    REAL(wp) ::  visc   !< viscosity of air (kg/(m s))                   
3677    REAL(wp), DIMENSION (2) ::  beta   !< Cunningham correction factor
3678    REAL(wp), DIMENSION (2) ::  dfpart !< particle diffusion coefficient
3679                                       !< (m2/s)       
3680    REAL(wp), DIMENSION (2) ::  diam   !< diameters of particles (m)
3681    REAL(wp), DIMENSION (2) ::  flux   !< flux in continuum and free molec.
3682                                       !< regime (m/s)       
3683    REAL(wp), DIMENSION (2) ::  knud   !< particle Knudsen number       
3684    REAL(wp), DIMENSION (2) ::  mpart  !< masses of particles (kg)
3685    REAL(wp), DIMENSION (2) ::  mtvel  !< particle mean thermal velocity (m/s)
3686    REAL(wp), DIMENSION (2) ::  omega  !< particle mean free path             
3687    REAL(wp), DIMENSION (2) ::  tva    !< temporary variable (m)       
3688!
3689!-- Initialisation
3690    coagc   = 0.0_wp
3691!
3692!-- 1) Initializing particle and ambient air variables
3693    diam  = (/ diam1, diam2 /) !< particle diameters (m)
3694    mpart = (/ mass1, mass2 /) !< particle masses (kg)
3695!-- Viscosity of air (kg/(m s))       
3696    visc = ( 7.44523E-3_wp * temp ** 1.5_wp ) /                                &
3697           ( 5093.0_wp * ( temp + 110.4_wp ) ) 
3698!-- Mean free path of air (m)           
3699    mfp = ( 1.656E-10_wp * temp + 1.828E-8_wp ) * ( p_0 + 1325.0_wp ) / pres
3700!
3701!-- 2) Slip correction factor for small particles
3702    knud = 2.0_wp * EXP( LOG(mfp) - LOG(diam) )! Knudsen number for air (15.23)
3703!-- Cunningham correction factor (Allen and Raabe, Aerosol Sci. Tech. 4, 269)       
3704    beta = 1.0_wp + knud * ( 1.142_wp + 0.558_wp * EXP( -0.999_wp / knud ) )
3705!
3706!-- 3) Particle properties
3707!-- Diffusion coefficient (m2/s) (Jacobson (2005) eq. 15.29)
3708    dfpart = beta * abo * temp / ( 3.0_wp * pi * visc * diam ) 
3709!-- Mean thermal velocity (m/s) (Jacobson (2005) eq. 15.32)
3710    mtvel = SQRT( ( 8.0_wp * abo * temp ) / ( pi * mpart ) )
3711!-- Particle mean free path (m) (Jacobson (2005) eq. 15.34 )
3712    omega = 8.0_wp * dfpart / ( pi * mtvel ) 
3713!-- Mean diameter (m)
3714    mdiam = 0.5_wp * ( diam(1) + diam(2) )
3715!
3716!-- 4) Calculation of fluxes (Brownian collision kernels) and flux matching
3717!-- following Jacobson (2005):
3718!-- Flux in continuum regime (m3/s) (eq. 15.28)
3719    flux(1) = 4.0_wp * pi * mdiam * ( dfpart(1) + dfpart(2) )
3720!-- Flux in free molec. regime (m3/s) (eq. 15.31)
3721    flux(2) = pi * SQRT( ( mtvel(1)**2.0_wp ) + ( mtvel(2)**2.0_wp ) ) *      &
3722              ( mdiam**2.0_wp )
3723!-- temporary variables (m) to calculate flux matching distance (m)
3724    tva(1) = ( ( mdiam + omega(1) )**3.0_wp - ( mdiam**2.0_wp +                &
3725               omega(1)**2.0_wp ) * SQRT( ( mdiam**2.0_wp + omega(1)**2.0_wp ) &
3726               ) ) / ( 3.0_wp * mdiam * omega(1) ) - mdiam
3727    tva(2) = ( ( mdiam + omega(2) )**3.0_wp - ( mdiam**2.0_wp +                &
3728               omega(2)**2.0_wp ) * SQRT( ( mdiam**2 + omega(2)**2 ) ) ) /     &
3729             ( 3.0_wp * mdiam * omega(2) ) - mdiam
3730!-- Flux matching distance (m) i.e. the mean distance from the centre of a
3731!-- sphere reached by particles leaving sphere's surface and travelling a
3732!-- distance of particle mean free path mfp (eq. 15 34)                 
3733    fmdist = SQRT( tva(1)**2 + tva(2)**2.0_wp) 
3734!
3735!-- 5) Coagulation coefficient (m3/s) (eq. 15.33). Here assumed
3736!-- coalescence efficiency 1!!
3737    coagc = flux(1) / ( mdiam / ( mdiam + fmdist) + flux(1) / flux(2) ) 
3738!-- coagulation coefficient = coalescence efficiency * collision kernel
3739!
3740!-- Corrected collision kernel following Karl et al., 2016 (ACP):
3741!-- Inclusion of van der Waals and viscous forces
3742    IF ( van_der_waals_coagc )  THEN
3743       knud_p = SQRT( omega(1)**2 + omega(2)**2 ) / mdiam   
3744       IF ( knud_p >= 0.1_wp  .AND.  knud_p <= 10.0_wp )  THEN
3745          coagc = coagc * ( 2.0_wp + 0.4_wp * LOG( knud_p ) )
3746       ELSE
3747          coagc = coagc * 3.0_wp
3748       ENDIF
3749    ENDIF
3750   
3751 END FUNCTION coagc
3752 
3753!------------------------------------------------------------------------------!   
3754! Description:
3755! ------------
3756!> Calculates the change in particle volume and gas phase
3757!> concentrations due to nucleation, condensation and dissolutional growth.
3758!
3759!> Sulphuric acid and organic vapour: only condensation and no evaporation.
3760!
3761!> New gas and aerosol phase concentrations calculated according to Jacobson
3762!> (1997): Numerical techniques to solve condensational and dissolutional growth
3763!> equations when growth is coupled to reversible reactions, Aerosol Sci. Tech.,
3764!> 27, pp 491-498.
3765!
3766!> Following parameterization has been used:
3767!> Molecular diffusion coefficient of condensing vapour (m2/s)
3768!> (Reid et al. (1987): Properties of gases and liquids, McGraw-Hill, New York.)
3769!> D = {1.d-7*sqrt(1/M_air + 1/M_gas)*T^1.75} / &
3770!      {p_atm/p_stand * (d_air^(1/3) + d_gas^(1/3))^2 }
3771! M_air = 28.965 : molar mass of air (g/mol)
3772! d_air = 19.70  : diffusion volume of air
3773! M_h2so4 = 98.08 : molar mass of h2so4 (g/mol)
3774! d_h2so4 = 51.96  : diffusion volume of h2so4
3775!
3776!> Called from main aerosol model
3777!
3778!> fxm: calculated for empty bins too
3779!> fxm: same diffusion coefficients and mean free paths used for sulphuric acid
3780!>      and organic vapours (average values? 'real' values for each?)
3781!> fxm: one should really couple with vapour production and loss terms as well
3782!>      should nucleation be coupled here as well????
3783!
3784! Coded by:
3785! Hannele Korhonen (FMI) 2005
3786! Harri Kokkola (FMI) 2006
3787! Juha Tonttila (FMI) 2014
3788! Rewritten to PALM by Mona Kurppa (UHel) 2017
3789!------------------------------------------------------------------------------!
3790 SUBROUTINE condensation( paero, pcsa, pcocnv, pcocsv, pchno3, pcnh3, pcw, pcs,&
3791                          ptemp, ppres, ptstep, prtcl )
3792       
3793    IMPLICIT NONE
3794   
3795!-- Input and output variables
3796    REAL(wp), INTENT(IN) ::  ppres !< ambient pressure (Pa)
3797    REAL(wp), INTENT(IN) ::  pcs   !< Water vapour saturation concentration
3798                                   !< (kg/m3)     
3799    REAL(wp), INTENT(IN) ::  ptemp !< ambient temperature (K)
3800    REAL(wp), INTENT(IN) ::  ptstep            !< timestep (s) 
3801    TYPE(component_index), INTENT(in) :: prtcl !< Keeps track which substances
3802                                               !< are used                                               
3803    REAL(wp), INTENT(INOUT) ::  pchno3 !< Gas concentrations (#/m3):
3804                                       !< nitric acid HNO3
3805    REAL(wp), INTENT(INOUT) ::  pcnh3  !< ammonia NH3
3806    REAL(wp), INTENT(INOUT) ::  pcocnv !< non-volatile organics
3807    REAL(wp), INTENT(INOUT) ::  pcocsv !< semi-volatile organics
3808    REAL(wp), INTENT(INOUT) ::  pcsa   !< sulphuric acid H2SO4
3809    REAL(wp), INTENT(INOUT) ::  pcw    !< Water vapor concentration (kg/m3)
3810    TYPE(t_section), INTENT(inout) ::  paero(fn2b) !< Aerosol properties                                     
3811!-- Local variables
3812    REAL(wp) ::  zbeta(fn2b) !< transitional correction factor for aerosols
3813    REAL(wp) ::  zcolrate(fn2b) !< collision rate of molecules to particles
3814                                !< (1/s)
3815    REAL(wp) ::  zcolrate_ocnv(fn2b) !< collision rate of organic molecules
3816                                     !< to particles (1/s)
3817    REAL(wp) ::  zcs_ocnv !< condensation sink of nonvolatile organics (1/s)       
3818    REAL(wp) ::  zcs_ocsv !< condensation sink of semivolatile organics (1/s)
3819    REAL(wp) ::  zcs_su !< condensation sink of sulfate (1/s)
3820    REAL(wp) ::  zcs_tot!< total condensation sink (1/s) (gases)
3821!-- vapour concentration after time step (#/m3)
3822    REAL(wp) ::  zcvap_new1 !< sulphuric acid
3823    REAL(wp) ::  zcvap_new2 !< nonvolatile organics
3824    REAL(wp) ::  zcvap_new3 !< semivolatile organics
3825    REAL(wp) ::  zdfpart(in1a+1) !< particle diffusion coefficient (m2/s)     
3826    REAL(wp) ::  zdfvap !< air diffusion coefficient (m2/s)
3827!-- change in vapour concentration (#/m3)
3828    REAL(wp) ::  zdvap1 !< sulphuric acid
3829    REAL(wp) ::  zdvap2 !< nonvolatile organics
3830    REAL(wp) ::  zdvap3 !< semivolatile organics
3831    REAL(wp) ::  zdvoloc(fn2b) !< change of organics volume in each bin [fxm]   
3832    REAL(wp) ::  zdvolsa(fn2b) !< change of sulphate volume in each bin [fxm]
3833    REAL(wp) ::  zj3n3(2)      !< Formation massrate of molecules in
3834                               !< nucleation, (molec/m3s). 1: H2SO4
3835                               !< and 2: organic vapor       
3836    REAL(wp) ::  zknud(fn2b) !< particle Knudsen number       
3837    REAL(wp) ::  zmfp    !< mean free path of condensing vapour (m)
3838    REAL(wp) ::  zrh     !< Relative humidity [0-1]         
3839    REAL(wp) ::  zvisc   !< viscosity of air (kg/(m s))     
3840    REAL(wp) ::  zn_vs_c !< ratio of nucleation of all mass transfer in the
3841                         !< smallest bin
3842    REAL(wp) ::  zxocnv  !< ratio of organic vapour in 3nm particles
3843    REAL(wp) ::  zxsa    !< Ratio in 3nm particles: sulphuric acid
3844   
3845    zj3n3  = 0.0_wp
3846    zrh    = pcw / pcs   
3847    zxocnv = 0.0_wp
3848    zxsa   = 0.0_wp
3849!
3850!-- Nucleation
3851    IF ( nsnucl > 0 )  THEN
3852       CALL nucleation( paero, ptemp, zrh, ppres, pcsa, pcocnv, pcnh3, ptstep, &
3853                        zj3n3, zxsa, zxocnv )
3854    ENDIF
3855!
3856!-- Condensation on pre-existing particles
3857    IF ( lscndgas )  THEN
3858!
3859!--    Initialise:
3860       zdvolsa = 0.0_wp 
3861       zdvoloc = 0.0_wp
3862       zcolrate = 0.0_wp
3863!             
3864!--    1) Properties of air and condensing gases:
3865!--    Viscosity of air (kg/(m s)) (Eq. 4.54 in Jabonson (2005))
3866       zvisc = ( 7.44523E-3_wp * ptemp ** 1.5_wp ) / ( 5093.0_wp *             &
3867                 ( ptemp + 110.4_wp ) )
3868!--    Diffusion coefficient of air (m2/s)
3869       zdfvap = 5.1111E-10_wp * ptemp ** 1.75_wp * ( p_0 + 1325.0_wp ) / ppres 
3870!--    Mean free path (m): same for H2SO4 and organic compounds
3871       zmfp = 3.0_wp * zdfvap * SQRT( pi * amh2so4 / ( 8.0_wp * argas * ptemp ) )
3872!                   
3873!--    2) Transition regime correction factor zbeta for particles:
3874!--       Fuchs and Sutugin (1971), In: Hidy et al. (ed.) Topics in current
3875!--       aerosol research, Pergamon. Size of condensing molecule considered 
3876!--       only for nucleation mode (3 - 20 nm)
3877!
3878!--    Particle Knudsen number: condensation of gases on aerosols
3879       zknud(in1a:in1a+1) = 2.0_wp * zmfp / ( paero(in1a:in1a+1)%dwet + d_sa )
3880       zknud(in1a+2:fn2b) = 2.0_wp * zmfp / paero(in1a+2:fn2b)%dwet
3881!   
3882!--    Transitional correction factor: aerosol + gas (the semi-empirical Fuchs-
3883!--    Sutugin interpolation function (Fuchs and Sutugin, 1971))
3884       zbeta = ( zknud + 1.0_wp ) / ( 0.377_wp * zknud + 1.0_wp + 4.0_wp /     &
3885               ( 3.0_wp * massacc ) * ( zknud + zknud ** 2.0_wp ) )
3886!                   
3887!--    3) Collision rate of molecules to particles
3888!--       Particle diffusion coefficient considered only for nucleation mode
3889!--       (3 - 20 nm)
3890!
3891!--    Particle diffusion coefficient (m2/s) (e.g. Eq. 15.29 in Jacobson (2005))
3892       zdfpart = abo * ptemp * zbeta(in1a:in1a+1) / ( 3.0_wp * pi * zvisc *    &
3893                 paero(in1a:in1a+1)%dwet )
3894!             
3895!--    Collision rate (mass-transfer coefficient): gases on aerosols (1/s)
3896!--    (Eq. 16.64 in Jacobson (2005))
3897       zcolrate(in1a:in1a+1) = MERGE( 2.0_wp * pi *                            &
3898                                      ( paero(in1a:in1a+1)%dwet + d_sa ) *     &
3899                                      ( zdfvap + zdfpart ) * zbeta(in1a:in1a+1)& 
3900                                        * paero(in1a:in1a+1)%numc, 0.0_wp,     &
3901                                      paero(in1a:in1a+1)%numc > nclim )
3902       zcolrate(in1a+2:fn2b) = MERGE( 2.0_wp * pi * paero(in1a+2:fn2b)%dwet *  &
3903                                      zdfvap * zbeta(in1a+2:fn2b) *            &
3904                                      paero(in1a+2:fn2b)%numc, 0.0_wp,         &
3905                                      paero(in1a+2:fn2b)%numc > nclim )
3906!                 
3907!-- 4) Condensation sink (1/s)
3908       zcs_tot = SUM( zcolrate )   ! total sink
3909!
3910!--    5) Changes in gas-phase concentrations and particle volume
3911!
3912!--    5.1) Organic vapours
3913!
3914!--    5.1.1) Non-volatile organic compound: condenses onto all bins
3915       IF ( pcocnv > 1.0E+10_wp  .AND.  zcs_tot > 1.0E-30_wp  .AND.            &
3916            is_used( prtcl,'OC' ) )                                            &
3917       THEN
3918!--       Ratio of nucleation vs. condensation rates in the smallest bin   
3919          zn_vs_c = 0.0_wp 
3920          IF ( zj3n3(2) > 1.0_wp )  THEN
3921             zn_vs_c = ( zj3n3(2) ) / ( zj3n3(2) + pcocnv * zcolrate(in1a) )
3922          ENDIF
3923!       
3924!--       Collision rate in the smallest bin, including nucleation and
3925!--       condensation(see Jacobson, Fundamentals of Atmospheric Modeling, 2nd
3926!--       Edition (2005), equation (16.73) )
3927          zcolrate_ocnv = zcolrate
3928          zcolrate_ocnv(in1a) = zcolrate_ocnv(in1a) + zj3n3(2) / pcocnv
3929!       
3930!--       Total sink for organic vapor
3931          zcs_ocnv = zcs_tot + zj3n3(2) / pcocnv
3932!       
3933!--       New gas phase concentration (#/m3)
3934          zcvap_new2 = pcocnv / ( 1.0_wp + ptstep * zcs_ocnv )
3935!       
3936!--       Change in gas concentration (#/m3)
3937          zdvap2 = pcocnv - zcvap_new2
3938!
3939!--       Updated vapour concentration (#/m3)               
3940          pcocnv = zcvap_new2
3941!       
3942!--       Volume change of particles (m3(OC)/m3(air))
3943          zdvoloc = zcolrate_ocnv(in1a:fn2b) / zcs_ocnv * amvoc * zdvap2
3944!       
3945!--       Change of volume due to condensation in 1a-2b
3946          paero(in1a:fn2b)%volc(2) = paero(in1a:fn2b)%volc(2) + zdvoloc 
3947!       
3948!--       Change of number concentration in the smallest bin caused by
3949!--       nucleation (Jacobson (2005), equation (16.75)). If zxocnv = 0, then 
3950!--       the chosen nucleation mechanism doesn't take into account the non-
3951!--       volatile organic vapors and thus the paero doesn't have to be updated.
3952          IF ( zxocnv > 0.0_wp )  THEN
3953             paero(in1a)%numc = paero(in1a)%numc + zn_vs_c * zdvoloc(in1a) /   &
3954                                amvoc / ( n3 * zxocnv )
3955          ENDIF
3956       ENDIF
3957!   
3958!--    5.1.2) Semivolatile organic compound: all bins except subrange 1
3959       zcs_ocsv = SUM( zcolrate(in2a:fn2b) ) !< sink for semi-volatile organics
3960       IF ( pcocsv > 1.0E+10_wp  .AND.  zcs_ocsv > 1.0E-30  .AND.              &
3961            is_used( prtcl,'OC') )                                             &
3962       THEN
3963!
3964!--       New gas phase concentration (#/m3)
3965          zcvap_new3 = pcocsv / ( 1.0_wp + ptstep * zcs_ocsv )
3966!       
3967!--       Change in gas concentration (#/m3)
3968          zdvap3 = pcocsv - zcvap_new3 
3969!       
3970!--       Updated gas concentration (#/m3)               
3971          pcocsv = zcvap_new3
3972!       
3973!--       Volume change of particles (m3(OC)/m3(air))
3974          zdvoloc(in2a:fn2b) = zdvoloc(in2a:fn2b) + zcolrate(in2a:fn2b) /      &
3975                               zcs_ocsv * amvoc * zdvap3
3976!                           
3977!--       Change of volume due to condensation in 1a-2b
3978          paero(in1a:fn2b)%volc(2) = paero(in1a:fn2b)%volc(2) + zdvoloc 
3979       ENDIF
3980!
3981!-- 5.2) Sulphate: condensed on all bins
3982       IF ( pcsa > 1.0E+10_wp  .AND.  zcs_tot > 1.0E-30_wp  .AND.              &
3983            is_used( prtcl,'SO4' ) )                                           &
3984       THEN
3985!   
3986!--    Ratio of mass transfer between nucleation and condensation
3987          zn_vs_c = 0.0_wp
3988          IF ( zj3n3(1) > 1.0_wp )  THEN
3989             zn_vs_c = ( zj3n3(1) ) / ( zj3n3(1) + pcsa * zcolrate(in1a) )
3990          ENDIF
3991!       
3992!--       Collision rate in the smallest bin, including nucleation and
3993!--       condensation (see Jacobson, Fundamentals of Atmospheric Modeling, 2nd
3994!--       Edition (2005), equation (16.73))
3995          zcolrate(in1a) = zcolrate(in1a) + zj3n3(1) / pcsa     
3996!       
3997!--       Total sink for sulfate (1/s)
3998          zcs_su = zcs_tot + zj3n3(1) / pcsa
3999!       
4000!--       Sulphuric acid:
4001!--       New gas phase concentration (#/m3)
4002          zcvap_new1 = pcsa / ( 1.0_wp + ptstep * zcs_su )
4003!       
4004!--       Change in gas concentration (#/m3)
4005          zdvap1 = pcsa - zcvap_new1
4006!       
4007!--       Updating vapour concentration (#/m3)
4008          pcsa = zcvap_new1
4009!       
4010!--       Volume change of particles (m3(SO4)/m3(air)) by condensation
4011          zdvolsa = zcolrate(in1a:fn2b) / zcs_su * amvh2so4 * zdvap1
4012!--       For validation: zdvolsa = 5.5 mum3/cm3 per 12 h       
4013       !   zdvolsa = zdvolsa / SUM( zdvolsa ) * 5.5E-12_wp * dt_salsa / 43200.0_wp 
4014          !0.3E-12_wp, 0.6E-12_wp, 11.0E-12_wp, 4.6E-12_wp, 9.2E-12_wp   
4015!       
4016!--       Change of volume concentration of sulphate in aerosol [fxm]
4017          paero(in1a:fn2b)%volc(1) = paero(in1a:fn2b)%volc(1) + zdvolsa
4018!       
4019!--       Change of number concentration in the smallest bin caused by nucleation
4020!--       (Jacobson (2005), equation (16.75))
4021          IF ( zxsa > 0.0_wp )  THEN
4022             paero(in1a)%numc = paero(in1a)%numc + zn_vs_c * zdvolsa(in1a) /   &
4023                                amvh2so4 / ( n3 * zxsa )
4024          ENDIF
4025       ENDIF
4026    ENDIF
4027!
4028!
4029!-- Condensation of water vapour
4030    IF ( lscndh2oae )  THEN
4031       CALL gpparth2o( paero, ptemp, ppres, pcs, pcw, ptstep )
4032    ENDIF
4033!   
4034!
4035!-- Partitioning of H2O, HNO3, and NH3: Dissolutional growth
4036    IF ( lscndgas  .AND.  ino > 0  .AND.  inh > 0  .AND.                       &
4037         ( pchno3 > 1.0E+10_wp  .OR.  pcnh3 > 1.0E+10_wp ) )                   &
4038    THEN
4039       CALL gpparthno3( ppres, ptemp, paero, pchno3, pcnh3, pcw, pcs, zbeta,   &
4040                        ptstep )
4041    ENDIF
4042   
4043 END SUBROUTINE condensation
4044 
4045!------------------------------------------------------------------------------!
4046! Description:
4047! ------------
4048!> Calculates the particle number and volume increase, and gas-phase
4049!> concentration decrease due to nucleation subsequent growth to detectable size
4050!> of 3 nm.
4051!
4052!> Method:
4053!> When the formed clusters grow by condensation (possibly also by self-
4054!> coagulation), their number is reduced due to scavenging to pre-existing
4055!> particles. Thus, the apparent nucleation rate at 3 nm is significantly lower
4056!> than the real nucleation rate (at ~1 nm).
4057!
4058!> Calculation of the formation rate of detectable particles at 3 nm (i.e. J3):
4059!> nj3 = 1: Kerminen, V.-M. and Kulmala, M. (2002), J. Aerosol Sci.,33, 609-622.
4060!> nj3 = 2: Lehtinen et al. (2007), J. Aerosol Sci., 38(9), 988-994.
4061!> nj3 = 3: Anttila et al. (2010), J. Aerosol Sci., 41(7), 621-636.
4062!
4063!> Called from subroutine condensation (in module salsa_dynamics_mod.f90)
4064!
4065!> Calls one of the following subroutines:
4066!>  - binnucl
4067!>  - ternucl
4068!>  - kinnucl
4069!>  - actnucl
4070!
4071!> fxm: currently only sulphuric acid grows particles from 1 to 3 nm
4072!>  (if asked from Markku, this is terribly wrong!!!)
4073!
4074!> Coded by:
4075!> Hannele Korhonen (FMI) 2005
4076!> Harri Kokkola (FMI) 2006
4077!> Matti Niskanen(FMI) 2012
4078!> Anton Laakso  (FMI) 2013
4079!------------------------------------------------------------------------------!
4080
4081 SUBROUTINE nucleation( paero, ptemp, prh, ppres, pcsa, pcocnv, pcnh3, ptstep, &
4082                        pj3n3, pxsa, pxocnv )
4083    IMPLICIT NONE
4084!       
4085!-- Input and output variables
4086    REAL(wp), INTENT(in) ::  pcnh3    !< ammonia concentration (#/m3)
4087    REAL(wp), INTENT(in) ::  pcocnv   !< conc. of non-volatile OC (#/m3)     
4088    REAL(wp), INTENT(in) ::  pcsa     !< sulphuric acid conc. (#/m3)
4089    REAL(wp), INTENT(in) ::  ppres    !< ambient air pressure (Pa)
4090    REAL(wp), INTENT(in) ::  prh      !< ambient rel. humidity [0-1]       
4091    REAL(wp), INTENT(in) ::  ptemp    !< ambient temperature (K)
4092    REAL(wp), INTENT(in) ::  ptstep   !< time step (s) of SALSA
4093    TYPE(t_section), INTENT(inout) ::  paero(fn2b) !< aerosol properties                                                 
4094    REAL(wp), INTENT(inout) ::  pj3n3(2) !< formation mass rate of molecules
4095                                         !< (molec/m3s) for 1: H2SO4 and
4096                                         !< 2: organic vapour
4097    REAL(wp), INTENT(out) ::  pxocnv !< ratio of non-volatile organic vapours in
4098                                     !< 3nm aerosol particles
4099    REAL(wp), INTENT(out) ::  pxsa   !< ratio of H2SO4 in 3nm aerosol particles
4100!-- Local variables
4101    INTEGER(iwp) ::  iteration
4102    REAL(wp) ::  zbeta(fn2b)  !< transitional correction factor                                         
4103    REAL(wp) ::  zc_h2so4     !< H2SO4 conc. (#/cm3) !UNITS!
4104    REAL(wp) ::  zc_org       !< organic vapour conc. (#/cm3)
4105    REAL(wp) ::  zCoagStot    !< total losses due to coagulation, including
4106                              !< condensation and self-coagulation       
4107    REAL(wp) ::  zcocnv_local !< organic vapour conc. (#/m3)
4108    REAL(wp) ::  zcsink       !< condensational sink (#/m2)       
4109    REAL(wp) ::  zcsa_local   !< H2SO4 conc. (#/m3)       
4110    REAL(wp) ::  zdcrit       !< diameter of critical cluster (m)
4111    REAL(wp) ::  zdelta_vap   !< change of H2SO4 and organic vapour
4112                              !< concentration (#/m3)       
4113    REAL(wp) ::  zdfvap       !< air diffusion coefficient (m2/s)
4114    REAL(wp) ::  zdmean       !< mean diameter of existing particles (m)
4115    REAL(wp) ::  zeta         !< constant: proportional to ratio of CS/GR (m)
4116                              !< (condensation sink / growth rate)                                   
4117    REAL(wp) ::  zgamma       !< proportionality factor ((nm2*m2)/h)                                       
4118    REAL(wp) ::  zGRclust     !< growth rate of formed clusters (nm/h)
4119    REAL(wp) ::  zGRtot       !< total growth rate       
4120    REAL(wp) ::  zj3          !< number conc. of formed 3nm particles (#/m3)       
4121    REAL(wp) ::  zjnuc        !< nucleation rate at ~1nm (#/m3s)
4122    REAL(wp) ::  zKeff        !< effective cogulation coefficient between
4123                              !< freshly nucleated particles       
4124    REAL(wp) ::  zknud(fn2b)  !< particle Knudsen number       
4125    REAL(wp) ::  zkocnv       !< lever: zkocnv=1 --> organic compounds involved
4126                              !< in nucleation   
4127    REAL(wp) ::  zksa         !< lever: zksa=1 --> H2SO4 involved in nucleation
4128    REAL(wp) ::  zlambda      !< parameter for adjusting the growth rate due to
4129                              !< self-coagulation                                 
4130    REAL(wp) ::  zmfp         !< mean free path of condesing vapour(m)                                       
4131    REAL(wp) ::  zmixnh3      !< ammonia mixing ratio (ppt)
4132    REAL(wp) ::  zNnuc        !< number of clusters/particles at the size range
4133                              !< d1-dx (#/m3) 
4134    REAL(wp) ::  znoc         !< number of organic molecules in critical cluster
4135    REAL(wp) ::  znsa         !< number of H2SO4 molecules in critical cluster                                           
4136!
4137!-- Variable determined for the m-parameter
4138    REAL(wp) ::  zCc_2(fn2b) !<
4139    REAL(wp) ::  zCc_c !<
4140    REAL(wp) ::  zCc_x !<
4141    REAL(wp) ::  zCoagS_c !<
4142    REAL(wp) ::  zCoagS_x !<
4143    REAL(wp) ::  zcv_2(fn2b) !<
4144    REAL(wp) ::  zcv_c !<
4145    REAL(wp) ::  zcv_c2(fn2b) !<
4146    REAL(wp) ::  zcv_x !<
4147    REAL(wp) ::  zcv_x2(fn2b) !<
4148    REAL(wp) ::  zDc_2(fn2b) !<
4149    REAL(wp) ::  zDc_c(fn2b) !<
4150    REAL(wp) ::  zDc_c2(fn2b) !<
4151    REAL(wp) ::  zDc_x(fn2b) !<
4152    REAL(wp) ::  zDc_x2(fn2b) !<
4153    REAL(wp) ::  zgammaF_2(fn2b) !<
4154    REAL(wp) ::  zgammaF_c(fn2b) !<
4155    REAL(wp) ::  zgammaF_x(fn2b) !<
4156    REAL(wp) ::  zK_c2(fn2b) !<
4157    REAL(wp) ::  zK_x2(fn2b) !<
4158    REAL(wp) ::  zknud_2(fn2b) !<
4159    REAL(wp) ::  zknud_c !<
4160    REAL(wp) ::  zknud_x !<       
4161    REAL(wp) ::  zm_2(fn2b) !<
4162    REAL(wp) ::  zm_c !<
4163    REAL(wp) ::  zm_para !<
4164    REAL(wp) ::  zm_x !<
4165    REAL(wp) ::  zmyy !<
4166    REAL(wp) ::  zomega_2c(fn2b) !<
4167    REAL(wp) ::  zomega_2x(fn2b) !<
4168    REAL(wp) ::  zomega_c(fn2b) !<
4169    REAL(wp) ::  zomega_x(fn2b) !<
4170    REAL(wp) ::  zRc2(fn2b) !<
4171    REAL(wp) ::  zRx2(fn2b) !<
4172    REAL(wp) ::  zsigma_c2(fn2b) !<
4173    REAL(wp) ::  zsigma_x2(fn2b) !<
4174!
4175!-- 1) Nucleation rate (zjnuc) and diameter of critical cluster (zdcrit)
4176    zjnuc  = 0.0_wp
4177    znsa   = 0.0_wp
4178    znoc   = 0.0_wp
4179    zdcrit = 0.0_wp
4180    zksa   = 0.0_wp
4181    zkocnv = 0.0_wp
4182   
4183    SELECT CASE ( nsnucl )
4184   
4185    CASE(1)   ! Binary H2SO4-H2O nucleation
4186       
4187       zc_h2so4 = pcsa * 1.0E-6_wp   ! sulphuric acid conc. to #/cm3
4188       CALL binnucl( zc_h2so4, ptemp, prh, zjnuc, znsa, znoc, zdcrit,  zksa, &
4189                     zkocnv )     
4190   
4191    CASE(2)   ! Activation type nucleation
4192   
4193       zc_h2so4 = pcsa * 1.0E-6_wp   ! sulphuric acid conc. to #/cm3
4194       CALL binnucl( zc_h2so4, ptemp, prh, zjnuc, znsa,  znoc, zdcrit, zksa,  &
4195                     zkocnv )
4196       CALL actnucl( pcsa, zjnuc, zdcrit, znsa, znoc, zksa, zkocnv, act_coeff )
4197   
4198    CASE(3)   ! Kinetically limited nucleation of (NH4)HSO4 clusters
4199       
4200       zc_h2so4 = pcsa * 1.0E-6_wp   ! sulphuric acid conc. to #/cm3
4201       CALL binnucl( zc_h2so4, ptemp, prh, zjnuc, znsa, znoc, zdcrit, zksa,    &
4202                     zkocnv )
4203
4204       CALL kinnucl( zc_h2so4, zjnuc, zdcrit, znsa, znoc, zksa, zkocnv )
4205   
4206    CASE(4)   ! Ternary H2SO4-H2O-NH3 nucleation
4207   
4208       zmixnh3 = pcnh3 * ptemp * argas / ( ppres * avo )
4209       zc_h2so4 = pcsa * 1.0E-6_wp   ! sulphuric acid conc. to #/cm3
4210       CALL ternucl( zc_h2so4, zmixnh3, ptemp, prh, zjnuc, znsa, znoc, zdcrit, &
4211                     zksa, zkocnv ) 
4212   
4213    CASE(5)   ! Organic nucleation, J~[ORG] or J~[ORG]**2
4214   
4215       zc_org = pcocnv * 1.0E-6_wp   ! conc. of non-volatile OC to #/cm3
4216       zc_h2so4 = pcsa * 1.0E-6_wp   ! sulphuric acid conc. to #/cm3
4217       CALL binnucl( zc_h2so4, ptemp, prh, zjnuc, znsa, znoc, zdcrit, zksa,    &
4218                     zkocnv ) 
4219       CALL orgnucl( pcocnv, zjnuc, zdcrit, znsa, znoc, zksa, zkocnv )
4220   
4221    CASE(6)   ! Sum of H2SO4 and organic activation type nucleation,
4222              ! J~[H2SO4]+[ORG]
4223       
4224       zc_h2so4 = pcsa * 1.0E-6_wp   ! sulphuric acid conc. to #/cm3
4225       CALL binnucl( zc_h2so4, ptemp, prh, zjnuc, znsa, znoc, zdcrit, zksa,    &
4226                     zkocnv ) 
4227       CALL sumnucl( pcsa, pcocnv, zjnuc, zdcrit, znsa, znoc, zksa, zkocnv )
4228
4229           
4230    CASE(7)   ! Heteromolecular nucleation, J~[H2SO4]*[ORG]
4231       
4232       zc_h2so4 = pcsa * 1.0E-6_wp   ! sulphuric acid conc. to #/cm3
4233       zc_org = pcocnv * 1.0E-6_wp   ! conc. of non-volatile OC to #/cm3
4234       CALL binnucl( zc_h2so4, ptemp, prh, zjnuc, znsa, znoc, zdcrit, zksa,    &
4235                     zkocnv ) 
4236       CALL hetnucl( zc_h2so4, zc_org, zjnuc, zdcrit, znsa, znoc, zksa, zkocnv )
4237   
4238    CASE(8)   ! Homomolecular nucleation of H2SO4 and heteromolecular
4239              ! nucleation of H2SO4 and organic vapour,
4240              ! J~[H2SO4]**2 + [H2SO4]*[ORG] (EUCAARI project)
4241       zc_h2so4 = pcsa * 1.0E-6_wp   ! sulphuric acid conc. to #/cm3
4242       zc_org = pcocnv * 1.0E-6_wp   ! conc. of non-volatile OC to #/cm3
4243       CALL binnucl( zc_h2so4, ptemp, prh, zjnuc, znsa, znoc, zdcrit, zksa,    &
4244                     zkocnv ) 
4245       CALL SAnucl( zc_h2so4, zc_org, zjnuc, zdcrit, znsa, znoc, zksa, zkocnv )
4246   
4247    CASE(9)   ! Homomolecular nucleation of H2SO4 and organic vapour and
4248              ! heteromolecular nucleation of H2SO4 and organic vapour,
4249              ! J~[H2SO4]**2 + [H2SO4]*[ORG]+[ORG]**2 (EUCAARI project)
4250   
4251       zc_h2so4 = pcsa * 1.0E-6_wp   ! sulphuric acid conc. to #/cm3
4252       zc_org = pcocnv * 1.0E-6_wp   ! conc. of non-volatile OC to #/cm3
4253       CALL binnucl( zc_h2so4, ptemp, prh, zjnuc, znsa, znoc, zdcrit, zksa,    &
4254                     zkocnv ) 
4255
4256       CALL SAORGnucl( zc_h2so4, zc_org, zjnuc, zdcrit, znsa, znoc, zksa,      &
4257                       zkocnv )
4258    END SELECT
4259   
4260    zcsa_local = pcsa
4261    zcocnv_local = pcocnv
4262!
4263!-- 2) Change of particle and gas concentrations due to nucleation
4264!         
4265!-- 2.1) Check that there is enough H2SO4 and organic vapour to produce the
4266!--      nucleation 
4267    IF ( nsnucl <= 4 )  THEN 
4268!--    If the chosen nucleation scheme is 1-4, nucleation occurs only due to
4269!--    H2SO4. All of the total vapour concentration that is taking part to the
4270!--    nucleation is there for sulphuric acid (sa = H2SO4) and non-volatile
4271!--    organic vapour is zero.
4272       pxsa   = 1.0_wp   ! ratio of sulphuric acid in 3nm particles
4273       pxocnv = 0.0_wp   ! ratio of non-volatile origanic vapour
4274                                ! in 3nm particles
4275    ELSEIF ( nsnucl > 4 )  THEN
4276!--    If the chosen nucleation scheme is 5-9, nucleation occurs due to organic
4277!--    vapour or the combination of organic vapour and H2SO4. The number of
4278!--    needed molecules depends on the chosen nucleation type and it has an
4279!--    effect also on the minimum ratio of the molecules present.
4280       IF ( pcsa * znsa + pcocnv * znoc < 1.E-14_wp )  THEN
4281          pxsa   = 0.0_wp
4282          pxocnv = 0.0_wp             
4283       ELSE
4284          pxsa   = pcsa * znsa / ( pcsa * znsa + pcocnv * znoc ) 
4285          pxocnv = pcocnv * znoc / ( pcsa * znsa + pcocnv * znoc )
4286       ENDIF 
4287    ENDIF
4288!   
4289!-- The change in total vapour concentration is the sum of the concentrations
4290!-- of the vapours taking part to the nucleation (depends on the chosen
4291!-- nucleation scheme)
4292    zdelta_vap = MIN( zjnuc * ( znoc + znsa ), ( pcocnv * zkocnv + pcsa *      &
4293                      zksa ) / ptstep ) 
4294!                     
4295!-- Nucleation rate J at ~1nm (#/m3s)                           
4296    zjnuc = zdelta_vap / ( znoc + znsa )
4297!   
4298!-- H2SO4 concentration after nucleation in #/m3           
4299    zcsa_local = MAX( 1.0_wp, pcsa - zdelta_vap * pxsa ) 
4300!   
4301!-- Non-volative organic vapour concentration after nucleation (#/m3)
4302    zcocnv_local = MAX( 1.0_wp, pcocnv - zdelta_vap * pxocnv )
4303!
4304!-- 2.2) Formation rate of 3 nm particles (Kerminen & Kulmala, 2002)
4305!
4306!-- 2.2.1) Growth rate of clusters formed by H2SO4
4307!
4308!-- GR = 3.0e-15 / dens_clus * sum( molecspeed * molarmass * conc )
4309
4310!-- dens_clus  = density of the clusters (here 1830 kg/m3)
4311!-- molarmass  = molar mass of condensing species (here 98.08 g/mol)
4312!-- conc       = concentration of condensing species [#/m3]
4313!-- molecspeed = molecular speed of condensing species [m/s]
4314!--            = sqrt( 8.0 * R * ptemp / ( pi * molarmass ) )
4315!-- (Seinfeld & Pandis, 1998)
4316!
4317!-- Growth rate by H2SO4 and organic vapour in nm/h (Eq. 21)
4318    zGRclust = 2.3623E-15_wp * SQRT( ptemp ) * ( zcsa_local + zcocnv_local )
4319!   
4320!-- 2.2.2) Condensational sink of pre-existing particle population
4321!
4322!-- Diffusion coefficient (m2/s)
4323    zdfvap = 5.1111E-10_wp * ptemp ** 1.75_wp * ( p_0 + 1325.0_wp ) / ppres
4324!-- Mean free path of condensing vapour (m) (Jacobson (2005), Eq. 15.25 and
4325!-- 16.29)
4326    zmfp = 3.0_wp * zdfvap * SQRT( pi * amh2so4 / ( 8.0_wp * argas * ptemp ) )
4327!-- Knudsen number           
4328    zknud = 2.0_wp * zmfp / ( paero(:)%dwet + d_sa )                     
4329!-- Transitional regime correction factor (zbeta) according to Fuchs and
4330!-- Sutugin (1971), In: Hidy et al. (ed.), Topics in current  aerosol research,
4331!-- Pergamon. (Eq. 4 in Kerminen and Kulmala, 2002)
4332    zbeta = ( zknud + 1.0_wp) / ( 0.377_wp * zknud + 1.0_wp + 4.0_wp /         &
4333            ( 3.0_wp * massacc ) * ( zknud + zknud ** 2 ) ) 
4334!-- Condensational sink (#/m2) (Eq. 3)
4335    zcsink = SUM( paero(:)%dwet * zbeta * paero(:)%numc )
4336!
4337!-- Parameterised formation rate of detectable 3 nm particles (i.e. J3)
4338    IF ( nj3 == 1 )  THEN   ! Kerminen and Kulmala (2002)
4339!--    2.2.3) Parameterised formation rate of detectable 3 nm particles
4340!--    Constants needed for the parameterisation:
4341!--    dapp = 3 nm and dens_nuc = 1830 kg/m3
4342       IF ( zcsink < 1.0E-30_wp )  THEN
4343          zeta = 0._dp
4344       ELSE
4345!--       Mean diameter of backgroud population (nm)
4346          zdmean = 1.0_wp / SUM( paero(:)%numc ) * SUM( paero(:)%numc *        &
4347                   paero(:)%dwet ) * 1.0E+9_wp
4348!--       Proportionality factor (nm2*m2/h) (Eq. 22)
4349          zgamma = 0.23_wp * ( zdcrit * 1.0E+9_wp ) ** 0.2_wp * ( zdmean /     &
4350                 150.0_wp ) ** 0.048_wp * ( ptemp / 293.0_wp ) ** ( -0.75_wp ) &
4351                 * ( arhoh2so4 / 1000.0_wp ) ** ( -0.33_wp )
4352!--       Factor eta (nm) (Eq. 11)
4353          zeta = MIN( zgamma * zcsink / zGRclust, zdcrit * 1.0E11_wp ) 
4354       ENDIF
4355!       
4356!--    Number conc. of clusters surviving to 3 nm in a time step (#/m3) (Eq.14)
4357       zj3 = zjnuc * EXP( MIN( 0.0_wp, zeta / 3.0_wp - zeta /                  &
4358                               ( zdcrit * 1.0E9_wp ) ) )                   
4359
4360    ELSEIF ( nj3 > 1 )  THEN
4361!--    Defining the value for zm_para. The growth is investigated between
4362!--    [d1,reglim(1)] = [zdcrit,3nm]   
4363!--    m = LOG( CoagS_dx / CoagX_zdcrit ) / LOG( reglim / zdcrit )
4364!--    (Lehtinen et al. 2007, Eq. 5)
4365!--    The steps for the coagulation sink for reglim = 3nm and zdcrit ~= 1nm are
4366!--    explained in article of Kulmala et al. (2001). The particles of diameter
4367!--    zdcrit ~1.14 nm  and reglim = 3nm are both in turn the "number 1"
4368!--    variables (Kulmala et al. 2001).             
4369!--    c = critical (1nm), x = 3nm, 2 = wet or mean droplet
4370!--    Sum of the radii, R12 = R1 + zR2 (m) of two particles 1 and 2
4371       zRc2 = zdcrit / 2.0_wp + paero(:)%dwet / 2.0_wp
4372       zRx2 = reglim(1) / 2.0_wp + paero(:)%dwet / 2.0_wp
4373!       
4374!--    The mass of particle (kg) (comes only from H2SO4)
4375       zm_c = 4.0_wp / 3.0_wp * pi * ( zdcrit / 2.0_wp ) ** 3.0_wp * arhoh2so4                     
4376       zm_x = 4.0_wp / 3.0_wp * pi * ( reglim(1) / 2.0_wp ) ** 3.0_wp *        &
4377              arhoh2so4                 
4378       zm_2 = 4.0_wp / 3.0_wp * pi * ( paero(:)%dwet / 2.0_wp )** 3.0_wp *     &
4379              arhoh2so4
4380!             
4381!--    Mean relative thermal velocity between the particles (m/s)
4382       zcv_c = SQRT( 8.0_wp * abo * ptemp / ( pi * zm_c ) )
4383       zcv_x = SQRT( 8.0_wp * abo * ptemp / ( pi * zm_x ) )
4384       zcv_2 = SQRT( 8.0_wp * abo * ptemp / ( pi * zm_2 ) )
4385!       
4386!--    Average velocity after coagulation               
4387       zcv_c2 = SQRT( zcv_c ** 2.0_wp + zcv_2 ** 2.0_wp )
4388       zcv_x2 = SQRT( zcv_x ** 2.0_wp + zcv_2 ** 2.0_wp )
4389!       
4390!--    Knudsen number (zmfp = mean free path of condensing vapour)
4391       zknud_c = 2.0_wp * zmfp / zdcrit
4392       zknud_x = 2.0_wp * zmfp / reglim(1)
4393       zknud_2 = MAX( 0.0_wp, 2.0_wp * zmfp / paero(:)%dwet )
4394!
4395!--    Cunningham correction factor               
4396       zCc_c = 1.0_wp + zknud_c * ( 1.142_wp + 0.558_wp *                      &
4397               EXP( -0.999_wp / zknud_c ) ) 
4398       zCc_x = 1.0_wp + zknud_x * ( 1.142_wp + 0.558_wp *                      &
4399               EXP( -0.999_wp / zknud_x ) )
4400       zCc_2 = 1.0_wp + zknud_2 * ( 1.142_wp + 0.558_wp *                      &
4401               EXP( -0.999_wp / zknud_2 ) )
4402!                     
4403!--    Gas dynamic viscosity (N*s/m2).
4404!--    Viscocity(air @20C) = 1.81e-5_dp N/m2 *s (Hinds, p. 25)                     
4405       zmyy = 1.81E-5_wp * ( ptemp / 293.0_wp) ** ( 0.74_wp ) 
4406!       
4407!--    Particle diffusion coefficient (m2/s)               
4408       zDc_c = abo * ptemp * zCc_c / ( 3.0_wp * pi * zmyy * zdcrit ) 
4409       zDc_x = abo * ptemp * zCc_x / ( 3.0_wp * pi * zmyy * reglim(1) )
4410       zDc_2 = abo * ptemp * zCc_2 / ( 3.0_wp * pi * zmyy * paero(:)%dwet )
4411!       
4412!--    D12 = D1+D2 (Seinfield and Pandis, 2nd ed. Eq. 13.38)
4413       zDc_c2 = zDc_c + zDc_2   
4414       zDc_x2 = zDc_x + zDc_2 
4415!       
4416!--    zgammaF = 8*D/pi/zcv (m) for calculating zomega
4417       zgammaF_c = 8.0_wp * zDc_c / pi / zcv_c 
4418       zgammaF_x = 8.0_wp * zDc_x / pi / zcv_x
4419       zgammaF_2 = 8.0_wp * zDc_2 / pi / zcv_2
4420!       
4421!--    zomega (m) for calculating zsigma             
4422       zomega_c = ( ( zRc2 + zgammaF_c ) ** 3 - ( zRc2 ** 2 +                  &
4423                      zgammaF_c ) ** ( 3.0_wp / 2.0_wp ) ) / ( 3.0_wp *        &
4424                      zRc2 * zgammaF_c ) - zRc2 
4425       zomega_x = ( ( zRx2 + zgammaF_x ) ** 3.0_wp - ( zRx2 ** 2.0_wp +        &
4426                      zgammaF_x ) ** ( 3.0_wp / 2.0_wp ) ) / ( 3.0_wp *        &
4427                      zRx2 * zgammaF_x ) - zRx2
4428       zomega_2c = ( ( zRc2 + zgammaF_2 ) ** 3.0_wp - ( zRc2 ** 2.0_wp +       &
4429                       zgammaF_2 ) ** ( 3.0_wp / 2.0_wp ) ) / ( 3.0_wp *       &
4430                       zRc2 * zgammaF_2 ) - zRc2 
4431       zomega_2x = ( ( zRx2 + zgammaF_2 ) ** 3.0_wp - ( zRx2 ** 2.0_wp +       &
4432                       zgammaF_2 ) ** ( 3.0_wp / 2.0_wp ) ) / ( 3.0_wp *       &
4433                       zRx2 * zgammaF_2 ) - zRx2 
4434!                       
4435!--    The distance (m) at which the two fluxes are matched (condensation and
4436!--    coagulation sinks?)           
4437       zsigma_c2 = SQRT( zomega_c ** 2.0_wp + zomega_2c ** 2.0_wp ) 
4438       zsigma_x2 = SQRT( zomega_x ** 2.0_wp + zomega_2x ** 2.0_wp ) 
4439!       
4440!--    Coagulation coefficient in the continuum regime (m*m2/s)
4441       zK_c2 = 4.0_wp * pi * zRc2 * zDc_c2 / ( zRc2 / ( zRc2 + zsigma_c2 ) +   &
4442               4.0_wp * zDc_c2 / ( zcv_c2 * zRc2 ) ) 
4443       zK_x2 = 4.0_wp * pi * zRx2 * zDc_x2 / ( zRx2 / ( zRx2 + zsigma_x2 ) +   &
4444               4.0_wp * zDc_x2 / ( zcv_x2 * zRx2 ) )
4445!               
4446!--    Coagulation sink (1/s)
4447       zCoagS_c = MAX( 1.0E-20_wp, SUM( zK_c2 * paero(:)%numc ) )         
4448       zCoagS_x = MAX( 1.0E-20_wp, SUM( zK_x2 * paero(:)%numc ) ) 
4449!       
4450!--    Parameter m for calculating the coagulation sink onto background
4451!--    particles (Eq. 5&6 in Lehtinen et al. 2007)             
4452       zm_para = LOG( zCoagS_x / zCoagS_c ) / LOG( reglim(1) / zdcrit )
4453!       
4454!--    Parameter gamma for calculating the formation rate J of particles having
4455!--    a diameter zdcrit < d < reglim(1) (Anttila et al. 2010, eq. 5)
4456       zgamma = ( ( ( reglim(1) / zdcrit ) ** ( zm_para + 1.0_wp ) ) - 1.0_wp )&
4457                / ( zm_para + 1.0_wp )     
4458               
4459       IF ( nj3 == 2 )  THEN   ! Coagulation sink
4460!       
4461!--       Formation rate J before iteration (#/m3s)               
4462          zj3 = zjnuc * EXP( MIN( 0.0_wp, -zgamma * zdcrit * zCoagS_c /        &
4463                ( zGRclust * 1.0E-9_wp / ( 60.0_wp ** 2.0_wp ) ) ) )
4464               
4465       ELSEIF ( nj3 == 3 )  THEN  ! Coagulation sink and self-coag.
4466!--       IF polluted air... then the self-coagulation becomes important.
4467!--       Self-coagulation of small particles < 3 nm.
4468!
4469!--       "Effective" coagulation coefficient between freshly-nucleated
4470!--       particles:
4471          zKeff = 5.0E-16_wp   ! cm3/s
4472!         
4473!--       zlambda parameter for "adjusting" the growth rate due to the
4474!--       self-coagulation
4475          zlambda = 6.0_wp 
4476          IF ( reglim(1) >= 10.0E-9_wp )  THEN   ! for particles >10 nm:
4477             zKeff   = 5.0E-17_wp
4478             zlambda = 3.0_wp
4479          ENDIF
4480!         
4481!--       Initial values for coagulation sink and growth rate  (m/s)
4482          zCoagStot = zCoagS_c
4483          zGRtot = zGRclust * 1.0E-9_wp / 60.0_wp ** 2.0_wp 
4484!         
4485!--       Number of clusters/particles at the size range [d1,dx] (#/m3):
4486          zNnuc = zjnuc / zCoagStot !< Initial guess
4487!         
4488!--       Coagulation sink and growth rate due to self-coagulation:
4489          DO  iteration = 1, 5
4490             zCoagStot = zCoagS_c + zKeff * zNnuc * 1.0E-6_wp   ! (1/s) 
4491             zGRtot = zGRclust * 1.0E-9_wp / ( 3600.0_wp ) +  1.5708E-6_wp *   &
4492                      zlambda * zdcrit ** 3.0_wp * ( zNnuc * 1.0E-6_wp ) *     &
4493                      zcv_c * avo * 1.0E-9_wp / 3600.0_wp 
4494             zeta = - zCoagStot / ( ( zm_para + 1.0_wp ) * zGRtot * ( zdcrit **&
4495                      zm_para ) )   ! Eq. 7b (Anttila)
4496             zNnuc =  zNnuc_tayl( zdcrit, reglim(1), zm_para, zjnuc, zeta,     &
4497                      zGRtot )
4498          ENDDO
4499!         
4500!--       Calculate the final values with new zNnuc:   
4501          zCoagStot = zCoagS_c + zKeff * zNnuc * 1.0E-6_wp   ! (1/s)
4502          zGRtot = zGRclust * 1.0E-9_wp / 3600.0_wp + 1.5708E-6_wp *  zlambda  &
4503                   * zdcrit ** 3.0_wp * ( zNnuc * 1.0E-6_wp ) * zcv_c * avo *  &
4504                   1.0E-9_wp / 3600.0_wp !< (m/s)
4505          zj3 = zjnuc * EXP( MIN( 0.0_wp, -zgamma * zdcrit * zCoagStot /       &
4506                zGRtot ) )   ! (Eq. 5a) (#/m3s)
4507               
4508       ENDIF
4509       
4510    ENDIF
4511!-- If J3 very small (< 1 #/cm3), neglect particle formation. In real atmosphere
4512!-- this would mean that clusters form but coagulate to pre-existing particles
4513!-- who gain sulphate. Since CoagS ~ CS (4piD*CS'), we do *not* update H2SO4
4514!-- concentration here but let condensation take care of it.
4515!-- Formation mass rate of molecules (molec/m3s) for 1: H2SO4 and 2: organic
4516!-- vapour
4517    pj3n3(1) = zj3 * n3 * pxsa
4518    pj3n3(2) = zj3 * n3 * pxocnv
4519                                 
4520                         
4521 END SUBROUTINE nucleation
4522
4523!------------------------------------------------------------------------------!
4524! Description:
4525! ------------
4526!> Calculate the nucleation rate and the size of critical clusters assuming
4527!> binary nucleation.
4528!> Parametrisation according to Vehkamaki et al. (2002), J. Geophys. Res.,
4529!> 107(D22), 4622. Called from subroutine nucleation.
4530!------------------------------------------------------------------------------!
4531 SUBROUTINE binnucl( pc_sa, ptemp, prh, pnuc_rate, pn_crit_sa, pn_crit_ocnv,   &
4532                     pd_crit, pk_sa, pk_ocnv )
4533                   
4534    IMPLICIT NONE
4535!       
4536!-- Input and output variables       
4537    REAL(wp), INTENT(in) ::   pc_sa        !< H2SO4 conc. (#/cm3)
4538    REAL(wp), INTENT(in) ::   prh          !< relative humidity [0-1]       
4539    REAL(wp), INTENT(in) ::   ptemp        !< ambient temperature (K)
4540    REAL(wp), INTENT(out) ::  pnuc_rate    !< nucleation rate (#/(m3 s))
4541    REAL(wp), INTENT(out) ::  pn_crit_sa   !< number of H2SO4 molecules in
4542                                           !< cluster (#)
4543    REAL(wp), INTENT(out) ::  pn_crit_ocnv !< number of organic molecules in
4544                                           !< cluster (#)
4545    REAL(wp), INTENT(out) ::  pd_crit      !< diameter of critical cluster (m)
4546    REAL(wp), INTENT(out) ::  pk_sa        !< Lever: if pk_sa = 1, H2SO4 is
4547                                           !< involved in nucleation.
4548    REAL(wp), INTENT(out) ::  pk_ocnv      !< Lever: if pk_ocnv = 1, organic
4549                                           !< compounds are involved in
4550                                           !< nucleation.
4551!-- Local variables
4552    REAL(wp) ::  zx    !< mole fraction of sulphate in critical cluster
4553    REAL(wp) ::  zntot !< number of molecules in critical cluster
4554    REAL(wp) ::  zt    !< temperature
4555    REAL(wp) ::  zpcsa !< sulfuric acid concentration
4556    REAL(wp) ::  zrh   !< relative humidity
4557    REAL(wp) ::  zma   !<
4558    REAL(wp) ::  zmw   !<
4559    REAL(wp) ::  zxmass!<
4560    REAL(wp) ::  za    !<
4561    REAL(wp) ::  zb    !<
4562    REAL(wp) ::  zc    !<
4563    REAL(wp) ::  zroo  !<
4564    REAL(wp) ::  zm1   !<
4565    REAL(wp) ::  zm2   !<
4566    REAL(wp) ::  zv1   !<
4567    REAL(wp) ::  zv2   !<
4568    REAL(wp) ::  zcoll !<
4569   
4570    pnuc_rate = 0.0_wp
4571    pd_crit   = 1.0E-9_wp
4572
4573!             
4574!-- 1) Checking that we are in the validity range of the parameterization 
4575    zt    = MAX( ptemp, 190.15_wp )
4576    zt    = MIN( zt,    300.15_wp )
4577    zpcsa = MAX( pc_sa, 1.0E4_wp  )
4578    zpcsa = MIN( zpcsa, 1.0E11_wp ) 
4579    zrh   = MAX( prh,   0.0001_wp )
4580    zrh   = MIN( zrh,   1.0_wp    )
4581!               
4582!-- 2) Mole fraction of sulphate in a critical cluster (Eq. 11)
4583    zx = 0.7409967177282139_wp                                           &
4584         - 0.002663785665140117_wp * zt                                  &
4585         + 0.002010478847383187_wp * LOG( zrh )                          &
4586         - 0.0001832894131464668_wp* zt * LOG( zrh )                     &
4587         + 0.001574072538464286_wp * LOG( zrh ) ** 2                     &
4588         - 0.00001790589121766952_wp * zt * LOG( zrh ) ** 2              &
4589         + 0.0001844027436573778_wp * LOG( zrh ) ** 3                    &
4590         - 1.503452308794887E-6_wp * zt * LOG( zrh ) ** 3                &
4591         - 0.003499978417957668_wp * LOG( zpcsa )                        &
4592         + 0.0000504021689382576_wp * zt * LOG( zpcsa )
4593!                   
4594!-- 3) Nucleation rate (Eq. 12)
4595    pnuc_rate = 0.1430901615568665_wp                                    &
4596        + 2.219563673425199_wp * zt                                      &
4597        - 0.02739106114964264_wp * zt ** 2                               &
4598        + 0.00007228107239317088_wp * zt ** 3                            &
4599        + 5.91822263375044_wp / zx                                       &
4600        + 0.1174886643003278_wp * LOG( zrh )                             &
4601        + 0.4625315047693772_wp * zt * LOG( zrh )                        &
4602        - 0.01180591129059253_wp * zt ** 2 * LOG( zrh )                  &
4603        + 0.0000404196487152575_wp * zt ** 3 * LOG( zrh )                &
4604        + ( 15.79628615047088_wp * LOG( zrh ) ) / zx                     &
4605        - 0.215553951893509_wp * LOG( zrh ) ** 2                         &
4606        - 0.0810269192332194_wp * zt * LOG( zrh ) ** 2                   &
4607        + 0.001435808434184642_wp * zt ** 2 * LOG( zrh ) ** 2            &
4608        - 4.775796947178588E-6_wp * zt ** 3 * LOG( zrh ) ** 2            &
4609        - (2.912974063702185_wp * LOG( zrh ) ** 2 ) / zx                 &
4610        - 3.588557942822751_wp * LOG( zrh ) ** 3                         &
4611        + 0.04950795302831703_wp * zt * LOG( zrh ) ** 3                  &
4612        - 0.0002138195118737068_wp * zt ** 2 * LOG( zrh ) ** 3           &
4613        + 3.108005107949533E-7_wp * zt ** 3 * LOG( zrh ) ** 3            &
4614        - ( 0.02933332747098296_wp * LOG( zrh ) ** 3 ) / zx              &
4615        + 1.145983818561277_wp * LOG( zpcsa )                            &
4616        - 0.6007956227856778_wp * zt * LOG( zpcsa )                      &
4617        + 0.00864244733283759_wp * zt ** 2 * LOG( zpcsa )                &
4618        - 0.00002289467254710888_wp * zt ** 3 * LOG( zpcsa )             &
4619        - ( 8.44984513869014_wp * LOG( zpcsa ) ) / zx                    &
4620        + 2.158548369286559_wp * LOG( zrh ) * LOG( zpcsa )               &
4621        + 0.0808121412840917_wp * zt * LOG( zrh ) * LOG( zpcsa )         &
4622        - 0.0004073815255395214_wp * zt ** 2 * LOG( zrh ) * LOG( zpcsa ) &
4623        - 4.019572560156515E-7_wp * zt ** 3 * LOG( zrh ) * LOG( zpcsa )  & 
4624        + ( 0.7213255852557236_wp * LOG( zrh ) * LOG( zpcsa ) ) / zx     &
4625        + 1.62409850488771_wp * LOG( zrh ) ** 2 * LOG( zpcsa )           &
4626        - 0.01601062035325362_wp * zt * LOG( zrh ) ** 2 * LOG( zpcsa )   &
4627        + 0.00003771238979714162_wp*zt**2* LOG( zrh )**2 * LOG( zpcsa )  &
4628        + 3.217942606371182E-8_wp * zt**3 * LOG( zrh )**2 * LOG( zpcsa ) &
4629        - (0.01132550810022116_wp * LOG( zrh )**2 * LOG( zpcsa ) ) / zx  &
4630        + 9.71681713056504_wp * LOG( zpcsa ) ** 2                        &
4631        - 0.1150478558347306_wp * zt * LOG( zpcsa ) ** 2                 &
4632        + 0.0001570982486038294_wp * zt ** 2 * LOG( zpcsa ) ** 2         &
4633        + 4.009144680125015E-7_wp * zt ** 3 * LOG( zpcsa ) ** 2          &
4634        + ( 0.7118597859976135_wp * LOG( zpcsa ) ** 2 ) / zx             &
4635        - 1.056105824379897_wp * LOG( zrh ) * LOG( zpcsa ) ** 2          &
4636        + 0.00903377584628419_wp * zt * LOG( zrh ) * LOG( zpcsa )**2     &
4637        - 0.00001984167387090606_wp*zt**2*LOG( zrh )*LOG( zpcsa )**2     &
4638        + 2.460478196482179E-8_wp * zt**3 * LOG( zrh ) * LOG( zpcsa )**2 &
4639        - ( 0.05790872906645181_wp * LOG( zrh ) * LOG( zpcsa )**2 ) / zx &
4640        - 0.1487119673397459_wp * LOG( zpcsa ) ** 3                      &
4641        + 0.002835082097822667_wp * zt * LOG( zpcsa ) ** 3               &
4642        - 9.24618825471694E-6_wp * zt ** 2 * LOG( zpcsa ) ** 3           &
4643        + 5.004267665960894E-9_wp * zt ** 3 * LOG( zpcsa ) ** 3          &
4644        - ( 0.01270805101481648_wp * LOG( zpcsa ) ** 3 ) / zx
4645!           
4646!-- Nucleation rate in #/(cm3 s)
4647    pnuc_rate = EXP( pnuc_rate ) 
4648!       
4649!-- Check the validity of parameterization
4650    IF ( pnuc_rate < 1.0E-7_wp )  THEN
4651       pnuc_rate = 0.0_wp
4652       pd_crit   = 1.0E-9_wp
4653    ENDIF
4654!               
4655!-- 4) Total number of molecules in the critical cluster (Eq. 13)
4656    zntot = - 0.002954125078716302_wp                                    &
4657      - 0.0976834264241286_wp * zt                                       &
4658      + 0.001024847927067835_wp * zt ** 2                                &
4659      - 2.186459697726116E-6_wp * zt ** 3                                &
4660      - 0.1017165718716887_wp / zx                                       &
4661      - 0.002050640345231486_wp * LOG( zrh )                             &
4662      - 0.007585041382707174_wp * zt * LOG( zrh )                        &
4663      + 0.0001926539658089536_wp * zt ** 2 * LOG( zrh )                  &
4664      - 6.70429719683894E-7_wp * zt ** 3 * LOG( zrh )                    &
4665      - ( 0.2557744774673163_wp * LOG( zrh ) ) / zx                      &
4666      + 0.003223076552477191_wp * LOG( zrh ) ** 2                        &
4667      + 0.000852636632240633_wp * zt * LOG( zrh ) ** 2                   &
4668      - 0.00001547571354871789_wp * zt ** 2 * LOG( zrh ) ** 2            &
4669      + 5.666608424980593E-8_wp * zt ** 3 * LOG( zrh ) ** 2              &
4670      + ( 0.03384437400744206_wp * LOG( zrh ) ** 2 ) / zx                &
4671      + 0.04743226764572505_wp * LOG( zrh ) ** 3                         &
4672      - 0.0006251042204583412_wp * zt * LOG( zrh ) ** 3                  &
4673      + 2.650663328519478E-6_wp * zt ** 2 * LOG( zrh ) ** 3              &
4674      - 3.674710848763778E-9_wp * zt ** 3 * LOG( zrh ) ** 3              &
4675      - ( 0.0002672510825259393_wp * LOG( zrh ) ** 3 ) / zx              &
4676      - 0.01252108546759328_wp * LOG( zpcsa )                            &
4677      + 0.005806550506277202_wp * zt * LOG( zpcsa )                      &
4678      - 0.0001016735312443444_wp * zt ** 2 * LOG( zpcsa )                &
4679      + 2.881946187214505E-7_wp * zt ** 3 * LOG( zpcsa )                 &
4680      + ( 0.0942243379396279_wp * LOG( zpcsa ) ) / zx                    &
4681      - 0.0385459592773097_wp * LOG( zrh ) * LOG( zpcsa )                &
4682      - 0.0006723156277391984_wp * zt * LOG( zrh ) * LOG( zpcsa )        &
4683      + 2.602884877659698E-6_wp * zt ** 2 * LOG( zrh ) * LOG( zpcsa )    &
4684      + 1.194163699688297E-8_wp * zt ** 3 * LOG( zrh ) * LOG( zpcsa )    &
4685      - ( 0.00851515345806281_wp * LOG( zrh ) * LOG( zpcsa ) ) / zx      &
4686      - 0.01837488495738111_wp * LOG( zrh ) ** 2 * LOG( zpcsa )          &
4687      + 0.0001720723574407498_wp * zt * LOG( zrh ) ** 2 * LOG( zpcsa )   &
4688      - 3.717657974086814E-7_wp * zt**2 * LOG( zrh )**2 * LOG( zpcsa )   &
4689      - 5.148746022615196E-10_wp * zt**3 * LOG( zrh )**2 * LOG( zpcsa )  &
4690      + ( 0.0002686602132926594_wp * LOG(zrh)**2 * LOG(zpcsa) ) / zx     &
4691      - 0.06199739728812199_wp * LOG( zpcsa ) ** 2                       &
4692      + 0.000906958053583576_wp * zt * LOG( zpcsa ) ** 2                 &
4693      - 9.11727926129757E-7_wp * zt ** 2 * LOG( zpcsa ) ** 2             &
4694      - 5.367963396508457E-9_wp * zt ** 3 * LOG( zpcsa ) ** 2            &
4695      - ( 0.007742343393937707_wp * LOG( zpcsa ) ** 2 ) / zx             &
4696      + 0.0121827103101659_wp * LOG( zrh ) * LOG( zpcsa ) ** 2           &
4697      - 0.0001066499571188091_wp * zt * LOG( zrh ) * LOG( zpcsa ) ** 2   &
4698      + 2.534598655067518E-7_wp * zt**2 * LOG( zrh ) * LOG( zpcsa )**2   &
4699      - 3.635186504599571E-10_wp * zt**3 * LOG( zrh ) * LOG( zpcsa )**2  &
4700      + ( 0.0006100650851863252_wp * LOG( zrh ) * LOG( zpcsa ) **2 )/ zx &
4701      + 0.0003201836700403512_wp * LOG( zpcsa ) ** 3                     &
4702      - 0.0000174761713262546_wp * zt * LOG( zpcsa ) ** 3                &
4703      + 6.065037668052182E-8_wp * zt ** 2 * LOG( zpcsa ) ** 3            &
4704      - 1.421771723004557E-11_wp * zt ** 3 * LOG( zpcsa ) ** 3           &
4705      + ( 0.0001357509859501723_wp * LOG( zpcsa ) ** 3 ) / zx
4706    zntot = EXP( zntot )  ! in #
4707!
4708!-- 5) Size of the critical cluster pd_crit (m) (diameter) (Eq. 14)
4709    pn_crit_sa = zx * zntot
4710    pd_crit    = 2.0E-9_wp * EXP( -1.6524245_wp + 0.42316402_wp  * zx +        &
4711                 0.33466487_wp * LOG( zntot ) )
4712!
4713!-- 6) Organic compounds not involved when binary nucleation is assumed
4714    pn_crit_ocnv = 0.0_wp   ! number of organic molecules
4715    pk_sa        = 1.0_wp   ! if = 1, H2SO4 involved in nucleation
4716    pk_ocnv      = 0.0_wp   ! if = 1, organic compounds involved
4717!               
4718!-- Set nucleation rate to collision rate               
4719    IF ( pn_crit_sa < 4.0_wp ) THEN
4720!       
4721!--    Volumes of the colliding objects
4722       zma    = 96.0_wp   ! molar mass of SO4 in g/mol
4723       zmw    = 18.0_wp   ! molar mass of water in g/mol
4724       zxmass = 1.0_wp    ! mass fraction of H2SO4
4725       za = 0.7681724_wp + zxmass * ( 2.1847140_wp + zxmass * (     &
4726            7.1630022_wp + zxmass * ( -44.31447_wp + zxmass * (     &
4727            88.75606 + zxmass * ( -75.73729_wp + zxmass *           &
4728            23.43228_wp ) ) ) ) )
4729       zb = 1.808225E-3_wp + zxmass * ( -9.294656E-3_wp + zxmass *  &
4730            ( -0.03742148_wp + zxmass * ( 0.2565321_wp + zxmass *   &
4731            ( -0.5362872_wp + zxmass * ( 0.4857736 - zxmass *       &
4732            0.1629592_wp ) ) ) ) )
4733       zc = - 3.478524E-6_wp + zxmass * ( 1.335867E-5_wp + zxmass * &
4734           ( 5.195706E-5_wp + zxmass * ( -3.717636E-4_wp + zxmass * &
4735           ( 7.990811E-4_wp + zxmass * ( -7.458060E-4_wp + zxmass * &
4736             2.58139E-4_wp ) ) ) ) )
4737!             
4738!--    Density for the sulphuric acid solution (Eq. 10 in Vehkamaki)
4739       zroo = za + zt * ( zb + zc * zt )   ! g/cm^3
4740       zroo = zroo * 1.0E+3_wp   ! kg/m^3
4741       zm1  = 0.098_wp   ! molar mass of H2SO4 in kg/mol
4742       zm2  = zm1
4743       zv1  = zm1 / avo / zroo   ! volume
4744       zv2  = zv1
4745!       
4746!--    Collision rate
4747       zcoll =  zpcsa * zpcsa * ( 3.0_wp * pi / 4.0_wp ) ** ( 1.0_wp / 6.0_wp )&
4748                * SQRT( 6.0_wp * argas * zt / zm1 + 6.0_wp * argas * zt / zm2 )&
4749                * ( zv1 ** ( 1.0_wp / 3.0_wp ) + zv2 ** ( 1.0_wp /3.0_wp ) ) **&
4750                2.0_wp * 1.0E+6_wp    ! m3 -> cm3
4751
4752       zcoll      = MIN( zcoll, 1.0E+10_wp )
4753       pnuc_rate  = zcoll   ! (#/(cm3 s))
4754       
4755    ELSE             
4756       pnuc_rate  = MIN( pnuc_rate, 1.0E+10_wp )               
4757    ENDIF             
4758    pnuc_rate = pnuc_rate * 1.0E+6_wp   ! (#/(m3 s))
4759       
4760 END SUBROUTINE binnucl
4761 
4762!------------------------------------------------------------------------------!
4763! Description:
4764! ------------
4765!> Calculate the nucleation rate and the size of critical clusters assuming
4766!> ternary nucleation. Parametrisation according to:
4767!> Napari et al. (2002), J. Chem. Phys., 116, 4221-4227 and
4768!> Napari et al. (2002), J. Geophys. Res., 107(D19), AAC 6-1-ACC 6-6.
4769!> Called from subroutine nucleation.
4770!------------------------------------------------------------------------------!
4771 SUBROUTINE ternucl( pc_sa, pc_nh3, ptemp, prh, pnuc_rate, pn_crit_sa,         &
4772                     pn_crit_ocnv, pd_crit, pk_sa, pk_ocnv )
4773                     
4774    IMPLICIT NONE
4775   
4776!-- Input and output variables
4777    REAL(wp), INTENT(in) ::   pc_nh3  !< ammonia mixing ratio (ppt)       
4778    REAL(wp), INTENT(in) ::   pc_sa   !< H2SO4 conc. (#/cm3)
4779    REAL(wp), INTENT(in) ::   prh     !< relative humidity [0-1]
4780    REAL(wp), INTENT(in) ::   ptemp   !< ambient temperature (K)
4781    REAL(wp), INTENT(out) ::  pd_crit !< diameter of critical
4782                                                  !< cluster (m)
4783    REAL(wp), INTENT(out) ::  pk_ocnv !< Lever: if pk_ocnv = 1,organic compounds
4784                                      !< are involved in nucleation                                                     
4785    REAL(wp), INTENT(out) ::  pk_sa   !< Lever: if pk_sa = 1, H2SO4 is involved
4786                                      !< in nucleation                                                     
4787    REAL(wp), INTENT(out) ::  pn_crit_ocnv !< number of organic molecules in
4788                                           !< cluster (#)
4789    REAL(wp), INTENT(out) ::  pn_crit_sa   !< number of H2SO4 molecules in
4790                                           !< cluster (#)                                                     
4791    REAL(wp), INTENT(out) ::  pnuc_rate    !< nucleation rate (#/(m3 s))
4792!-- Local variables
4793    REAL(wp) ::  zlnj !< logarithm of nucleation rate
4794   
4795!-- 1) Checking that we are in the validity range of the parameterization.
4796!--    Validity of parameterization : DO NOT REMOVE!
4797    IF ( ptemp < 240.0_wp  .OR.  ptemp > 300.0_wp )  THEN
4798       message_string = 'Invalid input value: ptemp'
4799       CALL message( 'salsa_mod: ternucl', 'SA0045', 1, 2, 0, 6, 0 )
4800    ENDIF
4801    IF ( prh < 0.05_wp  .OR.  prh > 0.95_wp )  THEN
4802       message_string = 'Invalid input value: prh'
4803       CALL message( 'salsa_mod: ternucl', 'SA0046', 1, 2, 0, 6, 0 )
4804    ENDIF
4805    IF ( pc_sa < 1.0E+4_wp  .OR.  pc_sa > 1.0E+9_wp )  THEN
4806       message_string = 'Invalid input value: pc_sa'
4807       CALL message( 'salsa_mod: ternucl', 'SA0047', 1, 2, 0, 6, 0 )
4808    ENDIF
4809    IF ( pc_nh3 < 0.1_wp  .OR.  pc_nh3 > 100.0_wp )  THEN
4810       message_string = 'Invalid input value: pc_nh3'
4811       CALL message( 'salsa_mod: ternucl', 'SA0048', 1, 2, 0, 6, 0 )
4812    ENDIF
4813!
4814!-- 2) Nucleation rate (Eq. 7 in Napari et al., 2002: Parameterization of
4815!--    ternary nucleation of sulfuric acid - ammonia - water.
4816    zlnj = - 84.7551114741543_wp                                               &
4817           + 0.3117595133628944_wp * prh                                       &
4818           + 1.640089605712946_wp * prh * ptemp                                &
4819           - 0.003438516933381083_wp * prh * ptemp ** 2.0_wp                   &
4820           - 0.00001097530402419113_wp * prh * ptemp ** 3.0_wp                 &
4821           - 0.3552967070274677_wp / LOG( pc_sa )                              &
4822           - ( 0.06651397829765026_wp * prh ) / LOG( pc_sa )                   &
4823           - ( 33.84493989762471_wp * ptemp ) / LOG( pc_sa )                   &
4824           - ( 7.823815852128623_wp * prh * ptemp ) / LOG( pc_sa)              &
4825           + ( 0.3453602302090915_wp * ptemp ** 2.0_wp ) / LOG( pc_sa )        &
4826           + ( 0.01229375748100015_wp * prh * ptemp ** 2.0_wp ) / LOG( pc_sa ) &
4827           - ( 0.000824007160514956_wp *ptemp ** 3.0_wp ) / LOG( pc_sa )       &
4828           + ( 0.00006185539100670249_wp * prh * ptemp ** 3.0_wp )             &
4829             / LOG( pc_sa )                                                    &
4830           + 3.137345238574998_wp * LOG( pc_sa )                               &
4831           + 3.680240980277051_wp * prh * LOG( pc_sa )                         &
4832           - 0.7728606202085936_wp * ptemp * LOG( pc_sa )                      &
4833           - 0.204098217156962_wp * prh * ptemp * LOG( pc_sa )                 &
4834           + 0.005612037586790018_wp * ptemp ** 2.0_wp * LOG( pc_sa )          &
4835           + 0.001062588391907444_wp * prh * ptemp ** 2.0_wp * LOG( pc_sa )    &
4836           - 9.74575691760229E-6_wp * ptemp ** 3.0_wp * LOG( pc_sa )           &
4837           - 1.265595265137352E-6_wp * prh * ptemp ** 3.0_wp * LOG( pc_sa )    &
4838           + 19.03593713032114_wp * LOG( pc_sa ) ** 2.0_wp                     &
4839           - 0.1709570721236754_wp * ptemp * LOG( pc_sa ) ** 2.0_wp            &
4840           + 0.000479808018162089_wp * ptemp ** 2.0_wp * LOG( pc_sa ) ** 2.0_wp&
4841           - 4.146989369117246E-7_wp * ptemp ** 3.0_wp * LOG( pc_sa ) ** 2.0_wp&
4842           + 1.076046750412183_wp * LOG( pc_nh3 )                              &
4843           + 0.6587399318567337_wp * prh * LOG( pc_nh3 )                       &
4844           + 1.48932164750748_wp * ptemp * LOG( pc_nh3 )                       & 
4845           + 0.1905424394695381_wp * prh * ptemp * LOG( pc_nh3 )               &
4846           - 0.007960522921316015_wp * ptemp ** 2.0_wp * LOG( pc_nh3 )         &
4847           - 0.001657184248661241_wp * prh * ptemp ** 2.0_wp * LOG( pc_nh3 )   &
4848           + 7.612287245047392E-6_wp * ptemp ** 3.0_wp * LOG( pc_nh3 )         &
4849           + 3.417436525881869E-6_wp * prh * ptemp ** 3.0_wp * LOG( pc_nh3 )   &
4850           + ( 0.1655358260404061_wp * LOG( pc_nh3 ) ) / LOG( pc_sa)           &
4851           + ( 0.05301667612522116_wp * prh * LOG( pc_nh3 ) ) / LOG( pc_sa )   &
4852           + ( 3.26622914116752_wp * ptemp * LOG( pc_nh3 ) ) / LOG( pc_sa )    &
4853           - ( 1.988145079742164_wp * prh * ptemp * LOG( pc_nh3 ) )            &
4854             / LOG( pc_sa )                                                    &
4855           - ( 0.04897027401984064_wp * ptemp ** 2.0_wp * LOG( pc_nh3) )       &
4856             / LOG( pc_sa )                                                    &
4857           + ( 0.01578269253599732_wp * prh * ptemp ** 2.0_wp * LOG( pc_nh3 )  &
4858             ) / LOG( pc_sa )                                                  &
4859           + ( 0.0001469672236351303_wp * ptemp ** 3.0_wp * LOG( pc_nh3 ) )    &
4860             / LOG( pc_sa )                                                    &
4861           - ( 0.00002935642836387197_wp * prh * ptemp ** 3.0_wp *LOG( pc_nh3 )&
4862             ) / LOG( pc_sa )                                                  &
4863           + 6.526451177887659_wp * LOG( pc_sa ) * LOG( pc_nh3 )               & 
4864           - 0.2580021816722099_wp * ptemp * LOG( pc_sa ) * LOG( pc_nh3 )      &
4865           + 0.001434563104474292_wp * ptemp ** 2.0_wp * LOG( pc_sa )          &
4866             * LOG( pc_nh3 )                                                   &
4867           -  2.020361939304473E-6_wp * ptemp ** 3.0_wp * LOG( pc_sa )         &
4868             * LOG( pc_nh3 )                                                   &
4869           - 0.160335824596627_wp * LOG( pc_sa ) ** 2.0_wp * LOG( pc_nh3 )     &
4870           +  0.00889880721460806_wp * ptemp * LOG( pc_sa ) ** 2.0_wp          &
4871             * LOG( pc_nh3 )                                                   &
4872           -  0.00005395139051155007_wp * ptemp ** 2.0_wp                      &
4873             * LOG( pc_sa) ** 2.0_wp * LOG( pc_nh3 )                           &
4874           +  8.39521718689596E-8_wp * ptemp ** 3.0_wp * LOG( pc_sa ) ** 2.0_wp&
4875             * LOG( pc_nh3 )                                                   &
4876           + 6.091597586754857_wp * LOG( pc_nh3 ) ** 2.0_wp                    &
4877           + 8.5786763679309_wp * prh * LOG( pc_nh3 ) ** 2.0_wp                &
4878           - 1.253783854872055_wp * ptemp * LOG( pc_nh3 ) ** 2.0_wp            &
4879           - 0.1123577232346848_wp * prh * ptemp * LOG( pc_nh3 ) ** 2.0_wp     &
4880           + 0.00939835595219825_wp * ptemp ** 2.0_wp * LOG( pc_nh3 ) ** 2.0_wp&
4881           + 0.0004726256283031513_wp * prh * ptemp ** 2.0_wp                  &
4882             * LOG( pc_nh3) ** 2.0_wp                                          &
4883           - 0.00001749269360523252_wp * ptemp ** 3.0_wp                       &
4884             * LOG( pc_nh3 ) ** 2.0_wp                                         &
4885           - 6.483647863710339E-7_wp * prh * ptemp ** 3.0_wp                   &
4886             * LOG( pc_nh3 ) ** 2.0_wp                                         &
4887           + ( 0.7284285726576598_wp * LOG( pc_nh3 ) ** 2.0_wp ) / LOG( pc_sa )&
4888           + ( 3.647355600846383_wp * ptemp * LOG( pc_nh3 ) ** 2.0_wp )        &
4889             / LOG( pc_sa )                                                    &
4890           - ( 0.02742195276078021_wp * ptemp ** 2.0_wp                        &
4891             * LOG( pc_nh3) ** 2.0_wp ) / LOG( pc_sa )                         &
4892           + ( 0.00004934777934047135_wp * ptemp ** 3.0_wp                     &
4893             * LOG( pc_nh3 ) ** 2.0_wp ) / LOG( pc_sa )                        &
4894           + 41.30162491567873_wp * LOG( pc_sa ) * LOG( pc_nh3 ) ** 2.0_wp     &
4895           - 0.357520416800604_wp * ptemp * LOG( pc_sa )                       &
4896             * LOG( pc_nh3 ) ** 2.0_wp                                         &
4897           + 0.000904383005178356_wp * ptemp ** 2.0_wp * LOG( pc_sa )          &
4898             * LOG( pc_nh3 ) ** 2.0_wp                                         &
4899           - 5.737876676408978E-7_wp * ptemp ** 3.0_wp * LOG( pc_sa )          &
4900             * LOG( pc_nh3 ) ** 2.0_wp                                         &
4901           - 2.327363918851818_wp * LOG( pc_sa ) ** 2.0_wp                     &
4902             * LOG( pc_nh3 ) ** 2.0_wp                                         &
4903           + 0.02346464261919324_wp * ptemp * LOG( pc_sa ) ** 2.0_wp           &
4904             * LOG( pc_nh3 ) ** 2.0_wp                                         &
4905           - 0.000076518969516405_wp * ptemp ** 2.0_wp                         &
4906             * LOG( pc_sa ) ** 2.0_wp * LOG( pc_nh3 ) ** 2.0_wp                &
4907           + 8.04589834836395E-8_wp * ptemp ** 3.0_wp * LOG( pc_sa ) ** 2.0_wp &
4908             * LOG( pc_nh3 ) ** 2.0_wp                                         &
4909           - 0.02007379204248076_wp * LOG( prh )                               &
4910           - 0.7521152446208771_wp * ptemp * LOG( prh )                        &
4911           + 0.005258130151226247_wp * ptemp ** 2.0_wp * LOG( prh )            &
4912           - 8.98037634284419E-6_wp * ptemp ** 3.0_wp * LOG( prh )             &
4913           + ( 0.05993213079516759_wp * LOG( prh ) ) / LOG( pc_sa )            &
4914           + ( 5.964746463184173_wp * ptemp * LOG( prh ) ) / LOG( pc_sa )      &
4915           - ( 0.03624322255690942_wp * ptemp ** 2.0_wp * LOG( prh ) )         &
4916             / LOG( pc_sa )                                                    &
4917           + ( 0.00004933369382462509_wp * ptemp ** 3.0_wp * LOG( prh ) )      &
4918             / LOG( pc_sa )                                                    &
4919           - 0.7327310805365114_wp * LOG( pc_nh3 ) * LOG( prh )                &
4920           - 0.01841792282958795_wp * ptemp * LOG( pc_nh3 ) * LOG( prh )       &
4921           + 0.0001471855981005184_wp * ptemp ** 2.0_wp * LOG( pc_nh3 )        &
4922             * LOG( prh )                                                      &
4923           - 2.377113195631848E-7_wp * ptemp ** 3.0_wp * LOG( pc_nh3 )         &
4924             * LOG( prh )
4925    pnuc_rate = EXP( zlnj )   ! (#/(cm3 s))
4926!   
4927!-- Check validity of parametrization             
4928    IF ( pnuc_rate < 1.0E-5_wp )  THEN
4929       pnuc_rate = 0.0_wp
4930       pd_crit   = 1.0E-9_wp
4931    ELSEIF ( pnuc_rate > 1.0E6_wp )  THEN
4932       message_string = 'Invalid output value: nucleation rate > 10^6 1/cm3s'
4933       CALL message( 'salsa_mod: ternucl', 'SA0049', 1, 2, 0, 6, 0 )
4934    ENDIF
4935    pnuc_rate = pnuc_rate * 1.0E6_wp   ! (#/(m3 s))
4936!             
4937!-- 3) Number of H2SO4 molecules in a critical cluster (Eq. 9)
4938    pn_crit_sa = 38.16448247950508_wp + 0.7741058259731187_wp * zlnj +         &
4939                 0.002988789927230632_wp * zlnj ** 2.0_wp -                    &
4940                 0.3576046920535017_wp * ptemp -                               &
4941                 0.003663583011953248_wp * zlnj * ptemp +                      &
4942                 0.000855300153372776_wp * ptemp ** 2.0_wp
4943!-- Kinetic limit: at least 2 H2SO4 molecules in a cluster                                 
4944    pn_crit_sa = MAX( pn_crit_sa, 2.0E0_wp ) 
4945!             
4946!-- 4) Size of the critical cluster in nm (Eq. 12)
4947    pd_crit = 0.1410271086638381_wp - 0.001226253898894878_wp * zlnj -         &
4948              7.822111731550752E-6_wp * zlnj ** 2.0_wp -                       &
4949              0.001567273351921166_wp * ptemp -                                &
4950              0.00003075996088273962_wp * zlnj * ptemp +                       &
4951              0.00001083754117202233_wp * ptemp ** 2.0_wp 
4952    pd_crit = pd_crit * 2.0E-9_wp   ! Diameter in m
4953!
4954!-- 5) Organic compounds not involved when ternary nucleation assumed
4955    pn_crit_ocnv = 0.0_wp 
4956    pk_sa   = 1.0_wp
4957    pk_ocnv = 0.0_wp
4958   
4959 END SUBROUTINE ternucl
4960 
4961!------------------------------------------------------------------------------!
4962! Description:
4963! ------------
4964!> Calculate the nucleation rate and the size of critical clusters assuming
4965!> kinetic nucleation. Each sulphuric acid molecule forms an (NH4)HSO4 molecule
4966!> in the atmosphere and two colliding (NH4)HSO4 molecules form a stable
4967!> cluster. See Sihto et al. (2006), Atmos. Chem. Phys., 6(12), 4079-4091.
4968!>
4969!> Below the following assumption have been made:
4970!>  nucrate = coagcoeff*zpcsa**2
4971!>  coagcoeff = 8*sqrt(3*boltz*ptemp*r_abs/dens_abs)
4972!>  r_abs = 0.315d-9 radius of bisulphate molecule [m]
4973!>  dens_abs = 1465  density of - " - [kg/m3]
4974!------------------------------------------------------------------------------!
4975 SUBROUTINE kinnucl( pc_sa, pnuc_rate, pd_crit, pn_crit_sa, pn_crit_ocnv,      &
4976                     pk_sa, pk_ocnv ) 
4977                     
4978    IMPLICIT NONE
4979   
4980!-- Input and output variables
4981    REAL(wp), INTENT(in) ::  pc_sa     !< H2SO4 conc. (#/m3)
4982    REAL(wp), INTENT(out) ::  pd_crit  !< critical diameter of clusters (m)
4983    REAL(wp), INTENT(out) ::  pk_ocnv  !< Lever: if pk_ocnv = 1, organic
4984                                       !< compounds are involved in nucleation
4985    REAL(wp), INTENT(out) ::  pk_sa    !< Lever: if pk_sa = 1, H2SO4 is involved
4986                                       !< in nucleation
4987    REAL(wp), INTENT(out) ::  pn_crit_ocnv !< number of organic molecules in
4988                                           !< cluster (#)
4989    REAL(wp), INTENT(out) ::  pn_crit_sa   !< number of H2SO4 molecules in
4990                                           !< cluster (#)
4991    REAL(wp), INTENT(out) ::  pnuc_rate    !< nucl. rate (#/(m3 s))
4992   
4993!-- Nucleation rate (#/(m3 s))
4994    pnuc_rate = 5.0E-13_wp * pc_sa ** 2.0_wp * 1.0E+6_wp
4995!-- Organic compounds not involved when kinetic nucleation is assumed.
4996    pn_crit_sa   = 2.0_wp
4997    pn_crit_ocnv = 0.0_wp 
4998    pk_sa        = 1.0_wp
4999    pk_ocnv      = 0.0_wp             
5000    pd_crit      = 7.9375E-10_wp   ! (m)
5001   
5002 END SUBROUTINE kinnucl
5003!------------------------------------------------------------------------------!
5004! Description:
5005! ------------
5006!> Calculate the nucleation rate and the size of critical clusters assuming
5007!> activation type nucleation.
5008!> See Riipinen et al. (2007), Atmos. Chem. Phys., 7(8), 1899-1914.
5009!------------------------------------------------------------------------------!
5010 SUBROUTINE actnucl( psa_conc, pnuc_rate, pd_crit, pn_crit_sa, pn_crit_ocnv,   &
5011                     pk_sa, pk_ocnv, activ ) 
5012
5013    IMPLICIT NONE
5014   
5015!-- Input and output variables
5016    REAL(wp), INTENT(in) ::  psa_conc !< H2SO4 conc. (#/m3)
5017    REAL(wp), INTENT(in) ::  activ    !<
5018    REAL(wp), INTENT(out) ::  pd_crit !< critical diameter of clusters (m)
5019    REAL(wp), INTENT(out) ::  pk_ocnv !< Lever: if pk_ocnv = 1, organic
5020                                      !< compounds are involved in nucleation
5021    REAL(wp), INTENT(out) ::  pk_sa   !< Lever: if pk_sa = 1, H2SO4 is involved
5022                                      !< in nucleation
5023    REAL(wp), INTENT(out) ::  pn_crit_ocnv !< number of organic molecules in
5024                                           !< cluster (#)
5025    REAL(wp), INTENT(out) ::  pn_crit_sa   !< number of H2SO4 molecules in
5026                                           !< cluster (#)
5027    REAL(wp), INTENT(out) ::  pnuc_rate    !< nucl. rate (#/(m3 s))
5028   
5029!-- act_coeff 1e-7 by default
5030    pnuc_rate = activ * psa_conc   ! (#/(m3 s))
5031!-- Organic compounds not involved when kinetic nucleation is assumed.
5032    pn_crit_sa   = 2.0_wp
5033    pn_crit_ocnv = 0.0_wp 
5034    pk_sa        = 1.0_wp
5035    pk_ocnv      = 0.0_wp
5036    pd_crit      = 7.9375E-10_wp   ! (m)
5037 END SUBROUTINE actnucl
5038!------------------------------------------------------------------------------!
5039! Description:
5040! ------------
5041!> Conciders only the organic matter in nucleation. Paasonen et al. (2010)
5042!> determined particle formation rates for 2 nm particles, J2, from different
5043!> kind of combinations of sulphuric acid and organic matter concentration.
5044!> See Paasonen et al. (2010), Atmos. Chem. Phys., 10, 11223-11242.
5045!------------------------------------------------------------------------------!
5046 SUBROUTINE orgnucl( pc_org, pnuc_rate, pd_crit, pn_crit_sa, pn_crit_ocnv,     &
5047                     pk_sa, pk_ocnv )
5048
5049    IMPLICIT NONE
5050   
5051!-- Input and output variables
5052    REAL(wp), INTENT(in) ::  pc_org   !< organic vapour concentration (#/m3)
5053    REAL(wp), INTENT(out) ::  pd_crit !< critical diameter of clusters (m)
5054    REAL(wp), INTENT(out) ::  pk_ocnv !< Lever: if pk_ocnv = 1, organic
5055                                      !< compounds are involved in nucleation
5056    REAL(wp), INTENT(out) ::  pk_sa !< Lever: if pk_sa = 1, H2SO4 is involved
5057                                    !< in nucleation
5058    REAL(wp), INTENT(out) ::  pn_crit_ocnv !< number of organic molecules in
5059                                           !< cluster (#)
5060    REAL(wp), INTENT(out) ::  pn_crit_sa   !< number of H2SO4 molecules in
5061                                           !< cluster (#)
5062    REAL(wp), INTENT(out) ::  pnuc_rate    !< nucl. rate (#/(m3 s))
5063!-- Local variables
5064    REAL(wp) ::  Aorg = 1.3E-7_wp !< (1/s) (Paasonen et al. Table 4: median)
5065   
5066!-- Homomolecular nuleation - which one?         
5067    pnuc_rate = Aorg * pc_org 
5068!-- H2SO4 not involved when pure organic nucleation is assumed.
5069    pn_crit_sa   = 0.0_wp
5070    pn_crit_ocnv = 1.0_wp 
5071    pk_sa        = 0.0_wp
5072    pk_ocnv      = 1.0_wp
5073    pd_crit      = 1.5E-9_wp   ! (m)
5074   
5075 END SUBROUTINE orgnucl
5076!------------------------------------------------------------------------------!
5077! Description:
5078! ------------
5079!> Conciders both the organic vapor and H2SO4 in nucleation - activation type
5080!> of nucleation.
5081!> See Paasonen et al. (2010), Atmos. Chem. Phys., 10, 11223-11242.
5082!------------------------------------------------------------------------------!
5083 SUBROUTINE sumnucl( pc_sa, pc_org, pnuc_rate, pd_crit, pn_crit_sa,            &
5084                     pn_crit_ocnv, pk_sa, pk_ocnv )
5085
5086    IMPLICIT NONE
5087   
5088!-- Input and output variables
5089    REAL(wp), INTENT(in) ::  pc_org   !< organic vapour concentration (#/m3)
5090    REAL(wp), INTENT(in) ::  pc_sa    !< H2SO4 conc. (#/m3)
5091    REAL(wp), INTENT(out) ::  pd_crit !< critical diameter of clusters (m)
5092    REAL(wp), INTENT(out) ::  pk_ocnv !< Lever: if pk_ocnv = 1, organic
5093                                      !< compounds are involved in nucleation
5094    REAL(wp), INTENT(out) ::  pk_sa   !< Lever: if pk_sa = 1, H2SO4 is involved
5095                                      !< in nucleation
5096    REAL(wp), INTENT(out) ::  pn_crit_ocnv !< number of organic molecules in
5097                                           !< cluster (#)
5098    REAL(wp), INTENT(out) ::  pn_crit_sa   !< number of H2SO4 molecules in
5099                                           !< cluster (#)
5100    REAL(wp), INTENT(out) ::  pnuc_rate    !< nucl. rate (#/(m3 s))
5101!-- Local variables
5102    REAL(wp) ::  As1 = 6.1E-7_wp  !< (1/s)
5103    REAL(wp) ::  As2 = 0.39E-7_wp !< (1/s) (Paasonen et al. Table 3.)
5104   
5105!-- Nucleation rate  (#/m3/s)
5106    pnuc_rate = As1 * pc_sa + As2 * pc_org 
5107!-- Both Organic compounds and H2SO4 are involved when SUMnucleation is assumed.
5108    pn_crit_sa   = 1.0_wp
5109    pn_crit_ocnv = 1.0_wp 
5110    pk_sa        = 1.0_wp
5111    pk_ocnv      = 1.0_wp           
5112    pd_crit      = 1.5E-9_wp   ! (m)
5113   
5114 END SUBROUTINE sumnucl
5115!------------------------------------------------------------------------------!
5116! Description:
5117! ------------
5118!> Conciders both the organic vapor and H2SO4 in nucleation - heteromolecular
5119!> nucleation.
5120!> See Paasonen et al. (2010), Atmos. Chem. Phys., 10, 11223-11242.
5121!------------------------------------------------------------------------------!
5122 SUBROUTINE hetnucl( pc_sa, pc_org, pnuc_rate, pd_crit, pn_crit_sa,            &
5123                     pn_crit_ocnv, pk_sa, pk_ocnv )
5124
5125    IMPLICIT NONE
5126   
5127!-- Input and output variables
5128    REAL(wp), INTENT(in) ::  pc_org   !< organic vapour concentration (#/m3)
5129    REAL(wp), INTENT(in) ::  pc_sa    !< H2SO4 conc. (#/m3)
5130    REAL(wp), INTENT(out) ::  pd_crit !< critical diameter of clusters (m)
5131    REAL(wp), INTENT(out) ::  pk_ocnv !< Lever: if pk_ocnv = 1, organic
5132                                      !< compounds are involved in nucleation
5133    REAL(wp), INTENT(out) ::  pk_sa   !< Lever: if pk_sa = 1, H2SO4 is involved
5134                                      !< in nucleation
5135    REAL(wp), INTENT(out) ::  pn_crit_ocnv !< number of organic molecules in
5136                                           !< cluster (#)
5137    REAL(wp), INTENT(out) ::  pn_crit_sa   !< number of H2SO4 molecules in
5138                                           !< cluster (#)
5139    REAL(wp), INTENT(out) ::  pnuc_rate    !< nucl. rate (#/(m3 s))
5140!-- Local variables
5141    REAL(wp) ::  zKhet = 4.1E-14_wp !< (cm3/s) (Paasonen et al. Table 4: median)
5142   
5143!-- Nucleation rate (#/m3/s)
5144    pnuc_rate = zKhet * pc_sa * pc_org * 1.0E6_wp 
5145!-- Both Organic compounds and H2SO4 are involved when heteromolecular
5146!-- nucleation is assumed.
5147    pn_crit_sa   = 1.0_wp
5148    pn_crit_ocnv = 1.0_wp 
5149    pk_sa        = 1.0_wp
5150    pk_ocnv      = 1.0_wp 
5151    pd_crit      = 1.5E-9_wp   ! (m)
5152   
5153 END SUBROUTINE hetnucl
5154!------------------------------------------------------------------------------!
5155! Description:
5156! ------------
5157!> Takes into account the homomolecular nucleation of sulphuric acid H2SO4 with
5158!> both of the available vapours.
5159!> See Paasonen et al. (2010), Atmos. Chem. Phys., 10, 11223-11242.
5160!------------------------------------------------------------------------------!
5161 SUBROUTINE SAnucl( pc_sa, pc_org, pnuc_rate, pd_crit, pn_crit_sa,             &
5162                    pn_crit_ocnv, pk_sa, pk_ocnv )
5163
5164    IMPLICIT NONE
5165   
5166!-- Input and output variables
5167    REAL(wp), INTENT(in) ::  pc_org   !< organic vapour concentration (#/m3)
5168    REAL(wp), INTENT(in) ::  pc_sa    !< H2SO4 conc. (#/m3)
5169    REAL(wp), INTENT(out) ::  pd_crit !< critical diameter of clusters (m)
5170    REAL(wp), INTENT(out) ::  pk_ocnv !< Lever: if pk_ocnv = 1, organic
5171                                      !< compounds are involved in nucleation
5172    REAL(wp), INTENT(out) ::  pk_sa   !< Lever: if pk_sa = 1, H2SO4 is involved
5173                                      !< in nucleation
5174    REAL(wp), INTENT(out) ::  pn_crit_ocnv !< number of organic molecules in
5175                                           !< cluster (#)
5176    REAL(wp), INTENT(out) ::  pn_crit_sa   !< number of H2SO4 molecules in
5177                                           !< cluster (#)
5178    REAL(wp), INTENT(out) ::  pnuc_rate    !< nucleation rate (#/(m3 s))
5179!-- Local variables
5180    REAL(wp) ::  zKsa1 = 1.1E-14_wp !< (cm3/s)
5181    REAL(wp) ::  zKsa2 = 3.2E-14_wp  !< (cm3/s) (Paasonen et al. Table 3.)
5182   
5183!-- Nucleation rate (#/m3/s)
5184    pnuc_rate = ( zKsa1 * pc_sa ** 2.0_wp + zKsa2 * pc_sa * pc_org ) * 1.0E+6_wp 
5185!-- Both Organic compounds and H2SO4 are involved when SAnucleation is assumed.
5186    pn_crit_sa   = 3.0_wp
5187    pn_crit_ocnv = 1.0_wp 
5188    pk_sa        = 1.0_wp
5189    pk_ocnv      = 1.0_wp
5190    pd_crit      = 1.5E-9_wp   ! (m)
5191   
5192 END SUBROUTINE SAnucl
5193!------------------------------------------------------------------------------!
5194! Description:
5195! ------------
5196!> Takes into account the homomolecular nucleation of both sulphuric acid and
5197!> Lorganic with heteromolecular nucleation.
5198!> See Paasonen et al. (2010), Atmos. Chem. Phys., 10, 11223-11242.
5199!------------------------------------------------------------------------------!
5200 SUBROUTINE SAORGnucl( pc_sa, pc_org, pnuc_rate, pd_crit, pn_crit_sa,          &
5201                       pn_crit_ocnv, pk_sa, pk_ocnv )
5202
5203    IMPLICIT NONE
5204   
5205!-- Input and output variables
5206    REAL(wp), INTENT(in) ::  pc_org   !< organic vapour concentration (#/m3)
5207    REAL(wp), INTENT(in) ::  pc_sa    !< H2SO4 conc. (#/m3)
5208    REAL(wp), INTENT(out) ::  pd_crit !< critical diameter of clusters (m)
5209    REAL(wp), INTENT(out) ::  pk_ocnv !< Lever: if pk_ocnv = 1, organic
5210                                      !< compounds are involved in nucleation
5211    REAL(wp), INTENT(out) ::  pk_sa   !< Lever: if pk_sa = 1, H2SO4 is involved
5212                                      !< in nucleation
5213    REAL(wp), INTENT(out) ::  pn_crit_ocnv !< number of organic molecules in
5214                                           !< cluster (#)
5215    REAL(wp), INTENT(out) ::  pn_crit_sa   !< number of H2SO4 molecules in
5216                                           !< cluster (#)
5217    REAL(wp), INTENT(out) ::  pnuc_rate    !< nucl. rate (#/(m3 s))
5218!-- Local variables
5219    REAL(wp) ::  zKs1 = 1.4E-14_wp   !< (cm3/s])
5220    REAL(wp) ::  zKs2 = 2.6E-14_wp   !< (cm3/s])
5221    REAL(wp) ::  zKs3 = 0.037E-14_wp !< (cm3/s]) (Paasonen et al. Table 3.)
5222   
5223!-- Nucleation rate (#/m3/s)         
5224    pnuc_rate = ( zKs1 * pc_sa **2 + zKs2 * pc_sa * pc_org + zKs3 *            &
5225                  pc_org ** 2.0_wp ) * 1.0E+6_wp
5226!-- Organic compounds not involved when kinetic nucleation is assumed.
5227    pn_crit_sa   = 3.0_wp
5228    pn_crit_ocnv = 3.0_wp 
5229    pk_sa        = 1.0_wp
5230    pk_ocnv      = 1.0_wp
5231    pd_crit      = 1.5E-9_wp   ! (m)
5232 
5233 END SUBROUTINE SAORGnucl
5234 
5235!------------------------------------------------------------------------------!
5236! Description:
5237! ------------
5238!> Function zNnuc_tayl is connected to the calculation of self-coagualtion of
5239!> small particles. It calculates number of the particles in the size range
5240!> [zdcrit,dx] using Taylor-expansion (please note that the expansion is not
5241!> valid for certain rational numbers, e.g. -4/3 and -3/2)
5242!------------------------------------------------------------------------------!
5243 FUNCTION zNnuc_tayl( d1, dx, zm_para, zjnuc_t, zeta, zGRtot ) 
5244    IMPLICIT NONE
5245 
5246    INTEGER(iwp) ::  i
5247    REAL(wp) ::  d1
5248    REAL(wp) ::  dx
5249    REAL(wp) ::  zjnuc_t
5250    REAL(wp) ::  zeta
5251    REAL(wp) ::  term1
5252    REAL(wp) ::  term2
5253    REAL(wp) ::  term3
5254    REAL(wp) ::  term4
5255    REAL(wp) ::  term5
5256    REAL(wp) ::  zNnuc_tayl
5257    REAL(wp) ::  zGRtot
5258    REAL(wp) ::  zm_para
5259
5260    zNnuc_tayl = 0.0_wp
5261
5262    DO  i = 0, 29
5263       IF ( i == 0  .OR.  i == 1 )  THEN
5264          term1 = 1.0_wp
5265       ELSE
5266          term1 = term1 * REAL( i, SELECTED_REAL_KIND(12,307) )
5267       END IF
5268       term2 = ( REAL( i, SELECTED_REAL_KIND(12,307) ) * ( zm_para + 1.0_wp    &
5269               ) + 1.0_wp ) * term1
5270       term3 = zeta ** i
5271       term4 = term3 / term2
5272       term5 = REAL( i, SELECTED_REAL_KIND(12,307) ) * ( zm_para + 1.0_wp )    &
5273               + 1.0_wp
5274       zNnuc_tayl = zNnuc_tayl + term4 * ( dx ** term5 - d1 ** term5 ) 
5275    ENDDO
5276    zNnuc_tayl = zNnuc_tayl * zjnuc_t * EXP( -zeta *                           &
5277                   ( d1 ** ( zm_para + 1 ) ) ) / zGRtot
5278                 
5279 END FUNCTION zNnuc_tayl
5280 
5281!------------------------------------------------------------------------------!
5282! Description:
5283! ------------
5284!> Calculates the condensation of water vapour on aerosol particles. Follows the
5285!> analytical predictor method by Jacobson (2005).
5286!> For equations, see Jacobson (2005), Fundamentals of atmospheric modelling
5287!> (2nd edition).
5288!------------------------------------------------------------------------------!
5289 SUBROUTINE gpparth2o( paero, ptemp, ppres, pcs, pcw, ptstep )
5290       
5291    IMPLICIT NONE
5292!
5293!-- Input and output variables 
5294    REAL(wp), INTENT(in) ::  ppres  !< Air pressure (Pa)
5295    REAL(wp), INTENT(in) ::  pcs    !< Water vapour saturation
5296                                             !< concentration (kg/m3)
5297    REAL(wp), INTENT(in) ::  ptemp  !< Ambient temperature (K) 
5298    REAL(wp), INTENT(in) ::  ptstep !< timestep (s)
5299    REAL(wp), INTENT(inout) ::  pcw !< Water vapour concentration
5300                                                !< (kg/m3)
5301    TYPE(t_section), INTENT(inout) ::  paero(nbins) !< Aerosol properties
5302!-- Local variables
5303    INTEGER(iwp) ::  b !< loop index
5304    INTEGER(iwp) ::  nstr
5305    REAL(wp) ::  adt     !< internal timestep in this subroutine
5306    REAL(wp) ::  adtc(nbins) 
5307    REAL(wp) ::  rhoair     
5308    REAL(wp) ::  ttot       
5309    REAL(wp) ::  zact    !< Water activity
5310    REAL(wp) ::  zaelwc1 !< Current aerosol water content
5311    REAL(wp) ::  zaelwc2 !< New aerosol water content after
5312                                     !< equilibrium calculation     
5313    REAL(wp) ::  zbeta   !< Transitional correction factor
5314    REAL(wp) ::  zcwc    !< Current water vapour mole concentration
5315    REAL(wp) ::  zcwcae(nbins) !< Current water mole concentrations
5316                               !< in aerosols
5317    REAL(wp) ::  zcwint  !< Current and new water vapour mole concentrations
5318    REAL(wp) ::  zcwintae(nbins) !< Current and new water mole concentrations
5319                                 !< in aerosols
5320    REAL(wp) ::  zcwn    !< New water vapour mole concentration
5321    REAL(wp) ::  zcwnae(nbins) !< New water mole concentration in aerosols
5322    REAL(wp) ::  zcwsurfae(nbins) !< Surface mole concentration
5323    REAL(wp) ::  zcwtot  !< Total water mole concentration
5324    REAL(wp) ::  zdfh2o
5325    REAL(wp) ::  zhlp1
5326    REAL(wp) ::  zhlp2
5327    REAL(wp) ::  zhlp3       
5328    REAL(wp) ::  zka(nbins)     !< Activity coefficient       
5329    REAL(wp) ::  zkelvin(nbins) !< Kelvin effect
5330    REAL(wp) ::  zknud
5331    REAL(wp) ::  zmfph2o        !< mean free path of H2O gas molecule
5332    REAL(wp) ::  zmtae(nbins)   !< Mass transfer coefficients
5333    REAL(wp) ::  zrh            !< Relative humidity [0-1]     
5334    REAL(wp) ::  zthcond       
5335    REAL(wp) ::  zwsatae(nbins) !< Water saturation ratio above aerosols
5336!
5337!-- Relative humidity [0-1]
5338    zrh = pcw / pcs
5339!-- Calculate the condensation only for 2a/2b aerosol bins
5340    nstr = in2a
5341!-- Save the current aerosol water content, 8 in paero is H2O
5342    zaelwc1 = SUM( paero(in1a:fn2b)%volc(8) ) * arhoh2o
5343!
5344!-- Equilibration:
5345    IF ( advect_particle_water )  THEN
5346       IF ( zrh < 0.98_wp  .OR.  .NOT. lscndh2oae )  THEN
5347          CALL equilibration( zrh, ptemp, paero, .TRUE. )
5348       ELSE
5349          CALL equilibration( zrh, ptemp, paero, .FALSE. )
5350       ENDIF
5351    ENDIF
5352!                                       
5353!-- The new aerosol water content after equilibrium calculation
5354    zaelwc2 = SUM( paero(in1a:fn2b)%volc(8) ) * arhoh2o
5355!-- New water vapour mixing ratio (kg/m3)
5356    pcw = pcw - ( zaelwc2 - zaelwc1 ) * ppres * amdair / ( argas * ptemp )
5357!                 
5358!-- Initialise variables
5359    adtc(:)  = 0.0_wp
5360    zcwc     = 0.0_wp
5361    zcwcae   = 0.0_wp       
5362    zcwint   = 0.0_wp
5363    zcwintae = 0.0_wp       
5364    zcwn     = 0.0_wp
5365    zcwnae   = 0.0_wp
5366    zhlp1    = 0.0_wp
5367    zwsatae  = 0.0_wp   
5368!         
5369!-- Air:
5370!-- Density (kg/m3)
5371    rhoair = amdair * ppres / ( argas * ptemp )
5372!-- Thermal conductivity of air                       
5373    zthcond = 0.023807_wp + 7.1128E-5_wp * ( ptemp - 273.16_wp )
5374!             
5375!-- Water vapour:
5376!
5377!-- Molecular diffusion coefficient (cm2/s) (eq.16.17)
5378    zdfh2o = ( 5.0_wp / ( 16.0_wp * avo * rhoair * 1.0E-3_wp *                 &
5379             ( 3.11E-8_wp ) ** 2.0_wp ) ) * SQRT( argas * 1.0E+7_wp * ptemp *  &
5380             amdair * 1.0E+3_wp * ( amh2o + amdair ) * 1.0E+3_wp / ( 2.0_wp *  &
5381             pi * amh2o * 1.0E+3_wp ) )
5382    zdfh2o = zdfh2o * 1.0E-4   ! Unit change to m^2/s
5383!   
5384!-- Mean free path (eq. 15.25 & 16.29)
5385    zmfph2o = 3.0_wp * zdfh2o * SQRT( pi * amh2o / ( 8.0_wp * argas * ptemp ) ) 
5386    zka = 1.0_wp   ! Assume activity coefficients as 1 for now.
5387!   
5388!-- Kelvin effect (eq. 16.33)
5389    zkelvin = 1.0_wp                   
5390    zkelvin(1:nbins) = EXP( 4.0_wp * surfw0 * amh2o / ( argas * ptemp *        &
5391                            arhoh2o * paero(1:nbins)%dwet) )
5392!                           
5393! --Aerosols:
5394    zmtae(:)     = 0.0_wp   ! mass transfer coefficient
5395    zcwsurfae(:) = 0.0_wp   ! surface mole concentrations
5396    DO  b = 1, nbins
5397       IF ( paero(b)%numc > nclim  .AND.  zrh > 0.98_wp )  THEN
5398!       
5399!--       Water activity
5400          zact = acth2o( paero(b) )
5401!         
5402!--       Saturation mole concentration over flat surface. Limit the super-
5403!--       saturation to max 1.01 for the mass transfer. Experimental!         
5404          zcwsurfae(b) = MAX( pcs, pcw / 1.01_wp ) * rhoair / amh2o
5405!         
5406!--       Equilibrium saturation ratio
5407          zwsatae(b) = zact * zkelvin(b)
5408!         
5409!--       Knudsen number (eq. 16.20)
5410          zknud = 2.0_wp * zmfph2o / paero(b)%dwet
5411!         
5412!--       Transitional correction factor (Fuks & Sutugin, 1971)
5413          zbeta = ( zknud + 1.0_wp ) / ( 0.377_wp * zknud + 1.0_wp + 4.0_wp /  &
5414                  ( 3.0_wp * massacc(b) ) * ( zknud + zknud ** 2.0_wp ) )
5415!                 
5416!--       Mass transfer of H2O: Eq. 16.64 but here D^eff =  zdfh2o * zbeta
5417          zhlp1 = paero(b)%numc * 2.0_wp * pi * paero(b)%dwet * zdfh2o *    &
5418                  zbeta 
5419!--       1st term on the left side of the denominator in eq. 16.55
5420          zhlp2 = amh2o * zdfh2o * alv * zwsatae(b) * zcwsurfae(b) /         &
5421                  ( zthcond * ptemp )
5422!--       2nd term on the left side of the denominator in eq. 16.55                           
5423          zhlp3 = ( (alv * amh2o ) / ( argas * ptemp ) ) - 1.0_wp
5424!--       Full eq. 16.64: Mass transfer coefficient (1/s)
5425          zmtae(b) = zhlp1 / ( zhlp2 * zhlp3 + 1.0_wp )
5426       ENDIF
5427    ENDDO
5428!
5429!-- Current mole concentrations of water
5430    zcwc = pcw * rhoair / amh2o   ! as vapour
5431    zcwcae(1:nbins) = paero(1:nbins)%volc(8) * arhoh2o / amh2o   ! in aerosols
5432    zcwtot = zcwc + SUM( zcwcae )   ! total water concentration
5433    ttot = 0.0_wp
5434    adtc = 0.0_wp
5435    zcwintae = zcwcae   
5436!             
5437!-- Substepping loop
5438    zcwint = 0.0_wp
5439    DO  WHILE ( ttot < ptstep )
5440       adt = 2.0E-2_wp   ! internal timestep
5441!       
5442!--    New vapour concentration: (eq. 16.71)
5443       zhlp1 = zcwc + adt * ( SUM( zmtae(nstr:nbins) * zwsatae(nstr:nbins) *   &
5444                                   zcwsurfae(nstr:nbins) ) )   ! numerator
5445       zhlp2 = 1.0_wp + adt * ( SUM( zmtae(nstr:nbins) ) )   ! denomin.
5446       zcwint = zhlp1 / zhlp2   ! new vapour concentration
5447       zcwint = MIN( zcwint, zcwtot )
5448       IF ( ANY( paero(:)%numc > nclim )  .AND. zrh > 0.98_wp )  THEN
5449          DO  b = nstr, nbins
5450             zcwintae(b) = zcwcae(b) + MIN( MAX( adt * zmtae(b) *           &
5451                          ( zcwint - zwsatae(b) * zcwsurfae(b) ),            &
5452                          -0.02_wp * zcwcae(b) ), 0.05_wp * zcwcae(b) )
5453             zwsatae(b) = acth2o( paero(b), zcwintae(b) ) * zkelvin(b)
5454          ENDDO
5455       ENDIF
5456       zcwintae(nstr:nbins) = MAX( zcwintae(nstr:nbins), 0.0_wp )
5457!       
5458!--    Update vapour concentration for consistency
5459       zcwint = zcwtot - SUM( zcwintae(1:nbins) )
5460!--    Update "old" values for next cycle
5461       zcwcae = zcwintae
5462
5463       ttot = ttot + adt
5464    ENDDO   ! ADT
5465    zcwn   = zcwint
5466    zcwnae = zcwintae
5467    pcw    = zcwn * amh2o / rhoair
5468    paero(1:nbins)%volc(8) = MAX( 0.0_wp, zcwnae(1:nbins) * amh2o / arhoh2o )
5469   
5470 END SUBROUTINE gpparth2o
5471
5472!------------------------------------------------------------------------------!
5473! Description:
5474! ------------
5475!> Calculates the activity coefficient of liquid water
5476!------------------------------------------------------------------------------!   
5477 REAL(wp) FUNCTION acth2o( ppart, pcw )
5478               
5479    IMPLICIT NONE
5480
5481    TYPE(t_section), INTENT(in) ::  ppart !< Aerosol properties of a bin
5482    REAL(wp), INTENT(in), OPTIONAL ::  pcw !< molar concentration of water
5483                                           !< (mol/m3)
5484
5485    REAL(wp) ::  zns !< molar concentration of solutes (mol/m3)
5486    REAL(wp) ::  znw !< molar concentration of water (mol/m3)
5487
5488    zns = ( 3.0_wp * ( ppart%volc(1) * arhoh2so4 / amh2so4 ) +               &
5489                     ( ppart%volc(2) * arhooc / amoc ) +                     &
5490            2.0_wp * ( ppart%volc(5) * arhoss / amss ) +                     &
5491                     ( ppart%volc(6) * arhohno3 / amhno3 ) +                 &
5492                     ( ppart%volc(7) * arhonh3 / amnh3 ) )
5493    IF ( PRESENT(pcw) ) THEN
5494       znw = pcw
5495    ELSE
5496       znw = ppart%volc(8) * arhoh2o / amh2o
5497    ENDIF
5498!-- Activity = partial pressure of water vapour /
5499!--            sat. vapour pressure of water over a bulk liquid surface
5500!--          = molality * activity coefficient (Jacobson, 2005: eq. 17.20-21)
5501!-- Assume activity coefficient of 1 for water
5502    acth2o = MAX( 0.1_wp, znw / MAX( EPSILON( 1.0_wp ),( znw + zns ) ) )
5503 END FUNCTION acth2o
5504
5505!------------------------------------------------------------------------------!
5506! Description:
5507! ------------
5508!> Calculates the dissolutional growth of particles (i.e. gas transfers to a
5509!> particle surface and dissolves in liquid water on the surface). Treated here
5510!> as a non-equilibrium (time-dependent) process. Gases: HNO3 and NH3
5511!> (Chapter 17.14 in Jacobson, 2005).
5512!
5513!> Called from subroutine condensation.
5514!> Coded by:
5515!> Harri Kokkola (FMI)
5516!------------------------------------------------------------------------------!
5517 SUBROUTINE gpparthno3( ppres, ptemp, paero, pghno3, pgnh3, pcw, pcs, pbeta,   &
5518                        ptstep )
5519               
5520    IMPLICIT NONE
5521!
5522!-- Input and output variables
5523    REAL(wp), INTENT(in) ::  pbeta(nbins) !< transitional correction factor for
5524                                          !< aerosols   
5525    REAL(wp), INTENT(in) ::  ppres        !< ambient pressure (Pa)
5526    REAL(wp), INTENT(in) ::  pcs          !< water vapour saturation
5527                                          !< concentration (kg/m3)
5528    REAL(wp), INTENT(in) ::  ptemp        !< ambient temperature (K)
5529    REAL(wp), INTENT(in) ::  ptstep       !< time step (s)
5530    REAL(wp), INTENT(inout) ::  pghno3    !< nitric acid concentration (#/m3)
5531    REAL(wp), INTENT(inout) ::  pgnh3     !< ammonia conc. (#/m3)   
5532    REAL(wp), INTENT(inout) ::  pcw       !< water vapour concentration (kg/m3)
5533    TYPE(t_section), INTENT(inout) ::  paero(nbins) !< Aerosol properties
5534!   
5535!-- Local variables
5536    INTEGER(iwp) ::  b              !< loop index
5537    REAL(wp) ::  adt                !< timestep
5538    REAL(wp) ::  zachhso4ae(nbins)  !< Activity coefficients for HHSO4
5539    REAL(wp) ::  zacnh3ae(nbins)    !< Activity coefficients for NH3
5540    REAL(wp) ::  zacnh4hso2ae(nbins)!< Activity coefficients for NH4HSO2
5541    REAL(wp) ::  zacno3ae(nbins)    !< Activity coefficients for HNO3
5542    REAL(wp) ::  zcgnh3eqae(nbins)  !< Equilibrium gas concentration: NH3
5543    REAL(wp) ::  zcgno3eqae(nbins)  !< Equilibrium gas concentration: HNO3
5544    REAL(wp) ::  zcgwaeqae(nbins)   !< Equilibrium gas concentration: H2O
5545    REAL(wp) ::  zcnh3c             !< Current NH3 gas concentration
5546    REAL(wp) ::  zcnh3int           !< Intermediate NH3 gas concentration
5547    REAL(wp) ::  zcnh3intae(nbins)  !< Intermediate NH3 aerosol concentration
5548    REAL(wp) ::  zcnh3n             !< New NH3 gas concentration
5549    REAL(wp) ::  zcnh3cae(nbins)    !< Current NH3 in aerosols
5550    REAL(wp) ::  zcnh3nae(nbins)    !< New NH3 in aerosols
5551    REAL(wp) ::  zcnh3tot           !< Total NH3 concentration
5552    REAL(wp) ::  zcno3c             !< Current HNO3 gas concentration
5553    REAL(wp) ::  zcno3int           !< Intermediate HNO3 gas concentration
5554    REAL(wp) ::  zcno3intae(nbins)  !< Intermediate HNO3 aerosol concentration
5555    REAL(wp) ::  zcno3n             !< New HNO3 gas concentration                 
5556    REAL(wp) ::  zcno3cae(nbins)    !< Current HNO3 in aerosols
5557    REAL(wp) ::  zcno3nae(nbins)    !< New HNO3 in aerosols
5558    REAL(wp) ::  zcno3tot           !< Total HNO3 concentration   
5559    REAL(wp) ::  zdfvap             !< Diffusion coefficient for vapors
5560    REAL(wp) ::  zhlp1              !< helping variable
5561    REAL(wp) ::  zhlp2              !< helping variable   
5562    REAL(wp) ::  zkelnh3ae(nbins)   !< Kelvin effects for NH3
5563    REAL(wp) ::  zkelno3ae(nbins)   !< Kelvin effect for HNO3
5564    REAL(wp) ::  zmolsae(nbins,7)   !< Ion molalities from pdfite
5565    REAL(wp) ::  zmtnh3ae(nbins)    !< Mass transfer coefficients for NH3
5566    REAL(wp) ::  zmtno3ae(nbins)    !< Mass transfer coefficients for HNO3
5567    REAL(wp) ::  zrh                !< relative humidity
5568    REAL(wp) ::  zsathno3ae(nbins)  !< HNO3 saturation ratio
5569    REAL(wp) ::  zsatnh3ae(nbins)   !< NH3 saturation ratio = the partial
5570                                    !< pressure of a gas divided by its
5571                                    !< saturation vapor pressure over a surface
5572!         
5573!-- Initialise:
5574    adt          = ptstep
5575    zachhso4ae   = 0.0_wp
5576    zacnh3ae     = 0.0_wp
5577    zacnh4hso2ae = 0.0_wp
5578    zacno3ae     = 0.0_wp
5579    zcgnh3eqae   = 0.0_wp
5580    zcgno3eqae   = 0.0_wp
5581    zcnh3c       = 0.0_wp
5582    zcnh3cae     = 0.0_wp
5583    zcnh3int     = 0.0_wp
5584    zcnh3intae   = 0.0_wp
5585    zcnh3n       = 0.0_wp
5586    zcnh3nae     = 0.0_wp
5587    zcnh3tot     = 0.0_wp
5588    zcno3c       = 0.0_wp
5589    zcno3cae     = 0.0_wp 
5590    zcno3int     = 0.0_wp
5591    zcno3intae   = 0.0_wp
5592    zcno3n       = 0.0_wp
5593    zcno3nae     = 0.0_wp
5594    zcno3tot     = 0.0_wp
5595    zhlp1        = 0.0_wp
5596    zhlp2        = 0.0_wp
5597    zkelno3ae    = 1.0_wp   
5598    zkelnh3ae    = 1.0_wp 
5599    zmolsae      = 0.0_wp
5600    zmtno3ae     = 0.0_wp
5601    zmtnh3ae     = 0.0_wp
5602    zrh          = 0.0_wp
5603    zsatnh3ae    = 1.0_wp
5604    zsathno3ae   = 1.0_wp
5605!             
5606!-- Diffusion coefficient (m2/s)             
5607    zdfvap = 5.1111E-10_wp * ptemp ** 1.75_wp * ( p_0 + 1325.0_wp ) / ppres 
5608!             
5609!-- Kelvin effects (Jacobson (2005), eq. 16.33)
5610    zkelno3ae(1:nbins) = EXP( 4.0_wp * surfw0 * amvhno3 / ( abo * ptemp *      &
5611                              paero(1:nbins)%dwet ) ) 
5612    zkelnh3ae(1:nbins) = EXP( 4.0_wp * surfw0 * amvnh3 / ( abo * ptemp *       &
5613                              paero(1:nbins)%dwet ) )
5614!                             
5615!-- Current vapour mole concentrations (mol/m3)
5616    zcno3c = pghno3 / avo            ! HNO3
5617    zcnh3c = pgnh3 / avo             ! NH3
5618!             
5619!-- Current particle mole concentrations (mol/m3)
5620    zcno3cae(1:nbins) = paero(1:nbins)%volc(6) * arhohno3 / amhno3
5621    zcnh3cae(1:nbins) = paero(1:nbins)%volc(7) * arhonh3 / amnh3
5622!   
5623!-- Total mole concentrations: gas and particle phase
5624    zcno3tot = zcno3c + SUM( zcno3cae(1:nbins) )
5625    zcnh3tot = zcnh3c + SUM( zcnh3cae(1:nbins) )
5626!   
5627!-- Relative humidity [0-1]
5628    zrh = pcw / pcs
5629!   
5630!-- Mass transfer coefficients (Jacobson, Eq. 16.64)
5631    zmtno3ae(1:nbins) = 2.0_wp * pi * paero(1:nbins)%dwet * zdfvap *           &
5632                        paero(1:nbins)%numc * pbeta(1:nbins)
5633    zmtnh3ae(1:nbins) = 2.0_wp * pi * paero(1:nbins)%dwet * zdfvap *           &
5634                        paero(1:nbins)%numc * pbeta(1:nbins)
5635
5636!   
5637!-- Get the equilibrium concentrations above aerosols
5638    CALL NONHEquil( zrh, ptemp, paero, zcgno3eqae, zcgnh3eqae, zacno3ae,       &
5639                    zacnh3ae, zacnh4hso2ae, zachhso4ae, zmolsae )
5640   
5641!
5642!-- NH4/HNO3 saturation ratios for aerosols
5643    CALL SVsat( ptemp, paero, zacno3ae, zacnh3ae, zacnh4hso2ae, zachhso4ae,    &
5644                zcgno3eqae, zcno3cae, zcnh3cae, zkelno3ae, zkelnh3ae,          &
5645                zsathno3ae, zsatnh3ae, zmolsae ) 
5646!   
5647!-- Intermediate concentrations   
5648    zhlp1 = SUM( zcno3cae(1:nbins) / ( 1.0_wp + adt * zmtno3ae(1:nbins) *      &
5649            zsathno3ae(1:nbins) ) )
5650    zhlp2 = SUM( zmtno3ae(1:nbins) / ( 1.0_wp + adt * zmtno3ae(1:nbins) *      &
5651            zsathno3ae(1:nbins) ) )
5652    zcno3int = ( zcno3tot - zhlp1 ) / ( 1.0_wp + adt * zhlp2 )
5653
5654    zhlp1 = SUM( zcnh3cae(1:nbins) / ( 1.0_wp + adt * zmtnh3ae(1:nbins) *      &
5655            zsatnh3ae(1:nbins) ) )
5656    zhlp2 = SUM( zmtnh3ae(1:nbins) / ( 1.0_wp + adt * zmtnh3ae(1:nbins) *      &
5657            zsatnh3ae(1:nbins) ) )
5658    zcnh3int = ( zcnh3tot - zhlp1 )/( 1.0_wp + adt * zhlp2 )
5659
5660    zcno3int = MIN(zcno3int, zcno3tot)
5661    zcnh3int = MIN(zcnh3int, zcnh3tot)
5662!
5663!-- Calculate the new particle concentrations
5664    zcno3intae = zcno3cae
5665    zcnh3intae = zcnh3cae
5666    DO  b = 1, nbins
5667       zcno3intae(b) = ( zcno3cae(b) + adt * zmtno3ae(b) * zcno3int ) /     &
5668            ( 1.0_wp + adt * zmtno3ae(b) * zsathno3ae(b) )
5669       zcnh3intae(b) = ( zcnh3cae(b) + adt * zmtnh3ae(b) * zcnh3int ) /     &
5670            ( 1.0_wp + adt * zmtnh3ae(b) * zsatnh3ae(b) )
5671    ENDDO
5672
5673    zcno3intae(1:nbins) = MAX( zcno3intae(1:nbins), 0.0_wp )
5674    zcnh3intae(1:nbins) = MAX( zcnh3intae(1:nbins), 0.0_wp )
5675
5676    zcno3n   = zcno3int    ! Final molar gas concentration of HNO3
5677    zcno3nae = zcno3intae  ! Final molar particle concentration of HNO3
5678   
5679    zcnh3n   = zcnh3int    ! Final molar gas concentration of NH3
5680    zcnh3nae = zcnh3intae  ! Final molar particle concentration of NH3
5681!
5682!-- Model timestep reached - update the new arrays
5683    pghno3 = zcno3n * avo
5684    pgnh3  = zcnh3n * avo
5685
5686    DO  b = in1a, fn2b
5687       paero(b)%volc(6) = zcno3nae(b) * amhno3 / arhohno3
5688       paero(b)%volc(7) = zcnh3nae(b) * amnh3 / arhonh3
5689    ENDDO
5690   
5691   
5692 END SUBROUTINE gpparthno3
5693!------------------------------------------------------------------------------!
5694! Description:
5695! ------------
5696!> Calculate the equilibrium concentrations above aerosols (reference?)
5697!------------------------------------------------------------------------------!
5698 SUBROUTINE NONHEquil( prh, ptemp, ppart, pcgno3eq, pcgnh3eq, pgammano,        &
5699                       pgammanh, pgammanh4hso2, pgammahhso4, pmols )
5700   
5701    IMPLICIT NONE
5702   
5703    REAL(wp), INTENT(in) ::  prh    !< relative humidity
5704    REAL(wp), INTENT(in) ::  ptemp  !< ambient temperature (K)
5705   
5706    TYPE(t_section), INTENT(inout) ::  ppart(nbins) !< Aerosol properties
5707!-- Equilibrium molar concentration above aerosols:
5708    REAL(wp), INTENT(inout) ::  pcgnh3eq(nbins)      !< of NH3
5709    REAL(wp), INTENT(inout) ::  pcgno3eq(nbins)      !< of HNO3
5710                                                     !< Activity coefficients:
5711    REAL(wp), INTENT(inout) ::  pgammahhso4(nbins)   !< HHSO4   
5712    REAL(wp), INTENT(inout) ::  pgammanh(nbins)      !< NH3
5713    REAL(wp), INTENT(inout) ::  pgammanh4hso2(nbins) !< NH4HSO2 
5714    REAL(wp), INTENT(inout) ::  pgammano(nbins)      !< HNO3
5715    REAL(wp), INTENT(inout) ::  pmols(nbins,7)       !< Ion molalities
5716   
5717    INTEGER(iwp) ::  b
5718
5719    REAL(wp) ::  zgammas(7)    !< Activity coefficients   
5720    REAL(wp) ::  zhlp          !< Dummy variable
5721    REAL(wp) ::  zions(7)      !< molar concentration of ion (mol/m3)
5722    REAL(wp) ::  zphcl         !< Equilibrium vapor pressures (Pa??)   
5723    REAL(wp) ::  zphno3        !< Equilibrium vapor pressures (Pa??)
5724    REAL(wp) ::  zpnh3         !< Equilibrium vapor pressures (Pa??)
5725    REAL(wp) ::  zwatertotal   !< Total water in particles (mol/m3) ???   
5726
5727    zgammas     = 0.0_wp
5728    zhlp        = 0.0_wp
5729    zions       = 0.0_wp
5730    zphcl       = 0.0_wp
5731    zphno3      = 0.0_wp
5732    zpnh3       = 0.0_wp
5733    zwatertotal = 0.0_wp
5734
5735    DO  b = 1, nbins
5736   
5737       IF ( ppart(b)%numc < nclim )  CYCLE
5738!
5739!--    2*H2SO4 + CL + NO3 - Na - NH4
5740       zhlp = 2.0_wp * ppart(b)%volc(1) * arhoh2so4 / amh2so4 +               &
5741              ppart(b)%volc(5) * arhoss / amss +                              &
5742              ppart(b)%volc(6) * arhohno3 / amhno3 -                          &
5743              ppart(b)%volc(5) * arhoss / amss -                              &
5744              ppart(b)%volc(7) * arhonh3 / amnh3
5745
5746       zhlp = MAX( zhlp, 1.0E-30_wp )
5747
5748       zions(1) = zhlp                                   ! H+
5749       zions(2) = ppart(b)%volc(7) * arhonh3 / amnh3     ! NH4+
5750       zions(3) = ppart(b)%volc(5) * arhoss / amss       ! Na+
5751       zions(4) = ppart(b)%volc(1) * arhoh2so4 / amh2so4 ! SO4(2-)
5752       zions(5) = 0.0_wp                                 ! HSO4-
5753       zions(6) = ppart(b)%volc(6) * arhohno3 / amhno3   ! NO3-
5754       zions(7) = ppart(b)%volc(5) * arhoss / amss       ! Cl-
5755
5756       zwatertotal = ppart(b)%volc(8) * arhoh2o / amh2o
5757       IF ( zwatertotal > 1.0E-30_wp )  THEN
5758          CALL inorganic_pdfite( prh, ptemp, zions, zwatertotal, zphno3, zphcl,&
5759                                 zpnh3, zgammas, pmols(b,:) )
5760       ENDIF
5761!
5762!--    Activity coefficients
5763       pgammano(b) = zgammas(1)           ! HNO3
5764       pgammanh(b) = zgammas(3)           ! NH3
5765       pgammanh4hso2(b) = zgammas(6)      ! NH4HSO2
5766       pgammahhso4(b) = zgammas(7)        ! HHSO4
5767!
5768!--    Equilibrium molar concentrations (mol/m3) from equlibrium pressures (Pa)
5769       pcgno3eq(b) = zphno3 / ( argas * ptemp )
5770       pcgnh3eq(b) = zpnh3 / ( argas * ptemp )
5771
5772    ENDDO
5773
5774  END SUBROUTINE NONHEquil
5775 
5776!------------------------------------------------------------------------------!
5777! Description:
5778! ------------
5779!> Calculate saturation ratios of NH4 and HNO3 for aerosols
5780!------------------------------------------------------------------------------!
5781 SUBROUTINE SVsat( ptemp, ppart, pachno3, pacnh3, pacnh4hso2, pachhso4,        &
5782                   pchno3eq, pchno3, pcnh3, pkelhno3, pkelnh3, psathno3,       &
5783                   psatnh3, pmols )
5784
5785    IMPLICIT NONE
5786   
5787    REAL(wp), INTENT(in) ::  ptemp  !< ambient temperature (K)
5788   
5789    TYPE(t_section), INTENT(inout) ::  ppart(nbins) !< Aerosol properties
5790!-- Activity coefficients
5791    REAL(wp), INTENT(in) ::  pachhso4(nbins)   !<
5792    REAL(wp), INTENT(in) ::  pacnh3(nbins)     !<
5793    REAL(wp), INTENT(in) ::  pacnh4hso2(nbins) !<
5794    REAL(wp), INTENT(in) ::  pachno3(nbins)    !<
5795    REAL(wp), INTENT(in) ::  pchno3eq(nbins) !< Equilibrium surface concentration
5796                                             !< of HNO3
5797    REAL(wp), INTENT(in) ::  pchno3(nbins)   !< Current particle mole
5798                                             !< concentration of HNO3 (mol/m3)
5799    REAL(wp), INTENT(in) ::  pcnh3(nbins)    !< Current particle mole
5800                                             !< concentration of NH3 (mol/m3)
5801    REAL(wp), INTENT(in) ::  pkelhno3(nbins) !< Kelvin effect for HNO3
5802    REAL(wp), INTENT(in) ::  pkelnh3(nbins)  !< Kelvin effect for NH3
5803    REAL(wp), INTENT(in) ::  pmols(nbins,7)
5804!-- Saturation ratios
5805    REAL(wp), INTENT(out) ::  psathno3(nbins) !<
5806    REAL(wp), INTENT(out) ::  psatnh3(nbins)  !<
5807   
5808    INTEGER :: b   !< running index for aerosol bins
5809!-- Constants for calculating equilibrium constants:   
5810    REAL(wp), PARAMETER ::  a1 = -22.52_wp     !<
5811    REAL(wp), PARAMETER ::  a2 = -1.50_wp      !<
5812    REAL(wp), PARAMETER ::  a3 = 13.79_wp      !<
5813    REAL(wp), PARAMETER ::  a4 = 29.17_wp      !<
5814    REAL(wp), PARAMETER ::  b1 = 26.92_wp      !<
5815    REAL(wp), PARAMETER ::  b2 = 26.92_wp      !<
5816    REAL(wp), PARAMETER ::  b3 = -5.39_wp      !<
5817    REAL(wp), PARAMETER ::  b4 = 16.84_wp      !<
5818    REAL(wp), PARAMETER ::  K01 = 1.01E-14_wp  !<
5819    REAL(wp), PARAMETER ::  K02 = 1.81E-5_wp   !<
5820    REAL(wp), PARAMETER ::  K03 = 57.64_wp     !<
5821    REAL(wp), PARAMETER ::  K04 = 2.51E+6_wp   !<
5822!-- Equilibrium constants of equilibrium reactions
5823    REAL(wp) ::  KllH2O    !< H2O(aq) <--> H+ + OH- (mol/kg)
5824    REAL(wp) ::  KllNH3    !< NH3(aq) + H2O(aq) <--> NH4+ + OH- (mol/kg)
5825    REAL(wp) ::  KglNH3    !< NH3(g) <--> NH3(aq) (mol/kg/atm)
5826    REAL(wp) ::  KglHNO3   !< HNO3(g) <--> H+ + NO3- (mol2/kg2/atm)
5827    REAL(wp) ::  zmolno3   !< molality of NO3- (mol/kg)
5828    REAL(wp) ::  zmolhp    !< molality of H+ (mol/kg)
5829    REAL(wp) ::  zmolso4   !< molality of SO4(2-) (mol/kg)
5830    REAL(wp) ::  zmolcl    !< molality of Cl (mol/kg)
5831    REAL(wp) ::  zmolnh4   !< Molality of NH4 (mol/kg)
5832    REAL(wp) ::  zmolna    !< Molality of Na (mol/kg)
5833    REAL(wp) ::  zhlp1     !<
5834    REAL(wp) ::  zhlp2     !<
5835    REAL(wp) ::  zhlp3     !<
5836    REAL(wp) ::  zxi       !<
5837    REAL(wp) ::  zt0       !< Reference temp
5838   
5839    zhlp1   = 0.0_wp
5840    zhlp2   = 0.0_wp 
5841    zhlp3   = 0.0_wp
5842    zmolcl  = 0.0_wp
5843    zmolhp  = 0.0_wp
5844    zmolna  = 0.0_wp
5845    zmolnh4 = 0.0_wp
5846    zmolno3 = 0.0_wp
5847    zmolso4 = 0.0_wp
5848    zt0     = 298.15_wp 
5849    zxi     = 0.0_wp
5850!
5851!-- Calculates equlibrium rate constants based on Table B.7 in Jacobson (2005)
5852!-- K^ll_H20, K^ll_NH3, K^gl_NH3, K^gl_HNO3
5853    zhlp1 = zt0 / ptemp
5854    zhlp2 = zhlp1 - 1.0_wp
5855    zhlp3 = 1.0_wp + LOG( zhlp1 ) - zhlp1
5856
5857    KllH2O = K01 * EXP( a1 * zhlp2 + b1 * zhlp3 )
5858    KllNH3 = K02 * EXP( a2 * zhlp2 + b2 * zhlp3 )
5859    KglNH3 = K03 * EXP( a3 * zhlp2 + b3 * zhlp3 )
5860    KglHNO3 = K04 * EXP( a4 * zhlp2 + b4 * zhlp3 )
5861
5862    DO  b = 1, nbins
5863
5864       IF ( ppart(b)%numc > nclim  .AND.  ppart(b)%volc(8) > 1.0E-30_wp  )  THEN
5865!
5866!--       Molality of H+ and NO3-
5867          zhlp1 = pcnh3(b) * amnh3 + ppart(b)%volc(1) * arhoh2so4 +            &
5868                  ppart(b)%volc(2) * arhooc + ppart(b)%volc(5) * arhoss +      &
5869                  ppart(b)%volc(8) * arhoh2o
5870          zmolno3 = pchno3(b) / zhlp1  !< mol/kg
5871!
5872!--       Particle mole concentration ratio: (NH3+SS)/H2SO4       
5873          zxi = ( pcnh3(b) + ppart(b)%volc(5) * arhoss / amss ) /              &
5874                ( ppart(b)%volc(1) * arhoh2so4 / amh2so4 )
5875               
5876          IF ( zxi <= 2.0_wp )  THEN
5877!
5878!--          Molality of SO4(2-)
5879             zhlp1 = pcnh3(b) * amnh3 + pchno3(b) * amhno3 +                   &
5880                     ppart(b)%volc(2) * arhooc + ppart(b)%volc(5) * arhoss +   &
5881                     ppart(b)%volc(8) * arhoh2o
5882             zmolso4 = ( ppart(b)%volc(1) * arhoh2so4 / amh2so4 ) / zhlp1
5883!
5884!--          Molality of Cl-
5885             zhlp1 = pcnh3(b) * amnh3 + pchno3(b) * amhno3 +                   &
5886                     ppart(b)%volc(2) * arhooc + ppart(b)%volc(1) * arhoh2so4  &
5887                     + ppart(b)%volc(8) * arhoh2o
5888             zmolcl = ( ppart(b)%volc(5) * arhoss / amss ) / zhlp1
5889!
5890!--          Molality of NH4+
5891             zhlp1 =  pchno3(b) * amhno3 + ppart(b)%volc(1) * arhoh2so4 +      &
5892                      ppart(b)%volc(2) * arhooc + ppart(b)%volc(5) * arhoss +  &
5893                      ppart(b)%volc(8) * arhoh2o
5894             zmolnh4 = pcnh3(b) / zhlp1
5895!             
5896!--          Molality of Na+
5897             zmolna = zmolcl
5898!
5899!--          Molality of H+
5900             zmolhp = 2.0_wp * zmolso4 + zmolno3 + zmolcl - ( zmolnh4 + zmolna )
5901
5902          ELSE
5903
5904             zhlp2 = pkelhno3(b) * zmolno3 * pachno3(b) ** 2.0_wp
5905!
5906!--          Mona debugging
5907             IF ( zhlp2 > 1.0E-30_wp )  THEN
5908                zmolhp = KglHNO3 * pchno3eq(b) / zhlp2 ! Eq. 17.38
5909             ELSE
5910                zmolhp = 0.0_wp
5911             ENDIF
5912
5913          ENDIF
5914
5915          zhlp1 = ppart(b)%volc(8) * arhoh2o * argas * ptemp * KglHNO3
5916!
5917!--       Saturation ratio for NH3 and for HNO3
5918          IF ( zmolhp > 0.0_wp )  THEN
5919             zhlp2 = pkelnh3(b) / ( zhlp1 * zmolhp )
5920             zhlp3 = KllH2O / ( KllNH3 + KglNH3 )
5921             psatnh3(b) = zhlp2 * ( ( pacnh4hso2(b) / pachhso4(b) ) **2.0_wp ) &
5922                          * zhlp3
5923             psathno3(b) = ( pkelhno3(b) * zmolhp * pachno3(b)**2.0_wp ) / zhlp1
5924          ELSE
5925             psatnh3(b) = 1.0_wp
5926             psathno3(b) = 1.0_wp
5927          ENDIF
5928       ELSE
5929          psatnh3(b) = 1.0_wp
5930          psathno3(b) = 1.0_wp
5931       ENDIF
5932
5933    ENDDO
5934
5935  END SUBROUTINE SVsat
5936 
5937!------------------------------------------------------------------------------!
5938! Description:
5939! ------------
5940!> Prototype module for calculating the water content of a mixed inorganic/
5941!> organic particle + equilibrium water vapour pressure above the solution
5942!> (HNO3, HCL, NH3 and representative organic compounds. Efficient calculation
5943!> of the partitioning of species between gas and aerosol. Based in a chamber
5944!> study.
5945!
5946!> Written by Dave Topping. Pure organic component properties predicted by Mark
5947!> Barley based on VOCs predicted in MCM simulations performed by Mike Jenkin.
5948!> Delivered by Gordon McFiggans as Deliverable D22 from WP1.4 in the EU FP6
5949!> EUCAARI Integrated Project.
5950!
5951!> Queries concerning the use of this code through Gordon McFiggans,
5952!> g.mcfiggans@manchester.ac.uk,
5953!> Ownership: D. Topping, Centre for Atmospheric Sciences, University of
5954!> Manchester, 2007
5955!
5956!> Rewritten to PALM by Mona Kurppa, UHel, 2017
5957!------------------------------------------------------------------------------!
5958 SUBROUTINE inorganic_pdfite( RH, temp, ions, water_total, Press_HNO3,         &
5959                               Press_HCL, Press_NH3, gamma_out, mols_out )
5960   
5961    IMPLICIT NONE
5962   
5963    REAL(wp), DIMENSION(:) ::  gamma_out !< Activity coefficient for calculating
5964                                         !< the non-ideal dissociation constants
5965                                         !< 1: HNO3, 2: HCL, 3: NH4+/H+ (NH3)
5966                                         !< 4: HHSO4**2/H2SO4,
5967                                         !< 5: H2SO4**3/HHSO4**2
5968                                         !< 6: NH4HSO2, 7: HHSO4
5969    REAL(wp), DIMENSION(:) ::  ions      !< ion molarities (mol/m3)
5970                                         !< 1: H+, 2: NH4+, 3: Na+, 4: SO4(2-),
5971                                         !< 5: HSO4-, 6: NO3-, 7: Cl-
5972    REAL(wp), DIMENSION(7) ::  ions_mol  !< ion molalities (mol/kg)
5973                                         !< 1: H+, 2: NH4+, 3: Na+, 4: SO4(2-),
5974                                         !< 5: HSO4-, 6: NO3-, 7: Cl-
5975    REAL(wp), DIMENSION(:) ::  mols_out  !< ion molality output (mol/kg)
5976                                         !< 1: H+, 2: NH4+, 3: Na+, 4: SO4(2-),
5977                                         !< 5: HSO4-, 6: NO3-, 7: Cl-
5978    REAL(wp) ::  act_product               !< ionic activity coef. product:
5979                                           !< = (gamma_h2so4**3d0) /
5980                                           !<   (gamma_hhso4**2d0)       
5981    REAL(wp) ::  ammonium_chloride         !<
5982    REAL(wp) ::  ammonium_chloride_eq_frac !<                         
5983    REAL(wp) ::  ammonium_nitrate          !<
5984    REAL(wp) ::  ammonium_nitrate_eq_frac  !<       
5985    REAL(wp) ::  ammonium_sulphate         !< 
5986    REAL(wp) ::  ammonium_sulphate_eq_frac !<
5987    REAL(wp) ::  binary_h2so4              !< binary H2SO4 activity coeff.       
5988    REAL(wp) ::  binary_hcl                !< binary HCL activity coeff.
5989    REAL(wp) ::  binary_hhso4              !< binary HHSO4 activity coeff.     
5990    REAL(wp) ::  binary_hno3               !< binary HNO3 activity coeff.
5991    REAL(wp) ::  binary_nh4hso4            !< binary NH4HSO4 activity coeff.   
5992    REAL(wp) ::  charge_sum                !< sum of ionic charges
5993    REAL(wp) ::  gamma_h2so4               !< activity coefficient       
5994    REAL(wp) ::  gamma_hcl                 !< activity coefficient
5995    REAL(wp) ::  gamma_hhso4               !< activity coeffient       
5996    REAL(wp) ::  gamma_hno3                !< activity coefficient
5997    REAL(wp) ::  gamma_nh3                 !< activity coefficient
5998    REAL(wp) ::  gamma_nh4hso4             !< activity coefficient
5999    REAL(wp) ::  h_out                     !<
6000    REAL(wp) ::  h_real                    !< new hydrogen ion conc.
6001    REAL(wp) ::  H2SO4_hcl                 !< contribution of H2SO4       
6002    REAL(wp) ::  H2SO4_hno3                !< contribution of H2SO4
6003    REAL(wp) ::  H2SO4_nh3                 !< contribution of H2SO4
6004    REAL(wp) ::  H2SO4_nh4hso4             !< contribution of H2SO4       
6005    REAL(wp) ::  HCL_h2so4                 !< contribution of HCL       
6006    REAL(wp) ::  HCL_hhso4                 !< contribution of HCL       
6007    REAL(wp) ::  HCL_hno3                  !< contribution of HCL
6008    REAL(wp) ::  HCL_nh3                   !< contribution of HCL
6009    REAL(wp) ::  HCL_nh4hso4               !< contribution of HCL
6010    REAL(wp) ::  henrys_temp_dep           !< temperature dependence of
6011                                           !< Henry's Law       
6012    REAL(wp) ::  HNO3_h2so4                !< contribution of HNO3       
6013    REAL(wp) ::  HNO3_hcl                  !< contribution of HNO3
6014    REAL(wp) ::  HNO3_hhso4                !< contribution of HNO3
6015    REAL(wp) ::  HNO3_nh3                  !< contribution of HNO3
6016    REAL(wp) ::  HNO3_nh4hso4              !< contribution of HNO3
6017    REAL(wp) ::  hso4_out                  !<
6018    REAL(wp) ::  hso4_real                 !< new bisulphate ion conc.
6019    REAL(wp) ::  hydrochloric_acid         !<
6020    REAL(wp) ::  hydrochloric_acid_eq_frac !<
6021    REAL(wp) ::  Kh                        !< equilibrium constant for H+       
6022    REAL(wp) ::  K_hcl                     !< equilibrium constant of HCL       
6023    REAL(wp) ::  K_hno3                    !< equilibrium constant of HNO3
6024    REAL(wp) ::  Knh4                      !< equilibrium constant for NH4+
6025    REAL(wp) ::  Kw                        !< equil. const. for water_surface 
6026    REAL(wp) ::  Ln_h2so4_act              !< gamma_h2so4 = EXP(Ln_h2so4_act)
6027    REAL(wp) ::  Ln_HCL_act                !< gamma_hcl = EXP( Ln_HCL_act )
6028    REAL(wp) ::  Ln_hhso4_act              !< gamma_hhso4 = EXP(Ln_hhso4_act)
6029    REAL(wp) ::  Ln_HNO3_act               !< gamma_hno3 = EXP( Ln_HNO3_act )
6030    REAL(wp) ::  Ln_NH4HSO4_act            !< gamma_nh4hso4 =
6031                                           !< EXP( Ln_NH4HSO4_act )
6032    REAL(wp) ::  molality_ratio_nh3        !< molality ratio of NH3
6033                                           !< (NH4+ and H+)
6034    REAL(wp) ::  Na2SO4_h2so4              !< contribution of Na2SO4                                             
6035    REAL(wp) ::  Na2SO4_hcl                !< contribution of Na2SO4
6036    REAL(wp) ::  Na2SO4_hhso4              !< contribution of Na2SO4       
6037    REAL(wp) ::  Na2SO4_hno3               !< contribution of Na2SO4
6038    REAL(wp) ::  Na2SO4_nh3                !< contribution of Na2SO4
6039    REAL(wp) ::  Na2SO4_nh4hso4            !< contribution of Na2SO4       
6040    REAL(wp) ::  NaCl_h2so4                !< contribution of NaCl       
6041    REAL(wp) ::  NaCl_hcl                  !< contribution of NaCl
6042    REAL(wp) ::  NaCl_hhso4                !< contribution of NaCl       
6043    REAL(wp) ::  NaCl_hno3                 !< contribution of NaCl
6044    REAL(wp) ::  NaCl_nh3                  !< contribution of NaCl
6045    REAL(wp) ::  NaCl_nh4hso4              !< contribution of NaCl       
6046    REAL(wp) ::  NaNO3_h2so4               !< contribution of NaNO3       
6047    REAL(wp) ::  NaNO3_hcl                 !< contribution of NaNO3
6048    REAL(wp) ::  NaNO3_hhso4               !< contribution of NaNO3       
6049    REAL(wp) ::  NaNO3_hno3                !< contribution of NaNO3
6050    REAL(wp) ::  NaNO3_nh3                 !< contribution of NaNO3 
6051    REAL(wp) ::  NaNO3_nh4hso4             !< contribution of NaNO3       
6052    REAL(wp) ::  NH42SO4_h2so4             !< contribution of NH42SO4       
6053    REAL(wp) ::  NH42SO4_hcl               !< contribution of NH42SO4
6054    REAL(wp) ::  NH42SO4_hhso4             !< contribution of NH42SO4       
6055    REAL(wp) ::  NH42SO4_hno3              !< contribution of NH42SO4
6056    REAL(wp) ::  NH42SO4_nh3               !< contribution of NH42SO4
6057    REAL(wp) ::  NH42SO4_nh4hso4           !< contribution of NH42SO4
6058    REAL(wp) ::  NH4Cl_h2so4               !< contribution of NH4Cl       
6059    REAL(wp) ::  NH4Cl_hcl                 !< contribution of NH4Cl
6060    REAL(wp) ::  NH4Cl_hhso4               !< contribution of NH4Cl       
6061    REAL(wp) ::  NH4Cl_hno3                !< contribution of NH4Cl
6062    REAL(wp) ::  NH4Cl_nh3                 !< contribution of NH4Cl
6063    REAL(wp) ::  NH4Cl_nh4hso4             !< contribution of NH4Cl       
6064    REAL(wp) ::  NH4NO3_h2so4              !< contribution of NH4NO3
6065    REAL(wp) ::  NH4NO3_hcl                !< contribution of NH4NO3
6066    REAL(wp) ::  NH4NO3_hhso4              !< contribution of NH4NO3
6067    REAL(wp) ::  NH4NO3_hno3               !< contribution of NH4NO3
6068    REAL(wp) ::  NH4NO3_nh3                !< contribution of NH4NO3
6069    REAL(wp) ::  NH4NO3_nh4hso4            !< contribution of NH4NO3       
6070    REAL(wp) ::  nitric_acid               !<
6071    REAL(wp) ::  nitric_acid_eq_frac       !< Equivalent fractions
6072    REAL(wp) ::  Press_HCL                 !< partial pressure of HCL       
6073    REAL(wp) ::  Press_HNO3                !< partial pressure of HNO3
6074    REAL(wp) ::  Press_NH3                 !< partial pressure of NH3       
6075    REAL(wp) ::  RH                        !< relative humidity [0-1]
6076    REAL(wp) ::  temp                      !< temperature
6077    REAL(wp) ::  so4_out                   !<
6078    REAL(wp) ::  so4_real                  !< new sulpate ion concentration       
6079    REAL(wp) ::  sodium_chloride           !<
6080    REAL(wp) ::  sodium_chloride_eq_frac   !<   
6081    REAL(wp) ::  sodium_nitrate            !<
6082    REAL(wp) ::  sodium_nitrate_eq_frac    !<   
6083    REAL(wp) ::  sodium_sulphate           !<
6084    REAL(wp) ::  sodium_sulphate_eq_frac   !<       
6085    REAL(wp) ::  solutes                   !<
6086    REAL(wp) ::  sulphuric_acid            !<       
6087    REAL(wp) ::  sulphuric_acid_eq_frac    !<
6088    REAL(wp) ::  water_total               !<
6089   
6090    REAL(wp) ::  a !< auxiliary variable
6091    REAL(wp) ::  b !< auxiliary variable
6092    REAL(wp) ::  c !< auxiliary variable
6093    REAL(wp) ::  root1 !< auxiliary variable
6094    REAL(wp) ::  root2 !< auxiliary variable
6095
6096    INTEGER(iwp) ::  binary_case
6097    INTEGER(iwp) ::  full_complexity
6098!       
6099!-- Value initialisation
6100    binary_h2so4    = 0.0_wp   
6101    binary_hcl      = 0.0_wp 
6102    binary_hhso4    = 0.0_wp 
6103    binary_hno3     = 0.0_wp 
6104    binary_nh4hso4  = 0.0_wp 
6105    henrys_temp_dep = ( 1.0_wp / temp - 1.0_wp / 298.0_wp )
6106    HCL_hno3        = 1.0_wp
6107    H2SO4_hno3      = 1.0_wp
6108    NH42SO4_hno3    = 1.0_wp
6109    NH4NO3_hno3     = 1.0_wp
6110    NH4Cl_hno3      = 1.0_wp
6111    Na2SO4_hno3     = 1.0_wp
6112    NaNO3_hno3      = 1.0_wp
6113    NaCl_hno3       = 1.0_wp
6114    HNO3_hcl        = 1.0_wp
6115    H2SO4_hcl       = 1.0_wp
6116    NH42SO4_hcl     = 1.0_wp
6117    NH4NO3_hcl      = 1.0_wp
6118    NH4Cl_hcl       = 1.0_wp
6119    Na2SO4_hcl      = 1.0_wp 
6120    NaNO3_hcl       = 1.0_wp
6121    NaCl_hcl        = 1.0_wp
6122    HNO3_nh3        = 1.0_wp
6123    HCL_nh3         = 1.0_wp
6124    H2SO4_nh3       = 1.0_wp 
6125    NH42SO4_nh3     = 1.0_wp 
6126    NH4NO3_nh3      = 1.0_wp
6127    NH4Cl_nh3       = 1.0_wp
6128    Na2SO4_nh3      = 1.0_wp
6129    NaNO3_nh3       = 1.0_wp
6130    NaCl_nh3        = 1.0_wp
6131    HNO3_hhso4      = 1.0_wp 
6132    HCL_hhso4       = 1.0_wp
6133    NH42SO4_hhso4   = 1.0_wp
6134    NH4NO3_hhso4    = 1.0_wp
6135    NH4Cl_hhso4     = 1.0_wp
6136    Na2SO4_hhso4    = 1.0_wp
6137    NaNO3_hhso4     = 1.0_wp
6138    NaCl_hhso4      = 1.0_wp
6139    HNO3_h2so4      = 1.0_wp
6140    HCL_h2so4       = 1.0_wp
6141    NH42SO4_h2so4   = 1.0_wp 
6142    NH4NO3_h2so4    = 1.0_wp
6143    NH4Cl_h2so4     = 1.0_wp
6144    Na2SO4_h2so4    = 1.0_wp
6145    NaNO3_h2so4     = 1.0_wp
6146    NaCl_h2so4      = 1.0_wp
6147!-- New NH3 variables
6148    HNO3_nh4hso4    = 1.0_wp 
6149    HCL_nh4hso4     = 1.0_wp
6150    H2SO4_nh4hso4   = 1.0_wp
6151    NH42SO4_nh4hso4 = 1.0_wp 
6152    NH4NO3_nh4hso4  = 1.0_wp
6153    NH4Cl_nh4hso4   = 1.0_wp
6154    Na2SO4_nh4hso4  = 1.0_wp
6155    NaNO3_nh4hso4   = 1.0_wp
6156    NaCl_nh4hso4    = 1.0_wp
6157!
6158!-- Juha Tonttila added
6159    mols_out   = 0.0_wp
6160    Press_HNO3 = 0.0_wp
6161    Press_HCL  = 0.0_wp
6162    Press_NH3  = 0.0_wp !< Initialising vapour pressure over the
6163                        !< multicomponent particle
6164    gamma_out  = 1.0_wp !< i.e. don't alter the ideal mixing ratios if
6165                        !< there's nothing there.
6166!       
6167!-- 1) - COMPOSITION DEFINITIONS
6168!
6169!-- a) Inorganic ion pairing:
6170!-- In order to calculate the water content, which is also used in
6171!-- calculating vapour pressures, one needs to pair the anions and cations
6172!-- for use in the ZSR mixing rule. The equation provided by Clegg et al.
6173!-- (2001) is used for ion pairing. The solutes chosen comprise of 9
6174!-- inorganic salts and acids which provide a pairing between each anion and
6175!-- cation: (NH4)2SO4, NH4NO3, NH4Cl, Na2SO4, NaNO3, NaCl, H2SO4, HNO3, HCL. 
6176!-- The organic compound is treated as a seperate solute.
6177!-- Ions: 1: H+, 2: NH4+, 3: Na+, 4: SO4(2-), 5: HSO4-, 6: NO3-, 7: Cl-
6178!
6179    charge_sum = ions(1) + ions(2) + ions(3) + 2.0_wp * ions(4) + ions(5) +    &
6180                 ions(6) + ions(7)
6181    nitric_acid       = 0.0_wp   ! HNO3
6182    nitric_acid       = ( 2.0_wp * ions(1) * ions(6) *                         &
6183                        ( ( 1.0_wp / 1.0_wp ) ** 0.5_wp ) ) / ( charge_sum )
6184    hydrochloric_acid = 0.0_wp   ! HCL
6185    hydrochloric_acid = ( 2.0_wp * ions(1) * ions(7) *                         &
6186                        ( ( 1.0_wp / 1.0_wp ) ** 0.5_wp ) ) / ( charge_sum )
6187    sulphuric_acid    = 0.0_wp   ! H2SO4
6188    sulphuric_acid    = ( 2.0_wp * ions(1) * ions(4) *                         &
6189                        ( ( 2.0_wp / 2.0_wp ) ** 0.5_wp ) ) / ( charge_sum )
6190    ammonium_sulphate = 0.0_wp   ! (NH4)2SO4
6191    ammonium_sulphate = ( 2.0_wp * ions(2) * ions(4) *                         &
6192                        ( ( 2.0_wp / 2.0_wp ) ** 0.5_wp ) ) / ( charge_sum ) 
6193    ammonium_nitrate  = 0.0_wp   ! NH4NO3
6194    ammonium_nitrate  = ( 2.0_wp * ions(2) * ions(6) *                         &
6195                        ( ( 1.0_wp / 1.0_wp ) ** 0.5_wp ) ) / ( charge_sum )
6196    ammonium_chloride = 0.0_wp   ! NH4Cl
6197    ammonium_chloride = ( 2.0_wp * ions(2) * ions(7) *                         &
6198                        ( ( 1.0_wp / 1.0_wp ) ** 0.5_wp ) ) / ( charge_sum )   
6199    sodium_sulphate   = 0.0_wp   ! Na2SO4
6200    sodium_sulphate   = ( 2.0_wp * ions(3) * ions(4) *                         &
6201                        ( ( 2.0_wp / 2.0_wp ) ** 0.5_wp ) ) / ( charge_sum )
6202    sodium_nitrate    = 0.0_wp   ! NaNO3
6203    sodium_nitrate    = ( 2.0_wp * ions(3) *ions(6) *                          &
6204                        ( ( 1.0_wp / 1.0_wp ) ** 0.5_wp ) ) / ( charge_sum )
6205    sodium_chloride   = 0.0_wp   ! NaCl
6206    sodium_chloride   = ( 2.0_wp * ions(3) * ions(7) *                         &
6207                        ( ( 1.0_wp / 1.0_wp ) ** 0.5_wp ) ) / ( charge_sum )
6208    solutes = 0.0_wp
6209    solutes = 3.0_wp * sulphuric_acid +   2.0_wp * hydrochloric_acid +         &
6210              2.0_wp * nitric_acid +      3.0_wp * ammonium_sulphate +         &
6211              2.0_wp * ammonium_nitrate + 2.0_wp * ammonium_chloride +         &
6212              3.0_wp * sodium_sulphate +  2.0_wp * sodium_nitrate +            &
6213              2.0_wp * sodium_chloride
6214
6215!
6216!-- b) Inorganic equivalent fractions:
6217!-- These values are calculated so that activity coefficients can be
6218!-- expressed by a linear additive rule, thus allowing more efficient
6219!-- calculations and future expansion (see more detailed description below)               
6220    nitric_acid_eq_frac       = 2.0_wp * nitric_acid / ( solutes )
6221    hydrochloric_acid_eq_frac = 2.0_wp * hydrochloric_acid / ( solutes )
6222    sulphuric_acid_eq_frac    = 3.0_wp * sulphuric_acid / ( solutes )
6223    ammonium_sulphate_eq_frac = 3.0_wp * ammonium_sulphate / ( solutes )
6224    ammonium_nitrate_eq_frac  = 2.0_wp * ammonium_nitrate / ( solutes )
6225    ammonium_chloride_eq_frac = 2.0_wp * ammonium_chloride / ( solutes )
6226    sodium_sulphate_eq_frac   = 3.0_wp * sodium_sulphate / ( solutes )
6227    sodium_nitrate_eq_frac    = 2.0_wp * sodium_nitrate / ( solutes )
6228    sodium_chloride_eq_frac   = 2.0_wp * sodium_chloride / ( solutes )
6229!
6230!-- Inorganic ion molalities
6231    ions_mol(:) = 0.0_wp
6232    ions_mol(1) = ions(1) / ( water_total * 18.01528E-3_wp )   ! H+
6233    ions_mol(2) = ions(2) / ( water_total * 18.01528E-3_wp )   ! NH4+
6234    ions_mol(3) = ions(3) / ( water_total * 18.01528E-3_wp )   ! Na+
6235    ions_mol(4) = ions(4) / ( water_total * 18.01528E-3_wp )   ! SO4(2-)
6236    ions_mol(5) = ions(5) / ( water_total * 18.01528E-3_wp )   ! HSO4(2-)
6237    ions_mol(6) = ions(6) / ( water_total * 18.01528E-3_wp )   !  NO3-
6238    ions_mol(7) = ions(7) / ( water_total * 18.01528E-3_wp )   ! Cl-
6239
6240!--    ***
6241!-- At this point we may need to introduce a method for prescribing H+ when
6242!-- there is no 'real' value for H+..i.e. in the sulphate poor domain
6243!-- This will give a value for solve quadratic proposed by Zaveri et al. 2005
6244!
6245!-- 2) - WATER CALCULATION
6246!
6247!-- a) The water content is calculated using the ZSR rule with solute
6248!-- concentrations calculated using 1a above. Whilst the usual approximation of
6249!-- ZSR relies on binary data consisting of 5th or higher order polynomials, in
6250!-- this code 4 different RH regimes are used, each housing cubic equations for
6251!-- the water associated with each solute listed above. Binary water contents
6252!-- for inorganic components were calculated using AIM online (Clegg et al
6253!-- 1998). The water associated with the organic compound is calculated assuming
6254!-- ideality and that aw = RH.
6255!
6256!-- b) Molality of each inorganic ion and organic solute (initial input) is
6257!-- calculated for use in vapour pressure calculation.
6258!
6259!-- 3) - BISULPHATE ION DISSOCIATION CALCULATION
6260!
6261!-- The dissociation of the bisulphate ion is calculated explicitly. A solution
6262!-- to the equilibrium equation between the bisulphate ion, hydrogen ion and
6263!-- sulphate ion is found using tabulated equilibrium constants (referenced). It
6264!-- is necessary to calculate the activity coefficients of HHSO4 and H2SO4 in a
6265!-- non-iterative manner. These are calculated using the same format as
6266!-- described in 4) below, where both activity coefficients were fit to the
6267!-- output from ADDEM (Topping et al 2005a,b) covering an extensive composition
6268!-- space, providing the activity coefficients and bisulphate ion dissociation
6269!-- as a function of equivalent mole fractions and relative humidity.
6270!
6271!-- NOTE: the flags "binary_case" and "full_complexity" are not used in this
6272!-- prototype. They are used for simplification of the fit expressions when
6273!-- using limited composition regions. This section of code calculates the
6274!-- bisulphate ion concentration
6275!
6276    IF ( ions(1) > 0.0_wp .AND. ions(4) > 0.0_wp ) THEN
6277!       
6278!--    HHSO4:
6279       binary_case = 1
6280       IF ( RH > 0.1_wp  .AND.  RH < 0.9_wp )  THEN
6281          binary_hhso4 = - 4.9521_wp * ( RH**3 ) + 9.2881_wp * ( RH**2 ) -     &
6282                           10.777_wp * RH + 6.0534_wp
6283       ELSEIF ( RH >= 0.9_wp  .AND.  RH < 0.955_wp )  THEN
6284          binary_hhso4 = - 6.3777_wp * RH + 5.962_wp
6285       ELSEIF ( RH >= 0.955_wp  .AND.  RH < 0.99_wp )  THEN
6286          binary_hhso4 = 2367.2_wp * ( RH**3 ) - 6849.7_wp * ( RH**2 ) +       &
6287                         6600.9_wp * RH - 2118.7_wp   
6288       ELSEIF ( RH >= 0.99_wp  .AND.  RH < 0.9999_wp )  THEN
6289          binary_hhso4 = 3E-7_wp * ( RH**5 ) - 2E-5_wp * ( RH**4 ) +           &
6290                         0.0004_wp * ( RH**3 ) - 0.0035_wp * ( RH**2 ) +       &
6291                         0.0123_wp * RH - 0.3025_wp
6292       ENDIF
6293       
6294       IF ( nitric_acid > 0.0_wp )  THEN
6295          HNO3_hhso4 = - 4.2204_wp * ( RH**4 ) + 12.193_wp * ( RH**3 ) -       &
6296                         12.481_wp * ( RH**2 ) + 6.459_wp * RH - 1.9004_wp
6297       ENDIF
6298       
6299       IF ( hydrochloric_acid > 0.0_wp )  THEN
6300          HCL_hhso4 = - 54.845_wp * ( RH**7 ) + 209.54_wp * ( RH**6 ) -        &
6301                        336.59_wp * ( RH**5 ) + 294.21_wp * ( RH**4 ) -        &
6302                        150.07_wp * ( RH**3 ) + 43.767_wp * ( RH**2 ) -        &
6303                        6.5495_wp * RH + 0.60048_wp
6304       ENDIF
6305       
6306       IF ( ammonium_sulphate > 0.0_wp )  THEN
6307          NH42SO4_hhso4 = 16.768_wp * ( RH**3 ) - 28.75_wp * ( RH**2 ) +       &
6308                          20.011_wp * RH - 8.3206_wp
6309       ENDIF
6310       
6311       IF ( ammonium_nitrate > 0.0_wp )  THEN
6312          NH4NO3_hhso4 = - 17.184_wp * ( RH**4 ) + 56.834_wp * ( RH**3 ) -     &
6313                           65.765_wp * ( RH**2 ) + 35.321_wp * RH - 9.252_wp
6314       ENDIF
6315       
6316       IF (ammonium_chloride > 0.0_wp )  THEN
6317          IF ( RH < 0.2_wp .AND. RH >= 0.1_wp )  THEN
6318             NH4Cl_hhso4 = 3.2809_wp * RH - 2.0637_wp
6319          ELSEIF ( RH >= 0.2_wp .AND. RH < 0.99_wp )  THEN
6320             NH4Cl_hhso4 = - 1.2981_wp * ( RH**3 ) + 4.7461_wp * ( RH**2 ) -   &
6321                             2.3269_wp * RH - 1.1259_wp
6322          ENDIF
6323       ENDIF
6324       
6325       IF ( sodium_sulphate > 0.0_wp )  THEN
6326          Na2SO4_hhso4 = 118.87_wp * ( RH**6 ) - 358.63_wp * ( RH**5 ) +       &
6327                         435.85_wp * ( RH**4 ) - 272.88_wp * ( RH**3 ) +       &
6328                         94.411_wp * ( RH**2 ) - 18.21_wp * RH + 0.45935_wp
6329       ENDIF
6330       
6331       IF ( sodium_nitrate > 0.0_wp )  THEN
6332          IF ( RH < 0.2_wp  .AND.  RH >= 0.1_wp )  THEN
6333             NaNO3_hhso4 = 4.8456_wp * RH - 2.5773_wp   
6334          ELSEIF ( RH >= 0.2_wp  .AND.  RH < 0.99_wp )  THEN
6335             NaNO3_hhso4 = 0.5964_wp * ( RH**3 ) - 0.38967_wp * ( RH**2 ) +    &
6336                           1.7918_wp * RH - 1.9691_wp 
6337          ENDIF
6338       ENDIF
6339       
6340       IF ( sodium_chloride > 0.0_wp )  THEN
6341          IF ( RH < 0.2_wp )  THEN
6342             NaCl_hhso4 = 0.51995_wp * RH - 1.3981_wp
6343          ELSEIF ( RH >= 0.2_wp  .AND.  RH < 0.99_wp )  THEN
6344             NaCl_hhso4 = 1.6539_wp * RH - 1.6101_wp
6345          ENDIF
6346       ENDIF
6347       
6348       Ln_hhso4_act = binary_hhso4 +                                           &
6349                      nitric_acid_eq_frac       * HNO3_hhso4 +                 &
6350                      hydrochloric_acid_eq_frac * HCL_hhso4 +                  &
6351                      ammonium_sulphate_eq_frac * NH42SO4_hhso4 +              &
6352                      ammonium_nitrate_eq_frac  * NH4NO3_hhso4 +               &
6353                      ammonium_chloride_eq_frac * NH4Cl_hhso4 +                &
6354                      sodium_sulphate_eq_frac   * Na2SO4_hhso4 +               &
6355                      sodium_nitrate_eq_frac    * NaNO3_hhso4 +                &
6356                      sodium_chloride_eq_frac   * NaCl_hhso4
6357       gamma_hhso4 = EXP( Ln_hhso4_act )   ! molal activity coefficient of HHSO4
6358
6359!--    H2SO4 (sulphuric acid):
6360       IF ( RH >= 0.1_wp  .AND.  RH < 0.9_wp )  THEN
6361          binary_h2so4 = 2.4493_wp * ( RH**2 ) - 6.2326_wp * RH + 2.1763_wp
6362       ELSEIF ( RH >= 0.9_wp  .AND.  RH < 0.98 )  THEN
6363          binary_h2so4 = 914.68_wp * ( RH**3 ) - 2502.3_wp * ( RH**2 ) +       &
6364                         2281.9_wp * RH - 695.11_wp
6365       ELSEIF ( RH >= 0.98  .AND.  RH < 0.9999 )  THEN
6366          binary_h2so4 = 3E-8_wp * ( RH**4 ) - 5E-6_wp * ( RH**3 ) +           &
6367                       0.0003_wp * ( RH**2 ) - 0.0022_wp * RH - 1.1305_wp
6368       ENDIF
6369       
6370       IF ( nitric_acid > 0.0_wp )  THEN
6371          HNO3_h2so4 = - 16.382_wp * ( RH**5 ) + 46.677_wp * ( RH**4 ) -       &
6372                         54.149_wp * ( RH**3 ) + 34.36_wp * ( RH**2 ) -        &
6373                         12.54_wp * RH + 2.1368_wp
6374       ENDIF
6375       
6376       IF ( hydrochloric_acid > 0.0_wp )  THEN
6377          HCL_h2so4 = - 14.409_wp * ( RH**5 ) + 42.804_wp * ( RH**4 ) -        &
6378                         47.24_wp * ( RH**3 ) + 24.668_wp * ( RH**2 ) -        &
6379                        5.8015_wp * RH + 0.084627_wp
6380       ENDIF
6381       
6382       IF ( ammonium_sulphate > 0.0_wp )  THEN
6383          NH42SO4_h2so4 = 66.71_wp * ( RH**5 ) - 187.5_wp * ( RH**4 ) +        &
6384                         210.57_wp * ( RH**3 ) - 121.04_wp * ( RH**2 ) +       &
6385                         39.182_wp * RH - 8.0606_wp
6386       ENDIF
6387       
6388       IF ( ammonium_nitrate > 0.0_wp )  THEN
6389          NH4NO3_h2so4 = - 22.532_wp * ( RH**4 ) + 66.615_wp * ( RH**3 ) -     &
6390                           74.647_wp * ( RH**2 ) + 37.638_wp * RH - 6.9711_wp 
6391       ENDIF
6392       
6393       IF ( ammonium_chloride > 0.0_wp )  THEN
6394          IF ( RH >= 0.1_wp  .AND.  RH < 0.2_wp )  THEN
6395             NH4Cl_h2so4 = - 0.32089_wp * RH + 0.57738_wp
6396          ELSEIF ( RH >= 0.2_wp  .AND.  RH < 0.9_wp )  THEN
6397             NH4Cl_h2so4 = 18.089_wp * ( RH**5 ) - 51.083_wp * ( RH**4 ) +     &
6398                            50.32_wp * ( RH**3 ) - 17.012_wp * ( RH**2 ) -     &
6399                          0.93435_wp * RH + 1.0548_wp
6400          ELSEIF ( RH >= 0.9_wp  .AND.  RH < 0.99_wp )  THEN
6401             NH4Cl_h2so4 = - 1.5749_wp * RH + 1.7002_wp
6402          ENDIF
6403       ENDIF
6404       
6405       IF ( sodium_sulphate > 0.0_wp )  THEN
6406          Na2SO4_h2so4 = 29.843_wp * ( RH**4 ) - 69.417_wp * ( RH**3 ) +       &
6407                         61.507_wp * ( RH**2 ) - 29.874_wp * RH + 7.7556_wp
6408       ENDIF
6409       
6410       IF ( sodium_nitrate > 0.0_wp )  THEN
6411          NaNO3_h2so4 = - 122.37_wp * ( RH**6 ) + 427.43_wp * ( RH**5 ) -      &
6412                          604.68_wp * ( RH**4 ) + 443.08_wp * ( RH**3 ) -      &
6413                          178.61_wp * ( RH**2 ) + 37.242_wp * RH - 1.9564_wp
6414       ENDIF
6415       
6416       IF ( sodium_chloride > 0.0_wp )  THEN
6417          NaCl_h2so4 = - 40.288_wp * ( RH**5 ) + 115.61_wp * ( RH**4 ) -       &
6418                         129.99_wp * ( RH**3 ) + 72.652_wp * ( RH**2 ) -       &
6419                         22.124_wp * RH + 4.2676_wp
6420       ENDIF
6421       
6422       Ln_h2so4_act = binary_h2so4 +                                           &
6423                      nitric_acid_eq_frac       * HNO3_h2so4 +                 &
6424                      hydrochloric_acid_eq_frac * HCL_h2so4 +                  &
6425                      ammonium_sulphate_eq_frac * NH42SO4_h2so4 +              &
6426                      ammonium_nitrate_eq_frac  * NH4NO3_h2so4 +               &
6427                      ammonium_chloride_eq_frac * NH4Cl_h2so4 +                &
6428                      sodium_sulphate_eq_frac   * Na2SO4_h2so4 +               &
6429                      sodium_nitrate_eq_frac    * NaNO3_h2so4 +                &
6430                      sodium_chloride_eq_frac   * NaCl_h2so4                     
6431
6432       gamma_h2so4 = EXP( Ln_h2so4_act )    ! molal activity coefficient
6433!         
6434!--    Export activity coefficients
6435       IF ( gamma_h2so4 > 1.0E-10_wp )  THEN
6436          gamma_out(4) = ( gamma_hhso4**2.0_wp ) / gamma_h2so4
6437       ENDIF
6438       IF ( gamma_hhso4 > 1.0E-10_wp )  THEN
6439          gamma_out(5) = ( gamma_h2so4**3.0_wp ) / ( gamma_hhso4**2.0_wp )
6440       ENDIF
6441!
6442!--    Ionic activity coefficient product
6443       act_product = ( gamma_h2so4**3.0_wp ) / ( gamma_hhso4**2.0_wp )
6444!
6445!--    Solve the quadratic equation (i.e. x in ax**2 + bx + c = 0)
6446       a = 1.0_wp
6447       b = - 1.0_wp * ( ions(4) + ions(1) + ( ( water_total * 18.0E-3_wp ) /   &
6448          ( 99.0_wp * act_product ) ) )
6449       c = ions(4) * ions(1)
6450       root1 = ( ( -1.0_wp * b ) + ( ( ( b**2 ) - 4.0_wp * a * c )**0.5_wp     &
6451               ) ) / ( 2 * a )
6452       root2 = ( ( -1.0_wp * b ) - ( ( ( b**2 ) - 4.0_wp * a * c) **0.5_wp     &
6453               ) ) / ( 2 * a )
6454
6455       IF ( root1 > ions(1)  .OR.  root1 < 0.0_wp )  THEN
6456          root1 = 0.0_wp
6457       ENDIF
6458
6459       IF ( root2 > ions(1)  .OR.  root2 < 0.0_wp )  THEN
6460          root2 = 0.0_wp
6461       ENDIF
6462!         
6463!--    Calculate the new hydrogen ion, bisulphate ion and sulphate ion
6464!--    concentration
6465       hso4_real = 0.0_wp
6466       h_real    = ions(1)
6467       so4_real  = ions(4)
6468       IF ( root1 == 0.0_wp )  THEN
6469          hso4_real = root2
6470       ELSEIF ( root2 == 0.0_wp )  THEN
6471          hso4_real = root1
6472       ENDIF
6473       h_real   = ions(1) - hso4_real
6474       so4_real = ions(4) - hso4_real
6475!
6476!--    Recalculate ion molalities
6477       ions_mol(1) = h_real    / ( water_total * 18.01528E-3_wp )   ! H+
6478       ions_mol(4) = so4_real  / ( water_total * 18.01528E-3_wp )   ! SO4(2-)
6479       ions_mol(5) = hso4_real / ( water_total * 18.01528E-3_wp )   ! HSO4(2-)
6480
6481       h_out    = h_real
6482       hso4_out = hso4_real
6483       so4_out  = so4_real
6484       
6485    ELSEIF ( ions(1) == 0.0_wp  .OR.  ions(4) == 0.0_wp )  THEN
6486       h_out    = ions(1)
6487       hso4_out = 0.0_wp
6488       so4_out  = ions(4)
6489    ENDIF
6490
6491!
6492!-- 4) ACTIVITY COEFFICIENTS -for vapour pressures of HNO3,HCL and NH3
6493!
6494!-- This section evaluates activity coefficients and vapour pressures using the
6495!-- water content calculated above) for each inorganic condensing species:
6496!-- a - HNO3, b - NH3, c - HCL.
6497!-- The following procedure is used:
6498!-- Zaveri et al (2005) found that one could express the variation of activity
6499!-- coefficients linearly in log-space if equivalent mole fractions were used.
6500!-- So, by a taylor series expansion LOG( activity coefficient ) =
6501!--    LOG( binary activity coefficient at a given RH ) +
6502!--    (equivalent mole fraction compound A) *
6503!--    ('interaction' parameter between A and condensing species) +
6504!--    equivalent mole fraction compound B) *
6505!--    ('interaction' parameter between B and condensing species).
6506!-- Here, the interaction parameters have been fit to ADDEM by searching the
6507!-- whole compositon space and fit usign the Levenberg-Marquardt non-linear
6508!-- least squares algorithm.
6509!
6510!-- They are given as a function of RH and vary with complexity ranging from
6511!-- linear to 5th order polynomial expressions, the binary activity coefficients
6512!-- were calculated using AIM online.
6513!-- NOTE: for NH3, no binary activity coefficient was used and the data were fit
6514!-- to the ratio of the activity coefficients for the ammonium and hydrogen
6515!-- ions. Once the activity coefficients are obtained the vapour pressure can be
6516!-- easily calculated using tabulated equilibrium constants (referenced). This
6517!-- procedure differs from that of Zaveri et al (2005) in that it is not assumed
6518!-- one can carry behaviour from binary mixtures in multicomponent systems. To
6519!-- this end we have fit the 'interaction' parameters explicitly to a general
6520!-- inorganic equilibrium model (ADDEM - Topping et al. 2005a,b). Such
6521!-- parameters take into account bisulphate ion dissociation and water content.
6522!-- This also allows us to consider one regime for all composition space, rather
6523!-- than defining sulphate rich and sulphate poor regimes
6524!-- NOTE: The flags "binary_case" and "full_complexity" are not used in this
6525!-- prototype. They are used for simplification of the fit expressions when
6526!-- using limited composition regions.
6527!
6528!-- a) - ACTIVITY COEFF/VAPOUR PRESSURE - HNO3
6529    IF ( ions(1) > 0.0_wp  .AND.  ions(6) > 0.0_wp )  THEN
6530       binary_case = 1
6531       IF ( RH > 0.1_wp  .AND.  RH < 0.98_wp )  THEN
6532          IF ( binary_case == 1 )  THEN
6533             binary_hno3 = 1.8514_wp * ( RH**3 ) - 4.6991_wp * ( RH**2 ) +     &
6534                           1.5514_wp * RH + 0.90236_wp
6535          ELSEIF ( binary_case == 2 )  THEN
6536             binary_hno3 = - 1.1751_wp * ( RH**2 ) - 0.53794_wp * RH +         &
6537                             1.2808_wp
6538          ENDIF
6539       ELSEIF ( RH >= 0.98_wp  .AND.  RH < 0.9999_wp )  THEN
6540          binary_hno3 = 1244.69635941351_wp * ( RH**3 ) -                      &
6541                        2613.93941099991_wp * ( RH**2 ) +                      &
6542                        1525.0684974546_wp * RH -155.946764059316_wp
6543       ENDIF
6544!         
6545!--    Contributions from other solutes
6546       full_complexity = 1
6547       IF ( hydrochloric_acid > 0.0_wp )  THEN   ! HCL
6548          IF ( full_complexity == 1  .OR.  RH < 0.4_wp )  THEN
6549             HCL_hno3 = 16.051_wp * ( RH**4 ) - 44.357_wp * ( RH**3 ) +        &
6550                        45.141_wp * ( RH**2 ) - 21.638_wp * RH + 4.8182_wp
6551          ELSEIF ( full_complexity == 0  .AND.  RH > 0.4_wp )  THEN
6552             HCL_hno3 = - 1.5833_wp * RH + 1.5569_wp
6553          ENDIF
6554       ENDIF
6555       
6556       IF ( sulphuric_acid > 0.0_wp )  THEN   ! H2SO4
6557          IF ( full_complexity == 1  .OR.  RH < 0.4_wp )  THEN
6558             H2SO4_hno3 = - 3.0849_wp * ( RH**3 ) + 5.9609_wp * ( RH**2 ) -    &
6559                             4.468_wp * RH + 1.5658_wp
6560          ELSEIF ( full_complexity == 0  .AND.  RH > 0.4_wp )  THEN
6561             H2SO4_hno3 = - 0.93473_wp * RH + 0.9363_wp
6562          ENDIF
6563       ENDIF
6564       
6565       IF ( ammonium_sulphate > 0.0_wp )  THEN   ! NH42SO4
6566          NH42SO4_hno3 = 16.821_wp * ( RH**3 ) - 28.391_wp * ( RH**2 ) +       &
6567                         18.133_wp * RH - 6.7356_wp
6568       ENDIF
6569       
6570       IF ( ammonium_nitrate > 0.0_wp )  THEN   ! NH4NO3
6571          NH4NO3_hno3 = 11.01_wp * ( RH**3 ) - 21.578_wp * ( RH**2 ) +         &
6572                       14.808_wp * RH - 4.2593_wp
6573       ENDIF
6574       
6575       IF ( ammonium_chloride > 0.0_wp )  THEN   ! NH4Cl
6576          IF ( full_complexity == 1  .OR.  RH <= 0.4_wp )  THEN
6577             NH4Cl_hno3 = - 1.176_wp * ( RH**3 ) + 5.0828_wp * ( RH**2 ) -     &
6578                           3.8792_wp * RH - 0.05518_wp
6579          ELSEIF ( full_complexity == 0  .AND.  RH > 0.4_wp )  THEN
6580             NH4Cl_hno3 = 2.6219_wp * ( RH**2 ) - 2.2609_wp * RH - 0.38436_wp
6581          ENDIF
6582       ENDIF
6583       
6584       IF ( sodium_sulphate > 0.0_wp )  THEN   ! Na2SO4
6585          Na2SO4_hno3 = 35.504_wp * ( RH**4 ) - 80.101_wp * ( RH**3 ) +        &
6586                        67.326_wp * ( RH**2 ) - 28.461_wp * RH + 5.6016_wp
6587       ENDIF
6588       
6589       IF ( sodium_nitrate > 0.0_wp )  THEN   ! NaNO3
6590          IF ( full_complexity == 1 .OR. RH <= 0.4_wp ) THEN
6591             NaNO3_hno3 = 23.659_wp * ( RH**5 ) - 66.917_wp * ( RH**4 ) +      &
6592                          74.686_wp * ( RH**3 ) - 40.795_wp * ( RH**2 ) +      &
6593                          10.831_wp * RH - 1.4701_wp
6594          ELSEIF ( full_complexity == 0  .AND.  RH > 0.4_wp )  THEN
6595             NaNO3_hno3 = 14.749_wp * ( RH**4 ) - 35.237_wp * ( RH**3 ) +      &
6596                          31.196_wp * ( RH**2 ) - 12.076_wp * RH + 1.3605_wp
6597          ENDIF
6598       ENDIF
6599       
6600       IF ( sodium_chloride > 0.0_wp )  THEN   ! NaCl
6601          IF ( full_complexity == 1  .OR.  RH <= 0.4_wp )  THEN
6602             NaCl_hno3 = 13.682_wp * ( RH**4 ) - 35.122_wp * ( RH**3 ) +       &
6603                         33.397_wp * ( RH**2 ) - 14.586_wp * RH + 2.6276_wp
6604          ELSEIF ( full_complexity == 0  .AND.  RH > 0.4_wp )  THEN
6605             NaCl_hno3 = 1.1882_wp * ( RH**3 ) - 1.1037_wp * ( RH**2 ) -       &
6606                         0.7642_wp * RH + 0.6671_wp
6607          ENDIF
6608       ENDIF
6609       
6610       Ln_HNO3_act = binary_hno3 +                                             &
6611                     hydrochloric_acid_eq_frac * HCL_hno3 +                    &
6612                     sulphuric_acid_eq_frac    * H2SO4_hno3 +                  &
6613                     ammonium_sulphate_eq_frac * NH42SO4_hno3 +                &
6614                     ammonium_nitrate_eq_frac  * NH4NO3_hno3 +                 &
6615                     ammonium_chloride_eq_frac * NH4Cl_hno3 +                  &
6616                     sodium_sulphate_eq_frac   * Na2SO4_hno3 +                 &
6617                     sodium_nitrate_eq_frac    * NaNO3_hno3 +                  &
6618                     sodium_chloride_eq_frac   * NaCl_hno3
6619
6620       gamma_hno3   = EXP( Ln_HNO3_act )   ! Molal activity coefficient of HNO3
6621       gamma_out(1) = gamma_hno3
6622!
6623!--    Partial pressure calculation
6624!--    K_hno3 = 2.51 * ( 10**6 ) 
6625!--    K_hno3 = 2.628145923d6 !< calculated by AIM online (Clegg et al 1998)
6626!--    after Chameides (1984) (and NIST database)
6627       K_hno3     = 2.6E6_wp * EXP( 8700.0_wp * henrys_temp_dep) 
6628       Press_HNO3 = ( ions_mol(1) * ions_mol(6) * ( gamma_hno3**2 ) ) /        &
6629                      K_hno3
6630    ENDIF
6631!       
6632!-- b) - ACTIVITY COEFF/VAPOUR PRESSURE - NH3
6633!-- Follow the two solute approach of Zaveri et al. (2005)
6634    IF ( ions(2) > 0.0_wp  .AND.  ions_mol(1) > 0.0_wp )  THEN 
6635!--    NH4HSO4:
6636       binary_nh4hso4 = 56.907_wp * ( RH**6 ) - 155.32_wp * ( RH**5 ) +        &
6637                        142.94_wp * ( RH**4 ) - 32.298_wp * ( RH**3 ) -        &
6638                        27.936_wp * ( RH**2 ) + 19.502_wp * RH - 4.2618_wp
6639       IF ( nitric_acid > 0.0_wp)  THEN   ! HNO3
6640          HNO3_nh4hso4 = 104.8369_wp * ( RH**8 ) - 288.8923_wp * ( RH**7 ) +   &
6641                         129.3445_wp * ( RH**6 ) + 373.0471_wp * ( RH**5 ) -   &
6642                         571.0385_wp * ( RH**4 ) + 326.3528_wp * ( RH**3 ) -   &
6643                           74.169_wp * ( RH**2 ) - 2.4999_wp * RH + 3.17_wp
6644       ENDIF
6645       
6646       IF ( hydrochloric_acid > 0.0_wp)  THEN   ! HCL
6647          HCL_nh4hso4 = - 7.9133_wp * ( RH**8 ) + 126.6648_wp * ( RH**7 ) -    &
6648                        460.7425_wp * ( RH**6 ) + 731.606_wp  * ( RH**5 ) -    &
6649                        582.7467_wp * ( RH**4 ) + 216.7197_wp * ( RH**3 ) -   &
6650                         11.3934_wp * ( RH**2 ) - 17.7728_wp  * RH + 5.75_wp
6651       ENDIF
6652       
6653       IF ( sulphuric_acid > 0.0_wp)  THEN   ! H2SO4
6654          H2SO4_nh4hso4 = 195.981_wp * ( RH**8 ) - 779.2067_wp * ( RH**7 ) +   &
6655                        1226.3647_wp * ( RH**6 ) - 964.0261_wp * ( RH**5 ) +   &
6656                         391.7911_wp * ( RH**4 ) - 84.1409_wp  * ( RH**3 ) +   &
6657                          20.0602_wp * ( RH**2 ) - 10.2663_wp  * RH + 3.5817_wp
6658       ENDIF
6659       
6660       IF ( ammonium_sulphate > 0.0_wp)  THEN   ! NH42SO4
6661          NH42SO4_nh4hso4 = 617.777_wp * ( RH**8 ) - 2547.427_wp * ( RH**7 )   &
6662                        + 4361.6009_wp * ( RH**6 ) - 4003.162_wp * ( RH**5 )   &
6663                        + 2117.8281_wp * ( RH**4 ) - 640.0678_wp * ( RH**3 )   &
6664                        + 98.0902_wp   * ( RH**2 ) - 2.2615_wp  * RH - 2.3811_wp
6665       ENDIF
6666       
6667       IF ( ammonium_nitrate > 0.0_wp)  THEN   ! NH4NO3
6668          NH4NO3_nh4hso4 = - 104.4504_wp * ( RH**8 ) + 539.5921_wp *           &
6669                ( RH**7 ) - 1157.0498_wp * ( RH**6 ) + 1322.4507_wp *          &
6670                ( RH**5 ) - 852.2475_wp * ( RH**4 ) + 298.3734_wp *            &
6671                ( RH**3 ) - 47.0309_wp * ( RH**2 ) + 1.297_wp * RH -           &
6672                0.8029_wp
6673       ENDIF
6674       
6675       IF ( ammonium_chloride > 0.0_wp)  THEN   ! NH4Cl
6676          NH4Cl_nh4hso4 = 258.1792_wp * ( RH**8 ) - 1019.3777_wp *             &
6677             ( RH**7 ) + 1592.8918_wp * ( RH**6 ) - 1221.0726_wp *             &
6678             ( RH**5 ) + 442.2548_wp * ( RH**4 ) - 43.6278_wp *                &
6679             ( RH**3 ) - 7.5282_wp * ( RH**2 ) - 3.8459_wp * RH + 2.2728_wp
6680       ENDIF
6681       
6682       IF ( sodium_sulphate > 0.0_wp)  THEN   ! Na2SO4
6683          Na2SO4_nh4hso4 = 225.4238_wp * ( RH**8 ) - 732.4113_wp *             &
6684               ( RH**7 ) + 843.7291_wp * ( RH**6 ) - 322.7328_wp *             &
6685               ( RH**5 ) - 88.6252_wp * ( RH**4 ) + 72.4434_wp *               &
6686               ( RH**3 ) + 22.9252_wp * ( RH**2 ) - 25.3954_wp * RH +          &
6687               4.6971_wp
6688       ENDIF
6689       
6690       IF ( sodium_nitrate > 0.0_wp)  THEN   ! NaNO3
6691          NaNO3_nh4hso4 = 96.1348_wp * ( RH**8 ) - 341.6738_wp * ( RH**7 ) +   &
6692                         406.5314_wp * ( RH**6 ) - 98.5777_wp * ( RH**5 ) -    &
6693                         172.8286_wp * ( RH**4 ) + 149.3151_wp * ( RH**3 ) -   &
6694                          38.9998_wp * ( RH**2 ) - 0.2251 * RH + 0.4953_wp
6695       ENDIF
6696       
6697       IF ( sodium_chloride > 0.0_wp)  THEN   ! NaCl
6698          NaCl_nh4hso4 = 91.7856_wp * ( RH**8 ) - 316.6773_wp * ( RH**7 ) +    &
6699                        358.2703_wp * ( RH**6 ) - 68.9142 * ( RH**5 ) -        &
6700                        156.5031_wp * ( RH**4 ) + 116.9592_wp * ( RH**3 ) -    &
6701                        22.5271_wp * ( RH**2 ) - 3.7716_wp * RH + 1.56_wp
6702       ENDIF
6703
6704       Ln_NH4HSO4_act = binary_nh4hso4 +                                       &
6705                        nitric_acid_eq_frac       * HNO3_nh4hso4 +             &
6706                        hydrochloric_acid_eq_frac * HCL_nh4hso4 +              &
6707                        sulphuric_acid_eq_frac    * H2SO4_nh4hso4 +            & 
6708                        ammonium_sulphate_eq_frac * NH42SO4_nh4hso4 +          &
6709                        ammonium_nitrate_eq_frac  * NH4NO3_nh4hso4 +           &
6710                        ammonium_chloride_eq_frac * NH4Cl_nh4hso4 +            &
6711                        sodium_sulphate_eq_frac   * Na2SO4_nh4hso4 +           & 
6712                        sodium_nitrate_eq_frac    * NaNO3_nh4hso4 +            &
6713                        sodium_chloride_eq_frac   * NaCl_nh4hso4
6714 
6715       gamma_nh4hso4 = EXP( Ln_NH4HSO4_act ) ! molal act. coefficient of NH4HSO4
6716!--    Molal activity coefficient of NO3-
6717       gamma_out(6)  = gamma_nh4hso4
6718!--    Molal activity coefficient of NH4+       
6719       gamma_nh3     = ( gamma_nh4hso4**2 ) / ( gamma_hhso4**2 )   
6720       gamma_out(3)  = gamma_nh3
6721!       
6722!--    This actually represents the ratio of the ammonium to hydrogen ion
6723!--    activity coefficients (see Zaveri paper) - multiply this by the ratio
6724!--    of the ammonium to hydrogen ion molality and the ratio of appropriate
6725!--    equilibrium constants
6726!
6727!--    Equilibrium constants
6728!--    Kh = 57.64d0    ! Zaveri et al. (2005)
6729       Kh = 5.8E1_wp * EXP( 4085.0_wp * henrys_temp_dep )   ! after Chameides
6730!                                                   ! (1984) (and NIST database)
6731!--    Knh4 = 1.81E-5_wp    ! Zaveri et al. (2005)
6732       Knh4 = 1.7E-5_wp * EXP( -4325.0_wp * henrys_temp_dep )   ! Chameides
6733                                                                ! (1984)
6734!--    Kw = 1.01E-14_wp    ! Zaveri et al (2005)
6735       Kw = 1.E-14_wp * EXP( -6716.0_wp * henrys_temp_dep )   ! Chameides
6736                                                              ! (1984)
6737!
6738       molality_ratio_nh3 = ions_mol(2) / ions_mol(1)
6739!--    Partial pressure calculation       
6740       Press_NH3 = molality_ratio_nh3 * gamma_nh3 * ( Kw / ( Kh * Knh4 ) )
6741   
6742    ENDIF
6743!       
6744!-- c) - ACTIVITY COEFF/VAPOUR PRESSURE - HCL
6745    IF ( ions(1) > 0.0_wp  .AND.  ions(7) > 0.0_wp )  THEN
6746       binary_case = 1
6747       IF ( RH > 0.1_wp  .AND.  RH < 0.98 )  THEN
6748          IF ( binary_case == 1 )  THEN
6749             binary_hcl = - 5.0179_wp * ( RH**3 ) + 9.8816_wp * ( RH**2 ) -    &
6750                            10.789_wp * RH + 5.4737_wp
6751          ELSEIF ( binary_case == 2 )  THEN
6752             binary_hcl = - 4.6221_wp * RH + 4.2633_wp
6753          ENDIF
6754       ELSEIF ( RH >= 0.98_wp  .AND.  RH < 0.9999_wp )  THEN
6755          binary_hcl = 775.6111008626_wp * ( RH**3 ) - 2146.01320888771_wp *   &
6756                     ( RH**2 ) + 1969.01979670259_wp *  RH - 598.878230033926_wp
6757       ENDIF
6758    ENDIF
6759   
6760    IF ( nitric_acid > 0.0_wp )  THEN   ! HNO3
6761       IF ( full_complexity == 1  .OR.  RH <= 0.4_wp )  THEN
6762          HNO3_hcl = 9.6256_wp * ( RH**4 ) - 26.507_wp * ( RH**3 ) +           &
6763                     27.622_wp * ( RH**2 ) - 12.958_wp * RH + 2.2193_wp
6764       ELSEIF ( full_complexity == 0  .AND.  RH > 0.4_wp )  THEN
6765          HNO3_hcl = 1.3242_wp * ( RH**2 ) - 1.8827_wp * RH + 0.55706_wp
6766       ENDIF
6767    ENDIF
6768   
6769    IF ( sulphuric_acid > 0.0_wp )  THEN   ! H2SO4
6770       IF ( full_complexity == 1  .OR.  RH <= 0.4 )  THEN
6771          H2SO4_hcl = 1.4406_wp * ( RH**3 ) - 2.7132_wp * ( RH**2 ) +          &
6772                       1.014_wp * RH + 0.25226_wp
6773       ELSEIF ( full_complexity == 0 .AND. RH > 0.4_wp ) THEN
6774          H2SO4_hcl = 0.30993_wp * ( RH**2 ) - 0.99171_wp * RH + 0.66913_wp
6775       ENDIF
6776    ENDIF
6777   
6778    IF ( ammonium_sulphate > 0.0_wp )  THEN   ! NH42SO4
6779       NH42SO4_hcl = 22.071_wp * ( RH**3 ) - 40.678_wp * ( RH**2 ) +           &
6780                     27.893_wp * RH - 9.4338_wp
6781    ENDIF
6782   
6783    IF ( ammonium_nitrate > 0.0_wp )  THEN   ! NH4NO3
6784       NH4NO3_hcl = 19.935_wp * ( RH**3 ) - 42.335_wp * ( RH**2 ) +            &
6785                    31.275_wp * RH - 8.8675_wp
6786    ENDIF
6787   
6788    IF ( ammonium_chloride > 0.0_wp )  THEN   ! NH4Cl
6789       IF ( full_complexity == 1  .OR.  RH <= 0.4_wp )  THEN
6790          NH4Cl_hcl = 2.8048_wp * ( RH**3 ) - 4.3182_wp * ( RH**2 ) +          &
6791                      3.1971_wp * RH - 1.6824_wp
6792       ELSEIF ( full_complexity == 0  .AND.  RH > 0.4_wp )  THEN
6793          NH4Cl_hcl = 1.2304_wp * ( RH**2 ) - 0.18262_wp * RH - 1.0643_wp
6794       ENDIF
6795    ENDIF
6796   
6797    IF ( sodium_sulphate > 0.0_wp )  THEN   ! Na2SO4
6798       Na2SO4_hcl = 36.104_wp * ( RH**4 ) - 78.658_wp * ( RH**3 ) +            &
6799                    63.441_wp * ( RH**2 ) - 26.727_wp * RH + 5.7007_wp
6800    ENDIF
6801   
6802    IF ( sodium_nitrate > 0.0_wp )  THEN   ! NaNO3
6803       IF ( full_complexity == 1  .OR.  RH <= 0.4_wp )  THEN
6804          NaNO3_hcl = 54.471_wp * ( RH**5 ) - 159.42_wp * ( RH**4 ) +          &
6805                      180.25_wp * ( RH**3 ) - 98.176_wp * ( RH**2 ) +          &
6806                      25.309_wp * RH - 2.4275_wp
6807       ELSEIF ( full_complexity == 0  .AND.  RH > 0.4_wp )  THEN
6808          NaNO3_hcl = 21.632_wp * ( RH**4 ) - 53.088_wp * ( RH**3 ) +          &
6809                      47.285_wp * ( RH**2 ) - 18.519_wp * RH + 2.6846_wp
6810       ENDIF
6811    ENDIF
6812   
6813    IF ( sodium_chloride > 0.0_wp )  THEN   ! NaCl
6814       IF ( full_complexity == 1  .OR.  RH <= 0.4_wp )  THEN
6815          NaCl_hcl = 5.4138_wp * ( RH**4 ) - 12.079_wp * ( RH**3 ) +           &
6816                      9.627_wp * ( RH**2 ) - 3.3164_wp * RH + 0.35224_wp
6817       ELSEIF ( full_complexity == 0  .AND.  RH > 0.4_wp )  THEN
6818          NaCl_hcl = 2.432_wp * ( RH**3 ) - 4.3453_wp * ( RH**2 ) +            &
6819                    2.3834_wp * RH - 0.4762_wp
6820       ENDIF
6821    ENDIF
6822             
6823    Ln_HCL_act = binary_hcl +                                                  &
6824                 nitric_acid_eq_frac       * HNO3_hcl +                        &
6825                 sulphuric_acid_eq_frac    * H2SO4_hcl +                       &
6826                 ammonium_sulphate_eq_frac * NH42SO4_hcl +                     &
6827                 ammonium_nitrate_eq_frac  * NH4NO3_hcl +                      &
6828                 ammonium_chloride_eq_frac * NH4Cl_hcl +                       &
6829                 sodium_sulphate_eq_frac   * Na2SO4_hcl +                      &
6830                 sodium_nitrate_eq_frac    * NaNO3_hcl +                       &
6831                 sodium_chloride_eq_frac   * NaCl_hcl
6832
6833     gamma_hcl    = EXP( Ln_HCL_act )   ! Molal activity coefficient
6834     gamma_out(2) = gamma_hcl
6835!     
6836!--  Equilibrium constant after Wagman et al. (1982) (and NIST database)
6837     K_hcl = 2E6_wp * EXP( 9000.0_wp * henrys_temp_dep )   
6838                                                   
6839     Press_HCL = ( ions_mol(1) * ions_mol(7) * ( gamma_hcl**2 ) ) / K_hcl
6840!
6841!-- 5) Ion molility output
6842    mols_out = ions_mol
6843!
6844!-- REFERENCES
6845!-- Clegg et al. (1998) A Thermodynamic Model of the System
6846!--    H+-NH4+-Na+-SO42- -NO3--Cl--H2O at 298.15 K, J. Phys. Chem., 102A,     
6847!--    2155-2171.
6848!-- Clegg et al. (2001) Thermodynamic modelling of aqueous aerosols containing
6849!--    electrolytes and dissolved organic compounds. Journal of Aerosol Science
6850!--    2001;32(6):713-738.
6851!-- Topping et al. (2005a) A curved multi-component aerosol hygroscopicity model
6852!--    framework: Part 1 - Inorganic compounds. Atmospheric Chemistry and
6853!--    Physics 2005;5:1205-1222.
6854!-- Topping et al. (2005b) A curved multi-component aerosol hygroscopicity model
6855!--    framework: Part 2 - Including organic compounds. Atmospheric Chemistry
6856!--    and Physics 2005;5:1223-1242.
6857!-- Wagman et al. (1982). The NBS tables of chemical thermodynamic properties:
6858!--    selected values for inorganic and C₁ and C₂ organic substances in SI
6859!--    units (book)
6860!-- Zaveri et al. (2005). A new method for multicomponent activity coefficients
6861!--    of electrolytes in aqueous atmospheric aerosols, JGR, 110, D02201, 2005.
6862 END SUBROUTINE inorganic_pdfite
6863 
6864!------------------------------------------------------------------------------!
6865! Description:
6866! ------------
6867!> Update the particle size distribution. Put particles into corrects bins.
6868!>
6869!> Moving-centre method assumed, i.e. particles are allowed to grow to their
6870!> exact size as long as they are not crossing the fixed diameter bin limits.
6871!> If the particles in a size bin cross the lower or upper diameter limit, they
6872!> are all moved to the adjacent diameter bin and their volume is averaged with
6873!> the particles in the new bin, which then get a new diameter.
6874!
6875!> Moving-centre method minimises numerical diffusion.
6876!------------------------------------------------------------------------------!     
6877 SUBROUTINE distr_update( paero )
6878   
6879    IMPLICIT NONE
6880
6881!-- Input and output variables
6882    TYPE(t_section), INTENT(inout) ::  paero(fn2b) !< Aerosols particle
6883                                    !< size distribution and properties
6884!-- Local variables
6885    INTEGER(iwp) ::  b !< loop index
6886    INTEGER(iwp) ::  mm !< loop index
6887    INTEGER(iwp) ::  counti
6888    LOGICAL  ::  within_bins !< logical (particle belongs to the bin?)   
6889    REAL(wp) ::  znfrac !< number fraction to be moved to the larger bin
6890    REAL(wp) ::  zvfrac !< volume fraction to be moved to the larger bin
6891    REAL(wp) ::  zVexc  !< Volume in the grown bin which exceeds the bin
6892                        !< upper limit   
6893    REAL(wp) ::  zVihi  !< particle volume at the high end of the bin   
6894    REAL(wp) ::  zVilo  !< particle volume at the low end of the bin     
6895    REAL(wp) ::  zvpart !< particle volume (m3)   
6896    REAL(wp) ::  zVrat  !< volume ratio of a size bin
6897   
6898    zvpart = 0.0_wp
6899    zvfrac = 0.0_wp
6900
6901    within_bins = .FALSE.
6902   
6903!
6904!-- Check if the volume of the bin is within bin limits after update
6905    counti = 0
6906    DO  WHILE ( .NOT. within_bins )
6907       within_bins = .TRUE.
6908
6909       DO  b = fn2b-1, in1a, -1
6910          mm = 0
6911          IF ( paero(b)%numc > nclim )  THEN
6912
6913             zvpart = 0.0_wp
6914             zvfrac = 0.0_wp
6915
6916             IF ( b == fn2a )  CYCLE 
6917!
6918!--          Dry volume
6919             zvpart = SUM( paero(b)%volc(1:7) ) / paero(b)%numc 
6920!
6921!--          Smallest bin cannot decrease
6922             IF ( paero(b)%vlolim > zvpart  .AND.  b == in1a ) CYCLE
6923!
6924!--          Decreasing bins
6925             IF ( paero(b)%vlolim > zvpart )  THEN
6926                mm = b - 1
6927                IF ( b == in2b )  mm = fn1a    ! 2b goes to 1a
6928               
6929                paero(mm)%numc = paero(mm)%numc + paero(b)%numc
6930                paero(b)%numc = 0.0_wp
6931                paero(mm)%volc(:) = paero(mm)%volc(:) + paero(b)%volc(:) 
6932                paero(b)%volc(:) = 0.0_wp
6933                CYCLE
6934             ENDIF
6935!
6936!--          If size bin has not grown, cycle
6937!--          Changed by Mona: compare to the arithmetic mean volume, as done
6938!--          originally. Now particle volume is derived from the geometric mean
6939!--          diameter, not arithmetic (see SUBROUTINE set_sizebins).
6940             IF ( zvpart <= api6 * ( ( aero(b)%vhilim + aero(b)%vlolim ) /     &
6941                  ( 2.0_wp * api6 ) ) )  CYCLE 
6942             IF ( ABS( zvpart - api6 * paero(b)%dmid ** 3.0_wp ) < &
6943                  1.0E-35_wp )  CYCLE  ! Mona: to avoid precision problems
6944!                   
6945!--          Volume ratio of the size bin
6946             zVrat = paero(b)%vhilim / paero(b)%vlolim
6947!--          Particle volume at the low end of the bin
6948             zVilo = 2.0_wp * zvpart / ( 1.0_wp + zVrat )
6949!--          Particle volume at the high end of the bin
6950             zVihi = zVrat * zVilo
6951!--          Volume in the grown bin which exceeds the bin upper limit
6952             zVexc = 0.5_wp * ( zVihi + paero(b)%vhilim )
6953!--          Number fraction to be moved to the larger bin
6954             znfrac = MIN( 1.0_wp, ( zVihi - paero(b)%vhilim) /                &
6955                           ( zVihi - zVilo ) )
6956!--          Volume fraction to be moved to the larger bin
6957             zvfrac = MIN( 0.99_wp, znfrac * zVexc / zvpart )
6958             IF ( zvfrac < 0.0_wp )  THEN
6959                message_string = 'Error: zvfrac < 0'
6960                CALL message( 'salsa_mod: distr_update', 'SA0050',             &
6961                              1, 2, 0, 6, 0 )
6962             ENDIF
6963!
6964!--          Update bin
6965             mm = b + 1
6966!--          Volume (cm3/cm3)
6967             paero(mm)%volc(:) = paero(mm)%volc(:) + znfrac * paero(b)%numc *  &
6968                                 zVexc * paero(b)%volc(:) /                    &
6969                                 SUM( paero(b)%volc(1:7) )
6970             paero(b)%volc(:) = paero(b)%volc(:) - znfrac * paero(b)%numc *    &
6971                                 zVexc * paero(b)%volc(:) /                    &
6972                                 SUM( paero(b)%volc(1:7) )
6973
6974!--          Number concentration (#/m3)
6975             paero(mm)%numc = paero(mm)%numc + znfrac * paero(b)%numc
6976             paero(b)%numc = paero(b)%numc * ( 1.0_wp - znfrac )
6977
6978          ENDIF     ! nclim
6979         
6980          IF ( paero(b)%numc > nclim )   THEN
6981             zvpart = SUM( paero(b)%volc(1:7) ) / paero(b)%numc 
6982             within_bins = ( paero(b)%vlolim < zvpart  .AND.                  &
6983                             zvpart < paero(b)%vhilim )
6984          ENDIF
6985
6986       ENDDO ! - b
6987
6988       counti = counti + 1
6989       IF ( counti > 100 )  THEN
6990          message_string = 'Error: Aerosol bin update not converged'
6991          CALL message( 'salsa_mod: distr_update', 'SA0051', 1, 2, 0, 6, 0 )
6992       ENDIF
6993
6994    ENDDO ! - within bins
6995   
6996 END SUBROUTINE distr_update
6997     
6998!------------------------------------------------------------------------------!
6999! Description:
7000! ------------
7001!> salsa_diagnostics: Update properties for the current timestep:
7002!>
7003!> Juha Tonttila, FMI, 2014
7004!> Tomi Raatikainen, FMI, 2016
7005!------------------------------------------------------------------------------!
7006 SUBROUTINE salsa_diagnostics( i, j )
7007 
7008    USE arrays_3d,                                                             &
7009        ONLY:  p, pt, zu
7010       
7011    USE basic_constants_and_equations_mod,                                     &
7012        ONLY: g
7013   
7014    USE control_parameters,                                                    &
7015        ONLY:  pt_surface, surface_pressure
7016       
7017    USE cpulog,                                                                &
7018        ONLY:  cpu_log, log_point_s
7019
7020    IMPLICIT NONE
7021   
7022    INTEGER(iwp), INTENT(in) ::  i  !<
7023    INTEGER(iwp), INTENT(in) ::  j  !<   
7024
7025    INTEGER(iwp) ::  b !<
7026    INTEGER(iwp) ::  c  !<
7027    INTEGER(iwp) ::  gt  !<
7028    INTEGER(iwp) ::  k  !<
7029    INTEGER(iwp) ::  nc !<
7030    REAL(wp), DIMENSION(nzb:nzt+1) ::  flag         !< flag to mask topography
7031    REAL(wp), DIMENSION(nzb:nzt+1) ::  flag_zddry   !< flag to mask zddry
7032    REAL(wp), DIMENSION(nzb:nzt+1) ::  in_adn       !< air density (kg/m3)   
7033    REAL(wp), DIMENSION(nzb:nzt+1) ::  in_p         !< pressure
7034    REAL(wp), DIMENSION(nzb:nzt+1) ::  in_t         !< temperature (K)   
7035    REAL(wp), DIMENSION(nzb:nzt+1) ::  mcsum        !< sum of mass concentration
7036    REAL(wp), DIMENSION(nzb:nzt+1) ::  ppm_to_nconc !< Conversion factor
7037                                                    !< from ppm to #/m3
7038    REAL(wp), DIMENSION(nzb:nzt+1) ::  zddry  !<
7039    REAL(wp), DIMENSION(nzb:nzt+1) ::  zvol   !<
7040   
7041    flag_zddry   = 0.0_wp
7042    in_adn       = 0.0_wp
7043    in_p         = 0.0_wp
7044    in_t         = 0.0_wp
7045    ppm_to_nconc = 1.0_wp
7046    zddry        = 0.0_wp
7047    zvol         = 0.0_wp
7048   
7049    CALL cpu_log( log_point_s(94), 'salsa diagnostics ', 'start' )
7050
7051!             
7052!-- Calculate thermodynamic quantities needed in SALSA
7053    CALL salsa_thrm_ij( i, j, p_ij=in_p, temp_ij=in_t, adn_ij=in_adn )       
7054!
7055!-- Calculate conversion factors for gas concentrations
7056    ppm_to_nconc = for_ppm_to_nconc * in_p / in_t
7057!
7058!-- Predetermine flag to mask topography
7059    flag = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(:,j,i), 0 ) ) 
7060   
7061    DO  b = 1, nbins   ! aerosol size bins
7062!             
7063!--    Remove negative values
7064       aerosol_number(b)%conc(:,j,i) = MAX( nclim,                             &
7065                                       aerosol_number(b)%conc(:,j,i) ) * flag
7066       mcsum = 0.0_wp   ! total mass concentration
7067       DO  c = 1, ncc_tot
7068!             
7069!--       Remove negative concentrations
7070          aerosol_mass((c-1)*nbins+b)%conc(:,j,i) = MAX( mclim,                &
7071                                     aerosol_mass((c-1)*nbins+b)%conc(:,j,i) ) &
7072                                     * flag
7073          mcsum = mcsum + aerosol_mass((c-1)*nbins+b)%conc(:,j,i) * flag
7074       ENDDO         
7075!               
7076!--    Check that number and mass concentration match qualitatively
7077       IF ( ANY ( aerosol_number(b)%conc(:,j,i) > nclim  .AND.                 &
7078                  mcsum <= 0.0_wp ) )                                          &
7079       THEN
7080          DO  k = nzb+1, nzt
7081             IF ( aerosol_number(b)%conc(k,j,i) > nclim  .AND.                 &
7082               mcsum(k) <= 0.0_wp ) &
7083             THEN
7084                aerosol_number(b)%conc(k,j,i) = nclim * flag(k)
7085                DO  c = 1, ncc_tot
7086                   aerosol_mass((c-1)*nbins+b)%conc(k,j,i) = mclim * flag(k)
7087                ENDDO
7088             ENDIF
7089          ENDDO
7090       ENDIF
7091!             
7092!--    Update aerosol particle radius
7093       CALL bin_mixrat( 'dry', b, i, j, zvol )
7094       zvol = zvol / arhoh2so4    ! Why on sulphate?
7095!                   
7096!--    Particles smaller then 0.1 nm diameter are set to zero
7097       zddry = ( zvol / MAX( nclim, aerosol_number(b)%conc(:,j,i) ) / api6 )** &
7098               ( 1.0_wp / 3.0_wp )
7099       flag_zddry = MERGE( 1.0_wp, 0.0_wp, ( zddry < 1.0E-10_wp  .AND.         &
7100                                       aerosol_number(b)%conc(:,j,i) > nclim ) )
7101!                   
7102!--    Volatile species to the gas phase
7103       IF ( is_used( prtcl, 'SO4' ) .AND. lscndgas )  THEN
7104          nc = get_index( prtcl, 'SO4' )
7105          c = ( nc - 1 ) * nbins + b                     
7106          IF ( salsa_gases_from_chem )  THEN
7107             chem_species( gas_index_chem(1) )%conc(:,j,i) =                   &
7108                               chem_species( gas_index_chem(1) )%conc(:,j,i) + &
7109                               aerosol_mass(c)%conc(:,j,i) * avo * flag *      &
7110                               flag_zddry / ( amh2so4 * ppm_to_nconc ) 
7111          ELSE
7112             salsa_gas(1)%conc(:,j,i) = salsa_gas(1)%conc(:,j,i) +             &
7113                                        aerosol_mass(c)%conc(:,j,i) / amh2so4 *&
7114                                        avo * flag * flag_zddry
7115          ENDIF
7116       ENDIF
7117       IF ( is_used( prtcl, 'OC' )  .AND.  lscndgas )  THEN
7118          nc = get_index( prtcl, 'OC' )
7119          c = ( nc - 1 ) * nbins + b
7120          IF ( salsa_gases_from_chem )  THEN
7121             chem_species( gas_index_chem(5) )%conc(:,j,i) =                   &
7122                               chem_species( gas_index_chem(5) )%conc(:,j,i) + &
7123                               aerosol_mass(c)%conc(:,j,i) * avo * flag *      &
7124                               flag_zddry / ( amoc * ppm_to_nconc ) 
7125          ELSE                         
7126             salsa_gas(5)%conc(:,j,i) = salsa_gas(5)%conc(:,j,i) + &
7127                                        aerosol_mass(c)%conc(:,j,i) / amoc *   &
7128                                        avo * flag * flag_zddry
7129          ENDIF
7130       ENDIF
7131       IF ( is_used( prtcl, 'NO' )  .AND.  lscndgas )  THEN
7132          nc = get_index( prtcl, 'NO' )
7133          c = ( nc - 1 ) * nbins + b                     
7134          IF ( salsa_gases_from_chem )  THEN
7135                chem_species( gas_index_chem(2) )%conc(:,j,i) =                &
7136                               chem_species( gas_index_chem(2) )%conc(:,j,i) + &
7137                               aerosol_mass(c)%conc(:,j,i) * avo * flag *      &
7138                               flag_zddry / ( amhno3 * ppm_to_nconc )                   
7139          ELSE
7140             salsa_gas(2)%conc(:,j,i) = salsa_gas(2)%conc(:,j,i) +             &
7141                                        aerosol_mass(c)%conc(:,j,i) / amhno3 * &
7142                                        avo * flag * flag_zddry
7143          ENDIF
7144       ENDIF
7145       IF ( is_used( prtcl, 'NH' )  .AND.  lscndgas )  THEN
7146          nc = get_index( prtcl, 'NH' )
7147          c = ( nc - 1 ) * nbins + b                     
7148          IF ( salsa_gases_from_chem )  THEN
7149                chem_species( gas_index_chem(3) )%conc(:,j,i) =                &
7150                               chem_species( gas_index_chem(3) )%conc(:,j,i) + &
7151                               aerosol_mass(c)%conc(:,j,i) * avo * flag *      &
7152                               flag_zddry / ( amnh3 * ppm_to_nconc )                         
7153          ELSE
7154             salsa_gas(3)%conc(:,j,i) = salsa_gas(3)%conc(:,j,i) +             &
7155                                        aerosol_mass(c)%conc(:,j,i) / amnh3 *  &
7156                                        avo * flag * flag_zddry
7157          ENDIF
7158       ENDIF
7159!                     
7160!--    Mass and number to zero (insoluble species and water are lost)
7161       DO  c = 1, ncc_tot
7162          aerosol_mass((c-1)*nbins+b)%conc(:,j,i) = MERGE( mclim * flag,       &
7163                                      aerosol_mass((c-1)*nbins+b)%conc(:,j,i), &
7164                                      flag_zddry > 0.0_wp )
7165       ENDDO
7166       aerosol_number(b)%conc(:,j,i) = MERGE( nclim * flag,                    &
7167                                              aerosol_number(b)%conc(:,j,i),   &
7168                                              flag_zddry > 0.0_wp )       
7169       Ra_dry(:,j,i,b) = MAX( 1.0E-10_wp, 0.5_wp * zddry )     
7170       
7171    ENDDO
7172    IF ( .NOT. salsa_gases_from_chem )  THEN
7173       DO  gt = 1, ngast
7174          salsa_gas(gt)%conc(:,j,i) = MAX( nclim, salsa_gas(gt)%conc(:,j,i) )  &
7175                                      * flag
7176       ENDDO
7177    ENDIF
7178   
7179    CALL cpu_log( log_point_s(94), 'salsa diagnostics ', 'stop' )
7180
7181 END SUBROUTINE salsa_diagnostics
7182
7183 
7184!
7185!------------------------------------------------------------------------------!
7186! Description:
7187! ------------
7188!> Calculate the tendencies for aerosol number and mass concentrations.
7189!> Cache-optimized.
7190!------------------------------------------------------------------------------!
7191 SUBROUTINE salsa_tendency_ij( id, rs_p, rs, trs_m, i, j, i_omp_start, tn, b,  &
7192                               c, flux_s, diss_s, flux_l, diss_l, rs_init )
7193   
7194    USE advec_ws,                                                              &
7195        ONLY:  advec_s_ws 
7196    USE advec_s_pw_mod,                                                        &
7197        ONLY:  advec_s_pw
7198    USE advec_s_up_mod,                                                        &
7199        ONLY:  advec_s_up
7200    USE arrays_3d,                                                             &
7201        ONLY:  ddzu, hyp, pt, rdf_sc, tend
7202    USE diffusion_s_mod,                                                       &
7203        ONLY:  diffusion_s
7204    USE indices,                                                               &
7205        ONLY:  wall_flags_0
7206    USE pegrid,                                                                &
7207        ONLY:  threads_per_task, myid     
7208    USE surface_mod,                                                           &
7209        ONLY :  surf_def_h, surf_def_v, surf_lsm_h, surf_lsm_v, surf_usm_h,    &
7210                                 surf_usm_v
7211   
7212    IMPLICIT NONE
7213   
7214    CHARACTER (LEN = *) ::  id
7215    INTEGER(iwp) ::  b   !< bin index in derived type aerosol_size_bin   
7216    INTEGER(iwp) ::  c   !< bin index in derived type aerosol_size_bin   
7217    INTEGER(iwp) ::  i   !<
7218    INTEGER(iwp) ::  i_omp_start !<
7219    INTEGER(iwp) ::  j   !<
7220    INTEGER(iwp) ::  k   !<
7221    INTEGER(iwp) ::  nc  !< (c-1)*nbins+b
7222    INTEGER(iwp) ::  tn  !<
7223    REAL(wp), DIMENSION(nzb+1:nzt,nys:nyn,0:threads_per_task-1) ::  diss_l  !<
7224    REAL(wp), DIMENSION(nzb+1:nzt,0:threads_per_task-1)         ::  diss_s  !<
7225    REAL(wp), DIMENSION(nzb+1:nzt,nys:nyn,0:threads_per_task-1) ::  flux_l  !<
7226    REAL(wp), DIMENSION(nzb+1:nzt,0:threads_per_task-1)         ::  flux_s  !<
7227    REAL(wp), DIMENSION(nzb:nzt+1)                              ::  rs_init !<
7228    REAL(wp), DIMENSION(:,:,:), POINTER                         ::  rs_p    !<
7229    REAL(wp), DIMENSION(:,:,:), POINTER                         ::  rs      !<
7230    REAL(wp), DIMENSION(:,:,:), POINTER                         ::  trs_m   !<
7231   
7232    nc = (c-1)*nbins+b   
7233!
7234!-- Tendency-terms for reactive scalar
7235    tend(:,j,i) = 0.0_wp
7236   
7237    IF ( id == 'aerosol_number'  .AND.  lod_aero == 3 )  THEN
7238       tend(:,j,i) = tend(:,j,i) + aerosol_number(b)%source(:,j,i)
7239    ELSEIF ( id == 'aerosol_mass'  .AND.  lod_aero == 3 )  THEN
7240       tend(:,j,i) = tend(:,j,i) + aerosol_mass(nc)%source(:,j,i)
7241    ENDIF
7242!   
7243!-- Advection terms
7244    IF ( timestep_scheme(1:5) == 'runge' )  THEN
7245       IF ( ws_scheme_sca )  THEN
7246          CALL advec_s_ws( i, j, rs, id, flux_s, diss_s, flux_l, diss_l,       &
7247                           i_omp_start, tn )
7248       ELSE
7249          CALL advec_s_pw( i, j, rs )
7250       ENDIF
7251    ELSE
7252       CALL advec_s_up( i, j, rs )
7253    ENDIF
7254!
7255!-- Diffusion terms   
7256    IF ( id == 'aerosol_number' )  THEN
7257       CALL diffusion_s( i, j, rs,                   surf_def_h(0)%answs(:,b), &
7258                           surf_def_h(1)%answs(:,b), surf_def_h(2)%answs(:,b), &
7259                           surf_lsm_h%answs(:,b),    surf_usm_h%answs(:,b),    &
7260                           surf_def_v(0)%answs(:,b), surf_def_v(1)%answs(:,b), &
7261                           surf_def_v(2)%answs(:,b), surf_def_v(3)%answs(:,b), &
7262                           surf_lsm_v(0)%answs(:,b), surf_lsm_v(1)%answs(:,b), &
7263                           surf_lsm_v(2)%answs(:,b), surf_lsm_v(3)%answs(:,b), &
7264                           surf_usm_v(0)%answs(:,b), surf_usm_v(1)%answs(:,b), &
7265                           surf_usm_v(2)%answs(:,b), surf_usm_v(3)%answs(:,b) )
7266!
7267!--    Sedimentation for aerosol number and mass
7268       IF ( lsdepo )  THEN
7269          tend(nzb+1:nzt,j,i) = tend(nzb+1:nzt,j,i) - MAX( 0.0_wp,             &
7270                         ( rs(nzb+2:nzt+1,j,i) * sedim_vd(nzb+2:nzt+1,j,i,b) - &
7271                           rs(nzb+1:nzt,j,i) * sedim_vd(nzb+1:nzt,j,i,b) ) *   &
7272                         ddzu(nzb+1:nzt) ) * MERGE( 1.0_wp, 0.0_wp,            &
7273                         BTEST( wall_flags_0(nzb:nzt-1,j,i), 0 ) )
7274       ENDIF
7275       
7276    ELSEIF ( id == 'aerosol_mass' )  THEN
7277       CALL diffusion_s( i, j, rs,                  surf_def_h(0)%amsws(:,nc), & 
7278                         surf_def_h(1)%amsws(:,nc), surf_def_h(2)%amsws(:,nc), &
7279                         surf_lsm_h%amsws(:,nc),    surf_usm_h%amsws(:,nc),    &
7280                         surf_def_v(0)%amsws(:,nc), surf_def_v(1)%amsws(:,nc), &
7281                         surf_def_v(2)%amsws(:,nc), surf_def_v(3)%amsws(:,nc), &
7282                         surf_lsm_v(0)%amsws(:,nc), surf_lsm_v(1)%amsws(:,nc), &
7283                         surf_lsm_v(2)%amsws(:,nc), surf_lsm_v(3)%amsws(:,nc), &
7284                         surf_usm_v(0)%amsws(:,nc), surf_usm_v(1)%amsws(:,nc), &
7285                         surf_usm_v(2)%amsws(:,nc), surf_usm_v(3)%amsws(:,nc) ) 
7286!
7287!--    Sedimentation for aerosol number and mass
7288       IF ( lsdepo )  THEN
7289          tend(nzb+1:nzt,j,i) = tend(nzb+1:nzt,j,i) - MAX( 0.0_wp,             &
7290                         ( rs(nzb+2:nzt+1,j,i) * sedim_vd(nzb+2:nzt+1,j,i,b) - &
7291                           rs(nzb+1:nzt,j,i) * sedim_vd(nzb+1:nzt,j,i,b) ) *   &
7292                         ddzu(nzb+1:nzt) ) * MERGE( 1.0_wp, 0.0_wp,            &
7293                         BTEST( wall_flags_0(nzb:nzt-1,j,i), 0 ) )
7294       ENDIF                         
7295    ELSEIF ( id == 'salsa_gas' )  THEN
7296       CALL diffusion_s( i, j, rs,                   surf_def_h(0)%gtsws(:,b), &
7297                           surf_def_h(1)%gtsws(:,b), surf_def_h(2)%gtsws(:,b), &
7298                           surf_lsm_h%gtsws(:,b),    surf_usm_h%gtsws(:,b),    &
7299                           surf_def_v(0)%gtsws(:,b), surf_def_v(1)%gtsws(:,b), &
7300                           surf_def_v(2)%gtsws(:,b), surf_def_v(3)%gtsws(:,b), &
7301                           surf_lsm_v(0)%gtsws(:,b), surf_lsm_v(1)%gtsws(:,b), &
7302                           surf_lsm_v(2)%gtsws(:,b), surf_lsm_v(3)%gtsws(:,b), &
7303                           surf_usm_v(0)%gtsws(:,b), surf_usm_v(1)%gtsws(:,b), &
7304                           surf_usm_v(2)%gtsws(:,b), surf_usm_v(3)%gtsws(:,b) ) 
7305    ENDIF
7306!
7307!-- Prognostic equation for a scalar
7308    DO  k = nzb+1, nzt
7309       rs_p(k,j,i) = rs(k,j,i) +  ( dt_3d  * ( tsc(2) * tend(k,j,i) +          &
7310                                               tsc(3) * trs_m(k,j,i) )         &
7311                                             - tsc(5) * rdf_sc(k)              &
7312                                           * ( rs(k,j,i) - rs_init(k) ) )      &
7313                                  * MERGE( 1.0_wp, 0.0_wp,                     &
7314                                           BTEST( wall_flags_0(k,j,i), 0 ) )
7315       IF ( rs_p(k,j,i) < 0.0_wp )  rs_p(k,j,i) = 0.1_wp * rs(k,j,i) 
7316    ENDDO
7317
7318!
7319!-- Calculate tendencies for the next Runge-Kutta step
7320    IF ( timestep_scheme(1:5) == 'runge' )  THEN
7321       IF ( intermediate_timestep_count == 1 )  THEN
7322          DO  k = nzb+1, nzt
7323             trs_m(k,j,i) = tend(k,j,i)
7324          ENDDO
7325       ELSEIF ( intermediate_timestep_count < &
7326                intermediate_timestep_count_max )  THEN
7327          DO  k = nzb+1, nzt
7328             trs_m(k,j,i) = -9.5625_wp * tend(k,j,i) + 5.3125_wp * trs_m(k,j,i)
7329          ENDDO
7330       ENDIF
7331    ENDIF
7332 
7333 END SUBROUTINE salsa_tendency_ij
7334 
7335!
7336!------------------------------------------------------------------------------!
7337! Description:
7338! ------------
7339!> Calculate the tendencies for aerosol number and mass concentrations.
7340!> Vector-optimized.
7341!------------------------------------------------------------------------------!
7342 SUBROUTINE salsa_tendency( id, rs_p, rs, trs_m, b, c, rs_init )
7343   
7344    USE advec_ws,                                                              &
7345        ONLY:  advec_s_ws 
7346    USE advec_s_pw_mod,                                                        &
7347        ONLY:  advec_s_pw
7348    USE advec_s_up_mod,                                                        &
7349        ONLY:  advec_s_up
7350    USE arrays_3d,                                                             &
7351        ONLY:  ddzu, hyp, pt, rdf_sc, tend
7352    USE diffusion_s_mod,                                                       &
7353        ONLY:  diffusion_s
7354    USE indices,                                                               &
7355        ONLY:  wall_flags_0
7356    USE surface_mod,                                                           &
7357        ONLY :  surf_def_h, surf_def_v, surf_lsm_h, surf_lsm_v, surf_usm_h,    &
7358                                 surf_usm_v
7359   
7360    IMPLICIT NONE
7361   
7362    CHARACTER (LEN = *) ::  id
7363    INTEGER(iwp) ::  b   !< bin index in derived type aerosol_size_bin   
7364    INTEGER(iwp) ::  c   !< bin index in derived type aerosol_size_bin   
7365    INTEGER(iwp) ::  i   !<
7366    INTEGER(iwp) ::  j   !<
7367    INTEGER(iwp) ::  k   !<
7368    INTEGER(iwp) ::  nc  !< (c-1)*nbins+b
7369    REAL(wp), DIMENSION(nzb:nzt+1)                              ::  rs_init !<
7370    REAL(wp), DIMENSION(:,:,:), POINTER                         ::  rs_p    !<
7371    REAL(wp), DIMENSION(:,:,:), POINTER                         ::  rs      !<
7372    REAL(wp), DIMENSION(:,:,:), POINTER                         ::  trs_m   !<
7373   
7374    nc = (c-1)*nbins+b   
7375!
7376!-- Tendency-terms for reactive scalar
7377    tend = 0.0_wp
7378   
7379    IF ( id == 'aerosol_number'  .AND.  lod_aero == 3 )  THEN
7380       tend = tend + aerosol_number(b)%source
7381    ELSEIF ( id == 'aerosol_mass'  .AND.  lod_aero == 3 )  THEN
7382       tend = tend + aerosol_mass(nc)%source
7383    ENDIF
7384!   
7385!-- Advection terms
7386    IF ( timestep_scheme(1:5) == 'runge' )  THEN
7387       IF ( ws_scheme_sca )  THEN
7388          CALL advec_s_ws( rs, id )
7389       ELSE
7390          CALL advec_s_pw( rs )
7391       ENDIF
7392    ELSE
7393       CALL advec_s_up( rs )
7394    ENDIF
7395!
7396!-- Diffusion terms   
7397    IF ( id == 'aerosol_number' )  THEN
7398       CALL diffusion_s(   rs,                       surf_def_h(0)%answs(:,b), &
7399                           surf_def_h(1)%answs(:,b), surf_def_h(2)%answs(:,b), &
7400                           surf_lsm_h%answs(:,b),    surf_usm_h%answs(:,b),    &
7401                           surf_def_v(0)%answs(:,b), surf_def_v(1)%answs(:,b), &
7402                           surf_def_v(2)%answs(:,b), surf_def_v(3)%answs(:,b), &
7403                           surf_lsm_v(0)%answs(:,b), surf_lsm_v(1)%answs(:,b), &
7404                           surf_lsm_v(2)%answs(:,b), surf_lsm_v(3)%answs(:,b), &
7405                           surf_usm_v(0)%answs(:,b), surf_usm_v(1)%answs(:,b), &
7406                           surf_usm_v(2)%answs(:,b), surf_usm_v(3)%answs(:,b) )                                 
7407    ELSEIF ( id == 'aerosol_mass' )  THEN
7408       CALL diffusion_s( rs,                        surf_def_h(0)%amsws(:,nc), & 
7409                         surf_def_h(1)%amsws(:,nc), surf_def_h(2)%amsws(:,nc), &
7410                         surf_lsm_h%amsws(:,nc),    surf_usm_h%amsws(:,nc),    &
7411                         surf_def_v(0)%amsws(:,nc), surf_def_v(1)%amsws(:,nc), &
7412                         surf_def_v(2)%amsws(:,nc), surf_def_v(3)%amsws(:,nc), &
7413                         surf_lsm_v(0)%amsws(:,nc), surf_lsm_v(1)%amsws(:,nc), &
7414                         surf_lsm_v(2)%amsws(:,nc), surf_lsm_v(3)%amsws(:,nc), &
7415                         surf_usm_v(0)%amsws(:,nc), surf_usm_v(1)%amsws(:,nc), &
7416                         surf_usm_v(2)%amsws(:,nc), surf_usm_v(3)%amsws(:,nc) )                         
7417    ELSEIF ( id == 'salsa_gas' )  THEN
7418       CALL diffusion_s(   rs,                       surf_def_h(0)%gtsws(:,b), &
7419                           surf_def_h(1)%gtsws(:,b), surf_def_h(2)%gtsws(:,b), &
7420                           surf_lsm_h%gtsws(:,b),    surf_usm_h%gtsws(:,b),    &
7421                           surf_def_v(0)%gtsws(:,b), surf_def_v(1)%gtsws(:,b), &
7422                           surf_def_v(2)%gtsws(:,b), surf_def_v(3)%gtsws(:,b), &
7423                           surf_lsm_v(0)%gtsws(:,b), surf_lsm_v(1)%gtsws(:,b), &
7424                           surf_lsm_v(2)%gtsws(:,b), surf_lsm_v(3)%gtsws(:,b), &
7425                           surf_usm_v(0)%gtsws(:,b), surf_usm_v(1)%gtsws(:,b), &
7426                           surf_usm_v(2)%gtsws(:,b), surf_usm_v(3)%gtsws(:,b) ) 
7427    ENDIF
7428!
7429!-- Prognostic equation for a scalar
7430    DO  i = nxl, nxr
7431       DO  j = nys, nyn
7432          IF ( id == 'salsa_gas'  .AND.  lod_gases == 3 )  THEN
7433             tend(:,j,i) = tend(:,j,i) + salsa_gas(b)%source(:,j,i) *          &
7434                           for_ppm_to_nconc * hyp(:) / pt(:,j,i) * ( hyp(:) /  &
7435                           100000.0_wp )**0.286_wp ! ppm to #/m3
7436          ELSEIF ( id == 'aerosol_mass'  .OR.  id == 'aerosol_number')  THEN
7437!
7438!--          Sedimentation for aerosol number and mass
7439             IF ( lsdepo )  THEN
7440                tend(nzb+1:nzt,j,i) = tend(nzb+1:nzt,j,i) - MAX( 0.0_wp,       &
7441                         ( rs(nzb+2:nzt+1,j,i) * sedim_vd(nzb+2:nzt+1,j,i,b) - &
7442                           rs(nzb+1:nzt,j,i) * sedim_vd(nzb+1:nzt,j,i,b) ) *   &
7443                         ddzu(nzb+1:nzt) ) * MERGE( 1.0_wp, 0.0_wp,            &
7444                         BTEST( wall_flags_0(nzb:nzt-1,j,i), 0 ) )
7445             ENDIF 
7446          ENDIF
7447          DO  k = nzb+1, nzt
7448             rs_p(k,j,i) = rs(k,j,i) +  ( dt_3d  * ( tsc(2) * tend(k,j,i) +    &
7449                                                     tsc(3) * trs_m(k,j,i) )   &
7450                                                   - tsc(5) * rdf_sc(k)        &
7451                                                 * ( rs(k,j,i) - rs_init(k) ) )&
7452                                        * MERGE( 1.0_wp, 0.0_wp,               &
7453                                          BTEST( wall_flags_0(k,j,i), 0 ) )
7454             IF ( rs_p(k,j,i) < 0.0_wp )  rs_p(k,j,i) = 0.1_wp * rs(k,j,i) 
7455          ENDDO
7456       ENDDO
7457    ENDDO
7458
7459!
7460!-- Calculate tendencies for the next Runge-Kutta step
7461    IF ( timestep_scheme(1:5) == 'runge' )  THEN
7462       IF ( intermediate_timestep_count == 1 )  THEN
7463          DO  i = nxl, nxr
7464             DO  j = nys, nyn
7465                DO  k = nzb+1, nzt
7466                   trs_m(k,j,i) = tend(k,j,i)
7467                ENDDO
7468             ENDDO
7469          ENDDO
7470       ELSEIF ( intermediate_timestep_count < &
7471                intermediate_timestep_count_max )  THEN
7472          DO  i = nxl, nxr
7473             DO  j = nys, nyn
7474                DO  k = nzb+1, nzt
7475                   trs_m(k,j,i) =  -9.5625_wp * tend(k,j,i)                    &
7476                                   + 5.3125_wp * trs_m(k,j,i)
7477                ENDDO
7478             ENDDO
7479          ENDDO
7480       ENDIF
7481    ENDIF
7482 
7483 END SUBROUTINE salsa_tendency
7484 
7485!------------------------------------------------------------------------------!
7486! Description:
7487! ------------
7488!> Boundary conditions for prognostic variables in SALSA
7489!------------------------------------------------------------------------------!
7490 SUBROUTINE salsa_boundary_conds
7491 
7492    USE surface_mod,                                                           &
7493        ONLY :  bc_h
7494
7495    IMPLICIT NONE
7496
7497    INTEGER(iwp) ::  b  !< index for aerosol size bins   
7498    INTEGER(iwp) ::  c  !< index for chemical compounds in aerosols
7499    INTEGER(iwp) ::  g  !< idex for gaseous compounds
7500    INTEGER(iwp) ::  i  !< grid index x direction
7501    INTEGER(iwp) ::  j  !< grid index y direction
7502    INTEGER(iwp) ::  k  !< grid index y direction
7503    INTEGER(iwp) ::  kb !< variable to set respective boundary value, depends on
7504                        !< facing.
7505    INTEGER(iwp) ::  l  !< running index boundary type, for up- and downward-
7506                        !< facing walls
7507    INTEGER(iwp) ::  m  !< running index surface elements
7508   
7509!
7510!-- Surface conditions:
7511    IF ( ibc_salsa_b == 0 )  THEN   ! Dirichlet
7512!   
7513!--    Run loop over all non-natural and natural walls. Note, in wall-datatype
7514!--    the k coordinate belongs to the atmospheric grid point, therefore, set
7515!--    s_p at k-1
7516 
7517       DO  l = 0, 1
7518!
7519!--       Set kb, for upward-facing surfaces value at topography top (k-1) is
7520!--       set, for downward-facing surfaces at topography bottom (k+1)
7521          kb = MERGE ( -1, 1, l == 0 )
7522          !$OMP PARALLEL PRIVATE( b, c, g, i, j, k )
7523          !$OMP DO
7524          DO  m = 1, bc_h(l)%ns
7525         
7526             i = bc_h(l)%i(m)
7527             j = bc_h(l)%j(m)
7528             k = bc_h(l)%k(m)
7529             
7530             DO  b = 1, nbins
7531                aerosol_number(b)%conc_p(k+kb,j,i) =                           &
7532                                                aerosol_number(b)%conc(k+kb,j,i)
7533                DO  c = 1, ncc_tot
7534                   aerosol_mass((c-1)*nbins+b)%conc_p(k+kb,j,i) =              &
7535                                      aerosol_mass((c-1)*nbins+b)%conc(k+kb,j,i)
7536                ENDDO
7537             ENDDO
7538             IF ( .NOT. salsa_gases_from_chem )  THEN
7539                DO  g = 1, ngast
7540                   salsa_gas(g)%conc_p(k+kb,j,i) = salsa_gas(g)%conc(k+kb,j,i)
7541                ENDDO
7542             ENDIF
7543             
7544          ENDDO
7545          !$OMP END PARALLEL
7546         
7547       ENDDO
7548   
7549    ELSE   ! Neumann
7550   
7551       DO l = 0, 1
7552!
7553!--       Set kb, for upward-facing surfaces value at topography top (k-1) is
7554!--       set, for downward-facing surfaces at topography bottom (k+1)       
7555          kb = MERGE( -1, 1, l == 0 )
7556          !$OMP PARALLEL PRIVATE( b, c, g, i, j, k )
7557          !$OMP DO
7558          DO  m = 1, bc_h(l)%ns
7559             
7560             i = bc_h(l)%i(m)
7561             j = bc_h(l)%j(m)
7562             k = bc_h(l)%k(m)
7563             
7564             DO  b = 1, nbins
7565                aerosol_number(b)%conc_p(k+kb,j,i) =                           &
7566                                                 aerosol_number(b)%conc_p(k,j,i)
7567                DO  c = 1, ncc_tot
7568                   aerosol_mass((c-1)*nbins+b)%conc_p(k+kb,j,i) =              &
7569                                       aerosol_mass((c-1)*nbins+b)%conc_p(k,j,i)
7570                ENDDO
7571             ENDDO
7572             IF ( .NOT. salsa_gases_from_chem ) THEN
7573                DO  g = 1, ngast
7574                   salsa_gas(g)%conc_p(k+kb,j,i) = salsa_gas(g)%conc_p(k,j,i)
7575                ENDDO
7576             ENDIF
7577               
7578          ENDDO
7579          !$OMP END PARALLEL
7580       ENDDO
7581     
7582    ENDIF
7583
7584!
7585!--Top boundary conditions:
7586    IF ( ibc_salsa_t == 0 )  THEN   ! Dirichlet
7587   
7588       DO  b = 1, nbins
7589          aerosol_number(b)%conc_p(nzt+1,:,:) =                                &
7590                                               aerosol_number(b)%conc(nzt+1,:,:)
7591          DO  c = 1, ncc_tot
7592             aerosol_mass((c-1)*nbins+b)%conc_p(nzt+1,:,:) =                   &
7593                                     aerosol_mass((c-1)*nbins+b)%conc(nzt+1,:,:)
7594          ENDDO
7595       ENDDO
7596       IF ( .NOT. salsa_gases_from_chem )  THEN
7597          DO  g = 1, ngast
7598             salsa_gas(g)%conc_p(nzt+1,:,:) = salsa_gas(g)%conc(nzt+1,:,:)
7599          ENDDO
7600       ENDIF
7601       
7602    ELSEIF ( ibc_salsa_t == 1 )  THEN   ! Neumann
7603   
7604       DO  b = 1, nbins
7605          aerosol_number(b)%conc_p(nzt+1,:,:) =                                &
7606                                               aerosol_number(b)%conc_p(nzt,:,:)
7607          DO  c = 1, ncc_tot
7608             aerosol_mass((c-1)*nbins+b)%conc_p(nzt+1,:,:) =                   &
7609                                     aerosol_mass((c-1)*nbins+b)%conc_p(nzt,:,:)
7610          ENDDO
7611       ENDDO
7612       IF ( .NOT. salsa_gases_from_chem )  THEN
7613          DO  g = 1, ngast
7614             salsa_gas(g)%conc_p(nzt+1,:,:) = salsa_gas(g)%conc_p(nzt,:,:)
7615          ENDDO
7616       ENDIF
7617       
7618    ENDIF
7619!
7620!-- Lateral boundary conditions at the outflow   
7621    IF ( bc_radiation_s )  THEN
7622       DO  b = 1, nbins
7623          aerosol_number(b)%conc_p(:,nys-1,:) = aerosol_number(b)%conc_p(:,nys,:)
7624          DO  c = 1, ncc_tot
7625             aerosol_mass((c-1)*nbins+b)%conc_p(:,nys-1,:) =                   &
7626                                     aerosol_mass((c-1)*nbins+b)%conc_p(:,nys,:)
7627          ENDDO
7628       ENDDO
7629    ELSEIF ( bc_radiation_n )  THEN
7630       DO  b = 1, nbins
7631          aerosol_number(b)%conc_p(:,nyn+1,:) = aerosol_number(b)%conc_p(:,nyn,:)
7632          DO  c = 1, ncc_tot
7633             aerosol_mass((c-1)*nbins+b)%conc_p(:,nyn+1,:) =                   &
7634                                     aerosol_mass((c-1)*nbins+b)%conc_p(:,nyn,:)
7635          ENDDO
7636       ENDDO
7637    ELSEIF ( bc_radiation_l )  THEN
7638       DO  b = 1, nbins
7639          aerosol_number(b)%conc_p(:,nxl-1,:) = aerosol_number(b)%conc_p(:,nxl,:)
7640          DO  c = 1, ncc_tot
7641             aerosol_mass((c-1)*nbins+b)%conc_p(:,nxl-1,:) =                   &
7642                                     aerosol_mass((c-1)*nbins+b)%conc_p(:,nxl,:)
7643          ENDDO
7644       ENDDO
7645    ELSEIF ( bc_radiation_r )  THEN
7646       DO  b = 1, nbins
7647          aerosol_number(b)%conc_p(:,nxr+1,:) = aerosol_number(b)%conc_p(:,nxr,:)
7648          DO  c = 1, ncc_tot
7649             aerosol_mass((c-1)*nbins+b)%conc_p(:,nxr+1,:) =                   &
7650                                     aerosol_mass((c-1)*nbins+b)%conc_p(:,nxr,:)
7651          ENDDO
7652       ENDDO
7653    ENDIF
7654
7655 END SUBROUTINE salsa_boundary_conds
7656
7657!------------------------------------------------------------------------------!
7658! Description:
7659! ------------
7660! Undoing of the previously done cyclic boundary conditions.
7661!------------------------------------------------------------------------------!
7662 SUBROUTINE salsa_boundary_conds_decycle ( sq, sq_init )
7663
7664    IMPLICIT NONE
7665
7666    INTEGER(iwp) ::  boundary !<
7667    INTEGER(iwp) ::  ee !<
7668    INTEGER(iwp) ::  copied !<
7669    INTEGER(iwp) ::  i  !<
7670    INTEGER(iwp) ::  j  !<
7671    INTEGER(iwp) ::  k  !<
7672    INTEGER(iwp) ::  ss !<
7673    REAL(wp), DIMENSION(nzb:nzt+1) ::  sq_init
7674    REAL(wp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  sq
7675    REAL(wp) ::  flag !< flag to mask topography grid points
7676
7677    flag = 0.0_wp
7678!
7679!-- Left and right boundaries
7680    IF ( decycle_lr  .AND.  ( bc_lr_cyc  .OR. bc_lr == 'nested' ) )  THEN
7681   
7682       DO  boundary = 1, 2
7683
7684          IF ( decycle_method(boundary) == 'dirichlet' )  THEN
7685!   
7686!--          Initial profile is copied to ghost and first three layers         
7687             ss = 1
7688             ee = 0
7689             IF ( boundary == 1  .AND.  nxl == 0 )  THEN
7690                ss = nxlg
7691                ee = nxl+2
7692             ELSEIF ( boundary == 2  .AND.  nxr == nx )  THEN
7693                ss = nxr-2
7694                ee = nxrg
7695             ENDIF
7696             
7697             DO  i = ss, ee
7698                DO  j = nysg, nyng
7699                   DO  k = nzb+1, nzt             
7700                      flag = MERGE( 1.0_wp, 0.0_wp,                            &
7701                                    BTEST( wall_flags_0(k,j,i), 0 ) )
7702                      sq(k,j,i) = sq_init(k) * flag
7703                   ENDDO
7704                ENDDO
7705             ENDDO
7706             
7707          ELSEIF ( decycle_method(boundary) == 'neumann' )  THEN
7708!
7709!--          The value at the boundary is copied to the ghost layers to simulate
7710!--          an outlet with zero gradient
7711             ss = 1
7712             ee = 0
7713             IF ( boundary == 1  .AND.  nxl == 0 )  THEN
7714                ss = nxlg
7715                ee = nxl-1
7716                copied = nxl
7717             ELSEIF ( boundary == 2  .AND.  nxr == nx )  THEN
7718                ss = nxr+1
7719                ee = nxrg
7720                copied = nxr
7721             ENDIF
7722             
7723              DO  i = ss, ee
7724                DO  j = nysg, nyng
7725                   DO  k = nzb+1, nzt             
7726                      flag = MERGE( 1.0_wp, 0.0_wp,                            &
7727                                    BTEST( wall_flags_0(k,j,i), 0 ) )
7728                      sq(k,j,i) = sq(k,j,copied) * flag
7729                   ENDDO
7730                ENDDO
7731             ENDDO
7732             
7733          ELSE
7734             WRITE(message_string,*)                                           &
7735                                 'unknown decycling method: decycle_method (', &
7736                     boundary, ') ="' // TRIM( decycle_method(boundary) ) // '"'
7737             CALL message( 'salsa_boundary_conds_decycle', 'SA0029',           &
7738                           1, 2, 0, 6, 0 )
7739          ENDIF
7740       ENDDO
7741    ENDIF
7742   
7743!
7744!-- South and north boundaries
7745     IF ( decycle_ns  .AND.  ( bc_ns_cyc  .OR. bc_ns == 'nested' ) )  THEN
7746   
7747       DO  boundary = 3, 4
7748
7749          IF ( decycle_method(boundary) == 'dirichlet' )  THEN
7750!   
7751!--          Initial profile is copied to ghost and first three layers         
7752             ss = 1
7753             ee = 0
7754             IF ( boundary == 3  .AND.  nys == 0 )  THEN
7755                ss = nysg
7756                ee = nys+2
7757             ELSEIF ( boundary == 4  .AND.  nyn == ny )  THEN
7758                ss = nyn-2
7759                ee = nyng
7760             ENDIF
7761             
7762             DO  i = nxlg, nxrg
7763                DO  j = ss, ee
7764                   DO  k = nzb+1, nzt             
7765                      flag = MERGE( 1.0_wp, 0.0_wp,                            &
7766                                    BTEST( wall_flags_0(k,j,i), 0 ) )
7767                      sq(k,j,i) = sq_init(k) * flag
7768                   ENDDO
7769                ENDDO
7770             ENDDO
7771             
7772          ELSEIF ( decycle_method(boundary) == 'neumann' )  THEN
7773!
7774!--          The value at the boundary is copied to the ghost layers to simulate
7775!--          an outlet with zero gradient
7776             ss = 1
7777             ee = 0
7778             IF ( boundary == 3  .AND.  nys == 0 )  THEN
7779                ss = nysg
7780                ee = nys-1
7781                copied = nys
7782             ELSEIF ( boundary == 4  .AND.  nyn == ny )  THEN
7783                ss = nyn+1
7784                ee = nyng
7785                copied = nyn
7786             ENDIF
7787             
7788              DO  i = nxlg, nxrg
7789                DO  j = ss, ee
7790                   DO  k = nzb+1, nzt             
7791                      flag = MERGE( 1.0_wp, 0.0_wp,                            &
7792                                    BTEST( wall_flags_0(k,j,i), 0 ) )
7793                      sq(k,j,i) = sq(k,copied,i) * flag
7794                   ENDDO
7795                ENDDO
7796             ENDDO
7797             
7798          ELSE
7799             WRITE(message_string,*)                                           &
7800                                 'unknown decycling method: decycle_method (', &
7801                     boundary, ') ="' // TRIM( decycle_method(boundary) ) // '"'
7802             CALL message( 'salsa_boundary_conds_decycle', 'SA0030',           &
7803                           1, 2, 0, 6, 0 )
7804          ENDIF
7805       ENDDO
7806    ENDIF   
7807 
7808 END SUBROUTINE salsa_boundary_conds_decycle
7809
7810!------------------------------------------------------------------------------!
7811! Description:
7812! ------------
7813!> Calculates the total dry or wet mass concentration for individual bins
7814!> Juha Tonttila (FMI) 2015
7815!> Tomi Raatikainen (FMI) 2016
7816!------------------------------------------------------------------------------!
7817 SUBROUTINE bin_mixrat( itype, ibin, i, j, mconc )
7818
7819    IMPLICIT NONE
7820   
7821    CHARACTER(len=*), INTENT(in) ::  itype !< 'dry' or 'wet'
7822    INTEGER(iwp), INTENT(in) ::  ibin   !< index of the chemical component
7823    INTEGER(iwp), INTENT(in) ::  i      !< loop index for x-direction
7824    INTEGER(iwp), INTENT(in) ::  j      !< loop index for y-direction
7825    REAL(wp), DIMENSION(:), INTENT(out) ::  mconc     !< total dry or wet mass
7826                                                      !< concentration
7827                                                     
7828    INTEGER(iwp) ::  c                  !< loop index for mass bin number
7829    INTEGER(iwp) ::  iend               !< end index: include water or not     
7830   
7831!-- Number of components
7832    IF ( itype == 'dry' )  THEN
7833       iend = get_n_comp( prtcl ) - 1 
7834    ELSE IF ( itype == 'wet' )  THEN
7835       iend = get_n_comp( prtcl ) 
7836    ELSE
7837       STOP 'bin_mixrat: Error in itype'
7838    ENDIF
7839
7840    mconc = 0.0_wp
7841   
7842    DO c = ibin, iend*nbins+ibin, nbins !< every nbins'th element
7843       mconc = mconc + aerosol_mass(c)%conc(:,j,i)
7844    ENDDO
7845   
7846 END SUBROUTINE bin_mixrat 
7847
7848!------------------------------------------------------------------------------!
7849!> Description:
7850!> ------------
7851!> Define aerosol fluxes: constant or read from a from file
7852!------------------------------------------------------------------------------!
7853 SUBROUTINE salsa_set_source
7854 
7855 !   USE date_and_time_mod,                                                     &
7856 !       ONLY:  index_dd, index_hh, index_mm
7857#if defined( __netcdf )
7858    USE NETCDF
7859   
7860    USE netcdf_data_input_mod,                                                 &
7861        ONLY:  get_attribute, netcdf_data_input_get_dimension_length,          &
7862               get_variable, open_read_file
7863   
7864    USE surface_mod,                                                           &
7865        ONLY:  surf_def_h, surf_lsm_h, surf_usm_h
7866 
7867    IMPLICIT NONE
7868   
7869    INTEGER(iwp), PARAMETER ::  ndm = 3  !< number of default modes
7870    INTEGER(iwp), PARAMETER ::  ndc = 4  !< number of default categories
7871   
7872    CHARACTER (LEN=10) ::  unita !< Unit of aerosol fluxes
7873    CHARACTER (LEN=10) ::  unitg !< Unit of gaseous fluxes
7874    INTEGER(iwp) ::  b           !< loop index: aerosol number bins
7875    INTEGER(iwp) ::  c           !< loop index: aerosol chemical components
7876    INTEGER(iwp) ::  ee          !< loop index: end
7877    INTEGER(iwp), ALLOCATABLE, DIMENSION(:) ::  eci !< emission category index
7878    INTEGER(iwp) ::  g           !< loop index: gaseous tracers
7879    INTEGER(iwp) ::  i           !< loop index: x-direction   
7880    INTEGER(iwp) ::  id_faero    !< NetCDF id of aerosol source input file
7881    INTEGER(iwp) ::  id_fchem    !< NetCDF id of aerosol source input file                             
7882    INTEGER(iwp) ::  id_sa       !< NetCDF id of variable: source   
7883    INTEGER(iwp) ::  j           !< loop index: y-direction
7884    INTEGER(iwp) ::  k           !< loop index: z-direction
7885    INTEGER(iwp) ::  kg          !< loop index: z-direction (gases)
7886    INTEGER(iwp) ::  n_dt        !< number of time steps in the emission file
7887    INTEGER(iwp) ::  nc_stat     !< local variable for storing the result of
7888                                 !< netCDF calls for error message handling
7889    INTEGER(iwp) ::  nb_file     !< Number of grid-points in file (bins)                                 
7890    INTEGER(iwp) ::  ncat        !< Number of emission categories
7891    INTEGER(iwp) ::  ng_file     !< Number of grid-points in file (gases) 
7892    INTEGER(iwp) ::  num_vars    !< number of variables in input file
7893    INTEGER(iwp) ::  nz_file     !< number of grid-points in file     
7894    INTEGER(iwp) ::  n           !< loop index
7895    INTEGER(iwp) ::  ni          !< loop index
7896    INTEGER(iwp) ::  ss          !< loop index
7897    LOGICAL  ::  netcdf_extend = .FALSE. !< Flag indicating wether netcdf
7898                                         !< topography input file or not   
7899    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:)   :: dum_var_4d !< variable for
7900                                                              !< temporary data                                       
7901    REAL(wp) ::  fillval         !< fill value
7902    REAL(wp) ::  flag            !< flag to mask topography grid points
7903    REAL(wp), DIMENSION(nbins) ::  nsect_emission  !< sectional emission (lod1)
7904    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:) ::  pm_emission  !< aerosol mass
7905                                                             !< emission (lod1)
7906    REAL(wp), DIMENSION(nbins) ::  source_ijka !< aerosol source at (k,j,i)
7907!
7908!-- The default size distribution and mass composition per emission category:
7909!-- 1 = traffic, 2 = road dust, 3 = wood combustion, 4 = other
7910!-- Mass fractions: H2SO4, OC, BC, DU, SS, HNO3, NH3
7911    CHARACTER(LEN=15), DIMENSION(ndc) ::  cat_name_table = &!< emission category
7912                                         (/'road traffic   ','road dust      ',&
7913                                           'wood combustion','other          '/)
7914    REAL(wp), DIMENSION(ndc) ::  avg_density        !< average density
7915    REAL(wp), DIMENSION(ndc) ::  conversion_factor  !< unit conversion factor 
7916                                                    !< for aerosol emissions
7917    REAL(wp), DIMENSION(ndm), PARAMETER ::  dpg_table = & !< mean diameter (mum)
7918                                            (/ 13.5E-3_wp, 1.4_wp, 5.4E-2_wp/)
7919    REAL(wp), DIMENSION(ndm) ::  ntot_table                                       
7920    REAL(wp), DIMENSION(maxspec,ndc), PARAMETER ::  mass_fraction_table =      &
7921       RESHAPE( (/ 0.04_wp, 0.48_wp, 0.48_wp, 0.0_wp,  0.0_wp, 0.0_wp, 0.0_wp, &
7922                   0.0_wp,  0.05_wp, 0.0_wp,  0.95_wp, 0.0_wp, 0.0_wp, 0.0_wp, &
7923                   0.0_wp,  0.5_wp,  0.5_wp,  0.0_wp,  0.0_wp, 0.0_wp, 0.0_wp, &
7924                   0.0_wp,  0.5_wp,  0.5_wp,  0.0_wp,  0.0_wp, 0.0_wp, 0.0_wp  &
7925                /), (/maxspec,ndc/) )         
7926    REAL(wp), DIMENSION(ndm,ndc), PARAMETER ::  PMfrac_table = & !< rel. mass
7927                                     RESHAPE( (/ 0.016_wp, 0.000_wp, 0.984_wp, &
7928                                                 0.000_wp, 1.000_wp, 0.000_wp, &
7929                                                 0.000_wp, 0.000_wp, 1.000_wp, &
7930                                                 1.000_wp, 0.000_wp, 1.000_wp  &
7931                                              /), (/ndm,ndc/) )                                   
7932    REAL(wp), DIMENSION(ndm), PARAMETER ::  sigmag_table = &     !< mode std
7933                                            (/1.6_wp, 1.4_wp, 1.7_wp/) 
7934    avg_density    = 1.0_wp
7935    nb_file        = 0
7936    ng_file        = 0
7937    nsect_emission = 0.0_wp
7938    nz_file        = 0
7939    source_ijka    = 0.0_wp
7940!
7941!-- First gases, if needed:
7942    IF ( .NOT. salsa_gases_from_chem )  THEN   
7943!       
7944!--    Read sources from PIDS_CHEM     
7945       INQUIRE( FILE='PIDS_CHEM' // TRIM( coupling_char ), EXIST=netcdf_extend )
7946       IF ( .NOT. netcdf_extend )  THEN
7947          message_string = 'Input file '// TRIM( 'PIDS_CHEM' ) //              &
7948                           TRIM( coupling_char ) // ' for SALSA missing!'
7949          CALL message( 'salsa_mod: salsa_set_source', 'SA0027', 1, 2, 0, 6, 0 )               
7950       ENDIF   ! netcdf_extend 
7951       
7952       CALL location_message( '    salsa_set_source: NOTE! Gaseous emissions'//&
7953               ' should be provided with following emission indices:'//        &
7954               ' 1=H2SO4, 2=HNO3, 3=NH3, 4=OCNV, 5=OCSV', .TRUE. )
7955       CALL location_message( '    salsa_set_source: No time dependency for '//&
7956                              'gaseous emissions. Use emission_values '//      &
7957                              'directly.', .TRUE. )
7958!
7959!--    Open PIDS_CHEM in read-only mode
7960       CALL open_read_file( 'PIDS_CHEM' // TRIM( coupling_char ), id_fchem )
7961!
7962!--    Inquire the level of detail (lod)
7963       CALL get_attribute( id_fchem, 'lod', lod_gases, .FALSE.,                &
7964                           "emission_values" ) 
7965                           
7966       IF ( lod_gases == 2 )  THEN
7967!                             
7968!--       Index of gaseous compounds
7969          CALL netcdf_data_input_get_dimension_length( id_fchem, ng_file, "nspecies" ) 
7970          IF ( ng_file < 5 )  THEN
7971             message_string = 'Some gaseous emissions missing.'
7972             CALL message( 'salsa_mod: salsa_set_source', 'SA0041',            &
7973                           1, 2, 0, 6, 0 )
7974          ENDIF       
7975!
7976!--       Get number of emission categories 
7977          CALL netcdf_data_input_get_dimension_length( id_fchem, ncat, "ncat" )       
7978!
7979!--       Inquire the unit of gaseous fluxes
7980          CALL get_attribute( id_fchem, 'units', unitg, .FALSE.,               &
7981                              "emission_values")       
7982!
7983!--       Inquire the fill value
7984          CALL get_attribute( id_fchem, '_FillValue', fillval, .FALSE.,        &
7985                              "emission_values" )
7986!       
7987!--       Read surface emission data (x,y) PE-wise   
7988          ALLOCATE( dum_var_4d(ng_file,ncat,nys:nyn,nxl:nxr) )     
7989          CALL get_variable( id_fchem, 'emission_values', dum_var_4d, nxl, nxr,&
7990                             nys, nyn, 0, ncat-1, 0, ng_file-1 )
7991          DO  g = 1, ngast
7992             ALLOCATE( salsa_gas(g)%source(ncat,nys:nyn,nxl:nxr) )
7993             salsa_gas(g)%source = 0.0_wp
7994             salsa_gas(g)%source = salsa_gas(g)%source + dum_var_4d(g,:,:,:)
7995          ENDDO                   
7996!   
7997!--       Set surface fluxes of gaseous compounds on horizontal surfaces.
7998!--       Set fluxes only for either default, land or urban surface.
7999          IF ( .NOT. land_surface  .AND.  .NOT. urban_surface )  THEN
8000             CALL set_gas_flux( surf_def_h(0), ncat, unitg  )
8001          ELSE
8002             CALL set_gas_flux( surf_lsm_h, ncat, unitg  )
8003             CALL set_gas_flux( surf_usm_h, ncat, unitg  )
8004          ENDIF
8005         
8006          DEALLOCATE( dum_var_4d )
8007          DO  g = 1, ngast
8008             DEALLOCATE( salsa_gas(g)%source )
8009          ENDDO
8010       ELSE
8011          message_string = 'Input file PIDS_CHEM needs to have lod = 2 when '//&
8012                           'SALSA is applied but not the chemistry module!'
8013          CALL message( 'salsa_mod: salsa_set_source', 'SA0039', 1, 2, 0, 6, 0 )   
8014       ENDIF             
8015    ENDIF 
8016!       
8017!-- Read sources from PIDS_SALSA       
8018    INQUIRE( FILE='PIDS_SALSA' // TRIM( coupling_char ), EXIST=netcdf_extend )
8019    IF ( .NOT. netcdf_extend )  THEN
8020       message_string = 'Input file '// TRIM( 'PIDS_SALSA' ) //                &
8021                         TRIM( coupling_char ) // ' for SALSA missing!'
8022       CALL message( 'salsa_mod: salsa_set_source', 'SA0034', 1, 2, 0, 6, 0 )               
8023    ENDIF   ! netcdf_extend     
8024!
8025!-- Open file in read-only mode     
8026    CALL open_read_file( 'PIDS_SALSA' // TRIM( coupling_char ), id_faero )
8027!
8028!-- Get number of emission categories and their indices       
8029    CALL netcdf_data_input_get_dimension_length( id_faero, ncat, "ncat" ) 
8030!
8031!-- Get emission category indices
8032    ALLOCATE( eci(1:ncat) )
8033    CALL get_variable( id_faero, 'emission_category_index', eci ) 
8034!
8035!-- Inquire the level of detail (lod)
8036    CALL get_attribute( id_faero, 'lod', lod_aero, .FALSE.,                    &
8037                        "aerosol_emission_values" ) 
8038                           
8039    IF ( lod_aero < 3  .AND.  ibc_salsa_b  == 0 ) THEN
8040       message_string = 'lod1/2 for aerosol emissions requires '//             &
8041                        'bc_salsa_b = "Neumann"'
8042       CALL message( 'salsa_mod: salsa_set_source','SA0025', 1, 2, 0, 6, 0 )
8043    ENDIF
8044!
8045!-- Inquire the fill value
8046    CALL get_attribute( id_faero, '_FillValue', fillval, .FALSE.,              &
8047                        "aerosol_emission_values" )
8048!
8049!-- Aerosol chemical composition:
8050    ALLOCATE( emission_mass_fracs(1:ncat,1:maxspec) )
8051    emission_mass_fracs = 0.0_wp
8052!-- Chemical composition: 1: H2SO4 (sulphuric acid), 2: OC (organic carbon),
8053!--                       3: BC (black carbon), 4: DU (dust), 
8054!--                       5: SS (sea salt),     6: HNO3 (nitric acid),
8055!--                       7: NH3 (ammonia)
8056    DO  n = 1, ncat
8057       IF  ( lod_aero < 2 )  THEN
8058          emission_mass_fracs(n,:) = mass_fraction_table(:,n)
8059       ELSE
8060          CALL get_variable( id_faero, "emission_mass_fracs",                  &
8061                             emission_mass_fracs(n,:) )
8062       ENDIF 
8063!
8064!--    If the chemical component is not activated, set its mass fraction to 0
8065!--    to avoid inbalance between number and mass flux
8066       IF ( iso4 < 0 )  emission_mass_fracs(n,1) = 0.0_wp
8067       IF ( ioc  < 0 )  emission_mass_fracs(n,2) = 0.0_wp
8068       IF ( ibc  < 0 )  emission_mass_fracs(n,3) = 0.0_wp
8069       IF ( idu  < 0 )  emission_mass_fracs(n,4) = 0.0_wp
8070       IF ( iss  < 0 )  emission_mass_fracs(n,5) = 0.0_wp
8071       IF ( ino  < 0 )  emission_mass_fracs(n,6) = 0.0_wp
8072       IF ( inh  < 0 )  emission_mass_fracs(n,7) = 0.0_wp
8073!--    Then normalise the mass fraction so that SUM = 1                   
8074       emission_mass_fracs(n,:) = emission_mass_fracs(n,:) /                   &
8075                                  SUM( emission_mass_fracs(n,:) )
8076    ENDDO
8077   
8078    IF ( lod_aero > 1 )  THEN
8079!
8080!--    Aerosol geometric mean diameter 
8081       CALL netcdf_data_input_get_dimension_length( id_faero, nb_file, 'Dmid' )     
8082       IF ( nb_file /= nbins )  THEN
8083          message_string = 'The number of size bins in aerosol input data '//  &
8084                           'does not correspond to the model set-up'
8085          CALL message( 'salsa_mod: salsa_set_source','SA0040', 1, 2, 0, 6, 0 )
8086       ENDIF
8087    ENDIF
8088
8089    IF ( lod_aero < 3 )  THEN
8090       CALL location_message( '    salsa_set_source: No time dependency for '//&
8091                             'aerosol emissions. Use aerosol_emission_values'//&
8092                             ' directly.', .TRUE. )
8093!
8094!--    Allocate source arrays
8095       DO  b = 1, nbins
8096          ALLOCATE( aerosol_number(b)%source(1:ncat,nys:nyn,nxl:nxr) )
8097          aerosol_number(b)%source = 0.0_wp
8098       ENDDO 
8099       DO  c = 1, ncc_tot*nbins
8100          ALLOCATE( aerosol_mass(c)%source(1:ncat,nys:nyn,nxl:nxr) )
8101          aerosol_mass(c)%source = 0.0_wp
8102       ENDDO
8103       
8104       IF ( lod_aero == 1 )  THEN
8105          DO  n = 1, ncat
8106             avg_density(n) = emission_mass_fracs(n,1) * arhoh2so4 +           &
8107                              emission_mass_fracs(n,2) * arhooc +              &
8108                              emission_mass_fracs(n,3) * arhobc +              &
8109                              emission_mass_fracs(n,4) * arhodu +              &
8110                              emission_mass_fracs(n,5) * arhoss +              &
8111                              emission_mass_fracs(n,6) * arhohno3 +            &
8112                              emission_mass_fracs(n,7) * arhonh3
8113          ENDDO   
8114!
8115!--       Emission unit
8116          CALL get_attribute( id_faero, 'units', unita, .FALSE.,               &
8117                              "aerosol_emission_values")
8118          conversion_factor = 1.0_wp
8119          IF  ( unita == 'kg/m2/yr' )  THEN
8120             conversion_factor = 3.170979e-8_wp / avg_density
8121          ELSEIF  ( unita == 'g/m2/yr' )  THEN
8122             conversion_factor = 3.170979e-8_wp * 1.0E-3_wp / avg_density
8123          ELSEIF  ( unita == 'kg/m2/s' )  THEN
8124             conversion_factor = 1.0_wp / avg_density
8125          ELSEIF  ( unita == 'g/m2/s' )  THEN
8126             conversion_factor = 1.0E-3_wp / avg_density
8127          ELSE
8128             message_string = 'unknown unit for aerosol emissions: '           &
8129                              // TRIM( unita ) // ' (lod1)'
8130             CALL message( 'salsa_mod: salsa_set_source','SA0035',             &
8131                           1, 2, 0, 6, 0 )
8132          ENDIF
8133!       
8134!--       Read surface emission data (x,y) PE-wise 
8135          ALLOCATE( pm_emission(ncat,nys:nyn,nxl:nxr) )
8136          CALL get_variable( id_faero, 'aerosol_emission_values', pm_emission, &
8137                             nxl, nxr, nys, nyn, 0, ncat-1 )
8138          DO  ni = 1, SIZE( eci )
8139             n = eci(ni)
8140!
8141!--          Calculate the number concentration of a log-normal size
8142!--          distribution following Jacobson (2005): Eq 13.25.
8143             ntot_table = 6.0_wp * PMfrac_table(:,n) / ( pi * dpg_table**3 *   &
8144                          EXP( 4.5_wp * LOG( sigmag_table )**2 ) ) * 1.0E+12_wp
8145!                         
8146!--          Sectional size distibution from a log-normal one                         
8147             CALL size_distribution( ntot_table, dpg_table, sigmag_table,      &
8148                                     nsect_emission )
8149             DO  b = 1, nbins
8150                aerosol_number(b)%source(ni,:,:) =                             &
8151                                    aerosol_number(b)%source(ni,:,:) +         &
8152                                    pm_emission(ni,:,:) * conversion_factor(n) &
8153                                    * nsect_emission(b) 
8154             ENDDO
8155          ENDDO
8156       ELSEIF ( lod_aero == 2 )  THEN             
8157!       
8158!--       Read surface emission data (x,y) PE-wise   
8159          ALLOCATE( dum_var_4d(nb_file,ncat,nys:nyn,nxl:nxr) )
8160          CALL get_variable( id_faero, 'aerosol_emission_values', dum_var_4d,  &
8161                             nxl, nxr, nys, nyn, 0, ncat-1, 0, nb_file-1 )
8162          DO  b = 1, nbins
8163             aerosol_number(b)%source = dum_var_4d(b,:,:,:)
8164          ENDDO
8165          DEALLOCATE( dum_var_4d )
8166       ENDIF
8167!   
8168!--    Set surface fluxes of aerosol number and mass on horizontal surfaces.
8169!--    Set fluxes only for either default, land or urban surface.
8170       IF ( .NOT. land_surface  .AND.  .NOT. urban_surface )  THEN
8171          CALL set_flux( surf_def_h(0), ncat )
8172       ELSE
8173          CALL set_flux( surf_usm_h, ncat )
8174          CALL set_flux( surf_lsm_h, ncat )
8175       ENDIF
8176         
8177    ELSEIF ( lod_aero == 3 )  THEN
8178!
8179!--    Inquire aerosol emission rate per bin (#/(m3s))
8180       nc_stat = NF90_INQ_VARID( id_faero, "aerosol_emission_values", id_sa )
8181 
8182!
8183!--    Emission time step
8184       CALL netcdf_data_input_get_dimension_length( id_faero, n_dt, 'dt_emission' ) 
8185       IF ( n_dt > 1 )  THEN
8186          CALL location_message( '    salsa_set_source: hourly emission data'//&
8187                                 ' provided but currently the value of the '// &
8188                                 ' first hour is applied.', .TRUE. )
8189       ENDIF
8190!
8191!--    Allocate source arrays
8192       DO  b = 1, nbins
8193          ALLOCATE( aerosol_number(b)%source(nzb:nzt+1,nys:nyn,nxl:nxr) )
8194          aerosol_number(b)%source = 0.0_wp
8195       ENDDO
8196       DO  c = 1, ncc_tot*nbins
8197          ALLOCATE( aerosol_mass(c)%source(nzb:nzt+1,nys:nyn,nxl:nxr) )
8198          aerosol_mass(c)%source = 0.0_wp
8199       ENDDO
8200!
8201!--    Get dimension of z-axis:     
8202       CALL netcdf_data_input_get_dimension_length( id_faero, nz_file, 'z' )
8203!       
8204!--    Read surface emission data (x,y) PE-wise             
8205       DO  i = nxl, nxr
8206          DO  j = nys, nyn
8207             DO  k = 0, nz_file-1
8208!
8209!--             Predetermine flag to mask topography                                 
8210                flag = MERGE( 1.0_wp, 0.0_wp, BTEST(wall_flags_0(k,j,i), 0 ))
8211!                                             
8212!--             No sources inside buildings !                                         
8213                IF ( flag == 0.0_wp )  CYCLE                         
8214!
8215!--             Read volume source:
8216                nc_stat = NF90_GET_VAR( id_faero, id_sa, source_ijka,          &
8217                                        start = (/ i+1, j+1, k+1, 1, 1 /),     &
8218                                        count = (/ 1, 1, 1, 1, nb_file /) )
8219                IF ( nc_stat /= NF90_NOERR )  THEN
8220                   message_string = 'error in aerosol emissions: lod3'
8221                   CALL message( 'salsa_mod: salsa_set_source','SA0038', 1, 2, &
8222                                 0, 6, 0 )
8223                ENDIF
8224!       
8225!--             Set mass fluxes.  First bins include only SO4 and/or OC. Call
8226!--             subroutine set_mass_source for larger bins.                           
8227!
8228!--             Sulphate and organic carbon
8229                IF ( iso4 > 0  .AND.  ioc > 0 ) THEN                 
8230!--                First sulphate:                     
8231                   ss = ( iso4 - 1 ) * nbins + in1a   ! start
8232                   ee = ( iso4 - 1 ) * nbins + fn1a   ! end
8233                   b = in1a           
8234                   DO  c = ss, ee
8235                      IF ( source_ijka(b) /= fillval )                         &
8236                      aerosol_mass(c)%source(k,j,i) =                          &
8237                         aerosol_mass(c)%source(k,j,i) +                       &
8238                         emission_mass_fracs(1,1) / ( emission_mass_fracs(1,1) &
8239                         + emission_mass_fracs(1,2) ) * source_ijka(b) *       &
8240                         aero(b)%core * arhoh2so4 
8241                      b = b+1
8242                   ENDDO                 
8243!--                Then organic carbon:                     
8244                   ss = ( ioc - 1 ) * nbins + in1a   ! start
8245                   ee = ( ioc - 1 ) * nbins + fn1a   ! end
8246                   b = in1a
8247                   DO  c = ss, ee 
8248                      IF ( source_ijka(b) /= fillval )                         &
8249                      aerosol_mass(c)%source(k,j,i) =                          &
8250                         aerosol_mass(c)%source(k,j,i) +                       &
8251                         emission_mass_fracs(1,2) / ( emission_mass_fracs(1,1) &
8252                         + emission_mass_fracs(1,2) ) * source_ijka(b) *       &
8253                         aero(b)%core * arhooc 
8254                      b = b+1
8255                   ENDDO
8256                   
8257                   CALL set_mass_source( k, j, i, iso4,                        &
8258                                        emission_mass_fracs(1,1), arhoh2so4,   &
8259                                        source_ijka, fillval )
8260                   CALL set_mass_source( k, j, i, ioc, emission_mass_fracs(1,2),&
8261                                         arhooc, source_ijka, fillval )                     
8262!--             Only sulphate:                                             
8263                ELSEIF ( iso4 > 0  .AND.  ioc < 0 ) THEN                   
8264                   ss = ( iso4 - 1 ) * nbins + in1a   ! start
8265                   ee = ( iso4 - 1 ) * nbins + fn1a   ! end
8266                   b = in1a           
8267                   DO  c = ss, ee
8268                      IF ( source_ijka(b) /= fillval )                         &
8269                      aerosol_mass(c)%source(k,j,i) =                          &
8270                         aerosol_mass(c)%source(k,j,i) + source_ijka(b) *      &
8271                         aero(b)%core * arhoh2so4 
8272                      b = b+1
8273                   ENDDO 
8274                   CALL set_mass_source( k, j, i, iso4,                        &
8275                                        emission_mass_fracs(1,1), arhoh2so4,   &
8276                                        source_ijka, fillval )   
8277!--             Only organic carbon:                                           
8278                ELSEIF ( iso4 < 0  .AND.  ioc > 0 ) THEN                   
8279                   ss = ( ioc - 1 ) * nbins + in1a   ! start
8280                   ee = ( ioc - 1 ) * nbins + fn1a   ! end
8281                   b = in1a
8282                   DO  c = ss, ee 
8283                      IF ( source_ijka(b) /= fillval )                         &
8284                      aerosol_mass(c)%source(k,j,i) =                          &
8285                         aerosol_mass(c)%source(k,j,i) + source_ijka(b)  *     &
8286                         aero(b)%core * arhooc 
8287                      b = b+1
8288                   ENDDO 
8289                   CALL set_mass_source( k, j, i, ioc, emission_mass_fracs(1,2),&
8290                                         arhooc,  source_ijka, fillval )                                   
8291                ENDIF
8292!--             Black carbon
8293                IF ( ibc > 0 ) THEN
8294                   CALL set_mass_source( k, j, i, ibc, emission_mass_fracs(1,3),&
8295                                         arhobc, source_ijka, fillval )
8296                ENDIF
8297!--             Dust
8298                IF ( idu > 0 ) THEN
8299                   CALL set_mass_source( k, j, i, idu, emission_mass_fracs(1,4),&
8300                                         arhodu, source_ijka, fillval )
8301                ENDIF
8302!--             Sea salt
8303                IF ( iss > 0 ) THEN
8304                   CALL set_mass_source( k, j, i, iss, emission_mass_fracs(1,5),&
8305                                         arhoss, source_ijka, fillval )
8306                ENDIF
8307!--             Nitric acid
8308                IF ( ino > 0 ) THEN
8309                   CALL set_mass_source( k, j, i, ino, emission_mass_fracs(1,6),&
8310                                         arhohno3, source_ijka, fillval )
8311                ENDIF
8312!--             Ammonia
8313                IF ( inh > 0 ) THEN
8314                   CALL set_mass_source( k, j, i, inh, emission_mass_fracs(1,7),&
8315                                         arhonh3, source_ijka, fillval )
8316                ENDIF
8317!                             
8318!--             Save aerosol number sources in the end                           
8319                DO  b = 1, nbins
8320                   IF ( source_ijka(b) /= fillval )                            &
8321                   aerosol_number(b)%source(k,j,i) =                           &
8322                      aerosol_number(b)%source(k,j,i) + source_ijka(b)
8323                ENDDO                     
8324             ENDDO    ! k
8325          ENDDO    ! j
8326       ENDDO    ! i
8327
8328    ELSE     
8329       message_string = 'NetCDF attribute lod is not set properly.'
8330       CALL message( 'salsa_mod: salsa_set_source','SA0026', 1, 2, 0, 6, 0 )
8331    ENDIF 
8332 
8333#endif   
8334 END SUBROUTINE salsa_set_source
8335 
8336!------------------------------------------------------------------------------!
8337! Description:
8338! ------------
8339!> Sets the gaseous fluxes
8340!------------------------------------------------------------------------------!
8341 SUBROUTINE set_gas_flux( surface, ncat_emission, unit )
8342 
8343    USE arrays_3d,                                                             &
8344        ONLY: dzw, hyp, pt, rho_air_zw
8345       
8346    USE grid_variables,                                                        &
8347        ONLY:  dx, dy
8348 
8349    USE surface_mod,                                                           &
8350        ONLY:  surf_type
8351   
8352    IMPLICIT NONE
8353   
8354    CHARACTER(LEN=*) ::  unit       !< flux unit in the input file 
8355    INTEGER(iwp) ::  ncat_emission  !< number of emission categories
8356    TYPE(surf_type), INTENT(inout) :: surface  !< respective surface type
8357    INTEGER(iwp) ::  g   !< loop index
8358    INTEGER(iwp) ::  i   !< loop index
8359    INTEGER(iwp) ::  j   !< loop index
8360    INTEGER(iwp) ::  k   !< loop index
8361    INTEGER(iwp) ::  m   !< running index for surface elements
8362    INTEGER(iwp) ::  n   !< running index for emission categories
8363    REAL(wp), DIMENSION(ngast) ::  conversion_factor 
8364   
8365    conversion_factor = 1.0_wp
8366   
8367    DO  m = 1, surface%ns
8368!
8369!--    Get indices of respective grid point
8370       i = surface%i(m)
8371       j = surface%j(m)
8372       k = surface%k(m)
8373       
8374       IF ( unit == '#/m2/s' )  THEN
8375          conversion_factor = 1.0_wp
8376       ELSEIF ( unit == 'g/m2/s' )  THEN
8377          conversion_factor(1) = avo / ( amh2so4 * 1000.0_wp )
8378          conversion_factor(2) = avo / ( amhno3 * 1000.0_wp )
8379          conversion_factor(3) = avo / ( amnh3 * 1000.0_wp )
8380          conversion_factor(4) = avo / ( amoc * 1000.0_wp )
8381          conversion_factor(5) = avo / ( amoc * 1000.0_wp )
8382       ELSEIF ( unit == 'ppm/m2/s' )  THEN
8383          conversion_factor = for_ppm_to_nconc * hyp(k) / pt(k,j,i) * ( hyp(k) &
8384                              / 100000.0_wp )**0.286_wp * dx * dy * dzw(k)
8385       ELSEIF ( unit == 'mumol/m2/s' )  THEN
8386          conversion_factor = 1.0E-6_wp * avo
8387       ELSE
8388          message_string = 'Unknown unit for gaseous emissions!'
8389          CALL message( 'salsa_mod: set_gas_flux', 'SA0031', 1, 2, 0, 6, 0 )
8390       ENDIF
8391       
8392       DO  n = 1, ncat_emission
8393          DO  g = 1, ngast
8394             IF ( .NOT. salsa_gas(g)%source(n,j,i) > 0.0_wp )  THEN
8395                salsa_gas(g)%source(n,j,i) = 0.0_wp
8396                CYCLE
8397             ENDIF
8398             surface%gtsws(m,g) = surface%gtsws(m,g) +                         &
8399                                  salsa_gas(g)%source(n,j,i) * rho_air_zw(k-1) &
8400                                  * conversion_factor(g)
8401          ENDDO
8402       ENDDO
8403    ENDDO
8404   
8405 END SUBROUTINE set_gas_flux 
8406 
8407 
8408!------------------------------------------------------------------------------!
8409! Description:
8410! ------------
8411!> Sets the aerosol flux to aerosol arrays in 2a and 2b.
8412!------------------------------------------------------------------------------!
8413 SUBROUTINE set_flux( surface, ncat_emission )
8414 
8415    USE arrays_3d,                                                             &
8416        ONLY: hyp, pt, rho_air_zw
8417 
8418    USE surface_mod,                                                           &
8419        ONLY:  surf_type
8420   
8421    IMPLICIT NONE
8422
8423    INTEGER(iwp) ::  ncat_emission  !< number of emission categories
8424    TYPE(surf_type), INTENT(inout) :: surface  !< respective surface type
8425    INTEGER(iwp) ::  b  !< loop index
8426    INTEGER(iwp) ::  ee  !< loop index
8427    INTEGER(iwp) ::  g   !< loop index
8428    INTEGER(iwp) ::  i   !< loop index
8429    INTEGER(iwp) ::  j   !< loop index
8430    INTEGER(iwp) ::  k   !< loop index
8431    INTEGER(iwp) ::  m   !< running index for surface elements
8432    INTEGER(iwp) ::  n   !< loop index for emission categories
8433    INTEGER(iwp) ::  c   !< loop index
8434    INTEGER(iwp) ::  ss  !< loop index
8435   
8436    DO  m = 1, surface%ns
8437!
8438!--    Get indices of respective grid point
8439       i = surface%i(m)
8440       j = surface%j(m)
8441       k = surface%k(m)
8442       
8443       DO  n = 1, ncat_emission 
8444          DO  b = 1, nbins
8445             IF (  aerosol_number(b)%source(n,j,i) < 0.0_wp )  THEN
8446                aerosol_number(b)%source(n,j,i) = 0.0_wp
8447                CYCLE
8448             ENDIF
8449!       
8450!--          Set mass fluxes.  First bins include only SO4 and/or OC.     
8451
8452             IF ( b <= fn1a )  THEN
8453!
8454!--             Both sulphate and organic carbon
8455                IF ( iso4 > 0  .AND.  ioc > 0 )  THEN
8456               
8457                   c = ( iso4 - 1 ) * nbins + b   
8458                   surface%amsws(m,c) = surface%amsws(m,c) +                   &
8459                                        emission_mass_fracs(n,1) /             &
8460                                        ( emission_mass_fracs(n,1) +           &
8461                                          emission_mass_fracs(n,2) ) *         &
8462                                          aerosol_number(b)%source(n,j,i) *    &
8463                                          api6 * aero(b)%dmid**3.0_wp *        &
8464                                          arhoh2so4 * rho_air_zw(k-1)
8465                   aerosol_mass(c)%source(n,j,i) =                             &
8466                              aerosol_mass(c)%source(n,j,i) + surface%amsws(m,c)
8467                   c = ( ioc - 1 ) * nbins + b   
8468                   surface%amsws(m,c) = surface%amsws(m,c) +                   &
8469                                        emission_mass_fracs(n,2) /             &
8470                                        ( emission_mass_fracs(n,1) +           & 
8471                                          emission_mass_fracs(n,2) ) *         &
8472                                          aerosol_number(b)%source(n,j,i) *    &
8473                                          api6 * aero(b)%dmid**3.0_wp * arhooc &
8474                                          * rho_air_zw(k-1)
8475                   aerosol_mass(c)%source(n,j,i) =                             &
8476                              aerosol_mass(c)%source(n,j,i) + surface%amsws(m,c)
8477!
8478!--             Only sulphates
8479                ELSEIF ( iso4 > 0  .AND.  ioc < 0 )  THEN
8480                   c = ( iso4 - 1 ) * nbins + b   
8481                   surface%amsws(m,c) = surface%amsws(m,c) +                   &
8482                                        aerosol_number(b)%source(n,j,i) * api6 &
8483                                        * aero(b)%dmid**3.0_wp * arhoh2so4     &
8484                                        * rho_air_zw(k-1)
8485                   aerosol_mass(c)%source(n,j,i) =                             &
8486                              aerosol_mass(c)%source(n,j,i) + surface%amsws(m,c)
8487!             
8488!--             Only organic carbon             
8489                ELSEIF ( iso4 < 0  .AND.  ioc > 0 )  THEN
8490                   c = ( ioc - 1 ) * nbins + b   
8491                   surface%amsws(m,c) = surface%amsws(m,c) +                   &
8492                                        aerosol_number(b)%source(n,j,i) * api6 &
8493                                        * aero(b)%dmid**3.0_wp * arhooc        &
8494                                        * rho_air_zw(k-1)
8495                   aerosol_mass(c)%source(n,j,i) =                             &
8496                              aerosol_mass(c)%source(n,j,i) + surface%amsws(m,c)
8497                ENDIF
8498               
8499             ELSEIF ( b > fn1a )  THEN
8500!
8501!--             Sulphate
8502                IF ( iso4 > 0 )  THEN
8503                   CALL set_mass_flux( surface, m, b, iso4, n,                 &
8504                                       emission_mass_fracs(n,1), arhoh2so4,    &
8505                                       aerosol_number(b)%source(n,j,i) )
8506                ENDIF 
8507!             
8508!--             Organic carbon                 
8509                IF ( ioc > 0 )  THEN         
8510                  CALL set_mass_flux( surface, m, b, ioc, n,                   &
8511                                      emission_mass_fracs(n,2), arhooc,        &
8512                                      aerosol_number(b)%source(n,j,i) )
8513                ENDIF
8514!
8515!--             Black carbon
8516                IF ( ibc > 0 )  THEN
8517                   CALL set_mass_flux( surface, m, b, ibc, n,                  &
8518                                       emission_mass_fracs(n,3), arhobc,       &
8519                                       aerosol_number(b)%source(n,j,i) )
8520                ENDIF
8521!
8522!--             Dust
8523                IF ( idu > 0 )  THEN
8524                   CALL set_mass_flux( surface, m, b, idu, n,                  &
8525                                       emission_mass_fracs(n,4), arhodu,       &
8526                                       aerosol_number(b)%source(n,j,i) )
8527                ENDIF
8528!
8529!--             Sea salt
8530                IF ( iss > 0 )  THEN
8531                   CALL set_mass_flux( surface, m, b, iss, n,                  &
8532                                       emission_mass_fracs(n,5), arhoss,       &
8533                                       aerosol_number(b)%source(n,j,i) )
8534                ENDIF
8535!
8536!--             Nitric acid
8537                IF ( ino > 0 )  THEN
8538                   CALL set_mass_flux( surface, m, b, ino, n,                  &
8539                                       emission_mass_fracs(n,6), arhohno3,     &
8540                                       aerosol_number(b)%source(n,j,i) )
8541                ENDIF
8542!
8543!--             Ammonia
8544                IF ( inh > 0 )  THEN
8545                   CALL set_mass_flux( surface, m, b, inh, n,                  &
8546                                       emission_mass_fracs(n,7), arhonh3,      &
8547                                       aerosol_number(b)%source(n,j,i) )
8548                ENDIF
8549               
8550             ENDIF
8551!             
8552!--          Save number fluxes in the end
8553             surface%answs(m,b) = surface%answs(m,b) +                         &
8554                               aerosol_number(b)%source(n,j,i) * rho_air_zw(k-1)
8555             aerosol_number(b)%source(n,j,i) = surface%answs(m,b)
8556          ENDDO
8557       
8558       ENDDO
8559       
8560    ENDDO
8561   
8562 END SUBROUTINE set_flux 
8563 
8564!------------------------------------------------------------------------------!
8565! Description:
8566! ------------
8567!> Sets the mass emissions to aerosol arrays in 2a and 2b.
8568!------------------------------------------------------------------------------!
8569 SUBROUTINE set_mass_flux( surface, surf_num, b, ispec, n, mass_frac, prho,    &
8570                           nsource )
8571                           
8572    USE arrays_3d,                                                             &
8573        ONLY:  rho_air_zw
8574
8575    USE surface_mod,                                                           &
8576        ONLY:  surf_type
8577   
8578    IMPLICIT NONE
8579
8580    INTEGER(iwp), INTENT(in) :: b         !< Aerosol size bin index
8581    INTEGER(iwp), INTENT(in) :: ispec     !< Aerosol species index
8582    INTEGER(iwp), INTENT(in) :: n         !< emission category number   
8583    INTEGER(iwp), INTENT(in) :: surf_num  !< index surface elements
8584    REAL(wp), INTENT(in) ::  mass_frac    !< mass fraction of a chemical
8585                                          !< compound in all bins
8586    REAL(wp), INTENT(in) ::  nsource      !< number source (#/m2/s)
8587    REAL(wp), INTENT(in) ::  prho         !< Aerosol density
8588    TYPE(surf_type), INTENT(inout) ::  surface  !< respective surface type
8589     
8590    INTEGER(iwp) ::  ee !< index: end
8591    INTEGER(iwp) ::  i  !< loop index
8592    INTEGER(iwp) ::  j  !< loop index
8593    INTEGER(iwp) ::  k  !< loop index
8594    INTEGER(iwp) ::  c  !< loop index
8595    INTEGER(iwp) ::  ss !<index: start
8596   
8597!
8598!-- Get indices of respective grid point
8599    i = surface%i(surf_num)
8600    j = surface%j(surf_num)
8601    k = surface%k(surf_num)
8602!         
8603!-- Subrange 2a:
8604    c = ( ispec - 1 ) * nbins + b
8605    surface%amsws(surf_num,c) = surface%amsws(surf_num,c) + mass_frac * nsource&
8606                                * aero(b)%core * prho * rho_air_zw(k-1)
8607    aerosol_mass(c)%source(n,j,i) = aerosol_mass(c)%source(n,j,i) +            &
8608                                    surface%amsws(surf_num,c)
8609!         
8610!-- Subrange 2b:
8611    IF ( .NOT. no_insoluble )  THEN
8612       WRITE(*,*) 'All emissions are soluble!'
8613    ENDIF
8614   
8615 END SUBROUTINE set_mass_flux
8616 
8617!------------------------------------------------------------------------------!
8618! Description:
8619! ------------
8620!> Sets the mass sources to aerosol arrays in 2a and 2b.
8621!------------------------------------------------------------------------------!
8622 SUBROUTINE set_mass_source( k, j, i,  ispec, mass_frac, prho, nsource, fillval )
8623
8624    USE surface_mod,                                                           &
8625        ONLY:  surf_type
8626   
8627    IMPLICIT NONE
8628   
8629    INTEGER(iwp), INTENT(in) :: ispec     !< Aerosol species index
8630    REAL(wp), INTENT(in) ::  fillval      !< _FillValue in the NetCDF file
8631    REAL(wp), INTENT(in) ::  mass_frac    !< mass fraction of a chemical
8632                                          !< compound in all bins 
8633    REAL(wp), INTENT(in), DIMENSION(:) ::  nsource  !< number source
8634    REAL(wp), INTENT(in) ::  prho         !< Aerosol density
8635   
8636    INTEGER(iwp) ::  b !< loop index   
8637    INTEGER(iwp) ::  ee !< index: end
8638    INTEGER(iwp) ::  i  !< loop index
8639    INTEGER(iwp) ::  j  !< loop index
8640    INTEGER(iwp) ::  k  !< loop index
8641    INTEGER(iwp) ::  c  !< loop index
8642    INTEGER(iwp) ::  ss !<index: start
8643!         
8644!-- Subrange 2a:
8645    ss = ( ispec - 1 ) * nbins + in2a
8646    ee = ( ispec - 1 ) * nbins + fn2a
8647    b = in2a
8648    DO c = ss, ee
8649       IF ( nsource(b) /= fillval )  THEN
8650          aerosol_mass(c)%source(k,j,i) = aerosol_mass(c)%source(k,j,i) +      &
8651                                       mass_frac * nsource(b) * aero(b)%core * &
8652                                       prho 
8653       ENDIF
8654       b = b+1
8655    ENDDO
8656!         
8657!-- Subrange 2b:
8658    IF ( .NOT. no_insoluble )  THEN
8659       WRITE(*,*) 'All sources are soluble!'
8660    ENDIF
8661   
8662 END SUBROUTINE set_mass_source 
8663 
8664!------------------------------------------------------------------------------!
8665! Description:
8666! ------------
8667!> Check data output for salsa.
8668!------------------------------------------------------------------------------!
8669 SUBROUTINE salsa_check_data_output( var, unit )
8670 
8671    USE control_parameters,                                                    &
8672        ONLY:  message_string
8673
8674    IMPLICIT NONE
8675
8676    CHARACTER (LEN=*) ::  unit     !<
8677    CHARACTER (LEN=*) ::  var      !<
8678
8679    SELECT CASE ( TRIM( var ) )
8680         
8681       CASE ( 'g_H2SO4', 'g_HNO3', 'g_NH3', 'g_OCNV',  'g_OCSV',               &
8682              'N_bin1', 'N_bin2', 'N_bin3', 'N_bin4',  'N_bin5',  'N_bin6',    &
8683              'N_bin7', 'N_bin8', 'N_bin9', 'N_bin10', 'N_bin11', 'N_bin12',   &
8684              'Ntot' )
8685          IF (  .NOT.  salsa )  THEN
8686             message_string = 'output of "' // TRIM( var ) // '" requi' //  &
8687                       'res salsa = .TRUE.'
8688             CALL message( 'check_parameters', 'SA0006', 1, 2, 0, 6, 0 )
8689          ENDIF
8690          unit = '#/m3'
8691         
8692       CASE ( 'LDSA' )
8693          IF (  .NOT.  salsa )  THEN
8694             message_string = 'output of "' // TRIM( var ) // '" requi' //  &
8695                       'res salsa = .TRUE.'
8696             CALL message( 'check_parameters', 'SA0003', 1, 2, 0, 6, 0 )
8697          ENDIF
8698          unit = 'mum2/cm3'         
8699         
8700       CASE ( 'm_bin1', 'm_bin2', 'm_bin3', 'm_bin4',  'm_bin5',  'm_bin6',    &
8701              'm_bin7', 'm_bin8', 'm_bin9', 'm_bin10', 'm_bin11', 'm_bin12',   &
8702              'PM2.5',  'PM10',   's_BC',   's_DU',    's_H2O',   's_NH',      &
8703              's_NO',   's_OC',   's_SO4',  's_SS' )
8704          IF (  .NOT.  salsa )  THEN
8705             message_string = 'output of "' // TRIM( var ) // '" requi' //  &
8706                       'res salsa = .TRUE.'
8707             CALL message( 'check_parameters', 'SA0001', 1, 2, 0, 6, 0 )
8708          ENDIF
8709          unit = 'kg/m3'
8710             
8711       CASE DEFAULT
8712          unit = 'illegal'
8713
8714    END SELECT
8715
8716 END SUBROUTINE salsa_check_data_output
8717 
8718!------------------------------------------------------------------------------!
8719!
8720! Description:
8721! ------------
8722!> Subroutine for averaging 3D data
8723!------------------------------------------------------------------------------!
8724 SUBROUTINE salsa_3d_data_averaging( mode, variable )
8725 
8726
8727    USE control_parameters
8728
8729    USE indices
8730
8731    USE kinds
8732
8733    IMPLICIT NONE
8734
8735    CHARACTER (LEN=*) ::  mode       !<
8736    CHARACTER (LEN=*) ::  variable   !<
8737
8738    INTEGER(iwp) ::  b   !<     
8739    INTEGER(iwp) ::  c   !<
8740    INTEGER(iwp) ::  i   !<
8741    INTEGER(iwp) ::  icc !<
8742    INTEGER(iwp) ::  j   !<
8743    INTEGER(iwp) ::  k   !<
8744   
8745    REAL(wp) ::  df       !< For calculating LDSA: fraction of particles
8746                          !< depositing in the alveolar (or tracheobronchial)
8747                          !< region of the lung. Depends on the particle size
8748    REAL(wp) ::  mean_d   !< Particle diameter in micrometres
8749    REAL(wp) ::  nc       !< Particle number concentration in units 1/cm**3
8750    REAL(wp) ::  temp_bin !<
8751    REAL(wp), DIMENSION(:,:,:), POINTER ::  to_be_resorted  !< points to
8752                                                     !< selected output variable
8753   
8754    temp_bin = 0.0_wp
8755
8756    IF ( mode == 'allocate' )  THEN
8757
8758       SELECT CASE ( TRIM( variable ) )
8759       
8760          CASE ( 'g_H2SO4' )
8761             IF ( .NOT. ALLOCATED( g_H2SO4_av ) )  THEN
8762                ALLOCATE( g_H2SO4_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
8763             ENDIF
8764             g_H2SO4_av = 0.0_wp
8765             
8766          CASE ( 'g_HNO3' )
8767             IF ( .NOT. ALLOCATED( g_HNO3_av ) )  THEN
8768                ALLOCATE( g_HNO3_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
8769             ENDIF
8770             g_HNO3_av = 0.0_wp
8771             
8772          CASE ( 'g_NH3' )
8773             IF ( .NOT. ALLOCATED( g_NH3_av ) )  THEN
8774                ALLOCATE( g_NH3_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
8775             ENDIF
8776             g_NH3_av = 0.0_wp
8777             
8778          CASE ( 'g_OCNV' )
8779             IF ( .NOT. ALLOCATED( g_OCNV_av ) )  THEN
8780                ALLOCATE( g_OCNV_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
8781             ENDIF
8782             g_OCNV_av = 0.0_wp
8783             
8784          CASE ( 'g_OCSV' )
8785             IF ( .NOT. ALLOCATED( g_OCSV_av ) )  THEN
8786                ALLOCATE( g_OCSV_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
8787             ENDIF
8788             g_OCSV_av = 0.0_wp             
8789             
8790          CASE ( 'LDSA' )
8791             IF ( .NOT. ALLOCATED( LDSA_av ) )  THEN
8792                ALLOCATE( LDSA_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
8793             ENDIF
8794             LDSA_av = 0.0_wp
8795             
8796          CASE ( 'N_bin1', 'N_bin2', 'N_bin3', 'N_bin4', 'N_bin5', 'N_bin6',   &
8797                 'N_bin7', 'N_bin8', 'N_bin9', 'N_bin10', 'N_bin11', 'N_bin12' )
8798             IF ( .NOT. ALLOCATED( Nbins_av ) )  THEN
8799                ALLOCATE( Nbins_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg,nbins) )
8800             ENDIF
8801             Nbins_av = 0.0_wp
8802             
8803          CASE ( 'Ntot' )
8804             IF ( .NOT. ALLOCATED( Ntot_av ) )  THEN
8805                ALLOCATE( Ntot_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
8806             ENDIF
8807             Ntot_av = 0.0_wp
8808             
8809          CASE ( 'm_bin1', 'm_bin2', 'm_bin3', 'm_bin4', 'm_bin5', 'm_bin6',   &
8810                 'm_bin7', 'm_bin8', 'm_bin9', 'm_bin10', 'm_bin11', 'm_bin12' )
8811             IF ( .NOT. ALLOCATED( mbins_av ) )  THEN
8812                ALLOCATE( mbins_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg,nbins) )
8813             ENDIF
8814             mbins_av = 0.0_wp
8815             
8816          CASE ( 'PM2.5' )
8817             IF ( .NOT. ALLOCATED( PM25_av ) )  THEN
8818                ALLOCATE( PM25_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
8819             ENDIF
8820             PM25_av = 0.0_wp
8821             
8822          CASE ( 'PM10' )
8823             IF ( .NOT. ALLOCATED( PM10_av ) )  THEN
8824                ALLOCATE( PM10_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
8825             ENDIF
8826             PM10_av = 0.0_wp
8827             
8828          CASE ( 's_BC' )
8829             IF ( .NOT. ALLOCATED( s_BC_av ) )  THEN
8830                ALLOCATE( s_BC_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
8831             ENDIF
8832             s_BC_av = 0.0_wp
8833         
8834          CASE ( 's_DU' )
8835             IF ( .NOT. ALLOCATED( s_DU_av ) )  THEN
8836                ALLOCATE( s_DU_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
8837             ENDIF
8838             s_DU_av = 0.0_wp
8839             
8840          CASE ( 's_H2O' )
8841             IF ( .NOT. ALLOCATED( s_H2O_av ) )  THEN
8842                ALLOCATE( s_H2O_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
8843             ENDIF
8844             s_H2O_av = 0.0_wp
8845             
8846          CASE ( 's_NH' )
8847             IF ( .NOT. ALLOCATED( s_NH_av ) )  THEN
8848                ALLOCATE( s_NH_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
8849             ENDIF
8850             s_NH_av = 0.0_wp
8851             
8852          CASE ( 's_NO' )
8853             IF ( .NOT. ALLOCATED( s_NO_av ) )  THEN
8854                ALLOCATE( s_NO_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
8855             ENDIF
8856             s_NO_av = 0.0_wp
8857             
8858          CASE ( 's_OC' )
8859             IF ( .NOT. ALLOCATED( s_OC_av ) )  THEN
8860                ALLOCATE( s_OC_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
8861             ENDIF
8862             s_OC_av = 0.0_wp
8863             
8864          CASE ( 's_SO4' )
8865             IF ( .NOT. ALLOCATED( s_SO4_av ) )  THEN
8866                ALLOCATE( s_SO4_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
8867             ENDIF
8868             s_SO4_av = 0.0_wp   
8869         
8870          CASE ( 's_SS' )
8871             IF ( .NOT. ALLOCATED( s_SS_av ) )  THEN
8872                ALLOCATE( s_SS_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
8873             ENDIF
8874             s_SS_av = 0.0_wp
8875         
8876          CASE DEFAULT
8877             CONTINUE
8878
8879       END SELECT
8880
8881    ELSEIF ( mode == 'sum' )  THEN
8882
8883       SELECT CASE ( TRIM( variable ) )
8884       
8885          CASE ( 'g_H2SO4', 'g_HNO3', 'g_NH3', 'g_OCNV', 'g_OCSV' )
8886             IF ( TRIM( variable(3:) ) == 'H2SO4' )  THEN
8887                icc = 1
8888                to_be_resorted => g_H2SO4_av
8889             ELSEIF ( TRIM( variable(3:) ) == 'HNO3' )  THEN
8890                icc = 2
8891                to_be_resorted => g_HNO3_av   
8892             ELSEIF ( TRIM( variable(3:) ) == 'NH3' )  THEN
8893                icc = 3
8894                to_be_resorted => g_NH3_av   
8895             ELSEIF ( TRIM( variable(3:) ) == 'OCNV' )  THEN
8896                icc = 4
8897                to_be_resorted => g_OCNV_av   
8898             ELSEIF ( TRIM( variable(3:) ) == 'OCSV' )  THEN
8899                icc = 5
8900                to_be_resorted => g_OCSV_av       
8901             ENDIF
8902             DO  i = nxlg, nxrg
8903                DO  j = nysg, nyng
8904                   DO  k = nzb, nzt+1
8905                      to_be_resorted(k,j,i) = to_be_resorted(k,j,i) +         &
8906                                              salsa_gas(icc)%conc(k,j,i)
8907                   ENDDO
8908                ENDDO
8909             ENDDO
8910             
8911          CASE ( 'LDSA' )
8912             DO  i = nxlg, nxrg
8913                DO  j = nysg, nyng
8914                   DO  k = nzb, nzt+1
8915                      temp_bin = 0.0_wp
8916                      DO  b = 1, nbins 
8917!                     
8918!--                      Diameter in micrometres
8919                         mean_d = 1.0E+6_wp * Ra_dry(k,j,i,b) * 2.0_wp
8920!                               
8921!--                      Deposition factor: alveolar (use Ra_dry)                             
8922                         df = ( 0.01555_wp / mean_d ) * ( EXP( -0.416_wp *     &
8923                                ( LOG( mean_d ) + 2.84_wp )**2.0_wp )          &
8924                                  + 19.11_wp * EXP( -0.482_wp *                &
8925                                  ( LOG( mean_d ) - 1.362_wp )**2.0_wp ) )
8926!                                   
8927!--                      Number concentration in 1/cm3
8928                         nc = 1.0E-6_wp * aerosol_number(b)%conc(k,j,i)   
8929!                         
8930!--                      Lung-deposited surface area LDSA (units mum2/cm3)                           
8931                         temp_bin = temp_bin + pi * mean_d**2.0_wp * df * nc
8932                      ENDDO
8933                      LDSA_av(k,j,i) = LDSA_av(k,j,i) + temp_bin
8934                   ENDDO
8935                ENDDO
8936             ENDDO
8937             
8938          CASE ( 'N_bin1', 'N_bin2', 'N_bin3', 'N_bin4', 'N_bin5', 'N_bin6',   &
8939                 'N_bin7', 'N_bin8', 'N_bin9', 'N_bin10', 'N_bin11', 'N_bin12' )
8940             DO  i = nxlg, nxrg
8941                DO  j = nysg, nyng
8942                   DO  k = nzb, nzt+1
8943                      DO  b = 1, nbins 
8944                         Nbins_av(k,j,i,b) = Nbins_av(k,j,i,b) +               &
8945                                             aerosol_number(b)%conc(k,j,i)
8946                      ENDDO
8947                   ENDDO
8948                ENDDO
8949             ENDDO
8950         
8951          CASE ( 'Ntot' )
8952             DO  i = nxlg, nxrg
8953                DO  j = nysg, nyng
8954                   DO  k = nzb, nzt+1
8955                      DO  b = 1, nbins 
8956                         Ntot_av(k,j,i) = Ntot_av(k,j,i) +                     &
8957                                          aerosol_number(b)%conc(k,j,i)
8958                      ENDDO
8959                   ENDDO
8960                ENDDO
8961             ENDDO
8962             
8963          CASE ( 'm_bin1', 'm_bin2', 'm_bin3', 'm_bin4', 'm_bin5', 'm_bin6',   &
8964                 'm_bin7', 'm_bin8', 'm_bin9', 'm_bin10', 'm_bin11', 'm_bin12' )
8965             DO  i = nxlg, nxrg
8966                DO  j = nysg, nyng
8967                   DO  k = nzb, nzt+1
8968                      DO  b = 1, nbins 
8969                         DO  c = b, nbins*ncc_tot, nbins
8970                            mbins_av(k,j,i,b) = mbins_av(k,j,i,b) +            &
8971                                                aerosol_mass(c)%conc(k,j,i)
8972                         ENDDO
8973                      ENDDO
8974                   ENDDO
8975                ENDDO
8976             ENDDO
8977             
8978          CASE ( 'PM2.5' )
8979             DO  i = nxlg, nxrg
8980                DO  j = nysg, nyng
8981                   DO  k = nzb, nzt+1
8982                      temp_bin = 0.0_wp
8983                      DO  b = 1, nbins
8984                         IF ( 2.0_wp * Ra_dry(k,j,i,b) <= 2.5E-6_wp )  THEN
8985                            DO  c = b, nbins*ncc, nbins
8986                               temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
8987                            ENDDO
8988                         ENDIF
8989                      ENDDO
8990                      PM25_av(k,j,i) = PM25_av(k,j,i) + temp_bin
8991                   ENDDO
8992                ENDDO
8993             ENDDO
8994             
8995          CASE ( 'PM10' )
8996             DO  i = nxlg, nxrg
8997                DO  j = nysg, nyng
8998                   DO  k = nzb, nzt+1
8999                      temp_bin = 0.0_wp
9000                      DO  b = 1, nbins
9001                         IF ( 2.0_wp * Ra_dry(k,j,i,b) <= 10.0E-6_wp )  THEN
9002                            DO  c = b, nbins*ncc, nbins
9003                               temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
9004                            ENDDO
9005                         ENDIF
9006                      ENDDO
9007                      PM10_av(k,j,i) = PM10_av(k,j,i) + temp_bin
9008                   ENDDO
9009                ENDDO
9010             ENDDO
9011             
9012          CASE ( 's_BC', 's_DU', 's_H2O', 's_NH', 's_NO', 's_OC', 's_SO4',     &
9013                 's_SS' )
9014             IF ( is_used( prtcl, TRIM( variable(3:) ) ) )  THEN
9015                icc = get_index( prtcl, TRIM( variable(3:) ) )
9016                IF ( TRIM( variable(3:) ) == 'BC' )   to_be_resorted => s_BC_av
9017                IF ( TRIM( variable(3:) ) == 'DU' )   to_be_resorted => s_DU_av   
9018                IF ( TRIM( variable(3:) ) == 'NH' )   to_be_resorted => s_NH_av   
9019                IF ( TRIM( variable(3:) ) == 'NO' )   to_be_resorted => s_NO_av   
9020                IF ( TRIM( variable(3:) ) == 'OC' )   to_be_resorted => s_OC_av   
9021                IF ( TRIM( variable(3:) ) == 'SO4' )  to_be_resorted => s_SO4_av   
9022                IF ( TRIM( variable(3:) ) == 'SS' )   to_be_resorted => s_SS_av       
9023                DO  i = nxlg, nxrg
9024                   DO  j = nysg, nyng
9025                      DO  k = nzb, nzt+1
9026                         DO  c = ( icc-1 )*nbins+1, icc*nbins 
9027                            to_be_resorted(k,j,i) = to_be_resorted(k,j,i) +    &
9028                                                    aerosol_mass(c)%conc(k,j,i)
9029                         ENDDO
9030                      ENDDO
9031                   ENDDO
9032                ENDDO
9033             ENDIF
9034             
9035          CASE DEFAULT
9036             CONTINUE
9037
9038       END SELECT
9039
9040    ELSEIF ( mode == 'average' )  THEN
9041
9042       SELECT CASE ( TRIM( variable ) )
9043       
9044          CASE ( 'g_H2SO4', 'g_HNO3', 'g_NH3', 'g_OCNV', 'g_OCSV' )
9045             IF ( TRIM( variable(3:) ) == 'H2SO4' )  THEN
9046                icc = 1
9047                to_be_resorted => g_H2SO4_av
9048             ELSEIF ( TRIM( variable(3:) ) == 'HNO3' )  THEN
9049                icc = 2
9050                to_be_resorted => g_HNO3_av   
9051             ELSEIF ( TRIM( variable(3:) ) == 'NH3' )  THEN
9052                icc = 3
9053                to_be_resorted => g_NH3_av   
9054             ELSEIF ( TRIM( variable(3:) ) == 'OCNV' )  THEN
9055                icc = 4
9056                to_be_resorted => g_OCNV_av   
9057             ELSEIF ( TRIM( variable(3:) ) == 'OCSV' )  THEN
9058                icc = 5
9059                to_be_resorted => g_OCSV_av       
9060             ENDIF
9061             DO  i = nxlg, nxrg
9062                DO  j = nysg, nyng
9063                   DO  k = nzb, nzt+1
9064                      to_be_resorted(k,j,i) = to_be_resorted(k,j,i)            &
9065                                             / REAL( average_count_3d, KIND=wp )
9066                   ENDDO
9067                ENDDO
9068             ENDDO
9069             
9070          CASE ( 'LDSA' )
9071             DO  i = nxlg, nxrg
9072                DO  j = nysg, nyng
9073                   DO  k = nzb, nzt+1
9074                      LDSA_av(k,j,i) = LDSA_av(k,j,i)                          &
9075                                        / REAL( average_count_3d, KIND=wp )
9076                   ENDDO
9077                ENDDO
9078             ENDDO
9079             
9080          CASE ( 'N_bin1', 'N_bin2', 'N_bin3', 'N_bin4', 'N_bin5', 'N_bin6',   &
9081                 'N_bin7', 'N_bin8', 'N_bin9', 'N_bin10', 'N_bin11', 'N_bin12' )
9082             DO  i = nxlg, nxrg
9083                DO  j = nysg, nyng
9084                   DO  k = nzb, nzt+1
9085                      DO  b = 1, nbins 
9086                         Nbins_av(k,j,i,b) = Nbins_av(k,j,i,b)                 &
9087                                             / REAL( average_count_3d, KIND=wp )
9088                      ENDDO
9089                   ENDDO
9090                ENDDO
9091             ENDDO
9092             
9093          CASE ( 'Ntot' )
9094             DO  i = nxlg, nxrg
9095                DO  j = nysg, nyng
9096                   DO  k = nzb, nzt+1
9097                      Ntot_av(k,j,i) = Ntot_av(k,j,i)                          &
9098                                        / REAL( average_count_3d, KIND=wp )
9099                   ENDDO
9100                ENDDO
9101             ENDDO
9102             
9103          CASE ( 'm_bin1', 'm_bin2', 'm_bin3', 'm_bin4', 'm_bin5', 'm_bin6',   &
9104                 'm_bin7', 'm_bin8', 'm_bin9', 'm_bin10', 'm_bin11', 'm_bin12' )
9105             DO  i = nxlg, nxrg
9106                DO  j = nysg, nyng
9107                   DO  k = nzb, nzt+1
9108                      DO  b = 1, nbins 
9109                         DO  c = b, nbins*ncc, nbins
9110                            mbins_av(k,j,i,b) = mbins_av(k,j,i,b)              &
9111                                             / REAL( average_count_3d, KIND=wp )
9112                         ENDDO
9113                      ENDDO
9114                   ENDDO
9115                ENDDO
9116             ENDDO
9117             
9118          CASE ( 'PM2.5' )
9119             DO  i = nxlg, nxrg
9120                DO  j = nysg, nyng
9121                   DO  k = nzb, nzt+1
9122                      PM25_av(k,j,i) = PM25_av(k,j,i)                          &
9123                                        / REAL( average_count_3d, KIND=wp )
9124                   ENDDO
9125                ENDDO
9126             ENDDO
9127             
9128          CASE ( 'PM10' )
9129             DO  i = nxlg, nxrg
9130                DO  j = nysg, nyng
9131                   DO  k = nzb, nzt+1
9132                      PM10_av(k,j,i) = PM10_av(k,j,i)                          &
9133                                        / REAL( average_count_3d, KIND=wp )
9134                   ENDDO
9135                ENDDO
9136             ENDDO
9137             
9138          CASE ( 's_BC', 's_DU', 's_H2O', 's_NH', 's_NO', 's_OC', 's_SO4',     &
9139                 's_SS' )
9140             IF ( is_used( prtcl, TRIM( variable(3:) ) ) )  THEN
9141                icc = get_index( prtcl, TRIM( variable(3:) ) )
9142                IF ( TRIM( variable(3:) ) == 'BC' )   to_be_resorted => s_BC_av
9143                IF ( TRIM( variable(3:) ) == 'DU' )   to_be_resorted => s_DU_av   
9144                IF ( TRIM( variable(3:) ) == 'NH' )   to_be_resorted => s_NH_av   
9145                IF ( TRIM( variable(3:) ) == 'NO' )   to_be_resorted => s_NO_av   
9146                IF ( TRIM( variable(3:) ) == 'OC' )   to_be_resorted => s_OC_av   
9147                IF ( TRIM( variable(3:) ) == 'SO4' )  to_be_resorted => s_SO4_av   
9148                IF ( TRIM( variable(3:) ) == 'SS' )   to_be_resorted => s_SS_av 
9149                DO  i = nxlg, nxrg
9150                   DO  j = nysg, nyng
9151                      DO  k = nzb, nzt+1
9152                         to_be_resorted(k,j,i) = to_be_resorted(k,j,i)         &
9153                                             / REAL( average_count_3d, KIND=wp )
9154                      ENDDO
9155                   ENDDO
9156                ENDDO
9157             ENDIF
9158
9159       END SELECT
9160
9161    ENDIF
9162
9163 END SUBROUTINE salsa_3d_data_averaging
9164
9165
9166!------------------------------------------------------------------------------!
9167!
9168! Description:
9169! ------------
9170!> Subroutine defining 2D output variables
9171!------------------------------------------------------------------------------!
9172 SUBROUTINE salsa_data_output_2d( av, variable, found, grid, mode,             &
9173                                      local_pf, two_d )
9174 
9175    USE indices
9176
9177    USE kinds
9178
9179    IMPLICIT NONE
9180
9181    CHARACTER (LEN=*) ::  grid       !<
9182    CHARACTER (LEN=*) ::  mode       !<
9183    CHARACTER (LEN=*) ::  variable   !<
9184    CHARACTER (LEN=5) ::  vari       !<  trimmed format of variable
9185
9186    INTEGER(iwp) ::  av   !<
9187    INTEGER(iwp) ::  b    !<
9188    INTEGER(iwp) ::  c    !<
9189    INTEGER(iwp) ::  i    !<
9190    INTEGER(iwp) ::  icc  !< index of a chemical compound
9191    INTEGER(iwp) ::  j    !<
9192    INTEGER(iwp) ::  k    !<
9193
9194    LOGICAL ::  found   !<
9195    LOGICAL ::  two_d   !< flag parameter that indicates 2D variables
9196                        !< (horizontal cross sections)
9197   
9198    REAL(wp) ::  df       !< For calculating LDSA: fraction of particles
9199                          !< depositing in the alveolar (or tracheobronchial)
9200                          !< region of the lung. Depends on the particle size
9201    REAL(wp) ::  mean_d   !< Particle diameter in micrometres
9202    REAL(wp) ::  nc       !< Particle number concentration in units 1/cm**3
9203    REAL(wp), DIMENSION(nxl:nxr,nys:nyn,nzb:nzt+1) ::  local_pf !< local
9204       !< array to which output data is resorted to
9205    REAL(wp) ::  temp_bin !<
9206    REAL(wp), DIMENSION(:,:,:), POINTER ::  to_be_resorted  !< points to
9207                                                     !< selected output variable
9208   
9209    found = .TRUE.
9210    temp_bin  = 0.0_wp
9211   
9212    IF ( TRIM( variable(1:2) ) == 'g_' )  THEN
9213       vari = TRIM( variable( 3:LEN( TRIM( variable ) ) - 3 ) )
9214       IF ( av == 0 )  THEN
9215          IF ( vari == 'H2SO4')  icc = 1
9216          IF ( vari == 'HNO3')   icc = 2
9217          IF ( vari == 'NH3')    icc = 3
9218          IF ( vari == 'OCNV')   icc = 4
9219          IF ( vari == 'OCSV')   icc = 5
9220          DO  i = nxl, nxr
9221             DO  j = nys, nyn
9222                DO  k = nzb, nzt+1
9223                   local_pf(i,j,k) = MERGE( salsa_gas(icc)%conc(k,j,i),        &
9224                                            REAL( -999.0_wp, KIND = wp ),      &
9225                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9226                ENDDO
9227             ENDDO
9228          ENDDO
9229       ELSE
9230          IF ( vari == 'H2SO4' )  to_be_resorted => g_H2SO4_av
9231          IF ( vari == 'HNO3' )   to_be_resorted => g_HNO3_av   
9232          IF ( vari == 'NH3' )    to_be_resorted => g_NH3_av   
9233          IF ( vari == 'OCNV' )   to_be_resorted => g_OCNV_av   
9234          IF ( vari == 'OCSV' )   to_be_resorted => g_OCSV_av       
9235          DO  i = nxl, nxr
9236             DO  j = nys, nyn
9237                DO  k = nzb, nzt+1
9238                   local_pf(i,j,k) = MERGE( to_be_resorted(k,j,i),             &
9239                                            REAL( -999.0_wp, KIND = wp ),      &
9240                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9241                ENDDO
9242             ENDDO
9243          ENDDO
9244       ENDIF
9245
9246       IF ( mode == 'xy' )  grid = 'zu'
9247
9248    ELSEIF ( TRIM( variable(1:4) ) == 'LDSA' )  THEN
9249       IF ( av == 0 )  THEN
9250          DO  i = nxl, nxr
9251             DO  j = nys, nyn
9252                DO  k = nzb, nzt+1
9253                   temp_bin = 0.0_wp
9254                   DO  b = 1, nbins
9255!                     
9256!--                   Diameter in micrometres
9257                      mean_d = 1.0E+6_wp * Ra_dry(k,j,i,b) * 2.0_wp 
9258!                               
9259!--                   Deposition factor: alveolar                               
9260                      df = ( 0.01555_wp / mean_d ) * ( EXP( -0.416_wp * ( LOG( &
9261                             mean_d ) + 2.84_wp )**2.0_wp ) + 19.11_wp * EXP(  &
9262                            -0.482_wp * ( LOG( mean_d ) - 1.362_wp )**2.0_wp ) )
9263!                                   
9264!--                   Number concentration in 1/cm3
9265                      nc = 1.0E-6_wp * aerosol_number(b)%conc(k,j,i)
9266!                         
9267!--                   Lung-deposited surface area LDSA (units mum2/cm3)                       
9268                      temp_bin = temp_bin + pi * mean_d**2.0_wp * df * nc 
9269                   ENDDO
9270                   local_pf(i,j,k) = MERGE( temp_bin,  REAL( -999.0_wp,        &
9271                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9272                ENDDO
9273             ENDDO
9274          ENDDO
9275       ELSE
9276          DO  i = nxl, nxr
9277             DO  j = nys, nyn
9278                DO  k = nzb, nzt+1
9279                   local_pf(i,j,k) = MERGE( LDSA_av(k,j,i), REAL( -999.0_wp,   &
9280                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9281                ENDDO
9282             ENDDO
9283          ENDDO
9284       ENDIF
9285
9286       IF ( mode == 'xy' )  grid = 'zu'
9287   
9288    ELSEIF ( TRIM( variable(1:6) ) == 'N_bin1' )  THEN
9289       IF ( av == 0 )  THEN
9290          DO  i = nxl, nxr
9291             DO  j = nys, nyn
9292                DO  k = nzb, nzt+1                     
9293                   local_pf(i,j,k) = MERGE( aerosol_number(1)%conc(k,j,i),     &
9294                                            REAL( -999.0_wp, KIND = wp ),      &
9295                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9296                ENDDO
9297             ENDDO
9298          ENDDO
9299       ELSE
9300          DO  i = nxl, nxr
9301             DO  j = nys, nyn
9302                DO  k = nzb, nzt+1                     
9303                   local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,1),                 &
9304                                            REAL( -999.0_wp, KIND = wp ),      &
9305                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9306                ENDDO
9307             ENDDO
9308          ENDDO
9309       ENDIF
9310   
9311    ELSEIF ( TRIM( variable(1:6) ) == 'N_bin2' )  THEN
9312       IF ( av == 0 )  THEN
9313          DO  i = nxl, nxr
9314             DO  j = nys, nyn
9315                DO  k = nzb, nzt+1                     
9316                   local_pf(i,j,k) = MERGE( aerosol_number(2)%conc(k,j,i),     &
9317                                            REAL( -999.0_wp, KIND = wp ),      &
9318                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9319                ENDDO
9320             ENDDO
9321          ENDDO
9322       ELSE
9323          DO  i = nxl, nxr
9324             DO  j = nys, nyn
9325                DO  k = nzb, nzt+1                     
9326                   local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,2),                 &
9327                                            REAL( -999.0_wp, KIND = wp ),      &
9328                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9329                ENDDO
9330             ENDDO
9331          ENDDO
9332       ENDIF
9333       
9334    ELSEIF ( TRIM( variable(1:6) ) == 'N_bin3' )  THEN
9335       IF ( av == 0 )  THEN
9336          DO  i = nxl, nxr
9337             DO  j = nys, nyn
9338                DO  k = nzb, nzt+1                     
9339                   local_pf(i,j,k) = MERGE( aerosol_number(3)%conc(k,j,i),     &
9340                                            REAL( -999.0_wp, KIND = wp ),      &
9341                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9342                ENDDO
9343             ENDDO
9344          ENDDO
9345       ELSE
9346          DO  i = nxl, nxr
9347             DO  j = nys, nyn
9348                DO  k = nzb, nzt+1                     
9349                   local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,3),                 &
9350                                            REAL( -999.0_wp, KIND = wp ),      &
9351                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9352                ENDDO
9353             ENDDO
9354          ENDDO
9355       ENDIF
9356   
9357    ELSEIF ( TRIM( variable(1:6) ) == 'N_bin4' )  THEN
9358       IF ( av == 0 )  THEN
9359          DO  i = nxl, nxr
9360             DO  j = nys, nyn
9361                DO  k = nzb, nzt+1                     
9362                   local_pf(i,j,k) = MERGE( aerosol_number(4)%conc(k,j,i),     &
9363                                            REAL( -999.0_wp, KIND = wp ),      &
9364                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9365                ENDDO
9366             ENDDO
9367          ENDDO
9368       ELSE
9369          DO  i = nxl, nxr
9370             DO  j = nys, nyn
9371                DO  k = nzb, nzt+1                     
9372                   local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,4),                 &
9373                                            REAL( -999.0_wp, KIND = wp ),      &
9374                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9375                ENDDO
9376             ENDDO
9377          ENDDO
9378       ENDIF
9379       
9380    ELSEIF ( TRIM( variable(1:6) ) == 'N_bin5' )  THEN
9381       IF ( av == 0 )  THEN
9382          DO  i = nxl, nxr
9383             DO  j = nys, nyn
9384                DO  k = nzb, nzt+1                     
9385                   local_pf(i,j,k) = MERGE( aerosol_number(5)%conc(k,j,i),     &
9386                                            REAL( -999.0_wp, KIND = wp ),      &
9387                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9388                ENDDO
9389             ENDDO
9390          ENDDO
9391       ELSE
9392          DO  i = nxl, nxr
9393             DO  j = nys, nyn
9394                DO  k = nzb, nzt+1                     
9395                   local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,5),                 &
9396                                            REAL( -999.0_wp, KIND = wp ),      &
9397                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9398                ENDDO
9399             ENDDO
9400          ENDDO
9401       ENDIF
9402       
9403    ELSEIF ( TRIM( variable(1:6) ) == 'N_bin6' )  THEN
9404       IF ( av == 0 )  THEN
9405          DO  i = nxl, nxr
9406             DO  j = nys, nyn
9407                DO  k = nzb, nzt+1                     
9408                   local_pf(i,j,k) = MERGE( aerosol_number(6)%conc(k,j,i),     &
9409                                            REAL( -999.0_wp, KIND = wp ),      &
9410                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9411                ENDDO
9412             ENDDO
9413          ENDDO
9414       ELSE
9415          DO  i = nxl, nxr
9416             DO  j = nys, nyn
9417                DO  k = nzb, nzt+1                     
9418                   local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,6),                 &
9419                                            REAL( -999.0_wp, KIND = wp ),      &
9420                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9421                ENDDO
9422             ENDDO
9423          ENDDO
9424       ENDIF
9425       
9426    ELSEIF ( TRIM( variable(1:6) ) == 'N_bin7' )  THEN
9427       IF ( av == 0 )  THEN
9428          DO  i = nxl, nxr
9429             DO  j = nys, nyn
9430                DO  k = nzb, nzt+1                     
9431                   local_pf(i,j,k) = MERGE( aerosol_number(7)%conc(k,j,i),     &
9432                                            REAL( -999.0_wp, KIND = wp ),      &
9433                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9434                ENDDO
9435             ENDDO
9436          ENDDO
9437       ELSE
9438          DO  i = nxl, nxr
9439             DO  j = nys, nyn
9440                DO  k = nzb, nzt+1                     
9441                   local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,7),                 &
9442                                            REAL( -999.0_wp, KIND = wp ),      &
9443                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9444                ENDDO
9445             ENDDO
9446          ENDDO
9447       ENDIF
9448       
9449    ELSEIF ( TRIM( variable(1:6) ) == 'N_bin8' )  THEN
9450       IF ( av == 0 )  THEN
9451          DO  i = nxl, nxr
9452             DO  j = nys, nyn
9453                DO  k = nzb, nzt+1                     
9454                   local_pf(i,j,k) = MERGE( aerosol_number(8)%conc(k,j,i),     &
9455                                            REAL( -999.0_wp, KIND = wp ),      &
9456                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9457                ENDDO
9458             ENDDO
9459          ENDDO
9460       ELSE
9461          DO  i = nxl, nxr
9462             DO  j = nys, nyn
9463                DO  k = nzb, nzt+1                     
9464                   local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,8),                 &
9465                                            REAL( -999.0_wp, KIND = wp ),      &
9466                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9467                ENDDO
9468             ENDDO
9469          ENDDO
9470       ENDIF
9471       
9472    ELSEIF ( TRIM( variable(1:6) ) == 'N_bin9' )  THEN
9473       IF ( av == 0 )  THEN
9474          DO  i = nxl, nxr
9475             DO  j = nys, nyn
9476                DO  k = nzb, nzt+1                     
9477                   local_pf(i,j,k) = MERGE( aerosol_number(9)%conc(k,j,i),     &
9478                                            REAL( -999.0_wp, KIND = wp ),      &
9479                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9480                ENDDO
9481             ENDDO
9482          ENDDO
9483       ELSE
9484          DO  i = nxl, nxr
9485             DO  j = nys, nyn
9486                DO  k = nzb, nzt+1                     
9487                   local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,9),                 &
9488                                            REAL( -999.0_wp, KIND = wp ),      &
9489                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9490                ENDDO
9491             ENDDO
9492          ENDDO
9493       ENDIF
9494   
9495    ELSEIF ( TRIM( variable(1:7) ) == 'N_bin10' )  THEN
9496       IF ( av == 0 )  THEN
9497          DO  i = nxl, nxr
9498             DO  j = nys, nyn
9499                DO  k = nzb, nzt+1                     
9500                   local_pf(i,j,k) = MERGE( aerosol_number(10)%conc(k,j,i),    &
9501                                            REAL( -999.0_wp, KIND = wp ),      &
9502                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9503                ENDDO
9504             ENDDO
9505          ENDDO
9506       ELSE
9507          DO  i = nxl, nxr
9508             DO  j = nys, nyn
9509                DO  k = nzb, nzt+1                     
9510                   local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,10),                &
9511                                            REAL( -999.0_wp, KIND = wp ),      &
9512                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9513                ENDDO
9514             ENDDO
9515          ENDDO
9516       ENDIF
9517       
9518    ELSEIF ( TRIM( variable(1:7) ) == 'N_bin11' )  THEN
9519       IF ( av == 0 )  THEN
9520          DO  i = nxl, nxr
9521             DO  j = nys, nyn
9522                DO  k = nzb, nzt+1                     
9523                   local_pf(i,j,k) = MERGE( aerosol_number(11)%conc(k,j,i),    &
9524                                            REAL( -999.0_wp, KIND = wp ),      &
9525                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9526                ENDDO
9527             ENDDO
9528          ENDDO
9529       ELSE
9530          DO  i = nxl, nxr
9531             DO  j = nys, nyn
9532                DO  k = nzb, nzt+1                     
9533                   local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,11),                &
9534                                            REAL( -999.0_wp, KIND = wp ),      &
9535                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9536                ENDDO
9537             ENDDO
9538          ENDDO
9539       ENDIF
9540       
9541    ELSEIF ( TRIM( variable(1:7) ) == 'N_bin12' )  THEN
9542       IF ( av == 0 )  THEN
9543          DO  i = nxl, nxr
9544             DO  j = nys, nyn
9545                DO  k = nzb, nzt+1                     
9546                   local_pf(i,j,k) = MERGE( aerosol_number(12)%conc(k,j,i),    &
9547                                            REAL( -999.0_wp, KIND = wp ),      &
9548                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9549                ENDDO
9550             ENDDO
9551          ENDDO
9552       ELSE
9553          DO  i = nxl, nxr
9554             DO  j = nys, nyn
9555                DO  k = nzb, nzt+1                     
9556                   local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,12),                &
9557                                            REAL( -999.0_wp, KIND = wp ),      &
9558                                            BTEST( wall_flags_0(k,j,i), 0 ) ) 
9559                ENDDO
9560             ENDDO
9561          ENDDO
9562       ENDIF
9563   
9564    ELSEIF ( TRIM( variable(1:4) ) == 'Ntot' )  THEN
9565       IF ( av == 0 )  THEN
9566          DO  i = nxl, nxr
9567             DO  j = nys, nyn
9568                DO  k = nzb, nzt+1
9569                   temp_bin = 0.0_wp
9570                   DO  b = 1, nbins
9571                      temp_bin = temp_bin + aerosol_number(b)%conc(k,j,i)
9572                   ENDDO
9573                   local_pf(i,j,k) = MERGE( temp_bin, REAL( -999.0_wp,         &
9574                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9575                ENDDO
9576             ENDDO
9577          ENDDO
9578       ELSE
9579          DO  i = nxl, nxr
9580             DO  j = nys, nyn
9581                DO  k = nzb, nzt+1
9582                   local_pf(i,j,k) = MERGE( Ntot_av(k,j,i), REAL( -999.0_wp,   &
9583                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9584                ENDDO
9585             ENDDO
9586          ENDDO
9587       ENDIF
9588
9589       IF ( mode == 'xy' )  grid = 'zu'
9590   
9591   
9592    ELSEIF ( TRIM( variable(1:6) ) == 'm_bin1' )  THEN
9593       IF ( av == 0 )  THEN
9594          DO  i = nxl, nxr
9595             DO  j = nys, nyn
9596                DO  k = nzb, nzt+1   
9597                   temp_bin = 0.0_wp
9598                   DO  c = 1, ncc_tot*nbins, nbins
9599                      temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
9600                   ENDDO
9601                   local_pf(i,j,k) = MERGE( temp_bin, REAL( -999.0_wp,         &
9602                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) )
9603                ENDDO
9604             ENDDO
9605          ENDDO
9606       ELSE
9607          DO  i = nxl, nxr
9608             DO  j = nys, nyn
9609                DO  k = nzb, nzt+1                     
9610                   local_pf(i,j,k) = MERGE( mbins_av(k,j,i,1), REAL( -999.0_wp,&
9611                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9612                ENDDO
9613             ENDDO
9614          ENDDO
9615       ENDIF
9616   
9617    ELSEIF ( TRIM( variable(1:6) ) == 'm_bin2' )  THEN
9618       IF ( av == 0 )  THEN
9619          DO  i = nxl, nxr
9620             DO  j = nys, nyn
9621                DO  k = nzb, nzt+1   
9622                   temp_bin = 0.0_wp
9623                   DO  c = 2, ncc_tot*nbins, nbins
9624                      temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
9625                   ENDDO
9626                   local_pf(i,j,k) = MERGE( temp_bin, REAL( -999.0_wp,         &
9627                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) )
9628                ENDDO
9629             ENDDO
9630          ENDDO
9631       ELSE
9632          DO  i = nxl, nxr
9633             DO  j = nys, nyn
9634                DO  k = nzb, nzt+1                     
9635                   local_pf(i,j,k) = MERGE( mbins_av(k,j,i,2), REAL( -999.0_wp,&
9636                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9637                ENDDO
9638             ENDDO
9639          ENDDO
9640       ENDIF
9641       
9642    ELSEIF ( TRIM( variable(1:6) ) == 'm_bin3' )  THEN
9643       IF ( av == 0 )  THEN
9644          DO  i = nxl, nxr
9645             DO  j = nys, nyn
9646                DO  k = nzb, nzt+1   
9647                   temp_bin = 0.0_wp
9648                   DO  c = 3, ncc_tot*nbins, nbins
9649                      temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
9650                   ENDDO
9651                   local_pf(i,j,k) = MERGE( temp_bin, REAL( -999.0_wp,         &
9652                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) )
9653                ENDDO
9654             ENDDO
9655          ENDDO
9656       ELSE
9657          DO  i = nxl, nxr
9658             DO  j = nys, nyn
9659                DO  k = nzb, nzt+1                     
9660                   local_pf(i,j,k) = MERGE( mbins_av(k,j,i,3), REAL( -999.0_wp,&
9661                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9662                ENDDO
9663             ENDDO
9664          ENDDO
9665       ENDIF
9666       
9667    ELSEIF ( TRIM( variable(1:6) ) == 'm_bin4' )  THEN
9668       IF ( av == 0 )  THEN
9669          DO  i = nxl, nxr
9670             DO  j = nys, nyn
9671                DO  k = nzb, nzt+1   
9672                   temp_bin = 0.0_wp
9673                   DO  c = 4, ncc_tot*nbins, nbins
9674                      temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
9675                   ENDDO
9676                   local_pf(i,j,k) = MERGE( temp_bin, REAL( -999.0_wp,         &
9677                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) )
9678                ENDDO
9679             ENDDO
9680          ENDDO
9681       ELSE
9682          DO  i = nxl, nxr
9683             DO  j = nys, nyn
9684                DO  k = nzb, nzt+1                     
9685                   local_pf(i,j,k) = MERGE( mbins_av(k,j,i,4), REAL( -999.0_wp,&
9686                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9687                ENDDO
9688             ENDDO
9689          ENDDO
9690       ENDIF
9691       
9692    ELSEIF ( TRIM( variable(1:6) ) == 'm_bin5' )  THEN
9693       IF ( av == 0 )  THEN
9694          DO  i = nxl, nxr
9695             DO  j = nys, nyn
9696                DO  k = nzb, nzt+1   
9697                   temp_bin = 0.0_wp
9698                   DO  c = 5, ncc_tot*nbins, nbins
9699                      temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
9700                   ENDDO
9701                   local_pf(i,j,k) = MERGE( temp_bin, REAL( -999.0_wp,         &
9702                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) )
9703                ENDDO
9704             ENDDO
9705          ENDDO
9706       ELSE
9707          DO  i = nxl, nxr
9708             DO  j = nys, nyn
9709                DO  k = nzb, nzt+1                     
9710                   local_pf(i,j,k) = MERGE( mbins_av(k,j,i,5), REAL( -999.0_wp,&
9711                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9712                ENDDO
9713             ENDDO
9714          ENDDO
9715       ENDIF
9716       
9717    ELSEIF ( TRIM( variable(1:6) ) == 'm_bin6' )  THEN
9718       IF ( av == 0 )  THEN
9719          DO  i = nxl, nxr
9720             DO  j = nys, nyn
9721                DO  k = nzb, nzt+1   
9722                   temp_bin = 0.0_wp
9723                   DO  c = 6, ncc_tot*nbins, nbins
9724                      temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
9725                   ENDDO
9726                   local_pf(i,j,k) = MERGE( temp_bin, REAL( -999.0_wp,         &
9727                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) )
9728                ENDDO
9729             ENDDO
9730          ENDDO
9731       ELSE
9732          DO  i = nxl, nxr
9733             DO  j = nys, nyn
9734                DO  k = nzb, nzt+1                     
9735                   local_pf(i,j,k) = MERGE( mbins_av(k,j,i,6), REAL( -999.0_wp,&
9736                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9737                ENDDO
9738             ENDDO
9739          ENDDO
9740       ENDIF
9741       
9742    ELSEIF ( TRIM( variable(1:6) ) == 'm_bin7' )  THEN
9743       IF ( av == 0 )  THEN
9744          DO  i = nxl, nxr
9745             DO  j = nys, nyn
9746                DO  k = nzb, nzt+1   
9747                   temp_bin = 0.0_wp
9748                   DO  c = 7, ncc_tot*nbins, nbins
9749                      temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
9750                   ENDDO
9751                   local_pf(i,j,k) = MERGE( temp_bin, REAL( -999.0_wp,         &
9752                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) )
9753                ENDDO
9754             ENDDO
9755          ENDDO
9756       ELSE
9757          DO  i = nxl, nxr
9758             DO  j = nys, nyn
9759                DO  k = nzb, nzt+1                     
9760                   local_pf(i,j,k) = MERGE( mbins_av(k,j,i,7), REAL( -999.0_wp,&
9761                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9762                ENDDO
9763             ENDDO
9764          ENDDO
9765       ENDIF
9766       
9767    ELSEIF ( TRIM( variable(1:6) ) == 'm_bin8' )  THEN
9768       IF ( av == 0 )  THEN
9769          DO  i = nxl, nxr
9770             DO  j = nys, nyn
9771                DO  k = nzb, nzt+1   
9772                   temp_bin = 0.0_wp
9773                   DO  c = 8, ncc_tot*nbins, nbins
9774                      temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
9775                   ENDDO
9776                   local_pf(i,j,k) = MERGE( temp_bin, REAL( -999.0_wp,         &
9777                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) )
9778                ENDDO
9779             ENDDO
9780          ENDDO
9781       ELSE
9782          DO  i = nxl, nxr
9783             DO  j = nys, nyn
9784                DO  k = nzb, nzt+1                     
9785                   local_pf(i,j,k) = MERGE( mbins_av(k,j,i,8), REAL( -999.0_wp,&
9786                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9787                ENDDO
9788             ENDDO
9789          ENDDO
9790       ENDIF
9791       
9792    ELSEIF ( TRIM( variable(1:6) ) == 'm_bin9' )  THEN
9793       IF ( av == 0 )  THEN
9794          DO  i = nxl, nxr
9795             DO  j = nys, nyn
9796                DO  k = nzb, nzt+1   
9797                   temp_bin = 0.0_wp
9798                   DO  c = 9, ncc_tot*nbins, nbins
9799                      temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
9800                   ENDDO
9801                   local_pf(i,j,k) = MERGE( temp_bin, REAL( -999.0_wp,         &
9802                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) )
9803                ENDDO
9804             ENDDO
9805          ENDDO
9806       ELSE
9807          DO  i = nxl, nxr
9808             DO  j = nys, nyn
9809                DO  k = nzb, nzt+1                     
9810                   local_pf(i,j,k) = MERGE( mbins_av(k,j,i,9), REAL( -999.0_wp,&
9811                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9812                ENDDO
9813             ENDDO
9814          ENDDO
9815       ENDIF
9816       
9817    ELSEIF ( TRIM( variable(1:7) ) == 'm_bin10' )  THEN
9818       IF ( av == 0 )  THEN
9819          DO  i = nxl, nxr
9820             DO  j = nys, nyn
9821                DO  k = nzb, nzt+1   
9822                   temp_bin = 0.0_wp
9823                   DO  c = 10, ncc_tot*nbins, nbins
9824                      temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
9825                   ENDDO
9826                   local_pf(i,j,k) = MERGE( temp_bin, REAL( -999.0_wp,         &
9827                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) )
9828                ENDDO
9829             ENDDO
9830          ENDDO
9831       ELSE
9832          DO  i = nxl, nxr
9833             DO  j = nys, nyn
9834                DO  k = nzb, nzt+1                     
9835                   local_pf(i,j,k) = MERGE( mbins_av(k,j,i,10), REAL(          &
9836                       -999.0_wp, KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9837                ENDDO
9838             ENDDO
9839          ENDDO
9840       ENDIF
9841       
9842    ELSEIF ( TRIM( variable(1:7) ) == 'm_bin11' )  THEN
9843       IF ( av == 0 )  THEN
9844          DO  i = nxl, nxr
9845             DO  j = nys, nyn
9846                DO  k = nzb, nzt+1   
9847                   temp_bin = 0.0_wp
9848                   DO  c = 11, ncc_tot*nbins, nbins
9849                      temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
9850                   ENDDO
9851                   local_pf(i,j,k) = MERGE( temp_bin, REAL( -999.0_wp,         &
9852                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) )
9853                ENDDO
9854             ENDDO
9855          ENDDO
9856       ELSE
9857          DO  i = nxl, nxr
9858             DO  j = nys, nyn
9859                DO  k = nzb, nzt+1                     
9860                   local_pf(i,j,k) = MERGE( mbins_av(k,j,i,11), REAL(          &
9861                       -999.0_wp, KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9862                ENDDO
9863             ENDDO
9864          ENDDO
9865       ENDIF
9866       
9867    ELSEIF ( TRIM( variable(1:7) ) == 'm_bin12' )  THEN
9868       IF ( av == 0 )  THEN
9869          DO  i = nxl, nxr
9870             DO  j = nys, nyn
9871                DO  k = nzb, nzt+1   
9872                   temp_bin = 0.0_wp
9873                   DO  c = 12, ncc_tot*nbins, nbins
9874                      temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
9875                   ENDDO
9876                   local_pf(i,j,k) = MERGE( temp_bin, REAL( -999.0_wp,         &
9877                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) )
9878                ENDDO
9879             ENDDO
9880          ENDDO
9881       ELSE
9882          DO  i = nxl, nxr
9883             DO  j = nys, nyn
9884                DO  k = nzb, nzt+1                     
9885                   local_pf(i,j,k) = MERGE( mbins_av(k,j,i,12), REAL(          &
9886                       -999.0_wp, KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9887                ENDDO
9888             ENDDO
9889          ENDDO
9890       ENDIF
9891   
9892    ELSEIF ( TRIM( variable(1:5) ) == 'PM2.5' )  THEN
9893       IF ( av == 0 )  THEN
9894          DO  i = nxl, nxr
9895             DO  j = nys, nyn
9896                DO  k = nzb, nzt+1
9897                   temp_bin = 0.0_wp
9898                   DO  b = 1, nbins
9899                      IF ( 2.0_wp * Ra_dry(k,j,i,b) <= 2.5E-6_wp )  THEN
9900                         DO  c = b, nbins*ncc, nbins
9901                            temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
9902                         ENDDO
9903                      ENDIF
9904                   ENDDO
9905                   local_pf(i,j,k) = MERGE( temp_bin, REAL( -999.0_wp,         &
9906                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9907                ENDDO
9908             ENDDO
9909          ENDDO
9910       ELSE
9911          DO  i = nxl, nxr
9912             DO  j = nys, nyn
9913                DO  k = nzb, nzt+1
9914                   local_pf(i,j,k) = MERGE( PM25_av(k,j,i), REAL( -999.0_wp,   &
9915                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9916                ENDDO
9917             ENDDO
9918          ENDDO
9919       ENDIF
9920
9921       IF ( mode == 'xy' )  grid = 'zu'
9922   
9923   
9924    ELSEIF ( TRIM( variable(1:4) ) == 'PM10' )  THEN
9925       IF ( av == 0 )  THEN
9926          DO  i = nxl, nxr
9927             DO  j = nys, nyn
9928                DO  k = nzb, nzt+1
9929                   temp_bin = 0.0_wp
9930                   DO  b = 1, nbins
9931                      IF ( 2.0_wp * Ra_dry(k,j,i,b) <= 10.0E-6_wp )  THEN
9932                         DO  c = b, nbins*ncc, nbins
9933                            temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
9934                         ENDDO
9935                      ENDIF
9936                   ENDDO
9937                   local_pf(i,j,k) = MERGE( temp_bin,  REAL( -999.0_wp,        &
9938                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9939                ENDDO
9940             ENDDO
9941          ENDDO
9942       ELSE
9943          DO  i = nxl, nxr
9944             DO  j = nys, nyn
9945                DO  k = nzb, nzt+1
9946                   local_pf(i,j,k) = MERGE( PM10_av(k,j,i), REAL( -999.0_wp,   &
9947                                 KIND = wp ),  BTEST( wall_flags_0(k,j,i), 0 ) ) 
9948                ENDDO
9949             ENDDO
9950          ENDDO
9951       ENDIF
9952
9953       IF ( mode == 'xy' )  grid = 'zu'
9954   
9955    ELSEIF ( TRIM( variable(1:2) ) == 's_' )  THEN
9956       vari = TRIM( variable( 3:LEN( TRIM( variable ) ) - 3 ) )
9957       IF ( is_used( prtcl, vari ) )  THEN
9958          icc = get_index( prtcl, vari )
9959          IF ( av == 0 )  THEN
9960             DO  i = nxl, nxr
9961                DO  j = nys, nyn
9962                   DO  k = nzb, nzt+1
9963                      temp_bin = 0.0_wp
9964                      DO  c = ( icc-1 )*nbins+1, icc*nbins, 1
9965                         temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
9966                      ENDDO
9967                      local_pf(i,j,k) = MERGE( temp_bin, REAL( -999.0_wp,      &
9968                                  KIND = wp ), BTEST( wall_flags_0(k,j,i), 0 ) ) 
9969                   ENDDO
9970                ENDDO
9971             ENDDO
9972          ELSE
9973             IF ( vari == 'BC' )   to_be_resorted => s_BC_av
9974             IF ( vari == 'DU' )   to_be_resorted => s_DU_av   
9975             IF ( vari == 'NH' )   to_be_resorted => s_NH_av   
9976             IF ( vari == 'NO' )   to_be_resorted => s_NO_av   
9977             IF ( vari == 'OC' )   to_be_resorted => s_OC_av   
9978             IF ( vari == 'SO4' )  to_be_resorted => s_SO4_av   
9979             IF ( vari == 'SS' )   to_be_resorted => s_SS_av       
9980             DO  i = nxl, nxr
9981                DO  j = nys, nyn
9982                   DO  k = nzb, nzt+1
9983                      local_pf(i,j,k) = MERGE( to_be_resorted(k,j,i),          &
9984                                               REAL( -999.0_wp, KIND = wp ),   &
9985                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
9986                   ENDDO
9987                ENDDO
9988             ENDDO
9989          ENDIF
9990       ELSE
9991          local_pf = 0.0_wp 
9992       ENDIF
9993
9994       IF ( mode == 'xy' )  grid = 'zu'
9995       
9996    ELSE
9997       found = .FALSE.
9998       grid  = 'none'
9999   
10000    ENDIF
10001 
10002 END SUBROUTINE salsa_data_output_2d
10003
10004 
10005!------------------------------------------------------------------------------!
10006!
10007! Description:
10008! ------------
10009!> Subroutine defining 3D output variables
10010!------------------------------------------------------------------------------!
10011 SUBROUTINE salsa_data_output_3d( av, variable, found, local_pf )
10012
10013    USE indices
10014
10015    USE kinds
10016
10017    IMPLICIT NONE
10018
10019    CHARACTER (LEN=*), INTENT(in) ::  variable   !<
10020   
10021    INTEGER(iwp) ::  av   !<
10022    INTEGER(iwp) ::  c    !<
10023    INTEGER(iwp) ::  i    !<
10024    INTEGER(iwp) ::  icc  !< index of a chemical compound
10025    INTEGER(iwp) ::  j    !<
10026    INTEGER(iwp) ::  k    !<
10027    INTEGER(iwp) ::  n    !<
10028
10029    LOGICAL ::  found   !<
10030    REAL(wp) ::  df       !< For calculating LDSA: fraction of particles
10031                          !< depositing in the alveolar (or tracheobronchial)
10032                          !< region of the lung. Depends on the particle size
10033    REAL(wp) ::  mean_d   !< Particle diameter in micrometres
10034    REAL(wp) ::  nc       !< Particle number concentration in units 1/cm**3
10035
10036    REAL(sp), DIMENSION(nxl:nxr,nys:nyn,nzb:nzt+1) ::  local_pf  !< local
10037                                  !< array to which output data is resorted to
10038    REAL(wp) ::  temp_bin  !<
10039    REAL(wp), DIMENSION(:,:,:), POINTER ::  to_be_resorted  !< points to
10040                                                     !< selected output variable
10041       
10042    found     = .TRUE.
10043    temp_bin  = 0.0_wp
10044   
10045    SELECT CASE ( TRIM( variable ) )
10046   
10047       CASE ( 'g_H2SO4', 'g_HNO3', 'g_NH3', 'g_OCNV',  'g_OCSV' )
10048          IF ( av == 0 )  THEN
10049             IF ( TRIM( variable ) == 'g_H2SO4')  icc = 1
10050             IF ( TRIM( variable ) == 'g_HNO3')   icc = 2
10051             IF ( TRIM( variable ) == 'g_NH3')    icc = 3
10052             IF ( TRIM( variable ) == 'g_OCNV')   icc = 4
10053             IF ( TRIM( variable ) == 'g_OCSV')   icc = 5
10054             
10055             DO  i = nxl, nxr
10056                DO  j = nys, nyn
10057                   DO  k = nzb, nzt+1
10058                      local_pf(i,j,k) = MERGE( salsa_gas(icc)%conc(k,j,i),     &
10059                                               REAL( -999.0_wp, KIND = wp ),   &
10060                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10061                   ENDDO
10062                ENDDO
10063             ENDDO
10064          ELSE
10065             IF ( TRIM( variable(3:) ) == 'H2SO4' ) to_be_resorted => g_H2SO4_av
10066             IF ( TRIM( variable(3:) ) == 'HNO3' )  to_be_resorted => g_HNO3_av   
10067             IF ( TRIM( variable(3:) ) == 'NH3' )   to_be_resorted => g_NH3_av   
10068             IF ( TRIM( variable(3:) ) == 'OCNV' )  to_be_resorted => g_OCNV_av   
10069             IF ( TRIM( variable(3:) ) == 'OCSV' )  to_be_resorted => g_OCSV_av 
10070             DO  i = nxl, nxr
10071                DO  j = nys, nyn
10072                   DO  k = nzb, nzt+1
10073                      local_pf(i,j,k) = MERGE( to_be_resorted(k,j,i),          &
10074                                               REAL( -999.0_wp, KIND = wp ),   &
10075                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10076                   ENDDO
10077                ENDDO
10078             ENDDO
10079          ENDIF
10080         
10081       CASE ( 'LDSA' )
10082          IF ( av == 0 )  THEN
10083             DO  i = nxl, nxr
10084                DO  j = nys, nyn
10085                   DO  k = nzb, nzt+1
10086                      temp_bin = 0.0_wp
10087                      DO  n = 1, nbins
10088!                     
10089!--                      Diameter in micrometres
10090                         mean_d = 1.0E+6_wp * Ra_dry(k,j,i,n) * 2.0_wp 
10091!                               
10092!--                      Deposition factor: alveolar                             
10093                         df = ( 0.01555_wp / mean_d ) * ( EXP( -0.416_wp *     &
10094                                ( LOG( mean_d ) + 2.84_wp )**2.0_wp )          &
10095                                  + 19.11_wp * EXP( -0.482_wp *                &
10096                                  ( LOG( mean_d ) - 1.362_wp )**2.0_wp ) )
10097!                                   
10098!--                      Number concentration in 1/cm3
10099                         nc = 1.0E-6_wp * aerosol_number(n)%conc(k,j,i)
10100!                         
10101!--                      Lung-deposited surface area LDSA (units mum2/cm3)
10102                         temp_bin = temp_bin + pi * mean_d**2.0_wp * df * nc 
10103                      ENDDO
10104                      local_pf(i,j,k) = MERGE( temp_bin,                       &
10105                                               REAL( -999.0_wp, KIND = wp ),   &
10106                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10107                   ENDDO
10108                ENDDO
10109             ENDDO
10110          ELSE
10111             DO  i = nxl, nxr
10112                DO  j = nys, nyn
10113                   DO  k = nzb, nzt+1
10114                      local_pf(i,j,k) = MERGE( LDSA_av(k,j,i),                 &
10115                                               REAL( -999.0_wp, KIND = wp ),   &
10116                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10117                   ENDDO
10118                ENDDO
10119             ENDDO
10120          ENDIF
10121         
10122       CASE ( 'Ntot' )
10123          IF ( av == 0 )  THEN
10124             DO  i = nxl, nxr
10125                DO  j = nys, nyn
10126                   DO  k = nzb, nzt+1
10127                      temp_bin = 0.0_wp
10128                      DO  n = 1, nbins                         
10129                         temp_bin = temp_bin + aerosol_number(n)%conc(k,j,i)
10130                      ENDDO
10131                      local_pf(i,j,k) = MERGE( temp_bin,                       &
10132                                               REAL( -999.0_wp, KIND = wp ),   &
10133                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10134                   ENDDO
10135                ENDDO
10136             ENDDO
10137          ELSE
10138             DO  i = nxl, nxr
10139                DO  j = nys, nyn
10140                   DO  k = nzb, nzt+1
10141                      local_pf(i,j,k) = MERGE( Ntot_av(k,j,i),                 &
10142                                               REAL( -999.0_wp, KIND = wp ),   &
10143                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10144                   ENDDO
10145                ENDDO
10146             ENDDO
10147          ENDIF
10148         
10149       CASE ( 'PM2.5' )
10150          IF ( av == 0 )  THEN
10151             DO  i = nxl, nxr
10152                DO  j = nys, nyn
10153                   DO  k = nzb, nzt+1
10154                      temp_bin = 0.0_wp
10155                      DO  n = 1, nbins
10156                         IF ( 2.0_wp * Ra_dry(k,j,i,n) <= 2.5E-6_wp )  THEN
10157                            DO  c = n, nbins*ncc, nbins
10158                               temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
10159                            ENDDO
10160                         ENDIF
10161                      ENDDO
10162                      local_pf(i,j,k) = MERGE( temp_bin,                       &
10163                                               REAL( -999.0_wp, KIND = wp ),   &
10164                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10165                   ENDDO
10166                ENDDO
10167             ENDDO
10168          ELSE
10169             DO  i = nxl, nxr
10170                DO  j = nys, nyn
10171                   DO  k = nzb, nzt+1
10172                      local_pf(i,j,k) = MERGE( PM25_av(k,j,i),                 &
10173                                               REAL( -999.0_wp, KIND = wp ),   &
10174                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10175                   ENDDO
10176                ENDDO
10177             ENDDO
10178          ENDIF
10179         
10180       CASE ( 'PM10' )
10181          IF ( av == 0 )  THEN
10182             DO  i = nxl, nxr
10183                DO  j = nys, nyn
10184                   DO  k = nzb, nzt+1
10185                      temp_bin = 0.0_wp
10186                      DO  n = 1, nbins
10187                         IF ( 2.0_wp * Ra_dry(k,j,i,n) <= 10.0E-6_wp )  THEN
10188                            DO  c = n, nbins*ncc, nbins
10189                               temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
10190                            ENDDO
10191                         ENDIF
10192                      ENDDO
10193                      local_pf(i,j,k) = MERGE( temp_bin,                       &
10194                                               REAL( -999.0_wp, KIND = wp ),   &
10195                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10196                   ENDDO
10197                ENDDO
10198             ENDDO
10199          ELSE
10200             DO  i = nxl, nxr
10201                DO  j = nys, nyn
10202                   DO  k = nzb, nzt+1
10203                      local_pf(i,j,k) = MERGE( PM10_av(k,j,i),                 &
10204                                               REAL( -999.0_wp, KIND = wp ),   &
10205                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10206                   ENDDO
10207                ENDDO
10208             ENDDO
10209          ENDIF
10210         
10211       CASE ( 'N_bin1' )
10212          IF ( av == 0 )  THEN
10213             DO  i = nxl, nxr
10214                DO  j = nys, nyn
10215                   DO  k = nzb, nzt+1                     
10216                      local_pf(i,j,k) = MERGE( aerosol_number(1)%conc(k,j,i),  &
10217                                               REAL( -999.0_wp, KIND = wp ),   &
10218                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10219                   ENDDO
10220                ENDDO
10221             ENDDO
10222          ELSE
10223             DO  i = nxl, nxr
10224                DO  j = nys, nyn
10225                   DO  k = nzb, nzt+1                     
10226                      local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,1),              &
10227                                               REAL( -999.0_wp, KIND = wp ),   &
10228                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10229                   ENDDO
10230                ENDDO
10231             ENDDO
10232          ENDIF
10233       
10234       CASE ( 'N_bin2' )
10235          IF ( av == 0 )  THEN
10236             DO  i = nxl, nxr
10237                DO  j = nys, nyn
10238                   DO  k = nzb, nzt+1 
10239                      local_pf(i,j,k) = MERGE( aerosol_number(2)%conc(k,j,i),  &
10240                                               REAL( -999.0_wp, KIND = wp ),   &
10241                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10242                   ENDDO
10243                ENDDO
10244             ENDDO
10245          ELSE
10246             DO  i = nxl, nxr
10247                DO  j = nys, nyn
10248                   DO  k = nzb, nzt+1                     
10249                      local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,2),              &
10250                                               REAL( -999.0_wp, KIND = wp ),   &
10251                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10252                   ENDDO
10253                ENDDO
10254             ENDDO
10255          ENDIF
10256         
10257       CASE ( 'N_bin3' )
10258          IF ( av == 0 )  THEN
10259             DO  i = nxl, nxr
10260                DO  j = nys, nyn
10261                   DO  k = nzb, nzt+1                     
10262                      local_pf(i,j,k) = MERGE( aerosol_number(3)%conc(k,j,i),  &
10263                                               REAL( -999.0_wp, KIND = wp ),   &
10264                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10265                   ENDDO
10266                ENDDO
10267             ENDDO
10268          ELSE
10269             DO  i = nxl, nxr
10270                DO  j = nys, nyn
10271                   DO  k = nzb, nzt+1                     
10272                      local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,3),              &
10273                                               REAL( -999.0_wp, KIND = wp ),   &
10274                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10275                   ENDDO
10276                ENDDO
10277             ENDDO
10278          ENDIF
10279       
10280       CASE ( 'N_bin4' )
10281          IF ( av == 0 )  THEN
10282             DO  i = nxl, nxr
10283                DO  j = nys, nyn
10284                   DO  k = nzb, nzt+1   
10285                      local_pf(i,j,k) = MERGE( aerosol_number(4)%conc(k,j,i),  &
10286                                               REAL( -999.0_wp, KIND = wp ),   &
10287                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10288                   ENDDO
10289                ENDDO
10290             ENDDO
10291          ELSE
10292             DO  i = nxl, nxr
10293                DO  j = nys, nyn
10294                   DO  k = nzb, nzt+1                     
10295                      local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,4),              &
10296                                               REAL( -999.0_wp, KIND = wp ),   &
10297                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10298                   ENDDO
10299                ENDDO
10300             ENDDO
10301          ENDIF
10302         
10303       CASE ( 'N_bin5' )
10304          IF ( av == 0 )  THEN
10305             DO  i = nxl, nxr
10306                DO  j = nys, nyn
10307                   DO  k = nzb, nzt+1                     
10308                      local_pf(i,j,k) = MERGE( aerosol_number(5)%conc(k,j,i),  &
10309                                               REAL( -999.0_wp, KIND = wp ),   &
10310                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10311                   ENDDO
10312                ENDDO
10313             ENDDO
10314          ELSE
10315             DO  i = nxl, nxr
10316                DO  j = nys, nyn
10317                   DO  k = nzb, nzt+1                     
10318                      local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,5),              &
10319                                               REAL( -999.0_wp, KIND = wp ),   &
10320                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10321                   ENDDO
10322                ENDDO
10323             ENDDO
10324          ENDIF
10325       
10326       CASE ( 'N_bin6' )
10327          IF ( av == 0 )  THEN
10328             DO  i = nxl, nxr
10329                DO  j = nys, nyn
10330                   DO  k = nzb, nzt+1                     
10331                      local_pf(i,j,k) = MERGE( aerosol_number(6)%conc(k,j,i),  &
10332                                               REAL( -999.0_wp, KIND = wp ),   &
10333                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10334                   ENDDO
10335                ENDDO
10336             ENDDO
10337          ELSE
10338             DO  i = nxl, nxr
10339                DO  j = nys, nyn
10340                   DO  k = nzb, nzt+1                     
10341                      local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,6),              &
10342                                               REAL( -999.0_wp, KIND = wp ),   &
10343                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10344                   ENDDO
10345                ENDDO
10346             ENDDO
10347          ENDIF
10348         
10349       CASE ( 'N_bin7' )
10350          IF ( av == 0 )  THEN
10351             DO  i = nxl, nxr
10352                DO  j = nys, nyn
10353                   DO  k = nzb, nzt+1                     
10354                      local_pf(i,j,k) = MERGE( aerosol_number(7)%conc(k,j,i),  &
10355                                               REAL( -999.0_wp, KIND = wp ),   &
10356                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10357                   ENDDO
10358                ENDDO
10359             ENDDO
10360          ELSE
10361             DO  i = nxl, nxr
10362                DO  j = nys, nyn
10363                   DO  k = nzb, nzt+1                     
10364                      local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,7),              &
10365                                               REAL( -999.0_wp, KIND = wp ),   &
10366                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10367                   ENDDO
10368                ENDDO
10369             ENDDO
10370          ENDIF
10371       
10372       CASE ( 'N_bin8' )
10373          IF ( av == 0 )  THEN
10374             DO  i = nxl, nxr
10375                DO  j = nys, nyn
10376                   DO  k = nzb, nzt+1                 
10377                      local_pf(i,j,k) = MERGE( aerosol_number(8)%conc(k,j,i),  &
10378                                               REAL( -999.0_wp, KIND = wp ),   &
10379                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10380                   ENDDO
10381                ENDDO
10382             ENDDO
10383          ELSE
10384             DO  i = nxl, nxr
10385                DO  j = nys, nyn
10386                   DO  k = nzb, nzt+1                     
10387                      local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,8),              &
10388                                               REAL( -999.0_wp, KIND = wp ),   &
10389                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10390                   ENDDO
10391                ENDDO
10392             ENDDO
10393          ENDIF
10394         
10395       CASE ( 'N_bin9' )
10396          IF ( av == 0 )  THEN
10397             DO  i = nxl, nxr
10398                DO  j = nys, nyn
10399                   DO  k = nzb, nzt+1                     
10400                      local_pf(i,j,k) = MERGE( aerosol_number(9)%conc(k,j,i),  &
10401                                               REAL( -999.0_wp, KIND = wp ),   &
10402                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10403                   ENDDO
10404                ENDDO
10405             ENDDO
10406          ELSE
10407             DO  i = nxl, nxr
10408                DO  j = nys, nyn
10409                   DO  k = nzb, nzt+1                     
10410                      local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,9),              &
10411                                               REAL( -999.0_wp, KIND = wp ),   &
10412                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10413                   ENDDO
10414                ENDDO
10415             ENDDO
10416          ENDIF
10417       
10418       CASE ( 'N_bin10' )
10419          IF ( av == 0 )  THEN
10420             DO  i = nxl, nxr
10421                DO  j = nys, nyn
10422                   DO  k = nzb, nzt+1                     
10423                      local_pf(i,j,k) = MERGE( aerosol_number(10)%conc(k,j,i), &
10424                                               REAL( -999.0_wp, KIND = wp ),   &
10425                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10426                   ENDDO
10427                ENDDO
10428             ENDDO
10429          ELSE
10430             DO  i = nxl, nxr
10431                DO  j = nys, nyn
10432                   DO  k = nzb, nzt+1                     
10433                      local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,10),             &
10434                                               REAL( -999.0_wp, KIND = wp ),   &
10435                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10436                   ENDDO
10437                ENDDO
10438             ENDDO
10439          ENDIF
10440         
10441       CASE ( 'N_bin11' )
10442          IF ( av == 0 )  THEN
10443             DO  i = nxl, nxr
10444                DO  j = nys, nyn
10445                   DO  k = nzb, nzt+1                     
10446                      local_pf(i,j,k) = MERGE( aerosol_number(11)%conc(k,j,i), &
10447                                               REAL( -999.0_wp, KIND = wp ),   &
10448                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10449                   ENDDO
10450                ENDDO
10451             ENDDO
10452          ELSE
10453             DO  i = nxl, nxr
10454                DO  j = nys, nyn
10455                   DO  k = nzb, nzt+1                     
10456                      local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,11),             &
10457                                               REAL( -999.0_wp, KIND = wp ),   &
10458                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10459                   ENDDO
10460                ENDDO
10461             ENDDO
10462          ENDIF
10463         
10464       CASE ( 'N_bin12' )
10465          IF ( av == 0 )  THEN
10466             DO  i = nxl, nxr
10467                DO  j = nys, nyn
10468                   DO  k = nzb, nzt+1                     
10469                      local_pf(i,j,k) = MERGE( aerosol_number(12)%conc(k,j,i), &
10470                                               REAL( -999.0_wp, KIND = wp ),   &
10471                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10472                   ENDDO
10473                ENDDO
10474             ENDDO
10475          ELSE
10476             DO  i = nxl, nxr
10477                DO  j = nys, nyn
10478                   DO  k = nzb, nzt+1                     
10479                      local_pf(i,j,k) = MERGE( Nbins_av(k,j,i,12),             &
10480                                               REAL( -999.0_wp, KIND = wp ),   &
10481                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10482                   ENDDO
10483                ENDDO
10484             ENDDO
10485          ENDIF
10486         
10487       CASE ( 'm_bin1' )
10488          IF ( av == 0 )  THEN
10489             DO  i = nxl, nxr
10490                DO  j = nys, nyn
10491                   DO  k = nzb, nzt+1   
10492                      temp_bin = 0.0_wp
10493                      DO  c = 1, ncc_tot*nbins, nbins
10494                         temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
10495                      ENDDO
10496                      local_pf(i,j,k) = MERGE( temp_bin,                       &
10497                                               REAL( -999.0_wp, KIND = wp ),   &
10498                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10499                   ENDDO
10500                ENDDO
10501             ENDDO
10502          ELSE
10503             DO  i = nxl, nxr
10504                DO  j = nys, nyn
10505                   DO  k = nzb, nzt+1                     
10506                      local_pf(i,j,k) = MERGE( mbins_av(k,j,i,1),              &
10507                                               REAL( -999.0_wp, KIND = wp ),   &
10508                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10509                   ENDDO
10510                ENDDO
10511             ENDDO
10512          ENDIF
10513       
10514       CASE ( 'm_bin2' )
10515          IF ( av == 0 )  THEN
10516             DO  i = nxl, nxr
10517                DO  j = nys, nyn
10518                   DO  k = nzb, nzt+1   
10519                      temp_bin = 0.0_wp
10520                      DO  c = 2, ncc_tot*nbins, nbins
10521                         temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
10522                      ENDDO
10523                      local_pf(i,j,k) = MERGE( temp_bin,                       &
10524                                               REAL( -999.0_wp, KIND = wp ),   &
10525                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10526                   ENDDO
10527                ENDDO
10528             ENDDO
10529          ELSE
10530             DO  i = nxl, nxr
10531                DO  j = nys, nyn
10532                   DO  k = nzb, nzt+1                     
10533                      local_pf(i,j,k) = MERGE( mbins_av(k,j,i,2),              &
10534                                               REAL( -999.0_wp, KIND = wp ),   &
10535                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10536                   ENDDO
10537                ENDDO
10538             ENDDO
10539          ENDIF
10540         
10541       CASE ( 'm_bin3' )
10542          IF ( av == 0 )  THEN
10543             DO  i = nxl, nxr
10544                DO  j = nys, nyn
10545                   DO  k = nzb, nzt+1   
10546                      temp_bin = 0.0_wp
10547                      DO  c = 3, ncc_tot*nbins, nbins
10548                         temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
10549                      ENDDO
10550                      local_pf(i,j,k) = MERGE( temp_bin,                       &
10551                                               REAL( -999.0_wp, KIND = wp ),   &
10552                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10553                   ENDDO
10554                ENDDO
10555             ENDDO
10556          ELSE
10557             DO  i = nxl, nxr
10558                DO  j = nys, nyn
10559                   DO  k = nzb, nzt+1                     
10560                      local_pf(i,j,k) = MERGE( mbins_av(k,j,i,3),              &
10561                                               REAL( -999.0_wp, KIND = wp ),   &
10562                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10563                   ENDDO
10564                ENDDO
10565             ENDDO
10566          ENDIF
10567       
10568       CASE ( 'm_bin4' )
10569          IF ( av == 0 )  THEN
10570             DO  i = nxl, nxr
10571                DO  j = nys, nyn
10572                   DO  k = nzb, nzt+1   
10573                      temp_bin = 0.0_wp
10574                      DO  c = 4, ncc_tot*nbins, nbins
10575                         temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
10576                      ENDDO
10577                      local_pf(i,j,k) = MERGE( temp_bin,                       &
10578                                               REAL( -999.0_wp, KIND = wp ),   &
10579                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10580                   ENDDO
10581                ENDDO
10582             ENDDO
10583          ELSE
10584             DO  i = nxl, nxr
10585                DO  j = nys, nyn
10586                   DO  k = nzb, nzt+1                     
10587                      local_pf(i,j,k) = MERGE( mbins_av(k,j,i,4),              &
10588                                               REAL( -999.0_wp, KIND = wp ),   &
10589                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10590                   ENDDO
10591                ENDDO
10592             ENDDO
10593          ENDIF
10594         
10595       CASE ( 'm_bin5' )
10596          IF ( av == 0 )  THEN
10597             DO  i = nxl, nxr
10598                DO  j = nys, nyn
10599                   DO  k = nzb, nzt+1   
10600                      temp_bin = 0.0_wp
10601                      DO  c = 5, ncc_tot*nbins, nbins
10602                         temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
10603                      ENDDO
10604                      local_pf(i,j,k) = MERGE( temp_bin,                       &
10605                                               REAL( -999.0_wp, KIND = wp ),   &
10606                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10607                   ENDDO
10608                ENDDO
10609             ENDDO
10610          ELSE
10611             DO  i = nxl, nxr
10612                DO  j = nys, nyn
10613                   DO  k = nzb, nzt+1                     
10614                      local_pf(i,j,k) = MERGE( mbins_av(k,j,i,5),              &
10615                                               REAL( -999.0_wp, KIND = wp ),   &
10616                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10617                   ENDDO
10618                ENDDO
10619             ENDDO
10620          ENDIF
10621       
10622       CASE ( 'm_bin6' )
10623          IF ( av == 0 )  THEN
10624             DO  i = nxl, nxr
10625                DO  j = nys, nyn
10626                   DO  k = nzb, nzt+1   
10627                      temp_bin = 0.0_wp
10628                      DO  c = 6, ncc_tot*nbins, nbins
10629                         temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
10630                      ENDDO
10631                      local_pf(i,j,k) = MERGE( temp_bin,                       &
10632                                               REAL( -999.0_wp, KIND = wp ),   &
10633                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10634                   ENDDO
10635                ENDDO
10636             ENDDO
10637          ELSE
10638             DO  i = nxl, nxr
10639                DO  j = nys, nyn
10640                   DO  k = nzb, nzt+1                     
10641                      local_pf(i,j,k) = MERGE( mbins_av(k,j,i,6),              &
10642                                               REAL( -999.0_wp, KIND = wp ),   &
10643                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10644                   ENDDO
10645                ENDDO
10646             ENDDO
10647          ENDIF
10648         
10649       CASE ( 'm_bin7' )
10650          IF ( av == 0 )  THEN
10651             DO  i = nxl, nxr
10652                DO  j = nys, nyn
10653                   DO  k = nzb, nzt+1   
10654                      temp_bin = 0.0_wp
10655                      DO  c = 7, ncc_tot*nbins, nbins
10656                         temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
10657                      ENDDO
10658                      local_pf(i,j,k) = MERGE( temp_bin,                       &
10659                                               REAL( -999.0_wp, KIND = wp ),   &
10660                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10661                   ENDDO
10662                ENDDO
10663             ENDDO
10664          ELSE
10665             DO  i = nxl, nxr
10666                DO  j = nys, nyn
10667                   DO  k = nzb, nzt+1                     
10668                      local_pf(i,j,k) = MERGE( mbins_av(k,j,i,7),              &
10669                                               REAL( -999.0_wp, KIND = wp ),   &
10670                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10671                   ENDDO
10672                ENDDO
10673             ENDDO
10674          ENDIF
10675       
10676       CASE ( 'm_bin8' )
10677          IF ( av == 0 )  THEN
10678             DO  i = nxl, nxr
10679                DO  j = nys, nyn
10680                   DO  k = nzb, nzt+1   
10681                      temp_bin = 0.0_wp
10682                      DO  c = 8, ncc_tot*nbins, nbins
10683                         temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
10684                      ENDDO
10685                      local_pf(i,j,k) = MERGE( temp_bin,                       &
10686                                               REAL( -999.0_wp, KIND = wp ),   &
10687                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10688                   ENDDO
10689                ENDDO
10690             ENDDO
10691          ELSE
10692             DO  i = nxl, nxr
10693                DO  j = nys, nyn
10694                   DO  k = nzb, nzt+1                     
10695                      local_pf(i,j,k) = MERGE( mbins_av(k,j,i,8),              &
10696                                               REAL( -999.0_wp, KIND = wp ),   &
10697                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10698                   ENDDO
10699                ENDDO
10700             ENDDO
10701          ENDIF
10702         
10703       CASE ( 'm_bin9' )
10704          IF ( av == 0 )  THEN
10705             DO  i = nxl, nxr
10706                DO  j = nys, nyn
10707                   DO  k = nzb, nzt+1   
10708                      temp_bin = 0.0_wp
10709                      DO  c = 9, ncc_tot*nbins, nbins
10710                         temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
10711                      ENDDO
10712                      local_pf(i,j,k) = MERGE( temp_bin,                       &
10713                                               REAL( -999.0_wp, KIND = wp ),   &
10714                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10715                   ENDDO
10716                ENDDO
10717             ENDDO
10718          ELSE
10719             DO  i = nxl, nxr
10720                DO  j = nys, nyn
10721                   DO  k = nzb, nzt+1                     
10722                      local_pf(i,j,k) = MERGE( mbins_av(k,j,i,9),              &
10723                                               REAL( -999.0_wp, KIND = wp ),   &
10724                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10725                   ENDDO
10726                ENDDO
10727             ENDDO
10728          ENDIF
10729       
10730       CASE ( 'm_bin10' )
10731          IF ( av == 0 )  THEN
10732             DO  i = nxl, nxr
10733                DO  j = nys, nyn
10734                   DO  k = nzb, nzt+1   
10735                      temp_bin = 0.0_wp
10736                      DO  c = 10, ncc_tot*nbins, nbins
10737                         temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
10738                      ENDDO
10739                      local_pf(i,j,k) = MERGE( temp_bin,                       &
10740                                               REAL( -999.0_wp, KIND = wp ),   &
10741                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10742                   ENDDO
10743                ENDDO
10744             ENDDO
10745          ELSE
10746             DO  i = nxl, nxr
10747                DO  j = nys, nyn
10748                   DO  k = nzb, nzt+1                     
10749                      local_pf(i,j,k) = MERGE( mbins_av(k,j,i,10),             &
10750                                               REAL( -999.0_wp, KIND = wp ),   &
10751                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10752                   ENDDO
10753                ENDDO
10754             ENDDO
10755          ENDIF
10756         
10757       CASE ( 'm_bin11' )
10758          IF ( av == 0 )  THEN
10759             DO  i = nxl, nxr
10760                DO  j = nys, nyn
10761                   DO  k = nzb, nzt+1   
10762                      temp_bin = 0.0_wp
10763                      DO  c = 11, ncc_tot*nbins, nbins
10764                         temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
10765                      ENDDO
10766                      local_pf(i,j,k) = MERGE( temp_bin,                       &
10767                                               REAL( -999.0_wp, KIND = wp ),   &
10768                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10769                   ENDDO
10770                ENDDO
10771             ENDDO
10772          ELSE
10773             DO  i = nxl, nxr
10774                DO  j = nys, nyn
10775                   DO  k = nzb, nzt+1                     
10776                      local_pf(i,j,k) = MERGE( mbins_av(k,j,i,11),             &
10777                                               REAL( -999.0_wp, KIND = wp ),   &
10778                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10779                   ENDDO
10780                ENDDO
10781             ENDDO
10782          ENDIF
10783         
10784       CASE ( 'm_bin12' )
10785          IF ( av == 0 )  THEN
10786             DO  i = nxl, nxr
10787                DO  j = nys, nyn
10788                   DO  k = nzb, nzt+1   
10789                      temp_bin = 0.0_wp
10790                      DO  c = 12, ncc_tot*nbins, nbins
10791                         temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
10792                      ENDDO
10793                      local_pf(i,j,k) = MERGE( temp_bin,                       &
10794                                               REAL( -999.0_wp, KIND = wp ),   &
10795                                               BTEST( wall_flags_0(k,j,i), 0 ) )
10796                   ENDDO
10797                ENDDO
10798             ENDDO
10799          ELSE
10800             DO  i = nxl, nxr
10801                DO  j = nys, nyn
10802                   DO  k = nzb, nzt+1                     
10803                      local_pf(i,j,k) = MERGE( mbins_av(k,j,i,12),             &
10804                                               REAL( -999.0_wp, KIND = wp ),   &
10805                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10806                   ENDDO
10807                ENDDO
10808             ENDDO
10809          ENDIF
10810                 
10811       CASE ( 's_BC', 's_DU', 's_H2O', 's_NH', 's_NO', 's_OC', 's_SO4', 's_SS' )
10812          IF ( is_used( prtcl, TRIM( variable(3:) ) ) )  THEN
10813             icc = get_index( prtcl, TRIM( variable(3:) ) )
10814             IF ( av == 0 )  THEN
10815                DO  i = nxl, nxr
10816                   DO  j = nys, nyn
10817                      DO  k = nzb, nzt+1
10818                         temp_bin = 0.0_wp
10819                         DO  c = ( icc-1 )*nbins+1, icc*nbins                         
10820                            temp_bin = temp_bin + aerosol_mass(c)%conc(k,j,i)
10821                         ENDDO
10822                         local_pf(i,j,k) = MERGE( temp_bin,                    &
10823                                               REAL( -999.0_wp, KIND = wp ),   &
10824                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10825                      ENDDO
10826                   ENDDO
10827                ENDDO
10828             ELSE
10829                IF ( TRIM( variable(3:) ) == 'BC' )   to_be_resorted => s_BC_av
10830                IF ( TRIM( variable(3:) ) == 'DU' )   to_be_resorted => s_DU_av   
10831                IF ( TRIM( variable(3:) ) == 'NH' )   to_be_resorted => s_NH_av   
10832                IF ( TRIM( variable(3:) ) == 'NO' )   to_be_resorted => s_NO_av   
10833                IF ( TRIM( variable(3:) ) == 'OC' )   to_be_resorted => s_OC_av   
10834                IF ( TRIM( variable(3:) ) == 'SO4' )  to_be_resorted => s_SO4_av   
10835                IF ( TRIM( variable(3:) ) == 'SS' )   to_be_resorted => s_SS_av 
10836                DO  i = nxl, nxr
10837                   DO  j = nys, nyn
10838                      DO  k = nzb, nzt+1                     
10839                         local_pf(i,j,k) = MERGE( to_be_resorted(k,j,i),       &
10840                                               REAL( -999.0_wp, KIND = wp ),   &
10841                                               BTEST( wall_flags_0(k,j,i), 0 ) ) 
10842                      ENDDO
10843                   ENDDO
10844                ENDDO
10845             ENDIF
10846          ENDIF
10847       CASE DEFAULT
10848          found = .FALSE.
10849
10850    END SELECT
10851
10852 END SUBROUTINE salsa_data_output_3d
10853
10854!------------------------------------------------------------------------------!
10855!
10856! Description:
10857! ------------
10858!> Subroutine defining mask output variables
10859!------------------------------------------------------------------------------!
10860 SUBROUTINE salsa_data_output_mask( av, variable, found, local_pf )
10861 
10862    USE control_parameters,                                                    &
10863        ONLY:  mask_size_l, mid
10864 
10865    IMPLICIT NONE
10866   
10867    CHARACTER (LEN=*) ::  variable   !<
10868
10869    INTEGER(iwp) ::  av   !<
10870    INTEGER(iwp) ::  c    !<
10871    INTEGER(iwp) ::  i    !<
10872    INTEGER(iwp) ::  icc  !< index of a chemical compound
10873    INTEGER(iwp) ::  j    !<
10874    INTEGER(iwp) ::  k    !<
10875    INTEGER(iwp) ::  n    !<
10876
10877    LOGICAL  ::  found    !<
10878    REAL(wp) ::  df       !< For calculating LDSA: fraction of particles
10879                          !< depositing in the alveolar (or tracheobronchial)
10880                          !< region of the lung. Depends on the particle size
10881    REAL(wp) ::  mean_d       !< Particle diameter in micrometres
10882    REAL(wp) ::  nc       !< Particle number concentration in units 1/cm**3
10883
10884    REAL(wp),                                                                  &
10885       DIMENSION(mask_size_l(mid,1),mask_size_l(mid,2),mask_size_l(mid,3)) ::  &
10886          local_pf   !<
10887    REAL(wp) ::  temp_bin   !<
10888    REAL(wp), DIMENSION(:,:,:), POINTER ::  to_be_resorted  !< points to
10889                                                     !< selected output variable
10890
10891    found     = .TRUE.
10892    temp_bin  = 0.0_wp
10893
10894    SELECT CASE ( TRIM( variable ) )
10895   
10896       CASE ( 'g_H2SO4', 'g_HNO3', 'g_NH3', 'g_OCNV',  'g_OCSV' )
10897          IF ( av == 0 )  THEN
10898             IF ( TRIM( variable ) == 'g_H2SO4')  icc = 1
10899             IF ( TRIM( variable ) == 'g_HNO3')   icc = 2
10900             IF ( TRIM( variable ) == 'g_NH3')    icc = 3
10901             IF ( TRIM( variable ) == 'g_OCNV')   icc = 4
10902             IF ( TRIM( variable ) == 'g_OCSV')   icc = 5
10903             
10904             DO  i = 1, mask_size_l(mid,1)
10905                DO  j = 1, mask_size_l(mid,2)
10906                   DO  k = 1, mask_size_l(mid,3)
10907                      local_pf(i,j,k) = salsa_gas(icc)%conc(mask_k(mid,k),     &
10908                                                    mask_j(mid,j),mask_i(mid,i))
10909                   ENDDO
10910                ENDDO
10911             ENDDO
10912          ELSE
10913             IF ( TRIM( variable(3:) ) == 'H2SO4' ) to_be_resorted => g_H2SO4_av
10914             IF ( TRIM( variable(3:) ) == 'HNO3' )  to_be_resorted => g_HNO3_av   
10915             IF ( TRIM( variable(3:) ) == 'NH3' )   to_be_resorted => g_NH3_av   
10916             IF ( TRIM( variable(3:) ) == 'OCNV' )  to_be_resorted => g_OCNV_av   
10917             IF ( TRIM( variable(3:) ) == 'OCSV' )  to_be_resorted => g_OCSV_av 
10918             DO  i = 1, mask_size_l(mid,1)
10919                DO  j = 1, mask_size_l(mid,2)
10920                   DO  k = 1, mask_size_l(mid,3)
10921                      local_pf(i,j,k) = to_be_resorted(mask_k(mid,k),          &
10922                                                    mask_j(mid,j),mask_i(mid,i))
10923                   ENDDO
10924                ENDDO
10925             ENDDO
10926          ENDIF
10927       
10928       CASE ( 'LDSA' )
10929          IF ( av == 0 )  THEN
10930             DO  i = 1, mask_size_l(mid,1)
10931                DO  j = 1, mask_size_l(mid,2)
10932                   DO  k = 1, mask_size_l(mid,3)
10933                      temp_bin = 0.0_wp
10934                      DO  n = 1, nbins
10935!                     
10936!--                      Diameter in micrometres
10937                         mean_d = 1.0E+6_wp * Ra_dry(mask_k(mid,k),            &
10938                                       mask_j(mid,j),mask_i(mid,i),n) * 2.0_wp
10939!                               
10940!--                      Deposition factor: alveolar (use Ra_dry for the size??)                               
10941                         df = ( 0.01555_wp / mean_d ) * ( EXP( -0.416_wp *     &
10942                                ( LOG( mean_d ) + 2.84_wp )**2.0_wp )          &
10943                                  + 19.11_wp * EXP( -0.482_wp *                &
10944                                  ( LOG( mean_d ) - 1.362_wp )**2.0_wp ) )
10945!                                   
10946!--                      Number concentration in 1/cm3
10947                         nc = 1.0E-6_wp * aerosol_number(n)%conc(mask_k(mid,k),&
10948                                                    mask_j(mid,j),mask_i(mid,i))
10949!                         
10950!--                      Lung-deposited surface area LDSA (units mum2/cm3)
10951                         temp_bin = temp_bin + pi * mean_d**2.0_wp * df * nc 
10952                      ENDDO
10953                      local_pf(i,j,k) = temp_bin
10954                   ENDDO
10955                ENDDO
10956             ENDDO
10957          ELSE
10958             DO  i = 1, mask_size_l(mid,1)
10959                DO  j = 1, mask_size_l(mid,2)
10960                   DO  k = 1, mask_size_l(mid,3)
10961                       local_pf(i,j,k) = LDSA_av(mask_k(mid,k),                &
10962                                                 mask_j(mid,j),mask_i(mid,i))
10963                   ENDDO
10964                ENDDO
10965             ENDDO
10966          ENDIF
10967       
10968       CASE ( 'Ntot' )
10969          IF ( av == 0 )  THEN
10970             DO  i = 1, mask_size_l(mid,1)
10971                DO  j = 1, mask_size_l(mid,2)
10972                   DO  k = 1, mask_size_l(mid,3)
10973                      temp_bin = 0.0_wp
10974                      DO  n = 1, nbins
10975                         temp_bin = temp_bin + aerosol_number(n)%conc(         &
10976                                    mask_k(mid,k),mask_j(mid,j),mask_i(mid,i))
10977                      ENDDO
10978                      local_pf(i,j,k) = temp_bin
10979                   ENDDO
10980                ENDDO
10981             ENDDO
10982          ELSE
10983             DO  i = 1, mask_size_l(mid,1)
10984                DO  j = 1, mask_size_l(mid,2)
10985                   DO  k = 1, mask_size_l(mid,3)
10986                       local_pf(i,j,k) = Ntot_av(mask_k(mid,k),                &
10987                                                 mask_j(mid,j),mask_i(mid,i))
10988                   ENDDO
10989                ENDDO
10990             ENDDO
10991          ENDIF
10992       
10993       CASE ( 'PM2.5' )
10994          IF ( av == 0 )  THEN
10995             DO  i = 1, mask_size_l(mid,1)
10996                DO  j = 1, mask_size_l(mid,2)
10997                   DO  k = 1, mask_size_l(mid,3)
10998                      temp_bin = 0.0_wp
10999                      DO  n = 1, nbins
11000                         IF ( 2.0_wp * Ra_dry(mask_k(mid,k),mask_j(mid,j),     &
11001                              mask_i(mid,i),n) <= 2.5E-6_wp )  THEN
11002                            DO  c = n, nbins*ncc, nbins
11003                               temp_bin = temp_bin + aerosol_mass(c)%conc(     &
11004                                     mask_k(mid,k), mask_j(mid,j),mask_i(mid,i))
11005                            ENDDO
11006                         ENDIF
11007                      ENDDO
11008                      local_pf(i,j,k) = temp_bin
11009                   ENDDO
11010                ENDDO
11011             ENDDO
11012          ELSE
11013             DO  i = 1, mask_size_l(mid,1)
11014                DO  j = 1, mask_size_l(mid,2)
11015                   DO  k = 1, mask_size_l(mid,3)
11016                       local_pf(i,j,k) = PM25_av(mask_k(mid,k),                &
11017                                                 mask_j(mid,j),mask_i(mid,i))
11018                   ENDDO
11019                ENDDO
11020             ENDDO
11021          ENDIF
11022       
11023       CASE ( 'PM10' )
11024          IF ( av == 0 )  THEN
11025             DO  i = 1, mask_size_l(mid,1)
11026                DO  j = 1, mask_size_l(mid,2)
11027                   DO  k = 1, mask_size_l(mid,3)
11028                      temp_bin = 0.0_wp
11029                      DO  n = 1, nbins
11030                         IF ( 2.0_wp * Ra_dry(mask_k(mid,k),mask_j(mid,j),     &
11031                              mask_i(mid,i),n) <= 10.0E-6_wp )  THEN
11032                            DO  c = n, nbins*ncc, nbins
11033                               temp_bin = temp_bin + aerosol_mass(c)%conc(     &
11034                                      mask_k(mid,k),mask_j(mid,j),mask_i(mid,i))
11035                            ENDDO
11036                         ENDIF
11037                      ENDDO
11038                      local_pf(i,j,k) = temp_bin
11039                   ENDDO
11040                ENDDO
11041             ENDDO
11042          ELSE
11043             DO  i = 1, mask_size_l(mid,1)
11044                DO  j = 1, mask_size_l(mid,2)
11045                   DO  k = 1, mask_size_l(mid,3)
11046                       local_pf(i,j,k) = PM10_av(mask_k(mid,k),                &
11047                                                 mask_j(mid,j),mask_i(mid,i))
11048                   ENDDO
11049                ENDDO
11050             ENDDO
11051          ENDIF
11052         
11053       CASE ( 'N_bin1' )
11054          IF ( av == 0 )  THEN
11055             DO  i = 1, mask_size_l(mid,1)
11056                DO  j = 1, mask_size_l(mid,2)
11057                   DO  k = 1, mask_size_l(mid,3)                     
11058                      local_pf(i,j,k) = aerosol_number(1)%conc(mask_k(mid,k),  &
11059                                                 mask_j(mid,j),mask_i(mid,i))
11060                   ENDDO
11061                ENDDO
11062             ENDDO
11063          ELSE
11064             DO  i = 1, mask_size_l(mid,1)
11065                DO  j = 1, mask_size_l(mid,2)
11066                   DO  k = 1, mask_size_l(mid,3)
11067                       local_pf(i,j,k) = Nbins_av(mask_k(mid,k),               &
11068                                                  mask_j(mid,j),mask_i(mid,i),1)
11069                   ENDDO
11070                ENDDO
11071             ENDDO
11072          ENDIF
11073       
11074       CASE ( 'N_bin2' )
11075          IF ( av == 0 )  THEN
11076             DO  i = 1, mask_size_l(mid,1)
11077                DO  j = 1, mask_size_l(mid,2)
11078                   DO  k = 1, mask_size_l(mid,3)                     
11079                      local_pf(i,j,k) = aerosol_number(2)%conc(mask_k(mid,k),  &
11080                                                 mask_j(mid,j),mask_i(mid,i)) 
11081                   ENDDO
11082                ENDDO
11083             ENDDO
11084          ELSE
11085             DO  i = 1, mask_size_l(mid,1)
11086                DO  j = 1, mask_size_l(mid,2)
11087                   DO  k = 1, mask_size_l(mid,3)
11088                       local_pf(i,j,k) = Nbins_av(mask_k(mid,k),               &
11089                                                  mask_j(mid,j),mask_i(mid,i),2)
11090                   ENDDO
11091                ENDDO
11092             ENDDO
11093          ENDIF
11094         
11095       CASE ( 'N_bin3' )
11096          IF ( av == 0 )  THEN
11097             DO  i = 1, mask_size_l(mid,1)
11098                DO  j = 1, mask_size_l(mid,2)
11099                   DO  k = 1, mask_size_l(mid,3)                     
11100                      local_pf(i,j,k) = aerosol_number(3)%conc(mask_k(mid,k),  &
11101                                                 mask_j(mid,j),mask_i(mid,i))
11102                   ENDDO
11103                ENDDO
11104             ENDDO
11105          ELSE
11106             DO  i = 1, mask_size_l(mid,1)
11107                DO  j = 1, mask_size_l(mid,2)
11108                   DO  k = 1, mask_size_l(mid,3)
11109                       local_pf(i,j,k) = Nbins_av(mask_k(mid,k),               &
11110                                                  mask_j(mid,j),mask_i(mid,i),3)
11111                   ENDDO
11112                ENDDO
11113             ENDDO
11114          ENDIF
11115       
11116       CASE ( 'N_bin4' )
11117          IF ( av == 0 )  THEN
11118             DO  i = 1, mask_size_l(mid,1)
11119                DO  j = 1, mask_size_l(mid,2)
11120                   DO  k = 1, mask_size_l(mid,3)                     
11121                      local_pf(i,j,k) = aerosol_number(4)%conc(mask_k(mid,k),  &
11122                                                 mask_j(mid,j),mask_i(mid,i))
11123                   ENDDO
11124                ENDDO
11125             ENDDO
11126          ELSE
11127             DO  i = 1, mask_size_l(mid,1)
11128                DO  j = 1, mask_size_l(mid,2)
11129                   DO  k = 1, mask_size_l(mid,3)
11130                       local_pf(i,j,k) = Nbins_av(mask_k(mid,k),               &
11131                                                  mask_j(mid,j),mask_i(mid,i),4)
11132                   ENDDO
11133                ENDDO
11134             ENDDO
11135          ENDIF
11136       
11137       CASE ( 'N_bin5' )
11138          IF ( av == 0 )  THEN
11139             DO  i = 1, mask_size_l(mid,1)
11140                DO  j = 1, mask_size_l(mid,2)
11141                   DO  k = 1, mask_size_l(mid,3)                     
11142                      local_pf(i,j,k) = aerosol_number(5)%conc(mask_k(mid,k),  &
11143                                                 mask_j(mid,j),mask_i(mid,i))
11144                   ENDDO
11145                ENDDO
11146             ENDDO
11147          ELSE
11148             DO  i = 1, mask_size_l(mid,1)
11149                DO  j = 1, mask_size_l(mid,2)
11150                   DO  k = 1, mask_size_l(mid,3)
11151                       local_pf(i,j,k) = Nbins_av(mask_k(mid,k),               &
11152                                                  mask_j(mid,j),mask_i(mid,i),5)
11153                   ENDDO
11154                ENDDO
11155             ENDDO
11156          ENDIF
11157       
11158       CASE ( 'N_bin6' )
11159          IF ( av == 0 )  THEN
11160             DO  i = 1, mask_size_l(mid,1)
11161                DO  j = 1, mask_size_l(mid,2)
11162                   DO  k = 1, mask_size_l(mid,3)                     
11163                      local_pf(i,j,k) = aerosol_number(6)%conc(mask_k(mid,k),  &
11164                                                 mask_j(mid,j),mask_i(mid,i)) 
11165                   ENDDO
11166                ENDDO
11167             ENDDO
11168          ELSE
11169             DO  i = 1, mask_size_l(mid,1)
11170                DO  j = 1, mask_size_l(mid,2)
11171                   DO  k = 1, mask_size_l(mid,3)
11172                       local_pf(i,j,k) = Nbins_av(mask_k(mid,k),               &
11173                                                  mask_j(mid,j),mask_i(mid,i),6)
11174                   ENDDO
11175                ENDDO
11176             ENDDO
11177          ENDIF
11178         
11179       CASE ( 'N_bin7' )
11180          IF ( av == 0 )  THEN
11181             DO  i = 1, mask_size_l(mid,1)
11182                DO  j = 1, mask_size_l(mid,2)
11183                   DO  k = 1, mask_size_l(mid,3)                     
11184                      local_pf(i,j,k) = aerosol_number(7)%conc(mask_k(mid,k),  &
11185                                                 mask_j(mid,j),mask_i(mid,i)) 
11186                   ENDDO
11187                ENDDO
11188             ENDDO
11189          ELSE
11190             DO  i = 1, mask_size_l(mid,1)
11191                DO  j = 1, mask_size_l(mid,2)
11192                   DO  k = 1, mask_size_l(mid,3)
11193                       local_pf(i,j,k) = Nbins_av(mask_k(mid,k),               &
11194                                                  mask_j(mid,j),mask_i(mid,i),7)
11195                   ENDDO
11196                ENDDO
11197             ENDDO
11198          ENDIF
11199       
11200       CASE ( 'N_bin8' )
11201          IF ( av == 0 )  THEN
11202             DO  i = 1, mask_size_l(mid,1)
11203                DO  j = 1, mask_size_l(mid,2)
11204                   DO  k = 1, mask_size_l(mid,3)                     
11205                      local_pf(i,j,k) = aerosol_number(8)%conc(mask_k(mid,k),  &
11206                                                 mask_j(mid,j),mask_i(mid,i)) 
11207                   ENDDO
11208                ENDDO
11209             ENDDO
11210          ELSE
11211             DO  i = 1, mask_size_l(mid,1)
11212                DO  j = 1, mask_size_l(mid,2)
11213                   DO  k = 1, mask_size_l(mid,3)
11214                       local_pf(i,j,k) = Nbins_av(mask_k(mid,k),               &
11215                                                  mask_j(mid,j),mask_i(mid,i),8)
11216                   ENDDO
11217                ENDDO
11218             ENDDO
11219          ENDIF
11220         
11221       CASE ( 'N_bin9' )
11222          IF ( av == 0 )  THEN
11223             DO  i = 1, mask_size_l(mid,1)
11224                DO  j = 1, mask_size_l(mid,2)
11225                   DO  k = 1, mask_size_l(mid,3)                     
11226                      local_pf(i,j,k) = aerosol_number(9)%conc(mask_k(mid,k),  &
11227                                                 mask_j(mid,j),mask_i(mid,i)) 
11228                   ENDDO
11229                ENDDO
11230             ENDDO
11231          ELSE
11232             DO  i = 1, mask_size_l(mid,1)
11233                DO  j = 1, mask_size_l(mid,2)
11234                   DO  k = 1, mask_size_l(mid,3)
11235                       local_pf(i,j,k) = Nbins_av(mask_k(mid,k),               &
11236                                                  mask_j(mid,j),mask_i(mid,i),9)
11237                   ENDDO
11238                ENDDO
11239             ENDDO
11240          ENDIF
11241       
11242       CASE ( 'N_bin10' )
11243          IF ( av == 0 )  THEN
11244             DO  i = 1, mask_size_l(mid,1)
11245                DO  j = 1, mask_size_l(mid,2)
11246                   DO  k = 1, mask_size_l(mid,3)                     
11247                      local_pf(i,j,k) = aerosol_number(10)%conc(mask_k(mid,k), &
11248                                                 mask_j(mid,j),mask_i(mid,i)) 
11249                   ENDDO
11250                ENDDO
11251             ENDDO
11252          ELSE
11253             DO  i = 1, mask_size_l(mid,1)
11254                DO  j = 1, mask_size_l(mid,2)
11255                   DO  k = 1, mask_size_l(mid,3)
11256                       local_pf(i,j,k) = Nbins_av(mask_k(mid,k),               &
11257                                                 mask_j(mid,j),mask_i(mid,i),10)
11258                   ENDDO
11259                ENDDO
11260             ENDDO
11261          ENDIF
11262       
11263       CASE ( 'N_bin11' )
11264          IF ( av == 0 )  THEN
11265             DO  i = 1, mask_size_l(mid,1)
11266                DO  j = 1, mask_size_l(mid,2)
11267                   DO  k = 1, mask_size_l(mid,3)                     
11268                      local_pf(i,j,k) = aerosol_number(11)%conc(mask_k(mid,k), &
11269                                                 mask_j(mid,j),mask_i(mid,i)) 
11270                   ENDDO
11271                ENDDO
11272             ENDDO
11273          ELSE
11274             DO  i = 1, mask_size_l(mid,1)
11275                DO  j = 1, mask_size_l(mid,2)
11276                   DO  k = 1, mask_size_l(mid,3)
11277                       local_pf(i,j,k) = Nbins_av(mask_k(mid,k),               &
11278                                                 mask_j(mid,j),mask_i(mid,i),11)
11279                   ENDDO
11280                ENDDO
11281             ENDDO
11282          ENDIF
11283         
11284       CASE ( 'N_bin12' )
11285          IF ( av == 0 )  THEN
11286             DO  i = 1, mask_size_l(mid,1)
11287                DO  j = 1, mask_size_l(mid,2)
11288                   DO  k = 1, mask_size_l(mid,3)                     
11289                      local_pf(i,j,k) = aerosol_number(12)%conc(mask_k(mid,k), &
11290                                                 mask_j(mid,j),mask_i(mid,i)) 
11291                   ENDDO
11292                ENDDO
11293             ENDDO
11294          ELSE
11295             DO  i = 1, mask_size_l(mid,1)
11296                DO  j = 1, mask_size_l(mid,2)
11297                   DO  k = 1, mask_size_l(mid,3)
11298                       local_pf(i,j,k) = Nbins_av(mask_k(mid,k),               &
11299                                                 mask_j(mid,j),mask_i(mid,i),12)
11300                   ENDDO
11301                ENDDO
11302             ENDDO
11303          ENDIF
11304         
11305       CASE ( 'm_bin1' )
11306          IF ( av == 0 )  THEN
11307             DO  i = 1, mask_size_l(mid,1)
11308                DO  j = 1, mask_size_l(mid,2)
11309                   DO  k = 1, mask_size_l(mid,3)
11310                      temp_bin = 0.0_wp
11311                      DO  c = 1, ncc_tot*nbins, nbins
11312                         temp_bin = temp_bin + aerosol_mass(c)%conc(           &
11313                                    mask_k(mid,k),mask_j(mid,j),mask_i(mid,i))
11314                      ENDDO
11315                      local_pf(i,j,k) = temp_bin
11316                   ENDDO
11317                ENDDO
11318             ENDDO
11319          ELSE
11320             DO  i = 1, mask_size_l(mid,1)
11321                DO  j = 1, mask_size_l(mid,2)
11322                   DO  k = 1, mask_size_l(mid,3)
11323                       local_pf(i,j,k) = mbins_av(mask_k(mid,k),               &
11324                                                  mask_j(mid,j),mask_i(mid,i),1)
11325                   ENDDO
11326                ENDDO
11327             ENDDO
11328          ENDIF
11329       
11330       CASE ( 'm_bin2' )
11331          IF ( av == 0 )  THEN
11332             DO  i = 1, mask_size_l(mid,1)
11333                DO  j = 1, mask_size_l(mid,2)
11334                   DO  k = 1, mask_size_l(mid,3)
11335                      temp_bin = 0.0_wp
11336                      DO  c = 2, ncc_tot*nbins, nbins
11337                         temp_bin = temp_bin + aerosol_mass(c)%conc(           &
11338                                    mask_k(mid,k),mask_j(mid,j),mask_i(mid,i))
11339                      ENDDO
11340                      local_pf(i,j,k) = temp_bin
11341                   ENDDO
11342                ENDDO
11343             ENDDO
11344          ELSE
11345             DO  i = 1, mask_size_l(mid,1)
11346                DO  j = 1, mask_size_l(mid,2)
11347                   DO  k = 1, mask_size_l(mid,3)
11348                       local_pf(i,j,k) = mbins_av(mask_k(mid,k),               &
11349                                                  mask_j(mid,j),mask_i(mid,i),2)
11350                   ENDDO
11351                ENDDO
11352             ENDDO
11353          ENDIF
11354         
11355       CASE ( 'm_bin3' )
11356          IF ( av == 0 )  THEN
11357             DO  i = 1, mask_size_l(mid,1)
11358                DO  j = 1, mask_size_l(mid,2)
11359                   DO  k = 1, mask_size_l(mid,3)
11360                      temp_bin = 0.0_wp
11361                      DO  c = 3, ncc_tot*nbins, nbins
11362                         temp_bin = temp_bin + aerosol_mass(c)%conc(           &
11363                                    mask_k(mid,k),mask_j(mid,j),mask_i(mid,i))
11364                      ENDDO
11365                      local_pf(i,j,k) = temp_bin
11366                   ENDDO
11367                ENDDO
11368             ENDDO
11369          ELSE
11370             DO  i = 1, mask_size_l(mid,1)
11371                DO  j = 1, mask_size_l(mid,2)
11372                   DO  k = 1, mask_size_l(mid,3)
11373                       local_pf(i,j,k) = mbins_av(mask_k(mid,k),               &
11374                                                  mask_j(mid,j),mask_i(mid,i),3)
11375                   ENDDO
11376                ENDDO
11377             ENDDO
11378          ENDIF
11379       
11380       CASE ( 'm_bin4' )
11381          IF ( av == 0 )  THEN
11382             DO  i = 1, mask_size_l(mid,1)
11383                DO  j = 1, mask_size_l(mid,2)
11384                   DO  k = 1, mask_size_l(mid,3)
11385                      temp_bin = 0.0_wp
11386                      DO  c = 4, ncc_tot*nbins, nbins
11387                         temp_bin = temp_bin + aerosol_mass(c)%conc(           &
11388                                    mask_k(mid,k),mask_j(mid,j),mask_i(mid,i))
11389                      ENDDO
11390                      local_pf(i,j,k) = temp_bin
11391                   ENDDO
11392                ENDDO
11393             ENDDO
11394          ELSE
11395             DO  i = 1, mask_size_l(mid,1)
11396                DO  j = 1, mask_size_l(mid,2)
11397                   DO  k = 1, mask_size_l(mid,3)
11398                       local_pf(i,j,k) = mbins_av(mask_k(mid,k),               &
11399                                                  mask_j(mid,j),mask_i(mid,i),4)
11400                   ENDDO
11401                ENDDO
11402             ENDDO
11403          ENDIF
11404       
11405       CASE ( 'm_bin5' )
11406          IF ( av == 0 )  THEN
11407             DO  i = 1, mask_size_l(mid,1)
11408                DO  j = 1, mask_size_l(mid,2)
11409                   DO  k = 1, mask_size_l(mid,3)
11410                      temp_bin = 0.0_wp
11411                      DO  c = 5, ncc_tot*nbins, nbins
11412                         temp_bin = temp_bin + aerosol_mass(c)%conc(           &
11413                                    mask_k(mid,k),mask_j(mid,j),mask_i(mid,i))
11414                      ENDDO
11415                      local_pf(i,j,k) = temp_bin
11416                   ENDDO
11417                ENDDO
11418             ENDDO
11419          ELSE
11420             DO  i = 1, mask_size_l(mid,1)
11421                DO  j = 1, mask_size_l(mid,2)
11422                   DO  k = 1, mask_size_l(mid,3)
11423                       local_pf(i,j,k) = mbins_av(mask_k(mid,k),               &
11424                                                  mask_j(mid,j),mask_i(mid,i),5)
11425                   ENDDO
11426                ENDDO
11427             ENDDO
11428          ENDIF
11429       
11430       CASE ( 'm_bin6' )
11431          IF ( av == 0 )  THEN
11432             DO  i = 1, mask_size_l(mid,1)
11433                DO  j = 1, mask_size_l(mid,2)
11434                   DO  k = 1, mask_size_l(mid,3)
11435                      temp_bin = 0.0_wp
11436                      DO  c = 6, ncc_tot*nbins, nbins
11437                         temp_bin = temp_bin + aerosol_mass(c)%conc(           &
11438                                    mask_k(mid,k),mask_j(mid,j),mask_i(mid,i))
11439                      ENDDO
11440                      local_pf(i,j,k) = temp_bin
11441                   ENDDO
11442                ENDDO
11443             ENDDO
11444          ELSE
11445             DO  i = 1, mask_size_l(mid,1)
11446                DO  j = 1, mask_size_l(mid,2)
11447                   DO  k = 1, mask_size_l(mid,3)
11448                       local_pf(i,j,k) = mbins_av(mask_k(mid,k),               &
11449                                                  mask_j(mid,j),mask_i(mid,i),6)
11450                   ENDDO
11451                ENDDO
11452             ENDDO
11453          ENDIF
11454         
11455       CASE ( 'm_bin7' )
11456          IF ( av == 0 )  THEN
11457             DO  i = 1, mask_size_l(mid,1)
11458                DO  j = 1, mask_size_l(mid,2)
11459                   DO  k = 1, mask_size_l(mid,3)
11460                      temp_bin = 0.0_wp
11461                      DO  c = 7, ncc_tot*nbins, nbins
11462                         temp_bin = temp_bin + aerosol_mass(c)%conc(           &
11463                                    mask_k(mid,k),mask_j(mid,j),mask_i(mid,i))
11464                      ENDDO
11465                      local_pf(i,j,k) = temp_bin
11466                   ENDDO
11467                ENDDO
11468             ENDDO
11469          ELSE
11470             DO  i = 1, mask_size_l(mid,1)
11471                DO  j = 1, mask_size_l(mid,2)
11472                   DO  k = 1, mask_size_l(mid,3)
11473                       local_pf(i,j,k) = mbins_av(mask_k(mid,k),               &
11474                                                  mask_j(mid,j),mask_i(mid,i),7)
11475                   ENDDO
11476                ENDDO
11477             ENDDO
11478          ENDIF
11479       
11480       CASE ( 'm_bin8' )
11481          IF ( av == 0 )  THEN
11482             DO  i = 1, mask_size_l(mid,1)
11483                DO  j = 1, mask_size_l(mid,2)
11484                   DO  k = 1, mask_size_l(mid,3)
11485                      temp_bin = 0.0_wp
11486                      DO  c = 8, ncc_tot*nbins, nbins
11487                         temp_bin = temp_bin + aerosol_mass(c)%conc(           &
11488                                    mask_k(mid,k),mask_j(mid,j),mask_i(mid,i))
11489                      ENDDO
11490                      local_pf(i,j,k) = temp_bin
11491                   ENDDO
11492                ENDDO
11493             ENDDO
11494          ELSE
11495             DO  i = 1, mask_size_l(mid,1)
11496                DO  j = 1, mask_size_l(mid,2)
11497                   DO  k = 1, mask_size_l(mid,3)
11498                       local_pf(i,j,k) = mbins_av(mask_k(mid,k),               &
11499                                                  mask_j(mid,j),mask_i(mid,i),8)
11500                   ENDDO
11501                ENDDO
11502             ENDDO
11503          ENDIF
11504         
11505       CASE ( 'm_bin9' )
11506          IF ( av == 0 )  THEN
11507             DO  i = 1, mask_size_l(mid,1)
11508                DO  j = 1, mask_size_l(mid,2)
11509                   DO  k = 1, mask_size_l(mid,3)
11510                      temp_bin = 0.0_wp
11511                      DO  c = 9, ncc_tot*nbins, nbins
11512                         temp_bin = temp_bin + aerosol_mass(c)%conc(           &
11513                                    mask_k(mid,k),mask_j(mid,j),mask_i(mid,i))
11514                      ENDDO
11515                      local_pf(i,j,k) = temp_bin
11516                   ENDDO
11517                ENDDO
11518             ENDDO
11519          ELSE
11520             DO  i = 1, mask_size_l(mid,1)
11521                DO  j = 1, mask_size_l(mid,2)
11522                   DO  k = 1, mask_size_l(mid,3)
11523                       local_pf(i,j,k) = mbins_av(mask_k(mid,k),               &
11524                                                  mask_j(mid,j),mask_i(mid,i),9)
11525                   ENDDO
11526                ENDDO
11527             ENDDO
11528          ENDIF
11529       
11530       CASE ( 'm_bin10' )
11531          IF ( av == 0 )  THEN
11532             DO  i = 1, mask_size_l(mid,1)
11533                DO  j = 1, mask_size_l(mid,2)
11534                   DO  k = 1, mask_size_l(mid,3)
11535                      temp_bin = 0.0_wp
11536                      DO  c = 10, ncc_tot*nbins, nbins
11537                         temp_bin = temp_bin + aerosol_mass(c)%conc(           &
11538                                    mask_k(mid,k),mask_j(mid,j),mask_i(mid,i))
11539                      ENDDO
11540                      local_pf(i,j,k) = temp_bin
11541                   ENDDO
11542                ENDDO
11543             ENDDO
11544          ELSE
11545             DO  i = 1, mask_size_l(mid,1)
11546                DO  j = 1, mask_size_l(mid,2)
11547                   DO  k = 1, mask_size_l(mid,3)
11548                       local_pf(i,j,k) = mbins_av(mask_k(mid,k),               &
11549                                                 mask_j(mid,j),mask_i(mid,i),10)
11550                   ENDDO
11551                ENDDO
11552             ENDDO
11553          ENDIF
11554         
11555       CASE ( 'm_bin11' )
11556         IF ( av == 0 )  THEN
11557             DO  i = 1, mask_size_l(mid,1)
11558                DO  j = 1, mask_size_l(mid,2)
11559                   DO  k = 1, mask_size_l(mid,3)
11560                      temp_bin = 0.0_wp
11561                      DO  c = 11, ncc_tot*nbins, nbins
11562                         temp_bin = temp_bin + aerosol_mass(c)%conc(           &
11563                                    mask_k(mid,k),mask_j(mid,j),mask_i(mid,i))
11564                      ENDDO
11565                      local_pf(i,j,k) = temp_bin
11566                   ENDDO
11567                ENDDO
11568             ENDDO
11569          ELSE
11570             DO  i = 1, mask_size_l(mid,1)
11571                DO  j = 1, mask_size_l(mid,2)
11572                   DO  k = 1, mask_size_l(mid,3)
11573                       local_pf(i,j,k) = mbins_av(mask_k(mid,k),               &
11574                                                 mask_j(mid,j),mask_i(mid,i),11)
11575                   ENDDO
11576                ENDDO
11577             ENDDO
11578          ENDIF
11579         
11580       CASE ( 'm_bin12' )
11581          IF ( av == 0 )  THEN
11582             DO  i = 1, mask_size_l(mid,1)
11583                DO  j = 1, mask_size_l(mid,2)
11584                   DO  k = 1, mask_size_l(mid,3)
11585                      temp_bin = 0.0_wp
11586                      DO  c = 12, ncc_tot*nbins, nbins
11587                         temp_bin = temp_bin + aerosol_mass(c)%conc(           &
11588                                    mask_k(mid,k),mask_j(mid,j),mask_i(mid,i))
11589                      ENDDO
11590                      local_pf(i,j,k) = temp_bin
11591                   ENDDO
11592                ENDDO
11593             ENDDO
11594          ELSE
11595             DO  i = 1, mask_size_l(mid,1)
11596                DO  j = 1, mask_size_l(mid,2)
11597                   DO  k = 1, mask_size_l(mid,3)
11598                       local_pf(i,j,k) = mbins_av(mask_k(mid,k),               &
11599                                                 mask_j(mid,j),mask_i(mid,i),12)
11600                   ENDDO
11601                ENDDO
11602             ENDDO
11603          ENDIF
11604         
11605       CASE ( 's_BC', 's_DU', 's_NH', 's_NO', 's_OC', 's_SO4', 's_SS' )
11606          IF ( is_used( prtcl, TRIM( variable(3:) ) ) )  THEN
11607             icc = get_index( prtcl, TRIM( variable(3:) ) )
11608             IF ( av == 0 )  THEN
11609                DO  i = 1, mask_size_l(mid,1)
11610                   DO  j = 1, mask_size_l(mid,2)
11611                      DO  k = 1, mask_size_l(mid,3)
11612                         temp_bin = 0.0_wp
11613                         DO  c = ( icc-1 )*nbins+1, icc*nbins 
11614                            temp_bin = temp_bin + aerosol_mass(c)%conc(        &
11615                                      mask_k(mid,k),mask_j(mid,j),mask_i(mid,i))
11616                         ENDDO
11617                         local_pf(i,j,k) = temp_bin
11618                      ENDDO
11619                   ENDDO
11620                ENDDO
11621             ELSE
11622                IF ( TRIM( variable(3:) ) == 'BC' )   to_be_resorted => s_BC_av
11623                IF ( TRIM( variable(3:) ) == 'DU' )   to_be_resorted => s_DU_av   
11624                IF ( TRIM( variable(3:) ) == 'NH' )   to_be_resorted => s_NH_av   
11625                IF ( TRIM( variable(3:) ) == 'NO' )   to_be_resorted => s_NO_av   
11626                IF ( TRIM( variable(3:) ) == 'OC' )   to_be_resorted => s_OC_av   
11627                IF ( TRIM( variable(3:) ) == 'SO4' )  to_be_resorted => s_SO4_av   
11628                IF ( TRIM( variable(3:) ) == 'SS' )   to_be_resorted => s_SS_av 
11629                DO  i = 1, mask_size_l(mid,1)
11630                   DO  j = 1, mask_size_l(mid,2)
11631                      DO  k = 1, mask_size_l(mid,3)                   
11632                         local_pf(i,j,k) = to_be_resorted(mask_k(mid,k),       &
11633                                                    mask_j(mid,j),mask_i(mid,i))
11634                      ENDDO
11635                   ENDDO
11636                ENDDO
11637             ENDIF
11638          ENDIF
11639       
11640       CASE DEFAULT
11641          found = .FALSE.
11642   
11643    END SELECT
11644   
11645 END SUBROUTINE salsa_data_output_mask
11646 
11647
11648 END MODULE salsa_mod
Note: See TracBrowser for help on using the repository browser.