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

Last change on this file since 4012 was 4012, checked in by monakurppa, 6 years ago

remove salsa_util_mod.f90 and correct some bugs in salsa and salsa test case

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