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

Last change on this file since 3472 was 3472, checked in by suehring, 6 years ago

module for virtual measurements added (in a preliminary state); new public routines to input NetCDF data directly from modules

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