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

Last change on this file since 3498 was 3498, checked in by gronemeier, 7 years ago

Bugfix: print error message by processor which encounters the error

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