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

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

Revision of virtual-measurement module and data output enabled. Further, post-processing tool added to merge distributed virtually sampled data and to output it into NetCDF files.

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