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

Last change on this file since 3602 was 3560, checked in by raasch, 6 years ago

bugfix to guarantee correct particle releases in case that the release interval is smaller than the model timestep

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