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

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

restart mechanism for surface elements commented, some formatting

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