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

Last change on this file since 3961 was 3961, checked in by suehring, 5 years ago

Revise check

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