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

Last change on this file since 3429 was 3429, checked in by knoop, 6 years ago

Bugfix: allow surface type allocations even if only one of "land_surface" or "urban_surface" is true

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