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

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

Coupling of indoor model to atmosphere; output of indoor temperatures and waste heat; enable restarts with indoor model; bugfix plant transpiration; bugfix - missing calculation of 10cm temperature

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