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

Last change on this file since 3542 was 3542, checked in by suehring, 4 years ago

Revise ghost point exchange in netcdf-data input; new routine for ghost point exchange of 1-Byte Integer; Remove tabs in chemistry model which prevent compilation with gfortran and debug options

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