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

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

Move checks for correct dimensions in static input file; improve checks concerning buildings; check whether at least one surface type is set at a natural-type surface element

  • Property svn:keywords set to Id
File size: 282.6 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-2019 Leibniz Universitaet Hannover
18!------------------------------------------------------------------------------!
19!
20! Current revisions:
21! -----------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: netcdf_data_input_mod.f90 3941 2019-04-30 09:48:33Z suehring $
27! Move check for grid dimension to an earlier point in time when first array
28! is read.
29! Improve checks for building types / IDs with respect to 2D/3D buildings.
30!
31! 3885 2019-04-11 11:29:34Z kanani
32! Changes related to global restructuring of location messages and introduction
33! of additional debug messages
34!
35! 3864 2019-04-05 09:01:56Z monakurppa
36! get_variable_4d_to_3d_real modified to enable read in data of type
37! data(t,y,x,n) one timestep at a time + some routines made public
38!
39! 3855 2019-04-03 10:00:59Z suehring
40! Typo removed
41!
42! 3854 2019-04-02 16:59:33Z suehring
43! Bugfix in one of the checks. Typo removed.
44!
45! 3744 2019-02-15 18:38:58Z suehring
46! Enable mesoscale offline nesting for chemistry variables as well as
47! initialization of chemistry via dynamic input file.
48!
49! 3705 2019-01-29 19:56:39Z suehring
50! Interface for attribute input of 8-bit and 32-bit integer
51!
52! 3704 2019-01-29 19:51:41Z suehring
53! unused variables removed
54!
55! 3560 2018-11-23 09:20:21Z raasch
56! Some formatting adjustment
57!
58! 3556 2018-11-22 14:11:57Z suehring
59! variables documented and unused variables removed
60!
61! 3552 2018-11-22 10:28:35Z suehring
62! Revise ghost point exchange and resizing of input variables
63!
64! 3542 2018-11-20 17:04:13Z suehring
65! - read optional attributes from file
66! - set default origin_time
67!
68! 3518 2018-11-12 18:10:23Z suehring
69! Additional checks
70!
71! 3516 2018-11-12 15:49:39Z gronemeier
72! bugfix: - difference in z coordinate between file and PALM must be <1e-6
73!         - output of error 553 for all PEs
74!
75! 3498 2018-11-07 10:53:03Z gronemeier
76! Bugfix: print error message by processor which encounters the error
77!
78! 3485 2018-11-03 17:09:40Z gronemeier
79! - get central meridian from origin_lon if crs does not exist
80! - set default origin_lon to 0
81!
82! 3483 2018-11-02 14:19:26Z raasch
83! bugfix: misplaced directives for netCDF fixed
84!
85! 3474 2018-10-30 21:07:39Z kanani
86! Add UV exposure model input (Schrempf)
87!
88! 3472 2018-10-30 20:43:50Z suehring
89! Salsa implemented
90!
91! 3464 2018-10-30 18:08:55Z kanani
92! Define coordinate reference system (crs) and read from input dataset
93! Revise default values for reference coordinates
94!
95! 3459 2018-10-30 15:04:11Z gronemeier
96! from chemistry branch r3443, banzhafs, Russo:
97! Uncommented lines on dimension of surface_fractions
98! Removed par_emis_time_factor variable, moved to chem_emissions_mod
99! Initialized nspec and other emission variables at time of declaration
100! Modified EXPERT mode to PRE-PROCESSED mode
101! Introduced Chemistry static netcdf file
102! Added the routine for reading-in netcdf data for chemistry
103! Added routines to get_variable interface specific for chemistry files
104!
105! 3429 2018-10-25 13:04:23Z knoop
106! add default values of origin_x/y/z
107!
108! 3404 2018-10-23 13:29:11Z suehring
109! Consider time-dependent geostrophic wind components in offline nesting
110!
111! 3376 2018-10-19 10:15:32Z suehring
112! Additional check for consistent building initialization implemented
113!
114! 3347 2018-10-15 14:21:08Z suehring
115! Subroutine renamed
116!
117! 3257 2018-09-17 17:11:46Z suehring
118! (from branch resler)
119! Formatting
120!
121! 3298 2018-10-02 12:21:11Z kanani
122! Modified EXPERT mode to PRE-PROCESSED mode (Russo)
123! Introduced Chemistry static netcdf file (Russo)
124! Added the routine for reading-in netcdf data for chemistry (Russo)
125! Added routines to get_variable interface specific for chemistry files (Russo)
126!
127! 3257 2018-09-17 17:11:46Z suehring
128! Adjust checks for building_type and building_id, which is necessary after
129! topography filtering (building_type and id can be modified by the filtering).
130!
131! 3254 2018-09-17 10:53:57Z suehring
132! Additional check for surface_fractions and and checks for building_id and
133! building_type extended.
134!
135! 3241 2018-09-12 15:02:00Z raasch
136! unused variables removed
137!
138! 3215 2018-08-29 09:58:59Z suehring
139! - Separate input of soil properties from input of atmospheric data. This
140!   enables input of soil properties also in child domains without any
141!   dependence on atmospheric input
142! - Check for missing initial 1D/3D data in dynamic input file
143! - Revise checks for matching grid spacing in model and input file
144! - Bugfix, add netcdf4_parallel directive for collective read operation
145! - Revise error message numbers
146!
147! 3209 2018-08-27 16:58:37Z suehring
148! Read zsoil dimension length only if soil variables are provided
149!
150! 3183 2018-07-27 14:25:55Z suehring
151! Adjust input of dynamic driver according to revised Inifor version.
152! Replace simulated_time by time_since_reference_point.
153! Rename variables in mesoscale-offline nesting mode.
154!
155! 3182 2018-07-27 13:36:03Z suehring
156! Slightly revise check for surface_fraction in order to check only the relevant
157! fractions
158!
159! 3103 2018-07-04 17:30:52Z suehring
160! New check for negative terrain heights
161!
162! 3089 2018-06-27 13:20:38Z suehring
163! Revise call for message routine in case of local data inconsistencies.
164!
165! 3054 2018-06-01 16:08:59Z gronemeier
166! Bugfix: force an MPI abort if errors occur while reading building heights
167! from ASCII file
168!
169! 3053 2018-06-01 12:59:07Z suehring
170! Revise checks for variable surface_fraction
171!
172! 3051 2018-05-30 17:43:55Z suehring
173! - Speed-up NetCDF input
174! - Revise input routines and remove NetCDF input via IO-blocks since this is
175!   not working in parallel mode in case blocking collective read operations
176!   are done
177! - Temporarily revoke renaming of input variables in dynamic driver (tend_ug,
178!   tend_vg, zsoil) in order to keep dynamic input file working with current
179!   model version
180! - More detailed error messages created
181!
182! 3045 2018-05-28 07:55:41Z Giersch
183! Error messages revised
184!
185! 3041 2018-05-25 10:39:54Z gronemeier
186! Add data type for global file attributes
187! Add read of global attributes of static driver
188!
189! 3037 2018-05-24 10:39:29Z gronemeier
190! renamed 'depth' to 'zsoil'
191!
192! 3036 2018-05-24 10:18:26Z gronemeier
193! Revision of input vars according to UC2 data standard
194!  - renamed 'orography_2D' to 'zt'
195!  - renamed 'buildings_2D' to 'buildings_2d'
196!  - renamed 'buildings_3D' to 'buildings_3d'
197!  - renamed 'leaf_are_density' to 'lad'
198!  - renamed 'basal_are_density' to 'bad'
199!  - renamed 'root_are_density_lad' to 'root_area_dens_r'
200!  - renamed 'root_are_density_lsm' to 'root_area_dens_s'
201!  - renamed 'ls_forcing_ug' to 'tend_ug'
202!  - renamed 'ls_forcing_vg' to 'tend_vg'
203!
204! 3019 2018-05-13 07:05:43Z maronga
205! Improved reading speed of large NetCDF files
206!
207! 2963 2018-04-12 14:47:44Z suehring
208! - Revise checks for static input variables.
209! - Introduce index for vegetation/wall, pavement/green-wall and water/window
210!   surfaces, for clearer access of surface fraction, albedo, emissivity, etc. .
211!
212! 2958 2018-04-11 15:38:13Z suehring
213! Synchronize longitude and latitude between nested model domains, values are
214! taken from the root model.
215!
216! 2955 2018-04-09 15:14:01Z suehring
217! Extend checks for consistent setting of buildings, its ID and type.
218! Add log-points to measure CPU time of NetCDF data input.
219!
220! 2953 2018-04-09 11:26:02Z suehring
221! Bugfix in checks for initialization data
222!
223! 2947 2018-04-04 18:01:41Z suehring
224! Checks for dynamic input revised
225!
226! 2946 2018-04-04 17:01:23Z suehring
227! Bugfix for revision 2945, perform checks only if dynamic input file is
228! available.
229!
230! 2945 2018-04-04 16:27:14Z suehring
231! - Mimic for topography input slightly revised, in order to enable consistency
232!   checks
233! - Add checks for dimensions in dynamic input file and move already existing
234!   checks
235!
236! 2938 2018-03-27 15:52:42Z suehring
237! Initial read of geostrophic wind components from dynamic driver.
238!
239! 2773 2018-01-30 14:12:54Z suehring
240! Revise checks for surface_fraction.
241!
242! 2925 2018-03-23 14:54:11Z suehring
243! Check for further inconsistent settings of surface_fractions.
244! Some messages slightly rephrased and error numbers renamed.
245!
246! 2898 2018-03-15 13:03:01Z suehring
247! Check if each building has a type. Further, check if dimensions in static
248! input file match the model dimensions.
249!
250! 2897 2018-03-15 11:47:16Z suehring
251! Relax restrictions for topography input, terrain and building heights can be
252! input separately and are not mandatory any more.
253!
254! 2874 2018-03-13 10:55:42Z knoop
255! Bugfix: wrong placement of netcdf cpp-macros fixed
256!
257! 2794 2018-02-07 14:09:43Z knoop
258! Check if 3D building input is consistent to numeric grid.
259!
260! 2773 2018-01-30 14:12:54Z suehring
261! - Enable initialization with 3D topography.
262! - Move check for correct initialization in nesting mode to check_parameters.
263!
264! 2772 2018-01-29 13:10:35Z suehring
265! Initialization of simulation independent on land-surface model.
266!
267! 2746 2018-01-15 12:06:04Z suehring
268! Read plant-canopy variables independently on land-surface model usage
269!
270! 2718 2018-01-02 08:49:38Z maronga
271! Corrected "Former revisions" section
272!
273! 2711 2017-12-20 17:04:49Z suehring
274! Rename subroutine close_file to avoid double-naming.
275!
276! 2700 2017-12-15 14:12:35Z suehring
277!
278! 2696 2017-12-14 17:12:51Z kanani
279! Initial revision (suehring)
280!
281!
282!
283!
284! Authors:
285! --------
286! @author Matthias Suehring
287!
288! Description:
289! ------------
290!> Modulue contains routines to input data according to Palm input data
291!> standart using dynamic and static input files.
292!> @todo - Chemistry: revise reading of netcdf file and ajdust formatting according to standard!!!
293!> @todo - Order input alphabetically
294!> @todo - Revise error messages and error numbers
295!> @todo - Input of missing quantities (chemical species, emission rates)
296!> @todo - Defninition and input of still missing variable attributes
297!> @todo - Input of initial geostrophic wind profiles with cyclic conditions.
298!------------------------------------------------------------------------------!
299 MODULE netcdf_data_input_mod
300
301    USE control_parameters,                                                    &
302        ONLY:  coupling_char, io_blocks, io_group
303
304    USE cpulog,                                                                &
305        ONLY:  cpu_log, log_point_s
306
307    USE indices,                                                               &
308        ONLY:  nbgp
309
310    USE kinds
311
312#if defined ( __netcdf )
313    USE NETCDF
314#endif
315
316    USE pegrid
317
318    USE surface_mod,                                                           &
319        ONLY:  ind_pav_green, ind_veg_wall, ind_wat_win
320!
321!-- Define type for dimensions.
322    TYPE dims_xy
323       INTEGER(iwp) :: nx                             !< dimension length in x
324       INTEGER(iwp) :: ny                             !< dimension length in y
325       INTEGER(iwp) :: nz                             !< dimension length in z
326       REAL(wp), DIMENSION(:), ALLOCATABLE :: x       !< dimension array in x
327       REAL(wp), DIMENSION(:), ALLOCATABLE :: y       !< dimension array in y
328       REAL(wp), DIMENSION(:), ALLOCATABLE :: z       !< dimension array in z
329    END TYPE dims_xy
330!
331!-- Define data type for nesting in larger-scale models like COSMO.
332!-- Data type comprises u, v, w, pt, and q at lateral and top boundaries.
333    TYPE nest_offl_type
334
335       CHARACTER(LEN=16) ::  char_l = 'ls_forcing_left_'  !< leading substring for variables at left boundary
336       CHARACTER(LEN=17) ::  char_n = 'ls_forcing_north_' !< leading substring for variables at north boundary 
337       CHARACTER(LEN=17) ::  char_r = 'ls_forcing_right_' !< leading substring for variables at right boundary 
338       CHARACTER(LEN=17) ::  char_s = 'ls_forcing_south_' !< leading substring for variables at south boundary
339       CHARACTER(LEN=15) ::  char_t = 'ls_forcing_top_'   !< leading substring for variables at top boundary
340   
341       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names         !< list of variable in dynamic input file
342       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names_chem_l  !< names of mesoscale nested chemistry variables at left boundary
343       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names_chem_n  !< names of mesoscale nested chemistry variables at north boundary
344       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names_chem_r  !< names of mesoscale nested chemistry variables at right boundary
345       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names_chem_s  !< names of mesoscale nested chemistry variables at south boundary
346       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names_chem_t  !< names of mesoscale nested chemistry variables at top boundary
347
348       INTEGER(iwp) ::  nt     !< number of time levels in dynamic input file
349       INTEGER(iwp) ::  nzu    !< number of vertical levels on scalar grid in dynamic input file
350       INTEGER(iwp) ::  nzw    !< number of vertical levels on w grid in dynamic input file
351       INTEGER(iwp) ::  tind   !< time index for reference time in mesoscale-offline nesting
352       INTEGER(iwp) ::  tind_p !< time index for following time in mesoscale-offline nesting
353
354       LOGICAL      ::  init         = .FALSE. !< flag indicating that offline nesting is already initialized
355       
356       LOGICAL, DIMENSION(:), ALLOCATABLE ::  chem_from_file_l !< flags inidicating whether left boundary data for chemistry is in dynamic input file 
357       LOGICAL, DIMENSION(:), ALLOCATABLE ::  chem_from_file_n !< flags inidicating whether north boundary data for chemistry is in dynamic input file
358       LOGICAL, DIMENSION(:), ALLOCATABLE ::  chem_from_file_r !< flags inidicating whether right boundary data for chemistry is in dynamic input file
359       LOGICAL, DIMENSION(:), ALLOCATABLE ::  chem_from_file_s !< flags inidicating whether south boundary data for chemistry is in dynamic input file
360       LOGICAL, DIMENSION(:), ALLOCATABLE ::  chem_from_file_t !< flags inidicating whether top boundary data for chemistry is in dynamic input file
361
362       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surface_pressure !< time dependent surface pressure
363       REAL(wp), DIMENSION(:), ALLOCATABLE ::  time             !< time levels in dynamic input file
364       REAL(wp), DIMENSION(:), ALLOCATABLE ::  zu_atmos         !< vertical levels at scalar grid in dynamic input file
365       REAL(wp), DIMENSION(:), ALLOCATABLE ::  zw_atmos         !< vertical levels at w grid in dynamic input file
366
367       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  ug         !< domain-averaged geostrophic component
368       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  vg         !< domain-averaged geostrophic component
369
370       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_left   !< u-component at left boundary
371       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_left   !< v-component at left boundary
372       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_left   !< w-component at left boundary
373       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  q_left   !< mixing ratio at left boundary
374       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  pt_left  !< potentital temperautre at left boundary
375
376       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_north  !< u-component at north boundary
377       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_north  !< v-component at north boundary
378       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_north  !< w-component at north boundary
379       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  q_north  !< mixing ratio at north boundary
380       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  pt_north !< potentital temperautre at north boundary
381
382       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_right  !< u-component at right boundary
383       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_right  !< v-component at right boundary
384       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_right  !< w-component at right boundary
385       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  q_right  !< mixing ratio at right boundary
386       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  pt_right !< potentital temperautre at right boundary
387
388       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_south  !< u-component at south boundary
389       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_south  !< v-component at south boundary
390       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_south  !< w-component at south boundary
391       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  q_south  !< mixing ratio at south boundary
392       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  pt_south !< potentital temperautre at south boundary
393
394       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_top    !< u-component at top boundary
395       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_top    !< v-component at top boundary
396       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_top    !< w-component at top boundary
397       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  q_top    !< mixing ratio at top boundary
398       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  pt_top   !< potentital temperautre at top boundary
399       
400       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  chem_left   !< chemical species at left boundary
401       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  chem_north  !< chemical species at left boundary
402       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  chem_right  !< chemical species at left boundary
403       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  chem_south  !< chemical species at left boundary
404       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  chem_top    !< chemical species at left boundary
405
406    END TYPE nest_offl_type
407
408    TYPE init_type
409
410       CHARACTER(LEN=16) ::  init_char = 'init_atmosphere_'          !< leading substring for init variables
411       CHARACTER(LEN=23) ::  origin_time = '2000-01-01 00:00:00 +00' !< reference time of input data
412       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names_chem !< list of chemistry variable names that can potentially be on file
413
414       INTEGER(iwp) ::  lod_msoil !< level of detail - soil moisture
415       INTEGER(iwp) ::  lod_pt    !< level of detail - pt
416       INTEGER(iwp) ::  lod_q     !< level of detail - q
417       INTEGER(iwp) ::  lod_tsoil !< level of detail - soil temperature
418       INTEGER(iwp) ::  lod_u     !< level of detail - u-component
419       INTEGER(iwp) ::  lod_v     !< level of detail - v-component
420       INTEGER(iwp) ::  lod_w     !< level of detail - w-component
421       INTEGER(iwp) ::  nx        !< number of scalar grid points along x in dynamic input file
422       INTEGER(iwp) ::  nxu       !< number of u grid points along x in dynamic input file
423       INTEGER(iwp) ::  ny        !< number of scalar grid points along y in dynamic input file
424       INTEGER(iwp) ::  nyv       !< number of v grid points along y in dynamic input file
425       INTEGER(iwp) ::  nzs       !< number of vertical soil levels in dynamic input file
426       INTEGER(iwp) ::  nzu       !< number of vertical levels on scalar grid in dynamic input file
427       INTEGER(iwp) ::  nzw       !< number of vertical levels on w grid in dynamic input file
428       
429       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  lod_chem !< level of detail - chemistry variables
430
431       LOGICAL ::  from_file_msoil  = .FALSE. !< flag indicating whether soil moisture is already initialized from file
432       LOGICAL ::  from_file_pt     = .FALSE. !< flag indicating whether pt is already initialized from file
433       LOGICAL ::  from_file_q      = .FALSE. !< flag indicating whether q is already initialized from file
434       LOGICAL ::  from_file_tsoil  = .FALSE. !< flag indicating whether soil temperature is already initialized from file
435       LOGICAL ::  from_file_u      = .FALSE. !< flag indicating whether u is already initialized from file
436       LOGICAL ::  from_file_ug     = .FALSE. !< flag indicating whether ug is already initialized from file
437       LOGICAL ::  from_file_v      = .FALSE. !< flag indicating whether v is already initialized from file
438       LOGICAL ::  from_file_vg     = .FALSE. !< flag indicating whether ug is already initialized from file
439       LOGICAL ::  from_file_w      = .FALSE. !< flag indicating whether w is already initialized from file
440       
441       LOGICAL, DIMENSION(:), ALLOCATABLE ::  from_file_chem !< flag indicating whether chemistry variable is read from file
442
443       REAL(wp) ::  fill_msoil              !< fill value for soil moisture
444       REAL(wp) ::  fill_pt                 !< fill value for pt
445       REAL(wp) ::  fill_q                  !< fill value for q
446       REAL(wp) ::  fill_tsoil              !< fill value for soil temperature
447       REAL(wp) ::  fill_u                  !< fill value for u
448       REAL(wp) ::  fill_v                  !< fill value for v
449       REAL(wp) ::  fill_w                  !< fill value for w
450       REAL(wp) ::  latitude = 0.0_wp       !< latitude of the lower left corner
451       REAL(wp) ::  longitude = 0.0_wp      !< longitude of the lower left corner
452       REAL(wp) ::  origin_x = 500000.0_wp  !< UTM easting of the lower left corner
453       REAL(wp) ::  origin_y = 0.0_wp       !< UTM northing of the lower left corner
454       REAL(wp) ::  origin_z = 0.0_wp       !< reference height of input data
455       REAL(wp) ::  rotation_angle = 0.0_wp !< rotation angle of input data
456
457       REAL(wp), DIMENSION(:), ALLOCATABLE ::  fill_chem    !< fill value - chemistry variables
458       REAL(wp), DIMENSION(:), ALLOCATABLE ::  msoil_1d     !< initial vertical profile of soil moisture
459       REAL(wp), DIMENSION(:), ALLOCATABLE ::  pt_init      !< initial vertical profile of pt
460       REAL(wp), DIMENSION(:), ALLOCATABLE ::  q_init       !< initial vertical profile of q
461       REAL(wp), DIMENSION(:), ALLOCATABLE ::  tsoil_1d     !< initial vertical profile of soil temperature
462       REAL(wp), DIMENSION(:), ALLOCATABLE ::  u_init       !< initial vertical profile of u
463       REAL(wp), DIMENSION(:), ALLOCATABLE ::  ug_init      !< initial vertical profile of ug
464       REAL(wp), DIMENSION(:), ALLOCATABLE ::  v_init       !< initial vertical profile of v
465       REAL(wp), DIMENSION(:), ALLOCATABLE ::  vg_init      !< initial vertical profile of ug
466       REAL(wp), DIMENSION(:), ALLOCATABLE ::  w_init       !< initial vertical profile of w
467       REAL(wp), DIMENSION(:), ALLOCATABLE ::  z_soil       !< vertical levels in soil in dynamic input file, used for interpolation
468       REAL(wp), DIMENSION(:), ALLOCATABLE ::  zu_atmos     !< vertical levels at scalar grid in dynamic input file, used for interpolation
469       REAL(wp), DIMENSION(:), ALLOCATABLE ::  zw_atmos     !< vertical levels at w grid in dynamic input file, used for interpolation
470       
471       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  chem_init  !< initial vertical profiles of chemistry variables
472
473
474       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  msoil_3d !< initial 3d soil moisture provide by Inifor and interpolated onto soil grid
475       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  tsoil_3d !< initial 3d soil temperature provide by Inifor and interpolated onto soil grid
476
477    END TYPE init_type
478
479!-- Data type for the general information of chemistry emissions, do not dependent on the particular chemical species
480    TYPE chem_emis_att_type 
481
482       !-DIMENSIONS
483       INTEGER(iwp)                                 :: nspec=0                   !< number of chem species for which emission values are provided
484       INTEGER(iwp)                                 :: ncat=0                    !< number of emission categories
485       INTEGER(iwp)                                 :: nvoc=0                    !< number of VOCs components
486       INTEGER(iwp)                                 :: npm=0                     !< number of PMs components
487       INTEGER(iwp)                                 :: nnox=2                    !< number of NOx components: NO and NO2
488       INTEGER(iwp)                                 :: nsox=2                    !< number of SOx components: SO and SO4
489       INTEGER(iwp)                                 :: nhoursyear                !< number of hours of a specific year in the HOURLY mode
490                                                                                 !  of the default mode
491       INTEGER(iwp)                                 :: nmonthdayhour             !< number of month days and hours in the MDH mode
492                                                                                 !  of the default mode
493       INTEGER(iwp)                                 :: dt_emission               !< Number of emissions timesteps for one year
494                                                                                 !  in the pre-processed emissions case
495       !-- 1d emission input variables
496       CHARACTER (LEN=25),ALLOCATABLE, DIMENSION(:) :: pm_name                   !< Names of PMs components
497       CHARACTER (LEN=25),ALLOCATABLE, DIMENSION(:) :: cat_name                  !< Emission categories names
498       CHARACTER (LEN=25),ALLOCATABLE, DIMENSION(:) :: species_name              !< Names of emission chemical species
499       CHARACTER (LEN=25),ALLOCATABLE, DIMENSION(:) :: voc_name                  !< Names of VOCs components
500       CHARACTER (LEN=25)                           :: units                     !< Units
501
502       INTEGER(iwp)                                 :: i_hour                    !< indices for assigning the emission values at different timesteps
503       INTEGER(iwp),ALLOCATABLE, DIMENSION(:)       :: cat_index                 !< Index of emission categories
504       INTEGER(iwp),ALLOCATABLE, DIMENSION(:)       :: species_index             !< Index of emission chem species
505
506       REAL(wp),ALLOCATABLE, DIMENSION(:)           :: xm                        !< Molecular masses of emission chem species
507
508       !-- 2d emission input variables
509       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: hourly_emis_time_factor   !< Time factors for HOURLY emissions (DEFAULT mode)
510       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: mdh_emis_time_factor      !< Time factors for MDH emissions (DEFAULT mode)
511       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: nox_comp                  !< Composition of NO and NO2
512       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: sox_comp                  !< Composition of SO2 and SO4
513       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: voc_comp                  !< Composition of different VOC components (number not fixed)
514
515       !-- 3d emission input variables
516       REAL(wp),ALLOCATABLE, DIMENSION(:,:,:)       :: pm_comp                   !< Composition of different PMs components (number not fixed)
517 
518    END TYPE chem_emis_att_type
519
520
521!-- Data type for the values of chemistry emissions ERUSSO
522    TYPE chem_emis_val_type 
523
524       !REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: stack_height              !< stack height
525
526       !-- 3d emission input variables
527       REAL(wp),ALLOCATABLE, DIMENSION(:,:,:)     :: default_emission_data     !< Input Values emissions DEFAULT mode
528
529       !-- 4d emission input variables
530       REAL(wp),ALLOCATABLE, DIMENSION(:,:,:,:)   :: preproc_emission_data      !< Input Values emissions PRE-PROCESSED mode
531
532    END TYPE chem_emis_val_type
533
534!
535!-- Define data structures for different input data types.
536!-- 8-bit Integer 2D
537    TYPE int_2d_8bit
538       INTEGER(KIND=1) ::  fill = -127                      !< fill value
539       INTEGER(KIND=1), DIMENSION(:,:), ALLOCATABLE ::  var !< respective variable
540
541       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
542    END TYPE int_2d_8bit
543!
544!-- 8-bit Integer 3D
545    TYPE int_3d_8bit
546       INTEGER(KIND=1) ::  fill = -127                           !< fill value
547       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var_3d !< respective variable
548
549       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
550    END TYPE int_3d_8bit
551!
552!-- 32-bit Integer 2D
553    TYPE int_2d_32bit
554       INTEGER(iwp) ::  fill = -9999                      !< fill value
555       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  var  !< respective variable
556
557       LOGICAL ::  from_file = .FALSE. !< flag indicating whether an input variable is available and read from file or default values are used
558    END TYPE int_2d_32bit
559
560!
561!-- Define data type to read 2D real variables
562    TYPE real_2d
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 ::  var !< respective variable
567    END TYPE real_2d
568
569!
570!-- Define data type to read 3D real variables
571    TYPE real_3d
572       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
573
574       INTEGER(iwp) ::  nz   !< number of grid points along vertical dimension
575
576       REAL(wp) ::  fill = -9999.9_wp                  !< fill value
577       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  var !< respective variable
578    END TYPE real_3d
579!
580!-- Define data structure where the dimension and type of the input depends
581!-- on the given level of detail.
582!-- For buildings, the input is either 2D float, or 3d byte.
583    TYPE build_in
584       INTEGER(iwp)    ::  lod = 1                               !< level of detail
585       INTEGER(KIND=1) ::  fill2 = -127                          !< fill value for lod = 2
586       INTEGER(iwp)    ::  nz                                    !< number of vertical layers in file
587       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var_3d !< 3d variable (lod = 2)
588
589       REAL(wp), DIMENSION(:), ALLOCATABLE ::  z                 !< vertical coordinate for 3D building, used for consistency check
590
591       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
592
593       REAL(wp)                              ::  fill1 = -9999.9_wp !< fill values for lod = 1
594       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  var_2d             !< 2d variable (lod = 1)
595    END TYPE build_in
596
597!
598!-- For soil_type, the input is either 2D or 3D one-byte integer.
599    TYPE soil_in
600       INTEGER(iwp)                                   ::  lod = 1      !< level of detail
601       INTEGER(KIND=1)                                ::  fill = -127  !< fill value for lod = 2
602       INTEGER(KIND=1), DIMENSION(:,:), ALLOCATABLE   ::  var_2d       !< 2d variable (lod = 1)
603       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var_3d       !< 3d variable (lod = 2)
604
605       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
606    END TYPE soil_in
607
608!
609!-- Define data type for fractions between surface types
610    TYPE fracs
611       INTEGER(iwp)                            ::  nf             !< total number of fractions
612       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nfracs         !< dimension array for fraction
613
614       LOGICAL ::  from_file = .FALSE. !< flag indicating whether an input variable is available and read from file or default values are used
615
616       REAL(wp)                                ::  fill = -9999.9_wp !< fill value
617       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  frac              !< respective fraction between different surface types
618    END TYPE fracs
619!
620!-- Data type for parameter lists, Depending on the given level of detail,
621!-- the input is 3D or 4D
622    TYPE pars
623       INTEGER(iwp)                            ::  lod = 1         !< level of detail
624       INTEGER(iwp)                            ::  np              !< total number of parameters
625       INTEGER(iwp)                            ::  nz              !< vertical dimension - number of soil layers
626       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  layers          !< dimension array for soil layers
627       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  pars            !< dimension array for parameters
628
629       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
630
631       REAL(wp)                                  ::  fill = -9999.9_wp !< fill value
632       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE   ::  pars_xy           !< respective parameters, level of detail = 1
633       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  pars_xyz          !< respective parameters, level of detail = 2
634    END TYPE pars
635!
636!-- Define type for global file attributes
637!-- Please refer to the PALM data standard for a detailed description of each
638!-- attribute.
639    TYPE global_atts_type
640       CHARACTER(LEN=12 ) ::  acronym                            !< acronym of institution
641       CHARACTER(LEN=7)   ::  acronym_char = 'acronym'           !< name of attribute
642       CHARACTER(LEN=200) ::  author                             !< first name, last name, email adress
643       CHARACTER(LEN=6)   ::  author_char = 'author'             !< name of attribute
644       CHARACTER(LEN=12 ) ::  campaign = 'PALM-4U'               !< name of campaign
645       CHARACTER(LEN=8)   ::  campaign_char = 'campaign'         !< name of attribute
646       CHARACTER(LEN=200) ::  comment                            !< comment to data
647       CHARACTER(LEN=7)   ::  comment_char = 'comment'           !< name of attribute
648       CHARACTER(LEN=200) ::  contact_person                     !< first name, last name, email adress
649       CHARACTER(LEN=14)  ::  contact_person_char = 'contact_person'  !< name of attribute
650       CHARACTER(LEN=200) ::  conventions = 'CF-1.7'             !< netCDF convention
651       CHARACTER(LEN=11)  ::  conventions_char = 'Conventions'   !< name of attribute
652       CHARACTER(LEN=23 ) ::  creation_time                      !< creation time of data set
653       CHARACTER(LEN=13)  ::  creation_time_char = 'creation_time'  !< name of attribute
654       CHARACTER(LEN=16 ) ::  data_content                       !< content of data set
655       CHARACTER(LEN=12)  ::  data_content_char = 'data_content' !< name of attribute
656       CHARACTER(LEN=200) ::  dependencies                       !< dependencies of data set
657       CHARACTER(LEN=12)  ::  dependencies_char = 'dependencies' !< name of attribute
658       CHARACTER(LEN=200) ::  history                            !< information about data processing
659       CHARACTER(LEN=7)   ::  history_char = 'history'           !< name of attribute
660       CHARACTER(LEN=200) ::  institution                        !< name of responsible institution
661       CHARACTER(LEN=11)  ::  institution_char = 'institution'   !< name of attribute
662       CHARACTER(LEN=200) ::  keywords                           !< keywords of data set
663       CHARACTER(LEN=8)   ::  keywords_char = 'keywords'         !< name of attribute
664       CHARACTER(LEN=200) ::  licence                            !< licence of data set
665       CHARACTER(LEN=7)   ::  licence_char = 'licence'           !< name of attribute
666       CHARACTER(LEN=200) ::  location                           !< place which refers to data set
667       CHARACTER(LEN=8)   ::  location_char = 'location'         !< name of attribute
668       CHARACTER(LEN=10)  ::  origin_lat_char = 'origin_lat'     !< name of attribute
669       CHARACTER(LEN=10)  ::  origin_lon_char = 'origin_lon'     !< name of attribute
670       CHARACTER(LEN=23 ) ::  origin_time = '2000-01-01 00:00:00 +00'  !< reference time
671       CHARACTER(LEN=11)  ::  origin_time_char = 'origin_time'   !< name of attribute
672       CHARACTER(LEN=8)   ::  origin_x_char = 'origin_x'         !< name of attribute
673       CHARACTER(LEN=8)   ::  origin_y_char = 'origin_y'         !< name of attribute
674       CHARACTER(LEN=8)   ::  origin_z_char = 'origin_z'         !< name of attribute
675       CHARACTER(LEN=12)  ::  palm_version_char = 'palm_version' !< name of attribute
676       CHARACTER(LEN=200) ::  references                         !< literature referring to data set
677       CHARACTER(LEN=10)  ::  references_char = 'references'     !< name of attribute
678       CHARACTER(LEN=14)  ::  rotation_angle_char = 'rotation_angle'  !< name of attribute
679       CHARACTER(LEN=12 ) ::  site                               !< name of model domain
680       CHARACTER(LEN=4)   ::  site_char = 'site'                 !< name of attribute
681       CHARACTER(LEN=200) ::  source                             !< source of data set
682       CHARACTER(LEN=6)   ::  source_char = 'source'             !< name of attribute
683       CHARACTER(LEN=200) ::  title                              !< title of data set
684       CHARACTER(LEN=5)   ::  title_char = 'title'               !< name of attribute
685       CHARACTER(LEN=7)   ::  version_char = 'version'           !< name of attribute
686
687       INTEGER(iwp) ::  version              !< version of data set
688
689       REAL(wp) ::  origin_lat               !< latitude of lower left corner
690       REAL(wp) ::  origin_lon               !< longitude of lower left corner
691       REAL(wp) ::  origin_x                 !< easting (UTM coordinate) of lower left corner
692       REAL(wp) ::  origin_y                 !< northing (UTM coordinate) of lower left corner
693       REAL(wp) ::  origin_z                 !< reference height
694       REAL(wp) ::  palm_version             !< PALM version of data set
695       REAL(wp) ::  rotation_angle           !< rotation angle of coordinate system of data set
696    END TYPE global_atts_type
697!
698!-- Define type for coordinate reference system (crs)
699    TYPE crs_type
700       CHARACTER(LEN=200) ::  epsg_code = 'EPSG:25831'                   !< EPSG code
701       CHARACTER(LEN=200) ::  grid_mapping_name = 'transverse_mercator'  !< name of grid mapping
702       CHARACTER(LEN=200) ::  long_name = 'coordinate reference system'  !< name of variable crs
703       CHARACTER(LEN=200) ::  units = 'm'                                !< unit of crs
704
705       REAL(wp) ::  false_easting = 500000.0_wp                  !< false easting
706       REAL(wp) ::  false_northing = 0.0_wp                      !< false northing
707       REAL(wp) ::  inverse_flattening = 298.257223563_wp        !< 1/f (default for WGS84)
708       REAL(wp) ::  latitude_of_projection_origin = 0.0_wp       !< latitude of projection origin
709       REAL(wp) ::  longitude_of_central_meridian = 3.0_wp       !< longitude of central meridian of UTM zone (default: zone 31)
710       REAL(wp) ::  longitude_of_prime_meridian = 0.0_wp         !< longitude of prime meridian
711       REAL(wp) ::  scale_factor_at_central_meridian = 0.9996_wp !< scale factor of UTM coordinates
712       REAL(wp) ::  semi_major_axis = 6378137.0_wp               !< length of semi major axis (default for WGS84)
713    END TYPE crs_type
714
715!
716!-- Define variables
717    TYPE(crs_type)   ::  coord_ref_sys  !< coordinate reference system
718
719    TYPE(dims_xy)    ::  dim_static     !< data structure for x, y-dimension in static input file
720
721    TYPE(nest_offl_type) ::  nest_offl  !< data structure for data input at lateral and top boundaries (provided by Inifor) 
722
723    TYPE(init_type) ::  init_3d    !< data structure for the initialization of the 3D flow and soil fields
724    TYPE(init_type) ::  init_model !< data structure for the initialization of the model
725
726!
727!-- Define 2D variables of type NC_BYTE
728    TYPE(int_2d_8bit)  ::  albedo_type_f     !< input variable for albedo type
729    TYPE(int_2d_8bit)  ::  building_type_f   !< input variable for building type
730    TYPE(int_2d_8bit)  ::  pavement_type_f   !< input variable for pavenment type
731    TYPE(int_2d_8bit)  ::  street_crossing_f !< input variable for water type
732    TYPE(int_2d_8bit)  ::  street_type_f     !< input variable for water type
733    TYPE(int_2d_8bit)  ::  vegetation_type_f !< input variable for vegetation type
734    TYPE(int_2d_8bit)  ::  water_type_f      !< input variable for water type
735!
736!-- Define 3D variables of type NC_BYTE
737    TYPE(int_3d_8bit)  ::  building_obstruction_f    !< input variable for building obstruction
738    TYPE(int_3d_8bit)  ::  building_obstruction_full !< input variable for building obstruction
739!
740!-- Define 2D variables of type NC_INT
741    TYPE(int_2d_32bit) ::  building_id_f     !< input variable for building ID
742!
743!-- Define 2D variables of type NC_FLOAT
744    TYPE(real_2d) ::  terrain_height_f       !< input variable for terrain height
745    TYPE(real_2d) ::  uvem_irradiance_f      !< input variable for uvem irradiance lookup table
746    TYPE(real_2d) ::  uvem_integration_f     !< input variable for uvem integration
747!
748!-- Define 3D variables of type NC_FLOAT
749    TYPE(real_3d) ::  basal_area_density_f    !< input variable for basal area density - resolved vegetation
750    TYPE(real_3d) ::  leaf_area_density_f     !< input variable for leaf area density - resolved vegetation
751    TYPE(real_3d) ::  root_area_density_lad_f !< input variable for root area density - resolved vegetation
752    TYPE(real_3d) ::  root_area_density_lsm_f !< input variable for root area density - parametrized vegetation
753    TYPE(real_3d) ::  uvem_radiance_f         !< input variable for uvem radiance lookup table
754    TYPE(real_3d) ::  uvem_projarea_f         !< input variable for uvem projection area lookup table
755!
756!-- Define input variable for buildings
757    TYPE(build_in) ::  buildings_f           !< input variable for buildings
758!
759!-- Define input variables for soil_type
760    TYPE(soil_in)  ::  soil_type_f           !< input variable for soil type
761
762    TYPE(fracs) ::  surface_fraction_f       !< input variable for surface fraction
763
764    TYPE(pars)  ::  albedo_pars_f              !< input variable for albedo parameters
765    TYPE(pars)  ::  building_pars_f            !< input variable for building parameters
766    TYPE(pars)  ::  pavement_pars_f            !< input variable for pavement parameters
767    TYPE(pars)  ::  pavement_subsurface_pars_f !< input variable for pavement parameters
768    TYPE(pars)  ::  soil_pars_f                !< input variable for soil parameters
769    TYPE(pars)  ::  vegetation_pars_f          !< input variable for vegetation parameters
770    TYPE(pars)  ::  water_pars_f               !< input variable for water parameters
771
772    TYPE(chem_emis_att_type)                             ::  chem_emis_att    !< Input Information of Chemistry Emission Data from netcdf 
773    TYPE(chem_emis_val_type), ALLOCATABLE, DIMENSION(:)  ::  chem_emis        !< Input Chemistry Emission Data from netcdf 
774
775    CHARACTER(LEN=3)  ::  char_lod  = 'lod'         !< name of level-of-detail attribute in NetCDF file
776
777    CHARACTER(LEN=10) ::  char_fill = '_FillValue'        !< name of fill value attribute in NetCDF file
778
779    CHARACTER(LEN=100) ::  input_file_static  = 'PIDS_STATIC'  !< Name of file which comprises static input data
780    CHARACTER(LEN=100) ::  input_file_dynamic = 'PIDS_DYNAMIC' !< Name of file which comprises dynamic input data
781    CHARACTER(LEN=100) ::  input_file_chem    = 'PIDS_CHEM'    !< Name of file which comprises chemistry input data
782    CHARACTER(LEN=100) ::  input_file_uvem    = 'PIDS_UVEM'    !< Name of file which comprises static uv_exposure model input data
783    CHARACTER(LEN=100) ::  input_file_vm      = 'PIDS_VM'      !< Name of file which comprises virtual measurement data
784   
785    CHARACTER(LEN=25), ALLOCATABLE, DIMENSION(:)    ::  string_values  !< output of string variables read from netcdf input files
786
787    INTEGER(iwp)                                     ::  id_emis        !< NetCDF id of input file for chemistry emissions: TBD: It has to be removed
788
789    INTEGER(iwp) ::  nc_stat         !< return value of nf90 function call
790
791    LOGICAL ::  input_pids_static  = .FALSE.   !< Flag indicating whether Palm-input-data-standard file containing static information exists
792    LOGICAL ::  input_pids_dynamic = .FALSE.   !< Flag indicating whether Palm-input-data-standard file containing dynamic information exists
793    LOGICAL ::  input_pids_chem    = .FALSE.   !< Flag indicating whether Palm-input-data-standard file containing chemistry information exists
794    LOGICAL ::  input_pids_uvem    = .FALSE.   !< Flag indicating whether uv-expoure-model input file containing static information exists
795    LOGICAL ::  input_pids_vm      = .FALSE.   !< Flag indicating whether input file for virtual measurements exist
796
797    LOGICAL ::  collective_read = .FALSE.      !< Enable NetCDF collective read
798
799    TYPE(global_atts_type) ::  input_file_atts !< global attributes of input file
800
801    SAVE
802
803    PRIVATE
804
805    INTERFACE netcdf_data_input_interpolate
806       MODULE PROCEDURE netcdf_data_input_interpolate_1d
807       MODULE PROCEDURE netcdf_data_input_interpolate_1d_soil
808       MODULE PROCEDURE netcdf_data_input_interpolate_2d
809       MODULE PROCEDURE netcdf_data_input_interpolate_3d
810    END INTERFACE netcdf_data_input_interpolate
811
812    INTERFACE netcdf_data_input_check_dynamic
813       MODULE PROCEDURE netcdf_data_input_check_dynamic
814    END INTERFACE netcdf_data_input_check_dynamic
815
816    INTERFACE netcdf_data_input_check_static
817       MODULE PROCEDURE netcdf_data_input_check_static
818    END INTERFACE netcdf_data_input_check_static
819
820    INTERFACE netcdf_data_input_chemistry_data                       
821       MODULE PROCEDURE netcdf_data_input_chemistry_data
822    END INTERFACE netcdf_data_input_chemistry_data
823   
824    INTERFACE netcdf_data_input_get_dimension_length                       
825       MODULE PROCEDURE netcdf_data_input_get_dimension_length
826    END INTERFACE netcdf_data_input_get_dimension_length
827
828    INTERFACE netcdf_data_input_inquire_file
829       MODULE PROCEDURE netcdf_data_input_inquire_file
830    END INTERFACE netcdf_data_input_inquire_file
831
832    INTERFACE netcdf_data_input_init
833       MODULE PROCEDURE netcdf_data_input_init
834    END INTERFACE netcdf_data_input_init
835   
836    INTERFACE netcdf_data_input_att
837       MODULE PROCEDURE netcdf_data_input_att_int8
838       MODULE PROCEDURE netcdf_data_input_att_int32
839       MODULE PROCEDURE netcdf_data_input_att_real
840       MODULE PROCEDURE netcdf_data_input_att_string
841    END INTERFACE netcdf_data_input_att
842
843    INTERFACE netcdf_data_input_init_3d
844       MODULE PROCEDURE netcdf_data_input_init_3d
845    END INTERFACE netcdf_data_input_init_3d
846   
847    INTERFACE netcdf_data_input_init_lsm
848       MODULE PROCEDURE netcdf_data_input_init_lsm
849    END INTERFACE netcdf_data_input_init_lsm
850
851    INTERFACE netcdf_data_input_offline_nesting
852       MODULE PROCEDURE netcdf_data_input_offline_nesting
853    END INTERFACE netcdf_data_input_offline_nesting
854
855    INTERFACE netcdf_data_input_surface_data
856       MODULE PROCEDURE netcdf_data_input_surface_data
857    END INTERFACE netcdf_data_input_surface_data
858
859    INTERFACE netcdf_data_input_var
860       MODULE PROCEDURE netcdf_data_input_var_char
861       MODULE PROCEDURE netcdf_data_input_var_real_1d
862       MODULE PROCEDURE netcdf_data_input_var_real_2d
863    END INTERFACE netcdf_data_input_var
864
865    INTERFACE netcdf_data_input_uvem
866       MODULE PROCEDURE netcdf_data_input_uvem
867    END INTERFACE netcdf_data_input_uvem
868
869    INTERFACE get_variable
870       MODULE PROCEDURE get_variable_1d_char
871       MODULE PROCEDURE get_variable_1d_int
872       MODULE PROCEDURE get_variable_1d_real
873       MODULE PROCEDURE get_variable_2d_int8
874       MODULE PROCEDURE get_variable_2d_int32
875       MODULE PROCEDURE get_variable_2d_real
876       MODULE PROCEDURE get_variable_3d_int8
877       MODULE PROCEDURE get_variable_3d_real
878       MODULE PROCEDURE get_variable_3d_real_dynamic
879       MODULE PROCEDURE get_variable_4d_to_3d_real
880       MODULE PROCEDURE get_variable_4d_real
881       MODULE PROCEDURE get_variable_5d_to_4d_real
882       MODULE PROCEDURE get_variable_string       
883    END INTERFACE get_variable
884
885    INTERFACE get_variable_pr
886       MODULE PROCEDURE get_variable_pr
887    END INTERFACE get_variable_pr
888
889    INTERFACE get_attribute
890       MODULE PROCEDURE get_attribute_real
891       MODULE PROCEDURE get_attribute_int8
892       MODULE PROCEDURE get_attribute_int32
893       MODULE PROCEDURE get_attribute_string
894    END INTERFACE get_attribute
895
896!
897!-- Public variables
898    PUBLIC albedo_pars_f, albedo_type_f, basal_area_density_f, buildings_f,    &
899           building_id_f, building_pars_f, building_type_f,                    &
900           chem_emis, chem_emis_att, chem_emis_att_type, chem_emis_val_type,   &
901           coord_ref_sys,                                                      &
902           init_3d, init_model, input_file_atts, input_file_static,            &
903           input_pids_static,                                                  &
904           input_pids_dynamic, input_pids_vm, input_file_vm,                   &
905           leaf_area_density_f, nest_offl,                                     &
906           pavement_pars_f, pavement_subsurface_pars_f, pavement_type_f,       &
907           root_area_density_lad_f, root_area_density_lsm_f, soil_pars_f,      &
908           soil_type_f, street_crossing_f, street_type_f, surface_fraction_f,  &
909           terrain_height_f, vegetation_pars_f, vegetation_type_f,             &
910           water_pars_f, water_type_f
911!
912!-- Public uv exposure variables
913    PUBLIC building_obstruction_f, input_file_uvem, input_pids_uvem,           &
914           netcdf_data_input_uvem,                                             &
915           uvem_integration_f, uvem_irradiance_f,                              &
916           uvem_projarea_f, uvem_radiance_f
917
918!
919!-- Public subroutines
920    PUBLIC netcdf_data_input_check_dynamic, netcdf_data_input_check_static,    &
921           netcdf_data_input_chemistry_data,                                   &
922           netcdf_data_input_get_dimension_length,                             &
923           netcdf_data_input_inquire_file,                                     &
924           netcdf_data_input_init, netcdf_data_input_init_lsm,                 &
925           netcdf_data_input_init_3d, netcdf_data_input_att,                   &
926           netcdf_data_input_interpolate, netcdf_data_input_offline_nesting,   &
927           netcdf_data_input_surface_data, netcdf_data_input_topo,             &
928           netcdf_data_input_var, get_attribute, get_variable, open_read_file, &
929           check_existence, inquire_num_variables, inquire_variable_names
930
931
932 CONTAINS
933
934!------------------------------------------------------------------------------!
935! Description:
936! ------------
937!> Inquires whether NetCDF input files according to Palm-input-data standard
938!> exist. Moreover, basic checks are performed.
939!------------------------------------------------------------------------------!
940    SUBROUTINE netcdf_data_input_inquire_file
941
942       USE control_parameters,                                                 &
943           ONLY:  topo_no_distinct
944
945       IMPLICIT NONE
946
947#if defined ( __netcdf )
948       INQUIRE( FILE = TRIM( input_file_static )   // TRIM( coupling_char ),   &
949                EXIST = input_pids_static  )
950       INQUIRE( FILE = TRIM( input_file_dynamic ) // TRIM( coupling_char ),    &
951                EXIST = input_pids_dynamic )
952       INQUIRE( FILE = TRIM( input_file_chem )    // TRIM( coupling_char ),    &
953                EXIST = input_pids_chem )
954       INQUIRE( FILE = TRIM( input_file_uvem ) // TRIM( coupling_char ),       &
955                EXIST = input_pids_uvem  )
956       INQUIRE( FILE = TRIM( input_file_vm )      // TRIM( coupling_char ),    &
957                EXIST = input_pids_vm )
958#endif
959
960!
961!--    As long as topography can be input via ASCII format, no distinction
962!--    between building and terrain can be made. This case, classify all
963!--    surfaces as default type. Same in case land-surface and urban-surface
964!--    model are not applied.
965       IF ( .NOT. input_pids_static )  THEN
966          topo_no_distinct = .TRUE.
967       ENDIF
968
969    END SUBROUTINE netcdf_data_input_inquire_file
970
971!------------------------------------------------------------------------------!
972! Description:
973! ------------
974!> Reads global attributes and coordinate reference system required for
975!> initialization of the model.
976!------------------------------------------------------------------------------!
977    SUBROUTINE netcdf_data_input_init
978
979       IMPLICIT NONE
980
981       INTEGER(iwp) ::  id_mod     !< NetCDF id of input file
982       INTEGER(iwp) ::  var_id_crs !< NetCDF id of variable crs
983
984       IF ( .NOT. input_pids_static )  RETURN
985
986#if defined ( __netcdf )
987!
988!--    Open file in read-only mode
989       CALL open_read_file( TRIM( input_file_static ) //                       &
990                            TRIM( coupling_char ), id_mod )
991!
992!--    Read global attributes
993       CALL get_attribute( id_mod, input_file_atts%origin_lat_char,            &
994                           input_file_atts%origin_lat, .TRUE. )
995
996       CALL get_attribute( id_mod, input_file_atts%origin_lon_char,            &
997                           input_file_atts%origin_lon, .TRUE. )
998
999       CALL get_attribute( id_mod, input_file_atts%origin_time_char,           &
1000                           input_file_atts%origin_time, .TRUE. )
1001
1002       CALL get_attribute( id_mod, input_file_atts%origin_x_char,              &
1003                           input_file_atts%origin_x, .TRUE. )
1004
1005       CALL get_attribute( id_mod, input_file_atts%origin_y_char,              &
1006                           input_file_atts%origin_y, .TRUE. )
1007
1008       CALL get_attribute( id_mod, input_file_atts%origin_z_char,              &
1009                           input_file_atts%origin_z, .TRUE. )
1010
1011       CALL get_attribute( id_mod, input_file_atts%rotation_angle_char,        &
1012                           input_file_atts%rotation_angle, .TRUE. )
1013
1014       CALL get_attribute( id_mod, input_file_atts%author_char,                &
1015                           input_file_atts%author, .TRUE., no_abort=.FALSE. )
1016       CALL get_attribute( id_mod, input_file_atts%contact_person_char,        &
1017                           input_file_atts%contact_person, .TRUE., no_abort=.FALSE. )
1018       CALL get_attribute( id_mod, input_file_atts%institution_char,           &
1019                           input_file_atts%institution,    .TRUE., no_abort=.FALSE. )
1020       CALL get_attribute( id_mod, input_file_atts%acronym_char,               &
1021                           input_file_atts%acronym,        .TRUE., no_abort=.FALSE. )
1022
1023       CALL get_attribute( id_mod, input_file_atts%campaign_char,              &
1024                           input_file_atts%campaign, .TRUE., no_abort=.FALSE. )
1025       CALL get_attribute( id_mod, input_file_atts%location_char,              &
1026                           input_file_atts%location, .TRUE., no_abort=.FALSE. )
1027       CALL get_attribute( id_mod, input_file_atts%site_char,                  &
1028                           input_file_atts%site,     .TRUE., no_abort=.FALSE. )
1029
1030       CALL get_attribute( id_mod, input_file_atts%source_char,                &
1031                           input_file_atts%source,     .TRUE., no_abort=.FALSE. )
1032       CALL get_attribute( id_mod, input_file_atts%references_char,            &
1033                           input_file_atts%references, .TRUE., no_abort=.FALSE. )
1034       CALL get_attribute( id_mod, input_file_atts%keywords_char,              &
1035                           input_file_atts%keywords,   .TRUE., no_abort=.FALSE. )
1036       CALL get_attribute( id_mod, input_file_atts%licence_char,               &
1037                           input_file_atts%licence,    .TRUE., no_abort=.FALSE. )
1038       CALL get_attribute( id_mod, input_file_atts%comment_char,               &
1039                           input_file_atts%comment,    .TRUE., no_abort=.FALSE. )
1040!
1041!--    Read coordinate reference system if available
1042       nc_stat = NF90_INQ_VARID( id_mod, 'crs', var_id_crs )
1043       IF ( nc_stat == NF90_NOERR )  THEN
1044          CALL get_attribute( id_mod, 'epsg_code',                             &
1045                              coord_ref_sys%epsg_code,                         &
1046                              .FALSE., 'crs' )
1047          CALL get_attribute( id_mod, 'false_easting',                         &
1048                              coord_ref_sys%false_easting,                     &
1049                              .FALSE., 'crs' )
1050          CALL get_attribute( id_mod, 'false_northing',                        &
1051                              coord_ref_sys%false_northing,                    &
1052                              .FALSE., 'crs' )
1053          CALL get_attribute( id_mod, 'grid_mapping_name',                     &
1054                              coord_ref_sys%grid_mapping_name,                 &
1055                              .FALSE., 'crs' )
1056          CALL get_attribute( id_mod, 'inverse_flattening',                    &
1057                              coord_ref_sys%inverse_flattening,                &
1058                              .FALSE., 'crs' )
1059          CALL get_attribute( id_mod, 'latitude_of_projection_origin',         &
1060                              coord_ref_sys%latitude_of_projection_origin,     &
1061                              .FALSE., 'crs' )
1062          CALL get_attribute( id_mod, 'long_name',                             &
1063                              coord_ref_sys%long_name,                         &
1064                              .FALSE., 'crs' )
1065          CALL get_attribute( id_mod, 'longitude_of_central_meridian',         &
1066                              coord_ref_sys%longitude_of_central_meridian,     &
1067                              .FALSE., 'crs' )
1068          CALL get_attribute( id_mod, 'longitude_of_prime_meridian',           &
1069                              coord_ref_sys%longitude_of_prime_meridian,       &
1070                              .FALSE., 'crs' )
1071          CALL get_attribute( id_mod, 'scale_factor_at_central_meridian',      &
1072                              coord_ref_sys%scale_factor_at_central_meridian,  &
1073                              .FALSE., 'crs' )
1074          CALL get_attribute( id_mod, 'semi_major_axis',                       &
1075                              coord_ref_sys%semi_major_axis,                   &
1076                              .FALSE., 'crs' )
1077          CALL get_attribute( id_mod, 'units',                                 &
1078                              coord_ref_sys%units,                             &
1079                              .FALSE., 'crs' )
1080       ELSE
1081!
1082!--       Calculate central meridian from origin_lon
1083          coord_ref_sys%longitude_of_central_meridian = &
1084             CEILING( input_file_atts%origin_lon / 6.0_wp ) * 6.0_wp - 3.0_wp
1085       ENDIF
1086!
1087!--    Finally, close input file
1088       CALL close_input_file( id_mod )
1089#endif
1090!
1091!--    Copy latitude, longitude, origin_z, rotation angle on init type
1092       init_model%latitude        = input_file_atts%origin_lat
1093       init_model%longitude       = input_file_atts%origin_lon
1094       init_model%origin_time     = input_file_atts%origin_time 
1095       init_model%origin_x        = input_file_atts%origin_x
1096       init_model%origin_y        = input_file_atts%origin_y
1097       init_model%origin_z        = input_file_atts%origin_z 
1098       init_model%rotation_angle  = input_file_atts%rotation_angle 
1099           
1100!
1101!--    In case of nested runs, each model domain might have different longitude
1102!--    and latitude, which would result in different Coriolis parameters and
1103!--    sun-zenith angles. To avoid this, longitude and latitude in each model
1104!--    domain will be set to the values of the root model. Please note, this
1105!--    synchronization is required already here.
1106#if defined( __parallel )
1107       CALL MPI_BCAST( init_model%latitude,  1, MPI_REAL, 0,                   &
1108                       MPI_COMM_WORLD, ierr )
1109       CALL MPI_BCAST( init_model%longitude, 1, MPI_REAL, 0,                   &
1110                       MPI_COMM_WORLD, ierr )
1111#endif
1112
1113    END SUBROUTINE netcdf_data_input_init
1114   
1115!------------------------------------------------------------------------------!
1116! Description:
1117! ------------
1118!> Read an array of characters.
1119!------------------------------------------------------------------------------!
1120    SUBROUTINE netcdf_data_input_var_char( val, search_string, id_mod )
1121
1122       IMPLICIT NONE
1123
1124       CHARACTER(LEN=*) ::  search_string     !< name of the variable
1125       CHARACTER(LEN=*), DIMENSION(:) ::  val !< variable which should be read
1126       
1127       INTEGER(iwp) ::  id_mod   !< NetCDF id of input file
1128
1129#if defined ( __netcdf )
1130!
1131!--    Read variable
1132       CALL get_variable( id_mod, search_string, val )
1133#endif           
1134
1135    END SUBROUTINE netcdf_data_input_var_char
1136   
1137!------------------------------------------------------------------------------!
1138! Description:
1139! ------------
1140!> Read an 1D array of REAL values.
1141!------------------------------------------------------------------------------!
1142    SUBROUTINE netcdf_data_input_var_real_1d( val, search_string, id_mod )
1143
1144       IMPLICIT NONE
1145
1146       CHARACTER(LEN=*) ::  search_string     !< name of the variable     
1147       
1148       INTEGER(iwp) ::  id_mod        !< NetCDF id of input file
1149       
1150       REAL(wp), DIMENSION(:) ::  val !< variable which should be read
1151
1152#if defined ( __netcdf )
1153!
1154!--    Read variable
1155       CALL get_variable( id_mod, search_string, val )
1156#endif           
1157
1158    END SUBROUTINE netcdf_data_input_var_real_1d
1159   
1160!------------------------------------------------------------------------------!
1161! Description:
1162! ------------
1163!> Read an 1D array of REAL values.
1164!------------------------------------------------------------------------------!
1165    SUBROUTINE netcdf_data_input_var_real_2d( val, search_string,              &
1166                                              id_mod, d1s, d1e, d2s, d2e )
1167
1168       IMPLICIT NONE
1169
1170       CHARACTER(LEN=*) ::  search_string     !< name of the variable     
1171       
1172       INTEGER(iwp) ::  id_mod  !< NetCDF id of input file
1173       INTEGER(iwp) ::  d1e     !< end index of first dimension to be read
1174       INTEGER(iwp) ::  d2e     !< end index of second dimension to be read
1175       INTEGER(iwp) ::  d1s     !< start index of first dimension to be read
1176       INTEGER(iwp) ::  d2s     !< start index of second dimension to be read
1177       
1178       REAL(wp), DIMENSION(:,:) ::  val !< variable which should be read
1179
1180#if defined ( __netcdf )
1181!
1182!--    Read character variable
1183       CALL get_variable( id_mod, search_string, val, d1s, d1e, d2s, d2e )
1184#endif           
1185
1186    END SUBROUTINE netcdf_data_input_var_real_2d
1187   
1188!------------------------------------------------------------------------------!
1189! Description:
1190! ------------
1191!> Read a global string attribute
1192!------------------------------------------------------------------------------!
1193    SUBROUTINE netcdf_data_input_att_string( val, search_string, id_mod,       &
1194                                             input_file, global, openclose,    &
1195                                             variable_name )
1196
1197       IMPLICIT NONE
1198
1199       CHARACTER(LEN=*) ::  search_string !< name of the attribue
1200       CHARACTER(LEN=*) ::  val           !< attribute
1201       
1202       CHARACTER(LEN=*) ::  input_file    !< name of input file
1203       CHARACTER(LEN=*) ::  openclose     !< string indicating whether NetCDF needs to be opend or closed
1204       CHARACTER(LEN=*) ::  variable_name !< string indicating whether NetCDF needs to be opend or closed 
1205       
1206       INTEGER(iwp) ::  id_mod   !< NetCDF id of input file
1207       
1208       LOGICAL ::  global                 !< flag indicating a global or a variable's attribute
1209
1210#if defined ( __netcdf )
1211!
1212!--    Open file in read-only mode if necessary
1213       IF ( openclose == 'open' )  THEN
1214          CALL open_read_file( TRIM( input_file ) // TRIM( coupling_char ), &
1215                                  id_mod )
1216       ENDIF
1217!
1218!--    Read global attribute
1219       IF ( global )  THEN
1220          CALL get_attribute( id_mod, search_string, val, global )
1221!
1222!--    Read variable attribute
1223       ELSE
1224          CALL get_attribute( id_mod, search_string, val, global, variable_name )
1225       ENDIF
1226!
1227!--    Close input file
1228       IF ( openclose == 'close' )  CALL close_input_file( id_mod )
1229#endif           
1230
1231    END SUBROUTINE netcdf_data_input_att_string
1232   
1233!------------------------------------------------------------------------------!
1234! Description:
1235! ------------
1236!> Read a global 8-bit integer attribute
1237!------------------------------------------------------------------------------!
1238    SUBROUTINE netcdf_data_input_att_int8( val, search_string, id_mod,         &
1239                                           input_file, global, openclose,      &
1240                                           variable_name )
1241
1242       IMPLICIT NONE
1243
1244       CHARACTER(LEN=*) ::  search_string !< name of the attribue
1245       
1246       CHARACTER(LEN=*) ::  input_file    !< name of input file
1247       CHARACTER(LEN=*) ::  openclose     !< string indicating whether NetCDF needs to be opend or closed
1248       CHARACTER(LEN=*) ::  variable_name !< string indicating whether NetCDF needs to be opend or closed
1249       
1250       INTEGER(iwp) ::  id_mod   !< NetCDF id of input file
1251       INTEGER(KIND=1) ::  val      !< value of the attribute
1252       
1253       LOGICAL ::  global        !< flag indicating a global or a variable's attribute
1254
1255#if defined ( __netcdf )
1256!
1257!--    Open file in read-only mode
1258       IF ( openclose == 'open' )  THEN
1259          CALL open_read_file( TRIM( input_file ) // TRIM( coupling_char ), &
1260                                  id_mod )
1261       ENDIF
1262!
1263!--    Read global attribute
1264       IF ( global )  THEN
1265          CALL get_attribute( id_mod, search_string, val, global )
1266!
1267!--    Read variable attribute
1268       ELSE
1269          CALL get_attribute( id_mod, search_string, val, global, variable_name )
1270       ENDIF
1271!
1272!--    Finally, close input file
1273       IF ( openclose == 'close' )  CALL close_input_file( id_mod )
1274#endif           
1275
1276    END SUBROUTINE netcdf_data_input_att_int8
1277   
1278!------------------------------------------------------------------------------!
1279! Description:
1280! ------------
1281!> Read a global 32-bit integer attribute
1282!------------------------------------------------------------------------------!
1283    SUBROUTINE netcdf_data_input_att_int32( val, search_string, id_mod,        &
1284                                            input_file, global, openclose,     &
1285                                            variable_name )
1286
1287       IMPLICIT NONE
1288
1289       CHARACTER(LEN=*) ::  search_string !< name of the attribue
1290       
1291       CHARACTER(LEN=*) ::  input_file    !< name of input file
1292       CHARACTER(LEN=*) ::  openclose     !< string indicating whether NetCDF needs to be opend or closed
1293       CHARACTER(LEN=*) ::  variable_name !< string indicating whether NetCDF needs to be opend or closed
1294       
1295       INTEGER(iwp) ::  id_mod   !< NetCDF id of input file
1296       INTEGER(iwp) ::  val      !< value of the attribute
1297       
1298       LOGICAL ::  global        !< flag indicating a global or a variable's attribute
1299
1300#if defined ( __netcdf )
1301!
1302!--    Open file in read-only mode
1303       IF ( openclose == 'open' )  THEN
1304          CALL open_read_file( TRIM( input_file ) // TRIM( coupling_char ), &
1305                                  id_mod )
1306       ENDIF
1307!
1308!--    Read global attribute
1309       IF ( global )  THEN
1310          CALL get_attribute( id_mod, search_string, val, global )
1311!
1312!--    Read variable attribute
1313       ELSE
1314          CALL get_attribute( id_mod, search_string, val, global, variable_name )
1315       ENDIF
1316!
1317!--    Finally, close input file
1318       IF ( openclose == 'close' )  CALL close_input_file( id_mod )
1319#endif           
1320
1321    END SUBROUTINE netcdf_data_input_att_int32
1322   
1323!------------------------------------------------------------------------------!
1324! Description:
1325! ------------
1326!> Read a global real attribute
1327!------------------------------------------------------------------------------!
1328    SUBROUTINE netcdf_data_input_att_real( val, search_string, id_mod,         &
1329                                           input_file, global, openclose,      &
1330                                           variable_name )
1331
1332       IMPLICIT NONE
1333
1334       CHARACTER(LEN=*) ::  search_string !< name of the attribue
1335       
1336       CHARACTER(LEN=*) ::  input_file    !< name of input file
1337       CHARACTER(LEN=*) ::  openclose     !< string indicating whether NetCDF needs to be opend or closed
1338       CHARACTER(LEN=*) ::  variable_name !< string indicating whether NetCDF needs to be opend or closed
1339       
1340       INTEGER(iwp) ::  id_mod            !< NetCDF id of input file
1341       
1342       LOGICAL ::  global                 !< flag indicating a global or a variable's attribute
1343       
1344       REAL(wp) ::  val                   !< value of the attribute
1345
1346#if defined ( __netcdf )
1347!
1348!--    Open file in read-only mode
1349       IF ( openclose == 'open' )  THEN
1350          CALL open_read_file( TRIM( input_file ) // TRIM( coupling_char ), &
1351                                  id_mod )
1352       ENDIF
1353!
1354!--    Read global attribute
1355       IF ( global )  THEN
1356          CALL get_attribute( id_mod, search_string, val, global )
1357!
1358!--    Read variable attribute
1359       ELSE
1360          CALL get_attribute( id_mod, search_string, val, global, variable_name )
1361       ENDIF
1362!
1363!--    Finally, close input file
1364       IF ( openclose == 'close' )  CALL close_input_file( id_mod )
1365#endif           
1366
1367    END SUBROUTINE netcdf_data_input_att_real
1368
1369!------------------------------------------------------------------------------!
1370! Description:
1371! ------------
1372!> Reads Chemistry NETCDF Input data, such as emission values, emission species, etc. .
1373!------------------------------------------------------------------------------!
1374    SUBROUTINE netcdf_data_input_chemistry_data(emt_att,emt)
1375
1376       USE chem_modules,                                       &
1377           ONLY:  mode_emis, time_fac_type, surface_csflux_name
1378
1379       USE control_parameters,                                 &
1380           ONLY:  message_string
1381
1382       USE indices,                                            &
1383           ONLY:  nx, ny, nxl, nxr, nys, nyn
1384
1385       IMPLICIT NONE
1386
1387       TYPE(chem_emis_att_type), INTENT(INOUT)                                        :: emt_att
1388       TYPE(chem_emis_val_type), ALLOCATABLE, DIMENSION(:), INTENT(INOUT)             :: emt
1389   
1390       INTEGER(iwp)                                     :: ispec                 !< index for number of emission species in input
1391
1392       INTEGER(iwp)                                     :: num_vars              !< number of variables in netcdf input file
1393       INTEGER(iwp)                                     :: len_dims              !< Length of dimension
1394
1395       REAL(wp), ALLOCATABLE, DIMENSION(:,:,:)          :: dum_var_3d            !< variable for storing temporary data of 3-dimensional
1396                                                                                 !  variables read from netcdf for chemistry emissions
1397
1398       REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:)        :: dum_var_4d            !< variable for storing temporary data of 5-dimensional
1399                                                                                 !< variables read from netcdf for chemistry emissions
1400!--
1401       !> Start the processing of the data
1402
1403       !> Parameterized mode of the emissions
1404       IF (TRIM(mode_emis)=="PARAMETERIZED" .OR. TRIM(mode_emis)=="parameterized") THEN
1405
1406           ispec=1
1407           emt_att%nspec=0
1408
1409          !number of species
1410           DO  WHILE (TRIM( surface_csflux_name( ispec ) ) /= 'novalue' )
1411
1412             emt_att%nspec=emt_att%nspec+1
1413             ispec=ispec+1
1414
1415           ENDDO
1416
1417          !-- allocate emission values data type arrays
1418          ALLOCATE(emt(emt_att%nspec))
1419
1420          !-- Read EMISSION SPECIES NAMES
1421
1422          !Assign values
1423          ALLOCATE(emt_att%species_name(emt_att%nspec))
1424 
1425         DO ispec=1,emt_att%nspec
1426            emt_att%species_name(ispec) = TRIM(surface_csflux_name(ispec))
1427         ENDDO
1428
1429
1430       !> DEFAULT AND PRE-PROCESSED MODE
1431       ELSE
1432
1433#if defined ( __netcdf )       
1434          IF ( .NOT. input_pids_chem )  RETURN
1435
1436          !-- Open file in read-only mode
1437          CALL open_read_file( TRIM( input_file_chem ) //                       &
1438                               TRIM( coupling_char ), id_emis )
1439          !-- inquire number of variables
1440          CALL inquire_num_variables( id_emis, num_vars )
1441
1442          !-- Get General Dimension Lengths: only number of species and number of categories.
1443          !                                  the other dimensions depend on the mode of the emissions or on the presence of specific components
1444          !nspecies
1445          CALL netcdf_data_input_get_dimension_length( id_emis, emt_att%nspec, 'nspecies' )
1446
1447 
1448          !-- Allocate emission values data type arrays
1449          ALLOCATE(emt(1:emt_att%nspec))
1450
1451
1452          !-- Read EMISSION SPECIES NAMES
1453          !Allocate Arrays
1454          ALLOCATE(emt_att%species_name(emt_att%nspec)) 
1455
1456          !Call get Variable
1457          CALL get_variable( id_emis, 'emission_name', string_values, emt_att%nspec )
1458          emt_att%species_name=string_values
1459          ! If allocated, Deallocate var_string, an array only used for reading-in strings
1460          IF (ALLOCATED(string_values)) DEALLOCATE(string_values) 
1461
1462          !-- Read EMISSION SPECIES INDEX
1463          !Allocate Arrays
1464          ALLOCATE(emt_att%species_index(emt_att%nspec))
1465          !Call get Variable
1466          CALL get_variable( id_emis, 'emission_index', emt_att%species_index )
1467
1468
1469          !-- Now the routine has to distinguish between DEFAULT and PRE-PROCESSED chemistry emission modes
1470
1471          IF (TRIM(mode_emis)=="DEFAULT" .OR. TRIM(mode_emis)=="default") THEN
1472 
1473             !number of categories
1474             CALL netcdf_data_input_get_dimension_length( id_emis, emt_att%ncat, 'ncat' )
1475
1476             !-- Read EMISSION CATEGORIES INDEX
1477             !Allocate Arrays
1478             ALLOCATE(emt_att%cat_index(emt_att%ncat))
1479             !Call get Variable
1480             CALL get_variable( id_emis, 'emission_cat_index', emt_att%cat_index )
1481
1482 
1483             DO ispec=1,emt_att%nspec
1484                !-- EMISSION_VOC_NAME (1-DIMENSIONAL)
1485                IF (TRIM(emt_att%species_name(ispec))=="VOC" .OR. TRIM(emt_att%species_name(ispec))=="voc") THEN
1486                   !Allocate Array
1487                   CALL netcdf_data_input_get_dimension_length( id_emis, emt_att%nvoc, 'nvoc' )
1488                   ALLOCATE(emt_att%voc_name(1:emt_att%nvoc))
1489                   !Read-in Variable
1490                   CALL get_variable( id_emis,"emission_voc_name",string_values, emt_att%nvoc)
1491                   emt_att%voc_name=string_values
1492                   IF (ALLOCATED(string_values)) DEALLOCATE(string_values)
1493 
1494                !-- COMPOSITION VOC (2-DIMENSIONAL)
1495                   !Allocate Array
1496                   ALLOCATE(emt_att%voc_comp(1:emt_att%ncat,1:emt_att%nvoc))
1497                   !Read-in Variable
1498!               CALL get_variable(id_emis,"composition_voc",emt%voc_comp,1,1,emt%ncat,emt%nvoc)
1499                   CALL get_variable(id_emis,"composition_voc",emt_att%voc_comp,1,emt_att%ncat,1,emt_att%nvoc)
1500                ENDIF
1501
1502                !-- EMISSION_PM_NAME (1-DIMENSIONAL)
1503                IF (TRIM(emt_att%species_name(ispec))=="PM" .OR. TRIM(emt_att%species_name(ispec))=="pm") THEN
1504                   CALL netcdf_data_input_get_dimension_length( id_emis, emt_att%npm, 'npm' )
1505                   ALLOCATE(emt_att%pm_name(1:emt_att%npm))
1506                   !Read-in Variable
1507                   CALL get_variable( id_emis,"pm_name",string_values, emt_att%npm)
1508                   emt_att%pm_name=string_values
1509                   IF (ALLOCATED(string_values)) DEALLOCATE(string_values)     
1510
1511                !-- COMPOSITION PM (3-DIMENSIONAL)
1512                   !Allocate
1513                   len_dims=3  !> number of PMs: PM1, PM2.5 and PM10
1514                   ALLOCATE(emt_att%pm_comp(1:emt_att%ncat,1:emt_att%npm,1:len_dims))
1515                   !Read-in Variable
1516                   CALL get_variable(id_emis,"composition_pm",emt_att%pm_comp,1,emt_att%ncat,1,emt_att%npm,1,len_dims)                   
1517                ENDIF
1518
1519                !-- COMPOSITION_NOX (2-DIMENSIONAL)
1520                IF (TRIM(emt_att%species_name(ispec))=="NOx" .OR. TRIM(emt_att%species_name(ispec))=="nox") THEN
1521                   !Allocate array
1522                   ALLOCATE(emt_att%nox_comp(1:emt_att%ncat,1:emt_att%nnox))
1523                   !Read-in Variable
1524                   CALL get_variable(id_emis,"composition_nox",emt_att%nox_comp,1,emt_att%ncat,1,emt_att%nnox)
1525                ENDIF
1526
1527                !-- COMPOSITION-SOX (2-DIMENSIONAL)
1528                IF (TRIM(emt_att%species_name(ispec))=="SOx" .OR. TRIM(emt_att%species_name(ispec))=="sox") THEN
1529                   ALLOCATE(emt_att%sox_comp(1:emt_att%ncat,1:emt_att%nsox))
1530                   !Read-in Variable
1531                   CALL get_variable(id_emis,"composition_sox",emt_att%sox_comp,1,emt_att%ncat,1,emt_att%nsox)
1532                ENDIF
1533             ENDDO !>ispec
1534
1535!-- For reading the emission time factors, the distinction between HOUR and MDH data is necessary
1536     
1537             !-- EMISSION_TIME_SCALING_FACTORS
1538                !-- HOUR   
1539             IF (TRIM(time_fac_type)=="HOUR" .OR. TRIM(time_fac_type)=="hour") THEN
1540                !-- Allocate Array
1541                CALL netcdf_data_input_get_dimension_length( id_emis, emt_att%nhoursyear, 'nhoursyear' )
1542                ALLOCATE(emt_att%hourly_emis_time_factor(1:emt_att%ncat,1:emt_att%nhoursyear))
1543                !Read-in Variable
1544                CALL get_variable(id_emis,"emission_time_factors",emt_att%hourly_emis_time_factor,1,   &
1545                                  emt_att%ncat,1,emt_att%nhoursyear)
1546
1547                !-- MDH
1548             ELSE IF (TRIM(time_fac_type) == "MDH" .OR. TRIM(time_fac_type) == "mdh") THEN
1549                !-- Allocate Array
1550                CALL netcdf_data_input_get_dimension_length( id_emis, emt_att%nmonthdayhour, 'nmonthdayhour' )
1551                ALLOCATE(emt_att%mdh_emis_time_factor(1:emt_att%ncat,1:emt_att%nmonthdayhour))
1552                !-- Read-in Variable
1553                CALL get_variable(id_emis,"emission_time_factors",emt_att%mdh_emis_time_factor,1,       &
1554                                  emt_att%ncat,1,emt_att%nmonthdayhour)
1555
1556             ELSE
1557
1558             message_string = 'We are in the DEFAULT chemistry emissions mode: '            //          &
1559                              '     !no time-factor type specified!'                        //          &
1560                              'Please, specify the value of time_fac_type:'                 //          &
1561                              '         either "MDH" or "HOUR"'                 
1562             CALL message( 'netcdf_data_input_chemistry_data', 'CM0200', 2, 2, 0, 6, 0 ) 
1563 
1564
1565             ENDIF
1566
1567             !-- Finally read-in the emission values and their units (DEFAULT mode)
1568
1569             DO ispec=1,emt_att%nspec
1570
1571                IF ( .NOT. ALLOCATED( emt(ispec)%default_emission_data ) )                              &
1572                    ALLOCATE(emt(ispec)%default_emission_data(1:emt_att%ncat,1:ny+1,1:nx+1))
1573
1574                ALLOCATE(dum_var_3d(1:emt_att%ncat,nys+1:nyn+1,nxl+1:nxr+1))
1575
1576                CALL get_variable(id_emis,"emission_values",dum_var_3d,ispec,1,emt_att%ncat,nys,nyn,nxl,nxr)         
1577
1578                emt(ispec)%default_emission_data(:,nys+1:nyn+1,nxl+1:nxr+1) =                           &
1579                    dum_var_3d(1:emt_att%ncat,nys+1:nyn+1,nxl+1:nxr+1)
1580
1581                DEALLOCATE (dum_var_3d)
1582
1583             ENDDO
1584
1585             !-- UNITS
1586             CALL get_attribute(id_emis,"units",emt_att%units,.FALSE.,"emission_values")
1587
1588
1589          !-- PRE-PROCESSED MODE --
1590
1591          ELSE IF (TRIM(mode_emis)=="PRE-PROCESSED" .OR. TRIM(mode_emis)=="pre-processed") THEN
1592          !-- 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
1593
1594             DO ispec=1,emt_att%nspec
1595
1596             !-- EMISSION_VOC_NAME (1-DIMENSIONAL)
1597                IF (TRIM(emt_att%species_name(ispec))=="VOC" .OR. TRIM(emt_att%species_name(ispec))=="voc") THEN
1598                   !Allocate Array
1599                   CALL netcdf_data_input_get_dimension_length( id_emis, emt_att%nvoc, 'nvoc' )
1600                   ALLOCATE(emt_att%voc_name(1:emt_att%nvoc))
1601                   !Read-in Variable
1602                   CALL get_variable( id_emis,"emission_voc_name",string_values, emt_att%nvoc)
1603                   emt_att%voc_name=string_values
1604                   IF (ALLOCATED(string_values)) DEALLOCATE(string_values)
1605 
1606             !-- COMPOSITION VOC (2-DIMENSIONAL)
1607                   !Allocate Array
1608                   ALLOCATE(emt_att%voc_comp(1:emt_att%ncat,1:emt_att%nvoc))
1609                   !Read-in Variable
1610                   CALL get_variable(id_emis,"composition_voc",emt_att%voc_comp,1,emt_att%ncat,1,emt_att%nvoc)
1611                ENDIF
1612 
1613             ENDDO !> ispec
1614
1615             !-- EMISSION_VALUES (4-DIMENSIONAL)
1616             !Calculate temporal dimension length
1617             CALL netcdf_data_input_get_dimension_length( id_emis, emt_att%dt_emission, 'time' )   
1618         
1619
1620             DO ispec=1,emt_att%nspec
1621
1622                !Allocation for the entire domain has to be done only for the first processor between all the subdomains     
1623                IF ( .NOT. ALLOCATED( emt(ispec)%preproc_emission_data ) )                              &
1624                    ALLOCATE(emt(ispec)%preproc_emission_data(emt_att%dt_emission,1,1:ny+1,1:nx+1))
1625
1626                !> allocate variable where to pass emission values read from netcdf
1627                ALLOCATE(dum_var_4d(1:emt_att%dt_emission,1,nys+1:nyn+1,nxl+1:nxr+1))
1628
1629                !Read-in Variable
1630                CALL get_variable(id_emis,"emission_values",dum_var_4d,ispec,1,emt_att%dt_emission,1,1,nys,nyn,nxl,nxr)         
1631
1632     
1633                emt(ispec)%preproc_emission_data(:,1,nys+1:nyn+1,nxl+1:nxr+1) =                         &
1634                      dum_var_4d(1:emt_att%dt_emission,1,nys+1:nyn+1,nxl+1:nxr+1)
1635
1636                DEALLOCATE ( dum_var_4d )
1637
1638             ENDDO
1639
1640             !-- UNITS
1641             CALL get_attribute(id_emis,"units",emt_att%units,.FALSE.,"emission_values")
1642       
1643          ENDIF
1644
1645       CALL close_input_file( id_emis )
1646
1647#endif
1648       ENDIF
1649
1650    END SUBROUTINE netcdf_data_input_chemistry_data
1651
1652!------------------------------------------------------------------------------!
1653! Description:
1654! ------------
1655!> Reads surface classification data, such as vegetation and soil type, etc. .
1656!------------------------------------------------------------------------------!
1657    SUBROUTINE netcdf_data_input_surface_data
1658
1659       USE control_parameters,                                                 &
1660           ONLY:  land_surface, plant_canopy, urban_surface
1661
1662       USE indices,                                                            &
1663           ONLY:  nbgp, nxl, nxr, nyn, nys
1664
1665
1666       IMPLICIT NONE
1667
1668       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names  !< variable names in static input file
1669
1670       INTEGER(iwp) ::  id_surf   !< NetCDF id of input file
1671       INTEGER(iwp) ::  k         !< running index along z-direction
1672       INTEGER(iwp) ::  k2        !< running index
1673       INTEGER(iwp) ::  num_vars  !< number of variables in input file
1674       INTEGER(iwp) ::  nz_soil   !< number of soil layers in file
1675
1676!
1677!--    If not static input file is available, skip this routine
1678       IF ( .NOT. input_pids_static )  RETURN
1679!
1680!--    Measure CPU time
1681       CALL cpu_log( log_point_s(82), 'NetCDF input', 'start' )
1682!
1683!--    Read plant canopy variables.
1684       IF ( plant_canopy )  THEN
1685#if defined ( __netcdf )
1686!
1687!--       Open file in read-only mode
1688          CALL open_read_file( TRIM( input_file_static ) //                    &
1689                               TRIM( coupling_char ) , id_surf )
1690!
1691!--       At first, inquire all variable names.
1692!--       This will be used to check whether an optional input variable
1693!--       exist or not.
1694          CALL inquire_num_variables( id_surf, num_vars )
1695
1696          ALLOCATE( var_names(1:num_vars) )
1697          CALL inquire_variable_names( id_surf, var_names )
1698
1699!
1700!--       Read leaf area density - resolved vegetation
1701          IF ( check_existence( var_names, 'lad' ) )  THEN
1702             leaf_area_density_f%from_file = .TRUE.
1703             CALL get_attribute( id_surf, char_fill,                           &
1704                                 leaf_area_density_f%fill,                     &
1705                                 .FALSE., 'lad' )
1706!
1707!--          Inquire number of vertical vegetation layer
1708             CALL netcdf_data_input_get_dimension_length( id_surf,             &
1709                                                 leaf_area_density_f%nz,       &
1710                                                 'zlad' )
1711!
1712!--          Allocate variable for leaf-area density
1713             ALLOCATE( leaf_area_density_f%var( 0:leaf_area_density_f%nz-1,    &
1714                                                nys:nyn,nxl:nxr) )
1715
1716             CALL get_variable( id_surf, 'lad', leaf_area_density_f%var,       &
1717                                nxl, nxr, nys, nyn,                            &
1718                                0, leaf_area_density_f%nz-1 )
1719
1720          ELSE
1721             leaf_area_density_f%from_file = .FALSE.
1722          ENDIF
1723
1724!
1725!--       Read basal area density - resolved vegetation
1726          IF ( check_existence( var_names, 'bad' ) )  THEN
1727             basal_area_density_f%from_file = .TRUE.
1728             CALL get_attribute( id_surf, char_fill,                           &
1729                                 basal_area_density_f%fill,                    &
1730                                 .FALSE., 'bad' )
1731!
1732!--          Inquire number of vertical vegetation layer
1733             CALL netcdf_data_input_get_dimension_length( id_surf,             &
1734                                                 basal_area_density_f%nz,      &
1735                                                 'zlad' )
1736!
1737!--          Allocate variable
1738             ALLOCATE( basal_area_density_f%var(0:basal_area_density_f%nz-1,   &
1739                                                nys:nyn,nxl:nxr) )
1740
1741             CALL get_variable( id_surf, 'bad', basal_area_density_f%var,      &
1742                                nxl, nxr, nys, nyn,                            &
1743                                0,  basal_area_density_f%nz-1 )
1744          ELSE
1745             basal_area_density_f%from_file = .FALSE.
1746          ENDIF
1747
1748!
1749!--       Read root area density - resolved vegetation
1750          IF ( check_existence( var_names, 'root_area_dens_r' ) )  THEN
1751             root_area_density_lad_f%from_file = .TRUE.
1752             CALL get_attribute( id_surf, char_fill,                           &
1753                                 root_area_density_lad_f%fill,                 &
1754                                 .FALSE., 'root_area_dens_r' )
1755!
1756!--          Inquire number of vertical soil layers
1757             CALL netcdf_data_input_get_dimension_length( id_surf,             &
1758                                                   root_area_density_lad_f%nz, &
1759                                                  'zsoil' )
1760!
1761!--          Allocate variable
1762             ALLOCATE( root_area_density_lad_f%var                             &
1763                                         (0:root_area_density_lad_f%nz-1,      &
1764                                          nys:nyn,nxl:nxr) )
1765
1766             CALL get_variable( id_surf, 'root_area_dens_r',                   &
1767                                root_area_density_lad_f%var,                   &
1768                                nxl, nxr, nys, nyn,                            &
1769                                0,  root_area_density_lad_f%nz-1 )
1770          ELSE
1771             root_area_density_lad_f%from_file = .FALSE.
1772          ENDIF
1773!
1774!--       Finally, close input file
1775          CALL close_input_file( id_surf )
1776#endif
1777       ENDIF
1778!
1779!--    Deallocate variable list. Will be re-allocated in case further
1780!--    variables are read from file.
1781       IF ( ALLOCATED( var_names ) )  DEALLOCATE( var_names )
1782!
1783!--    Skip the following if no land-surface or urban-surface module are
1784!--    applied. This case, no one of the following variables is used anyway.
1785       IF (  .NOT. land_surface  .AND.  .NOT. urban_surface )  RETURN
1786
1787#if defined ( __netcdf )
1788!
1789!--    Open file in read-only mode
1790       CALL open_read_file( TRIM( input_file_static ) //                       &
1791                            TRIM( coupling_char ) , id_surf )
1792!
1793!--    Inquire all variable names.
1794!--    This will be used to check whether an optional input variable exist
1795!--    or not.
1796       CALL inquire_num_variables( id_surf, num_vars )
1797
1798       ALLOCATE( var_names(1:num_vars) )
1799       CALL inquire_variable_names( id_surf, var_names )
1800!
1801!--    Read vegetation type and required attributes
1802       IF ( check_existence( var_names, 'vegetation_type' ) )  THEN
1803          vegetation_type_f%from_file = .TRUE.
1804          CALL get_attribute( id_surf, char_fill,                              &
1805                              vegetation_type_f%fill,                          &
1806                              .FALSE., 'vegetation_type' )
1807
1808          ALLOCATE ( vegetation_type_f%var(nys:nyn,nxl:nxr)  )
1809
1810          CALL get_variable( id_surf, 'vegetation_type',                       &
1811                             vegetation_type_f%var, nxl, nxr, nys, nyn )
1812       ELSE
1813          vegetation_type_f%from_file = .FALSE.
1814       ENDIF
1815
1816!
1817!--    Read soil type and required attributes
1818       IF ( check_existence( var_names, 'soil_type' ) )  THEN
1819             soil_type_f%from_file = .TRUE.
1820!
1821!--       Note, lod is currently not on file; skip for the moment
1822!           CALL get_attribute( id_surf, char_lod,                       &
1823!                                      soil_type_f%lod,                  &
1824!                                      .FALSE., 'soil_type' )
1825          CALL get_attribute( id_surf, char_fill,                              &
1826                              soil_type_f%fill,                                &
1827                              .FALSE., 'soil_type' )
1828
1829          IF ( soil_type_f%lod == 1 )  THEN
1830
1831             ALLOCATE ( soil_type_f%var_2d(nys:nyn,nxl:nxr)  )
1832
1833             CALL get_variable( id_surf, 'soil_type', soil_type_f%var_2d,      &
1834                                nxl, nxr, nys, nyn )
1835
1836          ELSEIF ( soil_type_f%lod == 2 )  THEN
1837!
1838!--          Obtain number of soil layers from file.
1839             CALL netcdf_data_input_get_dimension_length( id_surf, nz_soil,    &
1840                                                          'zsoil' )
1841
1842             ALLOCATE ( soil_type_f%var_3d(0:nz_soil,nys:nyn,nxl:nxr) )
1843
1844             CALL get_variable( id_surf, 'soil_type', soil_type_f%var_3d,      &
1845                                nxl, nxr, nys, nyn, 0, nz_soil )
1846 
1847          ENDIF
1848       ELSE
1849          soil_type_f%from_file = .FALSE.
1850       ENDIF
1851
1852!
1853!--    Read pavement type and required attributes
1854       IF ( check_existence( var_names, 'pavement_type' ) )  THEN
1855          pavement_type_f%from_file = .TRUE.
1856          CALL get_attribute( id_surf, char_fill,                              &
1857                              pavement_type_f%fill, .FALSE.,                   &
1858                              'pavement_type' )
1859
1860          ALLOCATE ( pavement_type_f%var(nys:nyn,nxl:nxr)  )
1861
1862          CALL get_variable( id_surf, 'pavement_type', pavement_type_f%var,    &
1863                             nxl, nxr, nys, nyn )
1864       ELSE
1865          pavement_type_f%from_file = .FALSE.
1866       ENDIF
1867
1868!
1869!--    Read water type and required attributes
1870       IF ( check_existence( var_names, 'water_type' ) )  THEN
1871          water_type_f%from_file = .TRUE.
1872          CALL get_attribute( id_surf, char_fill, water_type_f%fill,           &
1873                              .FALSE., 'water_type' )
1874
1875          ALLOCATE ( water_type_f%var(nys:nyn,nxl:nxr)  )
1876
1877          CALL get_variable( id_surf, 'water_type', water_type_f%var,          &
1878                             nxl, nxr, nys, nyn )
1879
1880       ELSE
1881          water_type_f%from_file = .FALSE.
1882       ENDIF
1883!
1884!--    Read relative surface fractions of vegetation, pavement and water.
1885       IF ( check_existence( var_names, 'surface_fraction' ) )  THEN
1886          surface_fraction_f%from_file = .TRUE.
1887          CALL get_attribute( id_surf, char_fill,                              &
1888                              surface_fraction_f%fill,                         &
1889                              .FALSE., 'surface_fraction' )
1890!
1891!--       Inquire number of surface fractions
1892          CALL netcdf_data_input_get_dimension_length( id_surf,                &
1893                                                       surface_fraction_f%nf,  &
1894                                                       'nsurface_fraction' )
1895!
1896!--       Allocate dimension array and input array for surface fractions
1897          ALLOCATE( surface_fraction_f%nfracs(0:surface_fraction_f%nf-1) )
1898          ALLOCATE( surface_fraction_f%frac(0:surface_fraction_f%nf-1,         &
1899                                            nys:nyn,nxl:nxr) )
1900!
1901!--       Get dimension of surface fractions
1902          CALL get_variable( id_surf, 'nsurface_fraction',                     &
1903                             surface_fraction_f%nfracs )
1904!
1905!--       Read surface fractions
1906          CALL get_variable( id_surf, 'surface_fraction',                      &
1907                             surface_fraction_f%frac, nxl, nxr, nys, nyn,      &
1908                             0, surface_fraction_f%nf-1 )
1909       ELSE
1910          surface_fraction_f%from_file = .FALSE.
1911       ENDIF
1912!
1913!--    Read building parameters and related information
1914       IF ( check_existence( var_names, 'building_pars' ) )  THEN
1915          building_pars_f%from_file = .TRUE.
1916          CALL get_attribute( id_surf, char_fill,                              &
1917                              building_pars_f%fill,                            &
1918                              .FALSE., 'building_pars' )
1919!
1920!--       Inquire number of building parameters
1921          CALL netcdf_data_input_get_dimension_length( id_surf,                &
1922                                                       building_pars_f%np,     &
1923                                                       'nbuilding_pars' )
1924!
1925!--       Allocate dimension array and input array for building parameters
1926          ALLOCATE( building_pars_f%pars(0:building_pars_f%np-1) )
1927          ALLOCATE( building_pars_f%pars_xy(0:building_pars_f%np-1,            &
1928                                            nys:nyn,nxl:nxr) )
1929!
1930!--       Get dimension of building parameters
1931          CALL get_variable( id_surf, 'nbuilding_pars',                        &
1932                             building_pars_f%pars )
1933!
1934!--       Read building_pars
1935          CALL get_variable( id_surf, 'building_pars',                         &
1936                             building_pars_f%pars_xy, nxl, nxr, nys, nyn,      &
1937                             0, building_pars_f%np-1 )
1938       ELSE
1939          building_pars_f%from_file = .FALSE.
1940       ENDIF
1941
1942!
1943!--    Read albedo type and required attributes
1944       IF ( check_existence( var_names, 'albedo_type' ) )  THEN
1945          albedo_type_f%from_file = .TRUE.
1946          CALL get_attribute( id_surf, char_fill, albedo_type_f%fill,          &
1947                              .FALSE.,  'albedo_type' )
1948
1949          ALLOCATE ( albedo_type_f%var(nys:nyn,nxl:nxr)  )
1950         
1951          CALL get_variable( id_surf, 'albedo_type', albedo_type_f%var,        &
1952                             nxl, nxr, nys, nyn )
1953       ELSE
1954          albedo_type_f%from_file = .FALSE.
1955       ENDIF
1956!
1957!--    Read albedo parameters and related information
1958       IF ( check_existence( var_names, 'albedo_pars' ) )  THEN
1959          albedo_pars_f%from_file = .TRUE.
1960          CALL get_attribute( id_surf, char_fill, albedo_pars_f%fill,          &
1961                              .FALSE., 'albedo_pars' )
1962!
1963!--       Inquire number of albedo parameters
1964          CALL netcdf_data_input_get_dimension_length( id_surf,                &
1965                                                       albedo_pars_f%np,       &
1966                                                       'nalbedo_pars' )
1967!
1968!--       Allocate dimension array and input array for albedo parameters
1969          ALLOCATE( albedo_pars_f%pars(0:albedo_pars_f%np-1) )
1970          ALLOCATE( albedo_pars_f%pars_xy(0:albedo_pars_f%np-1,                &
1971                                          nys:nyn,nxl:nxr) )
1972!
1973!--       Get dimension of albedo parameters
1974          CALL get_variable( id_surf, 'nalbedo_pars', albedo_pars_f%pars )
1975
1976          CALL get_variable( id_surf, 'albedo_pars', albedo_pars_f%pars_xy,    &
1977                             nxl, nxr, nys, nyn,                               &
1978                             0, albedo_pars_f%np-1 )
1979       ELSE
1980          albedo_pars_f%from_file = .FALSE.
1981       ENDIF
1982
1983!
1984!--    Read pavement parameters and related information
1985       IF ( check_existence( var_names, 'pavement_pars' ) )  THEN
1986          pavement_pars_f%from_file = .TRUE.
1987          CALL get_attribute( id_surf, char_fill,                              &
1988                              pavement_pars_f%fill,                            &
1989                              .FALSE., 'pavement_pars' )
1990!
1991!--       Inquire number of pavement parameters
1992          CALL netcdf_data_input_get_dimension_length( id_surf,                &
1993                                                       pavement_pars_f%np,     &
1994                                                       'npavement_pars' )
1995!
1996!--       Allocate dimension array and input array for pavement parameters
1997          ALLOCATE( pavement_pars_f%pars(0:pavement_pars_f%np-1) )
1998          ALLOCATE( pavement_pars_f%pars_xy(0:pavement_pars_f%np-1,            &
1999                                            nys:nyn,nxl:nxr) )
2000!
2001!--       Get dimension of pavement parameters
2002          CALL get_variable( id_surf, 'npavement_pars', pavement_pars_f%pars )
2003
2004          CALL get_variable( id_surf, 'pavement_pars', pavement_pars_f%pars_xy,&
2005                             nxl, nxr, nys, nyn,                               &
2006                             0, pavement_pars_f%np-1 )
2007       ELSE
2008          pavement_pars_f%from_file = .FALSE.
2009       ENDIF
2010
2011!
2012!--    Read pavement subsurface parameters and related information
2013       IF ( check_existence( var_names, 'pavement_subsurface_pars' ) )         &
2014       THEN
2015          pavement_subsurface_pars_f%from_file = .TRUE.
2016          CALL get_attribute( id_surf, char_fill,                              &
2017                              pavement_subsurface_pars_f%fill,                 &
2018                              .FALSE., 'pavement_subsurface_pars' )
2019!
2020!--       Inquire number of parameters
2021          CALL netcdf_data_input_get_dimension_length( id_surf,                &
2022                                                pavement_subsurface_pars_f%np, &
2023                                               'npavement_subsurface_pars' )
2024!
2025!--       Inquire number of soil layers
2026          CALL netcdf_data_input_get_dimension_length( id_surf,                &
2027                                                pavement_subsurface_pars_f%nz, &
2028                                                'zsoil' )
2029!
2030!--       Allocate dimension array and input array for pavement parameters
2031          ALLOCATE( pavement_subsurface_pars_f%pars                            &
2032                            (0:pavement_subsurface_pars_f%np-1) )
2033          ALLOCATE( pavement_subsurface_pars_f%pars_xyz                        &
2034                            (0:pavement_subsurface_pars_f%np-1,                &
2035                             0:pavement_subsurface_pars_f%nz-1,                &
2036                             nys:nyn,nxl:nxr) )
2037!
2038!--       Get dimension of pavement parameters
2039          CALL get_variable( id_surf, 'npavement_subsurface_pars',             &
2040                             pavement_subsurface_pars_f%pars )
2041
2042          CALL get_variable( id_surf, 'pavement_subsurface_pars',              &
2043                             pavement_subsurface_pars_f%pars_xyz,              &
2044                             nxl, nxr, nys, nyn,                               &
2045                             0, pavement_subsurface_pars_f%nz-1,               &
2046                             0, pavement_subsurface_pars_f%np-1 )
2047       ELSE
2048          pavement_subsurface_pars_f%from_file = .FALSE.
2049       ENDIF
2050
2051
2052!
2053!--    Read vegetation parameters and related information
2054       IF ( check_existence( var_names, 'vegetation_pars' ) )  THEN
2055          vegetation_pars_f%from_file = .TRUE.
2056          CALL get_attribute( id_surf, char_fill,                              &
2057                              vegetation_pars_f%fill,                          &
2058                              .FALSE.,  'vegetation_pars' )
2059!
2060!--       Inquire number of vegetation parameters
2061          CALL netcdf_data_input_get_dimension_length( id_surf,                &
2062                                                       vegetation_pars_f%np,   &
2063                                                       'nvegetation_pars' )
2064!
2065!--       Allocate dimension array and input array for surface fractions
2066          ALLOCATE( vegetation_pars_f%pars(0:vegetation_pars_f%np-1) )
2067          ALLOCATE( vegetation_pars_f%pars_xy(0:vegetation_pars_f%np-1,        &
2068                                              nys:nyn,nxl:nxr) )
2069!
2070!--       Get dimension of the parameters
2071          CALL get_variable( id_surf, 'nvegetation_pars',                      &
2072                             vegetation_pars_f%pars )
2073
2074          CALL get_variable( id_surf, 'vegetation_pars',                       &
2075                             vegetation_pars_f%pars_xy, nxl, nxr, nys, nyn,    &
2076                             0, vegetation_pars_f%np-1 )
2077       ELSE
2078          vegetation_pars_f%from_file = .FALSE.
2079       ENDIF
2080
2081!
2082!--    Read root parameters/distribution and related information
2083       IF ( check_existence( var_names, 'soil_pars' ) )  THEN
2084          soil_pars_f%from_file = .TRUE.
2085          CALL get_attribute( id_surf, char_fill,                              &
2086                              soil_pars_f%fill,                                &
2087                              .FALSE., 'soil_pars' )
2088
2089          CALL get_attribute( id_surf, char_lod,                               &
2090                              soil_pars_f%lod,                                 &
2091                              .FALSE., 'soil_pars' )
2092
2093!
2094!--       Inquire number of soil parameters
2095          CALL netcdf_data_input_get_dimension_length( id_surf,                &
2096                                                       soil_pars_f%np,         &
2097                                                       'nsoil_pars' )
2098!
2099!--       Read parameters array
2100          ALLOCATE( soil_pars_f%pars(0:soil_pars_f%np-1) )
2101          CALL get_variable( id_surf, 'nsoil_pars', soil_pars_f%pars )
2102
2103!
2104!--       In case of level of detail 2, also inquire number of vertical
2105!--       soil layers, allocate memory and read the respective dimension
2106          IF ( soil_pars_f%lod == 2 )  THEN
2107             CALL netcdf_data_input_get_dimension_length( id_surf,             &
2108                                                          soil_pars_f%nz,      &
2109                                                          'zsoil' )
2110
2111             ALLOCATE( soil_pars_f%layers(0:soil_pars_f%nz-1) )
2112             CALL get_variable( id_surf, 'zsoil', soil_pars_f%layers )
2113
2114          ENDIF
2115
2116!
2117!--       Read soil parameters, depending on level of detail
2118          IF ( soil_pars_f%lod == 1 )  THEN
2119             ALLOCATE( soil_pars_f%pars_xy(0:soil_pars_f%np-1,                 &
2120                                           nys:nyn,nxl:nxr) )
2121                 
2122             CALL get_variable( id_surf, 'soil_pars', soil_pars_f%pars_xy,     &
2123                                nxl, nxr, nys, nyn, 0, soil_pars_f%np-1 )
2124
2125          ELSEIF ( soil_pars_f%lod == 2 )  THEN
2126             ALLOCATE( soil_pars_f%pars_xyz(0:soil_pars_f%np-1,                &
2127                                            0:soil_pars_f%nz-1,                &
2128                                            nys:nyn,nxl:nxr) )
2129             CALL get_variable( id_surf, 'soil_pars',                          &
2130                                soil_pars_f%pars_xyz,                          &
2131                                nxl, nxr, nys, nyn, 0, soil_pars_f%nz-1,       &
2132                                0, soil_pars_f%np-1 )
2133
2134          ENDIF
2135       ELSE
2136          soil_pars_f%from_file = .FALSE.
2137       ENDIF
2138
2139!
2140!--    Read water parameters and related information
2141       IF ( check_existence( var_names, 'water_pars' ) )  THEN
2142          water_pars_f%from_file = .TRUE.
2143          CALL get_attribute( id_surf, char_fill,                              &
2144                              water_pars_f%fill,                               &
2145                              .FALSE., 'water_pars' )
2146!
2147!--       Inquire number of water parameters
2148          CALL netcdf_data_input_get_dimension_length( id_surf,                &
2149                                                       water_pars_f%np,        &
2150                                                       'nwater_pars' )
2151!
2152!--       Allocate dimension array and input array for water parameters
2153          ALLOCATE( water_pars_f%pars(0:water_pars_f%np-1) )
2154          ALLOCATE( water_pars_f%pars_xy(0:water_pars_f%np-1,                  &
2155                                         nys:nyn,nxl:nxr) )
2156!
2157!--       Get dimension of water parameters
2158          CALL get_variable( id_surf, 'nwater_pars', water_pars_f%pars )
2159
2160          CALL get_variable( id_surf, 'water_pars', water_pars_f%pars_xy,      &
2161                             nxl, nxr, nys, nyn, 0, water_pars_f%np-1 )
2162       ELSE
2163          water_pars_f%from_file = .FALSE.
2164       ENDIF
2165!
2166!--    Read root area density - parametrized vegetation
2167       IF ( check_existence( var_names, 'root_area_dens_s' ) )  THEN
2168          root_area_density_lsm_f%from_file = .TRUE.
2169          CALL get_attribute( id_surf, char_fill,                              &
2170                              root_area_density_lsm_f%fill,                    &
2171                              .FALSE., 'root_area_dens_s' )
2172!
2173!--       Obtain number of soil layers from file and allocate variable
2174          CALL netcdf_data_input_get_dimension_length( id_surf,                &
2175                                                   root_area_density_lsm_f%nz, &
2176                                                   'zsoil' )
2177          ALLOCATE( root_area_density_lsm_f%var                                &
2178                                        (0:root_area_density_lsm_f%nz-1,       &
2179                                         nys:nyn,nxl:nxr) )
2180
2181!
2182!--       Read root-area density
2183          CALL get_variable( id_surf, 'root_area_dens_s',                      &
2184                             root_area_density_lsm_f%var,                      &
2185                             nxl, nxr, nys, nyn,                               &
2186                             0, root_area_density_lsm_f%nz-1 )
2187
2188       ELSE
2189          root_area_density_lsm_f%from_file = .FALSE.
2190       ENDIF
2191!
2192!--    Read street type and street crossing
2193       IF ( check_existence( var_names, 'street_type' ) )  THEN
2194          street_type_f%from_file = .TRUE.
2195          CALL get_attribute( id_surf, char_fill,                              &
2196                              street_type_f%fill, .FALSE.,                     &
2197                              'street_type' )
2198
2199          ALLOCATE ( street_type_f%var(nys:nyn,nxl:nxr)  )
2200         
2201          CALL get_variable( id_surf, 'street_type', street_type_f%var,        &
2202                             nxl, nxr, nys, nyn )
2203       ELSE
2204          street_type_f%from_file = .FALSE.
2205       ENDIF
2206
2207       IF ( check_existence( var_names, 'street_crossing' ) )  THEN
2208          street_crossing_f%from_file = .TRUE.
2209          CALL get_attribute( id_surf, char_fill,                              &
2210                              street_crossing_f%fill, .FALSE.,                 &
2211                              'street_crossing' )
2212
2213          ALLOCATE ( street_crossing_f%var(nys:nyn,nxl:nxr)  )
2214
2215          CALL get_variable( id_surf, 'street_crossing',                       &
2216                             street_crossing_f%var, nxl, nxr, nys, nyn )
2217
2218       ELSE
2219          street_crossing_f%from_file = .FALSE.
2220       ENDIF
2221!
2222!--    Still missing: root_resolved and building_surface_pars.
2223!--    Will be implemented as soon as they are available.
2224
2225!
2226!--    Finally, close input file
2227       CALL close_input_file( id_surf )
2228#endif
2229!
2230!--    End of CPU measurement
2231       CALL cpu_log( log_point_s(82), 'NetCDF input', 'stop' )
2232!
2233!--    Exchange ghost points for surface variables. Therefore, resize
2234!--    variables.
2235       IF ( albedo_type_f%from_file )  THEN
2236          CALL resize_array_2d_int8( albedo_type_f%var, nys, nyn, nxl, nxr )
2237          CALL exchange_horiz_2d_byte( albedo_type_f%var, nys, nyn, nxl, nxr,  &
2238                                       nbgp )
2239       ENDIF
2240       IF ( pavement_type_f%from_file )  THEN
2241          CALL resize_array_2d_int8( pavement_type_f%var, nys, nyn, nxl, nxr )
2242          CALL exchange_horiz_2d_byte( pavement_type_f%var, nys, nyn, nxl, nxr,&
2243                                       nbgp )
2244       ENDIF
2245       IF ( soil_type_f%from_file  .AND.  ALLOCATED( soil_type_f%var_2d ) )  THEN
2246          CALL resize_array_2d_int8( soil_type_f%var_2d, nys, nyn, nxl, nxr )
2247          CALL exchange_horiz_2d_byte( soil_type_f%var_2d, nys, nyn, nxl, nxr, &
2248                                       nbgp )
2249       ENDIF
2250       IF ( vegetation_type_f%from_file )  THEN
2251          CALL resize_array_2d_int8( vegetation_type_f%var, nys, nyn, nxl, nxr )
2252          CALL exchange_horiz_2d_byte( vegetation_type_f%var, nys, nyn, nxl,   &
2253                                       nxr, nbgp )
2254       ENDIF
2255       IF ( water_type_f%from_file )  THEN
2256          CALL resize_array_2d_int8( water_type_f%var, nys, nyn, nxl, nxr )
2257          CALL exchange_horiz_2d_byte( water_type_f%var, nys, nyn, nxl, nxr,   &
2258                                       nbgp )
2259       ENDIF
2260!
2261!--    Exchange ghost points for 3/4-D variables. For the sake of simplicity,
2262!--    loop further dimensions to use 2D exchange routines. Unfortunately this
2263!--    is necessary, else new MPI-data types need to be introduced just for
2264!--    2 variables.
2265       IF ( soil_type_f%from_file  .AND.  ALLOCATED( soil_type_f%var_3d ) )    &
2266       THEN
2267          CALL resize_array_3d_int8( soil_type_f%var_3d, 0, nz_soil,           &
2268                                     nys, nyn, nxl, nxr )
2269          DO  k = 0, nz_soil
2270             CALL exchange_horiz_2d_int(                                       & 
2271                        soil_type_f%var_3d(k,:,:), nys, nyn, nxl, nxr, nbgp )
2272          ENDDO
2273       ENDIF
2274
2275       IF ( surface_fraction_f%from_file )  THEN
2276          CALL resize_array_3d_real( surface_fraction_f%frac,                  &
2277                                     0, surface_fraction_f%nf-1,               &
2278                                     nys, nyn, nxl, nxr )
2279          DO  k = 0, surface_fraction_f%nf-1
2280             CALL exchange_horiz_2d( surface_fraction_f%frac(k,:,:), nbgp )
2281          ENDDO
2282       ENDIF
2283
2284       IF ( building_pars_f%from_file )  THEN         
2285          CALL resize_array_3d_real( building_pars_f%pars_xy,                  &
2286                                     0, building_pars_f%np-1,                  &
2287                                     nys, nyn, nxl, nxr )
2288          DO  k = 0, building_pars_f%np-1
2289             CALL exchange_horiz_2d( building_pars_f%pars_xy(k,:,:), nbgp )
2290          ENDDO
2291       ENDIF
2292
2293       IF ( albedo_pars_f%from_file )  THEN         
2294          CALL resize_array_3d_real( albedo_pars_f%pars_xy,                    &
2295                                     0, albedo_pars_f%np-1,                    &
2296                                     nys, nyn, nxl, nxr )
2297          DO  k = 0, albedo_pars_f%np-1
2298             CALL exchange_horiz_2d( albedo_pars_f%pars_xy(k,:,:), nbgp )
2299          ENDDO
2300       ENDIF
2301
2302       IF ( pavement_pars_f%from_file )  THEN         
2303          CALL resize_array_3d_real( pavement_pars_f%pars_xy,                  &
2304                                     0, pavement_pars_f%np-1,                  &
2305                                     nys, nyn, nxl, nxr )
2306          DO  k = 0, pavement_pars_f%np-1
2307             CALL exchange_horiz_2d( pavement_pars_f%pars_xy(k,:,:), nbgp )
2308          ENDDO
2309       ENDIF
2310
2311       IF ( vegetation_pars_f%from_file )  THEN
2312          CALL resize_array_3d_real( vegetation_pars_f%pars_xy,                &
2313                                     0, vegetation_pars_f%np-1,                &
2314                                     nys, nyn, nxl, nxr )
2315          DO  k = 0, vegetation_pars_f%np-1
2316             CALL exchange_horiz_2d( vegetation_pars_f%pars_xy(k,:,:), nbgp )
2317          ENDDO
2318       ENDIF
2319
2320       IF ( water_pars_f%from_file )  THEN
2321          CALL resize_array_3d_real( water_pars_f%pars_xy,                     &
2322                                     0, water_pars_f%np-1,                     &
2323                                     nys, nyn, nxl, nxr )
2324          DO  k = 0, water_pars_f%np-1
2325             CALL exchange_horiz_2d( water_pars_f%pars_xy(k,:,:), nbgp )
2326          ENDDO
2327       ENDIF
2328
2329       IF ( root_area_density_lsm_f%from_file )  THEN
2330          CALL resize_array_3d_real( root_area_density_lsm_f%var,              &
2331                                     0, root_area_density_lsm_f%nz-1,          &
2332                                     nys, nyn, nxl, nxr )
2333          DO  k = 0, root_area_density_lsm_f%nz-1
2334             CALL exchange_horiz_2d( root_area_density_lsm_f%var(k,:,:), nbgp )
2335          ENDDO
2336       ENDIF
2337
2338       IF ( soil_pars_f%from_file )  THEN
2339          IF ( soil_pars_f%lod == 1 )  THEN
2340         
2341             CALL resize_array_3d_real( soil_pars_f%pars_xy,                   &
2342                                        0, soil_pars_f%np-1,                   &
2343                                        nys, nyn, nxl, nxr )
2344             DO  k = 0, soil_pars_f%np-1
2345                CALL exchange_horiz_2d( soil_pars_f%pars_xy(k,:,:), nbgp )
2346             ENDDO
2347             
2348          ELSEIF ( soil_pars_f%lod == 2 )  THEN
2349             CALL resize_array_4d_real( soil_pars_f%pars_xyz,                  &
2350                                        0, soil_pars_f%np-1,                   &
2351                                        0, soil_pars_f%nz-1,                   &
2352                                        nys, nyn, nxl, nxr )
2353
2354             DO  k2 = 0, soil_pars_f%nz-1
2355                DO  k = 0, soil_pars_f%np-1
2356                   CALL exchange_horiz_2d( soil_pars_f%pars_xyz(k,k2,:,:),     &
2357                                           nbgp )
2358                ENDDO
2359             ENDDO
2360          ENDIF
2361       ENDIF
2362
2363       IF ( pavement_subsurface_pars_f%from_file )  THEN         
2364          CALL resize_array_4d_real( pavement_subsurface_pars_f%pars_xyz,      &
2365                                     0, pavement_subsurface_pars_f%np-1,       &
2366                                     0, pavement_subsurface_pars_f%nz-1,       &
2367                                     nys, nyn, nxl, nxr )
2368
2369          DO  k2 = 0, pavement_subsurface_pars_f%nz-1
2370             DO  k = 0, pavement_subsurface_pars_f%np-1
2371                CALL exchange_horiz_2d(                                        &
2372                           pavement_subsurface_pars_f%pars_xyz(k,k2,:,:), nbgp )
2373             ENDDO
2374          ENDDO
2375       ENDIF
2376
2377    END SUBROUTINE netcdf_data_input_surface_data
2378
2379!------------------------------------------------------------------------------!
2380! Description:
2381! ------------
2382!> Reads uvem lookup table information.
2383!------------------------------------------------------------------------------!
2384    SUBROUTINE netcdf_data_input_uvem
2385       
2386       USE indices,                                                            &
2387           ONLY:  nxl, nxr, nyn, nys
2388
2389       IMPLICIT NONE
2390
2391       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names  !< variable names in static input file
2392
2393
2394       INTEGER(iwp) ::  id_uvem       !< NetCDF id of uvem lookup table input file
2395       INTEGER(iwp) ::  nli = 35      !< dimension length of lookup table in x
2396       INTEGER(iwp) ::  nlj =  9      !< dimension length of lookup table in y
2397       INTEGER(iwp) ::  nlk = 90      !< dimension length of lookup table in z
2398       INTEGER(iwp) ::  num_vars      !< number of variables in netcdf input file
2399!
2400!--    Input via uv exposure model lookup table input
2401       IF ( input_pids_uvem )  THEN
2402
2403#if defined ( __netcdf )
2404!
2405!--       Open file in read-only mode
2406          CALL open_read_file( TRIM( input_file_uvem ) //                    &
2407                               TRIM( coupling_char ), id_uvem )
2408!
2409!--       At first, inquire all variable names.
2410!--       This will be used to check whether an input variable exist or not.
2411          CALL inquire_num_variables( id_uvem, num_vars )
2412!
2413!--       Allocate memory to store variable names and inquire them.
2414          ALLOCATE( var_names(1:num_vars) )
2415          CALL inquire_variable_names( id_uvem, var_names )
2416!
2417!--       uvem integration
2418          IF ( check_existence( var_names, 'int_factors' ) )  THEN
2419             uvem_integration_f%from_file = .TRUE.
2420!
2421!--          Input 2D uvem integration.
2422             ALLOCATE ( uvem_integration_f%var(0:nlj,0:nli)  )
2423             
2424             CALL get_variable( id_uvem, 'int_factors', uvem_integration_f%var, 0, nli, 0, nlj )
2425          ELSE
2426             uvem_integration_f%from_file = .FALSE.
2427          ENDIF
2428!
2429!--       uvem irradiance
2430          IF ( check_existence( var_names, 'irradiance' ) )  THEN
2431             uvem_irradiance_f%from_file = .TRUE.
2432!
2433!--          Input 2D uvem irradiance.
2434             ALLOCATE ( uvem_irradiance_f%var(0:nlk, 0:2)  )
2435             
2436             CALL get_variable( id_uvem, 'irradiance', uvem_irradiance_f%var, 0, 2, 0, nlk )
2437          ELSE
2438             uvem_irradiance_f%from_file = .FALSE.
2439          ENDIF
2440!
2441!--       uvem porjection areas
2442          IF ( check_existence( var_names, 'projarea' ) )  THEN
2443             uvem_projarea_f%from_file = .TRUE.
2444!
2445!--          Input 3D uvem projection area (human geometgry)
2446             ALLOCATE ( uvem_projarea_f%var(0:2,0:nlj,0:nli)  )
2447           
2448             CALL get_variable( id_uvem, 'projarea', uvem_projarea_f%var, 0, nli, 0, nlj, 0, 2 )
2449          ELSE
2450             uvem_projarea_f%from_file = .FALSE.
2451          ENDIF
2452!
2453!--       uvem radiance
2454          IF ( check_existence( var_names, 'radiance' ) )  THEN
2455             uvem_radiance_f%from_file = .TRUE.
2456!
2457!--          Input 3D uvem radiance
2458             ALLOCATE ( uvem_radiance_f%var(0:nlk,0:nlj,0:nli)  )
2459             
2460             CALL get_variable( id_uvem, 'radiance', uvem_radiance_f%var, 0, nli, 0, nlj, 0, nlk )
2461          ELSE
2462             uvem_radiance_f%from_file = .FALSE.
2463          ENDIF
2464!
2465!--       Read building obstruction
2466          IF ( check_existence( var_names, 'obstruction' ) )  THEN
2467             building_obstruction_full%from_file = .TRUE.
2468!--          Input 3D uvem building obstruction
2469              ALLOCATE ( building_obstruction_full%var_3d(0:44,0:2,0:2) )
2470              CALL get_variable( id_uvem, 'obstruction', building_obstruction_full%var_3d,0, 2, 0, 2, 0, 44 )       
2471          ELSE
2472             building_obstruction_full%from_file = .FALSE.
2473          ENDIF
2474!
2475          IF ( check_existence( var_names, 'obstruction' ) )  THEN
2476             building_obstruction_f%from_file = .TRUE.
2477!
2478!--          Input 3D uvem building obstruction
2479             ALLOCATE ( building_obstruction_f%var_3d(0:44,nys:nyn,nxl:nxr) )
2480!
2481             CALL get_variable( id_uvem, 'obstruction', building_obstruction_f%var_3d,      &
2482                                nxl, nxr, nys, nyn, 0, 44 )       
2483          ELSE
2484             building_obstruction_f%from_file = .FALSE.
2485          ENDIF
2486!
2487!--       Close uvem lookup table input file
2488          CALL close_input_file( id_uvem )
2489#else
2490          CONTINUE
2491#endif
2492       ENDIF
2493    END SUBROUTINE netcdf_data_input_uvem
2494
2495!------------------------------------------------------------------------------!
2496! Description:
2497! ------------
2498!> Reads orography and building information.
2499!------------------------------------------------------------------------------!
2500    SUBROUTINE netcdf_data_input_topo
2501
2502       USE control_parameters,                                                 &
2503           ONLY:  message_string, topography
2504
2505       USE grid_variables,                                                     &
2506           ONLY:  dx, dy   
2507           
2508       USE indices,                                                            &
2509           ONLY:  nbgp, nx, nxl, nxr, ny, nyn, nys, nzb
2510
2511
2512       IMPLICIT NONE
2513
2514       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names  !< variable names in static input file
2515
2516
2517       INTEGER(iwp) ::  i             !< running index along x-direction
2518       INTEGER(iwp) ::  ii            !< running index for IO blocks
2519       INTEGER(iwp) ::  id_topo       !< NetCDF id of topograhy input file
2520       INTEGER(iwp) ::  j             !< running index along y-direction
2521       INTEGER(iwp) ::  num_vars      !< number of variables in netcdf input file
2522       INTEGER(iwp) ::  skip_n_rows   !< counting variable to skip rows while reading topography file
2523
2524       REAL(wp) ::  dum           !< dummy variable to skip columns while reading topography file
2525!
2526!--    CPU measurement
2527       CALL cpu_log( log_point_s(83), 'NetCDF/ASCII input topo', 'start' )
2528
2529!
2530!--    Input via palm-input data standard
2531       IF ( input_pids_static )  THEN
2532#if defined ( __netcdf )
2533!
2534!--       Open file in read-only mode
2535          CALL open_read_file( TRIM( input_file_static ) //                    &
2536                               TRIM( coupling_char ), id_topo )
2537!
2538!--       At first, inquire all variable names.
2539!--       This will be used to check whether an  input variable exist
2540!--       or not.
2541          CALL inquire_num_variables( id_topo, num_vars )
2542!
2543!--       Allocate memory to store variable names and inquire them.
2544          ALLOCATE( var_names(1:num_vars) )
2545          CALL inquire_variable_names( id_topo, var_names )
2546!
2547!--       Read x, y - dimensions. Only required for consistency checks.
2548          CALL netcdf_data_input_get_dimension_length( id_topo, dim_static%nx, 'x' )
2549          CALL netcdf_data_input_get_dimension_length( id_topo, dim_static%ny, 'y' )
2550          ALLOCATE( dim_static%x(0:dim_static%nx-1) )
2551          ALLOCATE( dim_static%y(0:dim_static%ny-1) )
2552          CALL get_variable( id_topo, 'x', dim_static%x )
2553          CALL get_variable( id_topo, 'y', dim_static%y )
2554!
2555!--       Check whether dimension size in input file matches the model dimensions
2556          IF ( dim_static%nx-1 /= nx  .OR.  dim_static%ny-1 /= ny )  THEN
2557             message_string = 'Static input file: horizontal dimension in ' // &
2558                              'x- and/or y-direction ' //                      &
2559                              'do not match the respective model dimension'
2560             CALL message( 'netcdf_data_input_mod', 'PA0548', 1, 2, 0, 6, 0 )
2561          ENDIF
2562!
2563!--       Check if grid spacing of provided input data matches the respective
2564!--       grid spacing in the model.
2565          IF ( ABS( dim_static%x(1) - dim_static%x(0) - dx ) > 10E-6_wp  .OR.  &
2566               ABS( dim_static%y(1) - dim_static%y(0) - dy ) > 10E-6_wp )  THEN
2567             message_string = 'Static input file: horizontal grid spacing ' // &
2568                              'in x- and/or y-direction ' //                   &
2569                              'do not match the respective model grid spacing.'
2570             CALL message( 'netcdf_data_input_mod', 'PA0549', 1, 2, 0, 6, 0 )
2571          ENDIF
2572!
2573!--       Terrain height. First, get variable-related _FillValue attribute
2574          IF ( check_existence( var_names, 'zt' ) )  THEN
2575             terrain_height_f%from_file = .TRUE.
2576             CALL get_attribute( id_topo, char_fill, terrain_height_f%fill,    &
2577                                 .FALSE., 'zt' )
2578!
2579!--          Input 2D terrain height.
2580             ALLOCATE ( terrain_height_f%var(nys:nyn,nxl:nxr)  )
2581             
2582             CALL get_variable( id_topo, 'zt', terrain_height_f%var,           &
2583                                nxl, nxr, nys, nyn )
2584
2585          ELSE
2586             terrain_height_f%from_file = .FALSE.
2587          ENDIF
2588
2589!
2590!--       Read building height. First, read its _FillValue attribute,
2591!--       as well as lod attribute
2592          buildings_f%from_file = .FALSE.
2593          IF ( check_existence( var_names, 'buildings_2d' ) )  THEN
2594             buildings_f%from_file = .TRUE.
2595             CALL get_attribute( id_topo, char_lod, buildings_f%lod,           &
2596                                 .FALSE., 'buildings_2d' )
2597
2598             CALL get_attribute( id_topo, char_fill, buildings_f%fill1,        &
2599                                 .FALSE., 'buildings_2d' )
2600
2601!
2602!--          Read 2D buildings
2603             IF ( buildings_f%lod == 1 )  THEN
2604                ALLOCATE ( buildings_f%var_2d(nys:nyn,nxl:nxr) )
2605
2606                CALL get_variable( id_topo, 'buildings_2d',                    &
2607                                   buildings_f%var_2d,                         &
2608                                   nxl, nxr, nys, nyn )
2609             ELSE
2610                message_string = 'NetCDF attribute lod ' //                    &
2611                                 '(level of detail) is not set ' //            &
2612                                 'properly for buildings_2d.'
2613                CALL message( 'netcdf_data_input_mod', 'PA0540',               &
2614                               1, 2, 0, 6, 0 )
2615             ENDIF
2616          ENDIF
2617!
2618!--       If available, also read 3D building information. If both are
2619!--       available, use 3D information.
2620          IF ( check_existence( var_names, 'buildings_3d' ) )  THEN
2621             buildings_f%from_file = .TRUE.
2622             CALL get_attribute( id_topo, char_lod, buildings_f%lod,           &
2623                                 .FALSE., 'buildings_3d' )     
2624
2625             CALL get_attribute( id_topo, char_fill, buildings_f%fill2,        &
2626                                 .FALSE., 'buildings_3d' )
2627
2628             CALL netcdf_data_input_get_dimension_length( id_topo,             &
2629                                                          buildings_f%nz, 'z' )
2630!
2631!--          Read 3D buildings
2632             IF ( buildings_f%lod == 2 )  THEN
2633                ALLOCATE( buildings_f%z(nzb:buildings_f%nz-1) )
2634                CALL get_variable( id_topo, 'z', buildings_f%z )
2635
2636                ALLOCATE( buildings_f%var_3d(nzb:buildings_f%nz-1,             &
2637                                             nys:nyn,nxl:nxr) )
2638                buildings_f%var_3d = 0
2639               
2640                CALL get_variable( id_topo, 'buildings_3d',                    &
2641                                   buildings_f%var_3d,                         &
2642                                   nxl, nxr, nys, nyn, 0, buildings_f%nz-1 )
2643             ELSE
2644                message_string = 'NetCDF attribute lod ' //                    &
2645                                 '(level of detail) is not set ' //            &
2646                                 'properly for buildings_3d.'
2647                CALL message( 'netcdf_data_input_mod', 'PA0541',               &
2648                               1, 2, 0, 6, 0 )
2649             ENDIF
2650          ENDIF
2651!
2652!--       Read building IDs and its FillValue attribute. Further required
2653!--       for mapping buildings on top of orography.
2654          IF ( check_existence( var_names, 'building_id' ) )  THEN
2655             building_id_f%from_file = .TRUE.
2656             CALL get_attribute( id_topo, char_fill,                           &
2657                                 building_id_f%fill, .FALSE.,                  &
2658                                 'building_id' )
2659
2660             ALLOCATE ( building_id_f%var(nys:nyn,nxl:nxr) )
2661             
2662             CALL get_variable( id_topo, 'building_id', building_id_f%var,     &
2663                                nxl, nxr, nys, nyn )
2664          ELSE
2665             building_id_f%from_file = .FALSE.
2666          ENDIF
2667!
2668!--       Read building_type and required attributes.
2669          IF ( check_existence( var_names, 'building_type' ) )  THEN
2670             building_type_f%from_file = .TRUE.
2671             CALL get_attribute( id_topo, char_fill,                           &
2672                                 building_type_f%fill, .FALSE.,                &
2673                                 'building_type' )
2674
2675             ALLOCATE ( building_type_f%var(nys:nyn,nxl:nxr) )
2676
2677             CALL get_variable( id_topo, 'building_type', building_type_f%var, &
2678                                nxl, nxr, nys, nyn )
2679
2680          ELSE
2681             building_type_f%from_file = .FALSE.
2682          ENDIF
2683!
2684!--       Close topography input file
2685          CALL close_input_file( id_topo )
2686#else
2687          CONTINUE
2688#endif
2689!
2690!--    ASCII input
2691       ELSEIF ( TRIM( topography ) == 'read_from_file' )  THEN
2692             
2693          DO  ii = 0, io_blocks-1
2694             IF ( ii == io_group )  THEN
2695
2696                OPEN( 90, FILE='TOPOGRAPHY_DATA'//TRIM( coupling_char ),       &
2697                      STATUS='OLD', FORM='FORMATTED', ERR=10 )
2698!
2699!--             Read topography PE-wise. Rows are read from nyn to nys, columns
2700!--             are read from nxl to nxr. At first, ny-nyn rows need to be skipped.
2701                skip_n_rows = 0
2702                DO WHILE ( skip_n_rows < ny - nyn )
2703                   READ( 90, * )
2704                   skip_n_rows = skip_n_rows + 1
2705                ENDDO
2706!
2707!--             Read data from nyn to nys and nxl to nxr. Therefore, skip
2708!--             column until nxl-1 is reached
2709                ALLOCATE ( buildings_f%var_2d(nys:nyn,nxl:nxr) )
2710                DO  j = nyn, nys, -1
2711                   READ( 90, *, ERR=11, END=11 )                               &
2712                                   ( dum, i = 0, nxl-1 ),                      &
2713                                   ( buildings_f%var_2d(j,i), i = nxl, nxr )
2714                ENDDO
2715
2716                GOTO 12
2717
2718 10             message_string = 'file TOPOGRAPHY_DATA'//                      &
2719                                 TRIM( coupling_char )// ' does not exist'
2720                CALL message( 'netcdf_data_input_mod', 'PA0208', 1, 2, 0, 6, 0 )
2721
2722 11             message_string = 'errors in file TOPOGRAPHY_DATA'//            &
2723                                 TRIM( coupling_char )
2724                CALL message( 'netcdf_data_input_mod', 'PA0209', 2, 2, 0, 6, 0 )
2725
2726 12             CLOSE( 90 )
2727                buildings_f%from_file = .TRUE.
2728
2729             ENDIF
2730#if defined( __parallel )
2731             CALL MPI_BARRIER( comm2d, ierr )
2732#endif
2733          ENDDO
2734
2735       ENDIF
2736!
2737!--    End of CPU measurement
2738       CALL cpu_log( log_point_s(83), 'NetCDF/ASCII input topo', 'stop' )
2739!
2740!--    Check for minimum requirement to setup building topography. If buildings
2741!--    are provided, also an ID and a type are required.
2742!--    Note, doing this check in check_parameters
2743!--    will be too late (data will be used for grid inititialization before).
2744       IF ( input_pids_static )  THEN
2745          IF ( buildings_f%from_file  .AND.                                    &
2746               .NOT. building_id_f%from_file )  THEN
2747             message_string = 'If building heights are prescribed in ' //      &
2748                              'static input file, also an ID is required.'
2749             CALL message( 'netcdf_data_input_mod', 'PA0542', 1, 2, 0, 6, 0 )
2750          ENDIF
2751       ENDIF
2752!
2753!--    In case no terrain height is provided by static input file, allocate
2754!--    array nevertheless and set terrain height to 0, which simplifies
2755!--    topography initialization.
2756       IF ( .NOT. terrain_height_f%from_file )  THEN
2757          ALLOCATE ( terrain_height_f%var(nys:nyn,nxl:nxr) )
2758          terrain_height_f%var = 0.0_wp
2759       ENDIF
2760!
2761!--    Finally, exchange 1 ghost point for building ID and type.
2762!--    In case of non-cyclic boundary conditions set Neumann conditions at the
2763!--    lateral boundaries.
2764       IF ( building_id_f%from_file )  THEN
2765          CALL resize_array_2d_int32( building_id_f%var, nys, nyn, nxl, nxr )
2766          CALL exchange_horiz_2d_int( building_id_f%var, nys, nyn, nxl, nxr,   &
2767                                      nbgp )
2768       ENDIF
2769
2770       IF ( building_type_f%from_file )  THEN
2771          CALL resize_array_2d_int8( building_type_f%var, nys, nyn, nxl, nxr )
2772          CALL exchange_horiz_2d_byte( building_type_f%var, nys, nyn, nxl, nxr,   &
2773                                       nbgp )
2774       ENDIF
2775
2776    END SUBROUTINE netcdf_data_input_topo
2777
2778!------------------------------------------------------------------------------!
2779! Description:
2780! ------------
2781!> Reads initialization data of u, v, w, pt, q, geostrophic wind components,
2782!> as well as soil moisture and soil temperature, derived from larger-scale
2783!> model (COSMO) by Inifor.
2784!------------------------------------------------------------------------------!
2785    SUBROUTINE netcdf_data_input_init_3d
2786
2787       USE arrays_3d,                                                          &
2788           ONLY:  q, pt, u, v, w, zu, zw
2789
2790       USE control_parameters,                                                 &
2791           ONLY:  air_chemistry, bc_lr_cyc, bc_ns_cyc, humidity,               &
2792                  message_string, neutral
2793
2794       USE indices,                                                            &
2795           ONLY:  nx, nxl, nxlu, nxr, ny, nyn, nys, nysv, nzb, nz, nzt
2796
2797       IMPLICIT NONE
2798
2799       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names
2800
2801       LOGICAL      ::  dynamic_3d = .TRUE. !< flag indicating that 3D data is read from dynamic file
2802       
2803       INTEGER(iwp) ::  id_dynamic !< NetCDF id of dynamic input file
2804       INTEGER(iwp) ::  n          !< running index for chemistry variables
2805       INTEGER(iwp) ::  num_vars   !< number of variables in netcdf input file
2806
2807       LOGICAL      ::  check_passed !< flag indicating if a check passed
2808
2809!
2810!--    Skip routine if no input file with dynamic input data is available.
2811       IF ( .NOT. input_pids_dynamic )  RETURN
2812!
2813!--    Please note, Inifor is designed to provide initial data for u and v for
2814!--    the prognostic grid points in case of lateral Dirichlet conditions.
2815!--    This means that Inifor provides data from nxlu:nxr (for u) and
2816!--    from nysv:nyn (for v) at the left and south domain boundary, respectively.
2817!--    However, as work-around for the moment, PALM will run with cyclic
2818!--    conditions and will be initialized with data provided by Inifor
2819!--    boundaries in case of Dirichlet.
2820!--    Hence, simply set set nxlu/nysv to 1 (will be reset to its original value
2821!--    at the end of this routine.
2822       IF ( bc_lr_cyc  .AND.  nxl == 0 )  nxlu = 1
2823       IF ( bc_ns_cyc  .AND.  nys == 0 )  nysv = 1
2824
2825!
2826!--    CPU measurement
2827       CALL cpu_log( log_point_s(85), 'NetCDF input init', 'start' )
2828
2829#if defined ( __netcdf )
2830!
2831!--    Open file in read-only mode
2832       CALL open_read_file( TRIM( input_file_dynamic ) //                      &
2833                            TRIM( coupling_char ), id_dynamic )
2834
2835!
2836!--    At first, inquire all variable names.
2837       CALL inquire_num_variables( id_dynamic, num_vars )
2838!
2839!--    Allocate memory to store variable names.
2840       ALLOCATE( var_names(1:num_vars) )
2841       CALL inquire_variable_names( id_dynamic, var_names )
2842!
2843!--    Read vertical dimension of scalar und w grid.
2844       CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%nzu, 'z'     )
2845       CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%nzw, 'zw'    )
2846!
2847!--    Read also the horizontal dimensions. These are used just used fo
2848!--    checking the compatibility with the PALM grid before reading.
2849       CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%nx,  'x'  )
2850       CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%nxu, 'xu' )
2851       CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%ny,  'y'  )
2852       CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%nyv, 'yv' )
2853
2854!
2855!--    Check for correct horizontal and vertical dimension. Please note,
2856!--    checks are performed directly here and not called from
2857!--    check_parameters as some varialbes are still not allocated there.
2858!--    Moreover, please note, u- and v-grid has 1 grid point less on
2859!--    Inifor grid.
2860       IF ( init_3d%nx-1 /= nx  .OR.  init_3d%nxu-1 /= nx - 1  .OR.            &
2861            init_3d%ny-1 /= ny  .OR.  init_3d%nyv-1 /= ny - 1 )  THEN
2862          message_string = 'Number of inifor horizontal grid points  '//       &
2863                           'does not match the number of numeric grid '//      &
2864                           'points.'
2865          CALL message( 'netcdf_data_input_mod', 'PA0543', 1, 2, 0, 6, 0 )
2866       ENDIF
2867
2868       IF ( init_3d%nzu /= nz )  THEN
2869          message_string = 'Number of inifor vertical grid points ' //         &
2870                           'does not match the number of numeric grid '//      &
2871                           'points.'
2872          CALL message( 'netcdf_data_input_mod', 'PA0543', 1, 2, 0, 6, 0 )
2873       ENDIF
2874!
2875!--    Read vertical dimensions. Later, these are required for eventual
2876!--    inter- and extrapolations of the initialization data.
2877       IF ( check_existence( var_names, 'z' ) )  THEN
2878          ALLOCATE( init_3d%zu_atmos(1:init_3d%nzu) )
2879          CALL get_variable( id_dynamic, 'z', init_3d%zu_atmos )
2880       ENDIF
2881       IF ( check_existence( var_names, 'zw' ) )  THEN
2882          ALLOCATE( init_3d%zw_atmos(1:init_3d%nzw) )
2883          CALL get_variable( id_dynamic, 'zw', init_3d%zw_atmos )
2884       ENDIF
2885!
2886!--    Check for consistency between vertical coordinates in dynamic
2887!--    driver and numeric grid.
2888!--    Please note, depending on compiler options both may be
2889!--    equal up to a certain threshold, and differences between
2890!--    the numeric grid and vertical coordinate in the driver can built-
2891!--    up to 10E-1-10E-0 m. For this reason, the check is performed not
2892!--    for exactly matching values.
2893       IF ( ANY( ABS( zu(1:nzt)   - init_3d%zu_atmos(1:init_3d%nzu) )    &
2894                      > 10E-1 )  .OR.                                    &
2895            ANY( ABS( zw(1:nzt-1) - init_3d%zw_atmos(1:init_3d%nzw) )    &
2896                      > 10E-1 ) )  THEN
2897          message_string = 'Vertical grid in dynamic driver does not '// &
2898                           'match the numeric grid.'
2899          CALL message( 'netcdf_data_input_mod', 'PA0543', 1, 2, 0, 6, 0 )
2900       ENDIF
2901!
2902!--    Read initial geostrophic wind components at
2903!--    t = 0 (index 1 in file).
2904       IF ( check_existence( var_names, 'ls_forcing_ug' ) )  THEN
2905          ALLOCATE( init_3d%ug_init(nzb:nzt+1) )
2906          init_3d%ug_init = 0.0_wp
2907
2908          CALL get_variable_pr( id_dynamic, 'ls_forcing_ug', 1,          &
2909                                init_3d%ug_init(1:nzt) )
2910!
2911!--       Set top-boundary condition (Neumann)
2912          init_3d%ug_init(nzt+1) = init_3d%ug_init(nzt)
2913
2914          init_3d%from_file_ug = .TRUE.
2915       ELSE
2916          init_3d%from_file_ug = .FALSE.
2917       ENDIF
2918       IF ( check_existence( var_names, 'ls_forcing_vg' ) )  THEN
2919          ALLOCATE( init_3d%vg_init(nzb:nzt+1) )
2920          init_3d%vg_init = 0.0_wp
2921
2922          CALL get_variable_pr( id_dynamic, 'ls_forcing_vg', 1,          &
2923                                init_3d%vg_init(1:nzt) )
2924!
2925!--       Set top-boundary condition (Neumann)
2926          init_3d%vg_init(nzt+1) = init_3d%vg_init(nzt)
2927
2928          init_3d%from_file_vg = .TRUE.
2929       ELSE
2930          init_3d%from_file_vg = .FALSE.
2931       ENDIF
2932!
2933!--    Read inital 3D data of u, v, w, pt and q,
2934!--    derived from COSMO model. Read PE-wise yz-slices.
2935!--    Please note, the u-, v- and w-component are defined on different
2936!--    grids with one element less in the x-, y-,
2937!--    and z-direction, respectively. Hence, reading is subdivided
2938!--    into separate loops. 
2939!--    Read u-component
2940       IF ( check_existence( var_names, 'init_atmosphere_u' ) )  THEN
2941!
2942!--       Read attributes for the fill value and level-of-detail
2943          CALL get_attribute( id_dynamic, char_fill, init_3d%fill_u,           &
2944                              .FALSE., 'init_atmosphere_u' )
2945          CALL get_attribute( id_dynamic, char_lod, init_3d%lod_u,             &
2946                              .FALSE., 'init_atmosphere_u' )
2947!
2948!--       level-of-detail 1 - read initialization profile
2949          IF ( init_3d%lod_u == 1 )  THEN
2950             ALLOCATE( init_3d%u_init(nzb:nzt+1) )
2951             init_3d%u_init = 0.0_wp
2952
2953             CALL get_variable( id_dynamic, 'init_atmosphere_u',               &
2954                                init_3d%u_init(nzb+1:nzt) )
2955!
2956!--          Set top-boundary condition (Neumann)
2957             init_3d%u_init(nzt+1) = init_3d%u_init(nzt)
2958!
2959!--       level-of-detail 2 - read 3D initialization data
2960          ELSEIF ( init_3d%lod_u == 2 )  THEN
2961             CALL get_variable( id_dynamic, 'init_atmosphere_u',               &
2962                                u(nzb+1:nzt,nys:nyn,nxlu:nxr),                 &
2963                                nxlu, nys+1, nzb+1,                            &
2964                                nxr-nxlu+1, nyn-nys+1, init_3d%nzu,            &
2965                                dynamic_3d )
2966!
2967!--          Set value at leftmost model grid point nxl = 0. This is because
2968!--          Inifor provides data only from 1:nx-1 since it assumes non-cyclic
2969!--          conditions.
2970             IF ( nxl == 0 )                                                   &
2971                u(nzb+1:nzt,nys:nyn,nxl) = u(nzb+1:nzt,nys:nyn,nxlu)
2972!
2973!--          Set bottom and top-boundary
2974             u(nzb,:,:)   = u(nzb+1,:,:)
2975             u(nzt+1,:,:) = u(nzt,:,:)
2976             
2977          ENDIF
2978          init_3d%from_file_u = .TRUE.
2979       ELSE
2980          message_string = 'Missing initial data for u-component'
2981          CALL message( 'netcdf_data_input_mod', 'PA0544', 1, 2, 0, 6, 0 )
2982       ENDIF
2983!
2984!--    Read v-component
2985       IF ( check_existence( var_names, 'init_atmosphere_v' ) )  THEN
2986!
2987!--       Read attributes for the fill value and level-of-detail
2988          CALL get_attribute( id_dynamic, char_fill, init_3d%fill_v,           &
2989                              .FALSE., 'init_atmosphere_v' )
2990          CALL get_attribute( id_dynamic, char_lod, init_3d%lod_v,             &
2991                              .FALSE., 'init_atmosphere_v' )
2992!
2993!--       level-of-detail 1 - read initialization profile
2994          IF ( init_3d%lod_v == 1 )  THEN
2995             ALLOCATE( init_3d%v_init(nzb:nzt+1) )
2996             init_3d%v_init = 0.0_wp
2997
2998             CALL get_variable( id_dynamic, 'init_atmosphere_v',               &
2999                                init_3d%v_init(nzb+1:nzt) )
3000!
3001!--          Set top-boundary condition (Neumann)
3002             init_3d%v_init(nzt+1) = init_3d%v_init(nzt)
3003!
3004!--       level-of-detail 2 - read 3D initialization data
3005          ELSEIF ( init_3d%lod_v == 2 )  THEN
3006         
3007             CALL get_variable( id_dynamic, 'init_atmosphere_v',               &
3008                                v(nzb+1:nzt,nysv:nyn,nxl:nxr),                 &
3009                                nxl+1, nysv, nzb+1,                            &
3010                                nxr-nxl+1, nyn-nysv+1, init_3d%nzu,            &
3011                                dynamic_3d )
3012!
3013!--          Set value at southmost model grid point nys = 0. This is because
3014!--          Inifor provides data only from 1:ny-1 since it assumes non-cyclic
3015!--          conditions.
3016             IF ( nys == 0 )                                                   &
3017                v(nzb+1:nzt,nys,nxl:nxr) = v(nzb+1:nzt,nysv,nxl:nxr)                               
3018!
3019!--          Set bottom and top-boundary
3020             v(nzb,:,:)   = v(nzb+1,:,:)
3021             v(nzt+1,:,:) = v(nzt,:,:)
3022             
3023          ENDIF
3024          init_3d%from_file_v = .TRUE.
3025       ELSE
3026          message_string = 'Missing initial data for v-component'
3027          CALL message( 'netcdf_data_input_mod', 'PA0544', 1, 2, 0, 6, 0 )
3028       ENDIF
3029!
3030!--    Read w-component
3031       IF ( check_existence( var_names, 'init_atmosphere_w' ) )  THEN
3032!
3033!--       Read attributes for the fill value and level-of-detail
3034          CALL get_attribute( id_dynamic, char_fill, init_3d%fill_w,           &
3035                              .FALSE., 'init_atmosphere_w' )
3036          CALL get_attribute( id_dynamic, char_lod, init_3d%lod_w,             &
3037                              .FALSE., 'init_atmosphere_w' )
3038!
3039!--       level-of-detail 1 - read initialization profile
3040          IF ( init_3d%lod_w == 1 )  THEN
3041             ALLOCATE( init_3d%w_init(nzb:nzt+1) )
3042             init_3d%w_init = 0.0_wp
3043
3044             CALL get_variable( id_dynamic, 'init_atmosphere_w',               &
3045                                init_3d%w_init(nzb+1:nzt-1) )
3046!
3047!--          Set top-boundary condition (Neumann)
3048             init_3d%w_init(nzt:nzt+1) = init_3d%w_init(nzt-1)
3049!
3050!--       level-of-detail 2 - read 3D initialization data
3051          ELSEIF ( init_3d%lod_w == 2 )  THEN
3052
3053             CALL get_variable( id_dynamic, 'init_atmosphere_w',                &
3054                                w(nzb+1:nzt-1,nys:nyn,nxl:nxr),                 &
3055                                nxl+1, nys+1, nzb+1,                            &
3056                                nxr-nxl+1, nyn-nys+1, init_3d%nzw,              &
3057                                dynamic_3d )
3058!
3059!--          Set bottom and top-boundary                               
3060             w(nzb,:,:)   = 0.0_wp 
3061             w(nzt,:,:)   = w(nzt-1,:,:)
3062             w(nzt+1,:,:) = w(nzt-1,:,:)
3063
3064          ENDIF
3065          init_3d%from_file_w = .TRUE.
3066       ELSE
3067          message_string = 'Missing initial data for w-component'
3068          CALL message( 'netcdf_data_input_mod', 'PA0544', 1, 2, 0, 6, 0 )
3069       ENDIF
3070!
3071!--    Read potential temperature
3072       IF ( .NOT. neutral )  THEN
3073          IF ( check_existence( var_names, 'init_atmosphere_pt' ) )  THEN
3074!
3075!--          Read attributes for the fill value and level-of-detail
3076             CALL get_attribute( id_dynamic, char_fill, init_3d%fill_pt,       &
3077                                 .FALSE., 'init_atmosphere_pt' )
3078             CALL get_attribute( id_dynamic, char_lod, init_3d%lod_pt,         &
3079                                 .FALSE., 'init_atmosphere_pt' )
3080!
3081!--          level-of-detail 1 - read initialization profile
3082             IF ( init_3d%lod_pt == 1 )  THEN
3083                ALLOCATE( init_3d%pt_init(nzb:nzt+1) )
3084
3085                CALL get_variable( id_dynamic, 'init_atmosphere_pt',           &
3086                                   init_3d%pt_init(nzb+1:nzt) )
3087!
3088!--             Set Neumann top and surface boundary condition for initial
3089!--             profil
3090                init_3d%pt_init(nzb)   = init_3d%pt_init(nzb+1)
3091                init_3d%pt_init(nzt+1) = init_3d%pt_init(nzt)
3092!
3093!--          level-of-detail 2 - read 3D initialization data
3094             ELSEIF ( init_3d%lod_pt == 2 )  THEN
3095
3096                CALL get_variable( id_dynamic, 'init_atmosphere_pt',           &
3097                                   pt(nzb+1:nzt,nys:nyn,nxl:nxr),              &
3098                                   nxl+1, nys+1, nzb+1,                        &
3099                                   nxr-nxl+1, nyn-nys+1, init_3d%nzu,          &
3100                                   dynamic_3d )
3101                                   
3102!
3103!--             Set bottom and top-boundary
3104                pt(nzb,:,:)   = pt(nzb+1,:,:)
3105                pt(nzt+1,:,:) = pt(nzt,:,:)             
3106
3107             ENDIF
3108             init_3d%from_file_pt = .TRUE.
3109          ELSE
3110             message_string = 'Missing initial data for ' //                   &
3111                              'potential temperature'
3112             CALL message( 'netcdf_data_input_mod', 'PA0544', 1, 2, 0, 6, 0 )
3113          ENDIF
3114       ENDIF
3115!
3116!--    Read mixing ratio
3117       IF ( humidity )  THEN
3118          IF ( check_existence( var_names, 'init_atmosphere_qv' ) )  THEN
3119!
3120!--          Read attributes for the fill value and level-of-detail
3121             CALL get_attribute( id_dynamic, char_fill, init_3d%fill_q,        &
3122                                 .FALSE., 'init_atmosphere_qv' )
3123             CALL get_attribute( id_dynamic, char_lod, init_3d%lod_q,          &
3124                                 .FALSE., 'init_atmosphere_qv' )
3125!
3126!--          level-of-detail 1 - read initialization profile
3127             IF ( init_3d%lod_q == 1 )  THEN
3128                ALLOCATE( init_3d%q_init(nzb:nzt+1) )
3129
3130                CALL get_variable( id_dynamic, 'init_atmosphere_qv',           &
3131                                    init_3d%q_init(nzb+1:nzt) )
3132!
3133!--             Set bottom and top boundary condition (Neumann)
3134                init_3d%q_init(nzb)   = init_3d%q_init(nzb+1)
3135                init_3d%q_init(nzt+1) = init_3d%q_init(nzt)
3136!
3137!--          level-of-detail 2 - read 3D initialization data
3138             ELSEIF ( init_3d%lod_q == 2 )  THEN
3139             
3140                CALL get_variable( id_dynamic, 'init_atmosphere_qv',           &
3141                                   q(nzb+1:nzt,nys:nyn,nxl:nxr),               &
3142                                   nxl+1, nys+1, nzb+1,                        &
3143                                   nxr-nxl+1, nyn-nys+1, init_3d%nzu,          &
3144                                   dynamic_3d )
3145                                   
3146!
3147!--             Set bottom and top-boundary
3148                q(nzb,:,:)   = q(nzb+1,:,:)
3149                q(nzt+1,:,:) = q(nzt,:,:)
3150               
3151             ENDIF
3152             init_3d%from_file_q = .TRUE.
3153          ELSE
3154             message_string = 'Missing initial data for ' //                   &
3155                              'mixing ratio'
3156             CALL message( 'netcdf_data_input_mod', 'PA0544', 1, 2, 0, 6, 0 )
3157          ENDIF
3158       ENDIF       
3159!
3160!--    Read chemistry variables.
3161!--    Please note, for the moment, only LOD=1 is allowed
3162       IF ( air_chemistry )  THEN
3163!
3164!--       Allocate chemistry input profiles, as well as arrays for fill values
3165!--       and LOD's.
3166          ALLOCATE( init_3d%chem_init(nzb:nzt+1,                               &
3167                                      1:UBOUND(init_3d%var_names_chem, 1 )) )
3168          ALLOCATE( init_3d%fill_chem(1:UBOUND(init_3d%var_names_chem, 1)) )   
3169          ALLOCATE( init_3d%lod_chem(1:UBOUND(init_3d%var_names_chem, 1))  ) 
3170         
3171          DO  n = 1, UBOUND(init_3d%var_names_chem, 1)
3172             IF ( check_existence( var_names,                                  &
3173                                   TRIM( init_3d%var_names_chem(n) ) ) )  THEN
3174!
3175!--             Read attributes for the fill value and level-of-detail
3176                CALL get_attribute( id_dynamic, char_fill,                     &
3177                                    init_3d%fill_chem(n),                      &
3178                                    .FALSE.,                                   &
3179                                    TRIM( init_3d%var_names_chem(n) ) )
3180                CALL get_attribute( id_dynamic, char_lod,                      &
3181                                    init_3d%lod_chem(n),                       &
3182                                    .FALSE.,                                   &
3183                                    TRIM( init_3d%var_names_chem(n) ) )
3184!
3185!--             Give message that only LOD=1 is allowed.
3186                IF ( init_3d%lod_chem(n) /= 1 )  THEN               
3187                   message_string = 'For chemistry variables only LOD=1 is ' //&
3188                                    'allowed.'
3189                   CALL message( 'netcdf_data_input_mod', 'PA0586',            &
3190                                 1, 2, 0, 6, 0 )
3191                ENDIF
3192!
3193!--             level-of-detail 1 - read initialization profile
3194                CALL get_variable( id_dynamic,                                 &
3195                                   TRIM( init_3d%var_names_chem(n) ),          &
3196                                   init_3d%chem_init(nzb+1:nzt,n) )
3197!
3198!--             Set bottom and top boundary condition (Neumann)
3199                init_3d%chem_init(nzb,n)   = init_3d%chem_init(nzb+1,n)
3200                init_3d%chem_init(nzt+1,n) = init_3d%chem_init(nzt,n)
3201               
3202                init_3d%from_file_chem(n) = .TRUE.
3203             ENDIF
3204          ENDDO
3205       ENDIF
3206!
3207!--    Close input file
3208       CALL close_input_file( id_dynamic )
3209#endif
3210!
3211!--    End of CPU measurement
3212       CALL cpu_log( log_point_s(85), 'NetCDF input init', 'stop' )
3213!
3214!--    Finally, check if the input data has any fill values. Please note,
3215!--    checks depend on the LOD of the input data.
3216       IF ( init_3d%from_file_u )  THEN
3217          check_passed = .TRUE.
3218          IF ( init_3d%lod_u == 1 )  THEN
3219             IF ( ANY( init_3d%u_init(nzb+1:nzt+1) == init_3d%fill_u ) )       &
3220                check_passed = .FALSE.
3221          ELSEIF ( init_3d%lod_u == 2 )  THEN
3222             IF ( ANY( u(nzb+1:nzt+1,nys:nyn,nxlu:nxr) == init_3d%fill_u ) )   &
3223                check_passed = .FALSE.
3224          ENDIF
3225          IF ( .NOT. check_passed )  THEN
3226             message_string = 'NetCDF input for init_atmosphere_u must ' //    &
3227                              'not contain any _FillValues'
3228             CALL message( 'netcdf_data_input_mod', 'PA0545', 2, 2, 0, 6, 0 )
3229          ENDIF
3230       ENDIF
3231
3232       IF ( init_3d%from_file_v )  THEN
3233          check_passed = .TRUE.
3234          IF ( init_3d%lod_v == 1 )  THEN
3235             IF ( ANY( init_3d%v_init(nzb+1:nzt+1) == init_3d%fill_v ) )       &
3236                check_passed = .FALSE.
3237          ELSEIF ( init_3d%lod_v == 2 )  THEN
3238             IF ( ANY( v(nzb+1:nzt+1,nysv:nyn,nxl:nxr) == init_3d%fill_v ) )   &
3239                check_passed = .FALSE.
3240          ENDIF
3241          IF ( .NOT. check_passed )  THEN
3242             message_string = 'NetCDF input for init_atmosphere_v must ' //    &
3243                              'not contain any _FillValues'
3244             CALL message( 'netcdf_data_input_mod', 'PA0545', 2, 2, 0, 6, 0 )
3245          ENDIF
3246       ENDIF
3247
3248       IF ( init_3d%from_file_w )  THEN
3249          check_passed = .TRUE.
3250          IF ( init_3d%lod_w == 1 )  THEN
3251             IF ( ANY( init_3d%w_init(nzb+1:nzt) == init_3d%fill_w ) )         &
3252                check_passed = .FALSE.
3253          ELSEIF ( init_3d%lod_w == 2 )  THEN
3254             IF ( ANY( w(nzb+1:nzt,nys:nyn,nxl:nxr) == init_3d%fill_w ) )      &
3255                check_passed = .FALSE.
3256          ENDIF
3257          IF ( .NOT. check_passed )  THEN
3258             message_string = 'NetCDF input for init_atmosphere_w must ' //    &
3259                              'not contain any _FillValues'
3260             CALL message( 'netcdf_data_input_mod', 'PA0545', 2, 2, 0, 6, 0 )
3261          ENDIF
3262       ENDIF
3263
3264       IF ( init_3d%from_file_pt )  THEN
3265          check_passed = .TRUE.
3266          IF ( init_3d%lod_pt == 1 )  THEN
3267             IF ( ANY( init_3d%pt_init(nzb+1:nzt+1) == init_3d%fill_pt ) )     &
3268                check_passed = .FALSE.
3269          ELSEIF ( init_3d%lod_pt == 2 )  THEN
3270             IF ( ANY( pt(nzb+1:nzt+1,nys:nyn,nxl:nxr) == init_3d%fill_pt ) )  &
3271                check_passed = .FALSE.
3272          ENDIF
3273          IF ( .NOT. check_passed )  THEN
3274             message_string = 'NetCDF input for init_atmosphere_pt must ' //   &
3275                              'not contain any _FillValues'
3276             CALL message( 'netcdf_data_input_mod', 'PA0545', 2, 2, 0, 6, 0 )
3277          ENDIF
3278       ENDIF
3279
3280       IF ( init_3d%from_file_q )  THEN
3281          check_passed = .TRUE.
3282          IF ( init_3d%lod_q == 1 )  THEN
3283             IF ( ANY( init_3d%q_init(nzb+1:nzt+1) == init_3d%fill_q ) )       &
3284                check_passed = .FALSE.
3285          ELSEIF ( init_3d%lod_q == 2 )  THEN
3286             IF ( ANY( q(nzb+1:nzt+1,nys:nyn,nxl:nxr) == init_3d%fill_q ) )    &
3287                check_passed = .FALSE.
3288          ENDIF
3289          IF ( .NOT. check_passed )  THEN
3290             message_string = 'NetCDF input for init_atmosphere_q must ' //    &
3291                              'not contain any _FillValues'
3292             CALL message( 'netcdf_data_input_mod', 'PA0545', 2, 2, 0, 6, 0 )
3293          ENDIF
3294       ENDIF
3295!
3296!--    Workaround for cyclic conditions. Please see above for further explanation.
3297       IF ( bc_lr_cyc  .AND.  nxl == 0 )  nxlu = nxl
3298       IF ( bc_ns_cyc  .AND.  nys == 0 )  nysv = nys
3299
3300    END SUBROUTINE netcdf_data_input_init_3d
3301   
3302!------------------------------------------------------------------------------!
3303! Description:
3304! ------------
3305!> Reads initialization data of u, v, w, pt, q, geostrophic wind components,
3306!> as well as soil moisture and soil temperature, derived from larger-scale
3307!> model (COSMO) by Inifor.
3308!------------------------------------------------------------------------------!
3309    SUBROUTINE netcdf_data_input_init_lsm
3310
3311       USE control_parameters,                                                 &
3312           ONLY:  message_string
3313
3314       USE indices,                                                            &
3315           ONLY:  nx, nxl, nxr, ny, nyn, nys
3316
3317       IMPLICIT NONE
3318
3319       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names !< string containing all variables on file
3320     
3321       INTEGER(iwp) ::  id_dynamic !< NetCDF id of dynamic input file
3322       INTEGER(iwp) ::  num_vars   !< number of variables in netcdf input file
3323
3324!
3325!--    Skip routine if no input file with dynamic input data is available.
3326       IF ( .NOT. input_pids_dynamic )  RETURN
3327!
3328!--    CPU measurement
3329       CALL cpu_log( log_point_s(85), 'NetCDF input init', 'start' )
3330
3331#if defined ( __netcdf )
3332!
3333!--    Open file in read-only mode
3334       CALL open_read_file( TRIM( input_file_dynamic ) //                      &
3335                            TRIM( coupling_char ), id_dynamic )
3336
3337!
3338!--    At first, inquire all variable names.
3339       CALL inquire_num_variables( id_dynamic, num_vars )
3340!
3341!--    Allocate memory to store variable names.
3342       ALLOCATE( var_names(1:num_vars) )
3343       CALL inquire_variable_names( id_dynamic, var_names )
3344!
3345!--    Read vertical dimension for soil depth.
3346       IF ( check_existence( var_names, 'zsoil' ) )                            &
3347          CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%nzs,&
3348                                                       'zsoil' )
3349!
3350!--    Read also the horizontal dimensions required for soil initialization.
3351!--    Please note, in case of non-nested runs or in case of root domain,
3352!--    these data is already available, but will be read again for the sake
3353!--    of clearness.
3354       CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%nx,    &
3355                                                    'x'  )
3356       CALL netcdf_data_input_get_dimension_length( id_dynamic, init_3d%ny,    &
3357                                                    'y'  )
3358!
3359!--    Check for correct horizontal and vertical dimension. Please note,
3360!--    in case of non-nested runs or in case of root domain, these checks
3361!--    are already performed
3362       IF ( init_3d%nx-1 /= nx  .OR.  init_3d%ny-1 /= ny )  THEN
3363          message_string = 'Number of inifor horizontal grid points  '//       &
3364                           'does not match the number of numeric grid points.'
3365          CALL message( 'netcdf_data_input_mod', 'PA0543', 1, 2, 0, 6, 0 )
3366       ENDIF
3367!
3368!--    Read vertical dimensions. Later, these are required for eventual
3369!--    inter- and extrapolations of the initialization data.
3370       IF ( check_existence( var_names, 'zsoil' ) )  THEN
3371          ALLOCATE( init_3d%z_soil(1:init_3d%nzs) )
3372          CALL get_variable( id_dynamic, 'zsoil', init_3d%z_soil )
3373       ENDIF
3374!
3375!--    Read initial data for soil moisture
3376       IF ( check_existence( var_names, 'init_soil_m' ) )  THEN
3377!
3378!--       Read attributes for the fill value and level-of-detail
3379          CALL get_attribute( id_dynamic, char_fill,                           &
3380                              init_3d%fill_msoil,                              &
3381                              .FALSE., 'init_soil_m' )
3382          CALL get_attribute( id_dynamic, char_lod,                            &
3383                              init_3d%lod_msoil,                               &
3384                              .FALSE., 'init_soil_m' )
3385!
3386!--       level-of-detail 1 - read initialization profile
3387          IF ( init_3d%lod_msoil == 1 )  THEN
3388             ALLOCATE( init_3d%msoil_1d(0:init_3d%nzs-1) )
3389
3390             CALL get_variable( id_dynamic, 'init_soil_m',                     &
3391                                init_3d%msoil_1d(0:init_3d%nzs-1) )
3392!
3393!--       level-of-detail 2 - read 3D initialization data
3394          ELSEIF ( init_3d%lod_msoil == 2 )  THEN
3395             ALLOCATE ( init_3d%msoil_3d(0:init_3d%nzs-1,nys:nyn,nxl:nxr) )
3396
3397            CALL get_variable( id_dynamic, 'init_soil_m',                      &   
3398                             init_3d%msoil_3d(0:init_3d%nzs-1,nys:nyn,nxl:nxr),&
3399                             nxl, nxr, nys, nyn, 0, init_3d%nzs-1 )
3400
3401          ENDIF
3402          init_3d%from_file_msoil = .TRUE.
3403       ENDIF
3404!
3405!--    Read soil temperature
3406       IF ( check_existence( var_names, 'init_soil_t' ) )  THEN
3407!
3408!--       Read attributes for the fill value and level-of-detail
3409          CALL get_attribute( id_dynamic, char_fill,                           &
3410                              init_3d%fill_tsoil,                              &
3411                              .FALSE., 'init_soil_t' )
3412          CALL get_attribute( id_dynamic, char_lod,                            &
3413                              init_3d%lod_tsoil,                               &
3414                              .FALSE., 'init_soil_t' )
3415!
3416!--       level-of-detail 1 - read initialization profile
3417          IF ( init_3d%lod_tsoil == 1 )  THEN
3418             ALLOCATE( init_3d%tsoil_1d(0:init_3d%nzs-1) )
3419
3420             CALL get_variable( id_dynamic, 'init_soil_t',                     &
3421                                init_3d%tsoil_1d(0:init_3d%nzs-1) )
3422
3423!
3424!--       level-of-detail 2 - read 3D initialization data
3425          ELSEIF ( init_3d%lod_tsoil == 2 )  THEN
3426             ALLOCATE ( init_3d%tsoil_3d(0:init_3d%nzs-1,nys:nyn,nxl:nxr) )
3427             
3428             CALL get_variable( id_dynamic, 'init_soil_t',                     &   
3429                             init_3d%tsoil_3d(0:init_3d%nzs-1,nys:nyn,nxl:nxr),&
3430                             nxl, nxr, nys, nyn, 0, init_3d%nzs-1 )
3431          ENDIF
3432          init_3d%from_file_tsoil = .TRUE.
3433       ENDIF
3434!
3435!--    Close input file
3436       CALL close_input_file( id_dynamic )
3437#endif
3438!
3439!--    End of CPU measurement
3440       CALL cpu_log( log_point_s(85), 'NetCDF input init', 'stop' )
3441
3442    END SUBROUTINE netcdf_data_input_init_lsm   
3443
3444!------------------------------------------------------------------------------!
3445! Description:
3446! ------------
3447!> Reads data at lateral and top boundaries derived from larger-scale model
3448!> (COSMO) by Inifor.
3449!------------------------------------------------------------------------------!
3450    SUBROUTINE netcdf_data_input_offline_nesting
3451
3452       USE control_parameters,                                                 &
3453           ONLY:  air_chemistry, bc_dirichlet_l, bc_dirichlet_n,               &
3454                  bc_dirichlet_r, bc_dirichlet_s, humidity, neutral,           &
3455                  nesting_offline, time_since_reference_point
3456
3457       USE indices,                                                            &
3458           ONLY:  nxl, nxlu, nxr, nyn, nys, nysv, nzb, nzt
3459
3460       IMPLICIT NONE
3461       
3462       INTEGER(iwp) ::  id_dynamic !< NetCDF id of dynamic input file
3463       INTEGER(iwp) ::  n          !< running index for chemistry variables
3464       INTEGER(iwp) ::  num_vars   !< number of variables in netcdf input file
3465       INTEGER(iwp) ::  t          !< running index time dimension
3466!
3467!--    Skip input if no forcing from larger-scale models is applied.
3468       IF ( .NOT. nesting_offline )  RETURN
3469
3470!
3471!--    CPU measurement
3472       CALL cpu_log( log_point_s(86), 'NetCDF input forcing', 'start' )
3473
3474#if defined ( __netcdf )
3475!
3476!--    Open file in read-only mode
3477       CALL open_read_file( TRIM( input_file_dynamic ) //                      &
3478                            TRIM( coupling_char ), id_dynamic )
3479!
3480!--    Initialize INIFOR forcing.
3481       IF ( .NOT. nest_offl%init )  THEN
3482!
3483!--       At first, inquire all variable names.
3484          CALL inquire_num_variables( id_dynamic, num_vars )
3485!
3486!--       Allocate memory to store variable names.
3487          ALLOCATE( nest_offl%var_names(1:num_vars) )
3488          CALL inquire_variable_names( id_dynamic, nest_offl%var_names )
3489!
3490!--       Read time dimension, allocate memory and finally read time array
3491          CALL netcdf_data_input_get_dimension_length( id_dynamic,             &
3492                                                       nest_offl%nt, 'time' )
3493
3494          IF ( check_existence( nest_offl%var_names, 'time' ) )  THEN
3495             ALLOCATE( nest_offl%time(0:nest_offl%nt-1) )
3496             CALL get_variable( id_dynamic, 'time', nest_offl%time )
3497          ENDIF
3498!
3499!--       Read vertical dimension of scalar und w grid
3500          CALL netcdf_data_input_get_dimension_length( id_dynamic,             &
3501                                                       nest_offl%nzu, 'z' )
3502          CALL netcdf_data_input_get_dimension_length( id_dynamic,             &
3503                                                       nest_offl%nzw, 'zw' )
3504
3505          IF ( check_existence( nest_offl%var_names, 'z' ) )  THEN
3506             ALLOCATE( nest_offl%zu_atmos(1:nest_offl%nzu) )
3507             CALL get_variable( id_dynamic, 'z', nest_offl%zu_atmos )
3508          ENDIF
3509          IF ( check_existence( nest_offl%var_names, 'zw' ) )  THEN
3510             ALLOCATE( nest_offl%zw_atmos(1:nest_offl%nzw) )
3511             CALL get_variable( id_dynamic, 'zw', nest_offl%zw_atmos )
3512          ENDIF
3513
3514!
3515!--       Read surface pressure
3516          IF ( check_existence( nest_offl%var_names,                           &
3517                                'surface_forcing_surface_pressure' ) )  THEN
3518             ALLOCATE( nest_offl%surface_pressure(0:nest_offl%nt-1) )
3519             CALL get_variable( id_dynamic,                                    &
3520                                'surface_forcing_surface_pressure',            &
3521                                nest_offl%surface_pressure )
3522          ENDIF
3523!
3524!--       Set control flag to indicate that initialization is already done
3525          nest_offl%init = .TRUE.
3526
3527       ENDIF
3528
3529!
3530!--    Obtain time index for current input starting at 0.
3531!--    @todo: At the moment INIFOR and simulated time correspond
3532!--           to each other. If required, adjust to daytime.
3533       nest_offl%tind = MINLOC( ABS( nest_offl%time -                          &
3534                                     time_since_reference_point ), DIM = 1 )   &
3535                        - 1
3536       nest_offl%tind_p = nest_offl%tind + 1       
3537!
3538!--    Read geostrophic wind components
3539       DO  t = nest_offl%tind, nest_offl%tind_p
3540          CALL get_variable_pr( id_dynamic, 'ls_forcing_ug', t+1,              &
3541                                nest_offl%ug(t-nest_offl%tind,nzb+1:nzt) )
3542          CALL get_variable_pr( id_dynamic, 'ls_forcing_vg', t+1,              &
3543                                nest_offl%vg(t-nest_offl%tind,nzb+1:nzt) )
3544       ENDDO
3545!
3546!--    Read data at lateral and top boundaries. Please note, at left and
3547!--    right domain boundary, yz-layers are read for u, v, w, pt and q.
3548!--    For the v-component, the data starts at nysv, while for the other
3549!--    quantities the data starts at nys. This is equivalent at the north
3550!--    and south domain boundary for the u-component.
3551!--    Further, lateral data is not accessed by parallel IO, indicated by the
3552!--    last passed flag in the subroutine get_variable(). This is because
3553!--    not every PE participates in this collective blocking read operation.
3554       IF ( bc_dirichlet_l )  THEN
3555          CALL get_variable( id_dynamic, 'ls_forcing_left_u',                  &
3556                           nest_offl%u_left(0:1,nzb+1:nzt,nys:nyn),            &
3557                           nys+1, nzb+1, nest_offl%tind+1,                     &
3558                           nyn-nys+1, nest_offl%nzu, 2, .FALSE. )
3559     
3560          CALL get_variable( id_dynamic, 'ls_forcing_left_v',                  &
3561                           nest_offl%v_left(0:1,nzb+1:nzt,nysv:nyn),           &
3562                           nysv, nzb+1, nest_offl%tind+1,                      &
3563                           nyn-nysv+1, nest_offl%nzu, 2, .FALSE. )
3564
3565          CALL get_variable( id_dynamic, 'ls_forcing_left_w',                  &
3566                           nest_offl%w_left(0:1,nzb+1:nzt-1,nys:nyn),          &
3567                           nys+1, nzb+1, nest_offl%tind+1,                     &
3568                           nyn-nys+1, nest_offl%nzw, 2, .FALSE. )
3569
3570          IF ( .NOT. neutral )  THEN
3571             CALL get_variable( id_dynamic, 'ls_forcing_left_pt',              &
3572                           nest_offl%pt_left(0:1,nzb+1:nzt,nys:nyn),           &
3573                           nys+1, nzb+1, nest_offl%tind+1,                     &
3574                           nyn-nys+1, nest_offl%nzu, 2, .FALSE. )
3575          ENDIF
3576
3577          IF ( humidity )  THEN
3578             CALL get_variable( id_dynamic, 'ls_forcing_left_qv',              &
3579                           nest_offl%q_left(0:1,nzb+1:nzt,nys:nyn),            &
3580                           nys+1, nzb+1, nest_offl%tind+1,                     &
3581                           nyn-nys+1, nest_offl%nzu, 2, .FALSE. )
3582          ENDIF
3583         
3584          IF ( air_chemistry )  THEN
3585             DO  n = 1, UBOUND(nest_offl%var_names_chem_l, 1)
3586                IF ( check_existence( nest_offl%var_names,                     &
3587                                      nest_offl%var_names_chem_l(n) ) )        &
3588                THEN
3589                   CALL get_variable( id_dynamic,                              &
3590                              TRIM( nest_offl%var_names_chem_l(n) ),           &
3591                              nest_offl%chem_left(0:1,nzb+1:nzt,nys:nyn,n),    &
3592                              nys+1, nzb+1, nest_offl%tind+1,                  &
3593                              nyn-nys+1, nest_offl%nzu, 2, .FALSE. )
3594                   nest_offl%chem_from_file_l(n) = .TRUE.
3595                ENDIF
3596             ENDDO
3597          ENDIF
3598
3599       ENDIF
3600
3601       IF ( bc_dirichlet_r )  THEN
3602          CALL get_variable( id_dynamic, 'ls_forcing_right_u',                 &
3603                           nest_offl%u_right(0:1,nzb+1:nzt,nys:nyn),           &
3604                           nys+1, nzb+1, nest_offl%tind+1,                     &
3605                           nyn-nys+1, nest_offl%nzu, 2, .FALSE. )
3606                           
3607          CALL get_variable( id_dynamic, 'ls_forcing_right_v',                 &
3608                           nest_offl%v_right(0:1,nzb+1:nzt,nysv:nyn),          &
3609                           nysv, nzb+1, nest_offl%tind+1,                      &
3610                           nyn-nysv+1, nest_offl%nzu, 2, .FALSE. )
3611                           
3612          CALL get_variable( id_dynamic, 'ls_forcing_right_w',                 &
3613                           nest_offl%w_right(0:1,nzb+1:nzt-1,nys:nyn),         &
3614                           nys+1, nzb+1, nest_offl%tind+1,                     &
3615                           nyn-nys+1, nest_offl%nzw, 2, .FALSE. )
3616                           
3617          IF ( .NOT. neutral )  THEN
3618             CALL get_variable( id_dynamic, 'ls_forcing_right_pt',             &
3619                           nest_offl%pt_right(0:1,nzb+1:nzt,nys:nyn),          &
3620                           nys+1, nzb+1, nest_offl%tind+1,                     &
3621                           nyn-nys+1, nest_offl%nzu, 2, .FALSE. )
3622          ENDIF
3623          IF ( humidity )  THEN
3624             CALL get_variable( id_dynamic, 'ls_forcing_right_qv',             &
3625                           nest_offl%q_right(0:1,nzb+1:nzt,nys:nyn),           &
3626                           nys+1, nzb+1, nest_offl%tind+1,                     &
3627                           nyn-nys+1, nest_offl%nzu, 2, .FALSE. )
3628          ENDIF
3629         
3630          IF ( air_chemistry )  THEN
3631             DO  n = 1, UBOUND(nest_offl%var_names_chem_r, 1)
3632                IF ( check_existence( nest_offl%var_names,                     &
3633                                      nest_offl%var_names_chem_r(n) ) )        &
3634                THEN
3635                   CALL get_variable( id_dynamic,                              &
3636                              TRIM( nest_offl%var_names_chem_r(n) ),           &
3637                              nest_offl%chem_right(0:1,nzb+1:nzt,nys:nyn,n),   &
3638                              nys+1, nzb+1, nest_offl%tind+1,                  &
3639                              nyn-nys+1, nest_offl%nzu, 2, .FALSE. )
3640                   nest_offl%chem_from_file_r(n) = .TRUE.
3641                ENDIF
3642             ENDDO
3643          ENDIF
3644       ENDIF
3645
3646       IF ( bc_dirichlet_n )  THEN
3647       
3648          CALL get_variable( id_dynamic, 'ls_forcing_north_u',                 &
3649                           nest_offl%u_north(0:1,nzb+1:nzt,nxlu:nxr),          &
3650                           nxlu, nzb+1, nest_offl%tind+1,                      &
3651                           nxr-nxlu+1, nest_offl%nzu, 2, .FALSE. )
3652                           
3653          CALL get_variable( id_dynamic, 'ls_forcing_north_v',                 &
3654                           nest_offl%v_north(0:1,nzb+1:nzt,nxl:nxr),           &
3655                           nxl+1, nzb+1, nest_offl%tind+1,                     &
3656                           nxr-nxl+1, nest_offl%nzu, 2, .FALSE. )
3657                           
3658          CALL get_variable( id_dynamic, 'ls_forcing_north_w',                 &
3659                           nest_offl%w_north(0:1,nzb+1:nzt-1,nxl:nxr),         &
3660                           nxl+1, nzb+1, nest_offl%tind+1,                     &
3661                           nxr-nxl+1, nest_offl%nzw, 2, .FALSE. )
3662                           
3663          IF ( .NOT. neutral )  THEN
3664             CALL get_variable( id_dynamic, 'ls_forcing_north_pt',             &
3665                           nest_offl%pt_north(0:1,nzb+1:nzt,nxl:nxr),          &
3666                           nxl+1, nzb+1, nest_offl%tind+1,                     &
3667                           nxr-nxl+1, nest_offl%nzu, 2, .FALSE. )
3668          ENDIF
3669          IF ( humidity )  THEN
3670             CALL get_variable( id_dynamic, 'ls_forcing_north_qv',             &
3671                           nest_offl%q_north(0:1,nzb+1:nzt,nxl:nxr),           &
3672                           nxl+1, nzb+1, nest_offl%tind+1,                     &
3673                           nxr-nxl+1, nest_offl%nzu, 2, .FALSE. )
3674          ENDIF
3675         
3676          IF ( air_chemistry )  THEN
3677             DO  n = 1, UBOUND(nest_offl%var_names_chem_n, 1)
3678                IF ( check_existence( nest_offl%var_names,                     &
3679                                      nest_offl%var_names_chem_n(n) ) )        &
3680                THEN
3681                   CALL get_variable( id_dynamic,                              &
3682                              TRIM( nest_offl%var_names_chem_n(n) ),           &
3683                              nest_offl%chem_north(0:1,nzb+1:nzt,nxl:nxr,n),   &
3684                              nxl+1, nzb+1, nest_offl%tind+1,                  &
3685                              nxr-nxl+1, nest_offl%nzu, 2, .FALSE. )
3686                   nest_offl%chem_from_file_n(n) = .TRUE.
3687                ENDIF
3688             ENDDO
3689          ENDIF
3690       ENDIF
3691
3692       IF ( bc_dirichlet_s )  THEN
3693          CALL get_variable( id_dynamic, 'ls_forcing_south_u',                 &
3694                           nest_offl%u_south(0:1,nzb+1:nzt,nxlu:nxr),          &
3695                           nxlu, nzb+1, nest_offl%tind+1,                      &
3696                           nxr-nxlu+1, nest_offl%nzu, 2, .FALSE. )
3697
3698          CALL get_variable( id_dynamic, 'ls_forcing_south_v',                 &
3699                           nest_offl%v_south(0:1,nzb+1:nzt,nxl:nxr),           &
3700                           nxl+1, nzb+1, nest_offl%tind+1,                     &
3701                           nxr-nxl+1, nest_offl%nzu, 2, .FALSE. )
3702                           
3703          CALL get_variable( id_dynamic, 'ls_forcing_south_w',                 &
3704                           nest_offl%w_south(0:1,nzb+1:nzt-1,nxl:nxr),         &
3705                           nxl+1, nzb+1, nest_offl%tind+1,                     &
3706                           nxr-nxl+1, nest_offl%nzw, 2, .FALSE. )
3707                           
3708          IF ( .NOT. neutral )  THEN
3709             CALL get_variable( id_dynamic, 'ls_forcing_south_pt',             &
3710                           nest_offl%pt_south(0:1,nzb+1:nzt,nxl:nxr),          &
3711                           nxl+1, nzb+1, nest_offl%tind+1,                     &
3712                           nxr-nxl+1, nest_offl%nzu, 2, .FALSE. )
3713          ENDIF
3714          IF ( humidity )  THEN
3715             CALL get_variable( id_dynamic, 'ls_forcing_south_qv',             &
3716                           nest_offl%q_south(0:1,nzb+1:nzt,nxl:nxr),           &
3717                           nxl+1, nzb+1, nest_offl%tind+1,                     &
3718                           nxr-nxl+1, nest_offl%nzu, 2, .FALSE. )
3719          ENDIF
3720         
3721          IF ( air_chemistry )  THEN
3722             DO  n = 1, UBOUND(nest_offl%var_names_chem_s, 1)
3723                IF ( check_existence( nest_offl%var_names,                     &
3724                                      nest_offl%var_names_chem_s(n) ) )        &
3725                THEN
3726                   CALL get_variable( id_dynamic,                              &
3727                              TRIM( nest_offl%var_names_chem_s(n) ),           &
3728                              nest_offl%chem_south(0:1,nzb+1:nzt,nxl:nxr,n),   &
3729                              nxl+1, nzb+1, nest_offl%tind+1,                  &
3730                              nxr-nxl+1, nest_offl%nzu, 2, .FALSE. )
3731                   nest_offl%chem_from_file_s(n) = .TRUE.
3732                ENDIF
3733             ENDDO
3734          ENDIF
3735       ENDIF
3736
3737!
3738!--    Top boundary
3739       CALL get_variable( id_dynamic, 'ls_forcing_top_u',                      &
3740                             nest_offl%u_top(0:1,nys:nyn,nxlu:nxr),            &
3741                             nxlu, nys+1, nest_offl%tind+1,                    &
3742                             nxr-nxlu+1, nyn-nys+1, 2, .TRUE. )
3743
3744       CALL get_variable( id_dynamic, 'ls_forcing_top_v',                      &
3745                             nest_offl%v_top(0:1,nysv:nyn,nxl:nxr),            &
3746                             nxl+1, nysv, nest_offl%tind+1,                    &
3747                             nxr-nxl+1, nyn-nysv+1, 2, .TRUE. )
3748                             
3749       CALL get_variable( id_dynamic, 'ls_forcing_top_w',                      &
3750                             nest_offl%w_top(0:1,nys:nyn,nxl:nxr),             &
3751                             nxl+1, nys+1, nest_offl%tind+1,                   &
3752                             nxr-nxl+1, nyn-nys+1, 2, .TRUE. )
3753                             
3754       IF ( .NOT. neutral )  THEN
3755          CALL get_variable( id_dynamic, 'ls_forcing_top_pt',                  &
3756                                nest_offl%pt_top(0:1,nys:nyn,nxl:nxr),         &
3757                                nxl+1, nys+1, nest_offl%tind+1,                &
3758                                nxr-nxl+1, nyn-nys+1, 2, .TRUE. )
3759       ENDIF
3760       IF ( humidity )  THEN
3761          CALL get_variable( id_dynamic, 'ls_forcing_top_qv',                  &
3762                                nest_offl%q_top(0:1,nys:nyn,nxl:nxr),          &
3763                                nxl+1, nys+1, nest_offl%tind+1,                &
3764                                nxr-nxl+1, nyn-nys+1, 2, .TRUE. )
3765       ENDIF
3766       
3767       IF ( air_chemistry )  THEN
3768          DO  n = 1, UBOUND(nest_offl%var_names_chem_t, 1)
3769             IF ( check_existence( nest_offl%var_names,                     &
3770                                   nest_offl%var_names_chem_t(n) ) )  THEN     
3771                CALL get_variable( id_dynamic,                                 &
3772                              TRIM( nest_offl%var_names_chem_t(n) ),           &
3773                              nest_offl%chem_top(0:1,nys:nyn,nxl:nxr,n),       &
3774                              nxl+1, nys+1, nest_offl%tind+1,                  &
3775                              nxr-nxl+1, nyn-nys+1, 2, .TRUE. )
3776                nest_offl%chem_from_file_t(n) = .TRUE.
3777             ENDIF
3778          ENDDO
3779       ENDIF
3780
3781!
3782!--    Close input file
3783       CALL close_input_file( id_dynamic )
3784#endif
3785!
3786!--    End of CPU measurement
3787       CALL cpu_log( log_point_s(86), 'NetCDF input forcing', 'stop' )
3788
3789    END SUBROUTINE netcdf_data_input_offline_nesting
3790
3791
3792!------------------------------------------------------------------------------!
3793! Description:
3794! ------------
3795!> Checks input file for consistency and minimum requirements.
3796!------------------------------------------------------------------------------!
3797    SUBROUTINE netcdf_data_input_check_dynamic
3798
3799       USE control_parameters,                                                 &
3800           ONLY:  initializing_actions, message_string, nesting_offline 
3801
3802       IMPLICIT NONE
3803
3804!
3805!--    In case of forcing, check whether dynamic input file is present
3806       IF ( .NOT. input_pids_dynamic  .AND.  nesting_offline  )  THEN
3807          message_string = 'nesting_offline = .TRUE. requires dynamic '  //    &
3808                            'input file ' //                                   &
3809                            TRIM( input_file_dynamic ) // TRIM( coupling_char )
3810          CALL message( 'netcdf_data_input_mod', 'PA0546', 1, 2, 0, 6, 0 )
3811       ENDIF
3812!
3813!--    Dynamic input file must also be present if initialization via inifor is
3814!--    prescribed.
3815       IF ( .NOT. input_pids_dynamic  .AND.                                    &
3816            TRIM( initializing_actions ) == 'inifor' )  THEN
3817          message_string = 'initializing_actions = inifor requires dynamic ' //&
3818                           'input file ' // TRIM( input_file_dynamic ) //      &
3819                           TRIM( coupling_char )
3820          CALL message( 'netcdf_data_input_mod', 'PA0547', 1, 2, 0, 6, 0 )
3821       ENDIF
3822
3823    END SUBROUTINE netcdf_data_input_check_dynamic
3824
3825!------------------------------------------------------------------------------!
3826! Description:
3827! ------------
3828!> Checks input file for consistency and minimum requirements.
3829!------------------------------------------------------------------------------!
3830    SUBROUTINE netcdf_data_input_check_static
3831
3832       USE arrays_3d,                                                          &
3833           ONLY:  zu
3834
3835       USE control_parameters,                                                 &
3836           ONLY:  land_surface, message_string, urban_surface
3837
3838       USE indices,                                                            &
3839           ONLY:  nxl, nxr, nyn, nys
3840
3841       IMPLICIT NONE
3842
3843       INTEGER(iwp) ::  i      !< loop index along x-direction
3844       INTEGER(iwp) ::  j      !< loop index along y-direction
3845       INTEGER(iwp) ::  n_surf !< number of different surface types at given location
3846
3847       LOGICAL      ::  check_passed !< flag indicating if a check passed
3848
3849!
3850!--    Return if no static input file is available
3851       IF ( .NOT. input_pids_static )  RETURN
3852!
3853!--    Check for correct dimension of surface_fractions, should run from 0-2.
3854       IF ( surface_fraction_f%from_file )  THEN
3855          IF ( surface_fraction_f%nf-1 > 2 )  THEN
3856             message_string = 'nsurface_fraction must not be larger than 3.' 
3857             CALL message( 'netcdf_data_input_mod', 'PA0580', 1, 2, 0, 6, 0 )
3858          ENDIF
3859       ENDIF
3860!
3861!--    Check orography for fill-values. For the moment, give an error message.
3862!--    More advanced methods, e.g. a nearest neighbor algorithm as used in GIS
3863!--    systems might be implemented later.
3864!--    Please note, if no terrain height is provided, it is set to 0.
3865       IF ( ANY( terrain_height_f%var == terrain_height_f%fill ) )  THEN
3866          message_string = 'NetCDF variable zt is not ' //                     &
3867                           'allowed to have missing data'
3868          CALL message( 'netcdf_data_input_mod', 'PA0550', 2, 2, myid, 6, 0 )
3869       ENDIF
3870!
3871!--    Check for negative terrain heights
3872       IF ( ANY( terrain_height_f%var < 0.0_wp ) )  THEN
3873          message_string = 'NetCDF variable zt is not ' //                     &
3874                           'allowed to have negative values'
3875          CALL message( 'netcdf_data_input_mod', 'PA0551', 2, 2, myid, 6, 0 )
3876       ENDIF
3877!
3878!--    If 3D buildings are read, check if building information is consistent
3879!--    to numeric grid.
3880       IF ( buildings_f%from_file )  THEN
3881          IF ( buildings_f%lod == 2 )  THEN
3882             IF ( buildings_f%nz > SIZE( zu ) )  THEN
3883                message_string = 'Reading 3D building data - too much ' //     &
3884                                 'data points along the vertical coordinate.'
3885                CALL message( 'netcdf_data_input_mod', 'PA0552', 2, 2, 0, 6, 0 )
3886             ENDIF
3887
3888             IF ( ANY( ABS( buildings_f%z(0:buildings_f%nz-1) -                &
3889                       zu(0:buildings_f%nz-1) ) > 1E-6_wp ) )  THEN
3890                message_string = 'Reading 3D building data - vertical ' //     &
3891                                 'coordinate do not match numeric grid.'
3892                CALL message( 'netcdf_data_input_mod', 'PA0553', 2, 2, myid, 6, 0 )
3893             ENDIF
3894          ENDIF
3895       ENDIF
3896
3897!
3898!--    Skip further checks concerning buildings and natural surface properties
3899!--    if no urban surface and land surface model are applied.
3900       IF (  .NOT. land_surface  .AND.  .NOT. urban_surface )  RETURN
3901!
3902!--    Check for minimum requirement of surface-classification data in case
3903!--    static input file is used.
3904       IF ( ( .NOT. vegetation_type_f%from_file  .OR.                          &
3905              .NOT. pavement_type_f%from_file    .OR.                          &
3906              .NOT. water_type_f%from_file       .OR.                          &
3907              .NOT. soil_type_f%from_file             ) .OR.                   &
3908             ( urban_surface  .AND.  .NOT. building_type_f%from_file ) )  THEN
3909          message_string = 'Minimum requirement for surface classification ' //&
3910                           'is not fulfilled. At least ' //                    &
3911                           'vegetation_type, pavement_type, ' //               &
3912                           'soil_type and water_type are '//                   &
3913                           'required. If urban-surface model is applied, ' //  &
3914                           'also building_type is required'
3915          CALL message( 'netcdf_data_input_mod', 'PA0554', 1, 2, 0, 6, 0 )
3916       ENDIF
3917!
3918!--    Check for general availability of input variables.
3919!--    If vegetation_type is 0 at any location, vegetation_pars as well as
3920!--    root_area_dens_s are required.
3921       IF ( vegetation_type_f%from_file )  THEN
3922          IF ( ANY( vegetation_type_f%var == 0 ) )  THEN
3923             IF ( .NOT. vegetation_pars_f%from_file )  THEN
3924                message_string = 'If vegetation_type = 0 at any location, ' // &
3925                                 'vegetation_pars is required'
3926                CALL message( 'netcdf_data_input_mod', 'PA0555', 2, 2, -1, 6, 0 )
3927             ENDIF
3928             IF ( .NOT. root_area_density_lsm_f%from_file )  THEN
3929                message_string = 'If vegetation_type = 0 at any location, ' // &
3930                                 'root_area_dens_s is required'
3931                CALL message( 'netcdf_data_input_mod', 'PA0556', 2, 2, myid, 6, 0 )
3932             ENDIF
3933          ENDIF
3934       ENDIF
3935!
3936!--    If soil_type is zero at any location, soil_pars is required.
3937       IF ( soil_type_f%from_file )  THEN
3938          check_passed = .TRUE.
3939          IF ( ALLOCATED( soil_type_f%var_2d ) )  THEN
3940             IF ( ANY( soil_type_f%var_2d == 0 ) )  THEN
3941                IF ( .NOT. soil_pars_f%from_file )  check_passed = .FALSE.
3942             ENDIF
3943          ELSE
3944             IF ( ANY( soil_type_f%var_3d == 0 ) )  THEN
3945                IF ( .NOT. soil_pars_f%from_file )  check_passed = .FALSE.
3946             ENDIF
3947          ENDIF
3948          IF ( .NOT. check_passed )  THEN
3949             message_string = 'If soil_type = 0 at any location, ' //          &
3950                              'soil_pars is required'
3951             CALL message( 'netcdf_data_input_mod', 'PA0557', 2, 2, myid, 6, 0 )
3952          ENDIF
3953       ENDIF
3954!
3955!--    Buildings require a type in case of urban-surface model.
3956       IF ( buildings_f%from_file  .AND.  .NOT. building_type_f%from_file  )  THEN
3957          message_string = 'If buildings are provided, also building_type ' // &
3958                           'is required'
3959          CALL message( 'netcdf_data_input_mod', 'PA0581', 2, 2, myid, 6, 0 )
3960       ENDIF
3961!
3962!--    Buildings require an ID.
3963       IF ( buildings_f%from_file  .AND.  .NOT. building_id_f%from_file  )  THEN
3964          message_string = 'If buildings are provided, also building_id ' //   &
3965                           'is required'
3966          CALL message( 'netcdf_data_input_mod', 'PA0582', 2, 2, myid, 6, 0 )
3967       ENDIF
3968!
3969!--    If building_type is zero at any location, building_pars is required.
3970       IF ( building_type_f%from_file )  THEN
3971          IF ( ANY( building_type_f%var == 0 ) )  THEN
3972             IF ( .NOT. building_pars_f%from_file )  THEN
3973                message_string = 'If building_type = 0 at any location, ' //   &
3974                                 'building_pars is required'
3975                CALL message( 'netcdf_data_input_mod', 'PA0558', 2, 2, myid, 6, 0 )
3976             ENDIF
3977          ENDIF
3978       ENDIF
3979!
3980!--    If building_type is provided, also building_id is needed (due to the
3981!--    filtering algorithm).
3982       IF ( building_type_f%from_file  .AND.  .NOT. building_id_f%from_file )  &
3983       THEN
3984          message_string = 'If building_type is provided, also building_id '// &
3985                           'is required'
3986          CALL message( 'netcdf_data_input_mod', 'PA0519', 2, 2, myid, 6, 0 )
3987       ENDIF       
3988!
3989!--    If albedo_type is zero at any location, albedo_pars is required.
3990       IF ( albedo_type_f%from_file )  THEN
3991          IF ( ANY( albedo_type_f%var == 0 ) )  THEN
3992             IF ( .NOT. albedo_pars_f%from_file )  THEN
3993                message_string = 'If albedo_type = 0 at any location, ' //     &
3994                                 'albedo_pars is required'
3995                CALL message( 'netcdf_data_input_mod', 'PA0559', 2, 2, myid, 6, 0 )
3996             ENDIF
3997          ENDIF
3998       ENDIF
3999!
4000!--    If pavement_type is zero at any location, pavement_pars is required.
4001       IF ( pavement_type_f%from_file )  THEN
4002          IF ( ANY( pavement_type_f%var == 0 ) )  THEN
4003             IF ( .NOT. pavement_pars_f%from_file )  THEN
4004                message_string = 'If pavement_type = 0 at any location, ' //   &
4005                                 'pavement_pars is required'
4006                CALL message( 'netcdf_data_input_mod', 'PA0560', 2, 2, myid, 6, 0 )
4007             ENDIF
4008          ENDIF
4009       ENDIF
4010!
4011!--    If pavement_type is zero at any location, also pavement_subsurface_pars
4012!--    is required.
4013       IF ( pavement_type_f%from_file )  THEN
4014          IF ( ANY( pavement_type_f%var == 0 ) )  THEN
4015             IF ( .NOT. pavement_subsurface_pars_f%from_file )  THEN
4016                message_string = 'If pavement_type = 0 at any location, ' //   &
4017                                 'pavement_subsurface_pars is required'
4018                CALL message( 'netcdf_data_input_mod', 'PA0561', 2, 2, myid, 6, 0 )
4019             ENDIF
4020          ENDIF
4021       ENDIF
4022!
4023!--    If water_type is zero at any location, water_pars is required.
4024       IF ( water_type_f%from_file )  THEN
4025          IF ( ANY( water_type_f%var == 0 ) )  THEN
4026             IF ( .NOT. water_pars_f%from_file )  THEN
4027                message_string = 'If water_type = 0 at any location, ' //      &
4028                                 'water_pars is required'
4029                CALL message( 'netcdf_data_input_mod', 'PA0562', 2, 2,myid, 6, 0 )
4030             ENDIF
4031          ENDIF
4032       ENDIF
4033!
4034!--    Check for local consistency of the input data.
4035       DO  i = nxl, nxr
4036          DO  j = nys, nyn
4037!
4038!--          For each (y,x)-location at least one of the parameters
4039!--          vegetation_type, pavement_type, building_type, or water_type
4040!--          must be set to a non­missing value.
4041             IF ( land_surface  .AND.  .NOT. urban_surface )  THEN
4042                IF ( vegetation_type_f%var(j,i) == vegetation_type_f%fill  .AND.&
4043                     pavement_type_f%var(j,i)   == pavement_type_f%fill    .AND.&
4044                     water_type_f%var(j,i)      == water_type_f%fill )  THEN
4045                   WRITE( message_string, * )                                  &
4046                                    'At least one of the parameters '//        &
4047                                    'vegetation_type, pavement_type, '     //  &
4048                                    'or water_type must be set '//             &
4049                                    'to a non-missing value. Grid point: ', j, i
4050                   CALL message( 'netcdf_data_input_mod', 'PA0563', 2, 2, myid, 6, 0 )
4051                ENDIF
4052             ELSEIF ( land_surface  .AND.  urban_surface )  THEN
4053                IF ( vegetation_type_f%var(j,i) == vegetation_type_f%fill  .AND.&
4054                     pavement_type_f%var(j,i)   == pavement_type_f%fill    .AND.&
4055                     building_type_f%var(j,i)   == building_type_f%fill    .AND.&
4056                     water_type_f%var(j,i)      == water_type_f%fill )  THEN
4057                   WRITE( message_string, * )                                  &
4058                                 'At least one of the parameters '//           &
4059                                 'vegetation_type, pavement_type, '  //        &
4060                                 'building_type, or water_type must be set '// &
4061                                 'to a non-missing value. Grid point: ', j, i
4062                   CALL message( 'netcdf_data_input_mod', 'PA0563', 2, 2, myid, 6, 0 )
4063                ENDIF
4064             ENDIF
4065               
4066!
4067!--          Note that a soil_type is required for each location (y,x) where
4068!--          either vegetation_type or pavement_type is a non­missing value.
4069             IF ( ( vegetation_type_f%var(j,i) /= vegetation_type_f%fill  .OR. &
4070                    pavement_type_f%var(j,i)   /= pavement_type_f%fill ) )  THEN
4071                check_passed = .TRUE.
4072                IF ( ALLOCATED( soil_type_f%var_2d ) )  THEN
4073                   IF ( soil_type_f%var_2d(j,i) == soil_type_f%fill )          &
4074                      check_passed = .FALSE.
4075                ELSE
4076                   IF ( ANY( soil_type_f%var_3d(:,j,i) == soil_type_f%fill) )  &
4077                      check_passed = .FALSE.
4078                ENDIF
4079
4080                IF ( .NOT. check_passed )  THEN
4081                   message_string = 'soil_type is required for each '//        &
4082                                 'location (y,x) where vegetation_type or ' // &
4083                                 'pavement_type is a non-missing value.'
4084                   CALL message( 'netcdf_data_input_mod', 'PA0564',            &
4085                                  2, 2, myid, 6, 0 )
4086                ENDIF
4087             ENDIF
4088!
4089!--          Check for consistency of surface fraction. If more than one type
4090!--          is set, surface fraction need to be given and the sum must not
4091!--          be larger than 1.
4092             n_surf = 0
4093             IF ( vegetation_type_f%var(j,i) /= vegetation_type_f%fill )       &
4094                n_surf = n_surf + 1
4095             IF ( water_type_f%var(j,i)      /= water_type_f%fill )            &
4096                n_surf = n_surf + 1
4097             IF ( pavement_type_f%var(j,i)   /= pavement_type_f%fill )         &
4098                n_surf = n_surf + 1
4099
4100             IF ( n_surf > 1 )  THEN
4101                IF ( .NOT. surface_fraction_f%from_file )  THEN
4102                   message_string = 'If more than one surface type is ' //     &
4103                                 'given at a location, surface_fraction ' //   &
4104                                 'must be provided.'
4105                   CALL message( 'netcdf_data_input_mod', 'PA0565',            &
4106                                  2, 2, myid, 6, 0 )
4107                ELSEIF ( ANY ( surface_fraction_f%frac(:,j,i) ==               &
4108                               surface_fraction_f%fill ) )  THEN
4109                   message_string = 'If more than one surface type is ' //     &
4110                                 'given at a location, surface_fraction ' //   &
4111                                 'must be provided.'
4112                   CALL message( 'netcdf_data_input_mod', 'PA0565',            &
4113                                  2, 2, myid, 6, 0 )
4114                ENDIF
4115             ENDIF
4116!
4117!--          Check for further mismatches. e.g. relative fractions exceed 1 or
4118!--          vegetation_type is set but surface vegetation fraction is zero,
4119!--          etc..
4120             IF ( surface_fraction_f%from_file )  THEN
4121!
4122!--             Sum of relative fractions must not exceed 1.
4123                IF ( SUM ( surface_fraction_f%frac(0:2,j,i) ) > 1.0_wp )  THEN
4124                   message_string = 'surface_fraction must not exceed 1'
4125                   CALL message( 'netcdf_data_input_mod', 'PA0566',            &
4126                                  2, 2, myid, 6, 0 )
4127                ENDIF
4128!
4129!--             Relative fraction for a type must not be zero at locations where
4130!--             this type is set.
4131                IF (                                                           &
4132                  ( vegetation_type_f%var(j,i) /= vegetation_type_f%fill  .AND.&
4133                 ( surface_fraction_f%frac(ind_veg_wall,j,i) == 0.0_wp .OR.    &
4134                   surface_fraction_f%frac(ind_veg_wall,j,i) ==                &
4135                                                     surface_fraction_f%fill ) &
4136                  )  .OR.                                                      &
4137                  ( pavement_type_f%var(j,i) /= pavement_type_f%fill     .AND. &
4138                 ( surface_fraction_f%frac(ind_pav_green,j,i) == 0.0_wp .OR.   &
4139                   surface_fraction_f%frac(ind_pav_green,j,i) ==               &
4140                                                     surface_fraction_f%fill ) &
4141                  )  .OR.                                                      &
4142                  ( water_type_f%var(j,i) /= water_type_f%fill           .AND. &
4143                 ( surface_fraction_f%frac(ind_wat_win,j,i) == 0.0_wp .OR.     &
4144                   surface_fraction_f%frac(ind_wat_win,j,i) ==                 &
4145                                                     surface_fraction_f%fill ) &
4146                  ) )  THEN
4147                   WRITE( message_string, * ) 'Mismatch in setting of '     // &
4148                             'surface_fraction. Vegetation-, pavement-, or '// &
4149                             'water surface is given at (i,j) = ( ', i, j,     &
4150                             ' ), but surface fraction is 0 for the given type.'
4151                   CALL message( 'netcdf_data_input_mod', 'PA0567',            &
4152                                  2, 2, myid, 6, 0 )
4153                ENDIF
4154!
4155!--             Relative fraction for a type must not contain non-zero values
4156!--             if this type is not set.
4157                IF (                                                           &
4158                  ( vegetation_type_f%var(j,i) == vegetation_type_f%fill  .AND.&
4159                 ( surface_fraction_f%frac(ind_veg_wall,j,i) /= 0.0_wp .AND.   &
4160                   surface_fraction_f%frac(ind_veg_wall,j,i) /=                &
4161                                                     surface_fraction_f%fill ) &
4162                  )  .OR.                                                      &
4163                  ( pavement_type_f%var(j,i) == pavement_type_f%fill     .AND. &
4164                 ( surface_fraction_f%frac(ind_pav_green,j,i) /= 0.0_wp .AND.  &
4165                   surface_fraction_f%frac(ind_pav_green,j,i) /=               &
4166                                                     surface_fraction_f%fill ) &
4167                  )  .OR.                                                      &
4168                  ( water_type_f%var(j,i) == water_type_f%fill           .AND. &
4169                 ( surface_fraction_f%frac(ind_wat_win,j,i) /= 0.0_wp .AND.    &
4170                   surface_fraction_f%frac(ind_wat_win,j,i) /=                 &
4171                                                     surface_fraction_f%fill ) &
4172                  ) )  THEN
4173                   WRITE( message_string, * ) 'Mismatch in setting of '     // &
4174                             'surface_fraction. Vegetation-, pavement-, or '// &
4175                             'water surface is not given at (i,j) = ( ', i, j, &
4176                             ' ), but surface fraction is not 0 for the ' //   &
4177                             'given type.'
4178                   CALL message( 'netcdf_data_input_mod', 'PA0568',            &
4179                                  2, 2, myid, 6, 0 )
4180                ENDIF
4181             ENDIF
4182!
4183!--          Check vegetation_pars. If vegetation_type is 0, all parameters
4184!--          need to be set, otherwise, single parameters set by
4185!--          vegetation_type can be overwritten.
4186             IF ( vegetation_type_f%from_file )  THEN
4187                IF ( vegetation_type_f%var(j,i) == 0 )  THEN
4188                   IF ( ANY( vegetation_pars_f%pars_xy(:,j,i) ==               &
4189                             vegetation_pars_f%fill ) )  THEN
4190                      message_string = 'If vegetation_type(y,x) = 0, all '  // &
4191                                       'parameters of vegetation_pars at '//   &
4192                                       'this location must be set.'
4193                      CALL message( 'netcdf_data_input_mod', 'PA0569',         &
4194                                     2, 2, myid, 6, 0 )
4195                   ENDIF
4196                ENDIF
4197             ENDIF
4198!
4199!--          Check root distribution. If vegetation_type is 0, all levels must
4200!--          be set.
4201             IF ( vegetation_type_f%from_file )  THEN
4202                IF ( vegetation_type_f%var(j,i) == 0 )  THEN
4203                   IF ( ANY( root_area_density_lsm_f%var(:,j,i) ==             &
4204                             root_area_density_lsm_f%fill ) )  THEN
4205                      message_string = 'If vegetation_type(y,x) = 0, all ' //  &
4206                                       'levels of root_area_dens_s ' //        &
4207                                       'must be set at this location.'
4208                      CALL message( 'netcdf_data_input_mod', 'PA0570',         &
4209                                     2, 2, myid, 6, 0 )
4210                   ENDIF
4211                ENDIF
4212             ENDIF
4213!
4214!--          Check soil parameters. If soil_type is 0, all parameters
4215!--          must be set.
4216             IF ( soil_type_f%from_file )  THEN
4217                check_passed = .TRUE.
4218                IF ( ALLOCATED( soil_type_f%var_2d ) )  THEN
4219                   IF ( soil_type_f%var_2d(j,i) == 0 )  THEN
4220                      IF ( ANY( soil_pars_f%pars_xy(:,j,i) ==                  &
4221                                soil_pars_f%fill ) )  check_passed = .FALSE.
4222                   ENDIF
4223                ELSE
4224                   IF ( ANY( soil_type_f%var_3d(:,j,i) == 0 ) )  THEN
4225                      IF ( ANY( soil_pars_f%pars_xy(:,j,i) ==                  &
4226                                soil_pars_f%fill ) )  check_passed = .FALSE.
4227                   ENDIF
4228                ENDIF
4229                IF ( .NOT. check_passed )  THEN
4230                   message_string = 'If soil_type(y,x) = 0, all levels of '  //&
4231                                    'soil_pars at this location must be set.'
4232                   CALL message( 'netcdf_data_input_mod', 'PA0571',            &
4233                                  2, 2, myid, 6, 0 )
4234                ENDIF
4235             ENDIF
4236
4237!
4238!--          Check building parameters. If building_type is 0, all parameters
4239!--          must be set.
4240             IF ( building_type_f%from_file )  THEN
4241                IF ( building_type_f%var(j,i) == 0 )  THEN
4242                   IF ( ANY( building_pars_f%pars_xy(:,j,i) ==                 &
4243                             building_pars_f%fill ) )  THEN
4244                      message_string = 'If building_type(y,x) = 0, all ' //    &
4245                                       'parameters of building_pars at this '//&
4246                                       'location must be set.'
4247                      CALL message( 'netcdf_data_input_mod', 'PA0572',         &
4248                                     2, 2, myid, 6, 0 )
4249                   ENDIF
4250                ENDIF
4251             ENDIF
4252!
4253!--          Check if building_type is set at each building and vice versa.
4254             IF ( building_type_f%from_file  .AND.  buildings_f%from_file )  THEN
4255                IF ( buildings_f%lod == 1 )  THEN
4256                   IF ( buildings_f%var_2d(j,i)  /= buildings_f%fill1  .AND.   &
4257                        building_type_f%var(j,i) == building_type_f%fill  .OR. &
4258                        buildings_f%var_2d(j,i)  == buildings_f%fill1  .AND.   &
4259                        building_type_f%var(j,i) /= building_type_f%fill )  THEN
4260                      WRITE( message_string, * ) 'Each location where a ' //   &
4261                                      '2D building is set requires a type ' // &
4262                                      '( and vice versa ) in case the ' //     &
4263                                      'urban-surface model is applied. ' //    &
4264                                      'i, j = ', i, j
4265                      CALL message( 'netcdf_data_input_mod', 'PA0573',         &
4266                                     2, 2, myid, 6, 0 )
4267                   ENDIF
4268                ENDIF
4269                IF ( buildings_f%lod == 2 )  THEN
4270                   IF ( ANY( buildings_f%var_3d(:,j,i) == 1 )  .AND.           &
4271                        building_type_f%var(j,i) == building_type_f%fill  .OR. &
4272                  .NOT. ANY( buildings_f%var_3d(:,j,i) == 1 )  .AND.           &
4273                        building_type_f%var(j,i) /= building_type_f%fill )  THEN
4274                      WRITE( message_string, * ) 'Each location where a ' //   &
4275                                      '3D building is set requires a type ' // &
4276                                      '( and vice versa ) in case the ' //     &
4277                                      'urban-surface model is applied. ' //    &
4278                                      'i, j = ', i, j
4279                      CALL message( 'netcdf_data_input_mod', 'PA0573',         &
4280                                     2, 2, myid, 6, 0 )
4281                   ENDIF
4282                ENDIF
4283             ENDIF
4284!
4285!--          Check if at each location where a building is present also an ID
4286!--          is set and vice versa.
4287             IF ( buildings_f%from_file )  THEN
4288                IF ( buildings_f%lod == 1 )  THEN
4289                   IF ( buildings_f%var_2d(j,i) /= buildings_f%fill1  .AND.    &
4290                        building_id_f%var(j,i)  == building_id_f%fill  .OR.    &
4291                        buildings_f%var_2d(j,i) == buildings_f%fill1  .AND.    &
4292                        building_id_f%var(j,i)  /= building_id_f%fill )  THEN
4293                      WRITE( message_string, * ) 'Each location where a ' //   &
4294                                      '2D building is set requires an ID ' //  &
4295                                      '( and vice versa ). i, j = ', i, j
4296                      CALL message( 'netcdf_data_input_mod', 'PA0574',         &
4297                                     2, 2, myid, 6, 0 )
4298                   ENDIF
4299                ELSEIF ( buildings_f%lod == 2 )  THEN
4300                   IF ( ANY( buildings_f%var_3d(:,j,i) == 1 )  .AND.           &
4301                        building_id_f%var(j,i) == building_id_f%fill  .OR.     &
4302                  .NOT. ANY( buildings_f%var_3d(:,j,i) == 1 )  .AND.           &
4303                        building_id_f%var(j,i) /= building_id_f%fill )  THEN
4304                      WRITE( message_string, * ) 'Each location where a ' //   &
4305                                      '3D building is set requires an ID ' //  &
4306                                      '( and vice versa ). i, j = ', i, j
4307                      CALL message( 'netcdf_data_input_mod', 'PA0574',         &
4308                                     2, 2, myid, 6, 0 )
4309                   ENDIF
4310                ENDIF
4311             ENDIF
4312!
4313!--          Check if building ID is set where a bulding is defined.
4314             IF ( buildings_f%from_file )  THEN
4315                IF ( buildings_f%lod == 1 )  THEN
4316                   IF ( buildings_f%var_2d(j,i) /= buildings_f%fill1  .AND.   &
4317                        building_id_f%var(j,i)  == building_id_f%fill )  THEN
4318                      WRITE( message_string, * ) 'Each building grid point '// &
4319                                                 'requires an ID.', i, j
4320                      CALL message( 'netcdf_data_input_mod', 'PA0575',         &
4321                                     2, 2, myid, 6, 0 )
4322                   ENDIF
4323                ELSEIF ( buildings_f%lod == 2 )  THEN
4324                   IF ( ANY( buildings_f%var_3d(:,j,i) == 1 )  .AND.           &
4325                        building_id_f%var(j,i) == building_id_f%fill )  THEN
4326                      WRITE( message_string, * ) 'Each building grid point '// &
4327                                                 'requires an ID.', i, j
4328                      CALL message( 'netcdf_data_input_mod', 'PA0575',         &
4329                                     2, 2, myid, 6, 0 )
4330                   ENDIF
4331                ENDIF
4332             ENDIF
4333!
4334!--          Check albedo parameters. If albedo_type is 0, all parameters
4335!--          must be set.
4336             IF ( albedo_type_f%from_file )  THEN
4337                IF ( albedo_type_f%var(j,i) == 0 )  THEN
4338                   IF ( ANY( albedo_pars_f%pars_xy(:,j,i) ==                   &
4339                             albedo_pars_f%fill ) )  THEN
4340                      message_string = 'If albedo_type(y,x) = 0, all ' //      &
4341                                       'parameters of albedo_pars at this ' // &
4342                                       'location must be set.'
4343                      CALL message( 'netcdf_data_input_mod', 'PA0576',         &
4344                                     2, 2, myid, 6, 0 )
4345                   ENDIF
4346                ENDIF
4347             ENDIF
4348
4349!
4350!--          Check pavement parameters. If pavement_type is 0, all parameters
4351!--          of pavement_pars must be set at this location.
4352             IF ( pavement_type_f%from_file )  THEN
4353                IF ( pavement_type_f%var(j,i) == 0 )  THEN
4354                   IF ( ANY( pavement_pars_f%pars_xy(:,j,i) ==                 &
4355                             pavement_pars_f%fill ) )  THEN
4356                      message_string = 'If pavement_type(y,x) = 0, all ' //    &
4357                                       'parameters of pavement_pars at this '//&
4358                                       'location must be set.'
4359                      CALL message( 'netcdf_data_input_mod', 'PA0577',         &
4360                                     2, 2, myid, 6, 0 )
4361                   ENDIF
4362                ENDIF
4363             ENDIF
4364!
4365!--          Check pavement-subsurface parameters. If pavement_type is 0,
4366!--          all parameters of pavement_subsurface_pars must be set  at this
4367!--          location.
4368             IF ( pavement_type_f%from_file )  THEN
4369                IF ( pavement_type_f%var(j,i) == 0 )  THEN
4370                   IF ( ANY( pavement_subsurface_pars_f%pars_xyz(:,:,j,i) ==   &
4371                             pavement_subsurface_pars_f%fill ) )  THEN
4372                      message_string = 'If pavement_type(y,x) = 0, all ' //    &
4373                                       'parameters of '                  //    &
4374                                       'pavement_subsurface_pars at this '//   &
4375                                       'location must be set.'
4376                      CALL message( 'netcdf_data_input_mod', 'PA0578',         &
4377                                     2, 2, myid, 6, 0 )
4378                   ENDIF
4379                ENDIF
4380             ENDIF
4381
4382!
4383!--          Check water parameters. If water_type is 0, all parameters
4384!--          must be set  at this location.
4385             IF ( water_type_f%from_file )  THEN
4386                IF ( water_type_f%var(j,i) == 0 )  THEN
4387                   IF ( ANY( water_pars_f%pars_xy(:,j,i) ==                    &
4388                             water_pars_f%fill ) )  THEN
4389                      message_string = 'If water_type(y,x) = 0, all ' //       &
4390                                       'parameters of water_pars at this ' //  &
4391                                       'location must be set.'
4392                      CALL message( 'netcdf_data_input_mod', 'PA0579',         &
4393                                     2, 2, myid, 6, 0 )
4394                   ENDIF
4395                ENDIF
4396             ENDIF
4397
4398          ENDDO
4399       ENDDO
4400
4401    END SUBROUTINE netcdf_data_input_check_static
4402
4403!------------------------------------------------------------------------------!
4404! Description:
4405! ------------
4406!> Resize 8-bit 2D Integer array: (nys:nyn,nxl:nxr) -> (nysg:nyng,nxlg:nxrg)
4407!------------------------------------------------------------------------------!
4408    SUBROUTINE resize_array_2d_int8( var, js, je, is, ie )
4409   
4410       IMPLICIT NONE
4411
4412       INTEGER(iwp) ::  je !< upper index bound along y direction
4413       INTEGER(iwp) ::  js !< lower index bound along y direction
4414       INTEGER(iwp) ::  ie !< upper index bound along x direction
4415       INTEGER(iwp) ::  is !< lower index bound along x direction
4416       
4417       INTEGER(KIND=1), DIMENSION(:,:), ALLOCATABLE ::  var     !< treated variable
4418       INTEGER(KIND=1), DIMENSION(:,:), ALLOCATABLE ::  var_tmp !< temporary copy
4419!
4420!--    Allocate temporary variable
4421       ALLOCATE( var_tmp(js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4422!
4423!--    Temporary copy of the variable
4424       var_tmp(js:je,is:ie) = var(js:je,is:ie)
4425!
4426!--    Resize the array
4427       DEALLOCATE( var )
4428       ALLOCATE( var(js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4429!
4430!--    Transfer temporary copy back to original array
4431       var(js:je,is:ie) = var_tmp(js:je,is:ie)
4432
4433    END SUBROUTINE resize_array_2d_int8
4434   
4435!------------------------------------------------------------------------------!
4436! Description:
4437! ------------
4438!> Resize 32-bit 2D Integer array: (nys:nyn,nxl:nxr) -> (nysg:nyng,nxlg:nxrg)
4439!------------------------------------------------------------------------------!
4440    SUBROUTINE resize_array_2d_int32( var, js, je, is, ie )
4441
4442       IMPLICIT NONE
4443       
4444       INTEGER(iwp) ::  je !< upper index bound along y direction
4445       INTEGER(iwp) ::  js !< lower index bound along y direction
4446       INTEGER(iwp) ::  ie !< upper index bound along x direction
4447       INTEGER(iwp) ::  is !< lower index bound along x direction
4448
4449       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  var     !< treated variable
4450       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  var_tmp !< temporary copy
4451!
4452!--    Allocate temporary variable
4453       ALLOCATE( var_tmp(js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4454!
4455!--    Temporary copy of the variable
4456       var_tmp(js:je,is:ie) = var(js:je,is:ie)
4457!
4458!--    Resize the array
4459       DEALLOCATE( var )
4460       ALLOCATE( var(js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4461!
4462!--    Transfer temporary copy back to original array
4463       var(js:je,is:ie) = var_tmp(js:je,is:ie)
4464
4465    END SUBROUTINE resize_array_2d_int32
4466   
4467!------------------------------------------------------------------------------!
4468! Description:
4469! ------------
4470!> Resize 8-bit 3D Integer array: (:,nys:nyn,nxl:nxr) -> (:,nysg:nyng,nxlg:nxrg)
4471!------------------------------------------------------------------------------!
4472    SUBROUTINE resize_array_3d_int8( var, ks, ke, js, je, is, ie )
4473
4474       IMPLICIT NONE
4475
4476       INTEGER(iwp) ::  je !< upper index bound along y direction
4477       INTEGER(iwp) ::  js !< lower index bound along y direction
4478       INTEGER(iwp) ::  ie !< upper index bound along x direction
4479       INTEGER(iwp) ::  is !< lower index bound along x direction
4480       INTEGER(iwp) ::  ke !< upper bound of treated array in z-direction 
4481       INTEGER(iwp) ::  ks !< lower bound of treated array in z-direction 
4482       
4483       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var     !< treated variable
4484       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var_tmp !< temporary copy
4485!
4486!--    Allocate temporary variable
4487       ALLOCATE( var_tmp(ks:ke,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4488!
4489!--    Temporary copy of the variable
4490       var_tmp(ks:ke,js:je,is:ie) = var(ks:ke,js:je,is:ie)
4491!
4492!--    Resize the array
4493       DEALLOCATE( var )
4494       ALLOCATE( var(ks:ke,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4495!
4496!--    Transfer temporary copy back to original array
4497       var(ks:ke,js:je,is:ie) = var_tmp(ks:ke,js:je,is:ie)
4498
4499    END SUBROUTINE resize_array_3d_int8
4500   
4501!------------------------------------------------------------------------------!
4502! Description:
4503! ------------
4504!> Resize 3D Real array: (:,nys:nyn,nxl:nxr) -> (:,nysg:nyng,nxlg:nxrg)
4505!------------------------------------------------------------------------------!
4506    SUBROUTINE resize_array_3d_real( var, ks, ke, js, je, is, ie )
4507
4508       IMPLICIT NONE
4509
4510       INTEGER(iwp) ::  je !< upper index bound along y direction
4511       INTEGER(iwp) ::  js !< lower index bound along y direction
4512       INTEGER(iwp) ::  ie !< upper index bound along x direction
4513       INTEGER(iwp) ::  is !< lower index bound along x direction
4514       INTEGER(iwp) ::  ke !< upper bound of treated array in z-direction 
4515       INTEGER(iwp) ::  ks !< lower bound of treated array in z-direction 
4516       
4517       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  var     !< treated variable
4518       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  var_tmp !< temporary copy
4519!
4520!--    Allocate temporary variable
4521       ALLOCATE( var_tmp(ks:ke,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4522!
4523!--    Temporary copy of the variable
4524       var_tmp(ks:ke,js:je,is:ie) = var(ks:ke,js:je,is:ie)
4525!
4526!--    Resize the array
4527       DEALLOCATE( var )
4528       ALLOCATE( var(ks:ke,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4529!
4530!--    Transfer temporary copy back to original array
4531       var(ks:ke,js:je,is:ie) = var_tmp(ks:ke,js:je,is:ie)
4532
4533    END SUBROUTINE resize_array_3d_real
4534   
4535!------------------------------------------------------------------------------!
4536! Description:
4537! ------------
4538!> Resize 4D Real array: (:,:,nys:nyn,nxl:nxr) -> (:,nysg:nyng,nxlg:nxrg)
4539!------------------------------------------------------------------------------!
4540    SUBROUTINE resize_array_4d_real( var, k1s, k1e, k2s, k2e, js, je, is, ie )
4541
4542       IMPLICIT NONE
4543       
4544       INTEGER(iwp) ::  je  !< upper index bound along y direction
4545       INTEGER(iwp) ::  js  !< lower index bound along y direction
4546       INTEGER(iwp) ::  ie  !< upper index bound along x direction
4547       INTEGER(iwp) ::  is  !< lower index bound along x direction
4548       INTEGER(iwp) ::  k1e !< upper bound of treated array in z-direction 
4549       INTEGER(iwp) ::  k1s !< lower bound of treated array in z-direction
4550       INTEGER(iwp) ::  k2e !< upper bound of treated array along parameter space 
4551       INTEGER(iwp) ::  k2s !< lower bound of treated array along parameter space 
4552       
4553       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  var     !< treated variable
4554       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  var_tmp !< temporary copy
4555!
4556!--    Allocate temporary variable
4557       ALLOCATE( var_tmp(k1s:k1e,k2s:k2e,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4558!
4559!--    Temporary copy of the variable
4560       var_tmp(k1s:k1e,k2s:k2e,js:je,is:ie) = var(k1s:k1e,k2s:k2e,js:je,is:ie)
4561!
4562!--    Resize the array
4563       DEALLOCATE( var )
4564       ALLOCATE( var(k1s:k1e,k2s:k2e,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4565!
4566!--    Transfer temporary copy back to original array
4567       var(k1s:k1e,k2s:k2e,js:je,is:ie) = var_tmp(k1s:k1e,k2s:k2e,js:je,is:ie)
4568
4569    END SUBROUTINE resize_array_4d_real
4570   
4571!------------------------------------------------------------------------------!
4572! Description:
4573! ------------
4574!> Vertical interpolation and extrapolation of 1D variables.
4575!------------------------------------------------------------------------------!
4576    SUBROUTINE netcdf_data_input_interpolate_1d( var, z_grid, z_file)
4577
4578       IMPLICIT NONE
4579
4580       INTEGER(iwp) ::  k       !< running index z-direction file
4581       INTEGER(iwp) ::  kk      !< running index z-direction stretched model grid
4582       INTEGER(iwp) ::  kl      !< lower index bound along z-direction
4583       INTEGER(iwp) ::  ku      !< upper index bound along z-direction
4584
4585       REAL(wp), DIMENSION(:) ::  z_grid                  !< grid levels on numeric grid
4586       REAL(wp), DIMENSION(:) ::  z_file                  !< grid levels on file grid
4587       REAL(wp), DIMENSION(:), INTENT(INOUT) ::  var      !< treated variable
4588       REAL(wp), DIMENSION(:), ALLOCATABLE   ::  var_tmp  !< temporary variable
4589
4590
4591       kl = LBOUND(var,1)
4592       ku = UBOUND(var,1)
4593       ALLOCATE( var_tmp(kl:ku) )
4594
4595       DO  k = kl, ku
4596
4597          kk = MINLOC( ABS( z_file - z_grid(k) ), DIM = 1 )
4598
4599          IF ( kk < ku )  THEN
4600             IF ( z_file(kk) - z_grid(k) <= 0.0_wp )  THEN
4601                var_tmp(k) = var(kk) +                                         &
4602                                       ( var(kk+1)        - var(kk)    ) /     &
4603                                       ( z_file(kk+1)     - z_file(kk) ) *     &
4604                                       ( z_grid(k)        - z_file(kk) )
4605
4606             ELSEIF ( z_file(kk) - z_grid(k) > 0.0_wp )  THEN
4607                var_tmp(k) = var(kk-1) +                                       &
4608                                         ( var(kk)     - var(kk-1)    ) /      &
4609                                         ( z_file(kk)  - z_file(kk-1) ) *      &
4610                                         ( z_grid(k)   - z_file(kk-1) )
4611             ENDIF
4612!
4613!--       Extrapolate
4614          ELSE
4615
4616             var_tmp(k) = var(ku) +   ( var(ku)    - var(ku-1)      ) /        &
4617                                      ( z_file(ku) - z_file(ku-1)   ) *        &
4618                                      ( z_grid(k)  - z_file(ku)     )
4619
4620          ENDIF
4621
4622       ENDDO
4623       var(:) = var_tmp(:)
4624
4625       DEALLOCATE( var_tmp )
4626
4627
4628    END SUBROUTINE netcdf_data_input_interpolate_1d
4629
4630
4631!------------------------------------------------------------------------------!
4632! Description:
4633! ------------
4634!> Vertical interpolation and extrapolation of 1D variables from Inifor grid
4635!> onto Palm grid, where both have same dimension. Please note, the passed
4636!> paramter list in 1D version is different compared to 2D version.
4637!------------------------------------------------------------------------------!
4638    SUBROUTINE netcdf_data_input_interpolate_1d_soil( var, var_file,           &
4639                                                      z_grid, z_file,          &
4640                                                      nzb_var, nzt_var,        &
4641                                                      nzb_file, nzt_file )
4642
4643       IMPLICIT NONE
4644
4645       INTEGER(iwp) ::  k        !< running index z-direction file
4646       INTEGER(iwp) ::  kk       !< running index z-direction stretched model grid
4647       INTEGER(iwp) ::  ku       !< upper index bound along z-direction for varialbe from file
4648       INTEGER(iwp) ::  nzb_var  !< lower bound of final array
4649       INTEGER(iwp) ::  nzt_var  !< upper bound of final array
4650       INTEGER(iwp) ::  nzb_file !< lower bound of file array
4651       INTEGER(iwp) ::  nzt_file !< upper bound of file array
4652
4653!        LOGICAL, OPTIONAL ::  zsoil !< flag indicating reverse z-axis, i.e. zsoil instead of height, e.g. in case of ocean or soil
4654
4655       REAL(wp), DIMENSION(nzb_var:nzt_var)   ::  z_grid   !< grid levels on numeric grid
4656       REAL(wp), DIMENSION(nzb_file:nzt_file) ::  z_file   !< grid levels on file grid
4657       REAL(wp), DIMENSION(nzb_var:nzt_var)   ::  var      !< treated variable
4658       REAL(wp), DIMENSION(nzb_file:nzt_file) ::  var_file !< temporary variable
4659
4660       ku = nzt_file
4661
4662       DO  k = nzb_var, nzt_var
4663!
4664!--       Determine index on Inifor grid which is closest to the actual height
4665          kk = MINLOC( ABS( z_file - z_grid(k) ), DIM = 1 )
4666!
4667!--       If closest index on Inifor grid is smaller than top index,
4668!--       interpolate the data
4669          IF ( kk < nzt_file )  THEN
4670             IF ( z_file(kk) - z_grid(k) <= 0.0_wp )  THEN
4671                var(k) = var_file(kk) + ( var_file(kk+1) - var_file(kk) ) /    &
4672                                        ( z_file(kk+1)   - z_file(kk)   ) *    &
4673                                        ( z_grid(k)      - z_file(kk)   )
4674
4675             ELSEIF ( z_file(kk) - z_grid(k) > 0.0_wp )  THEN
4676                var(k) = var_file(kk-1) + ( var_file(kk) - var_file(kk-1) ) /  &
4677                                          ( z_file(kk)   - z_file(kk-1)   ) *  &
4678                                          ( z_grid(k)    - z_file(kk-1)   )
4679             ENDIF
4680!
4681!--       Extrapolate if actual height is above the highest Inifor level
4682          ELSE
4683             var(k) = var_file(ku) + ( var_file(ku) - var_file(ku-1) ) /       &
4684                                     ( z_file(ku)   - z_file(ku-1)   ) *       &
4685                                     ( z_grid(k)    - z_file(ku)     )
4686
4687          ENDIF
4688
4689       ENDDO
4690
4691    END SUBROUTINE netcdf_data_input_interpolate_1d_soil
4692
4693!------------------------------------------------------------------------------!
4694! Description:
4695! ------------
4696!> Vertical interpolation and extrapolation of 2D variables at lateral boundaries.
4697!------------------------------------------------------------------------------!
4698    SUBROUTINE netcdf_data_input_interpolate_2d( var, z_grid, z_file)
4699
4700       IMPLICIT NONE
4701
4702       INTEGER(iwp) ::  i       !< running index x- or y -direction
4703       INTEGER(iwp) ::  il      !< lower index bound along x- or y-direction
4704       INTEGER(iwp) ::  iu      !< upper index bound along x- or y-direction
4705       INTEGER(iwp) ::  k       !< running index z-direction file
4706       INTEGER(iwp) ::  kk      !< running index z-direction stretched model grid
4707       INTEGER(iwp) ::  kl      !< lower index bound along z-direction
4708       INTEGER(iwp) ::  ku      !< upper index bound along z-direction
4709
4710       REAL(wp), DIMENSION(:) ::  z_grid                  !< grid levels on numeric grid
4711       REAL(wp), DIMENSION(:) ::  z_file                  !< grid levels on file grid
4712       REAL(wp), DIMENSION(:,:), INTENT(INOUT) ::  var    !< treated variable
4713       REAL(wp), DIMENSION(:), ALLOCATABLE   ::  var_tmp  !< temporary variable
4714
4715
4716       il = LBOUND(var,2)
4717       iu = UBOUND(var,2)
4718       kl = LBOUND(var,1)
4719       ku = UBOUND(var,1)
4720       ALLOCATE( var_tmp(kl:ku) )
4721
4722       DO  i = il, iu
4723          DO  k = kl, ku
4724
4725             kk = MINLOC( ABS( z_file - z_grid(k) ), DIM = 1 )
4726
4727             IF ( kk < ku )  THEN
4728                IF ( z_file(kk) - z_grid(k) <= 0.0_wp )  THEN
4729                   var_tmp(k) = var(kk,i) +                                    &
4730                                          ( var(kk+1,i)      - var(kk,i)  ) /  &
4731                                          ( z_file(kk+1)     - z_file(kk) ) *  &
4732                                          ( z_grid(k)        - z_file(kk) )
4733
4734                ELSEIF ( z_file(kk) - z_grid(k) > 0.0_wp )  THEN
4735                   var_tmp(k) = var(kk-1,i) +                                  &
4736                                            ( var(kk,i)   - var(kk-1,i)  ) /   &
4737                                            ( z_file(kk)  - z_file(kk-1) ) *   &
4738                                            ( z_grid(k)   - z_file(kk-1) )
4739                ENDIF
4740!
4741!--          Extrapolate
4742             ELSE
4743
4744                var_tmp(k) = var(ku,i) + ( var(ku,i)  - var(ku-1,i)    ) /     &
4745                                         ( z_file(ku) - z_file(ku-1)   ) *     &
4746                                         ( z_grid(k)  - z_file(ku)     )
4747
4748             ENDIF
4749
4750          ENDDO
4751          var(:,i) = var_tmp(:)
4752
4753       ENDDO
4754
4755       DEALLOCATE( var_tmp )
4756
4757
4758    END SUBROUTINE netcdf_data_input_interpolate_2d
4759
4760!------------------------------------------------------------------------------!
4761! Description:
4762! ------------
4763!> Vertical interpolation and extrapolation of 3D variables.
4764!------------------------------------------------------------------------------!
4765    SUBROUTINE netcdf_data_input_interpolate_3d( var, z_grid, z_file )
4766
4767       IMPLICIT NONE
4768
4769       INTEGER(iwp) ::  i       !< running index x-direction
4770       INTEGER(iwp) ::  il      !< lower index bound along x-direction
4771       INTEGER(iwp) ::  iu      !< upper index bound along x-direction
4772       INTEGER(iwp) ::  j       !< running index y-direction
4773       INTEGER(iwp) ::  jl      !< lower index bound along x-direction
4774       INTEGER(iwp) ::  ju      !< upper index bound along x-direction
4775       INTEGER(iwp) ::  k       !< running index z-direction file
4776       INTEGER(iwp) ::  kk      !< running index z-direction stretched model grid
4777       INTEGER(iwp) ::  kl      !< lower index bound along z-direction
4778       INTEGER(iwp) ::  ku      !< upper index bound along z-direction
4779
4780       REAL(wp), DIMENSION(:) ::  z_grid                      !< grid levels on numeric grid
4781       REAL(wp), DIMENSION(:) ::  z_file                      !< grid levels on file grid
4782       REAL(wp), DIMENSION(:,:,:), INTENT(INOUT) ::  var      !< treated variable
4783       REAL(wp), DIMENSION(:), ALLOCATABLE       ::  var_tmp  !< temporary variable
4784
4785       il = LBOUND(var,3)
4786       iu = UBOUND(var,3)
4787       jl = LBOUND(var,2)
4788       ju = UBOUND(var,2)
4789       kl = LBOUND(var,1)
4790       ku = UBOUND(var,1)
4791
4792       ALLOCATE( var_tmp(kl:ku) )
4793
4794       DO  i = il, iu
4795          DO  j = jl, ju
4796             DO  k = kl, ku
4797
4798                kk = MINLOC( ABS( z_file - z_grid(k) ), DIM = 1 )
4799
4800                IF ( kk < ku )  THEN
4801                   IF ( z_file(kk) - z_grid(k) <= 0.0_wp )  THEN
4802                      var_tmp(k) = var(kk,j,i) +                               &
4803                                             ( var(kk+1,j,i) - var(kk,j,i) ) / &
4804                                             ( z_file(kk+1)  - z_file(kk)  ) * &
4805                                             ( z_grid(k)     - z_file(kk)  )
4806
4807                   ELSEIF ( z_file(kk) - z_grid(k) > 0.0_wp )  THEN
4808                      var_tmp(k) = var(kk-1,j,i) +                             &
4809                                             ( var(kk,j,i) - var(kk-1,j,i) ) / &
4810                                             ( z_file(kk)  - z_file(kk-1)  ) * &
4811                                             ( z_grid(k)   - z_file(kk-1)  )
4812                   ENDIF
4813!
4814!--             Extrapolate
4815                ELSE
4816                   var_tmp(k) = var(ku,j,i) +                                  &
4817                                       ( var(ku,j,i)  - var(ku-1,j,i)   ) /    &
4818                                       ( z_file(ku)   - z_file(ku-1)    ) *    &
4819                                       ( z_grid(k)    - z_file(ku)      )
4820
4821                ENDIF
4822             ENDDO
4823             var(:,j,i) = var_tmp(:)
4824          ENDDO
4825       ENDDO
4826
4827       DEALLOCATE( var_tmp )
4828
4829
4830    END SUBROUTINE netcdf_data_input_interpolate_3d
4831
4832!------------------------------------------------------------------------------!
4833! Description:
4834! ------------
4835!> Checks if a given variables is on file
4836!------------------------------------------------------------------------------!
4837    FUNCTION check_existence( vars_in_file, var_name )
4838
4839       IMPLICIT NONE
4840
4841       CHARACTER(LEN=*) ::  var_name                   !< variable to be checked
4842       CHARACTER(LEN=*), DIMENSION(:) ::  vars_in_file !< list of variables in file
4843
4844       INTEGER(iwp) ::  i                              !< loop variable
4845
4846       LOGICAL ::  check_existence                     !< flag indicating whether a variable exist or not - actual return value
4847
4848       i = 1
4849       check_existence = .FALSE.
4850       DO  WHILE ( i <= SIZE( vars_in_file ) )
4851          check_existence = TRIM( vars_in_file(i) ) == TRIM( var_name )  .OR.  &
4852                            check_existence
4853          i = i + 1
4854       ENDDO
4855
4856       RETURN
4857
4858    END FUNCTION check_existence
4859
4860
4861!------------------------------------------------------------------------------!
4862! Description:
4863! ------------
4864!> Closes an existing netCDF file.
4865!------------------------------------------------------------------------------!
4866    SUBROUTINE close_input_file( id )
4867#if defined( __netcdf )
4868
4869       USE pegrid
4870
4871       IMPLICIT NONE
4872
4873       INTEGER(iwp), INTENT(INOUT)        ::  id        !< file id
4874
4875       nc_stat = NF90_CLOSE( id )
4876       CALL handle_error( 'close', 540 )
4877#endif
4878    END SUBROUTINE close_input_file
4879
4880!------------------------------------------------------------------------------!
4881! Description:
4882! ------------
4883!> Opens an existing netCDF file for reading only and returns its id.
4884!------------------------------------------------------------------------------!
4885    SUBROUTINE open_read_file( filename, id )
4886#if defined( __netcdf )
4887
4888       USE pegrid
4889
4890       IMPLICIT NONE
4891
4892       CHARACTER (LEN=*), INTENT(IN) ::  filename  !< filename
4893       INTEGER(iwp), INTENT(INOUT)   ::  id        !< file id
4894
4895#if defined( __netcdf4_parallel )
4896!      if __netcdf4_parallel is defined, parrallel NetCDF will be used unconditionally
4897       nc_stat = NF90_OPEN( filename, IOR( NF90_WRITE, NF90_MPIIO ), id,  &
4898                            COMM = comm2d, INFO = MPI_INFO_NULL )
4899       IF(nc_stat /= NF90_NOERR )  THEN                                       !possible NetCDF 3 file
4900           nc_stat = NF90_OPEN( filename, NF90_NOWRITE, id )
4901           collective_read = .FALSE.
4902       ELSE
4903           collective_read = .TRUE.
4904       END IF
4905#else
4906!      All MPI processes open und read
4907       nc_stat = NF90_OPEN( filename, NF90_NOWRITE, id )
4908#endif
4909
4910       CALL handle_error( 'open_read_file', 539 )
4911
4912#endif
4913    END SUBROUTINE open_read_file
4914
4915!------------------------------------------------------------------------------!
4916! Description:
4917! ------------
4918!> Reads global or variable-related attributes of type INTEGER (32-bit)
4919!------------------------------------------------------------------------------!
4920     SUBROUTINE get_attribute_int32( id, attribute_name, value, global,        &
4921                                     variable_name )
4922
4923       USE pegrid
4924
4925       IMPLICIT NONE
4926
4927       CHARACTER(LEN=*)            ::  attribute_name   !< attribute name
4928       CHARACTER(LEN=*), OPTIONAL  ::  variable_name    !< variable name
4929
4930       INTEGER(iwp), INTENT(IN)    ::  id               !< file id
4931       INTEGER(iwp)                ::  id_var           !< variable id
4932       INTEGER(iwp), INTENT(INOUT) ::  value            !< read value
4933
4934       LOGICAL, INTENT(IN) ::  global                   !< flag indicating global attribute
4935#if defined( __netcdf )
4936
4937!
4938!--    Read global attribute
4939       IF ( global )  THEN
4940          nc_stat = NF90_GET_ATT( id, NF90_GLOBAL, TRIM( attribute_name ), value )
4941          CALL handle_error( 'get_attribute_int32 global', 522, attribute_name )
4942!
4943!--    Read attributes referring to a single variable. Therefore, first inquire
4944!--    variable id
4945       ELSE
4946          nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
4947          CALL handle_error( 'get_attribute_int32', 522, attribute_name )
4948          nc_stat = NF90_GET_ATT( id, id_var, TRIM( attribute_name ), value )
4949          CALL handle_error( 'get_attribute_int32', 522, attribute_name )
4950       ENDIF
4951#endif
4952    END SUBROUTINE get_attribute_int32
4953
4954!------------------------------------------------------------------------------!
4955! Description:
4956! ------------
4957!> Reads global or variable-related attributes of type INTEGER (8-bit)
4958!------------------------------------------------------------------------------!
4959     SUBROUTINE get_attribute_int8( id, attribute_name, value, global,         &
4960                                    variable_name )
4961
4962       USE pegrid
4963
4964       IMPLICIT NONE
4965
4966       CHARACTER(LEN=*)            ::  attribute_name   !< attribute name
4967       CHARACTER(LEN=*), OPTIONAL  ::  variable_name    !< variable name
4968
4969       INTEGER(iwp), INTENT(IN)    ::  id               !< file id
4970       INTEGER(iwp)                ::  id_var           !< variable id
4971       INTEGER(KIND=1), INTENT(INOUT) ::  value         !< read value
4972
4973       LOGICAL, INTENT(IN) ::  global                   !< flag indicating global attribute
4974#if defined( __netcdf )
4975
4976!
4977!--    Read global attribute
4978       IF ( global )  THEN
4979          nc_stat = NF90_GET_ATT( id, NF90_GLOBAL, TRIM( attribute_name ), value )
4980          CALL handle_error( 'get_attribute_int8 global', 523, attribute_name )
4981!
4982!--    Read attributes referring to a single variable. Therefore, first inquire
4983!--    variable id
4984       ELSE
4985          nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
4986          CALL handle_error( 'get_attribute_int8', 523, attribute_name )
4987          nc_stat = NF90_GET_ATT( id, id_var, TRIM( attribute_name ), value )
4988          CALL handle_error( 'get_attribute_int8', 523, attribute_name )
4989       ENDIF
4990#endif
4991    END SUBROUTINE get_attribute_int8
4992
4993!------------------------------------------------------------------------------!
4994! Description:
4995! ------------
4996!> Reads global or variable-related attributes of type REAL
4997!------------------------------------------------------------------------------!
4998     SUBROUTINE get_attribute_real( id, attribute_name, value, global,         &
4999                                    variable_name )
5000
5001       USE pegrid
5002
5003       IMPLICIT NONE
5004
5005       CHARACTER(LEN=*)            ::  attribute_name   !< attribute name
5006       CHARACTER(LEN=*), OPTIONAL  ::  variable_name    !< variable name
5007
5008       INTEGER(iwp), INTENT(IN)    ::  id               !< file id
5009       INTEGER(iwp)                ::  id_var           !< variable id
5010
5011       LOGICAL, INTENT(IN) ::  global                   !< flag indicating global attribute
5012
5013       REAL(wp), INTENT(INOUT)     ::  value            !< read value
5014#if defined( __netcdf )
5015
5016
5017!
5018!-- Read global attribute
5019       IF ( global )  THEN
5020          nc_stat = NF90_GET_ATT( id, NF90_GLOBAL, TRIM( attribute_name ), value )
5021          CALL handle_error( 'get_attribute_real global', 524, attribute_name )
5022!
5023!-- Read attributes referring to a single variable. Therefore, first inquire
5024!-- variable id
5025       ELSE
5026          nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5027          CALL handle_error( 'get_attribute_real', 524, attribute_name )
5028          nc_stat = NF90_GET_ATT( id, id_var, TRIM( attribute_name ), value )
5029          CALL handle_error( 'get_attribute_real', 524, attribute_name )
5030       ENDIF
5031#endif
5032    END SUBROUTINE get_attribute_real
5033
5034!------------------------------------------------------------------------------!
5035! Description:
5036! ------------
5037!> Reads global or variable-related attributes of type CHARACTER
5038!> Remark: reading attributes of type NF_STRING return an error code 56 -
5039!> Attempt to convert between text & numbers.
5040!------------------------------------------------------------------------------!
5041     SUBROUTINE get_attribute_string( id, attribute_name, value, global,       &
5042                                      variable_name, no_abort )
5043
5044       USE pegrid
5045
5046       IMPLICIT NONE
5047
5048       CHARACTER(LEN=*)                ::  attribute_name   !< attribute name
5049       CHARACTER(LEN=*), OPTIONAL      ::  variable_name    !< variable name
5050       CHARACTER(LEN=*), INTENT(INOUT) ::  value            !< read value
5051
5052       INTEGER(iwp), INTENT(IN)    ::  id               !< file id
5053       INTEGER(iwp)                ::  id_var           !< variable id
5054
5055       LOGICAL ::  check_error                          !< flag indicating if handle_error shall be checked
5056       LOGICAL, INTENT(IN) ::  global                   !< flag indicating global attribute
5057       LOGICAL, INTENT(IN), OPTIONAL ::  no_abort       !< flag indicating if errors should be checked
5058#if defined( __netcdf )
5059
5060       IF ( PRESENT( no_abort ) )  THEN
5061          check_error = no_abort
5062       ELSE
5063          check_error = .TRUE.
5064       ENDIF
5065!
5066!--    Read global attribute
5067       IF ( global )  THEN
5068          nc_stat = NF90_GET_ATT( id, NF90_GLOBAL, TRIM( attribute_name ), value )
5069          IF ( check_error)  CALL handle_error( 'get_attribute_string global', 525, attribute_name )
5070!
5071!--    Read attributes referring to a single variable. Therefore, first inquire
5072!--    variable id
5073       ELSE
5074          nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5075          IF ( check_error)  CALL handle_error( 'get_attribute_string', 525, attribute_name )
5076
5077          nc_stat = NF90_GET_ATT( id, id_var, TRIM( attribute_name ), value )
5078          IF ( check_error)  CALL handle_error( 'get_attribute_string',525, attribute_name )
5079
5080       ENDIF
5081#endif
5082    END SUBROUTINE get_attribute_string
5083
5084
5085
5086!------------------------------------------------------------------------------!
5087! Description:
5088! ------------
5089!> Get dimension array for a given dimension
5090!------------------------------------------------------------------------------!
5091     SUBROUTINE netcdf_data_input_get_dimension_length( id, dim_len,           &
5092                                                        variable_name )
5093       USE pegrid
5094
5095       IMPLICIT NONE
5096
5097       CHARACTER(LEN=*)            ::  variable_name    !< dimension name
5098       CHARACTER(LEN=100)          ::  dum              !< dummy variable to receive return character
5099
5100       INTEGER(iwp)                ::  dim_len          !< dimension size
5101       INTEGER(iwp), INTENT(IN)    ::  id               !< file id
5102       INTEGER(iwp)                ::  id_dim           !< dimension id
5103
5104#if defined( __netcdf )
5105!
5106!--    First, inquire dimension ID
5107       nc_stat = NF90_INQ_DIMID( id, TRIM( variable_name ), id_dim )
5108       CALL handle_error( 'netcdf_data_input_get_dimension_length', 526,       &
5109                          variable_name )
5110!
5111!--    Inquire dimension length
5112       nc_stat = NF90_INQUIRE_DIMENSION( id, id_dim, dum, LEN = dim_len )
5113       CALL handle_error( 'netcdf_data_input_get_dimension_length', 526,       &
5114                          variable_name )
5115
5116#endif
5117    END SUBROUTINE netcdf_data_input_get_dimension_length
5118
5119!------------------------------------------------------------------------------!
5120! Description:
5121! ------------
5122!> Routine for reading-in a character string from the chem emissions netcdf
5123!> input file. 
5124!------------------------------------------------------------------------------!
5125    SUBROUTINE get_variable_string( id, variable_name, var_string, names_number)
5126#if defined( __netcdf )
5127
5128       USE indices
5129       USE pegrid
5130
5131       IMPLICIT NONE
5132
5133       CHARACTER (LEN=25), ALLOCATABLE, DIMENSION(:), INTENT(INOUT)  :: var_string
5134
5135       CHARACTER(LEN=*)                                              :: variable_name          !> variable name
5136
5137       CHARACTER (LEN=1), ALLOCATABLE, DIMENSION(:,:)                :: tmp_var_string         !> variable to be read
5138
5139
5140       INTEGER(iwp), INTENT(IN)                                      :: id                     !> file id
5141
5142       INTEGER(iwp), INTENT(IN)                                      :: names_number           !> number of names
5143
5144       INTEGER(iwp)                                                  :: id_var                 !> variable id
5145
5146       INTEGER(iwp)                                                  :: i,j                    !> index to go through the length of the dimensions
5147
5148       INTEGER(iwp)                                                  :: max_string_length=25   !> this is both the maximum length of a name, but also 
5149                                                                                            ! the number of the components of the first dimensions
5150                                                                                            ! (rows)
5151
5152
5153       ALLOCATE(tmp_var_string(max_string_length,names_number))
5154
5155       ALLOCATE(var_string(names_number))
5156
5157    !-- Inquire variable id
5158       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5159
5160
5161    !-- Get variable
5162    !-- Start cycle over the emission species
5163       DO i = 1, names_number 
5164       !-- read the first letter of each component
5165          nc_stat = NF90_GET_VAR( id, id_var, var_string(i), start = (/ 1,i /), &
5166                                 count = (/ 1,1 /) )
5167          CALL handle_error( 'get_variable_string', 701 )
5168
5169       !-- Start cycle over charachters
5170          DO j = 1, max_string_length
5171                       
5172          !-- read the rest of the components of the name
5173             nc_stat = NF90_GET_VAR( id, id_var, tmp_var_string(j,i), start = (/ j,i /),&
5174                                     count = (/ 1,1 /) )
5175             CALL handle_error( 'get_variable_string', 702 )
5176
5177             IF ( iachar(tmp_var_string(j,i) ) == 0 ) THEN
5178                  tmp_var_string(j,i)=''
5179             ENDIF
5180
5181             IF ( j>1 ) THEN
5182             !-- Concatenate first letter of the name and the others
5183                var_string(i)=TRIM(var_string(i)) // TRIM(tmp_var_string(j,i))
5184
5185             ENDIF
5186          ENDDO 
5187       ENDDO
5188
5189#endif
5190    END SUBROUTINE get_variable_string
5191
5192!------------------------------------------------------------------------------!
5193! Description:
5194! ------------
5195!> Reads a character variable in a 1D array
5196!------------------------------------------------------------------------------!
5197     SUBROUTINE get_variable_1d_char( id, variable_name, var )
5198
5199       USE pegrid
5200
5201       IMPLICIT NONE
5202
5203       CHARACTER(LEN=*)            ::  variable_name          !< variable name
5204       CHARACTER(LEN=*), DIMENSION(:), INTENT(INOUT) ::  var  !< variable to be read
5205
5206       INTEGER(iwp)                ::  i                !< running index over variable dimension
5207       INTEGER(iwp), INTENT(IN)    ::  id               !< file id
5208       INTEGER(iwp)                ::  id_var           !< dimension id
5209       
5210       INTEGER(iwp), DIMENSION(2)  ::  dimid            !< dimension IDs
5211       INTEGER(iwp), DIMENSION(2)  ::  dimsize          !< dimension size
5212
5213#if defined( __netcdf )
5214
5215!
5216!--    First, inquire variable ID
5217       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5218       CALL handle_error( 'get_variable_1d_int', 527, variable_name )
5219!
5220!--    Inquire dimension IDs
5221       nc_stat = NF90_INQUIRE_VARIABLE( id, id_var, dimids = dimid(1:2) )
5222       CALL handle_error( 'get_variable_1d_char', 527, variable_name )
5223!
5224!--    Read dimesnion length
5225       nc_stat = NF90_INQUIRE_DIMENSION( id, dimid(1), LEN = dimsize(1) )
5226       nc_stat = NF90_INQUIRE_DIMENSION( id, dimid(2), LEN = dimsize(2) )
5227       
5228!
5229!--    Read character array. Note, each element is read individually, in order
5230!--    to better separate single strings.
5231       DO  i = 1, dimsize(2)
5232          nc_stat = NF90_GET_VAR( id, id_var, var(i),                          &
5233                                  start = (/ 1, i /),                          &
5234                                  count = (/ dimsize(1), 1 /) )
5235          CALL handle_error( 'get_variable_1d_char', 527, variable_name )
5236       ENDDO     
5237                         
5238#endif
5239    END SUBROUTINE get_variable_1d_char
5240
5241   
5242!------------------------------------------------------------------------------!
5243! Description:
5244! ------------
5245!> Reads a 1D integer variable from file.
5246!------------------------------------------------------------------------------!
5247     SUBROUTINE get_variable_1d_int( id, variable_name, var )
5248
5249       USE pegrid
5250
5251       IMPLICIT NONE
5252
5253       CHARACTER(LEN=*)            ::  variable_name    !< variable name
5254
5255       INTEGER(iwp), INTENT(IN)    ::  id               !< file id
5256       INTEGER(iwp)                ::  id_var           !< dimension id
5257
5258       INTEGER(iwp), DIMENSION(:), INTENT(INOUT) ::  var  !< variable to be read
5259#if defined( __netcdf )
5260
5261!
5262!--    First, inquire variable ID
5263       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5264       CALL handle_error( 'get_variable_1d_int', 527, variable_name )
5265!
5266!--    Inquire dimension length
5267       nc_stat = NF90_GET_VAR( id, id_var, var )
5268       CALL handle_error( 'get_variable_1d_int', 527, variable_name )
5269
5270#endif
5271    END SUBROUTINE get_variable_1d_int
5272
5273!------------------------------------------------------------------------------!
5274! Description:
5275! ------------
5276!> Reads a 1D float variable from file.
5277!------------------------------------------------------------------------------!
5278     SUBROUTINE get_variable_1d_real( id, variable_name, var )
5279
5280       USE pegrid
5281
5282       IMPLICIT NONE
5283
5284       CHARACTER(LEN=*)            ::  variable_name    !< variable name
5285
5286       INTEGER(iwp), INTENT(IN)    ::  id               !< file id
5287       INTEGER(iwp)                ::  id_var           !< dimension id
5288
5289       REAL(wp), DIMENSION(:), INTENT(INOUT) ::  var    !< variable to be read
5290#if defined( __netcdf )
5291
5292!
5293!--    First, inquire variable ID
5294       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5295       CALL handle_error( 'get_variable_1d_real', 528, variable_name )
5296!
5297!--    Inquire dimension length
5298       nc_stat = NF90_GET_VAR( id, id_var, var )
5299       CALL handle_error( 'get_variable_1d_real', 528, variable_name )
5300
5301#endif
5302    END SUBROUTINE get_variable_1d_real
5303
5304
5305!------------------------------------------------------------------------------!
5306! Description:
5307! ------------
5308!> Reads a time-dependent 1D float variable from file.
5309!------------------------------------------------------------------------------!
5310    SUBROUTINE get_variable_pr( id, variable_name, t, var )
5311#if defined( __netcdf )
5312
5313       USE pegrid
5314
5315       IMPLICIT NONE
5316
5317       CHARACTER(LEN=*)                      ::  variable_name    !< variable name
5318
5319       INTEGER(iwp), INTENT(IN)              ::  id               !< file id
5320       INTEGER(iwp), DIMENSION(1:2)          ::  id_dim           !< dimension ids
5321       INTEGER(iwp)                          ::  id_var           !< dimension id
5322       INTEGER(iwp)                          ::  n_file           !< number of data-points in file along z dimension
5323       INTEGER(iwp), INTENT(IN)              ::  t                !< timestep number
5324
5325       REAL(wp), DIMENSION(:), INTENT(INOUT) ::  var  !< variable to be read
5326
5327!
5328!--    First, inquire variable ID
5329       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5330!
5331!--    Inquire dimension size of vertical dimension
5332       nc_stat = NF90_INQUIRE_VARIABLE( id, id_var, DIMIDS = id_dim )
5333       nc_stat = NF90_INQUIRE_DIMENSION( id, id_dim(1), LEN = n_file )
5334!
5335!--    Read variable.
5336       nc_stat = NF90_GET_VAR( id, id_var, var,                                &
5337                               start = (/ 1,      t     /),                    &
5338                               count = (/ n_file, 1     /) )
5339       CALL handle_error( 'get_variable_pr', 529, variable_name )
5340
5341#endif
5342    END SUBROUTINE get_variable_pr
5343
5344
5345!------------------------------------------------------------------------------!
5346! Description:
5347! ------------
5348!> Reads a 2D REAL variable from a file. Reading is done processor-wise,
5349!> i.e. each core reads its own domain in slices along x.
5350!------------------------------------------------------------------------------!
5351    SUBROUTINE get_variable_2d_real( id, variable_name, var, is, ie, js, je )
5352
5353       USE indices
5354       USE pegrid
5355
5356       IMPLICIT NONE
5357
5358       CHARACTER(LEN=*)              ::  variable_name   !< variable name
5359
5360       INTEGER(iwp)                  ::  i               !< running index along x direction
5361       INTEGER(iwp)                  ::  ie              !< start index for subdomain input along x direction
5362       INTEGER(iwp)                  ::  is              !< end index for subdomain input along x direction
5363       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
5364       INTEGER(iwp)                  ::  id_var          !< variable id
5365       INTEGER(iwp)                  ::  j               !< running index along y direction
5366       INTEGER(iwp)                  ::  je              !< start index for subdomain input along y direction
5367       INTEGER(iwp)                  ::  js              !< end index for subdomain input along y direction
5368       
5369       REAL(wp), DIMENSION(:,:), ALLOCATABLE   ::  tmp   !< temporary variable to read data from file according
5370                                                         !< to its reverse memory access
5371       REAL(wp), DIMENSION(:,:), INTENT(INOUT) ::  var   !< variable to be read
5372#if defined( __netcdf )
5373!
5374!--    Inquire variable id
5375       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5376!
5377!--    Check for collective read-operation and set respective NetCDF flags if
5378!--    required.
5379       IF ( collective_read )  THEN
5380#if defined( __netcdf4_parallel )
5381          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
5382#endif
5383       ENDIF
5384
5385
5386       !Temporary solution for reading emission chemistry files: TBD: we should discuss whether remove it or not
5387       IF ( id==id_emis ) THEN
5388
5389          !--    Allocate temporary variable according to memory access on file.
5390          ALLOCATE( tmp(is:ie,js:je) )
5391
5392          !--    Get variable
5393          nc_stat = NF90_GET_VAR( id, id_var, tmp,                                &
5394                                  start = (/ is,      js /),                  &
5395                                  count = (/ ie-is+1 , je-js+1 /) ) 
5396
5397          var=tmp
5398
5399          CALL handle_error( 'get_variable_2d_real', 530, variable_name ) !TBD: the error number shuld be changed, but since the solution is
5400                                                                          ! provisory, we give the same as below
5401 
5402          DEALLOCATE( tmp )
5403       
5404       !>  Original Subroutine part
5405       ELSE
5406!
5407!--       Allocate temporary variable according to memory access on file.
5408          ALLOCATE( tmp(is:ie,js:je) )
5409!
5410!--       Get variable
5411          nc_stat = NF90_GET_VAR( id, id_var, tmp,                             &
5412                                  start = (/ is+1,      js+1 /),               &
5413                                  count = (/ ie-is + 1, je-js+1 /) )   
5414          CALL handle_error( 'get_variable_2d_real', 530, variable_name )
5415!
5416!--       Resort data. Please note, dimension subscripts of var all start at 1.
5417          DO  i = is, ie 
5418             DO  j = js, je 
5419                var(j-js+1,i-is+1) = tmp(i,j)
5420             ENDDO
5421          ENDDO
5422       
5423          DEALLOCATE( tmp )
5424
5425       ENDIF
5426#endif
5427    END SUBROUTINE get_variable_2d_real
5428
5429!------------------------------------------------------------------------------!
5430! Description:
5431! ------------
5432!> Reads a 2D 32-bit INTEGER variable from file. Reading is done processor-wise,
5433!> i.e. each core reads its own domain in slices along x.
5434!------------------------------------------------------------------------------!
5435    SUBROUTINE get_variable_2d_int32( id, variable_name, var, is, ie, js, je )
5436
5437       USE indices
5438       USE pegrid
5439
5440       IMPLICIT NONE
5441
5442       CHARACTER(LEN=*)              ::  variable_name   !< variable name
5443
5444       INTEGER(iwp)                  ::  i               !< running index along x direction
5445       INTEGER(iwp)                  ::  ie              !< start index for subdomain input along x direction
5446       INTEGER(iwp)                  ::  is              !< end index for subdomain input along x direction
5447       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
5448       INTEGER(iwp)                  ::  id_var          !< variable id
5449       INTEGER(iwp)                  ::  j               !< running index along y direction
5450       INTEGER(iwp)                  ::  je              !< start index for subdomain input along y direction
5451       INTEGER(iwp)                  ::  js              !< end index for subdomain input along y direction
5452       
5453       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE   ::  tmp  !< temporary variable to read data from file according
5454                                                            !< to its reverse memory access
5455       INTEGER(iwp), DIMENSION(:,:), INTENT(INOUT) ::  var  !< variable to be read
5456#if defined( __netcdf )
5457!
5458!--    Inquire variable id
5459       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5460!
5461!--    Check for collective read-operation and set respective NetCDF flags if
5462!--    required.
5463       IF ( collective_read )  THEN
5464#if defined( __netcdf4_parallel )       
5465          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
5466#endif
5467       ENDIF
5468!
5469!--    Allocate temporary variable according to memory access on file.
5470       ALLOCATE( tmp(is:ie,js:je) )
5471!
5472!--    Get variable
5473       nc_stat = NF90_GET_VAR( id, id_var, tmp,                                &
5474                               start = (/ is+1,      js+1 /),                  &
5475                               count = (/ ie-is + 1, je-js+1 /) )   
5476                               
5477       CALL handle_error( 'get_variable_2d_int32', 531, variable_name )                             
5478!
5479!--    Resort data. Please note, dimension subscripts of var all start at 1.
5480       DO  i = is, ie 
5481          DO  j = js, je 
5482             var(j-js+1,i-is+1) = tmp(i,j)
5483          ENDDO
5484       ENDDO
5485       
5486       DEALLOCATE( tmp )
5487
5488#endif
5489    END SUBROUTINE get_variable_2d_int32
5490
5491!------------------------------------------------------------------------------!
5492! Description:
5493! ------------
5494!> Reads a 2D 8-bit INTEGER variable from file. Reading is done processor-wise,
5495!> i.e. each core reads its own domain in slices along x.
5496!------------------------------------------------------------------------------!
5497    SUBROUTINE get_variable_2d_int8( id, variable_name, var, is, ie, js, je )
5498
5499       USE indices
5500       USE pegrid
5501
5502       IMPLICIT NONE
5503
5504       CHARACTER(LEN=*)              ::  variable_name   !< variable name
5505
5506       INTEGER(iwp)                  ::  i               !< running index along x direction
5507       INTEGER(iwp)                  ::  ie              !< start index for subdomain input along x direction
5508       INTEGER(iwp)                  ::  is              !< end index for subdomain input along x direction
5509       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
5510       INTEGER(iwp)                  ::  id_var          !< variable id
5511       INTEGER(iwp)                  ::  j               !< running index along y direction
5512       INTEGER(iwp)                  ::  je              !< start index for subdomain input along y direction
5513       INTEGER(iwp)                  ::  js              !< end index for subdomain input along y direction
5514       
5515       INTEGER(KIND=1), DIMENSION(:,:), ALLOCATABLE   ::  tmp  !< temporary variable to read data from file according
5516                                                               !< to its reverse memory access
5517       INTEGER(KIND=1), DIMENSION(:,:), INTENT(INOUT) ::  var  !< variable to be read
5518#if defined( __netcdf )
5519!
5520!--    Inquire variable id
5521       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5522!
5523!--    Check for collective read-operation and set respective NetCDF flags if
5524!--    required.
5525       IF ( collective_read )  THEN
5526#if defined( __netcdf4_parallel )       
5527          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
5528#endif         
5529       ENDIF
5530!
5531!--    Allocate temporary variable according to memory access on file.
5532       ALLOCATE( tmp(is:ie,js:je) )
5533!
5534!--    Get variable
5535       nc_stat = NF90_GET_VAR( id, id_var, tmp,                                &
5536                               start = (/ is+1,      js+1 /),                  &
5537                               count = (/ ie-is + 1, je-js+1 /) )   
5538                               
5539       CALL handle_error( 'get_variable_2d_int8', 532, variable_name )
5540!
5541!--    Resort data. Please note, dimension subscripts of var all start at 1.
5542       DO  i = is, ie 
5543          DO  j = js, je 
5544             var(j-js+1,i-is+1) = tmp(i,j)
5545          ENDDO
5546       ENDDO
5547       
5548       DEALLOCATE( tmp )
5549
5550#endif
5551    END SUBROUTINE get_variable_2d_int8
5552
5553
5554!------------------------------------------------------------------------------!
5555! Description:
5556! ------------
5557!> Reads a 3D 8-bit INTEGER variable from file.
5558!------------------------------------------------------------------------------!
5559    SUBROUTINE get_variable_3d_int8( id, variable_name, var, is, ie, js, je,   &
5560                                     ks, ke )
5561
5562       USE indices
5563       USE pegrid
5564
5565       IMPLICIT NONE
5566
5567       CHARACTER(LEN=*)              ::  variable_name   !< variable name
5568
5569       INTEGER(iwp)                  ::  i               !< index along x direction
5570       INTEGER(iwp)                  ::  ie              !< start index for subdomain input along x direction
5571       INTEGER(iwp)                  ::  is              !< end index for subdomain input along x direction
5572       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
5573       INTEGER(iwp)                  ::  id_var          !< variable id
5574       INTEGER(iwp)                  ::  j               !< index along y direction
5575       INTEGER(iwp)                  ::  je              !< start index for subdomain input along y direction
5576       INTEGER(iwp)                  ::  js              !< end index for subdomain input along y direction
5577       INTEGER(iwp)                  ::  k               !< index along any 3rd dimension
5578       INTEGER(iwp)                  ::  ke              !< start index of 3rd dimension
5579       INTEGER(iwp)                  ::  ks              !< end index of 3rd dimension
5580
5581       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE   ::  tmp  !< temporary variable to read data from file according
5582                                                                 !< to its reverse memory access
5583
5584       INTEGER(KIND=1), DIMENSION(:,:,:), INTENT(INOUT) ::  var  !< variable to be read
5585#if defined( __netcdf )
5586
5587!
5588!--    Inquire variable id
5589       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5590!
5591!--    Check for collective read-operation and set respective NetCDF flags if
5592!--    required.
5593       IF ( collective_read )  THEN
5594#if defined( __netcdf4_parallel )
5595          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
5596#endif
5597       ENDIF
5598!
5599!--    Allocate temporary variable according to memory access on file.
5600       ALLOCATE( tmp(is:ie,js:je,ks:ke) )
5601!
5602!--    Get variable
5603       nc_stat = NF90_GET_VAR( id, id_var, tmp,                                &
5604                               start = (/ is+1,    js+1,    ks+1 /),           &
5605                               count = (/ ie-is+1, je-js+1, ke-ks+1 /) )
5606
5607       CALL handle_error( 'get_variable_3d_int8', 533, variable_name )
5608!
5609!--    Resort data. Please note, dimension subscripts of var all start at 1.
5610       DO  i = is, ie 
5611          DO  j = js, je
5612             DO  k = ks, ke
5613                var(k-ks+1,j-js+1,i-is+1) = tmp(i,j,k)
5614             ENDDO
5615          ENDDO
5616       ENDDO
5617
5618       DEALLOCATE( tmp )
5619
5620#endif
5621    END SUBROUTINE get_variable_3d_int8
5622
5623
5624!------------------------------------------------------------------------------!
5625! Description:
5626! ------------
5627!> Reads a 3D float variable from file.
5628!------------------------------------------------------------------------------!
5629    SUBROUTINE get_variable_3d_real( id, variable_name, var, is, ie, js, je,   &
5630                                     ks, ke )
5631
5632       USE indices
5633       USE pegrid
5634
5635       IMPLICIT NONE
5636
5637       CHARACTER(LEN=*)              ::  variable_name   !< variable name
5638
5639       INTEGER(iwp)                  ::  i               !< index along x direction
5640       INTEGER(iwp)                  ::  ie              !< start index for subdomain input along x direction
5641       INTEGER(iwp)                  ::  is              !< end index for subdomain input along x direction
5642       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
5643       INTEGER(iwp)                  ::  id_var          !< variable id
5644       INTEGER(iwp)                  ::  j               !< index along y direction
5645       INTEGER(iwp)                  ::  je              !< start index for subdomain input along y direction
5646       INTEGER(iwp)                  ::  js              !< end index for subdomain input along y direction
5647       INTEGER(iwp)                  ::  k               !< index along any 3rd dimension
5648       INTEGER(iwp)                  ::  ke              !< start index of 3rd dimension
5649       INTEGER(iwp)                  ::  ks              !< end index of 3rd dimension
5650
5651       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE   ::  tmp !< temporary variable to read data from file according
5652                                                         !< to its reverse memory access
5653
5654       REAL(wp), DIMENSION(:,:,:), INTENT(INOUT) ::  var !< variable to be read
5655#if defined( __netcdf )
5656
5657!
5658!--    Inquire variable id
5659       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var ) 
5660!
5661!--    Check for collective read-operation and set respective NetCDF flags if
5662!--    required.
5663       IF ( collective_read )  THEN
5664#if defined( __netcdf4_parallel )
5665          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
5666#endif
5667       ENDIF
5668!
5669!--    Allocate temporary variable according to memory access on file.
5670       ALLOCATE( tmp(is:ie,js:je,ks:ke) )
5671!
5672!--    Get variable
5673       nc_stat = NF90_GET_VAR( id, id_var, tmp,                                &
5674                               start = (/ is+1,    js+1,    ks+1 /),           &
5675                               count = (/ ie-is+1, je-js+1, ke-ks+1 /) )   
5676
5677       CALL handle_error( 'get_variable_3d_real', 534, variable_name )
5678!
5679!--    Resort data. Please note, dimension subscripts of var all start at 1.
5680       DO  i = is, ie 
5681          DO  j = js, je
5682             DO  k = ks, ke
5683                var(k-ks+1,j-js+1,i-is+1) = tmp(i,j,k)
5684             ENDDO
5685          ENDDO
5686       ENDDO
5687
5688       DEALLOCATE( tmp )
5689
5690#endif
5691    END SUBROUTINE get_variable_3d_real
5692
5693!------------------------------------------------------------------------------!
5694! Description:
5695! ------------
5696!> Reads a 4D float variable from file.
5697!------------------------------------------------------------------------------!
5698    SUBROUTINE get_variable_4d_real( id, variable_name, var, is, ie, js, je,   &
5699                                     k1s, k1e, k2s, k2e )
5700
5701       USE indices
5702       USE pegrid
5703
5704       IMPLICIT NONE
5705
5706       CHARACTER(LEN=*)              ::  variable_name   !< variable name
5707
5708       INTEGER(iwp)                  ::  i               !< index along x direction
5709       INTEGER(iwp)                  ::  ie              !< start index for subdomain input along x direction
5710       INTEGER(iwp)                  ::  is              !< end index for subdomain input along x direction
5711       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
5712       INTEGER(iwp)                  ::  id_var          !< variable id
5713       INTEGER(iwp)                  ::  j               !< index along y direction
5714       INTEGER(iwp)                  ::  je              !< start index for subdomain input along y direction
5715       INTEGER(iwp)                  ::  js              !< end index for subdomain input along y direction
5716       INTEGER(iwp)                  ::  k1              !< index along 3rd direction
5717       INTEGER(iwp)                  ::  k1e             !< start index for 3rd dimension
5718       INTEGER(iwp)                  ::  k1s             !< end index for 3rd dimension
5719       INTEGER(iwp)                  ::  k2              !< index along 4th direction
5720       INTEGER(iwp)                  ::  k2e             !< start index for 4th dimension
5721       INTEGER(iwp)                  ::  k2s             !< end index for 4th dimension
5722
5723       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE   ::  tmp  !< temporary variable to read data from file according
5724                                                            !< to its reverse memory access
5725       REAL(wp), DIMENSION(:,:,:,:), INTENT(INOUT) ::  var  !< variable to be read
5726#if defined( __netcdf )
5727
5728!
5729!--    Inquire variable id
5730       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5731!
5732!--    Check for collective read-operation and set respective NetCDF flags if
5733!--    required.
5734       IF ( collective_read )  THEN
5735#if defined( __netcdf4_parallel )       
5736          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
5737#endif
5738       ENDIF
5739
5740      !Temporary solution for reading emission chemistry files:
5741       IF ( id==id_emis ) THEN
5742
5743          !--    Allocate temporary variable according to memory access on file.
5744          ALLOCATE( tmp(is:ie,js:je,k1s:k1e,k2s:k2e) )
5745
5746          !--    Get variable
5747          nc_stat = NF90_GET_VAR( id, id_var, tmp,                                &
5748                                  start = (/ is,   js,   k1s+1,   k2s+1 /),                  &
5749                                  count = (/ ie-is+1 , je-js+1, k1e-k1s+1, k2e-k2s+1 /) ) 
5750
5751          var=tmp
5752
5753          CALL handle_error( 'get_variable_4d_real', 535, variable_name )
5754 
5755          DEALLOCATE( tmp )
5756
5757       !> Original subroutine part
5758       ELSE
5759!
5760!--       Allocate temporary variable according to memory access on file.
5761          ALLOCATE( tmp(is:ie,js:je,k1s:k1e,k2s:k2e) )
5762!
5763!--       Get variable
5764          nc_stat = NF90_GET_VAR( id, id_var, tmp,                             &
5765                               start = (/ is+1,    js+1,   k1s+1, k2s+1 /),    &
5766                               count = (/ ie-is+1, je-js+1,                    &
5767                                          k1e-k1s+1, k2e-k2s+1 /) )
5768
5769          CALL handle_error( 'get_variable_4d_real', 535, variable_name )
5770!
5771!--       Resort data. Please note, dimension subscripts of var all start at 1.
5772          DO  i = is, ie 
5773             DO  j = js, je
5774                DO  k1 = k1s, k1e
5775                   DO  k2 = k2s, k2e
5776                      var(k2-k2s+1,k1-k1s+1,j-js+1,i-is+1) = tmp(i,j,k1,k2)
5777                   ENDDO
5778                ENDDO
5779             ENDDO
5780          ENDDO
5781
5782          DEALLOCATE( tmp )
5783       ENDIF
5784#endif
5785    END SUBROUTINE get_variable_4d_real
5786
5787!------------------------------------------------------------------------------!
5788! Description:
5789! ------------
5790!> Reads a 4D float variable from file and store it to a 3-d variable.
5791!------------------------------------------------------------------------------!
5792    SUBROUTINE get_variable_4d_to_3d_real( id, variable_name, var, ns, is, ie, js, je,   &
5793                                           ks, ke )
5794
5795       USE indices
5796       USE pegrid
5797
5798       IMPLICIT NONE
5799
5800       CHARACTER(LEN=*)              ::  variable_name   !< variable name
5801
5802       INTEGER(iwp)                  ::  ns              !< start index for subdomain input along n dimension: ns coincides here with ne, since, we select only one value along the 1st dimension n
5803
5804       INTEGER(iwp)                  ::  i               !< index along x direction
5805       INTEGER(iwp)                  ::  ie              !< end index for subdomain input along x direction
5806       INTEGER(iwp)                  ::  is              !< start index for subdomain input along x direction
5807       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
5808       INTEGER(iwp)                  ::  id_var          !< variable id
5809       INTEGER(iwp)                  ::  j               !< index along y direction
5810       INTEGER(iwp)                  ::  je              !< end index for subdomain input along y direction
5811       INTEGER(iwp)                  ::  js              !< start index for subdomain input along y direction
5812       INTEGER(iwp)                  ::  k               !< index along any 4th dimension
5813       INTEGER(iwp)                  ::  ke              !< end index of 4th dimension
5814       INTEGER(iwp)                  ::  ks              !< start index of 4th dimension
5815       
5816       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE   ::  tmp !< temporary variable to read data from file according
5817                                                         !< to its reverse memory access
5818
5819       REAL(wp), DIMENSION(:,:,:), INTENT(INOUT) ::  var  !< variable where the read data have to be stored: one dimension is reduced in the process
5820#if defined( __netcdf )
5821
5822!
5823!--    Inquire variable id
5824       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var ) 
5825!
5826!--    Check for collective read-operation and set respective NetCDF flags if
5827!--    required.
5828       IF ( collective_read )  THEN
5829          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
5830       ENDIF
5831
5832      !Temporary solution for reading emission chemistry files:
5833       IF ( id==id_emis ) THEN
5834
5835          !--    Allocate temporary variable according to memory access on file.
5836          ALLOCATE( tmp(is:ie,js:je,ks:ke) )
5837
5838          !--    Get variable
5839          nc_stat = NF90_GET_VAR( id, id_var, tmp(is:ie,js:je,ks:ke),                                &
5840                                  start = (/ ns, is,   js+1,   ks+1 /),                  &
5841                                  count = (/ 1, ie-is+1 , je-js+1, ke-ks+1 /) ) 
5842
5843          var=tmp(:,:,:)
5844
5845          CALL handle_error( 'get_variable_4d_to_3d_real', 536, variable_name )
5846 
5847          DEALLOCATE( tmp )
5848
5849       ELSE
5850!
5851!--       Allocate temporary variable according to memory access on file.
5852          ALLOCATE( tmp(is:ie,js:je,ks:ke) )
5853!
5854!--       Get variable
5855          nc_stat = NF90_GET_VAR( id, id_var, tmp,                             &
5856                                  start = (/ is+1,    js+1,    ks+1,   ns+1 /),&
5857                                  count = (/ ie-is+1, je-js+1, ke-ks+1, 1   /) )
5858
5859          CALL handle_error( 'get_variable_4d_to_3d_real', 536, variable_name )
5860!
5861!--       Resort data. Please note, dimension subscripts of var all start at 1.
5862          DO  i = is, ie
5863             DO  j = js, je
5864                DO  k = ks, ke
5865                   var(k-ks+1,j-js+1,i-is+1) = tmp(i,j,k)
5866                ENDDO
5867             ENDDO
5868          ENDDO
5869
5870         DEALLOCATE( tmp )
5871
5872       ENDIF
5873#endif
5874    END SUBROUTINE get_variable_4d_to_3d_real
5875
5876!------------------------------------------------------------------------------!
5877! Description:
5878! ------------
5879!> Reads a 3D float variables from dynamic driver, such as time-dependent xy-,
5880!> xz- or yz-boundary data as well as 3D initialization data. Please note,
5881!> the passed arguments are start indices and number of elements in each
5882!> dimension, which is in contrast to the other 3d versions where start- and
5883!> end indices are passed. The different handling of 3D dynamic variables is
5884!> due to its asymmetry for the u- and v component.
5885!------------------------------------------------------------------------------!
5886    SUBROUTINE get_variable_3d_real_dynamic( id, variable_name, var,           &
5887                                             i1s, i2s, i3s,                    &
5888                                             count_1, count_2, count_3,        &
5889                                             par_access )
5890                               
5891       USE indices
5892       USE pegrid
5893
5894       IMPLICIT NONE
5895
5896       CHARACTER(LEN=*)              ::  variable_name   !< variable name
5897
5898       LOGICAL                       ::  par_access      !< additional flag indicating whether parallel read operations should be performed or not
5899       
5900       INTEGER(iwp)                  ::  count_1         !< number of elements to be read along 1st dimension (with respect to file)
5901       INTEGER(iwp)                  ::  count_2         !< number of elements to be read along 2nd dimension (with respect to file)
5902       INTEGER(iwp)                  ::  count_3         !< number of elements to be read along 3rd dimension (with respect to file)
5903       INTEGER(iwp)                  ::  i1              !< running index along 1st dimension on file
5904       INTEGER(iwp)                  ::  i1s             !< start index for subdomain input along 1st dimension (with respect to file)
5905       INTEGER(iwp)                  ::  i2              !< running index along 2nd dimension on file       
5906       INTEGER(iwp)                  ::  i2s             !< start index for subdomain input along 2nd dimension (with respect to file)
5907       INTEGER(iwp)                  ::  i3              !< running index along 3rd dimension on file
5908       INTEGER(iwp)                  ::  i3s             !< start index of 3rd dimension, in dynamic file this is either time (2D boundary) or z (3D)
5909       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
5910       INTEGER(iwp)                  ::  id_var          !< variable id
5911       INTEGER(iwp)                  ::  lb1             !< lower bound of 1st dimension (with respect to file)
5912       INTEGER(iwp)                  ::  lb2             !< lower bound of 2nd dimension (with respect to file)
5913       INTEGER(iwp)                  ::  lb3             !< lower bound of 3rd dimension (with respect to file)
5914       INTEGER(iwp)                  ::  ub1             !< upper bound of 1st dimension (with respect to file)
5915       INTEGER(iwp)                  ::  ub2             !< upper bound of 2nd dimension (with respect to file)
5916       INTEGER(iwp)                  ::  ub3             !< upper bound of 3rd dimension (with respect to file)
5917
5918       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE   ::  tmp !< temporary variable to read data from file according
5919                                                         !< to its reverse memory access
5920       
5921       REAL(wp), DIMENSION(:,:,:), INTENT(INOUT) ::  var !< input variable
5922       
5923#if defined( __netcdf )
5924!
5925!--    Inquire variable id.
5926       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5927!
5928!--    Check for collective read-operation and set respective NetCDF flags if
5929!--    required.
5930!--    Please note, in contrast to the other input routines where each PEs
5931!--    reads its subdomain data, dynamic input data not by all PEs, only
5932!--    by those which encompass lateral model boundaries. Hence, collective
5933!--    read operations are only enabled for top-boundary data.
5934       IF ( collective_read  .AND.  par_access )  THEN
5935#if defined( __netcdf4_parallel )       
5936          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
5937#endif
5938       ENDIF   
5939!
5940!--    Allocate temporary variable according to memory access on file.
5941!--    Therefore, determine dimension bounds of input array.
5942       lb1 = LBOUND(var,3)
5943       ub1 = UBOUND(var,3)
5944       lb2 = LBOUND(var,2)
5945       ub2 = UBOUND(var,2)
5946       lb3 = LBOUND(var,1)
5947       ub3 = UBOUND(var,1)
5948       ALLOCATE( tmp(lb1:ub1,lb2:ub2,lb3:ub3) )
5949!
5950!--    Get variable
5951       nc_stat = NF90_GET_VAR( id, id_var, tmp,                                &
5952                               start = (/ i1s,     i2s,     i3s /),            &
5953                               count = (/ count_1, count_2, count_3 /) )
5954
5955       CALL handle_error( 'get_variable_3d_real_dynamic', 537, variable_name )
5956!
5957!--    Resort data. Please note, dimension subscripts of var all start at 1.
5958       DO  i3 = lb3, ub3
5959          DO i2 = lb2, ub2
5960             DO  i1 = lb1, ub1
5961                var(i3,i2,i1) = tmp(i1,i2,i3)
5962             ENDDO
5963          ENDDO
5964       ENDDO
5965       
5966       DEALLOCATE( tmp )       
5967#endif
5968    END SUBROUTINE get_variable_3d_real_dynamic
5969
5970!------------------------------------------------------------------------------!
5971! Description:
5972! ------------
5973!> Reads a 5D float variable from file and store it to a 4-d variable.
5974!------------------------------------------------------------------------------!
5975    SUBROUTINE get_variable_5d_to_4d_real( id, variable_name, var,              &
5976                                           ns, ts, te, is, ie, js, je, ks, ke )
5977
5978       USE indices
5979       USE pegrid
5980
5981       IMPLICIT NONE
5982
5983       CHARACTER(LEN=*)              ::  variable_name   !< variable name
5984
5985       INTEGER(iwp)                  ::  ns              !< start index for subdomain input along n dimension:
5986                                                         !< ns coincides here with ne, since, we select only one
5987                                                         !< value along the 1st dimension n
5988
5989       INTEGER(iwp)                  ::  t               !< index along t direction
5990       INTEGER(iwp)                  ::  te              !< end index for subdomain input along t direction
5991       INTEGER(iwp)                  ::  ts              !< start index for subdomain input along t direction
5992
5993       INTEGER(iwp)                  ::  i               !< index along x direction
5994       INTEGER(iwp)                  ::  ie              !< end index for subdomain input along x direction
5995       INTEGER(iwp)                  ::  is              !< start index for subdomain input along x direction
5996       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
5997       INTEGER(iwp)                  ::  id_var          !< variable id
5998       INTEGER(iwp)                  ::  j               !< index along y direction
5999       INTEGER(iwp)                  ::  je              !< end index for subdomain input along y direction
6000       INTEGER(iwp)                  ::  js              !< start index for subdomain input along y direction
6001       INTEGER(iwp)                  ::  k               !< index along any 5th dimension
6002       INTEGER(iwp)                  ::  ke              !< end index of 5th dimension
6003       INTEGER(iwp)                  ::  ks              !< start index of 5th dimension
6004       
6005       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE   ::  tmp !< temporary variable to read data from file according
6006                                                           ! to its reverse memory access
6007       REAL(wp), DIMENSION(:,:,:,:), INTENT(INOUT) ::  var !< variable to be read
6008#if defined( __netcdf )
6009!
6010!--    Inquire variable id
6011       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
6012!
6013!--    Check for collective read-operation and set respective NetCDF flags if
6014!--    required.
6015       IF ( collective_read )  THEN
6016          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
6017       ENDIF
6018
6019      !Temporary solution for reading emission chemistry files:
6020       IF ( id==id_emis ) THEN
6021
6022          !--    Allocate temporary variable according to memory access on file.
6023          ALLOCATE( tmp(ts:te,1,js+1:je+1,ks+1:ke+1) )
6024
6025          !--    Get variable
6026          nc_stat = NF90_GET_VAR( id, id_var, tmp(ts:te,1,js+1:je+1,ks+1:ke+1),               &
6027                                  start = (/ ns, ts,  1,   js+1,   ks+1 /),                  &
6028                                  count = (/ 1, te-ts+1, 1, je-js+1, ke-ks+1 /) ) 
6029
6030          var=tmp
6031
6032          CALL handle_error( 'get_variable_5d_to_4d_real', 538, variable_name )
6033 
6034          DEALLOCATE( tmp )
6035
6036       !>  Original Subroutine part
6037       ELSE
6038!
6039!--    Allocate temporary variable according to memory access on file.
6040          ALLOCATE( tmp(ks:ke,js:je,is:is,ts:te) )
6041!
6042!--    Get variable
6043          nc_stat = NF90_GET_VAR( id, id_var, tmp,                                &
6044                                  start = (/ ks+1, js+1, is+1, ts+1, ns /),           &
6045                                  count = (/ ke-ks+1, je-js+1, ie-is+1, te-ts+1, 1 /) )   
6046                               
6047          CALL handle_error( 'get_variable_5d_to_4d_real', 538, variable_name )
6048!
6049!--    Resort data. Please note, dimension subscripts of var all start at 1.
6050
6051          DO  t = ts, te 
6052             DO  i = is, ie 
6053                DO  j = js, je
6054                   DO  k = ks, ke
6055                      var(t-ts+1,i-is+1,j-js+1,k-ks+1) = tmp(k,j,i,t)
6056                   ENDDO
6057                ENDDO
6058             ENDDO
6059          ENDDO 
6060
6061          DEALLOCATE( tmp )
6062
6063       ENDIF
6064#endif
6065    END SUBROUTINE get_variable_5d_to_4d_real
6066
6067
6068!------------------------------------------------------------------------------!
6069! Description:
6070! ------------
6071!> Inquires the number of variables in a file
6072!------------------------------------------------------------------------------!
6073    SUBROUTINE inquire_num_variables( id, num_vars )
6074
6075       USE indices
6076       USE pegrid
6077
6078       IMPLICIT NONE
6079
6080       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
6081       INTEGER(iwp), INTENT(INOUT)   ::  num_vars        !< number of variables in a file
6082#if defined( __netcdf )
6083
6084       nc_stat = NF90_INQUIRE( id, NVARIABLES = num_vars )
6085       CALL handle_error( 'inquire_num_variables', 539 )
6086
6087#endif
6088    END SUBROUTINE inquire_num_variables
6089
6090
6091!------------------------------------------------------------------------------!
6092! Description:
6093! ------------
6094!> Inquires the variable names belonging to a file.
6095!------------------------------------------------------------------------------!
6096    SUBROUTINE inquire_variable_names( id, var_names )
6097
6098       USE indices
6099       USE pegrid
6100
6101       IMPLICIT NONE
6102
6103       CHARACTER(LEN=*), DIMENSION(:), INTENT(INOUT) ::  var_names   !< return variable - variable names
6104       INTEGER(iwp)                                  ::  i           !< loop variable
6105       INTEGER(iwp), INTENT(IN)                      ::  id          !< file id
6106       INTEGER(iwp)                                  ::  num_vars    !< number of variables (unused return parameter)
6107       INTEGER(iwp), DIMENSION(:), ALLOCATABLE       ::  varids      !< dummy array to strore variable ids temporarily
6108#if defined( __netcdf )
6109
6110       ALLOCATE( varids(1:SIZE(var_names)) )
6111       nc_stat = NF90_INQ_VARIDS( id, NVARS = num_vars, VARIDS = varids )
6112       CALL handle_error( 'inquire_variable_names', 540 )
6113
6114       DO  i = 1, SIZE(var_names)
6115          nc_stat = NF90_INQUIRE_VARIABLE( id, varids(i), NAME = var_names(i) )
6116          CALL handle_error( 'inquire_variable_names', 540 )
6117       ENDDO
6118
6119       DEALLOCATE( varids )
6120#endif
6121    END SUBROUTINE inquire_variable_names
6122
6123!------------------------------------------------------------------------------!
6124! Description:
6125! ------------
6126!> Prints out a text message corresponding to the current status.
6127!------------------------------------------------------------------------------!
6128    SUBROUTINE handle_error( routine_name, errno, name )
6129
6130       USE control_parameters,                                                 &
6131           ONLY:  message_string
6132
6133       IMPLICIT NONE
6134
6135       CHARACTER(LEN=6) ::  message_identifier !< string for the error number
6136       CHARACTER(LEN=*) ::  routine_name       !< routine name where the error happened
6137       CHARACTER(LEN=*), OPTIONAL ::  name     !< name of variable where reading failed
6138
6139       INTEGER(iwp) ::  errno
6140#if defined( __netcdf )
6141       
6142       IF ( nc_stat /= NF90_NOERR )  THEN
6143
6144          WRITE( message_identifier, '(''NC'',I4.4)' )  errno
6145         
6146          IF ( PRESENT( name ) )  THEN
6147             message_string = "Problem reading attribute/variable - " //       &
6148                              TRIM(name) // ": " //                            &
6149                              TRIM( NF90_STRERROR( nc_stat ) )
6150          ELSE
6151             message_string = TRIM( NF90_STRERROR( nc_stat ) )
6152          ENDIF
6153
6154          CALL message( routine_name, message_identifier, 2, 2, myid, 6, 1 )
6155
6156       ENDIF
6157
6158#endif
6159    END SUBROUTINE handle_error
6160
6161
6162 END MODULE netcdf_data_input_mod
Note: See TracBrowser for help on using the repository browser.