source: palm/trunk/SOURCE/netcdf_data_input_mod.f90 @ 4012

Last change on this file since 4012 was 4012, checked in by monakurppa, 4 years ago

remove salsa_util_mod.f90 and correct some bugs in salsa and salsa test case

  • Property svn:keywords set to Id
File size: 295.7 KB
Line 
1!> @file netcdf_data_input_mod.f90
2!------------------------------------------------------------------------------!
3! This file is part of the PALM model system.
4!
5! PALM 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 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-2019 Leibniz Universitaet Hannover
18!------------------------------------------------------------------------------!
19!
20! Current revisions:
21! -----------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: netcdf_data_input_mod.f90 4012 2019-05-31 15:19:05Z monakurppa $
27!
28! 3994 2019-05-22 18:08:09Z suehring
29! Remove single location message
30!
31! 3976 2019-05-15 11:02:34Z hellstea
32! Remove unused variables from last commit
33!
34! 3969 2019-05-13 12:14:33Z suehring
35! - clean-up index notations for emission_values to eliminate magic numbers
36! - introduce temporary variable dum_var_5d as well as subroutines
37!   get_var_5d_real and get_var_5d_real_dynamic
38! - remove emission-specific code in generic get_variable routines
39! - in subroutine netcdf_data_input_chemistry_data change netCDF LOD 1
40!   (default) emission_values to the following index order:
41!   z, y, x, species, category
42! - in subroutine netcdf_data_input_chemistry_data
43!   changed netCDF LOD 2 pre-processed emission_values to the following index
44!   order: time, z, y, x, species
45! - in type chem_emis_att_type replace nspec with n_emiss_species
46!   but retained nspec for backward compatibility with salsa_mod. (E.C. Chan)
47!
48! 3961 2019-05-08 16:12:31Z suehring
49! Revise checks for building IDs and types
50!
51! 3943 2019-05-02 09:50:41Z maronga
52! Temporarily disabled some (faulty) checks for static driver.
53!
54! 3942 2019-04-30 13:08:30Z kanani
55! Fix: increase LEN of all NetCDF attribute values (caused crash in
56! netcdf_create_global_atts due to insufficient length)
57!
58! 3941 2019-04-30 09:48:33Z suehring
59! Move check for grid dimension to an earlier point in time when first array
60! is read.
61! Improve checks for building types / IDs with respect to 2D/3D buildings.
62!
63! 3885 2019-04-11 11:29:34Z kanani
64! Changes related to global restructuring of location messages and introduction
65! of additional debug messages
66!
67! 3864 2019-04-05 09:01:56Z monakurppa
68! get_variable_4d_to_3d_real modified to enable read in data of type
69! data(t,y,x,n) one timestep at a time + some routines made public
70!
71! 3855 2019-04-03 10:00:59Z suehring
72! Typo removed
73!
74! 3854 2019-04-02 16:59:33Z suehring
75! Bugfix in one of the checks. Typo removed.
76!
77! 3744 2019-02-15 18:38:58Z suehring
78! Enable mesoscale offline nesting for chemistry variables as well as
79! initialization of chemistry via dynamic input file.
80!
81! 3705 2019-01-29 19:56:39Z suehring
82! Interface for attribute input of 8-bit and 32-bit integer
83!
84! 3704 2019-01-29 19:51:41Z suehring
85! unused variables removed
86!
87! 3560 2018-11-23 09:20:21Z raasch
88! Some formatting adjustment
89!
90! 3556 2018-11-22 14:11:57Z suehring
91! variables documented and unused variables removed
92!
93! 3552 2018-11-22 10:28:35Z suehring
94! Revise ghost point exchange and resizing of input variables
95!
96! 3542 2018-11-20 17:04:13Z suehring
97! - read optional attributes from file
98! - set default origin_time
99!
100! 3518 2018-11-12 18:10:23Z suehring
101! Additional checks
102!
103! 3516 2018-11-12 15:49:39Z gronemeier
104! bugfix: - difference in z coordinate between file and PALM must be <1e-6
105!         - output of error 553 for all PEs
106!
107! 3498 2018-11-07 10:53:03Z gronemeier
108! Bugfix: print error message by processor which encounters the error
109!
110! 3485 2018-11-03 17:09:40Z gronemeier
111! - get central meridian from origin_lon if crs does not exist
112! - set default origin_lon to 0
113!
114! 3483 2018-11-02 14:19:26Z raasch
115! bugfix: misplaced directives for netCDF fixed
116!
117! 3474 2018-10-30 21:07:39Z kanani
118! Add UV exposure model input (Schrempf)
119!
120! 3472 2018-10-30 20:43:50Z suehring
121! Salsa implemented
122!
123! 3464 2018-10-30 18:08:55Z kanani
124! Define coordinate reference system (crs) and read from input dataset
125! Revise default values for reference coordinates
126!
127! 3459 2018-10-30 15:04:11Z gronemeier
128! from chemistry branch r3443, banzhafs, Russo:
129! Uncommented lines on dimension of surface_fractions
130! Removed par_emis_time_factor variable, moved to chem_emissions_mod
131! Initialized nspec and other emission variables at time of declaration
132! Modified EXPERT mode to PRE-PROCESSED mode
133! Introduced Chemistry static netcdf file
134! Added the routine for reading-in netcdf data for chemistry
135! Added routines to get_variable interface specific for chemistry files
136!
137! 3429 2018-10-25 13:04:23Z knoop
138! add default values of origin_x/y/z
139!
140! 3404 2018-10-23 13:29:11Z suehring
141! Consider time-dependent geostrophic wind components in offline nesting
142!
143! 3376 2018-10-19 10:15:32Z suehring
144! Additional check for consistent building initialization implemented
145!
146! 3347 2018-10-15 14:21:08Z suehring
147! Subroutine renamed
148!
149! 3257 2018-09-17 17:11:46Z suehring
150! (from branch resler)
151! Formatting
152!
153! 3298 2018-10-02 12:21:11Z kanani
154! Modified EXPERT mode to PRE-PROCESSED mode (Russo)
155! Introduced Chemistry static netcdf file (Russo)
156! Added the routine for reading-in netcdf data for chemistry (Russo)
157! Added routines to get_variable interface specific for chemistry files (Russo)
158!
159! 3257 2018-09-17 17:11:46Z suehring
160! Adjust checks for building_type and building_id, which is necessary after
161! topography filtering (building_type and id can be modified by the filtering).
162!
163! 3254 2018-09-17 10:53:57Z suehring
164! Additional check for surface_fractions and and checks for building_id and
165! building_type extended.
166!
167! 3241 2018-09-12 15:02:00Z raasch
168! unused variables removed
169!
170! 3215 2018-08-29 09:58:59Z suehring
171! - Separate input of soil properties from input of atmospheric data. This
172!   enables input of soil properties also in child domains without any
173!   dependence on atmospheric input
174! - Check for missing initial 1D/3D data in dynamic input file
175! - Revise checks for matching grid spacing in model and input file
176! - Bugfix, add netcdf4_parallel directive for collective read operation
177! - Revise error message numbers
178!
179! 3209 2018-08-27 16:58:37Z suehring
180! Read zsoil dimension length only if soil variables are provided
181!
182! 3183 2018-07-27 14:25:55Z suehring
183! Adjust input of dynamic driver according to revised Inifor version.
184! Replace simulated_time by time_since_reference_point.
185! Rename variables in mesoscale-offline nesting mode.
186!
187! 3182 2018-07-27 13:36:03Z suehring
188! Slightly revise check for surface_fraction in order to check only the relevant
189! fractions
190!
191! 3103 2018-07-04 17:30:52Z suehring
192! New check for negative terrain heights
193!
194! 3089 2018-06-27 13:20:38Z suehring
195! Revise call for message routine in case of local data inconsistencies.
196!
197! 3054 2018-06-01 16:08:59Z gronemeier
198! Bugfix: force an MPI abort if errors occur while reading building heights
199! from ASCII file
200!
201! 3053 2018-06-01 12:59:07Z suehring
202! Revise checks for variable surface_fraction
203!
204! 3051 2018-05-30 17:43:55Z suehring
205! - Speed-up NetCDF input
206! - Revise input routines and remove NetCDF input via IO-blocks since this is
207!   not working in parallel mode in case blocking collective read operations
208!   are done
209! - Temporarily revoke renaming of input variables in dynamic driver (tend_ug,
210!   tend_vg, zsoil) in order to keep dynamic input file working with current
211!   model version
212! - More detailed error messages created
213!
214! 3045 2018-05-28 07:55:41Z Giersch
215! Error messages revised
216!
217! 3041 2018-05-25 10:39:54Z gronemeier
218! Add data type for global file attributes
219! Add read of global attributes of static driver
220!
221! 3037 2018-05-24 10:39:29Z gronemeier
222! renamed 'depth' to 'zsoil'
223!
224! 3036 2018-05-24 10:18:26Z gronemeier
225! Revision of input vars according to UC2 data standard
226!  - renamed 'orography_2D' to 'zt'
227!  - renamed 'buildings_2D' to 'buildings_2d'
228!  - renamed 'buildings_3D' to 'buildings_3d'
229!  - renamed 'leaf_are_density' to 'lad'
230!  - renamed 'basal_are_density' to 'bad'
231!  - renamed 'root_are_density_lad' to 'root_area_dens_r'
232!  - renamed 'root_are_density_lsm' to 'root_area_dens_s'
233!  - renamed 'ls_forcing_ug' to 'tend_ug'
234!  - renamed 'ls_forcing_vg' to 'tend_vg'
235!
236! 3019 2018-05-13 07:05:43Z maronga
237! Improved reading speed of large NetCDF files
238!
239! 2963 2018-04-12 14:47:44Z suehring
240! - Revise checks for static input variables.
241! - Introduce index for vegetation/wall, pavement/green-wall and water/window
242!   surfaces, for clearer access of surface fraction, albedo, emissivity, etc. .
243!
244! 2958 2018-04-11 15:38:13Z suehring
245! Synchronize longitude and latitude between nested model domains, values are
246! taken from the root model.
247!
248! 2955 2018-04-09 15:14:01Z suehring
249! Extend checks for consistent setting of buildings, its ID and type.
250! Add log-points to measure CPU time of NetCDF data input.
251!
252! 2953 2018-04-09 11:26:02Z suehring
253! Bugfix in checks for initialization data
254!
255! 2947 2018-04-04 18:01:41Z suehring
256! Checks for dynamic input revised
257!
258! 2946 2018-04-04 17:01:23Z suehring
259! Bugfix for revision 2945, perform checks only if dynamic input file is
260! available.
261!
262! 2945 2018-04-04 16:27:14Z suehring
263! - Mimic for topography input slightly revised, in order to enable consistency
264!   checks
265! - Add checks for dimensions in dynamic input file and move already existing
266!   checks
267!
268! 2938 2018-03-27 15:52:42Z suehring
269! Initial read of geostrophic wind components from dynamic driver.
270!
271! 2773 2018-01-30 14:12:54Z suehring
272! Revise checks for surface_fraction.
273!
274! 2925 2018-03-23 14:54:11Z suehring
275! Check for further inconsistent settings of surface_fractions.
276! Some messages slightly rephrased and error numbers renamed.
277!
278! 2898 2018-03-15 13:03:01Z suehring
279! Check if each building has a type. Further, check if dimensions in static
280! input file match the model dimensions.
281!
282! 2897 2018-03-15 11:47:16Z suehring
283! Relax restrictions for topography input, terrain and building heights can be
284! input separately and are not mandatory any more.
285!
286! 2874 2018-03-13 10:55:42Z knoop
287! Bugfix: wrong placement of netcdf cpp-macros fixed
288!
289! 2794 2018-02-07 14:09:43Z knoop
290! Check if 3D building input is consistent to numeric grid.
291!
292! 2773 2018-01-30 14:12:54Z suehring
293! - Enable initialization with 3D topography.
294! - Move check for correct initialization in nesting mode to check_parameters.
295!
296! 2772 2018-01-29 13:10:35Z suehring
297! Initialization of simulation independent on land-surface model.
298!
299! 2746 2018-01-15 12:06:04Z suehring
300! Read plant-canopy variables independently on land-surface model usage
301!
302! 2718 2018-01-02 08:49:38Z maronga
303! Corrected "Former revisions" section
304!
305! 2711 2017-12-20 17:04:49Z suehring
306! Rename subroutine close_file to avoid double-naming.
307!
308! 2700 2017-12-15 14:12:35Z suehring
309!
310! 2696 2017-12-14 17:12:51Z kanani
311! Initial revision (suehring)
312!
313! Authors:
314! --------
315! @author Matthias Suehring
316! @author Edward C. Chan
317! @author Emanuele Russo
318!
319! Description:
320! ------------
321!> Modulue contains routines to input data according to Palm input data
322!> standart using dynamic and static input files.
323!> @todo - Chemistry: revise reading of netcdf file and ajdust formatting
324!>         according to standard!!! (ecc/done)
325!> @todo - Order input alphabetically
326!> @todo - Revise error messages and error numbers
327!> @todo - Input of missing quantities (chemical species, emission rates)
328!> @todo - Defninition and input of still missing variable attributes
329!>         (ecc/what are they?)
330!> @todo - Input of initial geostrophic wind profiles with cyclic conditions.
331!> @todo - remove z dimension from default_emission_data nad preproc_emission_data
332!          and correpsonding subroutines get_var_5d_real and get_var_5d_dynamic (ecc)
333!> @todo - decpreciate chem_emis_att_type@nspec (ecc)
334!> @todo - depreciate subroutines get_variable_4d_to_3d_real and
335!>         get_variable_5d_to_4d_real (ecc)
336!> @todo - introduce useful debug_message(s)
337!------------------------------------------------------------------------------!
338 MODULE netcdf_data_input_mod
339
340    USE control_parameters,                                                    &
341        ONLY:  coupling_char, io_blocks, io_group
342
343    USE cpulog,                                                                &
344        ONLY:  cpu_log, log_point_s
345
346    USE indices,                                                               &
347        ONLY:  nbgp
348
349    USE kinds
350
351#if defined ( __netcdf )
352    USE NETCDF
353#endif
354
355    USE pegrid
356
357    USE surface_mod,                                                           &
358        ONLY:  ind_pav_green, ind_veg_wall, ind_wat_win
359!
360!-- Define type for dimensions.
361    TYPE dims_xy
362       INTEGER(iwp) :: nx                             !< dimension length in x
363       INTEGER(iwp) :: ny                             !< dimension length in y
364       INTEGER(iwp) :: nz                             !< dimension length in z
365       REAL(wp), DIMENSION(:), ALLOCATABLE :: x       !< dimension array in x
366       REAL(wp), DIMENSION(:), ALLOCATABLE :: y       !< dimension array in y
367       REAL(wp), DIMENSION(:), ALLOCATABLE :: z       !< dimension array in z
368    END TYPE dims_xy
369!
370!-- Define data type for nesting in larger-scale models like COSMO.
371!-- Data type comprises u, v, w, pt, and q at lateral and top boundaries.
372    TYPE nest_offl_type
373
374       CHARACTER(LEN=16) ::  char_l = 'ls_forcing_left_'  !< leading substring for variables at left boundary
375       CHARACTER(LEN=17) ::  char_n = 'ls_forcing_north_' !< leading substring for variables at north boundary 
376       CHARACTER(LEN=17) ::  char_r = 'ls_forcing_right_' !< leading substring for variables at right boundary 
377       CHARACTER(LEN=17) ::  char_s = 'ls_forcing_south_' !< leading substring for variables at south boundary
378       CHARACTER(LEN=15) ::  char_t = 'ls_forcing_top_'   !< leading substring for variables at top boundary
379
380       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names         !< list of variable in dynamic input file
381       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names_chem_l  !< names of mesoscale nested chemistry variables at left boundary
382       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names_chem_n  !< names of mesoscale nested chemistry variables at north boundary
383       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names_chem_r  !< names of mesoscale nested chemistry variables at right boundary
384       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names_chem_s  !< names of mesoscale nested chemistry variables at south boundary
385       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names_chem_t  !< names of mesoscale nested chemistry variables at top boundary
386
387       INTEGER(iwp) ::  nt     !< number of time levels in dynamic input file
388       INTEGER(iwp) ::  nzu    !< number of vertical levels on scalar grid in dynamic input file
389       INTEGER(iwp) ::  nzw    !< number of vertical levels on w grid in dynamic input file
390       INTEGER(iwp) ::  tind   !< time index for reference time in mesoscale-offline nesting
391       INTEGER(iwp) ::  tind_p !< time index for following time in mesoscale-offline nesting
392
393       LOGICAL      ::  init         = .FALSE. !< flag indicating that offline nesting is already initialized
394
395       LOGICAL, DIMENSION(:), ALLOCATABLE ::  chem_from_file_l !< flags inidicating whether left boundary data for chemistry is in dynamic input file 
396       LOGICAL, DIMENSION(:), ALLOCATABLE ::  chem_from_file_n !< flags inidicating whether north boundary data for chemistry is in dynamic input file
397       LOGICAL, DIMENSION(:), ALLOCATABLE ::  chem_from_file_r !< flags inidicating whether right boundary data for chemistry is in dynamic input file
398       LOGICAL, DIMENSION(:), ALLOCATABLE ::  chem_from_file_s !< flags inidicating whether south boundary data for chemistry is in dynamic input file
399       LOGICAL, DIMENSION(:), ALLOCATABLE ::  chem_from_file_t !< flags inidicating whether top boundary data for chemistry is in dynamic input file
400
401       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surface_pressure !< time dependent surface pressure
402       REAL(wp), DIMENSION(:), ALLOCATABLE ::  time             !< time levels in dynamic input file
403       REAL(wp), DIMENSION(:), ALLOCATABLE ::  zu_atmos         !< vertical levels at scalar grid in dynamic input file
404       REAL(wp), DIMENSION(:), ALLOCATABLE ::  zw_atmos         !< vertical levels at w grid in dynamic input file
405
406       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  ug         !< domain-averaged geostrophic component
407       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  vg         !< domain-averaged geostrophic component
408
409       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_left   !< u-component at left boundary
410       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_left   !< v-component at left boundary
411       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_left   !< w-component at left boundary
412       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  q_left   !< mixing ratio at left boundary
413       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  pt_left  !< potentital temperautre at left boundary
414
415       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_north  !< u-component at north boundary
416       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_north  !< v-component at north boundary
417       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_north  !< w-component at north boundary
418       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  q_north  !< mixing ratio at north boundary
419       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  pt_north !< potentital temperautre at north boundary
420
421       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_right  !< u-component at right boundary
422       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_right  !< v-component at right boundary
423       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_right  !< w-component at right boundary
424       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  q_right  !< mixing ratio at right boundary
425       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  pt_right !< potentital temperautre at right boundary
426
427       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_south  !< u-component at south boundary
428       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_south  !< v-component at south boundary
429       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_south  !< w-component at south boundary
430       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  q_south  !< mixing ratio at south boundary
431       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  pt_south !< potentital temperautre at south boundary
432
433       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_top    !< u-component at top boundary
434       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_top    !< v-component at top boundary
435       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_top    !< w-component at top boundary
436       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  q_top    !< mixing ratio at top boundary
437       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  pt_top   !< potentital temperautre at top boundary
438       
439       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  chem_left   !< chemical species at left boundary
440       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  chem_north  !< chemical species at left boundary
441       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  chem_right  !< chemical species at left boundary
442       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  chem_south  !< chemical species at left boundary
443       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  chem_top    !< chemical species at left boundary
444
445    END TYPE nest_offl_type
446
447    TYPE init_type
448
449       CHARACTER(LEN=16) ::  init_char = 'init_atmosphere_'          !< leading substring for init variables
450       CHARACTER(LEN=23) ::  origin_time = '2000-01-01 00:00:00 +00' !< reference time of input data
451       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names_chem !< list of chemistry variable names that can potentially be on file
452
453       INTEGER(iwp) ::  lod_msoil !< level of detail - soil moisture
454       INTEGER(iwp) ::  lod_pt    !< level of detail - pt
455       INTEGER(iwp) ::  lod_q     !< level of detail - q
456       INTEGER(iwp) ::  lod_tsoil !< level of detail - soil temperature
457       INTEGER(iwp) ::  lod_u     !< level of detail - u-component
458       INTEGER(iwp) ::  lod_v     !< level of detail - v-component
459       INTEGER(iwp) ::  lod_w     !< level of detail - w-component
460       INTEGER(iwp) ::  nx        !< number of scalar grid points along x in dynamic input file
461       INTEGER(iwp) ::  nxu       !< number of u grid points along x in dynamic input file
462       INTEGER(iwp) ::  ny        !< number of scalar grid points along y in dynamic input file
463       INTEGER(iwp) ::  nyv       !< number of v grid points along y in dynamic input file
464       INTEGER(iwp) ::  nzs       !< number of vertical soil levels in dynamic input file
465       INTEGER(iwp) ::  nzu       !< number of vertical levels on scalar grid in dynamic input file
466       INTEGER(iwp) ::  nzw       !< number of vertical levels on w grid in dynamic input file
467       
468       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  lod_chem !< level of detail - chemistry variables
469
470       LOGICAL ::  from_file_msoil  = .FALSE. !< flag indicating whether soil moisture is already initialized from file
471       LOGICAL ::  from_file_pt     = .FALSE. !< flag indicating whether pt is already initialized from file
472       LOGICAL ::  from_file_q      = .FALSE. !< flag indicating whether q is already initialized from file
473       LOGICAL ::  from_file_tsoil  = .FALSE. !< flag indicating whether soil temperature is already initialized from file
474       LOGICAL ::  from_file_u      = .FALSE. !< flag indicating whether u is already initialized from file
475       LOGICAL ::  from_file_ug     = .FALSE. !< flag indicating whether ug is already initialized from file
476       LOGICAL ::  from_file_v      = .FALSE. !< flag indicating whether v is already initialized from file
477       LOGICAL ::  from_file_vg     = .FALSE. !< flag indicating whether ug is already initialized from file
478       LOGICAL ::  from_file_w      = .FALSE. !< flag indicating whether w is already initialized from file
479       
480       LOGICAL, DIMENSION(:), ALLOCATABLE ::  from_file_chem !< flag indicating whether chemistry variable is read from file
481
482       REAL(wp) ::  fill_msoil              !< fill value for soil moisture
483       REAL(wp) ::  fill_pt                 !< fill value for pt
484       REAL(wp) ::  fill_q                  !< fill value for q
485       REAL(wp) ::  fill_tsoil              !< fill value for soil temperature
486       REAL(wp) ::  fill_u                  !< fill value for u
487       REAL(wp) ::  fill_v                  !< fill value for v
488       REAL(wp) ::  fill_w                  !< fill value for w
489       REAL(wp) ::  latitude = 0.0_wp       !< latitude of the lower left corner
490       REAL(wp) ::  longitude = 0.0_wp      !< longitude of the lower left corner
491       REAL(wp) ::  origin_x = 500000.0_wp  !< UTM easting of the lower left corner
492       REAL(wp) ::  origin_y = 0.0_wp       !< UTM northing of the lower left corner
493       REAL(wp) ::  origin_z = 0.0_wp       !< reference height of input data
494       REAL(wp) ::  rotation_angle = 0.0_wp !< rotation angle of input data
495
496       REAL(wp), DIMENSION(:), ALLOCATABLE ::  fill_chem    !< fill value - chemistry variables
497       REAL(wp), DIMENSION(:), ALLOCATABLE ::  msoil_1d     !< initial vertical profile of soil moisture
498       REAL(wp), DIMENSION(:), ALLOCATABLE ::  pt_init      !< initial vertical profile of pt
499       REAL(wp), DIMENSION(:), ALLOCATABLE ::  q_init       !< initial vertical profile of q
500       REAL(wp), DIMENSION(:), ALLOCATABLE ::  tsoil_1d     !< initial vertical profile of soil temperature
501       REAL(wp), DIMENSION(:), ALLOCATABLE ::  u_init       !< initial vertical profile of u
502       REAL(wp), DIMENSION(:), ALLOCATABLE ::  ug_init      !< initial vertical profile of ug
503       REAL(wp), DIMENSION(:), ALLOCATABLE ::  v_init       !< initial vertical profile of v
504       REAL(wp), DIMENSION(:), ALLOCATABLE ::  vg_init      !< initial vertical profile of ug
505       REAL(wp), DIMENSION(:), ALLOCATABLE ::  w_init       !< initial vertical profile of w
506       REAL(wp), DIMENSION(:), ALLOCATABLE ::  z_soil       !< vertical levels in soil in dynamic input file, used for interpolation
507       REAL(wp), DIMENSION(:), ALLOCATABLE ::  zu_atmos     !< vertical levels at scalar grid in dynamic input file, used for interpolation
508       REAL(wp), DIMENSION(:), ALLOCATABLE ::  zw_atmos     !< vertical levels at w grid in dynamic input file, used for interpolation
509       
510       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  chem_init  !< initial vertical profiles of chemistry variables
511
512
513       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  msoil_3d !< initial 3d soil moisture provide by Inifor and interpolated onto soil grid
514       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  tsoil_3d !< initial 3d soil temperature provide by Inifor and interpolated onto soil grid
515
516    END TYPE init_type
517
518!-- Data type for the general information of chemistry emissions, do not dependent on the particular chemical species
519    TYPE chem_emis_att_type 
520
521       !-DIMENSIONS
522       
523       INTEGER(iwp)                                 :: nspec=0            !< no of chem species provided in emission_values
524       INTEGER(iwp)                                 :: n_emiss_species=0  !< no of chem species provided in emission_values
525                                                                          !< same function as nspec, which will be depreciated (ecc)
526                                                                                 
527       INTEGER(iwp)                                 :: ncat=0             !< number of emission categories
528       INTEGER(iwp)                                 :: nvoc=0             !< number of VOC components
529       INTEGER(iwp)                                 :: npm=0              !< number of PM components
530       INTEGER(iwp)                                 :: nnox=2             !< number of NOx components: NO and NO2
531       INTEGER(iwp)                                 :: nsox=2             !< number of SOX components: SO and SO4
532       INTEGER(iwp)                                 :: nhoursyear         !< number of hours of a specific year in the HOURLY mode
533                                                                          !< of the default mode
534       INTEGER(iwp)                                 :: nmonthdayhour      !< number of month days and hours in the MDH mode
535                                                                          !< of the default mode
536       INTEGER(iwp)                                 :: dt_emission        !< Number of emissions timesteps for one year
537                                                                          !< in the pre-processed emissions case
538       !-- 1d emission input variables
539       CHARACTER (LEN=25),ALLOCATABLE, DIMENSION(:) :: pm_name       !< Names of PM components
540       CHARACTER (LEN=25),ALLOCATABLE, DIMENSION(:) :: cat_name      !< Emission category names
541       CHARACTER (LEN=25),ALLOCATABLE, DIMENSION(:) :: species_name  !< Names of emission chemical species
542       CHARACTER (LEN=25),ALLOCATABLE, DIMENSION(:) :: voc_name      !< Names of VOCs components
543       CHARACTER (LEN=25)                           :: units         !< Units
544
545       INTEGER(iwp)                                 :: i_hour         !< indices for assigning emission values at different timesteps
546       INTEGER(iwp),ALLOCATABLE, DIMENSION(:)       :: cat_index      !< Indices for emission categories
547       INTEGER(iwp),ALLOCATABLE, DIMENSION(:)       :: species_index  !< Indices for emission chem species
548
549       REAL(wp),ALLOCATABLE, DIMENSION(:)           :: xm             !< Molecular masses of emission chem species
550
551       !-- 2d emission input variables
552       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: hourly_emis_time_factor  !< Time factors for HOURLY emissions (DEFAULT mode)
553       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: mdh_emis_time_factor     !< Time factors for MDH emissions (DEFAULT mode)
554       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: nox_comp                 !< Composition of NO and NO2
555       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: sox_comp                 !< Composition of SO2 and SO4
556       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: voc_comp                 !< Composition of VOC components (not fixed)
557
558       !-- 3d emission input variables
559       REAL(wp),ALLOCATABLE, DIMENSION(:,:,:)       :: pm_comp                  !< Composition of PM components (not fixed)
560 
561    END TYPE chem_emis_att_type
562
563
564!-- Data type for the values of chemistry emissions
565    TYPE chem_emis_val_type 
566
567       !REAL(wp),ALLOCATABLE, DIMENSION(:,:)     :: stack_height           !< stack height (ecc / to be implemented)
568       REAL(wp),ALLOCATABLE, DIMENSION(:,:,:)    :: default_emission_data  !< Emission input values for LOD1 (DEFAULT mode)
569       REAL(wp),ALLOCATABLE, DIMENSION(:,:,:,:)  :: preproc_emission_data  !< Emission input values for LOD2 (PRE-PROCESSED mode)
570
571    END TYPE chem_emis_val_type
572
573!
574!-- Define data structures for different input data types.
575!-- 8-bit Integer 2D
576    TYPE int_2d_8bit
577       INTEGER(KIND=1) ::  fill = -127                      !< fill value
578       INTEGER(KIND=1), DIMENSION(:,:), ALLOCATABLE ::  var !< respective variable
579
580       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
581    END TYPE int_2d_8bit
582!
583!-- 8-bit Integer 3D
584    TYPE int_3d_8bit
585       INTEGER(KIND=1) ::  fill = -127                           !< fill value
586       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var_3d !< respective variable
587
588       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
589    END TYPE int_3d_8bit
590!
591!-- 32-bit Integer 2D
592    TYPE int_2d_32bit
593       INTEGER(iwp) ::  fill = -9999                      !< fill value
594       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  var  !< respective variable
595
596       LOGICAL ::  from_file = .FALSE. !< flag indicating whether an input variable is available and read from file or default values are used
597    END TYPE int_2d_32bit
598
599!
600!-- Define data type to read 2D real variables
601    TYPE real_2d
602       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
603
604       REAL(wp) ::  fill = -9999.9_wp                !< fill value
605       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  var !< respective variable
606    END TYPE real_2d
607
608!
609!-- Define data type to read 3D real variables
610    TYPE real_3d
611       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
612
613       INTEGER(iwp) ::  nz   !< number of grid points along vertical dimension
614
615       REAL(wp) ::  fill = -9999.9_wp                  !< fill value
616       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  var !< respective variable
617    END TYPE real_3d
618!
619!-- Define data structure where the dimension and type of the input depends
620!-- on the given level of detail.
621!-- For buildings, the input is either 2D float, or 3d byte.
622    TYPE build_in
623       INTEGER(iwp)    ::  lod = 1                               !< level of detail
624       INTEGER(KIND=1) ::  fill2 = -127                          !< fill value for lod = 2
625       INTEGER(iwp)    ::  nz                                    !< number of vertical layers in file
626       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var_3d !< 3d variable (lod = 2)
627
628       REAL(wp), DIMENSION(:), ALLOCATABLE ::  z                 !< vertical coordinate for 3D building, used for consistency check
629
630       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
631
632       REAL(wp)                              ::  fill1 = -9999.9_wp !< fill values for lod = 1
633       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  var_2d             !< 2d variable (lod = 1)
634    END TYPE build_in
635
636!
637!-- For soil_type, the input is either 2D or 3D one-byte integer.
638    TYPE soil_in
639       INTEGER(iwp)                                   ::  lod = 1      !< level of detail
640       INTEGER(KIND=1)                                ::  fill = -127  !< fill value for lod = 2
641       INTEGER(KIND=1), DIMENSION(:,:), ALLOCATABLE   ::  var_2d       !< 2d variable (lod = 1)
642       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var_3d       !< 3d variable (lod = 2)
643
644       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
645    END TYPE soil_in
646
647!
648!-- Define data type for fractions between surface types
649    TYPE fracs
650       INTEGER(iwp)                            ::  nf             !< total number of fractions
651       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nfracs         !< dimension array for fraction
652
653       LOGICAL ::  from_file = .FALSE. !< flag indicating whether an input variable is available and read from file or default values are used
654
655       REAL(wp)                                ::  fill = -9999.9_wp !< fill value
656       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  frac              !< respective fraction between different surface types
657    END TYPE fracs
658!
659!-- Data type for parameter lists, Depending on the given level of detail,
660!-- the input is 3D or 4D
661    TYPE pars
662       INTEGER(iwp)                            ::  lod = 1         !< level of detail
663       INTEGER(iwp)                            ::  np              !< total number of parameters
664       INTEGER(iwp)                            ::  nz              !< vertical dimension - number of soil layers
665       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  layers          !< dimension array for soil layers
666       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  pars            !< dimension array for parameters
667
668       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
669
670       REAL(wp)                                  ::  fill = -9999.9_wp !< fill value
671       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE   ::  pars_xy           !< respective parameters, level of detail = 1
672       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  pars_xyz          !< respective parameters, level of detail = 2
673    END TYPE pars
674!
675!-- Define type for global file attributes
676!-- Please refer to the PALM data standard for a detailed description of each
677!-- attribute.
678    TYPE global_atts_type
679       CHARACTER(LEN=200) ::  acronym = ' '                      !< acronym of institution
680       CHARACTER(LEN=7)   ::  acronym_char = 'acronym'           !< name of attribute
681       CHARACTER(LEN=200) ::  author  = ' '                      !< first name, last name, email adress
682       CHARACTER(LEN=6)   ::  author_char = 'author'             !< name of attribute
683       CHARACTER(LEN=200) ::  campaign = 'PALM-4U'               !< name of campaign
684       CHARACTER(LEN=8)   ::  campaign_char = 'campaign'         !< name of attribute
685       CHARACTER(LEN=200) ::  comment = ' '                      !< comment to data
686       CHARACTER(LEN=7)   ::  comment_char = 'comment'           !< name of attribute
687       CHARACTER(LEN=200) ::  contact_person = ' '               !< first name, last name, email adress
688       CHARACTER(LEN=14)  ::  contact_person_char = 'contact_person'  !< name of attribute
689       CHARACTER(LEN=200) ::  conventions = 'CF-1.7'             !< netCDF convention
690       CHARACTER(LEN=11)  ::  conventions_char = 'Conventions'   !< name of attribute
691       CHARACTER(LEN=23 ) ::  creation_time = ' '                !< creation time of data set
692       CHARACTER(LEN=13)  ::  creation_time_char = 'creation_time'  !< name of attribute
693       CHARACTER(LEN=200) ::  data_content = ' '                 !< content of data set
694       CHARACTER(LEN=12)  ::  data_content_char = 'data_content' !< name of attribute
695       CHARACTER(LEN=200) ::  dependencies = ' '                 !< dependencies of data set
696       CHARACTER(LEN=12)  ::  dependencies_char = 'dependencies' !< name of attribute
697       CHARACTER(LEN=200) ::  history = ' '                      !< information about data processing
698       CHARACTER(LEN=7)   ::  history_char = 'history'           !< name of attribute
699       CHARACTER(LEN=200) ::  institution = ' '                  !< name of responsible institution
700       CHARACTER(LEN=11)  ::  institution_char = 'institution'   !< name of attribute
701       CHARACTER(LEN=200) ::  keywords = ' '                     !< keywords of data set
702       CHARACTER(LEN=8)   ::  keywords_char = 'keywords'         !< name of attribute
703       CHARACTER(LEN=200) ::  licence = ' '                      !< licence of data set
704       CHARACTER(LEN=7)   ::  licence_char = 'licence'           !< name of attribute
705       CHARACTER(LEN=200) ::  location = ' '                     !< place which refers to data set
706       CHARACTER(LEN=8)   ::  location_char = 'location'         !< name of attribute
707       CHARACTER(LEN=10)  ::  origin_lat_char = 'origin_lat'     !< name of attribute
708       CHARACTER(LEN=10)  ::  origin_lon_char = 'origin_lon'     !< name of attribute
709       CHARACTER(LEN=23 ) ::  origin_time = '2000-01-01 00:00:00 +00'  !< reference time
710       CHARACTER(LEN=11)  ::  origin_time_char = 'origin_time'   !< name of attribute
711       CHARACTER(LEN=8)   ::  origin_x_char = 'origin_x'         !< name of attribute
712       CHARACTER(LEN=8)   ::  origin_y_char = 'origin_y'         !< name of attribute
713       CHARACTER(LEN=8)   ::  origin_z_char = 'origin_z'         !< name of attribute
714       CHARACTER(LEN=12)  ::  palm_version_char = 'palm_version' !< name of attribute
715       CHARACTER(LEN=200) ::  references = ' '                   !< literature referring to data set
716       CHARACTER(LEN=10)  ::  references_char = 'references'     !< name of attribute
717       CHARACTER(LEN=14)  ::  rotation_angle_char = 'rotation_angle'  !< name of attribute
718       CHARACTER(LEN=200) ::  site = ' '                         !< name of model domain
719       CHARACTER(LEN=4)   ::  site_char = 'site'                 !< name of attribute
720       CHARACTER(LEN=200) ::  source = ' '                       !< source of data set
721       CHARACTER(LEN=6)   ::  source_char = 'source'             !< name of attribute
722       CHARACTER(LEN=200) ::  title = ' '                        !< title of data set
723       CHARACTER(LEN=5)   ::  title_char = 'title'               !< name of attribute
724       CHARACTER(LEN=7)   ::  version_char = 'version'           !< name of attribute
725
726       INTEGER(iwp) ::  version              !< version of data set
727
728       REAL(wp) ::  origin_lat               !< latitude of lower left corner
729       REAL(wp) ::  origin_lon               !< longitude of lower left corner
730       REAL(wp) ::  origin_x                 !< easting (UTM coordinate) of lower left corner
731       REAL(wp) ::  origin_y                 !< northing (UTM coordinate) of lower left corner
732       REAL(wp) ::  origin_z                 !< reference height
733       REAL(wp) ::  palm_version             !< PALM version of data set
734       REAL(wp) ::  rotation_angle           !< rotation angle of coordinate system of data set
735    END TYPE global_atts_type
736!
737!-- Define type for coordinate reference system (crs)
738    TYPE crs_type
739       CHARACTER(LEN=200) ::  epsg_code = 'EPSG:25831'                   !< EPSG code
740       CHARACTER(LEN=200) ::  grid_mapping_name = 'transverse_mercator'  !< name of grid mapping
741       CHARACTER(LEN=200) ::  long_name = 'coordinate reference system'  !< name of variable crs
742       CHARACTER(LEN=200) ::  units = 'm'                                !< unit of crs
743
744       REAL(wp) ::  false_easting = 500000.0_wp                  !< false easting
745       REAL(wp) ::  false_northing = 0.0_wp                      !< false northing
746       REAL(wp) ::  inverse_flattening = 298.257223563_wp        !< 1/f (default for WGS84)
747       REAL(wp) ::  latitude_of_projection_origin = 0.0_wp       !< latitude of projection origin
748       REAL(wp) ::  longitude_of_central_meridian = 3.0_wp       !< longitude of central meridian of UTM zone (default: zone 31)
749       REAL(wp) ::  longitude_of_prime_meridian = 0.0_wp         !< longitude of prime meridian
750       REAL(wp) ::  scale_factor_at_central_meridian = 0.9996_wp !< scale factor of UTM coordinates
751       REAL(wp) ::  semi_major_axis = 6378137.0_wp               !< length of semi major axis (default for WGS84)
752    END TYPE crs_type
753
754!
755!-- Define variables
756    TYPE(crs_type)   ::  coord_ref_sys  !< coordinate reference system
757
758    TYPE(dims_xy)    ::  dim_static     !< data structure for x, y-dimension in static input file
759
760    TYPE(nest_offl_type) ::  nest_offl  !< data structure for data input at lateral and top boundaries (provided by Inifor) 
761
762    TYPE(init_type) ::  init_3d    !< data structure for the initialization of the 3D flow and soil fields
763    TYPE(init_type) ::  init_model !< data structure for the initialization of the model
764
765!
766!-- Define 2D variables of type NC_BYTE
767    TYPE(int_2d_8bit)  ::  albedo_type_f     !< input variable for albedo type
768    TYPE(int_2d_8bit)  ::  building_type_f   !< input variable for building type
769    TYPE(int_2d_8bit)  ::  pavement_type_f   !< input variable for pavenment type
770    TYPE(int_2d_8bit)  ::  street_crossing_f !< input variable for water type
771    TYPE(int_2d_8bit)  ::  street_type_f     !< input variable for water type
772    TYPE(int_2d_8bit)  ::  vegetation_type_f !< input variable for vegetation type
773    TYPE(int_2d_8bit)  ::  water_type_f      !< input variable for water type
774!
775!-- Define 3D variables of type NC_BYTE
776    TYPE(int_3d_8bit)  ::  building_obstruction_f    !< input variable for building obstruction
777    TYPE(int_3d_8bit)  ::  building_obstruction_full !< input variable for building obstruction
778!
779!-- Define 2D variables of type NC_INT
780    TYPE(int_2d_32bit) ::  building_id_f     !< input variable for building ID
781!
782!-- Define 2D variables of type NC_FLOAT
783    TYPE(real_2d) ::  terrain_height_f       !< input variable for terrain height
784    TYPE(real_2d) ::  uvem_irradiance_f      !< input variable for uvem irradiance lookup table
785    TYPE(real_2d) ::  uvem_integration_f     !< input variable for uvem integration
786!
787!-- Define 3D variables of type NC_FLOAT
788    TYPE(real_3d) ::  basal_area_density_f    !< input variable for basal area density - resolved vegetation
789    TYPE(real_3d) ::  leaf_area_density_f     !< input variable for leaf area density - resolved vegetation
790    TYPE(real_3d) ::  root_area_density_lad_f !< input variable for root area density - resolved vegetation
791    TYPE(real_3d) ::  root_area_density_lsm_f !< input variable for root area density - parametrized vegetation
792    TYPE(real_3d) ::  uvem_radiance_f         !< input variable for uvem radiance lookup table
793    TYPE(real_3d) ::  uvem_projarea_f         !< input variable for uvem projection area lookup table
794!
795!-- Define input variable for buildings
796    TYPE(build_in) ::  buildings_f           !< input variable for buildings
797!
798!-- Define input variables for soil_type
799    TYPE(soil_in)  ::  soil_type_f           !< input variable for soil type
800
801    TYPE(fracs) ::  surface_fraction_f       !< input variable for surface fraction
802
803    TYPE(pars)  ::  albedo_pars_f              !< input variable for albedo parameters
804    TYPE(pars)  ::  building_pars_f            !< input variable for building parameters
805    TYPE(pars)  ::  pavement_pars_f            !< input variable for pavement parameters
806    TYPE(pars)  ::  pavement_subsurface_pars_f !< input variable for pavement parameters
807    TYPE(pars)  ::  soil_pars_f                !< input variable for soil parameters
808    TYPE(pars)  ::  vegetation_pars_f          !< input variable for vegetation parameters
809    TYPE(pars)  ::  water_pars_f               !< input variable for water parameters
810
811    TYPE(chem_emis_att_type)                             ::  chem_emis_att    !< Input Information of Chemistry Emission Data from netcdf 
812    TYPE(chem_emis_val_type), ALLOCATABLE, DIMENSION(:)  ::  chem_emis        !< Input Chemistry Emission Data from netcdf 
813
814    CHARACTER(LEN=3)  ::  char_lod  = 'lod'         !< name of level-of-detail attribute in NetCDF file
815
816    CHARACTER(LEN=10) ::  char_fill = '_FillValue'        !< name of fill value attribute in NetCDF file
817
818    CHARACTER(LEN=100) ::  input_file_static  = 'PIDS_STATIC'  !< Name of file which comprises static input data
819    CHARACTER(LEN=100) ::  input_file_dynamic = 'PIDS_DYNAMIC' !< Name of file which comprises dynamic input data
820    CHARACTER(LEN=100) ::  input_file_chem    = 'PIDS_CHEM'    !< Name of file which comprises chemistry input data
821    CHARACTER(LEN=100) ::  input_file_uvem    = 'PIDS_UVEM'    !< Name of file which comprises static uv_exposure model input data
822    CHARACTER(LEN=100) ::  input_file_vm      = 'PIDS_VM'      !< Name of file which comprises virtual measurement data
823   
824    CHARACTER(LEN=25), ALLOCATABLE, DIMENSION(:)    ::  string_values  !< output of string variables read from netcdf input files
825
826    INTEGER(iwp)                                     ::  id_emis        !< NetCDF id of input file for chemistry emissions: TBD: It has to be removed
827
828    INTEGER(iwp) ::  nc_stat         !< return value of nf90 function call
829
830    LOGICAL ::  input_pids_static  = .FALSE.   !< Flag indicating whether Palm-input-data-standard file containing static information exists
831    LOGICAL ::  input_pids_dynamic = .FALSE.   !< Flag indicating whether Palm-input-data-standard file containing dynamic information exists
832    LOGICAL ::  input_pids_chem    = .FALSE.   !< Flag indicating whether Palm-input-data-standard file containing chemistry information exists
833    LOGICAL ::  input_pids_uvem    = .FALSE.   !< Flag indicating whether uv-expoure-model input file containing static information exists
834    LOGICAL ::  input_pids_vm      = .FALSE.   !< Flag indicating whether input file for virtual measurements exist
835
836    LOGICAL ::  collective_read = .FALSE.      !< Enable NetCDF collective read
837
838    TYPE(global_atts_type) ::  input_file_atts !< global attributes of input file
839
840    SAVE
841
842    PRIVATE
843
844    INTERFACE netcdf_data_input_interpolate
845       MODULE PROCEDURE netcdf_data_input_interpolate_1d
846       MODULE PROCEDURE netcdf_data_input_interpolate_1d_soil
847       MODULE PROCEDURE netcdf_data_input_interpolate_2d
848       MODULE PROCEDURE netcdf_data_input_interpolate_3d
849    END INTERFACE netcdf_data_input_interpolate
850
851    INTERFACE netcdf_data_input_check_dynamic
852       MODULE PROCEDURE netcdf_data_input_check_dynamic
853    END INTERFACE netcdf_data_input_check_dynamic
854
855    INTERFACE netcdf_data_input_check_static
856       MODULE PROCEDURE netcdf_data_input_check_static
857    END INTERFACE netcdf_data_input_check_static
858
859    INTERFACE netcdf_data_input_chemistry_data                       
860       MODULE PROCEDURE netcdf_data_input_chemistry_data
861    END INTERFACE netcdf_data_input_chemistry_data
862   
863    INTERFACE netcdf_data_input_get_dimension_length                       
864       MODULE PROCEDURE netcdf_data_input_get_dimension_length
865    END INTERFACE netcdf_data_input_get_dimension_length
866
867    INTERFACE netcdf_data_input_inquire_file
868       MODULE PROCEDURE netcdf_data_input_inquire_file
869    END INTERFACE netcdf_data_input_inquire_file
870
871    INTERFACE netcdf_data_input_init
872       MODULE PROCEDURE netcdf_data_input_init
873    END INTERFACE netcdf_data_input_init
874   
875    INTERFACE netcdf_data_input_att
876       MODULE PROCEDURE netcdf_data_input_att_int8
877       MODULE PROCEDURE netcdf_data_input_att_int32
878       MODULE PROCEDURE netcdf_data_input_att_real
879       MODULE PROCEDURE netcdf_data_input_att_string
880    END INTERFACE netcdf_data_input_att
881
882    INTERFACE netcdf_data_input_init_3d
883       MODULE PROCEDURE netcdf_data_input_init_3d
884    END INTERFACE netcdf_data_input_init_3d
885   
886    INTERFACE netcdf_data_input_init_lsm
887       MODULE PROCEDURE netcdf_data_input_init_lsm
888    END INTERFACE netcdf_data_input_init_lsm
889
890    INTERFACE netcdf_data_input_offline_nesting
891       MODULE PROCEDURE netcdf_data_input_offline_nesting
892    END INTERFACE netcdf_data_input_offline_nesting
893
894    INTERFACE netcdf_data_input_surface_data
895       MODULE PROCEDURE netcdf_data_input_surface_data
896    END INTERFACE netcdf_data_input_surface_data
897
898    INTERFACE netcdf_data_input_var
899       MODULE PROCEDURE netcdf_data_input_var_char
900       MODULE PROCEDURE netcdf_data_input_var_real_1d
901       MODULE PROCEDURE netcdf_data_input_var_real_2d
902    END INTERFACE netcdf_data_input_var
903
904    INTERFACE netcdf_data_input_uvem
905       MODULE PROCEDURE netcdf_data_input_uvem
906    END INTERFACE netcdf_data_input_uvem
907
908    INTERFACE get_variable
909       MODULE PROCEDURE get_variable_1d_char
910       MODULE PROCEDURE get_variable_1d_int
911       MODULE PROCEDURE get_variable_1d_real
912       MODULE PROCEDURE get_variable_2d_int8
913       MODULE PROCEDURE get_variable_2d_int32
914       MODULE PROCEDURE get_variable_2d_real
915       MODULE PROCEDURE get_variable_3d_int8
916       MODULE PROCEDURE get_variable_3d_real
917       MODULE PROCEDURE get_variable_3d_real_dynamic
918       MODULE PROCEDURE get_variable_4d_to_3d_real
919       MODULE PROCEDURE get_variable_4d_real
920       MODULE PROCEDURE get_variable_5d_to_4d_real
921       MODULE PROCEDURE get_variable_5d_real           ! (ecc) temp subroutine 4 reading 5D NC arrays
922       MODULE PROCEDURE get_variable_5d_real_dynamic   ! 2B removed as z is out of emission_values
923       MODULE PROCEDURE get_variable_string
924    END INTERFACE get_variable
925
926    INTERFACE get_variable_pr
927       MODULE PROCEDURE get_variable_pr
928    END INTERFACE get_variable_pr
929
930    INTERFACE get_attribute
931       MODULE PROCEDURE get_attribute_real
932       MODULE PROCEDURE get_attribute_int8
933       MODULE PROCEDURE get_attribute_int32
934       MODULE PROCEDURE get_attribute_string
935    END INTERFACE get_attribute
936
937!
938!-- Public variables
939    PUBLIC albedo_pars_f, albedo_type_f, basal_area_density_f, buildings_f,    &
940           building_id_f, building_pars_f, building_type_f,                    &
941           chem_emis, chem_emis_att, chem_emis_att_type, chem_emis_val_type,   &
942           coord_ref_sys,                                                      &
943           init_3d, init_model, input_file_atts, input_file_static,            &
944           input_pids_static,                                                  &
945           input_pids_dynamic, input_pids_vm, input_file_vm,                   &
946           leaf_area_density_f, nest_offl,                                     &
947           pavement_pars_f, pavement_subsurface_pars_f, pavement_type_f,       &
948           root_area_density_lad_f, root_area_density_lsm_f, soil_pars_f,      &
949           soil_type_f, street_crossing_f, street_type_f, surface_fraction_f,  &
950           terrain_height_f, vegetation_pars_f, vegetation_type_f,             &
951           water_pars_f, water_type_f
952!
953!-- Public uv exposure variables
954    PUBLIC building_obstruction_f, input_file_uvem, input_pids_uvem,           &
955           netcdf_data_input_uvem,                                             &
956           uvem_integration_f, uvem_irradiance_f,                              &
957           uvem_projarea_f, uvem_radiance_f
958
959!
960!-- Public subroutines
961    PUBLIC netcdf_data_input_check_dynamic, netcdf_data_input_check_static,    &
962           netcdf_data_input_chemistry_data,                                   &
963           netcdf_data_input_get_dimension_length,                             &
964           netcdf_data_input_inquire_file,                                     &
965           netcdf_data_input_init, netcdf_data_input_init_lsm,                 &
966           netcdf_data_input_init_3d, netcdf_data_input_att,                   &
967           netcdf_data_input_interpolate, netcdf_data_input_offline_nesting,   &
968           netcdf_data_input_surface_data, netcdf_data_input_topo,             &
969           netcdf_data_input_var, get_attribute, get_variable, open_read_file, &
970           check_existence, inquire_num_variables, inquire_variable_names,     &
971           close_input_file
972
973
974 CONTAINS
975
976!------------------------------------------------------------------------------!
977! Description:
978! ------------
979!> Inquires whether NetCDF input files according to Palm-input-data standard
980!> exist. Moreover, basic checks are performed.
981!------------------------------------------------------------------------------!
982    SUBROUTINE netcdf_data_input_inquire_file
983
984       USE control_parameters,                                                 &
985           ONLY:  topo_no_distinct
986
987       IMPLICIT NONE
988
989#if defined ( __netcdf )
990       INQUIRE( FILE = TRIM( input_file_static )   // TRIM( coupling_char ),   &
991                EXIST = input_pids_static  )
992       INQUIRE( FILE = TRIM( input_file_dynamic ) // TRIM( coupling_char ),    &
993                EXIST = input_pids_dynamic )
994       INQUIRE( FILE = TRIM( input_file_chem )    // TRIM( coupling_char ),    &
995                EXIST = input_pids_chem )
996       INQUIRE( FILE = TRIM( input_file_uvem ) // TRIM( coupling_char ),       &
997                EXIST = input_pids_uvem  )
998       INQUIRE( FILE = TRIM( input_file_vm )      // TRIM( coupling_char ),    &
999                EXIST = input_pids_vm )
1000#endif
1001
1002!
1003!--    As long as topography can be input via ASCII format, no distinction
1004!--    between building and terrain can be made. This case, classify all
1005!--    surfaces as default type. Same in case land-surface and urban-surface
1006!--    model are not applied.
1007       IF ( .NOT. input_pids_static )  THEN
1008          topo_no_distinct = .TRUE.
1009       ENDIF
1010
1011    END SUBROUTINE netcdf_data_input_inquire_file
1012
1013!------------------------------------------------------------------------------!
1014! Description:
1015! ------------
1016!> Reads global attributes and coordinate reference system required for
1017!> initialization of the model.
1018!------------------------------------------------------------------------------!
1019    SUBROUTINE netcdf_data_input_init
1020
1021       IMPLICIT NONE
1022
1023       INTEGER(iwp) ::  id_mod     !< NetCDF id of input file
1024       INTEGER(iwp) ::  var_id_crs !< NetCDF id of variable crs
1025
1026       IF ( .NOT. input_pids_static )  RETURN
1027
1028#if defined ( __netcdf )
1029!
1030!--    Open file in read-only mode
1031       CALL open_read_file( TRIM( input_file_static ) //                       &
1032                            TRIM( coupling_char ), id_mod )
1033!
1034!--    Read global attributes
1035       CALL get_attribute( id_mod, input_file_atts%origin_lat_char,            &
1036                           input_file_atts%origin_lat, .TRUE. )
1037
1038       CALL get_attribute( id_mod, input_file_atts%origin_lon_char,            &
1039                           input_file_atts%origin_lon, .TRUE. )
1040
1041       CALL get_attribute( id_mod, input_file_atts%origin_time_char,           &
1042                           input_file_atts%origin_time, .TRUE. )
1043
1044       CALL get_attribute( id_mod, input_file_atts%origin_x_char,              &
1045                           input_file_atts%origin_x, .TRUE. )
1046
1047       CALL get_attribute( id_mod, input_file_atts%origin_y_char,              &
1048                           input_file_atts%origin_y, .TRUE. )
1049
1050       CALL get_attribute( id_mod, input_file_atts%origin_z_char,              &
1051                           input_file_atts%origin_z, .TRUE. )
1052
1053       CALL get_attribute( id_mod, input_file_atts%rotation_angle_char,        &
1054                           input_file_atts%rotation_angle, .TRUE. )
1055
1056       CALL get_attribute( id_mod, input_file_atts%author_char,                &
1057                           input_file_atts%author, .TRUE., no_abort=.FALSE. )
1058       CALL get_attribute( id_mod, input_file_atts%contact_person_char,        &
1059                           input_file_atts%contact_person, .TRUE., no_abort=.FALSE. )
1060       CALL get_attribute( id_mod, input_file_atts%institution_char,           &
1061                           input_file_atts%institution,    .TRUE., no_abort=.FALSE. )
1062       CALL get_attribute( id_mod, input_file_atts%acronym_char,               &
1063                           input_file_atts%acronym,        .TRUE., no_abort=.FALSE. )
1064
1065       CALL get_attribute( id_mod, input_file_atts%campaign_char,              &
1066                           input_file_atts%campaign, .TRUE., no_abort=.FALSE. )
1067       CALL get_attribute( id_mod, input_file_atts%location_char,              &
1068                           input_file_atts%location, .TRUE., no_abort=.FALSE. )
1069       CALL get_attribute( id_mod, input_file_atts%site_char,                  &
1070                           input_file_atts%site,     .TRUE., no_abort=.FALSE. )
1071
1072       CALL get_attribute( id_mod, input_file_atts%source_char,                &
1073                           input_file_atts%source,     .TRUE., no_abort=.FALSE. )
1074       CALL get_attribute( id_mod, input_file_atts%references_char,            &
1075                           input_file_atts%references, .TRUE., no_abort=.FALSE. )
1076       CALL get_attribute( id_mod, input_file_atts%keywords_char,              &
1077                           input_file_atts%keywords,   .TRUE., no_abort=.FALSE. )
1078       CALL get_attribute( id_mod, input_file_atts%licence_char,               &
1079                           input_file_atts%licence,    .TRUE., no_abort=.FALSE. )
1080       CALL get_attribute( id_mod, input_file_atts%comment_char,               &
1081                           input_file_atts%comment,    .TRUE., no_abort=.FALSE. )
1082!
1083!--    Read coordinate reference system if available
1084       nc_stat = NF90_INQ_VARID( id_mod, 'crs', var_id_crs )
1085       IF ( nc_stat == NF90_NOERR )  THEN
1086          CALL get_attribute( id_mod, 'epsg_code',                             &
1087                              coord_ref_sys%epsg_code,                         &
1088                              .FALSE., 'crs' )
1089          CALL get_attribute( id_mod, 'false_easting',                         &
1090                              coord_ref_sys%false_easting,                     &
1091                              .FALSE., 'crs' )
1092          CALL get_attribute( id_mod, 'false_northing',                        &
1093                              coord_ref_sys%false_northing,                    &
1094                              .FALSE., 'crs' )
1095          CALL get_attribute( id_mod, 'grid_mapping_name',                     &
1096                              coord_ref_sys%grid_mapping_name,                 &
1097                              .FALSE., 'crs' )
1098          CALL get_attribute( id_mod, 'inverse_flattening',                    &
1099                              coord_ref_sys%inverse_flattening,                &
1100                              .FALSE., 'crs' )
1101          CALL get_attribute( id_mod, 'latitude_of_projection_origin',         &
1102                              coord_ref_sys%latitude_of_projection_origin,     &
1103                              .FALSE., 'crs' )
1104          CALL get_attribute( id_mod, 'long_name',                             &
1105                              coord_ref_sys%long_name,                         &
1106                              .FALSE., 'crs' )
1107          CALL get_attribute( id_mod, 'longitude_of_central_meridian',         &
1108                              coord_ref_sys%longitude_of_central_meridian,     &
1109                              .FALSE., 'crs' )
1110          CALL get_attribute( id_mod, 'longitude_of_prime_meridian',           &
1111                              coord_ref_sys%longitude_of_prime_meridian,       &
1112                              .FALSE., 'crs' )
1113          CALL get_attribute( id_mod, 'scale_factor_at_central_meridian',      &
1114                              coord_ref_sys%scale_factor_at_central_meridian,  &
1115                              .FALSE., 'crs' )
1116          CALL get_attribute( id_mod, 'semi_major_axis',                       &
1117                              coord_ref_sys%semi_major_axis,                   &
1118                              .FALSE., 'crs' )
1119          CALL get_attribute( id_mod, 'units',                                 &
1120                              coord_ref_sys%units,                             &
1121                              .FALSE., 'crs' )
1122       ELSE
1123!
1124!--       Calculate central meridian from origin_lon
1125          coord_ref_sys%longitude_of_central_meridian = &
1126             CEILING( input_file_atts%origin_lon / 6.0_wp ) * 6.0_wp - 3.0_wp
1127       ENDIF
1128!
1129!--    Finally, close input file
1130       CALL close_input_file( id_mod )
1131#endif
1132!
1133!--    Copy latitude, longitude, origin_z, rotation angle on init type
1134       init_model%latitude        = input_file_atts%origin_lat
1135       init_model%longitude       = input_file_atts%origin_lon
1136       init_model%origin_time     = input_file_atts%origin_time 
1137       init_model%origin_x        = input_file_atts%origin_x
1138       init_model%origin_y        = input_file_atts%origin_y
1139       init_model%origin_z        = input_file_atts%origin_z 
1140       init_model%rotation_angle  = input_file_atts%rotation_angle 
1141           
1142!
1143!--    In case of nested runs, each model domain might have different longitude
1144!--    and latitude, which would result in different Coriolis parameters and
1145!--    sun-zenith angles. To avoid this, longitude and latitude in each model
1146!--    domain will be set to the values of the root model. Please note, this
1147!--    synchronization is required already here.
1148#if defined( __parallel )
1149       CALL MPI_BCAST( init_model%latitude,  1, MPI_REAL, 0,                   &
1150                       MPI_COMM_WORLD, ierr )
1151       CALL MPI_BCAST( init_model%longitude, 1, MPI_REAL, 0,                   &
1152                       MPI_COMM_WORLD, ierr )
1153#endif
1154
1155    END SUBROUTINE netcdf_data_input_init
1156   
1157!------------------------------------------------------------------------------!
1158! Description:
1159! ------------
1160!> Read an array of characters.
1161!------------------------------------------------------------------------------!
1162    SUBROUTINE netcdf_data_input_var_char( val, search_string, id_mod )
1163
1164       IMPLICIT NONE
1165
1166       CHARACTER(LEN=*) ::  search_string     !< name of the variable
1167       CHARACTER(LEN=*), DIMENSION(:) ::  val !< variable which should be read
1168       
1169       INTEGER(iwp) ::  id_mod   !< NetCDF id of input file
1170
1171#if defined ( __netcdf )
1172!
1173!--    Read variable
1174       CALL get_variable( id_mod, search_string, val )
1175#endif           
1176
1177    END SUBROUTINE netcdf_data_input_var_char
1178   
1179!------------------------------------------------------------------------------!
1180! Description:
1181! ------------
1182!> Read an 1D array of REAL values.
1183!------------------------------------------------------------------------------!
1184    SUBROUTINE netcdf_data_input_var_real_1d( val, search_string, id_mod )
1185
1186       IMPLICIT NONE
1187
1188       CHARACTER(LEN=*) ::  search_string     !< name of the variable     
1189       
1190       INTEGER(iwp) ::  id_mod        !< NetCDF id of input file
1191       
1192       REAL(wp), DIMENSION(:) ::  val !< variable which should be read
1193
1194#if defined ( __netcdf )
1195!
1196!--    Read variable
1197       CALL get_variable( id_mod, search_string, val )
1198#endif           
1199
1200    END SUBROUTINE netcdf_data_input_var_real_1d
1201   
1202!------------------------------------------------------------------------------!
1203! Description:
1204! ------------
1205!> Read an 1D array of REAL values.
1206!------------------------------------------------------------------------------!
1207    SUBROUTINE netcdf_data_input_var_real_2d( val, search_string,              &
1208                                              id_mod, d1s, d1e, d2s, d2e )
1209
1210       IMPLICIT NONE
1211
1212       CHARACTER(LEN=*) ::  search_string     !< name of the variable     
1213       
1214       INTEGER(iwp) ::  id_mod  !< NetCDF id of input file
1215       INTEGER(iwp) ::  d1e     !< end index of first dimension to be read
1216       INTEGER(iwp) ::  d2e     !< end index of second dimension to be read
1217       INTEGER(iwp) ::  d1s     !< start index of first dimension to be read
1218       INTEGER(iwp) ::  d2s     !< start index of second dimension to be read
1219       
1220       REAL(wp), DIMENSION(:,:) ::  val !< variable which should be read
1221
1222#if defined ( __netcdf )
1223!
1224!--    Read character variable
1225       CALL get_variable( id_mod, search_string, val, d1s, d1e, d2s, d2e )
1226#endif           
1227
1228    END SUBROUTINE netcdf_data_input_var_real_2d
1229   
1230!------------------------------------------------------------------------------!
1231! Description:
1232! ------------
1233!> Read a global string attribute
1234!------------------------------------------------------------------------------!
1235    SUBROUTINE netcdf_data_input_att_string( val, search_string, id_mod,       &
1236                                             input_file, global, openclose,    &
1237                                             variable_name )
1238
1239       IMPLICIT NONE
1240
1241       CHARACTER(LEN=*) ::  search_string !< name of the attribue
1242       CHARACTER(LEN=*) ::  val           !< attribute
1243       
1244       CHARACTER(LEN=*) ::  input_file    !< name of input file
1245       CHARACTER(LEN=*) ::  openclose     !< string indicating whether NetCDF needs to be opend or closed
1246       CHARACTER(LEN=*) ::  variable_name !< string indicating whether NetCDF needs to be opend or closed 
1247       
1248       INTEGER(iwp) ::  id_mod   !< NetCDF id of input file
1249       
1250       LOGICAL ::  global                 !< flag indicating a global or a variable's attribute
1251
1252#if defined ( __netcdf )
1253!
1254!--    Open file in read-only mode if necessary
1255       IF ( openclose == 'open' )  THEN
1256          CALL open_read_file( TRIM( input_file ) // TRIM( coupling_char ), &
1257                                  id_mod )
1258       ENDIF
1259!
1260!--    Read global attribute
1261       IF ( global )  THEN
1262          CALL get_attribute( id_mod, search_string, val, global )
1263!
1264!--    Read variable attribute
1265       ELSE
1266          CALL get_attribute( id_mod, search_string, val, global, variable_name )
1267       ENDIF
1268!
1269!--    Close input file
1270       IF ( openclose == 'close' )  CALL close_input_file( id_mod )
1271#endif           
1272
1273    END SUBROUTINE netcdf_data_input_att_string
1274   
1275!------------------------------------------------------------------------------!
1276! Description:
1277! ------------
1278!> Read a global 8-bit integer attribute
1279!------------------------------------------------------------------------------!
1280    SUBROUTINE netcdf_data_input_att_int8( val, search_string, id_mod,         &
1281                                           input_file, global, openclose,      &
1282                                           variable_name )
1283
1284       IMPLICIT NONE
1285
1286       CHARACTER(LEN=*) ::  search_string !< name of the attribue
1287       
1288       CHARACTER(LEN=*) ::  input_file    !< name of input file
1289       CHARACTER(LEN=*) ::  openclose     !< string indicating whether NetCDF needs to be opend or closed
1290       CHARACTER(LEN=*) ::  variable_name !< string indicating whether NetCDF needs to be opend or closed
1291       
1292       INTEGER(iwp) ::  id_mod   !< NetCDF id of input file
1293       INTEGER(KIND=1) ::  val      !< value of the attribute
1294       
1295       LOGICAL ::  global        !< flag indicating a global or a variable's attribute
1296
1297#if defined ( __netcdf )
1298!
1299!--    Open file in read-only mode
1300       IF ( openclose == 'open' )  THEN
1301          CALL open_read_file( TRIM( input_file ) // TRIM( coupling_char ), &
1302                                  id_mod )
1303       ENDIF
1304!
1305!--    Read global attribute
1306       IF ( global )  THEN
1307          CALL get_attribute( id_mod, search_string, val, global )
1308!
1309!--    Read variable attribute
1310       ELSE
1311          CALL get_attribute( id_mod, search_string, val, global, variable_name )
1312       ENDIF
1313!
1314!--    Finally, close input file
1315       IF ( openclose == 'close' )  CALL close_input_file( id_mod )
1316#endif           
1317
1318    END SUBROUTINE netcdf_data_input_att_int8
1319   
1320!------------------------------------------------------------------------------!
1321! Description:
1322! ------------
1323!> Read a global 32-bit integer attribute
1324!------------------------------------------------------------------------------!
1325    SUBROUTINE netcdf_data_input_att_int32( val, search_string, id_mod,        &
1326                                            input_file, global, openclose,     &
1327                                            variable_name )
1328
1329       IMPLICIT NONE
1330
1331       CHARACTER(LEN=*) ::  search_string !< name of the attribue
1332       
1333       CHARACTER(LEN=*) ::  input_file    !< name of input file
1334       CHARACTER(LEN=*) ::  openclose     !< string indicating whether NetCDF needs to be opend or closed
1335       CHARACTER(LEN=*) ::  variable_name !< string indicating whether NetCDF needs to be opend or closed
1336       
1337       INTEGER(iwp) ::  id_mod   !< NetCDF id of input file
1338       INTEGER(iwp) ::  val      !< value of the attribute
1339       
1340       LOGICAL ::  global        !< flag indicating a global or a variable's attribute
1341
1342#if defined ( __netcdf )
1343!
1344!--    Open file in read-only mode
1345       IF ( openclose == 'open' )  THEN
1346          CALL open_read_file( TRIM( input_file ) // TRIM( coupling_char ), &
1347                                  id_mod )
1348       ENDIF
1349!
1350!--    Read global attribute
1351       IF ( global )  THEN
1352          CALL get_attribute( id_mod, search_string, val, global )
1353!
1354!--    Read variable attribute
1355       ELSE
1356          CALL get_attribute( id_mod, search_string, val, global, variable_name )
1357       ENDIF
1358!
1359!--    Finally, close input file
1360       IF ( openclose == 'close' )  CALL close_input_file( id_mod )
1361#endif           
1362
1363    END SUBROUTINE netcdf_data_input_att_int32
1364   
1365!------------------------------------------------------------------------------!
1366! Description:
1367! ------------
1368!> Read a global real attribute
1369!------------------------------------------------------------------------------!
1370    SUBROUTINE netcdf_data_input_att_real( val, search_string, id_mod,         &
1371                                           input_file, global, openclose,      &
1372                                           variable_name )
1373
1374       IMPLICIT NONE
1375
1376       CHARACTER(LEN=*) ::  search_string !< name of the attribue
1377       
1378       CHARACTER(LEN=*) ::  input_file    !< name of input file
1379       CHARACTER(LEN=*) ::  openclose     !< string indicating whether NetCDF needs to be opend or closed
1380       CHARACTER(LEN=*) ::  variable_name !< string indicating whether NetCDF needs to be opend or closed
1381       
1382       INTEGER(iwp) ::  id_mod            !< NetCDF id of input file
1383       
1384       LOGICAL ::  global                 !< flag indicating a global or a variable's attribute
1385       
1386       REAL(wp) ::  val                   !< value of the attribute
1387
1388#if defined ( __netcdf )
1389!
1390!--    Open file in read-only mode
1391       IF ( openclose == 'open' )  THEN
1392          CALL open_read_file( TRIM( input_file ) // TRIM( coupling_char ), &
1393                                  id_mod )
1394       ENDIF
1395!
1396!--    Read global attribute
1397       IF ( global )  THEN
1398          CALL get_attribute( id_mod, search_string, val, global )
1399!
1400!--    Read variable attribute
1401       ELSE
1402          CALL get_attribute( id_mod, search_string, val, global, variable_name )
1403       ENDIF
1404!
1405!--    Finally, close input file
1406       IF ( openclose == 'close' )  CALL close_input_file( id_mod )
1407#endif           
1408
1409    END SUBROUTINE netcdf_data_input_att_real
1410
1411!------------------------------------------------------------------------------!
1412! Description:
1413! ------------
1414!> Reads Chemistry NETCDF Input data, such as emission values, emission species, etc.
1415!------------------------------------------------------------------------------!
1416
1417    SUBROUTINE netcdf_data_input_chemistry_data(emt_att,emt)
1418
1419       USE chem_modules,                                       &
1420           ONLY:  emiss_lod, time_fac_type, surface_csflux_name
1421
1422       USE control_parameters,                                 &
1423           ONLY:  message_string
1424
1425       USE indices,                                            &
1426           ONLY:  nxl, nxr, nys, nyn
1427
1428       IMPLICIT NONE
1429
1430       TYPE(chem_emis_att_type), INTENT(INOUT)                             ::  emt_att
1431       TYPE(chem_emis_val_type), ALLOCATABLE, DIMENSION(:), INTENT(INOUT)  ::  emt
1432   
1433       INTEGER(iwp)  ::  i, j, k      !< generic counters
1434       INTEGER(iwp)  ::  ispec        !< index for number of emission species in input
1435       INTEGER(iwp)  ::  len_dims     !< Length of dimension
1436       INTEGER(iwp)  ::  num_vars     !< number of variables in netcdf input file
1437
1438!
1439!-- dum_var_4d are designed to read in emission_values from the chemistry netCDF file.
1440!-- Currently the vestigial "z" dimension in emission_values makes it a 5D array,
1441!-- hence the corresponding dum_var_5d array.  When the "z" dimension is removed
1442!-- completely, dum_var_4d will be used instead
1443!-- (ecc 20190425)
1444
1445!       REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:)    ::  dum_var_4d  !< temp array 4 4D chem emission data
1446       REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:,:)  ::  dum_var_5d  !< temp array 4 5D chem emission data
1447
1448!
1449!-- Start processing data
1450!
1451!-- Emission LOD 0 (Parameterized mode)
1452
1453        IF  ( emiss_lod == 0 )  THEN
1454
1455! for reference (ecc)
1456!       IF (TRIM(mode_emis) == "PARAMETERIZED" .OR. TRIM(mode_emis) == "parameterized") THEN
1457
1458           ispec=1
1459           emt_att%n_emiss_species = 0
1460
1461!
1462!-- number of species
1463
1464           DO  WHILE (TRIM( surface_csflux_name( ispec ) ) /= 'novalue' )
1465
1466             emt_att%n_emiss_species = emt_att%n_emiss_species + 1
1467             ispec=ispec+1
1468!
1469!-- followling line retained for compatibility with salsa_mod
1470!-- which still uses emt_att%nspec heavily (ecc)
1471
1472             emt_att%nspec = emt_att%nspec + 1
1473
1474           ENDDO
1475
1476!
1477!-- allocate emission values data type arrays
1478
1479          ALLOCATE ( emt(emt_att%n_emiss_species) )
1480
1481!
1482!-- Read EMISSION SPECIES NAMES
1483
1484!
1485!-- allocate space for strings
1486
1487          ALLOCATE (emt_att%species_name(emt_att%n_emiss_species) )
1488 
1489         DO ispec = 1, emt_att%n_emiss_species
1490            emt_att%species_name(ispec) = TRIM(surface_csflux_name(ispec))
1491         ENDDO
1492
1493!
1494!-- LOD 1 (default mode) and LOD 2 (pre-processed mode)
1495
1496       ELSE
1497
1498#if defined ( __netcdf )
1499
1500          IF ( .NOT. input_pids_chem )  RETURN
1501
1502!
1503!-- first we allocate memory space for the emission species and then
1504!-- we differentiate between LOD 1 (default mode) and LOD 2 (pre-processed mode)
1505
1506!
1507!-- open emission data file ( {palmcase}_chemistry )
1508
1509          CALL open_read_file ( TRIM(input_file_chem) // TRIM(coupling_char), id_emis )
1510
1511!
1512!-- inquire number of variables
1513
1514          CALL inquire_num_variables ( id_emis, num_vars )
1515
1516!
1517!-- Get General Dimension Lengths: only # species and # categories.
1518!-- Tther dimensions depend on the emission mode or specific components
1519
1520          CALL netcdf_data_input_get_dimension_length (    &
1521                                 id_emis, emt_att%n_emiss_species, 'nspecies' )
1522
1523!
1524!-- backward compatibility for salsa_mod (ecc)
1525
1526          emt_att%nspec = emt_att%n_emiss_species
1527
1528!
1529!-- Allocate emission values data type arrays
1530
1531          ALLOCATE ( emt(emt_att%n_emiss_species) )
1532
1533!
1534!-- READING IN SPECIES NAMES
1535
1536!
1537!-- Allocate memory for species names
1538
1539          ALLOCATE ( emt_att%species_name(emt_att%n_emiss_species) )
1540
1541!
1542!-- Retrieve variable name (again, should use n_emiss_strlen)
1543
1544          CALL get_variable( id_emis, 'emission_name',    &
1545                             string_values, emt_att%n_emiss_species )
1546          emt_att%species_name=string_values
1547
1548!
1549!-- dealocate string_values previously allocated in get_variable call
1550
1551          IF  ( ALLOCATED(string_values) )  DEALLOCATE (string_values)
1552
1553!
1554!-- READING IN SPECIES INDICES
1555
1556!
1557!-- Allocate memory for species indices
1558
1559          ALLOCATE ( emt_att%species_index(emt_att%n_emiss_species) )
1560
1561!
1562!-- Retrieve variable data
1563
1564          CALL get_variable( id_emis, 'emission_index', emt_att%species_index )
1565!
1566!-- Now the routine has to distinguish between chemistry emission
1567!-- LOD 1 (DEFAULT mode) and LOD 2 (PRE-PROCESSED mode)
1568
1569!
1570!-- START OF EMISSION LOD 1 (DEFAULT MODE)
1571
1572
1573          IF  ( emiss_lod == 1 )  THEN
1574
1575! for reference (ecc)
1576!          IF (TRIM(mode_emis) == "DEFAULT" .OR. TRIM(mode_emis) == "default") THEN
1577
1578!
1579!-- get number of emission categories
1580
1581             CALL netcdf_data_input_get_dimension_length (           &
1582                                    id_emis, emt_att%ncat, 'ncat' )
1583
1584!-- READING IN EMISSION CATEGORIES INDICES
1585
1586             ALLOCATE ( emt_att%cat_index(emt_att%ncat) )
1587
1588!
1589!-- Retrieve variable data
1590
1591             CALL get_variable( id_emis, 'emission_cat_index', emt_att%cat_index )
1592
1593
1594!
1595!-- Loop through individual species to get basic information on
1596!-- VOC/PM/NOX/SOX
1597
1598!------------------------------------------------------------------------------
1599!-- NOTE - CHECK ARRAY INDICES FOR READING IN NAMES AND SPECIES
1600!--        IN LOD1 (DEFAULT MODE) FOR THE VARIOUS MODE SPLITS
1601!--        AS ALL ID_EMIS CONDITIONALS HAVE BEEN REMOVED FROM GET_VAR
1602!--        FUNCTIONS.  IN THEORY THIS WOULD MEAN ALL ARRAYS SHOULD BE
1603!--        READ FROM 0 to N-1 (C CONVENTION) AS OPPOSED TO 1 to N
1604!--        (FORTRAN CONVENTION).  KEEP THIS IN MIND !!
1605!--        (ecc 20190424)
1606!------------------------------------------------------------------------------
1607 
1608             DO  ispec = 1, emt_att%n_emiss_species
1609
1610!
1611!-- VOC DATA (name and composition)
1612
1613                IF  ( TRIM(emt_att%species_name(ispec)) == "VOC" .OR.                  &
1614                      TRIM(emt_att%species_name(ispec)) == "voc" )  THEN
1615
1616!
1617!-- VOC name
1618                   CALL netcdf_data_input_get_dimension_length (     &
1619                                          id_emis, emt_att%nvoc, 'nvoc' )
1620                   ALLOCATE ( emt_att%voc_name(emt_att%nvoc) )
1621                   CALL get_variable ( id_emis,"emission_voc_name",  &
1622                                       string_values, emt_att%nvoc )
1623                   emt_att%voc_name = string_values
1624                   IF  ( ALLOCATED(string_values) )  DEALLOCATE (string_values)
1625
1626!
1627!-- VOC composition
1628
1629                   ALLOCATE ( emt_att%voc_comp(emt_att%ncat,emt_att%nvoc) )
1630                   CALL get_variable ( id_emis, "composition_voc", emt_att%voc_comp,     &
1631                                       1, emt_att%ncat, 1, emt_att%nvoc )
1632
1633                ENDIF  ! VOC
1634
1635!
1636!-- PM DATA (name and composition)
1637
1638                IF  ( TRIM(emt_att%species_name(ispec)) == "PM" .OR.                   &
1639                      TRIM(emt_att%species_name(ispec)) == "pm")  THEN
1640
1641!
1642!-- PM name
1643
1644                   CALL netcdf_data_input_get_dimension_length (     &
1645                                          id_emis, emt_att%npm, 'npm' )
1646                   ALLOCATE ( emt_att%pm_name(emt_att%npm) )
1647                   CALL get_variable ( id_emis, "pm_name", string_values, emt_att%npm )
1648                   emt_att%pm_name = string_values
1649                   IF  ( ALLOCATED(string_values) )  DEALLOCATE (string_values)     
1650
1651!
1652!-- PM composition (PM1, PM2.5 and PM10)
1653
1654                   len_dims = 3  ! PM1, PM2.5, PM10
1655                   ALLOCATE(emt_att%pm_comp(emt_att%ncat,emt_att%npm,len_dims))
1656                   CALL get_variable ( id_emis, "composition_pm", emt_att%pm_comp,       &
1657                                       1, emt_att%ncat, 1, emt_att%npm, 1, len_dims )
1658
1659                ENDIF  ! PM
1660
1661!
1662!-- NOX (NO and NO2)
1663
1664                IF  ( TRIM(emt_att%species_name(ispec)) == "NOX" .OR.                  &
1665                      TRIM(emt_att%species_name(ispec)) == "nox" )  THEN
1666
1667                   ALLOCATE ( emt_att%nox_comp(emt_att%ncat,emt_att%nnox) )
1668                   CALL get_variable ( id_emis, "composition_nox", emt_att%nox_comp,     &
1669                                       1, emt_att%ncat, 1, emt_att%nnox )
1670
1671                ENDIF  ! NOX
1672
1673!
1674!-- SOX (SO2 and SO4)
1675
1676                IF  ( TRIM(emt_att%species_name(ispec)) == "SOX" .OR.                  &
1677                      TRIM(emt_att%species_name(ispec)) == "sox" )  THEN
1678
1679                   ALLOCATE ( emt_att%sox_comp(emt_att%ncat,emt_att%nsox) )
1680                   CALL get_variable ( id_emis, "composition_sox", emt_att%sox_comp,     &
1681                                       1, emt_att%ncat, 1, emt_att%nsox )
1682
1683                ENDIF  ! SOX
1684
1685             ENDDO  ! do ispec
1686
1687!
1688!-- EMISSION TIME SCALING FACTORS (hourly and MDH data)
1689 
1690!     
1691!-- HOUR   
1692             IF  ( TRIM(time_fac_type) == "HOUR" .OR.                        &
1693                   TRIM(time_fac_type) == "hour" )  THEN
1694
1695                CALL netcdf_data_input_get_dimension_length (                  &
1696                                       id_emis, emt_att%nhoursyear, 'nhoursyear' )
1697                ALLOCATE ( emt_att%hourly_emis_time_factor(emt_att%ncat,emt_att%nhoursyear) )
1698                CALL get_variable ( id_emis, "emission_time_factors",          &
1699                                    emt_att%hourly_emis_time_factor,           &
1700                                    1, emt_att%ncat, 1, emt_att%nhoursyear )
1701
1702!
1703!-- MDH
1704
1705             ELSE IF  ( TRIM(time_fac_type)  ==  "MDH" .OR.                  &
1706                        TRIM(time_fac_type)  ==  "mdh" )  THEN
1707
1708                CALL netcdf_data_input_get_dimension_length (                  &
1709                                       id_emis, emt_att%nmonthdayhour, 'nmonthdayhour' )
1710                ALLOCATE ( emt_att%mdh_emis_time_factor(emt_att%ncat,emt_att%nmonthdayhour) )
1711                CALL get_variable ( id_emis, "emission_time_factors",          &
1712                                    emt_att%mdh_emis_time_factor,              &
1713                                    1, emt_att%ncat, 1, emt_att%nmonthdayhour )
1714
1715!
1716!-- ERROR (time factor undefined)
1717
1718             ELSE
1719
1720                message_string = 'We are in the DEFAULT chemistry emissions mode: '  //  &
1721                                 '     !no time-factor type specified!'              //  &
1722                                 'Please specify the value of time_fac_type:'        //  &
1723                                 '         either "MDH" or "HOUR"'                 
1724                CALL message( 'netcdf_data_input_chemistry_data', 'CM0200', 2, 2, 0, 6, 0 ) 
1725 
1726
1727             ENDIF  ! time_fac_type
1728
1729!
1730!-- read in default (LOD1) emissions from chemisty netCDF file per species
1731
1732!
1733!-- NOTE - at the moment the data is read in per species, but in the future it would
1734!--        be much more sensible to read in per species per time step to reduce
1735!--        memory consumption and, to a lesser degree, dimensionality of data exchange
1736!--        (I expect this will be necessary when the problem size is large)
1737
1738             DO ispec = 1, emt_att%n_emiss_species
1739
1740!
1741!-- allocate space for species specific emission values
1742!-- NOTE - this array is extended by 1 cell in each horizontal direction
1743!--        to compensate for an apparent linear offset.  The reason of this
1744!--        offset is not known but it has been determined to take place beyond the
1745!--        scope of this module, and has little to do with index conventions.
1746!--        That is, setting the array horizontal limit from nx0:nx1 to 1:(nx1-nx0+1)
1747!--        or nx0+1:nx1+1 did not result in correct or definite behavior
1748!--        This must be looked at at some point by the Hannover team but for now
1749!--        this workaround is deemed reasonable (ecc 20190417)
1750
1751                IF ( .NOT. ALLOCATED ( emt(ispec)%default_emission_data ) )  THEN
1752                    ALLOCATE ( emt(ispec)%default_emission_data(emt_att%ncat,nys:nyn+1,nxl:nxr+1) )
1753                ENDIF
1754!
1755!-- allocate dummy variable w/ index order identical to that shown in the netCDF header
1756
1757                ALLOCATE ( dum_var_5d(1,nys:nyn,nxl:nxr,1,emt_att%ncat) )
1758!
1759!-- get variable.  be very careful
1760!-- I am using get_variable_5d_real_dynamic (note logical argument at the end)
1761!-- 1) use Fortran index convention (i.e., 1 to N)
1762!-- 2) index order must be in reverse order from above allocation order
1763 
1764                CALL get_variable ( id_emis, "emission_values", dum_var_5d, &
1765                                    1,            ispec, nxl+1,     nys+1,     1,                    &
1766                                    emt_att%ncat, 1,     nxr-nxl+1, nyn-nys+1, emt_att%dt_emission,  &
1767                                    .FALSE. )
1768!
1769!-- assign temp array to data structure then deallocate temp array
1770!-- NOTE - indices are shifted from nx0:nx1 to nx0+1:nx1+1 to offset
1771!--        the emission data array to counter said domain offset
1772!--        (ecc 20190417)
1773
1774                DO k = 1, emt_att%ncat
1775                   DO j = nys+1, nyn+1
1776                      DO i = nxl+1, nxr+1
1777                         emt(ispec)%default_emission_data(k,j,i) = dum_var_5d(1,j-1,i-1,1,k)
1778                      ENDDO
1779                   ENDDO
1780                ENDDO
1781
1782                DEALLOCATE ( dum_var_5d )
1783
1784             ENDDO  ! ispec
1785!
1786!-- UNITS
1787
1788             CALL get_attribute(id_emis,"units",emt_att%units,.FALSE.,"emission_values")
1789
1790!
1791!-- END DEFAULT MODE
1792
1793
1794!
1795!-- START LOD 2 (PRE-PROCESSED MODE)
1796
1797          ELSE IF  ( emiss_lod == 2 )  THEN
1798
1799! for reference (ecc)
1800!          ELSE IF (TRIM(mode_emis) == "PRE-PROCESSED" .OR. TRIM(mode_emis) == "pre-processed") THEN
1801
1802!
1803!-- For LOD 2 only VOC and emission data need be read
1804
1805!------------------------------------------------------------------------------
1806!-- NOTE - CHECK ARRAY INDICES FOR READING IN NAMES AND SPECIES
1807!--        IN LOD2 (PRE-PROCESSED MODE) FOR THE VARIOUS MODE SPLITS
1808!--        AS ALL ID_EMIS CONDITIONALS HAVE BEEN REMOVED FROM GET_VAR
1809!--        FUNCTIONS.  IN THEORY THIS WOULD MEAN ALL ARRAYS SHOULD BE
1810!--        READ FROM 0 to N-1 (C CONVENTION) AS OPPOSED TO 1 to N
1811!--        (FORTRAN CONVENTION).  KEEP THIS IN MIND !!
1812!--        (ecc 20190424)
1813!------------------------------------------------------------------------------
1814
1815             DO ispec = 1, emt_att%n_emiss_species
1816
1817!
1818!-- VOC DATA (name and composition)
1819
1820                IF  ( TRIM(emt_att%species_name(ispec)) == "VOC" .OR.                  &
1821                      TRIM(emt_att%species_name(ispec)) == "voc" )  THEN
1822
1823!
1824!-- VOC name
1825                   CALL netcdf_data_input_get_dimension_length (                         &
1826                                          id_emis, emt_att%nvoc, 'nvoc' )
1827                   ALLOCATE ( emt_att%voc_name(emt_att%nvoc) )
1828                   CALL get_variable ( id_emis, "emission_voc_name",                     &
1829                                       string_values, emt_att%nvoc)
1830                   emt_att%voc_name = string_values
1831                   IF  ( ALLOCATED(string_values) )  DEALLOCATE (string_values)
1832
1833!
1834!-- VOC composition
1835 
1836                   ALLOCATE ( emt_att%voc_comp(emt_att%ncat,emt_att%nvoc) )
1837                   CALL get_variable ( id_emis, "composition_voc", emt_att%voc_comp,     &
1838                                       1, emt_att%ncat, 1, emt_att%nvoc )
1839                ENDIF  ! VOC
1840 
1841             ENDDO  ! ispec
1842
1843!
1844!-- EMISSION DATA
1845
1846             CALL netcdf_data_input_get_dimension_length (                               &
1847                                    id_emis, emt_att%dt_emission, 'time' )   
1848 
1849!
1850!-- read in pre-processed (LOD2) emissions from chemisty netCDF file per species
1851
1852!
1853!-- NOTE - at the moment the data is read in per species, but in the future it would
1854!--        be much more sensible to read in per species per time step to reduce
1855!--        memory consumption and, to a lesser degree, dimensionality of data exchange
1856!--        (I expect this will be necessary when the problem size is large)
1857
1858             DO ispec = 1, emt_att%n_emiss_species
1859
1860!
1861!-- allocate space for species specific emission values
1862!-- NOTE - this array is extended by 1 cell in each horizontal direction
1863!--        to compensate for an apparent linear offset.  The reason of this
1864!--        offset is not known but it has been determined to take place beyond the
1865!--        scope of this module, and has little to do with index conventions.
1866!--        That is, setting the array horizontal limit from nx0:nx1 to 1:(nx1-nx0+1)
1867!--        or nx0+1:nx1+1 did not result in correct or definite behavior
1868!--        This must be looked at at some point by the Hannover team but for now
1869!--        this workaround is deemed reasonable (ecc 20190417)
1870
1871                IF ( .NOT. ALLOCATED( emt(ispec)%preproc_emission_data ) )  THEN
1872                   ALLOCATE( emt(ispec)%preproc_emission_data(                           &
1873                             emt_att%dt_emission, 1, nys:nyn+1, nxl:nxr+1) )
1874                ENDIF
1875!
1876!-- allocate dummy variable w/ index order identical to that shown in the netCDF header
1877
1878                ALLOCATE ( dum_var_5d(emt_att%dt_emission,1,nys:nyn,nxl:nxr,1) )
1879!
1880!-- get variable.  be very careful
1881!-- I am using get_variable_5d_real_dynamic (note logical argument at the end)
1882!-- 1) use Fortran index convention (i.e., 1 to N)
1883!-- 2) index order must be in reverse order from above allocation order
1884
1885                CALL get_variable ( id_emis, "emission_values", dum_var_5d, &
1886                                    ispec, nxl+1,     nys+1,     1, 1,                   &
1887                                    1,     nxr-nxl+1, nyn-nys+1, 1, emt_att%dt_emission, &
1888                                    .FALSE. )
1889!
1890!-- assign temp array to data structure then deallocate temp array
1891!-- NOTE - indices are shifted from nx0:nx1 to nx0+1:nx1+1 to offset
1892!--        the emission data array to counter said unkonwn offset
1893!--        (ecc 20190417)
1894
1895                DO k = 1, emt_att%dt_emission
1896                   DO j = nys+1, nyn+1
1897                      DO i = nxl+1, nxr+1
1898                         emt(ispec)%preproc_emission_data(k,1,j,i) = dum_var_5d(k,1,j-1,i-1,1)
1899                      ENDDO
1900                   ENDDO
1901                ENDDO
1902
1903                DEALLOCATE ( dum_var_5d )
1904
1905             ENDDO  ! ispec
1906!
1907!-- UNITS
1908
1909             CALL get_attribute ( id_emis, "units", emt_att%units, .FALSE. , "emission_values" )
1910       
1911          ENDIF  ! LOD1 & LOD2 (default and pre-processed mode)
1912
1913          CALL close_input_file (id_emis)
1914
1915#endif
1916
1917       ENDIF ! LOD0 (parameterized mode)
1918
1919    END SUBROUTINE netcdf_data_input_chemistry_data
1920
1921
1922!------------------------------------------------------------------------------!
1923! Description:
1924! ------------
1925!> Reads surface classification data, such as vegetation and soil type, etc. .
1926!------------------------------------------------------------------------------!
1927    SUBROUTINE netcdf_data_input_surface_data
1928
1929       USE control_parameters,                                                 &
1930           ONLY:  land_surface, plant_canopy, urban_surface
1931
1932       USE indices,                                                            &
1933           ONLY:  nbgp, nxl, nxr, nyn, nys
1934
1935
1936       IMPLICIT NONE
1937
1938       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names  !< variable names in static input file
1939
1940       INTEGER(iwp) ::  id_surf   !< NetCDF id of input file
1941       INTEGER(iwp) ::  k         !< running index along z-direction
1942       INTEGER(iwp) ::  k2        !< running index
1943       INTEGER(iwp) ::  num_vars  !< number of variables in input file
1944       INTEGER(iwp) ::  nz_soil   !< number of soil layers in file
1945
1946!
1947!--    If not static input file is available, skip this routine
1948       IF ( .NOT. input_pids_static )  RETURN
1949!
1950!--    Measure CPU time
1951       CALL cpu_log( log_point_s(82), 'NetCDF input', 'start' )
1952!
1953!--    Read plant canopy variables.
1954       IF ( plant_canopy )  THEN
1955#if defined ( __netcdf )
1956!
1957!--       Open file in read-only mode
1958          CALL open_read_file( TRIM( input_file_static ) //                    &
1959                               TRIM( coupling_char ) , id_surf )
1960!
1961!--       At first, inquire all variable names.
1962!--       This will be used to check whether an optional input variable
1963!--       exist or not.
1964          CALL inquire_num_variables( id_surf, num_vars )
1965
1966          ALLOCATE( var_names(1:num_vars) )
1967          CALL inquire_variable_names( id_surf, var_names )
1968
1969!
1970!--       Read leaf area density - resolved vegetation
1971          IF ( check_existence( var_names, 'lad' ) )  THEN
1972             leaf_area_density_f%from_file = .TRUE.
1973             CALL get_attribute( id_surf, char_fill,                           &
1974                                 leaf_area_density_f%fill,                     &
1975                                 .FALSE., 'lad' )
1976!
1977!--          Inquire number of vertical vegetation layer
1978             CALL netcdf_data_input_get_dimension_length( id_surf,             &
1979                                                 leaf_area_density_f%nz,       &
1980                                                 'zlad' )
1981!
1982!--          Allocate variable for leaf-area density
1983             ALLOCATE( leaf_area_density_f%var( 0:leaf_area_density_f%nz-1,    &
1984                                                nys:nyn,nxl:nxr) )
1985
1986             CALL get_variable( id_surf, 'lad', leaf_area_density_f%var,       &
1987                                nxl, nxr, nys, nyn,                            &
1988                                0, leaf_area_density_f%nz-1 )
1989
1990          ELSE
1991             leaf_area_density_f%from_file = .FALSE.
1992          ENDIF
1993
1994!
1995!--       Read basal area density - resolved vegetation
1996          IF ( check_existence( var_names, 'bad' ) )  THEN
1997             basal_area_density_f%from_file = .TRUE.
1998             CALL get_attribute( id_surf, char_fill,                           &
1999                                 basal_area_density_f%fill,                    &
2000                                 .FALSE., 'bad' )
2001!
2002!--          Inquire number of vertical vegetation layer
2003             CALL netcdf_data_input_get_dimension_length( id_surf,             &
2004                                                 basal_area_density_f%nz,      &
2005                                                 'zlad' )
2006!
2007!--          Allocate variable
2008             ALLOCATE( basal_area_density_f%var(0:basal_area_density_f%nz-1,   &
2009                                                nys:nyn,nxl:nxr) )
2010
2011             CALL get_variable( id_surf, 'bad', basal_area_density_f%var,      &
2012                                nxl, nxr, nys, nyn,                            &
2013                                0,  basal_area_density_f%nz-1 )
2014          ELSE
2015             basal_area_density_f%from_file = .FALSE.
2016          ENDIF
2017
2018!
2019!--       Read root area density - resolved vegetation
2020          IF ( check_existence( var_names, 'root_area_dens_r' ) )  THEN
2021             root_area_density_lad_f%from_file = .TRUE.
2022             CALL get_attribute( id_surf, char_fill,                           &
2023                                 root_area_density_lad_f%fill,                 &
2024                                 .FALSE., 'root_area_dens_r' )
2025!
2026!--          Inquire number of vertical soil layers
2027             CALL netcdf_data_input_get_dimension_length( id_surf,             &
2028                                                   root_area_density_lad_f%nz, &
2029                                                  'zsoil' )
2030!
2031!--          Allocate variable
2032             ALLOCATE( root_area_density_lad_f%var                             &
2033                                         (0:root_area_density_lad_f%nz-1,      &
2034                                          nys:nyn,nxl:nxr) )
2035
2036             CALL get_variable( id_surf, 'root_area_dens_r',                   &
2037                                root_area_density_lad_f%var,                   &
2038                                nxl, nxr, nys, nyn,                            &
2039                                0,  root_area_density_lad_f%nz-1 )
2040          ELSE
2041             root_area_density_lad_f%from_file = .FALSE.
2042          ENDIF
2043!
2044!--       Finally, close input file
2045          CALL close_input_file( id_surf )
2046#endif
2047       ENDIF
2048!
2049!--    Deallocate variable list. Will be re-allocated in case further
2050!--    variables are read from file.
2051       IF ( ALLOCATED( var_names ) )  DEALLOCATE( var_names )
2052!
2053!--    Skip the following if no land-surface or urban-surface module are
2054!--    applied. This case, no one of the following variables is used anyway.
2055       IF (  .NOT. land_surface  .AND.  .NOT. urban_surface )  RETURN
2056
2057#if defined ( __netcdf )
2058!
2059!--    Open file in read-only mode
2060       CALL open_read_file( TRIM( input_file_static ) //                       &
2061                            TRIM( coupling_char ) , id_surf )
2062!
2063!--    Inquire all variable names.
2064!--    This will be used to check whether an optional input variable exist
2065!--    or not.
2066       CALL inquire_num_variables( id_surf, num_vars )
2067
2068       ALLOCATE( var_names(1:num_vars) )
2069       CALL inquire_variable_names( id_surf, var_names )
2070!
2071!--    Read vegetation type and required attributes
2072       IF ( check_existence( var_names, 'vegetation_type' ) )  THEN
2073          vegetation_type_f%from_file = .TRUE.
2074          CALL get_attribute( id_surf, char_fill,                              &
2075                              vegetation_type_f%fill,                          &
2076                              .FALSE., 'vegetation_type' )
2077
2078          ALLOCATE ( vegetation_type_f%var(nys:nyn,nxl:nxr)  )
2079
2080          CALL get_variable( id_surf, 'vegetation_type',                       &
2081                             vegetation_type_f%var, nxl, nxr, nys, nyn )
2082       ELSE
2083          vegetation_type_f%from_file = .FALSE.
2084       ENDIF
2085
2086!
2087!--    Read soil type and required attributes
2088       IF ( check_existence( var_names, 'soil_type' ) )  THEN
2089             soil_type_f%from_file = .TRUE.
2090!
2091!--       Note, lod is currently not on file; skip for the moment
2092!           CALL get_attribute( id_surf, char_lod,                       &
2093!                                      soil_type_f%lod,                  &
2094!                                      .FALSE., 'soil_type' )
2095          CALL get_attribute( id_surf, char_fill,                              &
2096                              soil_type_f%fill,                                &
2097                              .FALSE., 'soil_type' )
2098
2099          IF ( soil_type_f%lod == 1 )  THEN
2100
2101             ALLOCATE ( soil_type_f%var_2d(nys:nyn,nxl:nxr)  )
2102
2103             CALL get_variable( id_surf, 'soil_type', soil_type_f%var_2d,      &
2104                                nxl, nxr, nys, nyn )
2105
2106          ELSEIF ( soil_type_f%lod == 2 )  THEN
2107!
2108!--          Obtain number of soil layers from file.
2109             CALL netcdf_data_input_get_dimension_length( id_surf, nz_soil,    &
2110                                                          'zsoil' )
2111
2112             ALLOCATE ( soil_type_f%var_3d(0:nz_soil,nys:nyn,nxl:nxr) )
2113
2114             CALL get_variable( id_surf, 'soil_type', soil_type_f%var_3d,      &
2115                                nxl, nxr, nys, nyn, 0, nz_soil )
2116 
2117          ENDIF
2118       ELSE
2119          soil_type_f%from_file = .FALSE.
2120       ENDIF
2121
2122!
2123!--    Read pavement type and required attributes
2124       IF ( check_existence( var_names, 'pavement_type' ) )  THEN
2125          pavement_type_f%from_file = .TRUE.
2126          CALL get_attribute( id_surf, char_fill,                              &
2127                              pavement_type_f%fill, .FALSE.,                   &
2128                              'pavement_type' )
2129
2130          ALLOCATE ( pavement_type_f%var(nys:nyn,nxl:nxr)  )
2131
2132          CALL get_variable( id_surf, 'pavement_type', pavement_type_f%var,    &
2133                             nxl, nxr, nys, nyn )
2134       ELSE
2135          pavement_type_f%from_file = .FALSE.
2136       ENDIF
2137
2138!
2139!--    Read water type and required attributes
2140       IF ( check_existence( var_names, 'water_type' ) )  THEN
2141          water_type_f%from_file = .TRUE.
2142          CALL get_attribute( id_surf, char_fill, water_type_f%fill,           &
2143                              .FALSE., 'water_type' )
2144
2145          ALLOCATE ( water_type_f%var(nys:nyn,nxl:nxr)  )
2146
2147          CALL get_variable( id_surf, 'water_type', water_type_f%var,          &
2148                             nxl, nxr, nys, nyn )
2149
2150       ELSE
2151          water_type_f%from_file = .FALSE.
2152       ENDIF
2153!
2154!--    Read relative surface fractions of vegetation, pavement and water.
2155       IF ( check_existence( var_names, 'surface_fraction' ) )  THEN
2156          surface_fraction_f%from_file = .TRUE.
2157          CALL get_attribute( id_surf, char_fill,                              &
2158                              surface_fraction_f%fill,                         &
2159                              .FALSE., 'surface_fraction' )
2160!
2161!--       Inquire number of surface fractions
2162          CALL netcdf_data_input_get_dimension_length( id_surf,                &
2163                                                       surface_fraction_f%nf,  &
2164                                                       'nsurface_fraction' )
2165!
2166!--       Allocate dimension array and input array for surface fractions
2167          ALLOCATE( surface_fraction_f%nfracs(0:surface_fraction_f%nf-1) )
2168          ALLOCATE( surface_fraction_f%frac(0:surface_fraction_f%nf-1,         &
2169                                            nys:nyn,nxl:nxr) )
2170!
2171!--       Get dimension of surface fractions
2172          CALL get_variable( id_surf, 'nsurface_fraction',                     &
2173                             surface_fraction_f%nfracs )
2174!
2175!--       Read surface fractions
2176          CALL get_variable( id_surf, 'surface_fraction',                      &
2177                             surface_fraction_f%frac, nxl, nxr, nys, nyn,      &
2178                             0, surface_fraction_f%nf-1 )
2179       ELSE
2180          surface_fraction_f%from_file = .FALSE.
2181       ENDIF
2182!
2183!--    Read building parameters and related information
2184       IF ( check_existence( var_names, 'building_pars' ) )  THEN
2185          building_pars_f%from_file = .TRUE.
2186          CALL get_attribute( id_surf, char_fill,                              &
2187                              building_pars_f%fill,                            &
2188                              .FALSE., 'building_pars' )
2189!
2190!--       Inquire number of building parameters
2191          CALL netcdf_data_input_get_dimension_length( id_surf,                &
2192                                                       building_pars_f%np,     &
2193                                                       'nbuilding_pars' )
2194!
2195!--       Allocate dimension array and input array for building parameters
2196          ALLOCATE( building_pars_f%pars(0:building_pars_f%np-1) )
2197          ALLOCATE( building_pars_f%pars_xy(0:building_pars_f%np-1,            &
2198                                            nys:nyn,nxl:nxr) )
2199!
2200!--       Get dimension of building parameters
2201          CALL get_variable( id_surf, 'nbuilding_pars',                        &
2202                             building_pars_f%pars )
2203!
2204!--       Read building_pars
2205          CALL get_variable( id_surf, 'building_pars',                         &
2206                             building_pars_f%pars_xy, nxl, nxr, nys, nyn,      &
2207                             0, building_pars_f%np-1 )
2208       ELSE
2209          building_pars_f%from_file = .FALSE.
2210       ENDIF
2211
2212!
2213!--    Read albedo type and required attributes
2214       IF ( check_existence( var_names, 'albedo_type' ) )  THEN
2215          albedo_type_f%from_file = .TRUE.
2216          CALL get_attribute( id_surf, char_fill, albedo_type_f%fill,          &
2217                              .FALSE.,  'albedo_type' )
2218
2219          ALLOCATE ( albedo_type_f%var(nys:nyn,nxl:nxr)  )
2220         
2221          CALL get_variable( id_surf, 'albedo_type', albedo_type_f%var,        &
2222                             nxl, nxr, nys, nyn )
2223       ELSE
2224          albedo_type_f%from_file = .FALSE.
2225       ENDIF
2226!
2227!--    Read albedo parameters and related information
2228       IF ( check_existence( var_names, 'albedo_pars' ) )  THEN
2229          albedo_pars_f%from_file = .TRUE.
2230          CALL get_attribute( id_surf, char_fill, albedo_pars_f%fill,          &
2231                              .FALSE., 'albedo_pars' )
2232!
2233!--       Inquire number of albedo parameters
2234          CALL netcdf_data_input_get_dimension_length( id_surf,                &
2235                                                       albedo_pars_f%np,       &
2236                                                       'nalbedo_pars' )
2237!
2238!--       Allocate dimension array and input array for albedo parameters
2239          ALLOCATE( albedo_pars_f%pars(0:albedo_pars_f%np-1) )
2240          ALLOCATE( albedo_pars_f%pars_xy(0:albedo_pars_f%np-1,                &
2241                                          nys:nyn,nxl:nxr) )
2242!
2243!--       Get dimension of albedo parameters
2244          CALL get_variable( id_surf, 'nalbedo_pars', albedo_pars_f%pars )
2245
2246          CALL get_variable( id_surf, 'albedo_pars', albedo_pars_f%pars_xy,    &
2247                             nxl, nxr, nys, nyn,                               &
2248                             0, albedo_pars_f%np-1 )
2249       ELSE
2250          albedo_pars_f%from_file = .FALSE.
2251       ENDIF
2252
2253!
2254!--    Read pavement parameters and related information
2255       IF ( check_existence( var_names, 'pavement_pars' ) )  THEN
2256          pavement_pars_f%from_file = .TRUE.
2257          CALL get_attribute( id_surf, char_fill,                              &
2258                              pavement_pars_f%fill,                            &
2259                              .FALSE., 'pavement_pars' )
2260!
2261!--       Inquire number of pavement parameters
2262          CALL netcdf_data_input_get_dimension_length( id_surf,                &
2263                                                       pavement_pars_f%np,     &
2264                                                       'npavement_pars' )
2265!
2266!--       Allocate dimension array and input array for pavement parameters
2267          ALLOCATE( pavement_pars_f%pars(0:pavement_pars_f%np-1) )
2268          ALLOCATE( pavement_pars_f%pars_xy(0:pavement_pars_f%np-1,            &
2269                                            nys:nyn,nxl:nxr) )
2270!
2271!--       Get dimension of pavement parameters
2272          CALL get_variable( id_surf, 'npavement_pars', pavement_pars_f%pars )
2273
2274          CALL get_variable( id_surf, 'pavement_pars', pavement_pars_f%pars_xy,&
2275                             nxl, nxr, nys, nyn,                               &
2276                             0, pavement_pars_f%np-1 )
2277       ELSE
2278          pavement_pars_f%from_file = .FALSE.
2279       ENDIF
2280
2281!
2282!--    Read pavement subsurface parameters and related information
2283       IF ( check_existence( var_names, 'pavement_subsurface_pars' ) )         &
2284       THEN
2285          pavement_subsurface_pars_f%from_file = .TRUE.
2286          CALL get_attribute( id_surf, char_fill,                              &
2287                              pavement_subsurface_pars_f%fill,                 &
2288                              .FALSE., 'pavement_subsurface_pars' )
2289!
2290!--       Inquire number of parameters
2291          CALL netcdf_data_input_get_dimension_length( id_surf,                &
2292                                                pavement_subsurface_pars_f%np, &
2293                                               'npavement_subsurface_pars' )
2294!
2295!--       Inquire number of soil layers
2296          CALL netcdf_data_input_get_dimension_length( id_surf,                &
2297                                                pavement_subsurface_pars_f%nz, &
2298                                                'zsoil' )
2299!
2300!--       Allocate dimension array and input array for pavement parameters
2301          ALLOCATE( pavement_subsurface_pars_f%pars                            &
2302                            (0:pavement_subsurface_pars_f%np-1) )
2303          ALLOCATE( pavement_subsurface_pars_f%pars_xyz                        &
2304                            (0:pavement_subsurface_pars_f%np-1,                &
2305                             0:pavement_subsurface_pars_f%nz-1,                &
2306                             nys:nyn,nxl:nxr) )
2307!
2308!--       Get dimension of pavement parameters
2309          CALL get_variable( id_surf, 'npavement_subsurface_pars',             &
2310                             pavement_subsurface_pars_f%pars )
2311
2312          CALL get_variable( id_surf, 'pavement_subsurface_pars',              &
2313                             pavement_subsurface_pars_f%pars_xyz,              &
2314                             nxl, nxr, nys, nyn,                               &
2315                             0, pavement_subsurface_pars_f%nz-1,               &
2316                             0, pavement_subsurface_pars_f%np-1 )
2317       ELSE
2318          pavement_subsurface_pars_f%from_file = .FALSE.
2319       ENDIF
2320
2321
2322!
2323!--    Read vegetation parameters and related information
2324       IF ( check_existence( var_names, 'vegetation_pars' ) )  THEN
2325          vegetation_pars_f%from_file = .TRUE.
2326          CALL get_attribute( id_surf, char_fill,                              &
2327                              vegetation_pars_f%fill,                          &
2328                              .FALSE.,  'vegetation_pars' )
2329!
2330!--       Inquire number of vegetation parameters
2331          CALL netcdf_data_input_get_dimension_length( id_surf,                &
2332                                                       vegetation_pars_f%np,   &
2333                                                       'nvegetation_pars' )
2334!
2335!--       Allocate dimension array and input array for surface fractions
2336          ALLOCATE( vegetation_pars_f%pars(0:vegetation_pars_f%np-1) )
2337          ALLOCATE( vegetation_pars_f%pars_xy(0:vegetation_pars_f%np-1,        &
2338                                              nys:nyn,nxl:nxr) )
2339!
2340!--       Get dimension of the parameters
2341          CALL get_variable( id_surf, 'nvegetation_pars',                      &
2342                             vegetation_pars_f%pars )
2343
2344          CALL get_variable( id_surf, 'vegetation_pars',                       &
2345                             vegetation_pars_f%pars_xy, nxl, nxr, nys, nyn,    &
2346                             0, vegetation_pars_f%np-1 )
2347       ELSE
2348          vegetation_pars_f%from_file = .FALSE.
2349       ENDIF
2350
2351!
2352!--    Read root parameters/distribution and related information
2353       IF ( check_existence( var_names, 'soil_pars' ) )  THEN
2354          soil_pars_f%from_file = .TRUE.
2355          CALL get_attribute( id_surf, char_fill,                              &
2356                              soil_pars_f%fill,                                &
2357                              .FALSE., 'soil_pars' )
2358
2359          CALL get_attribute( id_surf, char_lod,                               &
2360                              soil_pars_f%lod,                                 &
2361                              .FALSE., 'soil_pars' )
2362
2363!
2364!--       Inquire number of soil parameters
2365          CALL netcdf_data_input_get_dimension_length( id_surf,                &
2366                                                       soil_pars_f%np,         &
2367                                                       'nsoil_pars' )
2368!
2369!--       Read parameters array
2370          ALLOCATE( soil_pars_f%pars(0:soil_pars_f%np-1) )
2371          CALL get_variable( id_surf, 'nsoil_pars', soil_pars_f%pars )
2372
2373!
2374!--       In case of level of detail 2, also inquire number of vertical
2375!--       soil layers, allocate memory and read the respective dimension
2376          IF ( soil_pars_f%lod == 2 )  THEN
2377             CALL netcdf_data_input_get_dimension_length( id_surf,             &
2378                                                          soil_pars_f%nz,      &
2379                                                          'zsoil' )
2380
2381             ALLOCATE( soil_pars_f%layers(0:soil_pars_f%nz-1) )
2382             CALL get_variable( id_surf, 'zsoil', soil_pars_f%layers )
2383
2384          ENDIF
2385
2386!
2387!--       Read soil parameters, depending on level of detail
2388          IF ( soil_pars_f%lod == 1 )  THEN
2389             ALLOCATE( soil_pars_f%pars_xy(0:soil_pars_f%np-1,                 &
2390                                           nys:nyn,nxl:nxr) )
2391                 
2392             CALL get_variable( id_surf, 'soil_pars', soil_pars_f%pars_xy,     &
2393                                nxl, nxr, nys, nyn, 0, soil_pars_f%np-1 )
2394
2395          ELSEIF ( soil_pars_f%lod == 2 )  THEN
2396             ALLOCATE( soil_pars_f%pars_xyz(0:soil_pars_f%np-1,                &
2397                                            0:soil_pars_f%nz-1,                &
2398                                            nys:nyn,nxl:nxr) )
2399             CALL get_variable( id_surf, 'soil_pars',                          &
2400                                soil_pars_f%pars_xyz,                          &
2401                                nxl, nxr, nys, nyn, 0, soil_pars_f%nz-1,       &
2402                                0, soil_pars_f%np-1 )
2403
2404          ENDIF
2405       ELSE
2406          soil_pars_f%from_file = .FALSE.
2407       ENDIF
2408
2409!
2410!--    Read water parameters and related information
2411       IF ( check_existence( var_names, 'water_pars' ) )  THEN
2412          water_pars_f%from_file = .TRUE.
2413          CALL get_attribute( id_surf, char_fill,                              &
2414                              water_pars_f%fill,                               &
2415                              .FALSE., 'water_pars' )
2416!
2417!--       Inquire number of water parameters
2418          CALL netcdf_data_input_get_dimension_length( id_surf,                &
2419                                                       water_pars_f%np,        &
2420                                                       'nwater_pars' )
2421!
2422!--       Allocate dimension array and input array for water parameters
2423          ALLOCATE( water_pars_f%pars(0:water_pars_f%np-1) )
2424          ALLOCATE( water_pars_f%pars_xy(0:water_pars_f%np-1,                  &
2425                                         nys:nyn,nxl:nxr) )
2426!
2427!--       Get dimension of water parameters
2428          CALL get_variable( id_surf, 'nwater_pars', water_pars_f%pars )
2429
2430          CALL get_variable( id_surf, 'water_pars', water_pars_f%pars_xy,      &
2431                             nxl, nxr, nys, nyn, 0, water_pars_f%np-1 )
2432       ELSE
2433          water_pars_f%from_file = .FALSE.
2434       ENDIF
2435!
2436!--    Read root area density - parametrized vegetation
2437       IF ( check_existence( var_names, 'root_area_dens_s' ) )  THEN
2438          root_area_density_lsm_f%from_file = .TRUE.
2439          CALL get_attribute( id_surf, char_fill,                              &
2440                              root_area_density_lsm_f%fill,                    &
2441                              .FALSE., 'root_area_dens_s' )
2442!
2443!--       Obtain number of soil layers from file and allocate variable
2444          CALL netcdf_data_input_get_dimension_length( id_surf,                &
2445                                                   root_area_density_lsm_f%nz, &
2446                                                   'zsoil' )
2447          ALLOCATE( root_area_density_lsm_f%var                                &
2448                                        (0:root_area_density_lsm_f%nz-1,       &
2449                                         nys:nyn,nxl:nxr) )
2450
2451!
2452!--       Read root-area density
2453          CALL get_variable( id_surf, 'root_area_dens_s',                      &
2454                             root_area_density_lsm_f%var,                      &
2455                             nxl, nxr, nys, nyn,                               &
2456                             0, root_area_density_lsm_f%nz-1 )
2457
2458       ELSE
2459          root_area_density_lsm_f%from_file = .FALSE.
2460       ENDIF
2461!
2462!--    Read street type and street crossing
2463       IF ( check_existence( var_names, 'street_type' ) )  THEN
2464          street_type_f%from_file = .TRUE.
2465          CALL get_attribute( id_surf, char_fill,                              &
2466                              street_type_f%fill, .FALSE.,                     &
2467                              'street_type' )
2468
2469          ALLOCATE ( street_type_f%var(nys:nyn,nxl:nxr)  )
2470         
2471          CALL get_variable( id_surf, 'street_type', street_type_f%var,        &
2472                             nxl, nxr, nys, nyn )
2473       ELSE
2474          street_type_f%from_file = .FALSE.
2475       ENDIF
2476
2477       IF ( check_existence( var_names, 'street_crossing' ) )  THEN
2478          street_crossing_f%from_file = .TRUE.
2479          CALL get_attribute( id_surf, char_fill,                              &
2480                              street_crossing_f%fill, .FALSE.,                 &
2481                              'street_crossing' )
2482
2483          ALLOCATE ( street_crossing_f%var(nys:nyn,nxl:nxr)  )
2484
2485          CALL get_variable( id_surf, 'street_crossing',                       &
2486                             street_crossing_f%var, nxl, nxr, nys, nyn )
2487
2488       ELSE
2489          street_crossing_f%from_file = .FALSE.
2490       ENDIF
2491!
2492!--    Still missing: root_resolved and building_surface_pars.
2493!--    Will be implemented as soon as they are available.
2494
2495!
2496!--    Finally, close input file
2497       CALL close_input_file( id_surf )
2498#endif
2499!
2500!--    End of CPU measurement
2501       CALL cpu_log( log_point_s(82), 'NetCDF input', 'stop' )
2502!
2503!--    Exchange ghost points for surface variables. Therefore, resize
2504!--    variables.
2505       IF ( albedo_type_f%from_file )  THEN
2506          CALL resize_array_2d_int8( albedo_type_f%var, nys, nyn, nxl, nxr )
2507          CALL exchange_horiz_2d_byte( albedo_type_f%var, nys, nyn, nxl, nxr,  &
2508                                       nbgp )
2509       ENDIF
2510       IF ( pavement_type_f%from_file )  THEN
2511          CALL resize_array_2d_int8( pavement_type_f%var, nys, nyn, nxl, nxr )
2512          CALL exchange_horiz_2d_byte( pavement_type_f%var, nys, nyn, nxl, nxr,&
2513                                       nbgp )
2514       ENDIF
2515       IF ( soil_type_f%from_file  .AND.  ALLOCATED( soil_type_f%var_2d ) )  THEN
2516          CALL resize_array_2d_int8( soil_type_f%var_2d, nys, nyn, nxl, nxr )
2517          CALL exchange_horiz_2d_byte( soil_type_f%var_2d, nys, nyn, nxl, nxr, &
2518                                       nbgp )
2519       ENDIF
2520       IF ( vegetation_type_f%from_file )  THEN
2521          CALL resize_array_2d_int8( vegetation_type_f%var, nys, nyn, nxl, nxr )
2522          CALL exchange_horiz_2d_byte( vegetation_type_f%var, nys, nyn, nxl,   &
2523                                       nxr, nbgp )
2524       ENDIF
2525       IF ( water_type_f%from_file )  THEN
2526          CALL resize_array_2d_int8( water_type_f%var, nys, nyn, nxl, nxr )
2527          CALL exchange_horiz_2d_byte( water_type_f%var, nys, nyn, nxl, nxr,   &
2528                                       nbgp )
2529       ENDIF
2530!
2531!--    Exchange ghost points for 3/4-D variables. For the sake of simplicity,
2532!--    loop further dimensions to use 2D exchange routines. Unfortunately this
2533!--    is necessary, else new MPI-data types need to be introduced just for
2534!--    2 variables.
2535       IF ( soil_type_f%from_file  .AND.  ALLOCATED( soil_type_f%var_3d ) )    &
2536       THEN
2537          CALL resize_array_3d_int8( soil_type_f%var_3d, 0, nz_soil,           &
2538                                     nys, nyn, nxl, nxr )
2539          DO  k = 0, nz_soil
2540             CALL exchange_horiz_2d_int(                                       & 
2541                        soil_type_f%var_3d(k,:,:), nys, nyn, nxl, nxr, nbgp )
2542          ENDDO
2543       ENDIF
2544
2545       IF ( surface_fraction_f%from_file )  THEN
2546          CALL resize_array_3d_real( surface_fraction_f%frac,                  &
2547                                     0, surface_fraction_f%nf-1,               &
2548                                     nys, nyn, nxl, nxr )
2549          DO  k = 0, surface_fraction_f%nf-1
2550             CALL exchange_horiz_2d( surface_fraction_f%frac(k,:,:), nbgp )
2551          ENDDO
2552       ENDIF
2553
2554       IF ( building_pars_f%from_file )  THEN         
2555          CALL resize_array_3d_real( building_pars_f%pars_xy,                  &
2556                                     0, building_pars_f%np-1,                  &
2557                                     nys, nyn, nxl, nxr )
2558          DO  k = 0, building_pars_f%np-1
2559             CALL exchange_horiz_2d( building_pars_f%pars_xy(k,:,:), nbgp )
2560          ENDDO
2561       ENDIF
2562
2563       IF ( albedo_pars_f%from_file )  THEN         
2564          CALL resize_array_3d_real( albedo_pars_f%pars_xy,                    &
2565                                     0, albedo_pars_f%np-1,                    &
2566                                     nys, nyn, nxl, nxr )
2567          DO  k = 0, albedo_pars_f%np-1
2568             CALL exchange_horiz_2d( albedo_pars_f%pars_xy(k,:,:), nbgp )
2569          ENDDO
2570       ENDIF
2571
2572       IF ( pavement_pars_f%from_file )  THEN         
2573          CALL resize_array_3d_real( pavement_pars_f%pars_xy,                  &
2574                                     0, pavement_pars_f%np-1,                  &
2575                                     nys, nyn, nxl, nxr )
2576          DO  k = 0, pavement_pars_f%np-1
2577             CALL exchange_horiz_2d( pavement_pars_f%pars_xy(k,:,:), nbgp )
2578          ENDDO
2579       ENDIF
2580
2581       IF ( vegetation_pars_f%from_file )  THEN
2582          CALL resize_array_3d_real( vegetation_pars_f%pars_xy,                &
2583                                     0, vegetation_pars_f%np-1,                &
2584                                     nys, nyn, nxl, nxr )
2585          DO  k = 0, vegetation_pars_f%np-1
2586             CALL exchange_horiz_2d( vegetation_pars_f%pars_xy(k,:,:), nbgp )
2587          ENDDO
2588       ENDIF
2589
2590       IF ( water_pars_f%from_file )  THEN
2591          CALL resize_array_3d_real( water_pars_f%pars_xy,                     &
2592                                     0, water_pars_f%np-1,                     &
2593                                     nys, nyn, nxl, nxr )
2594          DO  k = 0, water_pars_f%np-1
2595             CALL exchange_horiz_2d( water_pars_f%pars_xy(k,:,:), nbgp )
2596          ENDDO
2597       ENDIF
2598
2599       IF ( root_area_density_lsm_f%from_file )  THEN
2600          CALL resize_array_3d_real( root_area_density_lsm_f%var,              &
2601                                     0, root_area_density_lsm_f%nz-1,          &
2602                                     nys, nyn, nxl, nxr )
2603          DO  k = 0, root_area_density_lsm_f%nz-1
2604             CALL exchange_horiz_2d( root_area_density_lsm_f%var(k,:,:), nbgp )
2605          ENDDO
2606       ENDIF
2607
2608       IF ( soil_pars_f%from_file )  THEN
2609          IF ( soil_pars_f%lod == 1 )  THEN
2610         
2611             CALL resize_array_3d_real( soil_pars_f%pars_xy,                   &
2612                                        0, soil_pars_f%np-1,                   &
2613                                        nys, nyn, nxl, nxr )
2614             DO  k = 0, soil_pars_f%np-1
2615                CALL exchange_horiz_2d( soil_pars_f%pars_xy(k,:,:), nbgp )
2616             ENDDO
2617             
2618          ELSEIF ( soil_pars_f%lod == 2 )  THEN
2619             CALL resize_array_4d_real( soil_pars_f%pars_xyz,                  &
2620                                        0, soil_pars_f%np-1,                   &
2621                                        0, soil_pars_f%nz-1,                   &
2622                                        nys, nyn, nxl, nxr )
2623
2624             DO  k2 = 0, soil_pars_f%nz-1
2625                DO  k = 0, soil_pars_f%np-1
2626                   CALL exchange_horiz_2d( soil_pars_f%pars_xyz(k,k2,:,:),     &
2627                                           nbgp )
2628                ENDDO
2629             ENDDO
2630          ENDIF
2631       ENDIF
2632
2633       IF ( pavement_subsurface_pars_f%from_file )  THEN         
2634          CALL resize_array_4d_real( pavement_subsurface_pars_f%pars_xyz,      &
2635                                     0, pavement_subsurface_pars_f%np-1,       &
2636                                     0, pavement_subsurface_pars_f%nz-1,       &
2637                                     nys, nyn, nxl, nxr )
2638
2639          DO  k2 = 0, pavement_subsurface_pars_f%nz-1
2640             DO  k = 0, pavement_subsurface_pars_f%np-1
2641                CALL exchange_horiz_2d(                                        &
2642                           pavement_subsurface_pars_f%pars_xyz(k,k2,:,:), nbgp )
2643             ENDDO
2644          ENDDO
2645       ENDIF
2646
2647    END SUBROUTINE netcdf_data_input_surface_data
2648
2649!------------------------------------------------------------------------------!
2650! Description:
2651! ------------
2652!> Reads uvem lookup table information.
2653!------------------------------------------------------------------------------!
2654    SUBROUTINE netcdf_data_input_uvem
2655       
2656       USE indices,                                                            &
2657           ONLY:  nxl, nxr, nyn, nys
2658
2659       IMPLICIT NONE
2660
2661       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names  !< variable names in static input file
2662
2663
2664       INTEGER(iwp) ::  id_uvem       !< NetCDF id of uvem lookup table input file
2665       INTEGER(iwp) ::  nli = 35      !< dimension length of lookup table in x
2666       INTEGER(iwp) ::  nlj =  9      !< dimension length of lookup table in y
2667       INTEGER(iwp) ::  nlk = 90      !< dimension length of lookup table in z
2668       INTEGER(iwp) ::  num_vars      !< number of variables in netcdf input file
2669!
2670!--    Input via uv exposure model lookup table input
2671       IF ( input_pids_uvem )  THEN
2672
2673#if defined ( __netcdf )
2674!
2675!--       Open file in read-only mode
2676          CALL open_read_file( TRIM( input_file_uvem ) //                    &
2677                               TRIM( coupling_char ), id_uvem )
2678!
2679!--       At first, inquire all variable names.
2680!--       This will be used to check whether an input variable exist or not.
2681          CALL inquire_num_variables( id_uvem, num_vars )
2682!
2683!--       Allocate memory to store variable names and inquire them.
2684          ALLOCATE( var_names(1:num_vars) )
2685          CALL inquire_variable_names( id_uvem, var_names )
2686!
2687!--       uvem integration
2688          IF ( check_existence( var_names, 'int_factors' ) )  THEN
2689             uvem_integration_f%from_file = .TRUE.
2690!
2691!--          Input 2D uvem integration.
2692             ALLOCATE ( uvem_integration_f%var(0:nlj,0:nli)  )
2693             
2694             CALL get_variable( id_uvem, 'int_factors', uvem_integration_f%var, 0, nli, 0, nlj )
2695          ELSE
2696             uvem_integration_f%from_file = .FALSE.
2697          ENDIF
2698!
2699!--       uvem irradiance
2700          IF ( check_existence( var_names, 'irradiance' ) )  THEN
2701             uvem_irradiance_f%from_file = .TRUE.
2702!
2703!--          Input 2D uvem irradiance.
2704             ALLOCATE ( uvem_irradiance_f%var(0:nlk, 0:2)  )
2705             
2706             CALL get_variable( id_uvem, 'irradiance', uvem_irradiance_f%var, 0, 2, 0, nlk )
2707          ELSE
2708             uvem_irradiance_f%from_file = .FALSE.
2709          ENDIF
2710!
2711!--       uvem porjection areas
2712          IF ( check_existence( var_names, 'projarea' ) )  THEN
2713             uvem_projarea_f%from_file = .TRUE.
2714!
2715!--          Input 3D uvem projection area (human geometgry)
2716             ALLOCATE ( uvem_projarea_f%var(0:2,0:nlj,0:nli)  )
2717           
2718             CALL get_variable( id_uvem, 'projarea', uvem_projarea_f%var, 0, nli, 0, nlj, 0, 2 )
2719          ELSE
2720             uvem_projarea_f%from_file = .FALSE.
2721          ENDIF
2722!
2723!--       uvem radiance
2724          IF ( check_existence( var_names, 'radiance' ) )  THEN
2725             uvem_radiance_f%from_file = .TRUE.
2726!
2727!--          Input 3D uvem radiance
2728             ALLOCATE ( uvem_radiance_f%var(0:nlk,0:nlj,0:nli)  )
2729             
2730             CALL get_variable( id_uvem, 'radiance', uvem_radiance_f%var, 0, nli, 0, nlj, 0, nlk )
2731          ELSE
2732             uvem_radiance_f%from_file = .FALSE.
2733          ENDIF
2734!
2735!--       Read building obstruction
2736          IF ( check_existence( var_names, 'obstruction' ) )  THEN
2737             building_obstruction_full%from_file = .TRUE.
2738!--          Input 3D uvem building obstruction
2739              ALLOCATE ( building_obstruction_full%var_3d(0:44,0:2,0:2) )
2740              CALL get_variable( id_uvem, 'obstruction', building_obstruction_full%var_3d,0, 2, 0, 2, 0, 44 )       
2741          ELSE
2742             building_obstruction_full%from_file = .FALSE.
2743          ENDIF
2744!
2745          IF ( check_existence( var_names, 'obstruction' ) )  THEN
2746             building_obstruction_f%from_file = .TRUE.
2747!
2748!--          Input 3D uvem building obstruction
2749             ALLOCATE ( building_obstruction_f%var_3d(0:44,nys:nyn,nxl:nxr) )
2750!
2751             CALL get_variable( id_uvem, 'obstruction', building_obstruction_f%var_3d,      &
2752                                nxl, nxr, nys, nyn, 0, 44 )       
2753          ELSE
2754             building_obstruction_f%from_file = .FALSE.
2755          ENDIF
2756!
2757!--       Close uvem lookup table input file
2758          CALL close_input_file( id_uvem )
2759#else
2760          CONTINUE
2761#endif
2762       ENDIF
2763    END SUBROUTINE netcdf_data_input_uvem
2764
2765!------------------------------------------------------------------------------!
2766! Description:
2767! ------------
2768!> Reads orography and building information.
2769!------------------------------------------------------------------------------!
2770    SUBROUTINE netcdf_data_input_topo
2771
2772       USE control_parameters,                                                 &
2773           ONLY:  message_string, topography
2774
2775       USE grid_variables,                                                     &
2776           ONLY:  dx, dy   
2777           
2778       USE indices,                                                            &
2779           ONLY:  nbgp, nx, nxl, nxr, ny, nyn, nys, nzb
2780
2781
2782       IMPLICIT NONE
2783
2784       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names  !< variable names in static input file
2785
2786
2787       INTEGER(iwp) ::  i             !< running index along x-direction
2788       INTEGER(iwp) ::  ii            !< running index for IO blocks
2789       INTEGER(iwp) ::  id_topo       !< NetCDF id of topograhy input file
2790       INTEGER(iwp) ::  j             !< running index along y-direction
2791       INTEGER(iwp) ::  num_vars      !< number of variables in netcdf input file
2792       INTEGER(iwp) ::  skip_n_rows   !< counting variable to skip rows while reading topography file
2793
2794       REAL(wp) ::  dum           !< dummy variable to skip columns while reading topography file
2795!
2796!--    CPU measurement
2797       CALL cpu_log( log_point_s(83), 'NetCDF/ASCII input topo', 'start' )
2798
2799!
2800!--    Input via palm-input data standard
2801       IF ( input_pids_static )  THEN
2802#if defined ( __netcdf )
2803!
2804!--       Open file in read-only mode
2805          CALL open_read_file( TRIM( input_file_static ) //                    &
2806                               TRIM( coupling_char ), id_topo )
2807!
2808!--       At first, inquire all variable names.
2809!--       This will be used to check whether an  input variable exist
2810!--       or not.
2811          CALL inquire_num_variables( id_topo, num_vars )
2812!
2813!--       Allocate memory to store variable names and inquire them.
2814          ALLOCATE( var_names(1:num_vars) )
2815          CALL inquire_variable_names( id_topo, var_names )
2816!
2817!--       Read x, y - dimensions. Only required for consistency checks.
2818          CALL netcdf_data_input_get_dimension_length( id_topo, dim_static%nx, 'x' )
2819          CALL netcdf_data_input_get_dimension_length( id_topo, dim_static%ny, 'y' )
2820          ALLOCATE( dim_static%x(0:dim_static%nx-1) )
2821          ALLOCATE( dim_static%y(0:dim_static%ny-1) )
2822          CALL get_variable( id_topo, 'x', dim_static%x )
2823          CALL get_variable( id_topo, 'y', dim_static%y )
2824!
2825!--       Check whether dimension size in input file matches the model dimensions
2826          IF ( dim_static%nx-1 /= nx  .OR.  dim_static%ny-1 /= ny )  THEN
2827             message_string = 'Static input file: horizontal dimension in ' // &
2828                              'x- and/or y-direction ' //                      &
2829                              'do not match the respective model dimension'
2830             CALL message( 'netcdf_data_input_mod', 'PA0548', 1, 2, 0, 6, 0 )
2831          ENDIF
2832!
2833!--       Check if grid spacing of provided input data matches the respective
2834!--       grid spacing in the model.
2835          IF ( ABS( dim_static%x(1) - dim_static%x(0) - dx ) > 10E-6_wp  .OR.  &
2836               ABS( dim_static%y(1) - dim_static%y(0) - dy ) > 10E-6_wp )  THEN
2837             message_string = 'Static input file: horizontal grid spacing ' // &
2838                              'in x- and/or y-direction ' //                   &
2839                              'do not match the respective model grid spacing.'
2840             CALL message( 'netcdf_data_input_mod', 'PA0549', 1, 2, 0, 6, 0 )
2841          ENDIF
2842!
2843!--       Terrain height. First, get variable-related _FillValue attribute
2844          IF ( check_existence( var_names, 'zt' ) )  THEN
2845             terrain_height_f%from_file = .TRUE.
2846             CALL get_attribute( id_topo, char_fill, terrain_height_f%fill,    &
2847                                 .FALSE., 'zt' )
2848!
2849!--          Input 2D terrain height.
2850             ALLOCATE ( terrain_height_f%var(nys:nyn,nxl:nxr)  )
2851             
2852             CALL get_variable( id_topo, 'zt', terrain_height_f%var,           &
2853                                nxl, nxr, nys, nyn )
2854
2855          ELSE
2856             terrain_height_f%from_file = .FALSE.
2857          ENDIF
2858
2859!
2860!--       Read building height. First, read its _FillValue attribute,
2861!--       as well as lod attribute
2862          buildings_f%from_file = .FALSE.
2863          IF ( check_existence( var_names, 'buildings_2d' ) )  THEN
2864             buildings_f%from_file = .TRUE.
2865             CALL get_attribute( id_topo, char_lod, buildings_f%lod,           &
2866                                 .FALSE., 'buildings_2d' )
2867
2868             CALL get_attribute( id_topo, char_fill, buildings_f%fill1,        &
2869                                 .FALSE., 'buildings_2d' )
2870
2871!
2872!--          Read 2D buildings
2873             IF ( buildings_f%lod == 1 )  THEN
2874                ALLOCATE ( buildings_f%var_2d(nys:nyn,nxl:nxr) )
2875
2876                CALL get_variable( id_topo, 'buildings_2d',                    &
2877                                   buildings_f%var_2d,                         &
2878                                   nxl, nxr, nys, nyn )
2879             ELSE
2880                message_string = 'NetCDF attribute lod ' //                    &
2881                                 '(level of detail) is not set ' //            &
2882                                 'properly for buildings_2d.'
2883                CALL message( 'netcdf_data_input_mod', 'PA0540',               &
2884                               1, 2, 0, 6, 0 )
2885             ENDIF
2886          ENDIF
2887!
2888!--       If available, also read 3D building information. If both are
2889!--       available, use 3D information.
2890          IF ( check_existence( var_names, 'buildings_3d' ) )  THEN
2891             buildings_f%from_file = .TRUE.
2892             CALL get_attribute( id_topo, char_lod, buildings_f%lod,           &
2893                                 .FALSE., 'buildings_3d' )     
2894
2895             CALL get_attribute( id_topo, char_fill, buildings_f%fill2,        &
2896                                 .FALSE., 'buildings_3d' )
2897
2898             CALL netcdf_data_input_get_dimension_length( id_topo,             &
2899                                                          buildings_f%nz, 'z' )
2900!
2901!--          Read 3D buildings
2902             IF ( buildings_f%lod == 2 )  THEN
2903                ALLOCATE( buildings_f%z(nzb:buildings_f%nz-1) )
2904                CALL get_variable( id_topo, 'z', buildings_f%z )
2905
2906                ALLOCATE( buildings_f%var_3d(nzb:buildings_f%nz-1,             &
2907                                             nys:nyn,nxl:nxr) )
2908                buildings_f%var_3d = 0
2909               
2910                CALL get_variable( id_topo, 'buildings_3d',                    &
2911                                   buildings_f%var_3d,                         &
2912                                   nxl, nxr, nys, nyn, 0, buildings_f%nz-1 )
2913             ELSE
2914                message_string = 'NetCDF attribute lod ' //                    &
2915                                 '(level of detail) is not set ' //            &
2916                                 'properly for buildings_3d.'
2917                CALL message( 'netcdf_data_input_mod', 'PA0541',               &
2918                               1, 2, 0, 6, 0 )
2919             ENDIF
2920          ENDIF
2921!
2922!--       Read building IDs and its FillValue attribute. Further required
2923!--       for mapping buildings on top of orography.
2924          IF ( check_existence( var_names, 'building_id' ) )  THEN
2925             building_id_f%from_file = .TRUE.
2926             CALL get_attribute( id_topo, char_fill,                           &
2927                                 building_id_f%fill, .FALSE.,                  &
2928                                 'building_id' )
2929
2930             ALLOCATE ( building_id_f%var(nys:nyn,nxl:nxr) )
2931             
2932             CALL get_variable( id_topo, 'building_id', building_id_f%var,     &
2933                                nxl, nxr, nys, nyn )
2934          ELSE
2935             building_id_f%from_file = .FALSE.
2936          ENDIF
2937!
2938!--       Read building_type and required attributes.
2939          IF ( check_existence( var_names, 'building_type' ) )  THEN
2940             building_type_f%from_file = .TRUE.
2941             CALL get_attribute( id_topo, char_fill,                           &
2942                                 building_type_f%fill, .FALSE.,                &
2943                                 'building_type' )
2944
2945             ALLOCATE ( building_type_f%var(nys:nyn,nxl:nxr) )
2946
2947             CALL get_variable( id_topo, 'building_type', building_type_f%var, &
2948                                nxl, nxr, nys, nyn )
2949
2950          ELSE
2951             building_type_f%from_file = .FALSE.
2952          ENDIF
2953!
2954!--       Close topography input file
2955          CALL close_input_file( id_topo )
2956#else
2957          CONTINUE
2958#endif
2959!
2960!--    ASCII input
2961       ELSEIF ( TRIM( topography ) == 'read_from_file' )  THEN
2962             
2963          DO  ii = 0, io_blocks-1
2964             IF ( ii == io_group )  THEN
2965
2966                OPEN( 90, FILE='TOPOGRAPHY_DATA'//TRIM( coupling_char ),       &
2967                      STATUS='OLD', FORM='FORMATTED', ERR=10 )
2968!
2969!--             Read topography PE-wise. Rows are read from nyn to nys, columns
2970!--             are read from nxl to nxr. At first, ny-nyn rows need to be skipped.
2971                skip_n_rows = 0
2972                DO WHILE ( skip_n_rows < ny - nyn )
2973                   READ( 90, * )
2974                   skip_n_rows = skip_n_rows + 1
2975                ENDDO
2976!
2977!--             Read data from nyn to nys and nxl to nxr. Therefore, skip
2978!--             column until nxl-1 is reached
2979                ALLOCATE ( buildings_f%var_2d(nys:nyn,nxl:nxr) )
2980                DO  j = nyn, nys, -1
2981                   READ( 90, *, ERR=11, END=11 )                               &
2982                                   ( dum, i = 0, nxl-1 ),                      &
2983                                   ( buildings_f%var_2d(j,i), i = nxl, nxr )
2984                ENDDO
2985
2986                GOTO 12
2987
2988 10             message_string = 'file TOPOGRAPHY_DATA'//                      &
2989                                 TRIM( coupling_char )// ' does not exist'
2990                CALL message( 'netcdf_data_input_mod', 'PA0208', 1, 2, 0, 6, 0 )
2991
2992 11             message_string = 'errors in file TOPOGRAPHY_DATA'//            &
2993                                 TRIM( coupling_char )
2994                CALL message( 'netcdf_data_input_mod', 'PA0209', 2, 2, 0, 6, 0 )
2995
2996 12             CLOSE( 90 )
2997                buildings_f%from_file = .TRUE.
2998
2999             ENDIF
3000#if defined( __parallel )
3001             CALL MPI_BARRIER( comm2d, ierr )
3002#endif
3003          ENDDO
3004
3005       ENDIF
3006!
3007!--    End of CPU measurement
3008       CALL cpu_log( log_point_s(83), 'NetCDF/ASCII input topo', 'stop' )
3009!
3010!--    Check for minimum requirement to setup building topography. If buildings
3011!--    are provided, also an ID and a type are required.
3012!--    Note, doing this check in check_parameters
3013!--    will be too late (data will be used for grid inititialization before).
3014       IF ( input_pids_static )  THEN
3015          IF ( buildings_f%from_file  .AND.                                    &
3016               .NOT. building_id_f%from_file )  THEN
3017             message_string = 'If building heights are prescribed in ' //      &
3018                              'static input file, also an ID is required.'
3019             CALL message( 'netcdf_data_input_mod', 'PA0542', 1, 2, 0, 6, 0 )
3020          ENDIF
3021       ENDIF
3022!
3023!--    In case no terrain height is provided by static input file, allocate
3024!--    array nevertheless and set terrain height to 0, which simplifies
3025!--    topography initialization.
3026       IF ( .NOT. terrain_height_f%from_file )  THEN
3027          ALLOCATE ( terrain_height_f%var(nys:nyn,nxl:nxr) )
3028          terrain_height_f%var = 0.0_wp
3029       ENDIF
3030!
3031!--    Finally, exchange 1 ghost point for building ID and type.
3032!--    In case of non-cyclic boundary conditions set Neumann conditions at the
3033!--    lateral boundaries.
3034       IF ( building_id_f%from_file )  THEN
3035          CALL resize_array_2d_int32( building_id_f%var, nys, nyn, nxl, nxr )
3036          CALL exchange_horiz_2d_int( building_id_f%var, nys, nyn, nxl, nxr,   &
3037                                      nbgp )
3038       ENDIF
3039
3040       IF ( building_type_f%from_file )  THEN
3041          CALL resize_array_2d_int8( building_type_f%var, nys, nyn, nxl, nxr )
3042          CALL exchange_horiz_2d_byte( building_type_f%var, nys, nyn, nxl, nxr,   &
3043                                       nbgp )
3044       ENDIF
3045
3046    END SUBROUTINE netcdf_data_input_topo
3047
3048!------------------------------------------------------------------------------!
3049! Description:
3050! ------------
3051!> Reads initialization data of u, v, w, pt, q, geostrophic wind components,
3052!> as well as soil moisture and soil temperature, derived from larger-scale
3053!> model (COSMO) by Inifor.
3054!------------------------------------------------------------------------------!
3055    SUBROUTINE netcdf_data_input_init_3d
3056
3057       USE arrays_3d,                                                          &
3058           ONLY:  q, pt, u, v, w, zu, zw
3059
3060       USE control_parameters,                                                 &
3061           ONLY:  air_chemistry, bc_lr_cyc, bc_ns_cyc, humidity,               &
3062                  message_string, neutral
3063
3064       USE indices,                                                            &
3065           ONLY:  nx, nxl, nxlu, nxr, ny, nyn, nys, nysv, nzb, nz, nzt
3066
3067       IMPLICIT NONE
3068
3069       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names
3070
3071       LOGICAL      ::  dynamic_3d = .TRUE. !< flag indicating that 3D data is read from dynamic file
3072       
3073       INTEGER(iwp) ::  id_dynamic !< NetCDF id of dynamic input file
3074       INTEGER(iwp) ::  n          !< running index for chemistry variables
3075       INTEGER(iwp) ::  num_vars   !< number of variables in netcdf input file
3076
3077       LOGICAL      ::  check_passed !< flag indicating if a check passed
3078
3079!
3080!--    Skip routine if no input file with dynamic input data is available.
3081       IF ( .NOT. input_pids_dynamic )  RETURN
3082!
3083!--    Please note, Inifor is designed to provide initial data for u and v for
3084!--    the prognostic grid points in case of lateral Dirichlet conditions.
3085!--    This means that Inifor provides data from nxlu:nxr (for u) and
3086!--    from nysv:nyn (for v) at the left and south domain boundary, respectively.
3087!--    However, as work-around for the moment, PALM will run with cyclic
3088!--    conditions and will be initialized with data provided by Inifor
3089!--    boundaries in case of Dirichlet.
3090!--    Hence, simply set set nxlu/nysv to 1 (will be reset to its original value
3091!--    at the end of this routine.
3092       IF ( bc_lr_cyc  .AND.  nxl == 0 )  nxlu = 1
3093       IF ( bc_ns_cyc  .AND.  nys == 0 )  nysv = 1
3094
3095!
3096!--    CPU measurement
3097       CALL cpu_log( log_point_s(85), 'NetCDF input init', 'start' )
3098
3099#if defined ( __netcdf )
3100!
3101!--    Open file in read-only mode
3102       CALL open_read_file( TRIM( input_file_dynamic ) //                      &
3103                            TRIM( coupling_char ), id_dynamic )
3104
3105!
3106!--    At first, inquire all variable names.
3107       CALL inquire_num_variables( id_dynamic, num_vars )
3108!
3109!--    Allocate memory to store variable names.
3110       ALLOCATE( var_names(1:num_vars) )
3111       CALL inquire_variable_names( id_dynamic, var_names )
3112!
3113!--    Read vertical dimension of scalar und w grid.
3114       CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%nzu, 'z'     )
3115       CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%nzw, 'zw'    )
3116!
3117!--    Read also the horizontal dimensions. These are used just used fo
3118!--    checking the compatibility with the PALM grid before reading.
3119       CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%nx,  'x'  )
3120       CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%nxu, 'xu' )
3121       CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%ny,  'y'  )
3122       CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%nyv, 'yv' )
3123
3124!
3125!--    Check for correct horizontal and vertical dimension. Please note,
3126!--    checks are performed directly here and not called from
3127!--    check_parameters as some varialbes are still not allocated there.
3128!--    Moreover, please note, u- and v-grid has 1 grid point less on
3129!--    Inifor grid.
3130       IF ( init_3d%nx-1 /= nx  .OR.  init_3d%nxu-1 /= nx - 1  .OR.            &
3131            init_3d%ny-1 /= ny  .OR.  init_3d%nyv-1 /= ny - 1 )  THEN
3132          message_string = 'Number of inifor horizontal grid points  '//       &
3133                           'does not match the number of numeric grid '//      &
3134                           'points.'
3135          CALL message( 'netcdf_data_input_mod', 'PA0543', 1, 2, 0, 6, 0 )
3136       ENDIF
3137
3138       IF ( init_3d%nzu /= nz )  THEN
3139          message_string = 'Number of inifor vertical grid points ' //         &
3140                           'does not match the number of numeric grid '//      &
3141                           'points.'
3142          CALL message( 'netcdf_data_input_mod', 'PA0543', 1, 2, 0, 6, 0 )
3143       ENDIF
3144!
3145!--    Read vertical dimensions. Later, these are required for eventual
3146!--    inter- and extrapolations of the initialization data.
3147       IF ( check_existence( var_names, 'z' ) )  THEN
3148          ALLOCATE( init_3d%zu_atmos(1:init_3d%nzu) )
3149          CALL get_variable( id_dynamic, 'z', init_3d%zu_atmos )
3150       ENDIF
3151       IF ( check_existence( var_names, 'zw' ) )  THEN
3152          ALLOCATE( init_3d%zw_atmos(1:init_3d%nzw) )
3153          CALL get_variable( id_dynamic, 'zw', init_3d%zw_atmos )
3154       ENDIF
3155!
3156!--    Check for consistency between vertical coordinates in dynamic
3157!--    driver and numeric grid.
3158!--    Please note, depending on compiler options both may be
3159!--    equal up to a certain threshold, and differences between
3160!--    the numeric grid and vertical coordinate in the driver can built-
3161!--    up to 10E-1-10E-0 m. For this reason, the check is performed not
3162!--    for exactly matching values.
3163       IF ( ANY( ABS( zu(1:nzt)   - init_3d%zu_atmos(1:init_3d%nzu) )    &
3164                      > 10E-1 )  .OR.                                    &
3165            ANY( ABS( zw(1:nzt-1) - init_3d%zw_atmos(1:init_3d%nzw) )    &
3166                      > 10E-1 ) )  THEN
3167          message_string = 'Vertical grid in dynamic driver does not '// &
3168                           'match the numeric grid.'
3169          CALL message( 'netcdf_data_input_mod', 'PA0543', 1, 2, 0, 6, 0 )
3170       ENDIF
3171!
3172!--    Read initial geostrophic wind components at
3173!--    t = 0 (index 1 in file).
3174       IF ( check_existence( var_names, 'ls_forcing_ug' ) )  THEN
3175          ALLOCATE( init_3d%ug_init(nzb:nzt+1) )
3176          init_3d%ug_init = 0.0_wp
3177
3178          CALL get_variable_pr( id_dynamic, 'ls_forcing_ug', 1,          &
3179                                init_3d%ug_init(1:nzt) )
3180!
3181!--       Set top-boundary condition (Neumann)
3182          init_3d%ug_init(nzt+1) = init_3d%ug_init(nzt)
3183
3184          init_3d%from_file_ug = .TRUE.
3185       ELSE
3186          init_3d%from_file_ug = .FALSE.
3187       ENDIF
3188       IF ( check_existence( var_names, 'ls_forcing_vg' ) )  THEN
3189          ALLOCATE( init_3d%vg_init(nzb:nzt+1) )
3190          init_3d%vg_init = 0.0_wp
3191
3192          CALL get_variable_pr( id_dynamic, 'ls_forcing_vg', 1,          &
3193                                init_3d%vg_init(1:nzt) )
3194!
3195!--       Set top-boundary condition (Neumann)
3196          init_3d%vg_init(nzt+1) = init_3d%vg_init(nzt)
3197
3198          init_3d%from_file_vg = .TRUE.
3199       ELSE
3200          init_3d%from_file_vg = .FALSE.
3201       ENDIF
3202!
3203!--    Read inital 3D data of u, v, w, pt and q,
3204!--    derived from COSMO model. Read PE-wise yz-slices.
3205!--    Please note, the u-, v- and w-component are defined on different
3206!--    grids with one element less in the x-, y-,
3207!--    and z-direction, respectively. Hence, reading is subdivided
3208!--    into separate loops. 
3209!--    Read u-component
3210       IF ( check_existence( var_names, 'init_atmosphere_u' ) )  THEN
3211!
3212!--       Read attributes for the fill value and level-of-detail
3213          CALL get_attribute( id_dynamic, char_fill, init_3d%fill_u,           &
3214                              .FALSE., 'init_atmosphere_u' )
3215          CALL get_attribute( id_dynamic, char_lod, init_3d%lod_u,             &
3216                              .FALSE., 'init_atmosphere_u' )
3217!
3218!--       level-of-detail 1 - read initialization profile
3219          IF ( init_3d%lod_u == 1 )  THEN
3220             ALLOCATE( init_3d%u_init(nzb:nzt+1) )
3221             init_3d%u_init = 0.0_wp
3222
3223             CALL get_variable( id_dynamic, 'init_atmosphere_u',               &
3224                                init_3d%u_init(nzb+1:nzt) )
3225!
3226!--          Set top-boundary condition (Neumann)
3227             init_3d%u_init(nzt+1) = init_3d%u_init(nzt)
3228!
3229!--       level-of-detail 2 - read 3D initialization data
3230          ELSEIF ( init_3d%lod_u == 2 )  THEN
3231             CALL get_variable( id_dynamic, 'init_atmosphere_u',               &
3232                                u(nzb+1:nzt,nys:nyn,nxlu:nxr),                 &
3233                                nxlu, nys+1, nzb+1,                            &
3234                                nxr-nxlu+1, nyn-nys+1, init_3d%nzu,            &
3235                                dynamic_3d )
3236!
3237!--          Set value at leftmost model grid point nxl = 0. This is because
3238!--          Inifor provides data only from 1:nx-1 since it assumes non-cyclic
3239!--          conditions.
3240             IF ( nxl == 0 )                                                   &
3241                u(nzb+1:nzt,nys:nyn,nxl) = u(nzb+1:nzt,nys:nyn,nxlu)
3242!
3243!--          Set bottom and top-boundary
3244             u(nzb,:,:)   = u(nzb+1,:,:)
3245             u(nzt+1,:,:) = u(nzt,:,:)
3246             
3247          ENDIF
3248          init_3d%from_file_u = .TRUE.
3249       ELSE
3250          message_string = 'Missing initial data for u-component'
3251          CALL message( 'netcdf_data_input_mod', 'PA0544', 1, 2, 0, 6, 0 )
3252       ENDIF
3253!
3254!--    Read v-component
3255       IF ( check_existence( var_names, 'init_atmosphere_v' ) )  THEN
3256!
3257!--       Read attributes for the fill value and level-of-detail
3258          CALL get_attribute( id_dynamic, char_fill, init_3d%fill_v,           &
3259                              .FALSE., 'init_atmosphere_v' )
3260          CALL get_attribute( id_dynamic, char_lod, init_3d%lod_v,             &
3261                              .FALSE., 'init_atmosphere_v' )
3262!
3263!--       level-of-detail 1 - read initialization profile
3264          IF ( init_3d%lod_v == 1 )  THEN
3265             ALLOCATE( init_3d%v_init(nzb:nzt+1) )
3266             init_3d%v_init = 0.0_wp
3267
3268             CALL get_variable( id_dynamic, 'init_atmosphere_v',               &
3269                                init_3d%v_init(nzb+1:nzt) )
3270!
3271!--          Set top-boundary condition (Neumann)
3272             init_3d%v_init(nzt+1) = init_3d%v_init(nzt)
3273!
3274!--       level-of-detail 2 - read 3D initialization data
3275          ELSEIF ( init_3d%lod_v == 2 )  THEN
3276         
3277             CALL get_variable( id_dynamic, 'init_atmosphere_v',               &
3278                                v(nzb+1:nzt,nysv:nyn,nxl:nxr),                 &
3279                                nxl+1, nysv, nzb+1,                            &
3280                                nxr-nxl+1, nyn-nysv+1, init_3d%nzu,            &
3281                                dynamic_3d )
3282!
3283!--          Set value at southmost model grid point nys = 0. This is because
3284!--          Inifor provides data only from 1:ny-1 since it assumes non-cyclic
3285!--          conditions.
3286             IF ( nys == 0 )                                                   &
3287                v(nzb+1:nzt,nys,nxl:nxr) = v(nzb+1:nzt,nysv,nxl:nxr)                               
3288!
3289!--          Set bottom and top-boundary
3290             v(nzb,:,:)   = v(nzb+1,:,:)
3291             v(nzt+1,:,:) = v(nzt,:,:)
3292             
3293          ENDIF
3294          init_3d%from_file_v = .TRUE.
3295       ELSE
3296          message_string = 'Missing initial data for v-component'
3297          CALL message( 'netcdf_data_input_mod', 'PA0544', 1, 2, 0, 6, 0 )
3298       ENDIF
3299!
3300!--    Read w-component
3301       IF ( check_existence( var_names, 'init_atmosphere_w' ) )  THEN
3302!
3303!--       Read attributes for the fill value and level-of-detail
3304          CALL get_attribute( id_dynamic, char_fill, init_3d%fill_w,           &
3305                              .FALSE., 'init_atmosphere_w' )
3306          CALL get_attribute( id_dynamic, char_lod, init_3d%lod_w,             &
3307                              .FALSE., 'init_atmosphere_w' )
3308!
3309!--       level-of-detail 1 - read initialization profile
3310          IF ( init_3d%lod_w == 1 )  THEN
3311             ALLOCATE( init_3d%w_init(nzb:nzt+1) )
3312             init_3d%w_init = 0.0_wp
3313
3314             CALL get_variable( id_dynamic, 'init_atmosphere_w',               &
3315                                init_3d%w_init(nzb+1:nzt-1) )
3316!
3317!--          Set top-boundary condition (Neumann)
3318             init_3d%w_init(nzt:nzt+1) = init_3d%w_init(nzt-1)
3319!
3320!--       level-of-detail 2 - read 3D initialization data
3321          ELSEIF ( init_3d%lod_w == 2 )  THEN
3322
3323             CALL get_variable( id_dynamic, 'init_atmosphere_w',                &
3324                                w(nzb+1:nzt-1,nys:nyn,nxl:nxr),                 &
3325                                nxl+1, nys+1, nzb+1,                            &
3326                                nxr-nxl+1, nyn-nys+1, init_3d%nzw,              &
3327                                dynamic_3d )
3328!
3329!--          Set bottom and top-boundary                               
3330             w(nzb,:,:)   = 0.0_wp 
3331             w(nzt,:,:)   = w(nzt-1,:,:)
3332             w(nzt+1,:,:) = w(nzt-1,:,:)
3333
3334          ENDIF
3335          init_3d%from_file_w = .TRUE.
3336       ELSE
3337          message_string = 'Missing initial data for w-component'
3338          CALL message( 'netcdf_data_input_mod', 'PA0544', 1, 2, 0, 6, 0 )
3339       ENDIF
3340!
3341!--    Read potential temperature
3342       IF ( .NOT. neutral )  THEN
3343          IF ( check_existence( var_names, 'init_atmosphere_pt' ) )  THEN
3344!
3345!--          Read attributes for the fill value and level-of-detail
3346             CALL get_attribute( id_dynamic, char_fill, init_3d%fill_pt,       &
3347                                 .FALSE., 'init_atmosphere_pt' )
3348             CALL get_attribute( id_dynamic, char_lod, init_3d%lod_pt,         &
3349                                 .FALSE., 'init_atmosphere_pt' )
3350!
3351!--          level-of-detail 1 - read initialization profile
3352             IF ( init_3d%lod_pt == 1 )  THEN
3353                ALLOCATE( init_3d%pt_init(nzb:nzt+1) )
3354
3355                CALL get_variable( id_dynamic, 'init_atmosphere_pt',           &
3356                                   init_3d%pt_init(nzb+1:nzt) )
3357!
3358!--             Set Neumann top and surface boundary condition for initial
3359!--             profil
3360                init_3d%pt_init(nzb)   = init_3d%pt_init(nzb+1)
3361                init_3d%pt_init(nzt+1) = init_3d%pt_init(nzt)
3362!
3363!--          level-of-detail 2 - read 3D initialization data
3364             ELSEIF ( init_3d%lod_pt == 2 )  THEN
3365
3366                CALL get_variable( id_dynamic, 'init_atmosphere_pt',           &
3367                                   pt(nzb+1:nzt,nys:nyn,nxl:nxr),              &
3368                                   nxl+1, nys+1, nzb+1,                        &
3369                                   nxr-nxl+1, nyn-nys+1, init_3d%nzu,          &
3370                                   dynamic_3d )
3371                                   
3372!
3373!--             Set bottom and top-boundary
3374                pt(nzb,:,:)   = pt(nzb+1,:,:)
3375                pt(nzt+1,:,:) = pt(nzt,:,:)             
3376
3377             ENDIF
3378             init_3d%from_file_pt = .TRUE.
3379          ELSE
3380             message_string = 'Missing initial data for ' //                   &
3381                              'potential temperature'
3382             CALL message( 'netcdf_data_input_mod', 'PA0544', 1, 2, 0, 6, 0 )
3383          ENDIF
3384       ENDIF
3385!
3386!--    Read mixing ratio
3387       IF ( humidity )  THEN
3388          IF ( check_existence( var_names, 'init_atmosphere_qv' ) )  THEN
3389!
3390!--          Read attributes for the fill value and level-of-detail
3391             CALL get_attribute( id_dynamic, char_fill, init_3d%fill_q,        &
3392                                 .FALSE., 'init_atmosphere_qv' )
3393             CALL get_attribute( id_dynamic, char_lod, init_3d%lod_q,          &
3394                                 .FALSE., 'init_atmosphere_qv' )
3395!
3396!--          level-of-detail 1 - read initialization profile
3397             IF ( init_3d%lod_q == 1 )  THEN
3398                ALLOCATE( init_3d%q_init(nzb:nzt+1) )
3399
3400                CALL get_variable( id_dynamic, 'init_atmosphere_qv',           &
3401                                    init_3d%q_init(nzb+1:nzt) )
3402!
3403!--             Set bottom and top boundary condition (Neumann)
3404                init_3d%q_init(nzb)   = init_3d%q_init(nzb+1)
3405                init_3d%q_init(nzt+1) = init_3d%q_init(nzt)
3406!
3407!--          level-of-detail 2 - read 3D initialization data
3408             ELSEIF ( init_3d%lod_q == 2 )  THEN
3409             
3410                CALL get_variable( id_dynamic, 'init_atmosphere_qv',           &
3411                                   q(nzb+1:nzt,nys:nyn,nxl:nxr),               &
3412                                   nxl+1, nys+1, nzb+1,                        &
3413                                   nxr-nxl+1, nyn-nys+1, init_3d%nzu,          &
3414                                   dynamic_3d )
3415                                   
3416!
3417!--             Set bottom and top-boundary
3418                q(nzb,:,:)   = q(nzb+1,:,:)
3419                q(nzt+1,:,:) = q(nzt,:,:)
3420               
3421             ENDIF
3422             init_3d%from_file_q = .TRUE.
3423          ELSE
3424             message_string = 'Missing initial data for ' //                   &
3425                              'mixing ratio'
3426             CALL message( 'netcdf_data_input_mod', 'PA0544', 1, 2, 0, 6, 0 )
3427          ENDIF
3428       ENDIF       
3429!
3430!--    Read chemistry variables.
3431!--    Please note, for the moment, only LOD=1 is allowed
3432       IF ( air_chemistry )  THEN
3433!
3434!--       Allocate chemistry input profiles, as well as arrays for fill values
3435!--       and LOD's.
3436          ALLOCATE( init_3d%chem_init(nzb:nzt+1,                               &
3437                                      1:UBOUND(init_3d%var_names_chem, 1 )) )
3438          ALLOCATE( init_3d%fill_chem(1:UBOUND(init_3d%var_names_chem, 1)) )   
3439          ALLOCATE( init_3d%lod_chem(1:UBOUND(init_3d%var_names_chem, 1))  ) 
3440         
3441          DO  n = 1, UBOUND(init_3d%var_names_chem, 1)
3442             IF ( check_existence( var_names,                                  &
3443                                   TRIM( init_3d%var_names_chem(n) ) ) )  THEN
3444!
3445!--             Read attributes for the fill value and level-of-detail
3446                CALL get_attribute( id_dynamic, char_fill,                     &
3447                                    init_3d%fill_chem(n),                      &
3448                                    .FALSE.,                                   &
3449                                    TRIM( init_3d%var_names_chem(n) ) )
3450                CALL get_attribute( id_dynamic, char_lod,                      &
3451                                    init_3d%lod_chem(n),                       &
3452                                    .FALSE.,                                   &
3453                                    TRIM( init_3d%var_names_chem(n) ) )
3454!
3455!--             Give message that only LOD=1 is allowed.
3456                IF ( init_3d%lod_chem(n) /= 1 )  THEN               
3457                   message_string = 'For chemistry variables only LOD=1 is ' //&
3458                                    'allowed.'
3459                   CALL message( 'netcdf_data_input_mod', 'PA0586',            &
3460                                 1, 2, 0, 6, 0 )
3461                ENDIF
3462!
3463!--             level-of-detail 1 - read initialization profile
3464                CALL get_variable( id_dynamic,                                 &
3465                                   TRIM( init_3d%var_names_chem(n) ),          &
3466                                   init_3d%chem_init(nzb+1:nzt,n) )
3467!
3468!--             Set bottom and top boundary condition (Neumann)
3469                init_3d%chem_init(nzb,n)   = init_3d%chem_init(nzb+1,n)
3470                init_3d%chem_init(nzt+1,n) = init_3d%chem_init(nzt,n)
3471               
3472                init_3d%from_file_chem(n) = .TRUE.
3473             ENDIF
3474          ENDDO
3475       ENDIF
3476!
3477!--    Close input file
3478       CALL close_input_file( id_dynamic )
3479#endif
3480!
3481!--    End of CPU measurement
3482       CALL cpu_log( log_point_s(85), 'NetCDF input init', 'stop' )
3483!
3484!--    Finally, check if the input data has any fill values. Please note,
3485!--    checks depend on the LOD of the input data.
3486       IF ( init_3d%from_file_u )  THEN
3487          check_passed = .TRUE.
3488          IF ( init_3d%lod_u == 1 )  THEN
3489             IF ( ANY( init_3d%u_init(nzb+1:nzt+1) == init_3d%fill_u ) )       &
3490                check_passed = .FALSE.
3491          ELSEIF ( init_3d%lod_u == 2 )  THEN
3492             IF ( ANY( u(nzb+1:nzt+1,nys:nyn,nxlu:nxr) == init_3d%fill_u ) )   &
3493                check_passed = .FALSE.
3494          ENDIF
3495          IF ( .NOT. check_passed )  THEN
3496             message_string = 'NetCDF input for init_atmosphere_u must ' //    &
3497                              'not contain any _FillValues'
3498             CALL message( 'netcdf_data_input_mod', 'PA0545', 2, 2, 0, 6, 0 )
3499          ENDIF
3500       ENDIF
3501
3502       IF ( init_3d%from_file_v )  THEN
3503          check_passed = .TRUE.
3504          IF ( init_3d%lod_v == 1 )  THEN
3505             IF ( ANY( init_3d%v_init(nzb+1:nzt+1) == init_3d%fill_v ) )       &
3506                check_passed = .FALSE.
3507          ELSEIF ( init_3d%lod_v == 2 )  THEN
3508             IF ( ANY( v(nzb+1:nzt+1,nysv:nyn,nxl:nxr) == init_3d%fill_v ) )   &
3509                check_passed = .FALSE.
3510          ENDIF
3511          IF ( .NOT. check_passed )  THEN
3512             message_string = 'NetCDF input for init_atmosphere_v must ' //    &
3513                              'not contain any _FillValues'
3514             CALL message( 'netcdf_data_input_mod', 'PA0545', 2, 2, 0, 6, 0 )
3515          ENDIF
3516       ENDIF
3517
3518       IF ( init_3d%from_file_w )  THEN
3519          check_passed = .TRUE.
3520          IF ( init_3d%lod_w == 1 )  THEN
3521             IF ( ANY( init_3d%w_init(nzb+1:nzt) == init_3d%fill_w ) )         &
3522                check_passed = .FALSE.
3523          ELSEIF ( init_3d%lod_w == 2 )  THEN
3524             IF ( ANY( w(nzb+1:nzt,nys:nyn,nxl:nxr) == init_3d%fill_w ) )      &
3525                check_passed = .FALSE.
3526          ENDIF
3527          IF ( .NOT. check_passed )  THEN
3528             message_string = 'NetCDF input for init_atmosphere_w must ' //    &
3529                              'not contain any _FillValues'
3530             CALL message( 'netcdf_data_input_mod', 'PA0545', 2, 2, 0, 6, 0 )
3531          ENDIF
3532       ENDIF
3533
3534       IF ( init_3d%from_file_pt )  THEN
3535          check_passed = .TRUE.
3536          IF ( init_3d%lod_pt == 1 )  THEN
3537             IF ( ANY( init_3d%pt_init(nzb+1:nzt+1) == init_3d%fill_pt ) )     &
3538                check_passed = .FALSE.
3539          ELSEIF ( init_3d%lod_pt == 2 )  THEN
3540             IF ( ANY( pt(nzb+1:nzt+1,nys:nyn,nxl:nxr) == init_3d%fill_pt ) )  &
3541                check_passed = .FALSE.
3542          ENDIF
3543          IF ( .NOT. check_passed )  THEN
3544             message_string = 'NetCDF input for init_atmosphere_pt must ' //   &
3545                              'not contain any _FillValues'
3546             CALL message( 'netcdf_data_input_mod', 'PA0545', 2, 2, 0, 6, 0 )
3547          ENDIF
3548       ENDIF
3549
3550       IF ( init_3d%from_file_q )  THEN
3551          check_passed = .TRUE.
3552          IF ( init_3d%lod_q == 1 )  THEN
3553             IF ( ANY( init_3d%q_init(nzb+1:nzt+1) == init_3d%fill_q ) )       &
3554                check_passed = .FALSE.
3555          ELSEIF ( init_3d%lod_q == 2 )  THEN
3556             IF ( ANY( q(nzb+1:nzt+1,nys:nyn,nxl:nxr) == init_3d%fill_q ) )    &
3557                check_passed = .FALSE.
3558          ENDIF
3559          IF ( .NOT. check_passed )  THEN
3560             message_string = 'NetCDF input for init_atmosphere_q must ' //    &
3561                              'not contain any _FillValues'
3562             CALL message( 'netcdf_data_input_mod', 'PA0545', 2, 2, 0, 6, 0 )
3563          ENDIF
3564       ENDIF
3565!
3566!--    Workaround for cyclic conditions. Please see above for further explanation.
3567       IF ( bc_lr_cyc  .AND.  nxl == 0 )  nxlu = nxl
3568       IF ( bc_ns_cyc  .AND.  nys == 0 )  nysv = nys
3569
3570    END SUBROUTINE netcdf_data_input_init_3d
3571   
3572!------------------------------------------------------------------------------!
3573! Description:
3574! ------------
3575!> Reads initialization data of u, v, w, pt, q, geostrophic wind components,
3576!> as well as soil moisture and soil temperature, derived from larger-scale
3577!> model (COSMO) by Inifor.
3578!------------------------------------------------------------------------------!
3579    SUBROUTINE netcdf_data_input_init_lsm
3580
3581       USE control_parameters,                                                 &
3582           ONLY:  message_string
3583
3584       USE indices,                                                            &
3585           ONLY:  nx, nxl, nxr, ny, nyn, nys
3586
3587       IMPLICIT NONE
3588
3589       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names !< string containing all variables on file
3590     
3591       INTEGER(iwp) ::  id_dynamic !< NetCDF id of dynamic input file
3592       INTEGER(iwp) ::  num_vars   !< number of variables in netcdf input file
3593
3594!
3595!--    Skip routine if no input file with dynamic input data is available.
3596       IF ( .NOT. input_pids_dynamic )  RETURN
3597!
3598!--    CPU measurement
3599       CALL cpu_log( log_point_s(85), 'NetCDF input init', 'start' )
3600
3601#if defined ( __netcdf )
3602!
3603!--    Open file in read-only mode
3604       CALL open_read_file( TRIM( input_file_dynamic ) //                      &
3605                            TRIM( coupling_char ), id_dynamic )
3606
3607!
3608!--    At first, inquire all variable names.
3609       CALL inquire_num_variables( id_dynamic, num_vars )
3610!
3611!--    Allocate memory to store variable names.
3612       ALLOCATE( var_names(1:num_vars) )
3613       CALL inquire_variable_names( id_dynamic, var_names )
3614!
3615!--    Read vertical dimension for soil depth.
3616       IF ( check_existence( var_names, 'zsoil' ) )                            &
3617          CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%nzs,&
3618                                                       'zsoil' )
3619!
3620!--    Read also the horizontal dimensions required for soil initialization.
3621!--    Please note, in case of non-nested runs or in case of root domain,
3622!--    these data is already available, but will be read again for the sake
3623!--    of clearness.
3624       CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%nx,    &
3625                                                    'x'  )
3626       CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%ny,    &
3627                                                    'y'  )
3628!
3629!--    Check for correct horizontal and vertical dimension. Please note,
3630!--    in case of non-nested runs or in case of root domain, these checks
3631!--    are already performed
3632       IF ( init_3d%nx-1 /= nx  .OR.  init_3d%ny-1 /= ny )  THEN
3633          message_string = 'Number of inifor horizontal grid points  '//       &
3634                           'does not match the number of numeric grid points.'
3635          CALL message( 'netcdf_data_input_mod', 'PA0543', 1, 2, 0, 6, 0 )
3636       ENDIF
3637!
3638!--    Read vertical dimensions. Later, these are required for eventual
3639!--    inter- and extrapolations of the initialization data.
3640       IF ( check_existence( var_names, 'zsoil' ) )  THEN
3641          ALLOCATE( init_3d%z_soil(1:init_3d%nzs) )
3642          CALL get_variable( id_dynamic, 'zsoil', init_3d%z_soil )
3643       ENDIF
3644!
3645!--    Read initial data for soil moisture
3646       IF ( check_existence( var_names, 'init_soil_m' ) )  THEN
3647!
3648!--       Read attributes for the fill value and level-of-detail
3649          CALL get_attribute( id_dynamic, char_fill,                           &
3650                              init_3d%fill_msoil,                              &
3651                              .FALSE., 'init_soil_m' )
3652          CALL get_attribute( id_dynamic, char_lod,                            &
3653                              init_3d%lod_msoil,                               &
3654                              .FALSE., 'init_soil_m' )
3655!
3656!--       level-of-detail 1 - read initialization profile
3657          IF ( init_3d%lod_msoil == 1 )  THEN
3658             ALLOCATE( init_3d%msoil_1d(0:init_3d%nzs-1) )
3659
3660             CALL get_variable( id_dynamic, 'init_soil_m',                     &
3661                                init_3d%msoil_1d(0:init_3d%nzs-1) )
3662!
3663!--       level-of-detail 2 - read 3D initialization data
3664          ELSEIF ( init_3d%lod_msoil == 2 )  THEN
3665             ALLOCATE ( init_3d%msoil_3d(0:init_3d%nzs-1,nys:nyn,nxl:nxr) )
3666
3667            CALL get_variable( id_dynamic, 'init_soil_m',                      &   
3668                             init_3d%msoil_3d(0:init_3d%nzs-1,nys:nyn,nxl:nxr),&
3669                             nxl, nxr, nys, nyn, 0, init_3d%nzs-1 )
3670
3671          ENDIF
3672          init_3d%from_file_msoil = .TRUE.
3673       ENDIF
3674!
3675!--    Read soil temperature
3676       IF ( check_existence( var_names, 'init_soil_t' ) )  THEN
3677!
3678!--       Read attributes for the fill value and level-of-detail
3679          CALL get_attribute( id_dynamic, char_fill,                           &
3680                              init_3d%fill_tsoil,                              &
3681                              .FALSE., 'init_soil_t' )
3682          CALL get_attribute( id_dynamic, char_lod,                            &
3683                              init_3d%lod_tsoil,                               &
3684                              .FALSE., 'init_soil_t' )
3685!
3686!--       level-of-detail 1 - read initialization profile
3687          IF ( init_3d%lod_tsoil == 1 )  THEN
3688             ALLOCATE( init_3d%tsoil_1d(0:init_3d%nzs-1) )
3689
3690             CALL get_variable( id_dynamic, 'init_soil_t',                     &
3691                                init_3d%tsoil_1d(0:init_3d%nzs-1) )
3692
3693!
3694!--       level-of-detail 2 - read 3D initialization data
3695          ELSEIF ( init_3d%lod_tsoil == 2 )  THEN
3696             ALLOCATE ( init_3d%tsoil_3d(0:init_3d%nzs-1,nys:nyn,nxl:nxr) )
3697             
3698             CALL get_variable( id_dynamic, 'init_soil_t',                     &   
3699                             init_3d%tsoil_3d(0:init_3d%nzs-1,nys:nyn,nxl:nxr),&
3700                             nxl, nxr, nys, nyn, 0, init_3d%nzs-1 )
3701          ENDIF
3702          init_3d%from_file_tsoil = .TRUE.
3703       ENDIF
3704!
3705!--    Close input file
3706       CALL close_input_file( id_dynamic )
3707#endif
3708!
3709!--    End of CPU measurement
3710       CALL cpu_log( log_point_s(85), 'NetCDF input init', 'stop' )
3711
3712    END SUBROUTINE netcdf_data_input_init_lsm   
3713
3714!------------------------------------------------------------------------------!
3715! Description:
3716! ------------
3717!> Reads data at lateral and top boundaries derived from larger-scale model
3718!> (COSMO) by Inifor.
3719!------------------------------------------------------------------------------!
3720    SUBROUTINE netcdf_data_input_offline_nesting
3721
3722       USE control_parameters,                                                 &
3723           ONLY:  air_chemistry, bc_dirichlet_l, bc_dirichlet_n,               &
3724                  bc_dirichlet_r, bc_dirichlet_s, humidity, neutral,           &
3725                  nesting_offline, time_since_reference_point
3726
3727       USE indices,                                                            &
3728           ONLY:  nxl, nxlu, nxr, nyn, nys, nysv, nzb, nzt
3729
3730       IMPLICIT NONE
3731       
3732       INTEGER(iwp) ::  id_dynamic !< NetCDF id of dynamic input file
3733       INTEGER(iwp) ::  n          !< running index for chemistry variables
3734       INTEGER(iwp) ::  num_vars   !< number of variables in netcdf input file
3735       INTEGER(iwp) ::  t          !< running index time dimension
3736!
3737!--    Skip input if no forcing from larger-scale models is applied.
3738       IF ( .NOT. nesting_offline )  RETURN
3739
3740!
3741!--    CPU measurement
3742       CALL cpu_log( log_point_s(86), 'NetCDF input forcing', 'start' )
3743
3744#if defined ( __netcdf )
3745!
3746!--    Open file in read-only mode
3747       CALL open_read_file( TRIM( input_file_dynamic ) //                      &
3748                            TRIM( coupling_char ), id_dynamic )
3749!
3750!--    Initialize INIFOR forcing.
3751       IF ( .NOT. nest_offl%init )  THEN
3752!
3753!--       At first, inquire all variable names.
3754          CALL inquire_num_variables( id_dynamic, num_vars )
3755!
3756!--       Allocate memory to store variable names.
3757          ALLOCATE( nest_offl%var_names(1:num_vars) )
3758          CALL inquire_variable_names( id_dynamic, nest_offl%var_names )
3759!
3760!--       Read time dimension, allocate memory and finally read time array
3761          CALL netcdf_data_input_get_dimension_length( id_dynamic,             &
3762                                                       nest_offl%nt, 'time' )
3763
3764          IF ( check_existence( nest_offl%var_names, 'time' ) )  THEN
3765             ALLOCATE( nest_offl%time(0:nest_offl%nt-1) )
3766             CALL get_variable( id_dynamic, 'time', nest_offl%time )
3767          ENDIF
3768!
3769!--       Read vertical dimension of scalar und w grid
3770          CALL netcdf_data_input_get_dimension_length( id_dynamic,             &
3771                                                       nest_offl%nzu, 'z' )
3772          CALL netcdf_data_input_get_dimension_length( id_dynamic,             &
3773                                                       nest_offl%nzw, 'zw' )
3774
3775          IF ( check_existence( nest_offl%var_names, 'z' ) )  THEN
3776             ALLOCATE( nest_offl%zu_atmos(1:nest_offl%nzu) )
3777             CALL get_variable( id_dynamic, 'z', nest_offl%zu_atmos )
3778          ENDIF
3779          IF ( check_existence( nest_offl%var_names, 'zw' ) )  THEN
3780             ALLOCATE( nest_offl%zw_atmos(1:nest_offl%nzw) )
3781             CALL get_variable( id_dynamic, 'zw', nest_offl%zw_atmos )
3782          ENDIF
3783
3784!
3785!--       Read surface pressure
3786          IF ( check_existence( nest_offl%var_names,                           &
3787                                'surface_forcing_surface_pressure' ) )  THEN
3788             ALLOCATE( nest_offl%surface_pressure(0:nest_offl%nt-1) )
3789             CALL get_variable( id_dynamic,                                    &
3790                                'surface_forcing_surface_pressure',            &
3791                                nest_offl%surface_pressure )
3792          ENDIF
3793!
3794!--       Set control flag to indicate that initialization is already done
3795          nest_offl%init = .TRUE.
3796
3797       ENDIF
3798
3799!
3800!--    Obtain time index for current input starting at 0.
3801!--    @todo: At the moment INIFOR and simulated time correspond
3802!--           to each other. If required, adjust to daytime.
3803       nest_offl%tind = MINLOC( ABS( nest_offl%time -                          &
3804                                     time_since_reference_point ), DIM = 1 )   &
3805                        - 1
3806       nest_offl%tind_p = nest_offl%tind + 1       
3807!
3808!--    Read geostrophic wind components
3809       DO  t = nest_offl%tind, nest_offl%tind_p
3810          CALL get_variable_pr( id_dynamic, 'ls_forcing_ug', t+1,              &
3811                                nest_offl%ug(t-nest_offl%tind,nzb+1:nzt) )
3812          CALL get_variable_pr( id_dynamic, 'ls_forcing_vg', t+1,              &
3813                                nest_offl%vg(t-nest_offl%tind,nzb+1:nzt) )
3814       ENDDO
3815!
3816!--    Read data at lateral and top boundaries. Please note, at left and
3817!--    right domain boundary, yz-layers are read for u, v, w, pt and q.
3818!--    For the v-component, the data starts at nysv, while for the other
3819!--    quantities the data starts at nys. This is equivalent at the north
3820!--    and south domain boundary for the u-component.
3821!--    Further, lateral data is not accessed by parallel IO, indicated by the
3822!--    last passed flag in the subroutine get_variable(). This is because
3823!--    not every PE participates in this collective blocking read operation.
3824       IF ( bc_dirichlet_l )  THEN
3825          CALL get_variable( id_dynamic, 'ls_forcing_left_u',                  &
3826                           nest_offl%u_left(0:1,nzb+1:nzt,nys:nyn),            &
3827                           nys+1, nzb+1, nest_offl%tind+1,                     &
3828                           nyn-nys+1, nest_offl%nzu, 2, .FALSE. )
3829     
3830          CALL get_variable( id_dynamic, 'ls_forcing_left_v',                  &
3831                           nest_offl%v_left(0:1,nzb+1:nzt,nysv:nyn),           &
3832                           nysv, nzb+1, nest_offl%tind+1,                      &
3833                           nyn-nysv+1, nest_offl%nzu, 2, .FALSE. )
3834
3835          CALL get_variable( id_dynamic, 'ls_forcing_left_w',                  &
3836                           nest_offl%w_left(0:1,nzb+1:nzt-1,nys:nyn),          &
3837                           nys+1, nzb+1, nest_offl%tind+1,                     &
3838                           nyn-nys+1, nest_offl%nzw, 2, .FALSE. )
3839
3840          IF ( .NOT. neutral )  THEN
3841             CALL get_variable( id_dynamic, 'ls_forcing_left_pt',              &
3842                           nest_offl%pt_left(0:1,nzb+1:nzt,nys:nyn),           &
3843                           nys+1, nzb+1, nest_offl%tind+1,                     &
3844                           nyn-nys+1, nest_offl%nzu, 2, .FALSE. )
3845          ENDIF
3846
3847          IF ( humidity )  THEN
3848             CALL get_variable( id_dynamic, 'ls_forcing_left_qv',              &
3849                           nest_offl%q_left(0:1,nzb+1:nzt,nys:nyn),            &
3850                           nys+1, nzb+1, nest_offl%tind+1,                     &
3851                           nyn-nys+1, nest_offl%nzu, 2, .FALSE. )
3852          ENDIF
3853         
3854          IF ( air_chemistry )  THEN
3855             DO  n = 1, UBOUND(nest_offl%var_names_chem_l, 1)
3856                IF ( check_existence( nest_offl%var_names,                     &
3857                                      nest_offl%var_names_chem_l(n) ) )        &
3858                THEN
3859                   CALL get_variable( id_dynamic,                              &
3860                              TRIM( nest_offl%var_names_chem_l(n) ),           &
3861                              nest_offl%chem_left(0:1,nzb+1:nzt,nys:nyn,n),    &
3862                              nys+1, nzb+1, nest_offl%tind+1,                  &
3863                              nyn-nys+1, nest_offl%nzu, 2, .FALSE. )
3864                   nest_offl%chem_from_file_l(n) = .TRUE.
3865                ENDIF
3866             ENDDO
3867          ENDIF
3868
3869       ENDIF
3870
3871       IF ( bc_dirichlet_r )  THEN
3872          CALL get_variable( id_dynamic, 'ls_forcing_right_u',                 &
3873                           nest_offl%u_right(0:1,nzb+1:nzt,nys:nyn),           &
3874                           nys+1, nzb+1, nest_offl%tind+1,                     &
3875                           nyn-nys+1, nest_offl%nzu, 2, .FALSE. )
3876                           
3877          CALL get_variable( id_dynamic, 'ls_forcing_right_v',                 &
3878                           nest_offl%v_right(0:1,nzb+1:nzt,nysv:nyn),          &
3879                           nysv, nzb+1, nest_offl%tind+1,                      &
3880                           nyn-nysv+1, nest_offl%nzu, 2, .FALSE. )
3881                           
3882          CALL get_variable( id_dynamic, 'ls_forcing_right_w',                 &
3883                           nest_offl%w_right(0:1,nzb+1:nzt-1,nys:nyn),         &
3884                           nys+1, nzb+1, nest_offl%tind+1,                     &
3885                           nyn-nys+1, nest_offl%nzw, 2, .FALSE. )
3886                           
3887          IF ( .NOT. neutral )  THEN
3888             CALL get_variable( id_dynamic, 'ls_forcing_right_pt',             &
3889                           nest_offl%pt_right(0:1,nzb+1:nzt,nys:nyn),          &
3890                           nys+1, nzb+1, nest_offl%tind+1,                     &
3891                           nyn-nys+1, nest_offl%nzu, 2, .FALSE. )
3892          ENDIF
3893          IF ( humidity )  THEN
3894             CALL get_variable( id_dynamic, 'ls_forcing_right_qv',             &
3895                           nest_offl%q_right(0:1,nzb+1:nzt,nys:nyn),           &
3896                           nys+1, nzb+1, nest_offl%tind+1,                     &
3897                           nyn-nys+1, nest_offl%nzu, 2, .FALSE. )
3898          ENDIF
3899         
3900          IF ( air_chemistry )  THEN
3901             DO  n = 1, UBOUND(nest_offl%var_names_chem_r, 1)
3902                IF ( check_existence( nest_offl%var_names,                     &
3903                                      nest_offl%var_names_chem_r(n) ) )        &
3904                THEN
3905                   CALL get_variable( id_dynamic,                              &
3906                              TRIM( nest_offl%var_names_chem_r(n) ),           &
3907                              nest_offl%chem_right(0:1,nzb+1:nzt,nys:nyn,n),   &
3908                              nys+1, nzb+1, nest_offl%tind+1,                  &
3909                              nyn-nys+1, nest_offl%nzu, 2, .FALSE. )
3910                   nest_offl%chem_from_file_r(n) = .TRUE.
3911                ENDIF
3912             ENDDO
3913          ENDIF
3914       ENDIF
3915
3916       IF ( bc_dirichlet_n )  THEN
3917       
3918          CALL get_variable( id_dynamic, 'ls_forcing_north_u',                 &
3919                           nest_offl%u_north(0:1,nzb+1:nzt,nxlu:nxr),          &
3920                           nxlu, nzb+1, nest_offl%tind+1,                      &
3921                           nxr-nxlu+1, nest_offl%nzu, 2, .FALSE. )
3922                           
3923          CALL get_variable( id_dynamic, 'ls_forcing_north_v',                 &
3924                           nest_offl%v_north(0:1,nzb+1:nzt,nxl:nxr),           &
3925                           nxl+1, nzb+1, nest_offl%tind+1,                     &
3926                           nxr-nxl+1, nest_offl%nzu, 2, .FALSE. )
3927                           
3928          CALL get_variable( id_dynamic, 'ls_forcing_north_w',                 &
3929                           nest_offl%w_north(0:1,nzb+1:nzt-1,nxl:nxr),         &
3930                           nxl+1, nzb+1, nest_offl%tind+1,                     &
3931                           nxr-nxl+1, nest_offl%nzw, 2, .FALSE. )
3932                           
3933          IF ( .NOT. neutral )  THEN
3934             CALL get_variable( id_dynamic, 'ls_forcing_north_pt',             &
3935                           nest_offl%pt_north(0:1,nzb+1:nzt,nxl:nxr),          &
3936                           nxl+1, nzb+1, nest_offl%tind+1,                     &
3937                           nxr-nxl+1, nest_offl%nzu, 2, .FALSE. )
3938          ENDIF
3939          IF ( humidity )  THEN
3940             CALL get_variable( id_dynamic, 'ls_forcing_north_qv',             &
3941                           nest_offl%q_north(0:1,nzb+1:nzt,nxl:nxr),           &
3942                           nxl+1, nzb+1, nest_offl%tind+1,                     &
3943                           nxr-nxl+1, nest_offl%nzu, 2, .FALSE. )
3944          ENDIF
3945         
3946          IF ( air_chemistry )  THEN
3947             DO  n = 1, UBOUND(nest_offl%var_names_chem_n, 1)
3948                IF ( check_existence( nest_offl%var_names,                     &
3949                                      nest_offl%var_names_chem_n(n) ) )        &
3950                THEN
3951                   CALL get_variable( id_dynamic,                              &
3952                              TRIM( nest_offl%var_names_chem_n(n) ),           &
3953                              nest_offl%chem_north(0:1,nzb+1:nzt,nxl:nxr,n),   &
3954                              nxl+1, nzb+1, nest_offl%tind+1,                  &
3955                              nxr-nxl+1, nest_offl%nzu, 2, .FALSE. )
3956                   nest_offl%chem_from_file_n(n) = .TRUE.
3957                ENDIF
3958             ENDDO
3959          ENDIF
3960       ENDIF
3961
3962       IF ( bc_dirichlet_s )  THEN
3963          CALL get_variable( id_dynamic, 'ls_forcing_south_u',                 &
3964                           nest_offl%u_south(0:1,nzb+1:nzt,nxlu:nxr),          &
3965                           nxlu, nzb+1, nest_offl%tind+1,                      &
3966                           nxr-nxlu+1, nest_offl%nzu, 2, .FALSE. )
3967
3968          CALL get_variable( id_dynamic, 'ls_forcing_south_v',                 &
3969                           nest_offl%v_south(0:1,nzb+1:nzt,nxl:nxr),           &
3970                           nxl+1, nzb+1, nest_offl%tind+1,                     &
3971                           nxr-nxl+1, nest_offl%nzu, 2, .FALSE. )
3972                           
3973          CALL get_variable( id_dynamic, 'ls_forcing_south_w',                 &
3974                           nest_offl%w_south(0:1,nzb+1:nzt-1,nxl:nxr),         &
3975                           nxl+1, nzb+1, nest_offl%tind+1,                     &
3976                           nxr-nxl+1, nest_offl%nzw, 2, .FALSE. )
3977                           
3978          IF ( .NOT. neutral )  THEN
3979             CALL get_variable( id_dynamic, 'ls_forcing_south_pt',             &
3980                           nest_offl%pt_south(0:1,nzb+1:nzt,nxl:nxr),          &
3981                           nxl+1, nzb+1, nest_offl%tind+1,                     &
3982                           nxr-nxl+1, nest_offl%nzu, 2, .FALSE. )
3983          ENDIF
3984          IF ( humidity )  THEN
3985             CALL get_variable( id_dynamic, 'ls_forcing_south_qv',             &
3986                           nest_offl%q_south(0:1,nzb+1:nzt,nxl:nxr),           &
3987                           nxl+1, nzb+1, nest_offl%tind+1,                     &
3988                           nxr-nxl+1, nest_offl%nzu, 2, .FALSE. )
3989          ENDIF
3990         
3991          IF ( air_chemistry )  THEN
3992             DO  n = 1, UBOUND(nest_offl%var_names_chem_s, 1)
3993                IF ( check_existence( nest_offl%var_names,                     &
3994                                      nest_offl%var_names_chem_s(n) ) )        &
3995                THEN
3996                   CALL get_variable( id_dynamic,                              &
3997                              TRIM( nest_offl%var_names_chem_s(n) ),           &
3998                              nest_offl%chem_south(0:1,nzb+1:nzt,nxl:nxr,n),   &
3999                              nxl+1, nzb+1, nest_offl%tind+1,                  &
4000                              nxr-nxl+1, nest_offl%nzu, 2, .FALSE. )
4001                   nest_offl%chem_from_file_s(n) = .TRUE.
4002                ENDIF
4003             ENDDO
4004          ENDIF
4005       ENDIF
4006
4007!
4008!--    Top boundary
4009       CALL get_variable( id_dynamic, 'ls_forcing_top_u',                      &
4010                             nest_offl%u_top(0:1,nys:nyn,nxlu:nxr),            &
4011                             nxlu, nys+1, nest_offl%tind+1,                    &
4012                             nxr-nxlu+1, nyn-nys+1, 2, .TRUE. )
4013
4014       CALL get_variable( id_dynamic, 'ls_forcing_top_v',                      &
4015                             nest_offl%v_top(0:1,nysv:nyn,nxl:nxr),            &
4016                             nxl+1, nysv, nest_offl%tind+1,                    &
4017                             nxr-nxl+1, nyn-nysv+1, 2, .TRUE. )
4018                             
4019       CALL get_variable( id_dynamic, 'ls_forcing_top_w',                      &
4020                             nest_offl%w_top(0:1,nys:nyn,nxl:nxr),             &
4021                             nxl+1, nys+1, nest_offl%tind+1,                   &
4022                             nxr-nxl+1, nyn-nys+1, 2, .TRUE. )
4023                             
4024       IF ( .NOT. neutral )  THEN
4025          CALL get_variable( id_dynamic, 'ls_forcing_top_pt',                  &
4026                                nest_offl%pt_top(0:1,nys:nyn,nxl:nxr),         &
4027                                nxl+1, nys+1, nest_offl%tind+1,                &
4028                                nxr-nxl+1, nyn-nys+1, 2, .TRUE. )
4029       ENDIF
4030       IF ( humidity )  THEN
4031          CALL get_variable( id_dynamic, 'ls_forcing_top_qv',                  &
4032                                nest_offl%q_top(0:1,nys:nyn,nxl:nxr),          &
4033                                nxl+1, nys+1, nest_offl%tind+1,                &
4034                                nxr-nxl+1, nyn-nys+1, 2, .TRUE. )
4035       ENDIF
4036       
4037       IF ( air_chemistry )  THEN
4038          DO  n = 1, UBOUND(nest_offl%var_names_chem_t, 1)
4039             IF ( check_existence( nest_offl%var_names,                     &
4040                                   nest_offl%var_names_chem_t(n) ) )  THEN     
4041                CALL get_variable( id_dynamic,                                 &
4042                              TRIM( nest_offl%var_names_chem_t(n) ),           &
4043                              nest_offl%chem_top(0:1,nys:nyn,nxl:nxr,n),       &
4044                              nxl+1, nys+1, nest_offl%tind+1,                  &
4045                              nxr-nxl+1, nyn-nys+1, 2, .TRUE. )
4046                nest_offl%chem_from_file_t(n) = .TRUE.
4047             ENDIF
4048          ENDDO
4049       ENDIF
4050
4051!
4052!--    Close input file
4053       CALL close_input_file( id_dynamic )
4054#endif
4055!
4056!--    End of CPU measurement
4057       CALL cpu_log( log_point_s(86), 'NetCDF input forcing', 'stop' )
4058
4059    END SUBROUTINE netcdf_data_input_offline_nesting
4060
4061
4062!------------------------------------------------------------------------------!
4063! Description:
4064! ------------
4065!> Checks input file for consistency and minimum requirements.
4066!------------------------------------------------------------------------------!
4067    SUBROUTINE netcdf_data_input_check_dynamic
4068
4069       USE control_parameters,                                                 &
4070           ONLY:  initializing_actions, message_string, nesting_offline 
4071
4072       IMPLICIT NONE
4073
4074!
4075!--    In case of forcing, check whether dynamic input file is present
4076       IF ( .NOT. input_pids_dynamic  .AND.  nesting_offline  )  THEN
4077          message_string = 'nesting_offline = .TRUE. requires dynamic '  //    &
4078                            'input file ' //                                   &
4079                            TRIM( input_file_dynamic ) // TRIM( coupling_char )
4080          CALL message( 'netcdf_data_input_mod', 'PA0546', 1, 2, 0, 6, 0 )
4081       ENDIF
4082!
4083!--    Dynamic input file must also be present if initialization via inifor is
4084!--    prescribed.
4085       IF ( .NOT. input_pids_dynamic  .AND.                                    &
4086            TRIM( initializing_actions ) == 'inifor' )  THEN
4087          message_string = 'initializing_actions = inifor requires dynamic ' //&
4088                           'input file ' // TRIM( input_file_dynamic ) //      &
4089                           TRIM( coupling_char )
4090          CALL message( 'netcdf_data_input_mod', 'PA0547', 1, 2, 0, 6, 0 )
4091       ENDIF
4092
4093    END SUBROUTINE netcdf_data_input_check_dynamic
4094
4095!------------------------------------------------------------------------------!
4096! Description:
4097! ------------
4098!> Checks input file for consistency and minimum requirements.
4099!------------------------------------------------------------------------------!
4100    SUBROUTINE netcdf_data_input_check_static
4101
4102       USE arrays_3d,                                                          &
4103           ONLY:  zu
4104
4105       USE control_parameters,                                                 &
4106           ONLY:  land_surface, message_string, urban_surface
4107
4108       USE indices,                                                            &
4109           ONLY:  nxl, nxr, nyn, nys, wall_flags_0
4110
4111       IMPLICIT NONE
4112
4113       INTEGER(iwp) ::  i      !< loop index along x-direction
4114       INTEGER(iwp) ::  j      !< loop index along y-direction
4115       INTEGER(iwp) ::  n_surf !< number of different surface types at given location
4116
4117       LOGICAL      ::  check_passed !< flag indicating if a check passed
4118
4119!
4120!--    Return if no static input file is available
4121       IF ( .NOT. input_pids_static )  RETURN
4122!
4123!--    Check for correct dimension of surface_fractions, should run from 0-2.
4124       IF ( surface_fraction_f%from_file )  THEN
4125          IF ( surface_fraction_f%nf-1 > 2 )  THEN
4126             message_string = 'nsurface_fraction must not be larger than 3.' 
4127             CALL message( 'netcdf_data_input_mod', 'PA0580', 1, 2, 0, 6, 0 )
4128          ENDIF
4129       ENDIF
4130!
4131!--    Check orography for fill-values. For the moment, give an error message.
4132!--    More advanced methods, e.g. a nearest neighbor algorithm as used in GIS
4133!--    systems might be implemented later.
4134!--    Please note, if no terrain height is provided, it is set to 0.
4135       IF ( ANY( terrain_height_f%var == terrain_height_f%fill ) )  THEN
4136          message_string = 'NetCDF variable zt is not ' //                     &
4137                           'allowed to have missing data'
4138          CALL message( 'netcdf_data_input_mod', 'PA0550', 2, 2, myid, 6, 0 )
4139       ENDIF
4140!
4141!--    Check for negative terrain heights
4142       IF ( ANY( terrain_height_f%var < 0.0_wp ) )  THEN
4143          message_string = 'NetCDF variable zt is not ' //                     &
4144                           'allowed to have negative values'
4145          CALL message( 'netcdf_data_input_mod', 'PA0551', 2, 2, myid, 6, 0 )
4146       ENDIF
4147!
4148!--    If 3D buildings are read, check if building information is consistent
4149!--    to numeric grid.
4150       IF ( buildings_f%from_file )  THEN
4151          IF ( buildings_f%lod == 2 )  THEN
4152             IF ( buildings_f%nz > SIZE( zu ) )  THEN
4153                message_string = 'Reading 3D building data - too much ' //     &
4154                                 'data points along the vertical coordinate.'
4155                CALL message( 'netcdf_data_input_mod', 'PA0552', 2, 2, 0, 6, 0 )
4156             ENDIF
4157
4158             IF ( ANY( ABS( buildings_f%z(0:buildings_f%nz-1) -                &
4159                       zu(0:buildings_f%nz-1) ) > 1E-6_wp ) )  THEN
4160                message_string = 'Reading 3D building data - vertical ' //     &
4161                                 'coordinate do not match numeric grid.'
4162                CALL message( 'netcdf_data_input_mod', 'PA0553', 2, 2, myid, 6, 0 )
4163             ENDIF
4164          ENDIF
4165       ENDIF
4166
4167!
4168!--    Skip further checks concerning buildings and natural surface properties
4169!--    if no urban surface and land surface model are applied.
4170       IF (  .NOT. land_surface  .AND.  .NOT. urban_surface )  RETURN
4171!
4172!--    Check for minimum requirement of surface-classification data in case
4173!--    static input file is used.
4174       IF ( ( .NOT. vegetation_type_f%from_file  .OR.                          &
4175              .NOT. pavement_type_f%from_file    .OR.                          &
4176              .NOT. water_type_f%from_file       .OR.                          &
4177              .NOT. soil_type_f%from_file             ) .OR.                   &
4178             ( urban_surface  .AND.  .NOT. building_type_f%from_file ) )  THEN
4179          message_string = 'Minimum requirement for surface classification ' //&
4180                           'is not fulfilled. At least ' //                    &
4181                           'vegetation_type, pavement_type, ' //               &
4182                           'soil_type and water_type are '//                   &
4183                           'required. If urban-surface model is applied, ' //  &
4184                           'also building_type is required'
4185          CALL message( 'netcdf_data_input_mod', 'PA0554', 1, 2, 0, 6, 0 )
4186       ENDIF
4187!
4188!--    Check for general availability of input variables.
4189!--    If vegetation_type is 0 at any location, vegetation_pars as well as
4190!--    root_area_dens_s are required.
4191       IF ( vegetation_type_f%from_file )  THEN
4192          IF ( ANY( vegetation_type_f%var == 0 ) )  THEN
4193             IF ( .NOT. vegetation_pars_f%from_file )  THEN
4194                message_string = 'If vegetation_type = 0 at any location, ' // &
4195                                 'vegetation_pars is required'
4196                CALL message( 'netcdf_data_input_mod', 'PA0555', 2, 2, -1, 6, 0 )
4197             ENDIF
4198             IF ( .NOT. root_area_density_lsm_f%from_file )  THEN
4199                message_string = 'If vegetation_type = 0 at any location, ' // &
4200                                 'root_area_dens_s is required'
4201                CALL message( 'netcdf_data_input_mod', 'PA0556', 2, 2, myid, 6, 0 )
4202             ENDIF
4203          ENDIF
4204       ENDIF
4205!
4206!--    If soil_type is zero at any location, soil_pars is required.
4207       IF ( soil_type_f%from_file )  THEN
4208          check_passed = .TRUE.
4209          IF ( ALLOCATED( soil_type_f%var_2d ) )  THEN
4210             IF ( ANY( soil_type_f%var_2d == 0 ) )  THEN
4211                IF ( .NOT. soil_pars_f%from_file )  check_passed = .FALSE.
4212             ENDIF
4213          ELSE
4214             IF ( ANY( soil_type_f%var_3d == 0 ) )  THEN
4215                IF ( .NOT. soil_pars_f%from_file )  check_passed = .FALSE.
4216             ENDIF
4217          ENDIF
4218          IF ( .NOT. check_passed )  THEN
4219             message_string = 'If soil_type = 0 at any location, ' //          &
4220                              'soil_pars is required'
4221             CALL message( 'netcdf_data_input_mod', 'PA0557', 2, 2, myid, 6, 0 )
4222          ENDIF
4223       ENDIF
4224!
4225!--    Buildings require a type in case of urban-surface model.
4226       IF ( buildings_f%from_file  .AND.  .NOT. building_type_f%from_file  )  THEN
4227          message_string = 'If buildings are provided, also building_type ' // &
4228                           'is required'
4229          CALL message( 'netcdf_data_input_mod', 'PA0581', 2, 2, myid, 6, 0 )
4230       ENDIF
4231!
4232!--    Buildings require an ID.
4233       IF ( buildings_f%from_file  .AND.  .NOT. building_id_f%from_file  )  THEN
4234          message_string = 'If buildings are provided, also building_id ' //   &
4235                           'is required'
4236          CALL message( 'netcdf_data_input_mod', 'PA0582', 2, 2, myid, 6, 0 )
4237       ENDIF
4238!
4239!--    If building_type is zero at any location, building_pars is required.
4240       IF ( building_type_f%from_file )  THEN
4241          IF ( ANY( building_type_f%var == 0 ) )  THEN
4242             IF ( .NOT. building_pars_f%from_file )  THEN
4243                message_string = 'If building_type = 0 at any location, ' //   &
4244                                 'building_pars is required'
4245                CALL message( 'netcdf_data_input_mod', 'PA0558', 2, 2, myid, 6, 0 )
4246             ENDIF
4247          ENDIF
4248       ENDIF
4249!
4250!--    If building_type is provided, also building_id is needed (due to the
4251!--    filtering algorithm).
4252       IF ( building_type_f%from_file  .AND.  .NOT. building_id_f%from_file )  &
4253       THEN
4254          message_string = 'If building_type is provided, also building_id '// &
4255                           'is required'
4256          CALL message( 'netcdf_data_input_mod', 'PA0519', 2, 2, myid, 6, 0 )
4257       ENDIF       
4258!
4259!--    If albedo_type is zero at any location, albedo_pars is required.
4260       IF ( albedo_type_f%from_file )  THEN
4261          IF ( ANY( albedo_type_f%var == 0 ) )  THEN
4262             IF ( .NOT. albedo_pars_f%from_file )  THEN
4263                message_string = 'If albedo_type = 0 at any location, ' //     &
4264                                 'albedo_pars is required'
4265                CALL message( 'netcdf_data_input_mod', 'PA0559', 2, 2, myid, 6, 0 )
4266             ENDIF
4267          ENDIF
4268       ENDIF
4269!
4270!--    If pavement_type is zero at any location, pavement_pars is required.
4271       IF ( pavement_type_f%from_file )  THEN
4272          IF ( ANY( pavement_type_f%var == 0 ) )  THEN
4273             IF ( .NOT. pavement_pars_f%from_file )  THEN
4274                message_string = 'If pavement_type = 0 at any location, ' //   &
4275                                 'pavement_pars is required'
4276                CALL message( 'netcdf_data_input_mod', 'PA0560', 2, 2, myid, 6, 0 )
4277             ENDIF
4278          ENDIF
4279       ENDIF
4280!
4281!--    If pavement_type is zero at any location, also pavement_subsurface_pars
4282!--    is required.
4283       IF ( pavement_type_f%from_file )  THEN
4284          IF ( ANY( pavement_type_f%var == 0 ) )  THEN
4285             IF ( .NOT. pavement_subsurface_pars_f%from_file )  THEN
4286                message_string = 'If pavement_type = 0 at any location, ' //   &
4287                                 'pavement_subsurface_pars is required'
4288                CALL message( 'netcdf_data_input_mod', 'PA0561', 2, 2, myid, 6, 0 )
4289             ENDIF
4290          ENDIF
4291       ENDIF
4292!
4293!--    If water_type is zero at any location, water_pars is required.
4294       IF ( water_type_f%from_file )  THEN
4295          IF ( ANY( water_type_f%var == 0 ) )  THEN
4296             IF ( .NOT. water_pars_f%from_file )  THEN
4297                message_string = 'If water_type = 0 at any location, ' //      &
4298                                 'water_pars is required'
4299                CALL message( 'netcdf_data_input_mod', 'PA0562', 2, 2,myid, 6, 0 )
4300             ENDIF
4301          ENDIF
4302       ENDIF
4303!
4304!--    Check for local consistency of the input data.
4305       DO  i = nxl, nxr
4306          DO  j = nys, nyn
4307!
4308!--          For each (y,x)-location at least one of the parameters
4309!--          vegetation_type, pavement_type, building_type, or water_type
4310!--          must be set to a non­missing value.
4311             IF ( land_surface  .AND.  .NOT. urban_surface )  THEN
4312                IF ( vegetation_type_f%var(j,i) == vegetation_type_f%fill  .AND.&
4313                     pavement_type_f%var(j,i)   == pavement_type_f%fill    .AND.&
4314                     water_type_f%var(j,i)      == water_type_f%fill )  THEN
4315                   WRITE( message_string, * )                                  &
4316                                    'At least one of the parameters '//        &
4317                                    'vegetation_type, pavement_type, '     //  &
4318                                    'or water_type must be set '//             &
4319                                    'to a non-missing value. Grid point: ', j, i
4320                   CALL message( 'netcdf_data_input_mod', 'PA0563', 2, 2, myid, 6, 0 )
4321                ENDIF
4322             ELSEIF ( land_surface  .AND.  urban_surface )  THEN
4323                IF ( vegetation_type_f%var(j,i) == vegetation_type_f%fill  .AND.&
4324                     pavement_type_f%var(j,i)   == pavement_type_f%fill    .AND.&
4325                     building_type_f%var(j,i)   == building_type_f%fill    .AND.&
4326                     water_type_f%var(j,i)      == water_type_f%fill )  THEN
4327                   WRITE( message_string, * )                                  &
4328                                 'At least one of the parameters '//           &
4329                                 'vegetation_type, pavement_type, '  //        &
4330                                 'building_type, or water_type must be set '// &
4331                                 'to a non-missing value. Grid point: ', j, i
4332                   CALL message( 'netcdf_data_input_mod', 'PA0563', 2, 2, myid, 6, 0 )
4333                ENDIF
4334             ENDIF
4335               
4336!
4337!--          Note that a soil_type is required for each location (y,x) where
4338!--          either vegetation_type or pavement_type is a non­missing value.
4339             IF ( ( vegetation_type_f%var(j,i) /= vegetation_type_f%fill  .OR. &
4340                    pavement_type_f%var(j,i)   /= pavement_type_f%fill ) )  THEN
4341                check_passed = .TRUE.
4342                IF ( ALLOCATED( soil_type_f%var_2d ) )  THEN
4343                   IF ( soil_type_f%var_2d(j,i) == soil_type_f%fill )          &
4344                      check_passed = .FALSE.
4345                ELSE
4346                   IF ( ANY( soil_type_f%var_3d(:,j,i) == soil_type_f%fill) )  &
4347                      check_passed = .FALSE.
4348                ENDIF
4349
4350                IF ( .NOT. check_passed )  THEN
4351                   message_string = 'soil_type is required for each '//        &
4352                                 'location (y,x) where vegetation_type or ' // &
4353                                 'pavement_type is a non-missing value.'
4354                   CALL message( 'netcdf_data_input_mod', 'PA0564',            &
4355                                  2, 2, myid, 6, 0 )
4356                ENDIF
4357             ENDIF
4358!
4359!--          Check for consistency of surface fraction. If more than one type
4360!--          is set, surface fraction need to be given and the sum must not
4361!--          be larger than 1.
4362             n_surf = 0
4363             IF ( vegetation_type_f%var(j,i) /= vegetation_type_f%fill )       &
4364                n_surf = n_surf + 1
4365             IF ( water_type_f%var(j,i)      /= water_type_f%fill )            &
4366                n_surf = n_surf + 1
4367             IF ( pavement_type_f%var(j,i)   /= pavement_type_f%fill )         &
4368                n_surf = n_surf + 1
4369
4370             IF ( n_surf > 1 )  THEN
4371                IF ( .NOT. surface_fraction_f%from_file )  THEN
4372                   message_string = 'If more than one surface type is ' //     &
4373                                 'given at a location, surface_fraction ' //   &
4374                                 'must be provided.'
4375                   CALL message( 'netcdf_data_input_mod', 'PA0565',            &
4376                                  2, 2, myid, 6, 0 )
4377                ELSEIF ( ANY ( surface_fraction_f%frac(:,j,i) ==               &
4378                               surface_fraction_f%fill ) )  THEN
4379                   message_string = 'If more than one surface type is ' //     &
4380                                 'given at a location, surface_fraction ' //   &
4381                                 'must be provided.'
4382                   CALL message( 'netcdf_data_input_mod', 'PA0565',            &
4383                                  2, 2, myid, 6, 0 )
4384                ENDIF
4385             ENDIF
4386!
4387!--          Check for further mismatches. e.g. relative fractions exceed 1 or
4388!--          vegetation_type is set but surface vegetation fraction is zero,
4389!--          etc..
4390             IF ( surface_fraction_f%from_file )  THEN
4391!
4392!--             Sum of relative fractions must not exceed 1.
4393                IF ( SUM ( surface_fraction_f%frac(0:2,j,i) ) > 1.0_wp )  THEN
4394                   message_string = 'surface_fraction must not exceed 1'
4395                   CALL message( 'netcdf_data_input_mod', 'PA0566',            &
4396                                  2, 2, myid, 6, 0 )
4397                ENDIF
4398!
4399!--             Relative fraction for a type must not be zero at locations where
4400!--             this type is set.
4401                IF (                                                           &
4402                  ( vegetation_type_f%var(j,i) /= vegetation_type_f%fill  .AND.&
4403                 ( surface_fraction_f%frac(ind_veg_wall,j,i) == 0.0_wp .OR.    &
4404                   surface_fraction_f%frac(ind_veg_wall,j,i) ==                &
4405                                                     surface_fraction_f%fill ) &
4406                  )  .OR.                                                      &
4407                  ( pavement_type_f%var(j,i) /= pavement_type_f%fill     .AND. &
4408                 ( surface_fraction_f%frac(ind_pav_green,j,i) == 0.0_wp .OR.   &
4409                   surface_fraction_f%frac(ind_pav_green,j,i) ==               &
4410                                                     surface_fraction_f%fill ) &
4411                  )  .OR.                                                      &
4412                  ( water_type_f%var(j,i) /= water_type_f%fill           .AND. &
4413                 ( surface_fraction_f%frac(ind_wat_win,j,i) == 0.0_wp .OR.     &
4414                   surface_fraction_f%frac(ind_wat_win,j,i) ==                 &
4415                                                     surface_fraction_f%fill ) &
4416                  ) )  THEN
4417                   WRITE( message_string, * ) 'Mismatch in setting of '     // &
4418                             'surface_fraction. Vegetation-, pavement-, or '// &
4419                             'water surface is given at (i,j) = ( ', i, j,     &
4420                             ' ), but surface fraction is 0 for the given type.'
4421                   CALL message( 'netcdf_data_input_mod', 'PA0567',            &
4422                                  2, 2, myid, 6, 0 )
4423                ENDIF
4424!
4425!--             Relative fraction for a type must not contain non-zero values
4426!--             if this type is not set.
4427                IF (                                                           &
4428                  ( vegetation_type_f%var(j,i) == vegetation_type_f%fill  .AND.&
4429                 ( surface_fraction_f%frac(ind_veg_wall,j,i) /= 0.0_wp .AND.   &
4430                   surface_fraction_f%frac(ind_veg_wall,j,i) /=                &
4431                                                     surface_fraction_f%fill ) &
4432                  )  .OR.                                                      &
4433                  ( pavement_type_f%var(j,i) == pavement_type_f%fill     .AND. &
4434                 ( surface_fraction_f%frac(ind_pav_green,j,i) /= 0.0_wp .AND.  &
4435                   surface_fraction_f%frac(ind_pav_green,j,i) /=               &
4436                                                     surface_fraction_f%fill ) &
4437                  )  .OR.                                                      &
4438                  ( water_type_f%var(j,i) == water_type_f%fill           .AND. &
4439                 ( surface_fraction_f%frac(ind_wat_win,j,i) /= 0.0_wp .AND.    &
4440                   surface_fraction_f%frac(ind_wat_win,j,i) /=                 &
4441                                                     surface_fraction_f%fill ) &
4442