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

Last change on this file since 3483 was 3483, checked in by raasch, 7 years ago

bugfix: misplaced positions of cpp-directives for netCDF and MPI fixed; output format limited to a maximum line length of 80

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