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

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

Bugfix in one of the checks

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