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

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

Branch salsa @3446 re-integrated into trunk

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