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

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

Offline nesting revised and separated from large_scale_forcing_mod; Time-dependent synthetic turbulence generator; bugfixes in USM/LSM radiation model and init_pegrid

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