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

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

changes from last commit documented

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