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

Last change on this file since 3417 was 3404, checked in by suehring, 7 years ago

Consider time-dependent geostrophic wind components in offline nesting

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