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

Last change on this file since 4226 was 4226, checked in by suehring, 4 years ago

Offline nesting: data input modularized; time variable is defined relative to time_utc_init, so that input data is correctly mapped to actual model time; checks rephrased and new checks for the time dimension added; Netcdf input: routine to retrieve dimension length renamed

  • Property svn:keywords set to Id
File size: 264.6 KB
Line 
1!> @file netcdf_data_input_mod.f90
2!------------------------------------------------------------------------------!
3! This file is part of the PALM model system.
4!
5! PALM is free software: you can redistribute it and/or modify it under the
6! terms of the GNU General Public License as published by the Free Software
7! Foundation, either version 3 of the License, or (at your option) any later
8! version.
9!
10! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13!
14! You should have received a copy of the GNU General Public License along with
15! PALM. If not, see <http://www.gnu.org/licenses/>.
16!
17! Copyright 1997-2019 Leibniz Universitaet Hannover
18!------------------------------------------------------------------------------!
19!
20! Current revisions:
21! -----------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: netcdf_data_input_mod.f90 4226 2019-09-10 17:03:24Z suehring $
27! - Netcdf input routine for dimension length renamed
28! - Move offline-nesting-specific checks to nesting_offl_mod
29! - Module-specific input of boundary data for offline nesting moved to
30!   nesting_offl_mod
31! - Define module specific data type for offline nesting in nesting_offl_mod
32!
33! 4190 2019-08-27 15:42:37Z suehring
34! type real_1d changed to real_1d_3d
35!
36! 4186 2019-08-23 16:06:14Z suehring
37! Minor formatting adjustments
38!
39! 4182 2019-08-22 15:20:23Z scharf
40! Corrected "Former revisions" section
41!
42! 4178 2019-08-21 11:13:06Z suehring
43! Implement input of external radiation forcing. Therefore, provide public
44! subroutines and variables.
45!
46! 4150 2019-08-08 20:00:47Z suehring
47! Some variables are given the public attribute, in order to call netcdf input
48! from single routines
49!
50! 4125 2019-07-29 13:31:44Z suehring
51! To enable netcdf-parallel access for lateral boundary data (dynamic input),
52! zero number of elements are passed to the respective get_variable routine
53! for non-boundary cores.
54!
55! 4100 2019-07-17 08:11:29Z forkel
56! Made check for input_pids_dynamic and 'inifor' more general
57!
58! 4012 2019-05-31 15:19:05Z monakurppa
59!
60! 3994 2019-05-22 18:08:09Z suehring
61! Remove single location message
62!
63! 3976 2019-05-15 11:02:34Z hellstea
64! Remove unused variables from last commit
65!
66! 3969 2019-05-13 12:14:33Z suehring
67! - clean-up index notations for emission_values to eliminate magic numbers
68! - introduce temporary variable dum_var_5d as well as subroutines
69!   get_var_5d_real and get_var_5d_real_dynamic
70! - remove emission-specific code in generic get_variable routines
71! - in subroutine netcdf_data_input_chemistry_data change netCDF LOD 1
72!   (default) emission_values to the following index order:
73!   z, y, x, species, category
74! - in subroutine netcdf_data_input_chemistry_data
75!   changed netCDF LOD 2 pre-processed emission_values to the following index
76!   order: time, z, y, x, species
77! - in type chem_emis_att_type replace nspec with n_emiss_species
78!   but retained nspec for backward compatibility with salsa_mod. (E.C. Chan)
79!
80! 3961 2019-05-08 16:12:31Z suehring
81! Revise checks for building IDs and types
82!
83! 3943 2019-05-02 09:50:41Z maronga
84! Temporarily disabled some (faulty) checks for static driver.
85!
86! 3942 2019-04-30 13:08:30Z kanani
87! Fix: increase LEN of all NetCDF attribute values (caused crash in
88! netcdf_create_global_atts due to insufficient length)
89!
90! 3941 2019-04-30 09:48:33Z suehring
91! Move check for grid dimension to an earlier point in time when first array
92! is read.
93! Improve checks for building types / IDs with respect to 2D/3D buildings.
94!
95! 3885 2019-04-11 11:29:34Z kanani
96! Changes related to global restructuring of location messages and introduction
97! of additional debug messages
98!
99! 3864 2019-04-05 09:01:56Z monakurppa
100! get_variable_4d_to_3d_real modified to enable read in data of type
101! data(t,y,x,n) one timestep at a time + some routines made public
102!
103! 3855 2019-04-03 10:00:59Z suehring
104! Typo removed
105!
106! 3854 2019-04-02 16:59:33Z suehring
107! Bugfix in one of the checks. Typo removed.
108!
109! 3744 2019-02-15 18:38:58Z suehring
110! Enable mesoscale offline nesting for chemistry variables as well as
111! initialization of chemistry via dynamic input file.
112!
113! 3705 2019-01-29 19:56:39Z suehring
114! Interface for attribute input of 8-bit and 32-bit integer
115!
116! 3704 2019-01-29 19:51:41Z suehring
117! unused variables removed
118!
119! 2696 2017-12-14 17:12:51Z kanani
120! Initial revision (suehring)
121!
122! Authors:
123! --------
124! @author Matthias Suehring
125! @author Edward C. Chan
126! @author Emanuele Russo
127!
128! Description:
129! ------------
130!> Modulue contains routines to input data according to Palm input data
131!> standart using dynamic and static input files.
132!> @todo - Chemistry: revise reading of netcdf file and ajdust formatting
133!>         according to standard!!! (ecc/done)
134!> @todo - Order input alphabetically
135!> @todo - Revise error messages and error numbers
136!> @todo - Input of missing quantities (chemical species, emission rates)
137!> @todo - Defninition and input of still missing variable attributes
138!>         (ecc/what are they?)
139!> @todo - Input of initial geostrophic wind profiles with cyclic conditions.
140!> @todo - remove z dimension from default_emission_data nad preproc_emission_data
141!          and correpsonding subroutines get_var_5d_real and get_var_5d_dynamic (ecc)
142!> @todo - decpreciate chem_emis_att_type@nspec (ecc)
143!> @todo - depreciate subroutines get_variable_4d_to_3d_real and
144!>         get_variable_5d_to_4d_real (ecc)
145!> @todo - introduce useful debug_message(s)
146!------------------------------------------------------------------------------!
147 MODULE netcdf_data_input_mod
148
149    USE control_parameters,                                                    &
150        ONLY:  coupling_char, io_blocks, io_group
151
152    USE cpulog,                                                                &
153        ONLY:  cpu_log, log_point_s
154
155    USE indices,                                                               &
156        ONLY:  nbgp
157
158    USE kinds
159
160#if defined ( __netcdf )
161    USE NETCDF
162#endif
163
164    USE pegrid
165
166    USE surface_mod,                                                           &
167        ONLY:  ind_pav_green, ind_veg_wall, ind_wat_win
168!
169!-- Define type for dimensions.
170    TYPE dims_xy
171       INTEGER(iwp) :: nx                             !< dimension length in x
172       INTEGER(iwp) :: ny                             !< dimension length in y
173       INTEGER(iwp) :: nz                             !< dimension length in z
174       REAL(wp), DIMENSION(:), ALLOCATABLE :: x       !< dimension array in x
175       REAL(wp), DIMENSION(:), ALLOCATABLE :: y       !< dimension array in y
176       REAL(wp), DIMENSION(:), ALLOCATABLE :: z       !< dimension array in z
177    END TYPE dims_xy
178    TYPE init_type
179
180       CHARACTER(LEN=16) ::  init_char = 'init_atmosphere_'          !< leading substring for init variables
181       CHARACTER(LEN=23) ::  origin_time = '2000-01-01 00:00:00 +00' !< reference time of input data
182       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names_chem !< list of chemistry variable names that can potentially be on file
183
184       INTEGER(iwp) ::  lod_msoil !< level of detail - soil moisture
185       INTEGER(iwp) ::  lod_pt    !< level of detail - pt
186       INTEGER(iwp) ::  lod_q     !< level of detail - q
187       INTEGER(iwp) ::  lod_tsoil !< level of detail - soil temperature
188       INTEGER(iwp) ::  lod_u     !< level of detail - u-component
189       INTEGER(iwp) ::  lod_v     !< level of detail - v-component
190       INTEGER(iwp) ::  lod_w     !< level of detail - w-component
191       INTEGER(iwp) ::  nx        !< number of scalar grid points along x in dynamic input file
192       INTEGER(iwp) ::  nxu       !< number of u grid points along x in dynamic input file
193       INTEGER(iwp) ::  ny        !< number of scalar grid points along y in dynamic input file
194       INTEGER(iwp) ::  nyv       !< number of v grid points along y in dynamic input file
195       INTEGER(iwp) ::  nzs       !< number of vertical soil levels in dynamic input file
196       INTEGER(iwp) ::  nzu       !< number of vertical levels on scalar grid in dynamic input file
197       INTEGER(iwp) ::  nzw       !< number of vertical levels on w grid in dynamic input file
198       
199       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  lod_chem !< level of detail - chemistry variables
200
201       LOGICAL ::  from_file_msoil  = .FALSE. !< flag indicating whether soil moisture is already initialized from file
202       LOGICAL ::  from_file_pt     = .FALSE. !< flag indicating whether pt is already initialized from file
203       LOGICAL ::  from_file_q      = .FALSE. !< flag indicating whether q is already initialized from file
204       LOGICAL ::  from_file_tsoil  = .FALSE. !< flag indicating whether soil temperature is already initialized from file
205       LOGICAL ::  from_file_u      = .FALSE. !< flag indicating whether u is already initialized from file
206       LOGICAL ::  from_file_ug     = .FALSE. !< flag indicating whether ug is already initialized from file
207       LOGICAL ::  from_file_v      = .FALSE. !< flag indicating whether v is already initialized from file
208       LOGICAL ::  from_file_vg     = .FALSE. !< flag indicating whether ug is already initialized from file
209       LOGICAL ::  from_file_w      = .FALSE. !< flag indicating whether w is already initialized from file
210       
211       LOGICAL, DIMENSION(:), ALLOCATABLE ::  from_file_chem !< flag indicating whether chemistry variable is read from file
212
213       REAL(wp) ::  fill_msoil              !< fill value for soil moisture
214       REAL(wp) ::  fill_pt                 !< fill value for pt
215       REAL(wp) ::  fill_q                  !< fill value for q
216       REAL(wp) ::  fill_tsoil              !< fill value for soil temperature
217       REAL(wp) ::  fill_u                  !< fill value for u
218       REAL(wp) ::  fill_v                  !< fill value for v
219       REAL(wp) ::  fill_w                  !< fill value for w
220       REAL(wp) ::  latitude = 0.0_wp       !< latitude of the lower left corner
221       REAL(wp) ::  longitude = 0.0_wp      !< longitude of the lower left corner
222       REAL(wp) ::  origin_x = 500000.0_wp  !< UTM easting of the lower left corner
223       REAL(wp) ::  origin_y = 0.0_wp       !< UTM northing of the lower left corner
224       REAL(wp) ::  origin_z = 0.0_wp       !< reference height of input data
225       REAL(wp) ::  rotation_angle = 0.0_wp !< rotation angle of input data
226
227       REAL(wp), DIMENSION(:), ALLOCATABLE ::  fill_chem    !< fill value - chemistry variables
228       REAL(wp), DIMENSION(:), ALLOCATABLE ::  msoil_1d     !< initial vertical profile of soil moisture
229       REAL(wp), DIMENSION(:), ALLOCATABLE ::  pt_init      !< initial vertical profile of pt
230       REAL(wp), DIMENSION(:), ALLOCATABLE ::  q_init       !< initial vertical profile of q
231       REAL(wp), DIMENSION(:), ALLOCATABLE ::  tsoil_1d     !< initial vertical profile of soil temperature
232       REAL(wp), DIMENSION(:), ALLOCATABLE ::  u_init       !< initial vertical profile of u
233       REAL(wp), DIMENSION(:), ALLOCATABLE ::  ug_init      !< initial vertical profile of ug
234       REAL(wp), DIMENSION(:), ALLOCATABLE ::  v_init       !< initial vertical profile of v
235       REAL(wp), DIMENSION(:), ALLOCATABLE ::  vg_init      !< initial vertical profile of ug
236       REAL(wp), DIMENSION(:), ALLOCATABLE ::  w_init       !< initial vertical profile of w
237       REAL(wp), DIMENSION(:), ALLOCATABLE ::  z_soil       !< vertical levels in soil in dynamic input file, used for interpolation
238       REAL(wp), DIMENSION(:), ALLOCATABLE ::  zu_atmos     !< vertical levels at scalar grid in dynamic input file, used for interpolation
239       REAL(wp), DIMENSION(:), ALLOCATABLE ::  zw_atmos     !< vertical levels at w grid in dynamic input file, used for interpolation
240       
241       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  chem_init  !< initial vertical profiles of chemistry variables
242
243
244       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  msoil_3d !< initial 3d soil moisture provide by Inifor and interpolated onto soil grid
245       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  tsoil_3d !< initial 3d soil temperature provide by Inifor and interpolated onto soil grid
246
247    END TYPE init_type
248
249!-- Data type for the general information of chemistry emissions, do not dependent on the particular chemical species
250    TYPE chem_emis_att_type 
251
252       !-DIMENSIONS
253       
254       INTEGER(iwp)                                 :: nspec=0            !< no of chem species provided in emission_values
255       INTEGER(iwp)                                 :: n_emiss_species=0  !< no of chem species provided in emission_values
256                                                                          !< same function as nspec, which will be depreciated (ecc)
257                                                                                 
258       INTEGER(iwp)                                 :: ncat=0             !< number of emission categories
259       INTEGER(iwp)                                 :: nvoc=0             !< number of VOC components
260       INTEGER(iwp)                                 :: npm=0              !< number of PM components
261       INTEGER(iwp)                                 :: nnox=2             !< number of NOx components: NO and NO2
262       INTEGER(iwp)                                 :: nsox=2             !< number of SOX components: SO and SO4
263       INTEGER(iwp)                                 :: nhoursyear         !< number of hours of a specific year in the HOURLY mode
264                                                                          !< of the default mode
265       INTEGER(iwp)                                 :: nmonthdayhour      !< number of month days and hours in the MDH mode
266                                                                          !< of the default mode
267       INTEGER(iwp)                                 :: dt_emission        !< Number of emissions timesteps for one year
268                                                                          !< in the pre-processed emissions case
269       !-- 1d emission input variables
270       CHARACTER (LEN=25),ALLOCATABLE, DIMENSION(:) :: pm_name       !< Names of PM components
271       CHARACTER (LEN=25),ALLOCATABLE, DIMENSION(:) :: cat_name      !< Emission category names
272       CHARACTER (LEN=25),ALLOCATABLE, DIMENSION(:) :: species_name  !< Names of emission chemical species
273       CHARACTER (LEN=25),ALLOCATABLE, DIMENSION(:) :: voc_name      !< Names of VOCs components
274       CHARACTER (LEN=25)                           :: units         !< Units
275
276       INTEGER(iwp)                                 :: i_hour         !< indices for assigning emission values at different timesteps
277       INTEGER(iwp),ALLOCATABLE, DIMENSION(:)       :: cat_index      !< Indices for emission categories
278       INTEGER(iwp),ALLOCATABLE, DIMENSION(:)       :: species_index  !< Indices for emission chem species
279
280       REAL(wp),ALLOCATABLE, DIMENSION(:)           :: xm             !< Molecular masses of emission chem species
281
282       !-- 2d emission input variables
283       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: hourly_emis_time_factor  !< Time factors for HOURLY emissions (DEFAULT mode)
284       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: mdh_emis_time_factor     !< Time factors for MDH emissions (DEFAULT mode)
285       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: nox_comp                 !< Composition of NO and NO2
286       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: sox_comp                 !< Composition of SO2 and SO4
287       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: voc_comp                 !< Composition of VOC components (not fixed)
288
289       !-- 3d emission input variables
290       REAL(wp),ALLOCATABLE, DIMENSION(:,:,:)       :: pm_comp                  !< Composition of PM components (not fixed)
291 
292    END TYPE chem_emis_att_type
293
294
295!-- Data type for the values of chemistry emissions
296    TYPE chem_emis_val_type 
297
298       !REAL(wp),ALLOCATABLE, DIMENSION(:,:)     :: stack_height           !< stack height (ecc / to be implemented)
299       REAL(wp),ALLOCATABLE, DIMENSION(:,:,:)    :: default_emission_data  !< Emission input values for LOD1 (DEFAULT mode)
300       REAL(wp),ALLOCATABLE, DIMENSION(:,:,:,:)  :: preproc_emission_data  !< Emission input values for LOD2 (PRE-PROCESSED mode)
301
302    END TYPE chem_emis_val_type
303
304!
305!-- Define data structures for different input data types.
306!-- 8-bit Integer 2D
307    TYPE int_2d_8bit
308       INTEGER(KIND=1) ::  fill = -127                      !< fill value
309       INTEGER(KIND=1), DIMENSION(:,:), ALLOCATABLE ::  var !< respective variable
310
311       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
312    END TYPE int_2d_8bit
313!
314!-- 8-bit Integer 3D
315    TYPE int_3d_8bit
316       INTEGER(KIND=1) ::  fill = -127                           !< fill value
317       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var_3d !< respective variable
318
319       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
320    END TYPE int_3d_8bit
321!
322!-- 32-bit Integer 2D
323    TYPE int_2d_32bit
324       INTEGER(iwp) ::  fill = -9999                      !< fill value
325       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  var  !< respective variable
326
327       LOGICAL ::  from_file = .FALSE. !< flag indicating whether an input variable is available and read from file or default values are used
328    END TYPE int_2d_32bit
329!
330!-- Define data type to read 1D or 3D real variables.
331    TYPE real_1d_3d
332       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
333
334       INTEGER(iwp) ::  lod = -1        !< level-of-detail
335       
336       REAL(wp) ::  fill = -9999.9_wp                  !< fill value
337       
338       REAL(wp), DIMENSION(:),     ALLOCATABLE ::  var1d     !< respective 1D variable
339       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  var3d     !< respective 3D variable
340    END TYPE real_1d_3d   
341!
342!-- Define data type to read 2D real variables
343    TYPE real_2d
344       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
345
346       INTEGER(iwp) ::  lod             !< level-of-detail
347       
348       REAL(wp) ::  fill = -9999.9_wp                !< fill value
349       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  var !< respective variable
350    END TYPE real_2d
351
352!
353!-- Define data type to read 3D real variables
354    TYPE real_3d
355       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
356
357       INTEGER(iwp) ::  nz   !< number of grid points along vertical dimension
358
359       REAL(wp) ::  fill = -9999.9_wp                  !< fill value
360       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  var !< respective variable
361    END TYPE real_3d
362!
363!-- Define data structure where the dimension and type of the input depends
364!-- on the given level of detail.
365!-- For buildings, the input is either 2D float, or 3d byte.
366    TYPE build_in
367       INTEGER(iwp)    ::  lod = 1                               !< level of detail
368       INTEGER(KIND=1) ::  fill2 = -127                          !< fill value for lod = 2
369       INTEGER(iwp)    ::  nz                                    !< number of vertical layers in file
370       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var_3d !< 3d variable (lod = 2)
371
372       REAL(wp), DIMENSION(:), ALLOCATABLE ::  z                 !< vertical coordinate for 3D building, used for consistency check
373
374       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
375
376       REAL(wp)                              ::  fill1 = -9999.9_wp !< fill values for lod = 1
377       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  var_2d             !< 2d variable (lod = 1)
378    END TYPE build_in
379
380!
381!-- For soil_type, the input is either 2D or 3D one-byte integer.
382    TYPE soil_in
383       INTEGER(iwp)                                   ::  lod = 1      !< level of detail
384       INTEGER(KIND=1)                                ::  fill = -127  !< fill value for lod = 2
385       INTEGER(KIND=1), DIMENSION(:,:), ALLOCATABLE   ::  var_2d       !< 2d variable (lod = 1)
386       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var_3d       !< 3d variable (lod = 2)
387
388       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
389    END TYPE soil_in
390
391!
392!-- Define data type for fractions between surface types
393    TYPE fracs
394       INTEGER(iwp)                            ::  nf             !< total number of fractions
395       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nfracs         !< dimension array for fraction
396
397       LOGICAL ::  from_file = .FALSE. !< flag indicating whether an input variable is available and read from file or default values are used
398
399       REAL(wp)                                ::  fill = -9999.9_wp !< fill value
400       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  frac              !< respective fraction between different surface types
401    END TYPE fracs
402!
403!-- Data type for parameter lists, Depending on the given level of detail,
404!-- the input is 3D or 4D
405    TYPE pars
406       INTEGER(iwp)                            ::  lod = 1         !< level of detail
407       INTEGER(iwp)                            ::  np              !< total number of parameters
408       INTEGER(iwp)                            ::  nz              !< vertical dimension - number of soil layers
409       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  layers          !< dimension array for soil layers
410       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  pars            !< dimension array for parameters
411
412       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
413
414       REAL(wp)                                  ::  fill = -9999.9_wp !< fill value
415       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE   ::  pars_xy           !< respective parameters, level of detail = 1
416       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  pars_xyz          !< respective parameters, level of detail = 2
417    END TYPE pars
418!
419!-- Define type for global file attributes
420!-- Please refer to the PALM data standard for a detailed description of each
421!-- attribute.
422    TYPE global_atts_type
423       CHARACTER(LEN=200) ::  acronym = ' '                      !< acronym of institution
424       CHARACTER(LEN=7)   ::  acronym_char = 'acronym'           !< name of attribute
425       CHARACTER(LEN=200) ::  author  = ' '                      !< first name, last name, email adress
426       CHARACTER(LEN=6)   ::  author_char = 'author'             !< name of attribute
427       CHARACTER(LEN=200) ::  campaign = 'PALM-4U'               !< name of campaign
428       CHARACTER(LEN=8)   ::  campaign_char = 'campaign'         !< name of attribute
429       CHARACTER(LEN=200) ::  comment = ' '                      !< comment to data
430       CHARACTER(LEN=7)   ::  comment_char = 'comment'           !< name of attribute
431       CHARACTER(LEN=200) ::  contact_person = ' '               !< first name, last name, email adress
432       CHARACTER(LEN=14)  ::  contact_person_char = 'contact_person'  !< name of attribute
433       CHARACTER(LEN=200) ::  conventions = 'CF-1.7'             !< netCDF convention
434       CHARACTER(LEN=11)  ::  conventions_char = 'Conventions'   !< name of attribute
435       CHARACTER(LEN=23 ) ::  creation_time = ' '                !< creation time of data set
436       CHARACTER(LEN=13)  ::  creation_time_char = 'creation_time'  !< name of attribute
437       CHARACTER(LEN=200) ::  data_content = ' '                 !< content of data set
438       CHARACTER(LEN=12)  ::  data_content_char = 'data_content' !< name of attribute
439       CHARACTER(LEN=200) ::  dependencies = ' '                 !< dependencies of data set
440       CHARACTER(LEN=12)  ::  dependencies_char = 'dependencies' !< name of attribute
441       CHARACTER(LEN=200) ::  history = ' '                      !< information about data processing
442       CHARACTER(LEN=7)   ::  history_char = 'history'           !< name of attribute
443       CHARACTER(LEN=200) ::  institution = ' '                  !< name of responsible institution
444       CHARACTER(LEN=11)  ::  institution_char = 'institution'   !< name of attribute
445       CHARACTER(LEN=200) ::  keywords = ' '                     !< keywords of data set
446       CHARACTER(LEN=8)   ::  keywords_char = 'keywords'         !< name of attribute
447       CHARACTER(LEN=200) ::  licence = ' '                      !< licence of data set
448       CHARACTER(LEN=7)   ::  licence_char = 'licence'           !< name of attribute
449       CHARACTER(LEN=200) ::  location = ' '                     !< place which refers to data set
450       CHARACTER(LEN=8)   ::  location_char = 'location'         !< name of attribute
451       CHARACTER(LEN=10)  ::  origin_lat_char = 'origin_lat'     !< name of attribute
452       CHARACTER(LEN=10)  ::  origin_lon_char = 'origin_lon'     !< name of attribute
453       CHARACTER(LEN=23 ) ::  origin_time = '2000-01-01 00:00:00 +00'  !< reference time
454       CHARACTER(LEN=11)  ::  origin_time_char = 'origin_time'   !< name of attribute
455       CHARACTER(LEN=8)   ::  origin_x_char = 'origin_x'         !< name of attribute
456       CHARACTER(LEN=8)   ::  origin_y_char = 'origin_y'         !< name of attribute
457       CHARACTER(LEN=8)   ::  origin_z_char = 'origin_z'         !< name of attribute
458       CHARACTER(LEN=12)  ::  palm_version_char = 'palm_version' !< name of attribute
459       CHARACTER(LEN=200) ::  references = ' '                   !< literature referring to data set
460       CHARACTER(LEN=10)  ::  references_char = 'references'     !< name of attribute
461       CHARACTER(LEN=14)  ::  rotation_angle_char = 'rotation_angle'  !< name of attribute
462       CHARACTER(LEN=200) ::  site = ' '                         !< name of model domain
463       CHARACTER(LEN=4)   ::  site_char = 'site'                 !< name of attribute
464       CHARACTER(LEN=200) ::  source = ' '                       !< source of data set
465       CHARACTER(LEN=6)   ::  source_char = 'source'             !< name of attribute
466       CHARACTER(LEN=200) ::  title = ' '                        !< title of data set
467       CHARACTER(LEN=5)   ::  title_char = 'title'               !< name of attribute
468       CHARACTER(LEN=7)   ::  version_char = 'version'           !< name of attribute
469
470       INTEGER(iwp) ::  version              !< version of data set
471
472       REAL(wp) ::  origin_lat               !< latitude of lower left corner
473       REAL(wp) ::  origin_lon               !< longitude of lower left corner
474       REAL(wp) ::  origin_x                 !< easting (UTM coordinate) of lower left corner
475       REAL(wp) ::  origin_y                 !< northing (UTM coordinate) of lower left corner
476       REAL(wp) ::  origin_z                 !< reference height
477       REAL(wp) ::  palm_version             !< PALM version of data set
478       REAL(wp) ::  rotation_angle           !< rotation angle of coordinate system of data set
479    END TYPE global_atts_type
480!
481!-- Define type for coordinate reference system (crs)
482    TYPE crs_type
483       CHARACTER(LEN=200) ::  epsg_code = 'EPSG:25831'                   !< EPSG code
484       CHARACTER(LEN=200) ::  grid_mapping_name = 'transverse_mercator'  !< name of grid mapping
485       CHARACTER(LEN=200) ::  long_name = 'coordinate reference system'  !< name of variable crs
486       CHARACTER(LEN=200) ::  units = 'm'                                !< unit of crs
487
488       REAL(wp) ::  false_easting = 500000.0_wp                  !< false easting
489       REAL(wp) ::  false_northing = 0.0_wp                      !< false northing
490       REAL(wp) ::  inverse_flattening = 298.257223563_wp        !< 1/f (default for WGS84)
491       REAL(wp) ::  latitude_of_projection_origin = 0.0_wp       !< latitude of projection origin
492       REAL(wp) ::  longitude_of_central_meridian = 3.0_wp       !< longitude of central meridian of UTM zone (default: zone 31)
493       REAL(wp) ::  longitude_of_prime_meridian = 0.0_wp         !< longitude of prime meridian
494       REAL(wp) ::  scale_factor_at_central_meridian = 0.9996_wp !< scale factor of UTM coordinates
495       REAL(wp) ::  semi_major_axis = 6378137.0_wp               !< length of semi major axis (default for WGS84)
496    END TYPE crs_type
497
498!
499!-- Define variables
500    TYPE(crs_type)   ::  coord_ref_sys  !< coordinate reference system
501
502    TYPE(dims_xy)    ::  dim_static     !< data structure for x, y-dimension in static input file
503
504    TYPE(init_type) ::  init_3d    !< data structure for the initialization of the 3D flow and soil fields
505    TYPE(init_type) ::  init_model !< data structure for the initialization of the model
506
507!
508!-- Define 2D variables of type NC_BYTE
509    TYPE(int_2d_8bit)  ::  albedo_type_f     !< input variable for albedo type
510    TYPE(int_2d_8bit)  ::  building_type_f   !< input variable for building type
511    TYPE(int_2d_8bit)  ::  pavement_type_f   !< input variable for pavenment type
512    TYPE(int_2d_8bit)  ::  street_crossing_f !< input variable for water type
513    TYPE(int_2d_8bit)  ::  street_type_f     !< input variable for water type
514    TYPE(int_2d_8bit)  ::  vegetation_type_f !< input variable for vegetation type
515    TYPE(int_2d_8bit)  ::  water_type_f      !< input variable for water type
516!
517!-- Define 3D variables of type NC_BYTE
518    TYPE(int_3d_8bit)  ::  building_obstruction_f    !< input variable for building obstruction
519    TYPE(int_3d_8bit)  ::  building_obstruction_full !< input variable for building obstruction
520!
521!-- Define 2D variables of type NC_INT
522    TYPE(int_2d_32bit) ::  building_id_f     !< input variable for building ID
523!
524!-- Define 2D variables of type NC_FLOAT
525    TYPE(real_2d) ::  terrain_height_f       !< input variable for terrain height
526    TYPE(real_2d) ::  uvem_irradiance_f      !< input variable for uvem irradiance lookup table
527    TYPE(real_2d) ::  uvem_integration_f     !< input variable for uvem integration
528!
529!-- Define 3D variables of type NC_FLOAT
530    TYPE(real_3d) ::  basal_area_density_f    !< input variable for basal area density - resolved vegetation
531    TYPE(real_3d) ::  leaf_area_density_f     !< input variable for leaf area density - resolved vegetation
532    TYPE(real_3d) ::  root_area_density_lad_f !< input variable for root area density - resolved vegetation
533    TYPE(real_3d) ::  root_area_density_lsm_f !< input variable for root area density - parametrized vegetation
534    TYPE(real_3d) ::  uvem_radiance_f         !< input variable for uvem radiance lookup table
535    TYPE(real_3d) ::  uvem_projarea_f         !< input variable for uvem projection area lookup table
536!
537!-- Define input variable for buildings
538    TYPE(build_in) ::  buildings_f           !< input variable for buildings
539!
540!-- Define input variables for soil_type
541    TYPE(soil_in)  ::  soil_type_f           !< input variable for soil type
542
543    TYPE(fracs) ::  surface_fraction_f       !< input variable for surface fraction
544
545    TYPE(pars)  ::  albedo_pars_f              !< input variable for albedo parameters
546    TYPE(pars)  ::  building_pars_f            !< input variable for building parameters
547    TYPE(pars)  ::  pavement_pars_f            !< input variable for pavement parameters
548    TYPE(pars)  ::  pavement_subsurface_pars_f !< input variable for pavement parameters
549    TYPE(pars)  ::  soil_pars_f                !< input variable for soil parameters
550    TYPE(pars)  ::  vegetation_pars_f          !< input variable for vegetation parameters
551    TYPE(pars)  ::  water_pars_f               !< input variable for water parameters
552
553    TYPE(chem_emis_att_type)                             ::  chem_emis_att    !< Input Information of Chemistry Emission Data from netcdf 
554    TYPE(chem_emis_val_type), ALLOCATABLE, DIMENSION(:)  ::  chem_emis        !< Input Chemistry Emission Data from netcdf 
555
556    CHARACTER(LEN=3)  ::  char_lod  = 'lod'         !< name of level-of-detail attribute in NetCDF file
557
558    CHARACTER(LEN=10) ::  char_fill = '_FillValue'        !< name of fill value attribute in NetCDF file
559
560    CHARACTER(LEN=100) ::  input_file_static  = 'PIDS_STATIC'  !< Name of file which comprises static input data
561    CHARACTER(LEN=100) ::  input_file_dynamic = 'PIDS_DYNAMIC' !< Name of file which comprises dynamic input data
562    CHARACTER(LEN=100) ::  input_file_chem    = 'PIDS_CHEM'    !< Name of file which comprises chemistry input data
563    CHARACTER(LEN=100) ::  input_file_uvem    = 'PIDS_UVEM'    !< Name of file which comprises static uv_exposure model input data
564    CHARACTER(LEN=100) ::  input_file_vm      = 'PIDS_VM'      !< Name of file which comprises virtual measurement data
565   
566    CHARACTER(LEN=25), ALLOCATABLE, DIMENSION(:) ::  string_values  !< output of string variables read from netcdf input files
567    CHARACTER(LEN=50), DIMENSION(:), ALLOCATABLE ::  vars_pids      !< variable in input file
568
569    INTEGER(iwp)                                     ::  id_emis        !< NetCDF id of input file for chemistry emissions: TBD: It has to be removed
570
571    INTEGER(iwp) ::  nc_stat         !< return value of nf90 function call
572    INTEGER(iwp) ::  num_var_pids    !< number of variables in file
573    INTEGER(iwp) ::  pids_id         !< file id
574
575    LOGICAL ::  input_pids_static  = .FALSE.   !< Flag indicating whether Palm-input-data-standard file containing static information exists
576    LOGICAL ::  input_pids_dynamic = .FALSE.   !< Flag indicating whether Palm-input-data-standard file containing dynamic information exists
577    LOGICAL ::  input_pids_chem    = .FALSE.   !< Flag indicating whether Palm-input-data-standard file containing chemistry information exists
578    LOGICAL ::  input_pids_uvem    = .FALSE.   !< Flag indicating whether uv-expoure-model input file containing static information exists
579    LOGICAL ::  input_pids_vm      = .FALSE.   !< Flag indicating whether input file for virtual measurements exist
580
581    LOGICAL ::  collective_read = .FALSE.      !< Enable NetCDF collective read
582
583    TYPE(global_atts_type) ::  input_file_atts !< global attributes of input file
584
585    SAVE
586
587    PRIVATE
588
589    INTERFACE netcdf_data_input_interpolate
590       MODULE PROCEDURE netcdf_data_input_interpolate_1d
591       MODULE PROCEDURE netcdf_data_input_interpolate_1d_soil
592       MODULE PROCEDURE netcdf_data_input_interpolate_2d
593       MODULE PROCEDURE netcdf_data_input_interpolate_3d
594    END INTERFACE netcdf_data_input_interpolate
595
596    INTERFACE netcdf_data_input_check_dynamic
597       MODULE PROCEDURE netcdf_data_input_check_dynamic
598    END INTERFACE netcdf_data_input_check_dynamic
599
600    INTERFACE netcdf_data_input_check_static
601       MODULE PROCEDURE netcdf_data_input_check_static
602    END INTERFACE netcdf_data_input_check_static
603
604    INTERFACE netcdf_data_input_chemistry_data                       
605       MODULE PROCEDURE netcdf_data_input_chemistry_data
606    END INTERFACE netcdf_data_input_chemistry_data
607   
608    INTERFACE get_dimension_length                       
609       MODULE PROCEDURE get_dimension_length
610    END INTERFACE get_dimension_length
611
612    INTERFACE netcdf_data_input_inquire_file
613       MODULE PROCEDURE netcdf_data_input_inquire_file
614    END INTERFACE netcdf_data_input_inquire_file
615
616    INTERFACE netcdf_data_input_init
617       MODULE PROCEDURE netcdf_data_input_init
618    END INTERFACE netcdf_data_input_init
619   
620    INTERFACE netcdf_data_input_att
621       MODULE PROCEDURE netcdf_data_input_att_int8
622       MODULE PROCEDURE netcdf_data_input_att_int32
623       MODULE PROCEDURE netcdf_data_input_att_real
624       MODULE PROCEDURE netcdf_data_input_att_string
625    END INTERFACE netcdf_data_input_att
626
627    INTERFACE netcdf_data_input_init_3d
628       MODULE PROCEDURE netcdf_data_input_init_3d
629    END INTERFACE netcdf_data_input_init_3d
630   
631    INTERFACE netcdf_data_input_init_lsm
632       MODULE PROCEDURE netcdf_data_input_init_lsm
633    END INTERFACE netcdf_data_input_init_lsm
634
635    INTERFACE netcdf_data_input_surface_data
636       MODULE PROCEDURE netcdf_data_input_surface_data
637    END INTERFACE netcdf_data_input_surface_data
638
639    INTERFACE netcdf_data_input_var
640       MODULE PROCEDURE netcdf_data_input_var_char
641       MODULE PROCEDURE netcdf_data_input_var_real_1d
642       MODULE PROCEDURE netcdf_data_input_var_real_2d
643    END INTERFACE netcdf_data_input_var
644
645    INTERFACE netcdf_data_input_uvem
646       MODULE PROCEDURE netcdf_data_input_uvem
647    END INTERFACE netcdf_data_input_uvem
648
649    INTERFACE get_variable
650       MODULE PROCEDURE get_variable_1d_char
651       MODULE PROCEDURE get_variable_1d_int
652       MODULE PROCEDURE get_variable_1d_real
653       MODULE PROCEDURE get_variable_2d_int8
654       MODULE PROCEDURE get_variable_2d_int32
655       MODULE PROCEDURE get_variable_2d_real
656       MODULE PROCEDURE get_variable_3d_int8
657       MODULE PROCEDURE get_variable_3d_real
658       MODULE PROCEDURE get_variable_3d_real_dynamic
659       MODULE PROCEDURE get_variable_4d_to_3d_real
660       MODULE PROCEDURE get_variable_4d_real
661       MODULE PROCEDURE get_variable_5d_to_4d_real
662       MODULE PROCEDURE get_variable_5d_real           ! (ecc) temp subroutine 4 reading 5D NC arrays
663       MODULE PROCEDURE get_variable_5d_real_dynamic   ! 2B removed as z is out of emission_values
664       MODULE PROCEDURE get_variable_string
665    END INTERFACE get_variable
666
667    INTERFACE get_variable_pr
668       MODULE PROCEDURE get_variable_pr
669    END INTERFACE get_variable_pr
670
671    INTERFACE get_attribute
672       MODULE PROCEDURE get_attribute_real
673       MODULE PROCEDURE get_attribute_int8
674       MODULE PROCEDURE get_attribute_int32
675       MODULE PROCEDURE get_attribute_string
676    END INTERFACE get_attribute
677
678!
679!-- Public data structures
680    PUBLIC real_1d_3d,                                                         &
681           real_2d
682!
683!-- Public variables
684    PUBLIC albedo_pars_f, albedo_type_f, basal_area_density_f, buildings_f,    &
685           building_id_f, building_pars_f, building_type_f,                    &
686           char_fill,                                                          &
687           char_lod,                                                           &
688           chem_emis, chem_emis_att, chem_emis_att_type, chem_emis_val_type,   &
689           coord_ref_sys,                                                      &
690           init_3d, init_model, input_file_atts,                               &
691           input_file_dynamic,                                                 &
692           input_file_static,                                                  &
693           input_pids_static,                                                  &
694           input_pids_dynamic, input_pids_vm, input_file_vm,                   &
695           leaf_area_density_f,                                                &
696           num_var_pids,                                                       &
697           pavement_pars_f, pavement_subsurface_pars_f, pavement_type_f,       &
698           pids_id,                                                            &
699           root_area_density_lad_f, root_area_density_lsm_f, soil_pars_f,      &
700           soil_type_f, street_crossing_f, street_type_f, surface_fraction_f,  &
701           terrain_height_f, vegetation_pars_f, vegetation_type_f,             &
702           vars_pids,                                                          &
703           water_pars_f, water_type_f
704!
705!-- Public uv exposure variables
706    PUBLIC building_obstruction_f, input_file_uvem, input_pids_uvem,           &
707           netcdf_data_input_uvem,                                             &
708           uvem_integration_f, uvem_irradiance_f,                              &
709           uvem_projarea_f, uvem_radiance_f
710
711!
712!-- Public subroutines
713    PUBLIC netcdf_data_input_check_dynamic, netcdf_data_input_check_static,    &
714           netcdf_data_input_chemistry_data,                                   &
715           get_dimension_length,                                               &
716           netcdf_data_input_inquire_file,                                     &
717           netcdf_data_input_init, netcdf_data_input_init_lsm,                 &
718           netcdf_data_input_init_3d, netcdf_data_input_att,                   &
719           netcdf_data_input_interpolate,                                      &
720           netcdf_data_input_surface_data, netcdf_data_input_topo,             &
721           netcdf_data_input_var,                                              &
722           get_attribute,                                                      &
723           get_variable,                                                       &
724           get_variable_pr,                                                    &
725           open_read_file,                                                     &
726           check_existence,                                                    &
727           inquire_num_variables,                                              &
728           inquire_variable_names,                                             &
729           close_input_file
730
731
732 CONTAINS
733
734!------------------------------------------------------------------------------!
735! Description:
736! ------------
737!> Inquires whether NetCDF input files according to Palm-input-data standard
738!> exist. Moreover, basic checks are performed.
739!------------------------------------------------------------------------------!
740    SUBROUTINE netcdf_data_input_inquire_file
741
742       USE control_parameters,                                                 &
743           ONLY:  topo_no_distinct
744
745       IMPLICIT NONE
746
747#if defined ( __netcdf )
748       INQUIRE( FILE = TRIM( input_file_static )   // TRIM( coupling_char ),   &
749                EXIST = input_pids_static  )
750       INQUIRE( FILE = TRIM( input_file_dynamic ) // TRIM( coupling_char ),    &
751                EXIST = input_pids_dynamic )
752       INQUIRE( FILE = TRIM( input_file_chem )    // TRIM( coupling_char ),    &
753                EXIST = input_pids_chem )
754       INQUIRE( FILE = TRIM( input_file_uvem ) // TRIM( coupling_char ),       &
755                EXIST = input_pids_uvem  )
756       INQUIRE( FILE = TRIM( input_file_vm )      // TRIM( coupling_char ),    &
757                EXIST = input_pids_vm )
758#endif
759
760!
761!--    As long as topography can be input via ASCII format, no distinction
762!--    between building and terrain can be made. This case, classify all
763!--    surfaces as default type. Same in case land-surface and urban-surface
764!--    model are not applied.
765       IF ( .NOT. input_pids_static )  THEN
766          topo_no_distinct = .TRUE.
767       ENDIF
768
769    END SUBROUTINE netcdf_data_input_inquire_file
770
771!------------------------------------------------------------------------------!
772! Description:
773! ------------
774!> Reads global attributes and coordinate reference system required for
775!> initialization of the model.
776!------------------------------------------------------------------------------!
777    SUBROUTINE netcdf_data_input_init
778
779       IMPLICIT NONE
780
781       INTEGER(iwp) ::  id_mod     !< NetCDF id of input file
782       INTEGER(iwp) ::  var_id_crs !< NetCDF id of variable crs
783
784       IF ( .NOT. input_pids_static )  RETURN
785
786#if defined ( __netcdf )
787!
788!--    Open file in read-only mode
789       CALL open_read_file( TRIM( input_file_static ) //                       &
790                            TRIM( coupling_char ), id_mod )
791!
792!--    Read global attributes
793       CALL get_attribute( id_mod, input_file_atts%origin_lat_char,            &
794                           input_file_atts%origin_lat, .TRUE. )
795
796       CALL get_attribute( id_mod, input_file_atts%origin_lon_char,            &
797                           input_file_atts%origin_lon, .TRUE. )
798
799       CALL get_attribute( id_mod, input_file_atts%origin_time_char,           &
800                           input_file_atts%origin_time, .TRUE. )
801
802       CALL get_attribute( id_mod, input_file_atts%origin_x_char,              &
803                           input_file_atts%origin_x, .TRUE. )
804
805       CALL get_attribute( id_mod, input_file_atts%origin_y_char,              &
806                           input_file_atts%origin_y, .TRUE. )
807
808       CALL get_attribute( id_mod, input_file_atts%origin_z_char,              &
809                           input_file_atts%origin_z, .TRUE. )
810
811       CALL get_attribute( id_mod, input_file_atts%rotation_angle_char,        &
812                           input_file_atts%rotation_angle, .TRUE. )
813
814       CALL get_attribute( id_mod, input_file_atts%author_char,                &
815                           input_file_atts%author, .TRUE., no_abort=.FALSE. )
816       CALL get_attribute( id_mod, input_file_atts%contact_person_char,        &
817                           input_file_atts%contact_person, .TRUE., no_abort=.FALSE. )
818       CALL get_attribute( id_mod, input_file_atts%institution_char,           &
819                           input_file_atts%institution,    .TRUE., no_abort=.FALSE. )
820       CALL get_attribute( id_mod, input_file_atts%acronym_char,               &
821                           input_file_atts%acronym,        .TRUE., no_abort=.FALSE. )
822
823       CALL get_attribute( id_mod, input_file_atts%campaign_char,              &
824                           input_file_atts%campaign, .TRUE., no_abort=.FALSE. )
825       CALL get_attribute( id_mod, input_file_atts%location_char,              &
826                           input_file_atts%location, .TRUE., no_abort=.FALSE. )
827       CALL get_attribute( id_mod, input_file_atts%site_char,                  &
828                           input_file_atts%site,     .TRUE., no_abort=.FALSE. )
829
830       CALL get_attribute( id_mod, input_file_atts%source_char,                &
831                           input_file_atts%source,     .TRUE., no_abort=.FALSE. )
832       CALL get_attribute( id_mod, input_file_atts%references_char,            &
833                           input_file_atts%references, .TRUE., no_abort=.FALSE. )
834       CALL get_attribute( id_mod, input_file_atts%keywords_char,              &
835                           input_file_atts%keywords,   .TRUE., no_abort=.FALSE. )
836       CALL get_attribute( id_mod, input_file_atts%licence_char,               &
837                           input_file_atts%licence,    .TRUE., no_abort=.FALSE. )
838       CALL get_attribute( id_mod, input_file_atts%comment_char,               &
839                           input_file_atts%comment,    .TRUE., no_abort=.FALSE. )
840!
841!--    Read coordinate reference system if available
842       nc_stat = NF90_INQ_VARID( id_mod, 'crs', var_id_crs )
843       IF ( nc_stat == NF90_NOERR )  THEN
844          CALL get_attribute( id_mod, 'epsg_code',                             &
845                              coord_ref_sys%epsg_code,                         &
846                              .FALSE., 'crs' )
847          CALL get_attribute( id_mod, 'false_easting',                         &
848                              coord_ref_sys%false_easting,                     &
849                              .FALSE., 'crs' )
850          CALL get_attribute( id_mod, 'false_northing',                        &
851                              coord_ref_sys%false_northing,                    &
852                              .FALSE., 'crs' )
853          CALL get_attribute( id_mod, 'grid_mapping_name',                     &
854                              coord_ref_sys%grid_mapping_name,                 &
855                              .FALSE., 'crs' )
856          CALL get_attribute( id_mod, 'inverse_flattening',                    &
857                              coord_ref_sys%inverse_flattening,                &
858                              .FALSE., 'crs' )
859          CALL get_attribute( id_mod, 'latitude_of_projection_origin',         &
860                              coord_ref_sys%latitude_of_projection_origin,     &
861                              .FALSE., 'crs' )
862          CALL get_attribute( id_mod, 'long_name',                             &
863                              coord_ref_sys%long_name,                         &
864                              .FALSE., 'crs' )
865          CALL get_attribute( id_mod, 'longitude_of_central_meridian',         &
866                              coord_ref_sys%longitude_of_central_meridian,     &
867                              .FALSE., 'crs' )
868          CALL get_attribute( id_mod, 'longitude_of_prime_meridian',           &
869                              coord_ref_sys%longitude_of_prime_meridian,       &
870                              .FALSE., 'crs' )
871          CALL get_attribute( id_mod, 'scale_factor_at_central_meridian',      &
872                              coord_ref_sys%scale_factor_at_central_meridian,  &
873                              .FALSE., 'crs' )
874          CALL get_attribute( id_mod, 'semi_major_axis',                       &
875                              coord_ref_sys%semi_major_axis,                   &
876                              .FALSE., 'crs' )
877          CALL get_attribute( id_mod, 'units',                                 &
878                              coord_ref_sys%units,                             &
879                              .FALSE., 'crs' )
880       ELSE
881!
882!--       Calculate central meridian from origin_lon
883          coord_ref_sys%longitude_of_central_meridian = &
884             CEILING( input_file_atts%origin_lon / 6.0_wp ) * 6.0_wp - 3.0_wp
885       ENDIF
886!
887!--    Finally, close input file
888       CALL close_input_file( id_mod )
889#endif
890!
891!--    Copy latitude, longitude, origin_z, rotation angle on init type
892       init_model%latitude        = input_file_atts%origin_lat
893       init_model%longitude       = input_file_atts%origin_lon
894       init_model%origin_time     = input_file_atts%origin_time 
895       init_model%origin_x        = input_file_atts%origin_x
896       init_model%origin_y        = input_file_atts%origin_y
897       init_model%origin_z        = input_file_atts%origin_z 
898       init_model%rotation_angle  = input_file_atts%rotation_angle 
899           
900!
901!--    In case of nested runs, each model domain might have different longitude
902!--    and latitude, which would result in different Coriolis parameters and
903!--    sun-zenith angles. To avoid this, longitude and latitude in each model
904!--    domain will be set to the values of the root model. Please note, this
905!--    synchronization is required already here.
906#if defined( __parallel )
907       CALL MPI_BCAST( init_model%latitude,  1, MPI_REAL, 0,                   &
908                       MPI_COMM_WORLD, ierr )
909       CALL MPI_BCAST( init_model%longitude, 1, MPI_REAL, 0,                   &
910                       MPI_COMM_WORLD, ierr )
911#endif
912
913    END SUBROUTINE netcdf_data_input_init
914   
915!------------------------------------------------------------------------------!
916! Description:
917! ------------
918!> Read an array of characters.
919!------------------------------------------------------------------------------!
920    SUBROUTINE netcdf_data_input_var_char( val, search_string, id_mod )
921
922       IMPLICIT NONE
923
924       CHARACTER(LEN=*) ::  search_string     !< name of the variable
925       CHARACTER(LEN=*), DIMENSION(:) ::  val !< variable which should be read
926       
927       INTEGER(iwp) ::  id_mod   !< NetCDF id of input file
928
929#if defined ( __netcdf )
930!
931!--    Read variable
932       CALL get_variable( id_mod, search_string, val )
933#endif           
934
935    END SUBROUTINE netcdf_data_input_var_char
936   
937!------------------------------------------------------------------------------!
938! Description:
939! ------------
940!> Read an 1D array of REAL values.
941!------------------------------------------------------------------------------!
942    SUBROUTINE netcdf_data_input_var_real_1d( val, search_string, id_mod )
943
944       IMPLICIT NONE
945
946       CHARACTER(LEN=*) ::  search_string     !< name of the variable     
947       
948       INTEGER(iwp) ::  id_mod        !< NetCDF id of input file
949       
950       REAL(wp), DIMENSION(:) ::  val !< variable which should be read
951
952#if defined ( __netcdf )
953!
954!--    Read variable
955       CALL get_variable( id_mod, search_string, val )
956#endif           
957
958    END SUBROUTINE netcdf_data_input_var_real_1d
959   
960!------------------------------------------------------------------------------!
961! Description:
962! ------------
963!> Read an 1D array of REAL values.
964!------------------------------------------------------------------------------!
965    SUBROUTINE netcdf_data_input_var_real_2d( val, search_string,              &
966                                              id_mod, d1s, d1e, d2s, d2e )
967
968       IMPLICIT NONE
969
970       CHARACTER(LEN=*) ::  search_string     !< name of the variable     
971       
972       INTEGER(iwp) ::  id_mod  !< NetCDF id of input file
973       INTEGER(iwp) ::  d1e     !< end index of first dimension to be read
974       INTEGER(iwp) ::  d2e     !< end index of second dimension to be read
975       INTEGER(iwp) ::  d1s     !< start index of first dimension to be read
976       INTEGER(iwp) ::  d2s     !< start index of second dimension to be read
977       
978       REAL(wp), DIMENSION(:,:) ::  val !< variable which should be read
979
980#if defined ( __netcdf )
981!
982!--    Read character variable
983       CALL get_variable( id_mod, search_string, val, d1s, d1e, d2s, d2e )
984#endif           
985
986    END SUBROUTINE netcdf_data_input_var_real_2d
987   
988!------------------------------------------------------------------------------!
989! Description:
990! ------------
991!> Read a global string attribute
992!------------------------------------------------------------------------------!
993    SUBROUTINE netcdf_data_input_att_string( val, search_string, id_mod,       &
994                                             input_file, global, openclose,    &
995                                             variable_name )
996
997       IMPLICIT NONE
998
999       CHARACTER(LEN=*) ::  search_string !< name of the attribue
1000       CHARACTER(LEN=*) ::  val           !< attribute
1001       
1002       CHARACTER(LEN=*) ::  input_file    !< name of input file
1003       CHARACTER(LEN=*) ::  openclose     !< string indicating whether NetCDF needs to be opend or closed
1004       CHARACTER(LEN=*) ::  variable_name !< string indicating whether NetCDF needs to be opend or closed 
1005       
1006       INTEGER(iwp) ::  id_mod   !< NetCDF id of input file
1007       
1008       LOGICAL ::  global                 !< flag indicating a global or a variable's attribute
1009
1010#if defined ( __netcdf )
1011!
1012!--    Open file in read-only mode if necessary
1013       IF ( openclose == 'open' )  THEN
1014          CALL open_read_file( TRIM( input_file ) // TRIM( coupling_char ), &
1015                                  id_mod )
1016       ENDIF
1017!
1018!--    Read global attribute
1019       IF ( global )  THEN
1020          CALL get_attribute( id_mod, search_string, val, global )
1021!
1022!--    Read variable attribute
1023       ELSE
1024          CALL get_attribute( id_mod, search_string, val, global, variable_name )
1025       ENDIF
1026!
1027!--    Close input file
1028       IF ( openclose == 'close' )  CALL close_input_file( id_mod )
1029#endif           
1030
1031    END SUBROUTINE netcdf_data_input_att_string
1032   
1033!------------------------------------------------------------------------------!
1034! Description:
1035! ------------
1036!> Read a global 8-bit integer attribute
1037!------------------------------------------------------------------------------!
1038    SUBROUTINE netcdf_data_input_att_int8( val, search_string, id_mod,         &
1039                                           input_file, global, openclose,      &
1040                                           variable_name )
1041
1042       IMPLICIT NONE
1043
1044       CHARACTER(LEN=*) ::  search_string !< name of the attribue
1045       
1046       CHARACTER(LEN=*) ::  input_file    !< name of input file
1047       CHARACTER(LEN=*) ::  openclose     !< string indicating whether NetCDF needs to be opend or closed
1048       CHARACTER(LEN=*) ::  variable_name !< string indicating whether NetCDF needs to be opend or closed
1049       
1050       INTEGER(iwp) ::  id_mod   !< NetCDF id of input file
1051       INTEGER(KIND=1) ::  val      !< value of the attribute
1052       
1053       LOGICAL ::  global        !< flag indicating a global or a variable's attribute
1054
1055#if defined ( __netcdf )
1056!
1057!--    Open file in read-only mode
1058       IF ( openclose == 'open' )  THEN
1059          CALL open_read_file( TRIM( input_file ) // TRIM( coupling_char ), &
1060                                  id_mod )
1061       ENDIF
1062!
1063!--    Read global attribute
1064       IF ( global )  THEN
1065          CALL get_attribute( id_mod, search_string, val, global )
1066!
1067!--    Read variable attribute
1068       ELSE
1069          CALL get_attribute( id_mod, search_string, val, global, variable_name )
1070       ENDIF
1071!
1072!--    Finally, close input file
1073       IF ( openclose == 'close' )  CALL close_input_file( id_mod )
1074#endif           
1075
1076    END SUBROUTINE netcdf_data_input_att_int8
1077   
1078!------------------------------------------------------------------------------!
1079! Description:
1080! ------------
1081!> Read a global 32-bit integer attribute
1082!------------------------------------------------------------------------------!
1083    SUBROUTINE netcdf_data_input_att_int32( val, search_string, id_mod,        &
1084                                            input_file, global, openclose,     &
1085                                            variable_name )
1086
1087       IMPLICIT NONE
1088
1089       CHARACTER(LEN=*) ::  search_string !< name of the attribue
1090       
1091       CHARACTER(LEN=*) ::  input_file    !< name of input file
1092       CHARACTER(LEN=*) ::  openclose     !< string indicating whether NetCDF needs to be opend or closed
1093       CHARACTER(LEN=*) ::  variable_name !< string indicating whether NetCDF needs to be opend or closed
1094       
1095       INTEGER(iwp) ::  id_mod   !< NetCDF id of input file
1096       INTEGER(iwp) ::  val      !< value of the attribute
1097       
1098       LOGICAL ::  global        !< flag indicating a global or a variable's attribute
1099
1100#if defined ( __netcdf )
1101!
1102!--    Open file in read-only mode
1103       IF ( openclose == 'open' )  THEN
1104          CALL open_read_file( TRIM( input_file ) // TRIM( coupling_char ), &
1105                                  id_mod )
1106       ENDIF
1107!
1108!--    Read global attribute
1109       IF ( global )  THEN
1110          CALL get_attribute( id_mod, search_string, val, global )
1111!
1112!--    Read variable attribute
1113       ELSE
1114          CALL get_attribute( id_mod, search_string, val, global, variable_name )
1115       ENDIF
1116!
1117!--    Finally, close input file
1118       IF ( openclose == 'close' )  CALL close_input_file( id_mod )
1119#endif           
1120
1121    END SUBROUTINE netcdf_data_input_att_int32
1122   
1123!------------------------------------------------------------------------------!
1124! Description:
1125! ------------
1126!> Read a global real attribute
1127!------------------------------------------------------------------------------!
1128    SUBROUTINE netcdf_data_input_att_real( val, search_string, id_mod,         &
1129                                           input_file, global, openclose,      &
1130                                           variable_name )
1131
1132       IMPLICIT NONE
1133
1134       CHARACTER(LEN=*) ::  search_string !< name of the attribue
1135       
1136       CHARACTER(LEN=*) ::  input_file    !< name of input file
1137       CHARACTER(LEN=*) ::  openclose     !< string indicating whether NetCDF needs to be opend or closed
1138       CHARACTER(LEN=*) ::  variable_name !< string indicating whether NetCDF needs to be opend or closed
1139       
1140       INTEGER(iwp) ::  id_mod            !< NetCDF id of input file
1141       
1142       LOGICAL ::  global                 !< flag indicating a global or a variable's attribute
1143       
1144       REAL(wp) ::  val                   !< value of the attribute
1145
1146#if defined ( __netcdf )
1147!
1148!--    Open file in read-only mode
1149       IF ( openclose == 'open' )  THEN
1150          CALL open_read_file( TRIM( input_file ) // TRIM( coupling_char ), &
1151                                  id_mod )
1152       ENDIF
1153!
1154!--    Read global attribute
1155       IF ( global )  THEN
1156          CALL get_attribute( id_mod, search_string, val, global )
1157!
1158!--    Read variable attribute
1159       ELSE
1160          CALL get_attribute( id_mod, search_string, val, global, variable_name )
1161       ENDIF
1162!
1163!--    Finally, close input file
1164       IF ( openclose == 'close' )  CALL close_input_file( id_mod )
1165#endif           
1166
1167    END SUBROUTINE netcdf_data_input_att_real
1168
1169!------------------------------------------------------------------------------!
1170! Description:
1171! ------------
1172!> Reads Chemistry NETCDF Input data, such as emission values, emission species, etc.
1173!------------------------------------------------------------------------------!
1174
1175    SUBROUTINE netcdf_data_input_chemistry_data(emt_att,emt)
1176
1177       USE chem_modules,                                       &
1178           ONLY:  emiss_lod, time_fac_type, surface_csflux_name
1179
1180       USE control_parameters,                                 &
1181           ONLY:  message_string
1182
1183       USE indices,                                            &
1184           ONLY:  nxl, nxr, nys, nyn
1185
1186       IMPLICIT NONE
1187
1188       TYPE(chem_emis_att_type), INTENT(INOUT)                             ::  emt_att
1189       TYPE(chem_emis_val_type), ALLOCATABLE, DIMENSION(:), INTENT(INOUT)  ::  emt
1190   
1191       INTEGER(iwp)  ::  i, j, k      !< generic counters
1192       INTEGER(iwp)  ::  ispec        !< index for number of emission species in input
1193       INTEGER(iwp)  ::  len_dims     !< Length of dimension
1194       INTEGER(iwp)  ::  num_vars     !< number of variables in netcdf input file
1195
1196!
1197!-- dum_var_4d are designed to read in emission_values from the chemistry netCDF file.
1198!-- Currently the vestigial "z" dimension in emission_values makes it a 5D array,
1199!-- hence the corresponding dum_var_5d array.  When the "z" dimension is removed
1200!-- completely, dum_var_4d will be used instead
1201!-- (ecc 20190425)
1202
1203!       REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:)    ::  dum_var_4d  !< temp array 4 4D chem emission data
1204       REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:,:)  ::  dum_var_5d  !< temp array 4 5D chem emission data
1205
1206!
1207!-- Start processing data
1208!
1209!-- Emission LOD 0 (Parameterized mode)
1210
1211        IF  ( emiss_lod == 0 )  THEN
1212
1213! for reference (ecc)
1214!       IF (TRIM(mode_emis) == "PARAMETERIZED" .OR. TRIM(mode_emis) == "parameterized") THEN
1215
1216           ispec=1
1217           emt_att%n_emiss_species = 0
1218
1219!
1220!-- number of species
1221
1222           DO  WHILE (TRIM( surface_csflux_name( ispec ) ) /= 'novalue' )
1223
1224             emt_att%n_emiss_species = emt_att%n_emiss_species + 1
1225             ispec=ispec+1
1226!
1227!-- followling line retained for compatibility with salsa_mod
1228!-- which still uses emt_att%nspec heavily (ecc)
1229
1230             emt_att%nspec = emt_att%nspec + 1
1231
1232           ENDDO
1233
1234!
1235!-- allocate emission values data type arrays
1236
1237          ALLOCATE ( emt(emt_att%n_emiss_species) )
1238
1239!
1240!-- Read EMISSION SPECIES NAMES
1241
1242!
1243!-- allocate space for strings
1244
1245          ALLOCATE (emt_att%species_name(emt_att%n_emiss_species) )
1246 
1247         DO ispec = 1, emt_att%n_emiss_species
1248            emt_att%species_name(ispec) = TRIM(surface_csflux_name(ispec))
1249         ENDDO
1250
1251!
1252!-- LOD 1 (default mode) and LOD 2 (pre-processed mode)
1253
1254       ELSE
1255
1256#if defined ( __netcdf )
1257
1258          IF ( .NOT. input_pids_chem )  RETURN
1259
1260!
1261!-- first we allocate memory space for the emission species and then
1262!-- we differentiate between LOD 1 (default mode) and LOD 2 (pre-processed mode)
1263
1264!
1265!-- open emission data file ( {palmcase}_chemistry )
1266
1267          CALL open_read_file ( TRIM(input_file_chem) // TRIM(coupling_char), id_emis )
1268
1269!
1270!-- inquire number of variables
1271
1272          CALL inquire_num_variables ( id_emis, num_vars )
1273
1274!
1275!-- Get General Dimension Lengths: only # species and # categories.
1276!-- Tther dimensions depend on the emission mode or specific components
1277
1278          CALL get_dimension_length ( id_emis, emt_att%n_emiss_species, 'nspecies' )
1279
1280!
1281!-- backward compatibility for salsa_mod (ecc)
1282
1283          emt_att%nspec = emt_att%n_emiss_species
1284
1285!
1286!-- Allocate emission values data type arrays
1287
1288          ALLOCATE ( emt(emt_att%n_emiss_species) )
1289
1290!
1291!-- READING IN SPECIES NAMES
1292
1293!
1294!-- Allocate memory for species names
1295
1296          ALLOCATE ( emt_att%species_name(emt_att%n_emiss_species) )
1297
1298!
1299!-- Retrieve variable name (again, should use n_emiss_strlen)
1300
1301          CALL get_variable( id_emis, 'emission_name',    &
1302                             string_values, emt_att%n_emiss_species )
1303          emt_att%species_name=string_values
1304
1305!
1306!-- dealocate string_values previously allocated in get_variable call
1307
1308          IF  ( ALLOCATED(string_values) )  DEALLOCATE (string_values)
1309
1310!
1311!-- READING IN SPECIES INDICES
1312
1313!
1314!-- Allocate memory for species indices
1315
1316          ALLOCATE ( emt_att%species_index(emt_att%n_emiss_species) )
1317
1318!
1319!-- Retrieve variable data
1320
1321          CALL get_variable( id_emis, 'emission_index', emt_att%species_index )
1322!
1323!-- Now the routine has to distinguish between chemistry emission
1324!-- LOD 1 (DEFAULT mode) and LOD 2 (PRE-PROCESSED mode)
1325
1326!
1327!-- START OF EMISSION LOD 1 (DEFAULT MODE)
1328
1329
1330          IF  ( emiss_lod == 1 )  THEN
1331
1332! for reference (ecc)
1333!          IF (TRIM(mode_emis) == "DEFAULT" .OR. TRIM(mode_emis) == "default") THEN
1334
1335!
1336!-- get number of emission categories
1337
1338             CALL get_dimension_length ( id_emis, emt_att%ncat, 'ncat' )
1339
1340!-- READING IN EMISSION CATEGORIES INDICES
1341
1342             ALLOCATE ( emt_att%cat_index(emt_att%ncat) )
1343
1344!
1345!-- Retrieve variable data
1346
1347             CALL get_variable( id_emis, 'emission_cat_index', emt_att%cat_index )
1348
1349
1350!
1351!-- Loop through individual species to get basic information on
1352!-- VOC/PM/NOX/SOX
1353
1354!------------------------------------------------------------------------------
1355!-- NOTE - CHECK ARRAY INDICES FOR READING IN NAMES AND SPECIES
1356!--        IN LOD1 (DEFAULT MODE) FOR THE VARIOUS MODE SPLITS
1357!--        AS ALL ID_EMIS CONDITIONALS HAVE BEEN REMOVED FROM GET_VAR
1358!--        FUNCTIONS.  IN THEORY THIS WOULD MEAN ALL ARRAYS SHOULD BE
1359!--        READ FROM 0 to N-1 (C CONVENTION) AS OPPOSED TO 1 to N
1360!--        (FORTRAN CONVENTION).  KEEP THIS IN MIND !!
1361!--        (ecc 20190424)
1362!------------------------------------------------------------------------------
1363 
1364             DO  ispec = 1, emt_att%n_emiss_species
1365
1366!
1367!-- VOC DATA (name and composition)
1368
1369                IF  ( TRIM(emt_att%species_name(ispec)) == "VOC" .OR.                  &
1370                      TRIM(emt_att%species_name(ispec)) == "voc" )  THEN
1371
1372!
1373!-- VOC name
1374                   CALL get_dimension_length ( id_emis, emt_att%nvoc, 'nvoc' )
1375                   ALLOCATE ( emt_att%voc_name(emt_att%nvoc) )
1376                   CALL get_variable ( id_emis,"emission_voc_name",  &
1377                                       string_values, emt_att%nvoc )
1378                   emt_att%voc_name = string_values
1379                   IF  ( ALLOCATED(string_values) )  DEALLOCATE (string_values)
1380
1381!
1382!-- VOC composition
1383
1384                   ALLOCATE ( emt_att%voc_comp(emt_att%ncat,emt_att%nvoc) )
1385                   CALL get_variable ( id_emis, "composition_voc", emt_att%voc_comp,     &
1386                                       1, emt_att%ncat, 1, emt_att%nvoc )
1387
1388                ENDIF  ! VOC
1389
1390!
1391!-- PM DATA (name and composition)
1392
1393                IF  ( TRIM(emt_att%species_name(ispec)) == "PM" .OR.                   &
1394                      TRIM(emt_att%species_name(ispec)) == "pm")  THEN
1395
1396!
1397!-- PM name
1398
1399                   CALL get_dimension_length ( id_emis, emt_att%npm, 'npm' )
1400                   ALLOCATE ( emt_att%pm_name(emt_att%npm) )
1401                   CALL get_variable ( id_emis, "pm_name", string_values, emt_att%npm )
1402                   emt_att%pm_name = string_values
1403                   IF  ( ALLOCATED(string_values) )  DEALLOCATE (string_values)     
1404
1405!
1406!-- PM composition (PM1, PM2.5 and PM10)
1407
1408                   len_dims = 3  ! PM1, PM2.5, PM10
1409                   ALLOCATE(emt_att%pm_comp(emt_att%ncat,emt_att%npm,len_dims))
1410                   CALL get_variable ( id_emis, "composition_pm", emt_att%pm_comp,       &
1411                                       1, emt_att%ncat, 1, emt_att%npm, 1, len_dims )
1412
1413                ENDIF  ! PM
1414
1415!
1416!-- NOX (NO and NO2)
1417
1418                IF  ( TRIM(emt_att%species_name(ispec)) == "NOX" .OR.                  &
1419                      TRIM(emt_att%species_name(ispec)) == "nox" )  THEN
1420
1421                   ALLOCATE ( emt_att%nox_comp(emt_att%ncat,emt_att%nnox) )
1422                   CALL get_variable ( id_emis, "composition_nox", emt_att%nox_comp,     &
1423                                       1, emt_att%ncat, 1, emt_att%nnox )
1424
1425                ENDIF  ! NOX
1426
1427!
1428!-- SOX (SO2 and SO4)
1429
1430                IF  ( TRIM(emt_att%species_name(ispec)) == "SOX" .OR.                  &
1431                      TRIM(emt_att%species_name(ispec)) == "sox" )  THEN
1432
1433                   ALLOCATE ( emt_att%sox_comp(emt_att%ncat,emt_att%nsox) )
1434                   CALL get_variable ( id_emis, "composition_sox", emt_att%sox_comp,     &
1435                                       1, emt_att%ncat, 1, emt_att%nsox )
1436
1437                ENDIF  ! SOX
1438
1439             ENDDO  ! do ispec
1440
1441!
1442!-- EMISSION TIME SCALING FACTORS (hourly and MDH data)
1443 
1444!     
1445!-- HOUR   
1446             IF  ( TRIM(time_fac_type) == "HOUR" .OR.                        &
1447                   TRIM(time_fac_type) == "hour" )  THEN
1448
1449                CALL get_dimension_length ( id_emis, emt_att%nhoursyear, 'nhoursyear' )
1450                ALLOCATE ( emt_att%hourly_emis_time_factor(emt_att%ncat,emt_att%nhoursyear) )
1451                CALL get_variable ( id_emis, "emission_time_factors",          &
1452                                    emt_att%hourly_emis_time_factor,           &
1453                                    1, emt_att%ncat, 1, emt_att%nhoursyear )
1454
1455!
1456!-- MDH
1457
1458             ELSE IF  ( TRIM(time_fac_type)  ==  "MDH" .OR.                  &
1459                        TRIM(time_fac_type)  ==  "mdh" )  THEN
1460
1461                CALL get_dimension_length ( id_emis, emt_att%nmonthdayhour, 'nmonthdayhour' )
1462                ALLOCATE ( emt_att%mdh_emis_time_factor(emt_att%ncat,emt_att%nmonthdayhour) )
1463                CALL get_variable ( id_emis, "emission_time_factors",          &
1464                                    emt_att%mdh_emis_time_factor,              &
1465                                    1, emt_att%ncat, 1, emt_att%nmonthdayhour )
1466
1467!
1468!-- ERROR (time factor undefined)
1469
1470             ELSE
1471
1472                message_string = 'We are in the DEFAULT chemistry emissions mode: '  //  &
1473                                 '     !no time-factor type specified!'              //  &
1474                                 'Please specify the value of time_fac_type:'        //  &
1475                                 '         either "MDH" or "HOUR"'                 
1476                CALL message( 'netcdf_data_input_chemistry_data', 'CM0200', 2, 2, 0, 6, 0 ) 
1477 
1478
1479             ENDIF  ! time_fac_type
1480
1481!
1482!-- read in default (LOD1) emissions from chemisty netCDF file per species
1483
1484!
1485!-- NOTE - at the moment the data is read in per species, but in the future it would
1486!--        be much more sensible to read in per species per time step to reduce
1487!--        memory consumption and, to a lesser degree, dimensionality of data exchange
1488!--        (I expect this will be necessary when the problem size is large)
1489
1490             DO ispec = 1, emt_att%n_emiss_species
1491
1492!
1493!-- allocate space for species specific emission values
1494!-- NOTE - this array is extended by 1 cell in each horizontal direction
1495!--        to compensate for an apparent linear offset.  The reason of this
1496!--        offset is not known but it has been determined to take place beyond the
1497!--        scope of this module, and has little to do with index conventions.
1498!--        That is, setting the array horizontal limit from nx0:nx1 to 1:(nx1-nx0+1)
1499!--        or nx0+1:nx1+1 did not result in correct or definite behavior
1500!--        This must be looked at at some point by the Hannover team but for now
1501!--        this workaround is deemed reasonable (ecc 20190417)
1502
1503                IF ( .NOT. ALLOCATED ( emt(ispec)%default_emission_data ) )  THEN
1504                    ALLOCATE ( emt(ispec)%default_emission_data(emt_att%ncat,nys:nyn+1,nxl:nxr+1) )
1505                ENDIF
1506!
1507!-- allocate dummy variable w/ index order identical to that shown in the netCDF header
1508
1509                ALLOCATE ( dum_var_5d(1,nys:nyn,nxl:nxr,1,emt_att%ncat) )
1510!
1511!-- get variable.  be very careful
1512!-- I am using get_variable_5d_real_dynamic (note logical argument at the end)
1513!-- 1) use Fortran index convention (i.e., 1 to N)
1514!-- 2) index order must be in reverse order from above allocation order
1515 
1516                CALL get_variable ( id_emis, "emission_values", dum_var_5d, &
1517                                    1,            ispec, nxl+1,     nys+1,     1,                    &
1518                                    emt_att%ncat, 1,     nxr-nxl+1, nyn-nys+1, emt_att%dt_emission,  &
1519                                    .FALSE. )
1520!
1521!-- assign temp array to data structure then deallocate temp array
1522!-- NOTE - indices are shifted from nx0:nx1 to nx0+1:nx1+1 to offset
1523!--        the emission data array to counter said domain offset
1524!--        (ecc 20190417)
1525
1526                DO k = 1, emt_att%ncat
1527                   DO j = nys+1, nyn+1
1528                      DO i = nxl+1, nxr+1
1529                         emt(ispec)%default_emission_data(k,j,i) = dum_var_5d(1,j-1,i-1,1,k)
1530                      ENDDO
1531                   ENDDO
1532                ENDDO
1533
1534                DEALLOCATE ( dum_var_5d )
1535
1536             ENDDO  ! ispec
1537!
1538!-- UNITS
1539
1540             CALL get_attribute(id_emis,"units",emt_att%units,.FALSE.,"emission_values")
1541
1542!
1543!-- END DEFAULT MODE
1544
1545
1546!
1547!-- START LOD 2 (PRE-PROCESSED MODE)
1548
1549          ELSE IF  ( emiss_lod == 2 )  THEN
1550
1551! for reference (ecc)
1552!          ELSE IF (TRIM(mode_emis) == "PRE-PROCESSED" .OR. TRIM(mode_emis) == "pre-processed") THEN
1553
1554!
1555!-- For LOD 2 only VOC and emission data need be read
1556
1557!------------------------------------------------------------------------------
1558!-- NOTE - CHECK ARRAY INDICES FOR READING IN NAMES AND SPECIES
1559!--        IN LOD2 (PRE-PROCESSED MODE) FOR THE VARIOUS MODE SPLITS
1560!--        AS ALL ID_EMIS CONDITIONALS HAVE BEEN REMOVED FROM GET_VAR
1561!--        FUNCTIONS.  IN THEORY THIS WOULD MEAN ALL ARRAYS SHOULD BE
1562!--        READ FROM 0 to N-1 (C CONVENTION) AS OPPOSED TO 1 to N
1563!--        (FORTRAN CONVENTION).  KEEP THIS IN MIND !!
1564!--        (ecc 20190424)
1565!------------------------------------------------------------------------------
1566
1567             DO ispec = 1, emt_att%n_emiss_species
1568
1569!
1570!-- VOC DATA (name and composition)
1571
1572                IF  ( TRIM(emt_att%species_name(ispec)) == "VOC" .OR.                  &
1573                      TRIM(emt_att%species_name(ispec)) == "voc" )  THEN
1574
1575!
1576!-- VOC name
1577                   CALL get_dimension_length ( id_emis, emt_att%nvoc, 'nvoc' )
1578                   ALLOCATE ( emt_att%voc_name(emt_att%nvoc) )
1579                   CALL get_variable ( id_emis, "emission_voc_name",                     &
1580                                       string_values, emt_att%nvoc)
1581                   emt_att%voc_name = string_values
1582                   IF  ( ALLOCATED(string_values) )  DEALLOCATE (string_values)
1583
1584!
1585!-- VOC composition
1586 
1587                   ALLOCATE ( emt_att%voc_comp(emt_att%ncat,emt_att%nvoc) )
1588                   CALL get_variable ( id_emis, "composition_voc", emt_att%voc_comp,     &
1589                                       1, emt_att%ncat, 1, emt_att%nvoc )
1590                ENDIF  ! VOC
1591 
1592             ENDDO  ! ispec
1593
1594!
1595!-- EMISSION DATA
1596
1597             CALL get_dimension_length ( id_emis, emt_att%dt_emission, 'time' )   
1598 
1599!
1600!-- read in pre-processed (LOD2) emissions from chemisty netCDF file per species
1601
1602!
1603!-- NOTE - at the moment the data is read in per species, but in the future it would
1604!--        be much more sensible to read in per species per time step to reduce
1605!--        memory consumption and, to a lesser degree, dimensionality of data exchange
1606!--        (I expect this will be necessary when the problem size is large)
1607
1608             DO ispec = 1, emt_att%n_emiss_species
1609
1610!
1611!-- allocate space for species specific emission values
1612!-- NOTE - this array is extended by 1 cell in each horizontal direction
1613!--        to compensate for an apparent linear offset.  The reason of this
1614!--        offset is not known but it has been determined to take place beyond the
1615!--        scope of this module, and has little to do with index conventions.
1616!--        That is, setting the array horizontal limit from nx0:nx1 to 1:(nx1-nx0+1)
1617!--        or nx0+1:nx1+1 did not result in correct or definite behavior
1618!--        This must be looked at at some point by the Hannover team but for now
1619!--        this workaround is deemed reasonable (ecc 20190417)
1620
1621                IF ( .NOT. ALLOCATED( emt(ispec)%preproc_emission_data ) )  THEN
1622                   ALLOCATE( emt(ispec)%preproc_emission_data(                           &
1623                             emt_att%dt_emission, 1, nys:nyn+1, nxl:nxr+1) )
1624                ENDIF
1625!
1626!-- allocate dummy variable w/ index order identical to that shown in the netCDF header
1627
1628                ALLOCATE ( dum_var_5d(emt_att%dt_emission,1,nys:nyn,nxl:nxr,1) )
1629!
1630!-- get variable.  be very careful
1631!-- I am using get_variable_5d_real_dynamic (note logical argument at the end)
1632!-- 1) use Fortran index convention (i.e., 1 to N)
1633!-- 2) index order must be in reverse order from above allocation order
1634
1635                CALL get_variable ( id_emis, "emission_values", dum_var_5d, &
1636                                    ispec, nxl+1,     nys+1,     1, 1,                   &
1637                                    1,     nxr-nxl+1, nyn-nys+1, 1, emt_att%dt_emission, &
1638                                    .FALSE. )
1639!
1640!-- assign temp array to data structure then deallocate temp array
1641!-- NOTE - indices are shifted from nx0:nx1 to nx0+1:nx1+1 to offset
1642!--        the emission data array to counter said unkonwn offset
1643!--        (ecc 20190417)
1644
1645                DO k = 1, emt_att%dt_emission
1646                   DO j = nys+1, nyn+1
1647                      DO i = nxl+1, nxr+1
1648                         emt(ispec)%preproc_emission_data(k,1,j,i) = dum_var_5d(k,1,j-1,i-1,1)
1649                      ENDDO
1650                   ENDDO
1651                ENDDO
1652
1653                DEALLOCATE ( dum_var_5d )
1654
1655             ENDDO  ! ispec
1656!
1657!-- UNITS
1658
1659             CALL get_attribute ( id_emis, "units", emt_att%units, .FALSE. , "emission_values" )
1660       
1661          ENDIF  ! LOD1 & LOD2 (default and pre-processed mode)
1662
1663          CALL close_input_file (id_emis)
1664
1665#endif
1666
1667       ENDIF ! LOD0 (parameterized mode)
1668
1669    END SUBROUTINE netcdf_data_input_chemistry_data
1670
1671
1672!------------------------------------------------------------------------------!
1673! Description:
1674! ------------
1675!> Reads surface classification data, such as vegetation and soil type, etc. .
1676!------------------------------------------------------------------------------!
1677    SUBROUTINE netcdf_data_input_surface_data
1678
1679       USE control_parameters,                                                 &
1680           ONLY:  land_surface, plant_canopy, urban_surface
1681
1682       USE indices,                                                            &
1683           ONLY:  nbgp, nxl, nxr, nyn, nys
1684
1685
1686       IMPLICIT NONE
1687
1688       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names  !< variable names in static input file
1689
1690       INTEGER(iwp) ::  id_surf   !< NetCDF id of input file
1691       INTEGER(iwp) ::  k         !< running index along z-direction
1692       INTEGER(iwp) ::  k2        !< running index
1693       INTEGER(iwp) ::  num_vars  !< number of variables in input file
1694       INTEGER(iwp) ::  nz_soil   !< number of soil layers in file
1695
1696!
1697!--    If not static input file is available, skip this routine
1698       IF ( .NOT. input_pids_static )  RETURN
1699!
1700!--    Measure CPU time
1701       CALL cpu_log( log_point_s(82), 'NetCDF input', 'start' )
1702!
1703!--    Read plant canopy variables.
1704       IF ( plant_canopy )  THEN
1705#if defined ( __netcdf )
1706!
1707!--       Open file in read-only mode
1708          CALL open_read_file( TRIM( input_file_static ) //                    &
1709                               TRIM( coupling_char ) , id_surf )
1710!
1711!--       At first, inquire all variable names.
1712!--       This will be used to check whether an optional input variable
1713!--       exist or not.
1714          CALL inquire_num_variables( id_surf, num_vars )
1715
1716          ALLOCATE( var_names(1:num_vars) )
1717          CALL inquire_variable_names( id_surf, var_names )
1718
1719!
1720!--       Read leaf area density - resolved vegetation
1721          IF ( check_existence( var_names, 'lad' ) )  THEN
1722             leaf_area_density_f%from_file = .TRUE.
1723             CALL get_attribute( id_surf, char_fill,                           &
1724                                 leaf_area_density_f%fill,                     &
1725                                 .FALSE., 'lad' )
1726!
1727!--          Inquire number of vertical vegetation layer
1728             CALL get_dimension_length( id_surf,                               &
1729                                        leaf_area_density_f%nz,                &
1730                                        'zlad' )
1731!
1732!--          Allocate variable for leaf-area density
1733             ALLOCATE( leaf_area_density_f%var( 0:leaf_area_density_f%nz-1,    &
1734                                                nys:nyn,nxl:nxr) )
1735
1736             CALL get_variable( id_surf, 'lad', leaf_area_density_f%var,       &
1737                                nxl, nxr, nys, nyn,                            &
1738                                0, leaf_area_density_f%nz-1 )
1739
1740          ELSE
1741             leaf_area_density_f%from_file = .FALSE.
1742          ENDIF
1743
1744!
1745!--       Read basal area density - resolved vegetation
1746          IF ( check_existence( var_names, 'bad' ) )  THEN
1747             basal_area_density_f%from_file = .TRUE.
1748             CALL get_attribute( id_surf, char_fill,                           &
1749                                 basal_area_density_f%fill,                    &
1750                                 .FALSE., 'bad' )
1751!
1752!--          Inquire number of vertical vegetation layer
1753             CALL get_dimension_length( id_surf,                               &
1754                                        basal_area_density_f%nz,               & 
1755                                        'zlad' )
1756!
1757!--          Allocate variable
1758             ALLOCATE( basal_area_density_f%var(0:basal_area_density_f%nz-1,   &
1759                                                nys:nyn,nxl:nxr) )
1760
1761             CALL get_variable( id_surf, 'bad', basal_area_density_f%var,      &
1762                                nxl, nxr, nys, nyn,                            &
1763                                0,  basal_area_density_f%nz-1 )
1764          ELSE
1765             basal_area_density_f%from_file = .FALSE.
1766          ENDIF
1767
1768!
1769!--       Read root area density - resolved vegetation
1770          IF ( check_existence( var_names, 'root_area_dens_r' ) )  THEN
1771             root_area_density_lad_f%from_file = .TRUE.
1772             CALL get_attribute( id_surf, char_fill,                           &
1773                                 root_area_density_lad_f%fill,                 &
1774                                 .FALSE., 'root_area_dens_r' )
1775!
1776!--          Inquire number of vertical soil layers
1777             CALL get_dimension_length( id_surf,             &
1778                                                   root_area_density_lad_f%nz, &
1779                                                  'zsoil' )
1780!
1781!--          Allocate variable
1782             ALLOCATE( root_area_density_lad_f%var                             &
1783                                         (0:root_area_density_lad_f%nz-1,      &
1784                                          nys:nyn,nxl:nxr) )
1785
1786             CALL get_variable( id_surf, 'root_area_dens_r',                   &
1787                                root_area_density_lad_f%var,                   &
1788                                nxl, nxr, nys, nyn,                            &
1789                                0,  root_area_density_lad_f%nz-1 )
1790          ELSE
1791             root_area_density_lad_f%from_file = .FALSE.
1792          ENDIF
1793!
1794!--       Finally, close input file
1795          CALL close_input_file( id_surf )
1796#endif
1797       ENDIF
1798!
1799!--    Deallocate variable list. Will be re-allocated in case further
1800!--    variables are read from file.
1801       IF ( ALLOCATED( var_names ) )  DEALLOCATE( var_names )
1802!
1803!--    Skip the following if no land-surface or urban-surface module are
1804!--    applied. This case, no one of the following variables is used anyway.
1805       IF (  .NOT. land_surface  .AND.  .NOT. urban_surface )  RETURN
1806
1807#if defined ( __netcdf )
1808!
1809!--    Open file in read-only mode
1810       CALL open_read_file( TRIM( input_file_static ) //                       &
1811                            TRIM( coupling_char ) , id_surf )
1812!
1813!--    Inquire all variable names.
1814!--    This will be used to check whether an optional input variable exist
1815!--    or not.
1816       CALL inquire_num_variables( id_surf, num_vars )
1817
1818       ALLOCATE( var_names(1:num_vars) )
1819       CALL inquire_variable_names( id_surf, var_names )
1820!
1821!--    Read vegetation type and required attributes
1822       IF ( check_existence( var_names, 'vegetation_type' ) )  THEN
1823          vegetation_type_f%from_file = .TRUE.
1824          CALL get_attribute( id_surf, char_fill,                              &
1825                              vegetation_type_f%fill,                          &
1826                              .FALSE., 'vegetation_type' )
1827
1828          ALLOCATE ( vegetation_type_f%var(nys:nyn,nxl:nxr)  )
1829
1830          CALL get_variable( id_surf, 'vegetation_type',                       &
1831                             vegetation_type_f%var, nxl, nxr, nys, nyn )
1832       ELSE
1833          vegetation_type_f%from_file = .FALSE.
1834       ENDIF
1835
1836!
1837!--    Read soil type and required attributes
1838       IF ( check_existence( var_names, 'soil_type' ) )  THEN
1839             soil_type_f%from_file = .TRUE.
1840!
1841!--       Note, lod is currently not on file; skip for the moment
1842!           CALL get_attribute( id_surf, char_lod,                       &
1843!                                      soil_type_f%lod,                  &
1844!                                      .FALSE., 'soil_type' )
1845          CALL get_attribute( id_surf, char_fill,                              &
1846                              soil_type_f%fill,                                &
1847                              .FALSE., 'soil_type' )
1848
1849          IF ( soil_type_f%lod == 1 )  THEN
1850
1851             ALLOCATE ( soil_type_f%var_2d(nys:nyn,nxl:nxr)  )
1852
1853             CALL get_variable( id_surf, 'soil_type', soil_type_f%var_2d,      &
1854                                nxl, nxr, nys, nyn )
1855
1856          ELSEIF ( soil_type_f%lod == 2 )  THEN
1857!
1858!--          Obtain number of soil layers from file.
1859             CALL get_dimension_length( id_surf, nz_soil, 'zsoil' )
1860
1861             ALLOCATE ( soil_type_f%var_3d(0:nz_soil,nys:nyn,nxl:nxr) )
1862
1863             CALL get_variable( id_surf, 'soil_type', soil_type_f%var_3d,      &
1864                                nxl, nxr, nys, nyn, 0, nz_soil )
1865 
1866          ENDIF
1867       ELSE
1868          soil_type_f%from_file = .FALSE.
1869       ENDIF
1870
1871!
1872!--    Read pavement type and required attributes
1873       IF ( check_existence( var_names, 'pavement_type' ) )  THEN
1874          pavement_type_f%from_file = .TRUE.
1875          CALL get_attribute( id_surf, char_fill,                              &
1876                              pavement_type_f%fill, .FALSE.,                   &
1877                              'pavement_type' )
1878
1879          ALLOCATE ( pavement_type_f%var(nys:nyn,nxl:nxr)  )
1880
1881          CALL get_variable( id_surf, 'pavement_type', pavement_type_f%var,    &
1882                             nxl, nxr, nys, nyn )
1883       ELSE
1884          pavement_type_f%from_file = .FALSE.
1885       ENDIF
1886
1887!
1888!--    Read water type and required attributes
1889       IF ( check_existence( var_names, 'water_type' ) )  THEN
1890          water_type_f%from_file = .TRUE.
1891          CALL get_attribute( id_surf, char_fill, water_type_f%fill,           &
1892                              .FALSE., 'water_type' )
1893
1894          ALLOCATE ( water_type_f%var(nys:nyn,nxl:nxr)  )
1895
1896          CALL get_variable( id_surf, 'water_type', water_type_f%var,          &
1897                             nxl, nxr, nys, nyn )
1898
1899       ELSE
1900          water_type_f%from_file = .FALSE.
1901       ENDIF
1902!
1903!--    Read relative surface fractions of vegetation, pavement and water.
1904       IF ( check_existence( var_names, 'surface_fraction' ) )  THEN
1905          surface_fraction_f%from_file = .TRUE.
1906          CALL get_attribute( id_surf, char_fill,                              &
1907                              surface_fraction_f%fill,                         &
1908                              .FALSE., 'surface_fraction' )
1909!
1910!--       Inquire number of surface fractions
1911          CALL get_dimension_length( id_surf,                                  &
1912                                     surface_fraction_f%nf,                    &
1913                                     'nsurface_fraction' )
1914!
1915!--       Allocate dimension array and input array for surface fractions
1916          ALLOCATE( surface_fraction_f%nfracs(0:surface_fraction_f%nf-1) )
1917          ALLOCATE( surface_fraction_f%frac(0:surface_fraction_f%nf-1,         &
1918                                            nys:nyn,nxl:nxr) )
1919!
1920!--       Get dimension of surface fractions
1921          CALL get_variable( id_surf, 'nsurface_fraction',                     &
1922                             surface_fraction_f%nfracs )
1923!
1924!--       Read surface fractions
1925          CALL get_variable( id_surf, 'surface_fraction',                      &
1926                             surface_fraction_f%frac, nxl, nxr, nys, nyn,      &
1927                             0, surface_fraction_f%nf-1 )
1928       ELSE
1929          surface_fraction_f%from_file = .FALSE.
1930       ENDIF
1931!
1932!--    Read building parameters and related information
1933       IF ( check_existence( var_names, 'building_pars' ) )  THEN
1934          building_pars_f%from_file = .TRUE.
1935          CALL get_attribute( id_surf, char_fill,                              &
1936                              building_pars_f%fill,                            &
1937                              .FALSE., 'building_pars' )
1938!
1939!--       Inquire number of building parameters
1940          CALL get_dimension_length( id_surf,                                  &
1941                                      building_pars_f%np,                      &
1942                                      'nbuilding_pars' )
1943!
1944!--       Allocate dimension array and input array for building parameters
1945          ALLOCATE( building_pars_f%pars(0:building_pars_f%np-1) )
1946          ALLOCATE( building_pars_f%pars_xy(0:building_pars_f%np-1,            &
1947                                            nys:nyn,nxl:nxr) )
1948!
1949!--       Get dimension of building parameters
1950          CALL get_variable( id_surf, 'nbuilding_pars',                        &
1951                             building_pars_f%pars )
1952!
1953!--       Read building_pars
1954          CALL get_variable( id_surf, 'building_pars',                         &
1955                             building_pars_f%pars_xy, nxl, nxr, nys, nyn,      &
1956                             0, building_pars_f%np-1 )
1957       ELSE
1958          building_pars_f%from_file = .FALSE.
1959       ENDIF
1960
1961!
1962!--    Read albedo type and required attributes
1963       IF ( check_existence( var_names, 'albedo_type' ) )  THEN
1964          albedo_type_f%from_file = .TRUE.
1965          CALL get_attribute( id_surf, char_fill, albedo_type_f%fill,          &
1966                              .FALSE.,  'albedo_type' )
1967
1968          ALLOCATE ( albedo_type_f%var(nys:nyn,nxl:nxr)  )
1969         
1970          CALL get_variable( id_surf, 'albedo_type', albedo_type_f%var,        &
1971                             nxl, nxr, nys, nyn )
1972       ELSE
1973          albedo_type_f%from_file = .FALSE.
1974       ENDIF
1975!
1976!--    Read albedo parameters and related information
1977       IF ( check_existence( var_names, 'albedo_pars' ) )  THEN
1978          albedo_pars_f%from_file = .TRUE.
1979          CALL get_attribute( id_surf, char_fill, albedo_pars_f%fill,          &
1980                              .FALSE., 'albedo_pars' )
1981!
1982!--       Inquire number of albedo parameters
1983          CALL get_dimension_length( id_surf,                                  &
1984                                     albedo_pars_f%np,                         &
1985                                     'nalbedo_pars' )
1986!
1987!--       Allocate dimension array and input array for albedo parameters
1988          ALLOCATE( albedo_pars_f%pars(0:albedo_pars_f%np-1) )
1989          ALLOCATE( albedo_pars_f%pars_xy(0:albedo_pars_f%np-1,                &
1990                                          nys:nyn,nxl:nxr) )
1991!
1992!--       Get dimension of albedo parameters
1993          CALL get_variable( id_surf, 'nalbedo_pars', albedo_pars_f%pars )
1994
1995          CALL get_variable( id_surf, 'albedo_pars', albedo_pars_f%pars_xy,    &
1996                             nxl, nxr, nys, nyn,                               &
1997                             0, albedo_pars_f%np-1 )
1998       ELSE
1999          albedo_pars_f%from_file = .FALSE.
2000       ENDIF
2001
2002!
2003!--    Read pavement parameters and related information
2004       IF ( check_existence( var_names, 'pavement_pars' ) )  THEN
2005          pavement_pars_f%from_file = .TRUE.
2006          CALL get_attribute( id_surf, char_fill,                              &
2007                              pavement_pars_f%fill,                            &
2008                              .FALSE., 'pavement_pars' )
2009!
2010!--       Inquire number of pavement parameters
2011          CALL get_dimension_length( id_surf,                                  &
2012                                     pavement_pars_f%np,                       &
2013                                     'npavement_pars' )
2014!
2015!--       Allocate dimension array and input array for pavement parameters
2016          ALLOCATE( pavement_pars_f%pars(0:pavement_pars_f%np-1) )
2017          ALLOCATE( pavement_pars_f%pars_xy(0:pavement_pars_f%np-1,            &
2018                                            nys:nyn,nxl:nxr) )
2019!
2020!--       Get dimension of pavement parameters
2021          CALL get_variable( id_surf, 'npavement_pars', pavement_pars_f%pars )
2022
2023          CALL get_variable( id_surf, 'pavement_pars', pavement_pars_f%pars_xy,&
2024                             nxl, nxr, nys, nyn,                               &
2025                             0, pavement_pars_f%np-1 )
2026       ELSE
2027          pavement_pars_f%from_file = .FALSE.
2028       ENDIF
2029
2030!
2031!--    Read pavement subsurface parameters and related information
2032       IF ( check_existence( var_names, 'pavement_subsurface_pars' ) )         &
2033       THEN
2034          pavement_subsurface_pars_f%from_file = .TRUE.
2035          CALL get_attribute( id_surf, char_fill,                              &
2036                              pavement_subsurface_pars_f%fill,                 &
2037                              .FALSE., 'pavement_subsurface_pars' )
2038!
2039!--       Inquire number of parameters
2040          CALL get_dimension_length( id_surf,                                  &
2041                                     pavement_subsurface_pars_f%np,            &
2042                                     'npavement_subsurface_pars' )
2043!
2044!--       Inquire number of soil layers
2045          CALL get_dimension_length( id_surf,                                  &
2046                                     pavement_subsurface_pars_f%nz,            &
2047                                     'zsoil' )
2048!
2049!--       Allocate dimension array and input array for pavement parameters
2050          ALLOCATE( pavement_subsurface_pars_f%pars                            &
2051                            (0:pavement_subsurface_pars_f%np-1) )
2052          ALLOCATE( pavement_subsurface_pars_f%pars_xyz                        &
2053                            (0:pavement_subsurface_pars_f%np-1,                &
2054                             0:pavement_subsurface_pars_f%nz-1,                &
2055                             nys:nyn,nxl:nxr) )
2056!
2057!--       Get dimension of pavement parameters
2058          CALL get_variable( id_surf, 'npavement_subsurface_pars',             &
2059                             pavement_subsurface_pars_f%pars )
2060
2061          CALL get_variable( id_surf, 'pavement_subsurface_pars',              &
2062                             pavement_subsurface_pars_f%pars_xyz,              &
2063                             nxl, nxr, nys, nyn,                               &
2064                             0, pavement_subsurface_pars_f%nz-1,               &
2065                             0, pavement_subsurface_pars_f%np-1 )
2066       ELSE
2067          pavement_subsurface_pars_f%from_file = .FALSE.
2068       ENDIF
2069
2070
2071!
2072!--    Read vegetation parameters and related information
2073       IF ( check_existence( var_names, 'vegetation_pars' ) )  THEN
2074          vegetation_pars_f%from_file = .TRUE.
2075          CALL get_attribute( id_surf, char_fill,                              &
2076                              vegetation_pars_f%fill,                          &
2077                              .FALSE.,  'vegetation_pars' )
2078!
2079!--       Inquire number of vegetation parameters
2080          CALL get_dimension_length( id_surf,                                  &
2081                                     vegetation_pars_f%np,                     &
2082                                     'nvegetation_pars' )
2083!
2084!--       Allocate dimension array and input array for surface fractions
2085          ALLOCATE( vegetation_pars_f%pars(0:vegetation_pars_f%np-1) )
2086          ALLOCATE( vegetation_pars_f%pars_xy(0:vegetation_pars_f%np-1,        &
2087                                              nys:nyn,nxl:nxr) )
2088!
2089!--       Get dimension of the parameters
2090          CALL get_variable( id_surf, 'nvegetation_pars',                      &
2091                             vegetation_pars_f%pars )
2092
2093          CALL get_variable( id_surf, 'vegetation_pars',                       &
2094                             vegetation_pars_f%pars_xy, nxl, nxr, nys, nyn,    &
2095                             0, vegetation_pars_f%np-1 )
2096       ELSE
2097          vegetation_pars_f%from_file = .FALSE.
2098       ENDIF
2099
2100!
2101!--    Read root parameters/distribution and related information
2102       IF ( check_existence( var_names, 'soil_pars' ) )  THEN
2103          soil_pars_f%from_file = .TRUE.
2104          CALL get_attribute( id_surf, char_fill,                              &
2105                              soil_pars_f%fill,                                &
2106                              .FALSE., 'soil_pars' )
2107
2108          CALL get_attribute( id_surf, char_lod,                               &
2109                              soil_pars_f%lod,                                 &
2110                              .FALSE., 'soil_pars' )
2111
2112!
2113!--       Inquire number of soil parameters
2114          CALL get_dimension_length( id_surf,                                  &
2115                                     soil_pars_f%np,                           &
2116                                     'nsoil_pars' )
2117!
2118!--       Read parameters array
2119          ALLOCATE( soil_pars_f%pars(0:soil_pars_f%np-1) )
2120          CALL get_variable( id_surf, 'nsoil_pars', soil_pars_f%pars )
2121
2122!
2123!--       In case of level of detail 2, also inquire number of vertical
2124!--       soil layers, allocate memory and read the respective dimension
2125          IF ( soil_pars_f%lod == 2 )  THEN
2126             CALL get_dimension_length( id_surf,                               &
2127                                        soil_pars_f%nz,                        &
2128                                        'zsoil' )
2129
2130             ALLOCATE( soil_pars_f%layers(0:soil_pars_f%nz-1) )
2131             CALL get_variable( id_surf, 'zsoil', soil_pars_f%layers )
2132
2133          ENDIF
2134
2135!
2136!--       Read soil parameters, depending on level of detail
2137          IF ( soil_pars_f%lod == 1 )  THEN
2138             ALLOCATE( soil_pars_f%pars_xy(0:soil_pars_f%np-1,                 &
2139                                           nys:nyn,nxl:nxr) )
2140                 
2141             CALL get_variable( id_surf, 'soil_pars', soil_pars_f%pars_xy,     &
2142                                nxl, nxr, nys, nyn, 0, soil_pars_f%np-1 )
2143
2144          ELSEIF ( soil_pars_f%lod == 2 )  THEN
2145             ALLOCATE( soil_pars_f%pars_xyz(0:soil_pars_f%np-1,                &
2146                                            0:soil_pars_f%nz-1,                &
2147                                            nys:nyn,nxl:nxr) )
2148             CALL get_variable( id_surf, 'soil_pars',                          &
2149                                soil_pars_f%pars_xyz,                          &
2150                                nxl, nxr, nys, nyn, 0, soil_pars_f%nz-1,       &
2151                                0, soil_pars_f%np-1 )
2152
2153          ENDIF
2154       ELSE
2155          soil_pars_f%from_file = .FALSE.
2156       ENDIF
2157
2158!
2159!--    Read water parameters and related information
2160       IF ( check_existence( var_names, 'water_pars' ) )  THEN
2161          water_pars_f%from_file = .TRUE.
2162          CALL get_attribute( id_surf, char_fill,                              &
2163                              water_pars_f%fill,                               &
2164                              .FALSE., 'water_pars' )
2165!
2166!--       Inquire number of water parameters
2167          CALL get_dimension_length( id_surf,                                  &
2168                                     water_pars_f%np,                          &
2169                                     'nwater_pars' )
2170!
2171!--       Allocate dimension array and input array for water parameters
2172          ALLOCATE( water_pars_f%pars(0:water_pars_f%np-1) )
2173          ALLOCATE( water_pars_f%pars_xy(0:water_pars_f%np-1,                  &
2174                                         nys:nyn,nxl:nxr) )
2175!
2176!--       Get dimension of water parameters
2177          CALL get_variable( id_surf, 'nwater_pars', water_pars_f%pars )
2178
2179          CALL get_variable( id_surf, 'water_pars', water_pars_f%pars_xy,      &
2180                             nxl, nxr, nys, nyn, 0, water_pars_f%np-1 )
2181       ELSE
2182          water_pars_f%from_file = .FALSE.
2183       ENDIF
2184!
2185!--    Read root area density - parametrized vegetation
2186       IF ( check_existence( var_names, 'root_area_dens_s' ) )  THEN
2187          root_area_density_lsm_f%from_file = .TRUE.
2188          CALL get_attribute( id_surf, char_fill,                              &
2189                              root_area_density_lsm_f%fill,                    &
2190                              .FALSE., 'root_area_dens_s' )
2191!
2192!--       Obtain number of soil layers from file and allocate variable
2193          CALL get_dimension_length( id_surf,                                  &
2194                                     root_area_density_lsm_f%nz,               &
2195                                     'zsoil' )
2196          ALLOCATE( root_area_density_lsm_f%var                                &
2197                                        (0:root_area_density_lsm_f%nz-1,       &
2198                                         nys:nyn,nxl:nxr) )
2199
2200!
2201!--       Read root-area density
2202          CALL get_variable( id_surf, 'root_area_dens_s',                      &
2203                             root_area_density_lsm_f%var,                      &
2204                             nxl, nxr, nys, nyn,                               &
2205                             0, root_area_density_lsm_f%nz-1 )
2206
2207       ELSE
2208          root_area_density_lsm_f%from_file = .FALSE.
2209       ENDIF
2210!
2211!--    Read street type and street crossing
2212       IF ( check_existence( var_names, 'street_type' ) )  THEN
2213          street_type_f%from_file = .TRUE.
2214          CALL get_attribute( id_surf, char_fill,                              &
2215                              street_type_f%fill, .FALSE.,                     &
2216                              'street_type' )
2217
2218          ALLOCATE ( street_type_f%var(nys:nyn,nxl:nxr)  )
2219         
2220          CALL get_variable( id_surf, 'street_type', street_type_f%var,        &
2221                             nxl, nxr, nys, nyn )
2222       ELSE
2223          street_type_f%from_file = .FALSE.
2224       ENDIF
2225
2226       IF ( check_existence( var_names, 'street_crossing' ) )  THEN
2227          street_crossing_f%from_file = .TRUE.
2228          CALL get_attribute( id_surf, char_fill,                              &
2229                              street_crossing_f%fill, .FALSE.,                 &
2230                              'street_crossing' )
2231
2232          ALLOCATE ( street_crossing_f%var(nys:nyn,nxl:nxr)  )
2233
2234          CALL get_variable( id_surf, 'street_crossing',                       &
2235                             street_crossing_f%var, nxl, nxr, nys, nyn )
2236
2237       ELSE
2238          street_crossing_f%from_file = .FALSE.
2239       ENDIF
2240!
2241!--    Still missing: root_resolved and building_surface_pars.
2242!--    Will be implemented as soon as they are available.
2243
2244!
2245!--    Finally, close input file
2246       CALL close_input_file( id_surf )
2247#endif
2248!
2249!--    End of CPU measurement
2250       CALL cpu_log( log_point_s(82), 'NetCDF input', 'stop' )
2251!
2252!--    Exchange ghost points for surface variables. Therefore, resize
2253!--    variables.
2254       IF ( albedo_type_f%from_file )  THEN
2255          CALL resize_array_2d_int8( albedo_type_f%var, nys, nyn, nxl, nxr )
2256          CALL exchange_horiz_2d_byte( albedo_type_f%var, nys, nyn, nxl, nxr,  &
2257                                       nbgp )
2258       ENDIF
2259       IF ( pavement_type_f%from_file )  THEN
2260          CALL resize_array_2d_int8( pavement_type_f%var, nys, nyn, nxl, nxr )
2261          CALL exchange_horiz_2d_byte( pavement_type_f%var, nys, nyn, nxl, nxr,&
2262                                       nbgp )
2263       ENDIF
2264       IF ( soil_type_f%from_file  .AND.  ALLOCATED( soil_type_f%var_2d ) )  THEN
2265          CALL resize_array_2d_int8( soil_type_f%var_2d, nys, nyn, nxl, nxr )
2266          CALL exchange_horiz_2d_byte( soil_type_f%var_2d, nys, nyn, nxl, nxr, &
2267                                       nbgp )
2268       ENDIF
2269       IF ( vegetation_type_f%from_file )  THEN
2270          CALL resize_array_2d_int8( vegetation_type_f%var, nys, nyn, nxl, nxr )
2271          CALL exchange_horiz_2d_byte( vegetation_type_f%var, nys, nyn, nxl,   &
2272                                       nxr, nbgp )
2273       ENDIF
2274       IF ( water_type_f%from_file )  THEN
2275          CALL resize_array_2d_int8( water_type_f%var, nys, nyn, nxl, nxr )
2276          CALL exchange_horiz_2d_byte( water_type_f%var, nys, nyn, nxl, nxr,   &
2277                                       nbgp )
2278       ENDIF
2279!
2280!--    Exchange ghost points for 3/4-D variables. For the sake of simplicity,
2281!--    loop further dimensions to use 2D exchange routines. Unfortunately this
2282!--    is necessary, else new MPI-data types need to be introduced just for
2283!--    2 variables.
2284       IF ( soil_type_f%from_file  .AND.  ALLOCATED( soil_type_f%var_3d ) )    &
2285       THEN
2286          CALL resize_array_3d_int8( soil_type_f%var_3d, 0, nz_soil,           &
2287                                     nys, nyn, nxl, nxr )
2288          DO  k = 0, nz_soil
2289             CALL exchange_horiz_2d_int(                                       & 
2290                        soil_type_f%var_3d(k,:,:), nys, nyn, nxl, nxr, nbgp )
2291          ENDDO
2292       ENDIF
2293
2294       IF ( surface_fraction_f%from_file )  THEN
2295          CALL resize_array_3d_real( surface_fraction_f%frac,                  &
2296                                     0, surface_fraction_f%nf-1,               &
2297                                     nys, nyn, nxl, nxr )
2298          DO  k = 0, surface_fraction_f%nf-1
2299             CALL exchange_horiz_2d( surface_fraction_f%frac(k,:,:), nbgp )
2300          ENDDO
2301       ENDIF
2302
2303       IF ( building_pars_f%from_file )  THEN         
2304          CALL resize_array_3d_real( building_pars_f%pars_xy,                  &
2305                                     0, building_pars_f%np-1,                  &
2306                                     nys, nyn, nxl, nxr )
2307          DO  k = 0, building_pars_f%np-1
2308             CALL exchange_horiz_2d( building_pars_f%pars_xy(k,:,:), nbgp )
2309          ENDDO
2310       ENDIF
2311
2312       IF ( albedo_pars_f%from_file )  THEN         
2313          CALL resize_array_3d_real( albedo_pars_f%pars_xy,                    &
2314                                     0, albedo_pars_f%np-1,                    &
2315                                     nys, nyn, nxl, nxr )
2316          DO  k = 0, albedo_pars_f%np-1
2317             CALL exchange_horiz_2d( albedo_pars_f%pars_xy(k,:,:), nbgp )
2318          ENDDO
2319       ENDIF
2320
2321       IF ( pavement_pars_f%from_file )  THEN         
2322          CALL resize_array_3d_real( pavement_pars_f%pars_xy,                  &
2323                                     0, pavement_pars_f%np-1,                  &
2324                                     nys, nyn, nxl, nxr )
2325          DO  k = 0, pavement_pars_f%np-1
2326             CALL exchange_horiz_2d( pavement_pars_f%pars_xy(k,:,:), nbgp )
2327          ENDDO
2328       ENDIF
2329
2330       IF ( vegetation_pars_f%from_file )  THEN
2331          CALL resize_array_3d_real( vegetation_pars_f%pars_xy,                &
2332                                     0, vegetation_pars_f%np-1,                &
2333                                     nys, nyn, nxl, nxr )
2334          DO  k = 0, vegetation_pars_f%np-1
2335             CALL exchange_horiz_2d( vegetation_pars_f%pars_xy(k,:,:), nbgp )
2336          ENDDO
2337       ENDIF
2338
2339       IF ( water_pars_f%from_file )  THEN
2340          CALL resize_array_3d_real( water_pars_f%pars_xy,                     &
2341                                     0, water_pars_f%np-1,                     &
2342                                     nys, nyn, nxl, nxr )
2343          DO  k = 0, water_pars_f%np-1
2344             CALL exchange_horiz_2d( water_pars_f%pars_xy(k,:,:), nbgp )
2345          ENDDO
2346       ENDIF
2347
2348       IF ( root_area_density_lsm_f%from_file )  THEN
2349          CALL resize_array_3d_real( root_area_density_lsm_f%var,              &
2350                                     0, root_area_density_lsm_f%nz-1,          &
2351                                     nys, nyn, nxl, nxr )
2352          DO  k = 0, root_area_density_lsm_f%nz-1
2353             CALL exchange_horiz_2d( root_area_density_lsm_f%var(k,:,:), nbgp )
2354          ENDDO
2355       ENDIF
2356
2357       IF ( soil_pars_f%from_file )  THEN
2358          IF ( soil_pars_f%lod == 1 )  THEN
2359         
2360             CALL resize_array_3d_real( soil_pars_f%pars_xy,                   &
2361                                        0, soil_pars_f%np-1,                   &
2362                                        nys, nyn, nxl, nxr )
2363             DO  k = 0, soil_pars_f%np-1
2364                CALL exchange_horiz_2d( soil_pars_f%pars_xy(k,:,:), nbgp )
2365             ENDDO
2366             
2367          ELSEIF ( soil_pars_f%lod == 2 )  THEN
2368             CALL resize_array_4d_real( soil_pars_f%pars_xyz,                  &
2369                                        0, soil_pars_f%np-1,                   &
2370                                        0, soil_pars_f%nz-1,                   &
2371                                        nys, nyn, nxl, nxr )
2372
2373             DO  k2 = 0, soil_pars_f%nz-1
2374                DO  k = 0, soil_pars_f%np-1
2375                   CALL exchange_horiz_2d( soil_pars_f%pars_xyz(k,k2,:,:),     &
2376                                           nbgp )
2377                ENDDO
2378             ENDDO
2379          ENDIF
2380       ENDIF
2381
2382       IF ( pavement_subsurface_pars_f%from_file )  THEN         
2383          CALL resize_array_4d_real( pavement_subsurface_pars_f%pars_xyz,      &
2384                                     0, pavement_subsurface_pars_f%np-1,       &
2385                                     0, pavement_subsurface_pars_f%nz-1,       &
2386                                     nys, nyn, nxl, nxr )
2387
2388          DO  k2 = 0, pavement_subsurface_pars_f%nz-1
2389             DO  k = 0, pavement_subsurface_pars_f%np-1
2390                CALL exchange_horiz_2d(                                        &
2391                           pavement_subsurface_pars_f%pars_xyz(k,k2,:,:), nbgp )
2392             ENDDO
2393          ENDDO
2394       ENDIF
2395
2396    END SUBROUTINE netcdf_data_input_surface_data
2397
2398!------------------------------------------------------------------------------!
2399! Description:
2400! ------------
2401!> Reads uvem lookup table information.
2402!------------------------------------------------------------------------------!
2403    SUBROUTINE netcdf_data_input_uvem
2404       
2405       USE indices,                                                            &
2406           ONLY:  nxl, nxr, nyn, nys
2407
2408       IMPLICIT NONE
2409
2410       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names  !< variable names in static input file
2411
2412
2413       INTEGER(iwp) ::  id_uvem       !< NetCDF id of uvem lookup table input file
2414       INTEGER(iwp) ::  nli = 35      !< dimension length of lookup table in x
2415       INTEGER(iwp) ::  nlj =  9      !< dimension length of lookup table in y
2416       INTEGER(iwp) ::  nlk = 90      !< dimension length of lookup table in z
2417       INTEGER(iwp) ::  num_vars      !< number of variables in netcdf input file
2418!
2419!--    Input via uv exposure model lookup table input
2420       IF ( input_pids_uvem )  THEN
2421
2422#if defined ( __netcdf )
2423!
2424!--       Open file in read-only mode
2425          CALL open_read_file( TRIM( input_file_uvem ) //                    &
2426                               TRIM( coupling_char ), id_uvem )
2427!
2428!--       At first, inquire all variable names.
2429!--       This will be used to check whether an input variable exist or not.
2430          CALL inquire_num_variables( id_uvem, num_vars )
2431!
2432!--       Allocate memory to store variable names and inquire them.
2433          ALLOCATE( var_names(1:num_vars) )
2434          CALL inquire_variable_names( id_uvem, var_names )
2435!
2436!--       uvem integration
2437          IF ( check_existence( var_names, 'int_factors' ) )  THEN
2438             uvem_integration_f%from_file = .TRUE.
2439!
2440!--          Input 2D uvem integration.
2441             ALLOCATE ( uvem_integration_f%var(0:nlj,0:nli)  )
2442             
2443             CALL get_variable( id_uvem, 'int_factors', uvem_integration_f%var, 0, nli, 0, nlj )
2444          ELSE
2445             uvem_integration_f%from_file = .FALSE.
2446          ENDIF
2447!
2448!--       uvem irradiance
2449          IF ( check_existence( var_names, 'irradiance' ) )  THEN
2450             uvem_irradiance_f%from_file = .TRUE.
2451!
2452!--          Input 2D uvem irradiance.
2453             ALLOCATE ( uvem_irradiance_f%var(0:nlk, 0:2)  )
2454             
2455             CALL get_variable( id_uvem, 'irradiance', uvem_irradiance_f%var, 0, 2, 0, nlk )
2456          ELSE
2457             uvem_irradiance_f%from_file = .FALSE.
2458          ENDIF
2459!
2460!--       uvem porjection areas
2461          IF ( check_existence( var_names, 'projarea' ) )  THEN
2462             uvem_projarea_f%from_file = .TRUE.
2463!
2464!--          Input 3D uvem projection area (human geometgry)
2465             ALLOCATE ( uvem_projarea_f%var(0:2,0:nlj,0:nli)  )
2466           
2467             CALL get_variable( id_uvem, 'projarea', uvem_projarea_f%var, 0, nli, 0, nlj, 0, 2 )
2468          ELSE
2469             uvem_projarea_f%from_file = .FALSE.
2470          ENDIF
2471!
2472!--       uvem radiance
2473          IF ( check_existence( var_names, 'radiance' ) )  THEN
2474             uvem_radiance_f%from_file = .TRUE.
2475!
2476!--          Input 3D uvem radiance
2477             ALLOCATE ( uvem_radiance_f%var(0:nlk,0:nlj,0:nli)  )
2478             
2479             CALL get_variable( id_uvem, 'radiance', uvem_radiance_f%var, 0, nli, 0, nlj, 0, nlk )
2480          ELSE
2481             uvem_radiance_f%from_file = .FALSE.
2482          ENDIF
2483!
2484!--       Read building obstruction
2485          IF ( check_existence( var_names, 'obstruction' ) )  THEN
2486             building_obstruction_full%from_file = .TRUE.
2487!--          Input 3D uvem building obstruction
2488              ALLOCATE ( building_obstruction_full%var_3d(0:44,0:2,0:2) )
2489              CALL get_variable( id_uvem, 'obstruction', building_obstruction_full%var_3d,0, 2, 0, 2, 0, 44 )       
2490          ELSE
2491             building_obstruction_full%from_file = .FALSE.
2492          ENDIF
2493!
2494          IF ( check_existence( var_names, 'obstruction' ) )  THEN
2495             building_obstruction_f%from_file = .TRUE.
2496!
2497!--          Input 3D uvem building obstruction
2498             ALLOCATE ( building_obstruction_f%var_3d(0:44,nys:nyn,nxl:nxr) )
2499!
2500             CALL get_variable( id_uvem, 'obstruction', building_obstruction_f%var_3d,      &
2501                                nxl, nxr, nys, nyn, 0, 44 )       
2502          ELSE
2503             building_obstruction_f%from_file = .FALSE.
2504          ENDIF
2505!
2506!--       Close uvem lookup table input file
2507          CALL close_input_file( id_uvem )
2508#else
2509          CONTINUE
2510#endif
2511       ENDIF
2512    END SUBROUTINE netcdf_data_input_uvem
2513
2514!------------------------------------------------------------------------------!
2515! Description:
2516! ------------
2517!> Reads orography and building information.
2518!------------------------------------------------------------------------------!
2519    SUBROUTINE netcdf_data_input_topo
2520
2521       USE control_parameters,                                                 &
2522           ONLY:  message_string, topography
2523
2524       USE grid_variables,                                                     &
2525           ONLY:  dx, dy   
2526           
2527       USE indices,                                                            &
2528           ONLY:  nbgp, nx, nxl, nxr, ny, nyn, nys, nzb
2529
2530
2531       IMPLICIT NONE
2532
2533       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names  !< variable names in static input file
2534
2535
2536       INTEGER(iwp) ::  i             !< running index along x-direction
2537       INTEGER(iwp) ::  ii            !< running index for IO blocks
2538       INTEGER(iwp) ::  id_topo       !< NetCDF id of topograhy input file
2539       INTEGER(iwp) ::  j             !< running index along y-direction
2540       INTEGER(iwp) ::  num_vars      !< number of variables in netcdf input file
2541       INTEGER(iwp) ::  skip_n_rows   !< counting variable to skip rows while reading topography file
2542
2543       REAL(wp) ::  dum           !< dummy variable to skip columns while reading topography file
2544!
2545!--    CPU measurement
2546       CALL cpu_log( log_point_s(83), 'NetCDF/ASCII input topo', 'start' )
2547
2548!
2549!--    Input via palm-input data standard
2550       IF ( input_pids_static )  THEN
2551#if defined ( __netcdf )
2552!
2553!--       Open file in read-only mode
2554          CALL open_read_file( TRIM( input_file_static ) //                    &
2555                               TRIM( coupling_char ), id_topo )
2556!
2557!--       At first, inquire all variable names.
2558!--       This will be used to check whether an  input variable exist
2559!--       or not.
2560          CALL inquire_num_variables( id_topo, num_vars )
2561!
2562!--       Allocate memory to store variable names and inquire them.
2563          ALLOCATE( var_names(1:num_vars) )
2564          CALL inquire_variable_names( id_topo, var_names )
2565!
2566!--       Read x, y - dimensions. Only required for consistency checks.
2567          CALL get_dimension_length( id_topo, dim_static%nx, 'x' )
2568          CALL get_dimension_length( id_topo, dim_static%ny, 'y' )
2569          ALLOCATE( dim_static%x(0:dim_static%nx-1) )
2570          ALLOCATE( dim_static%y(0:dim_static%ny-1) )
2571          CALL get_variable( id_topo, 'x', dim_static%x )
2572          CALL get_variable( id_topo, 'y', dim_static%y )
2573!
2574!--       Check whether dimension size in input file matches the model dimensions
2575          IF ( dim_static%nx-1 /= nx  .OR.  dim_static%ny-1 /= ny )  THEN
2576             message_string = 'Static input file: horizontal dimension in ' // &
2577                              'x- and/or y-direction ' //                      &
2578                              'do not match the respective model dimension'
2579             CALL message( 'netcdf_data_input_mod', 'PA0548', 1, 2, 0, 6, 0 )
2580          ENDIF
2581!
2582!--       Check if grid spacing of provided input data matches the respective
2583!--       grid spacing in the model.
2584          IF ( ABS( dim_static%x(1) - dim_static%x(0) - dx ) > 10E-6_wp  .OR.  &
2585               ABS( dim_static%y(1) - dim_static%y(0) - dy ) > 10E-6_wp )  THEN
2586             message_string = 'Static input file: horizontal grid spacing ' // &
2587                              'in x- and/or y-direction ' //                   &
2588                              'do not match the respective model grid spacing.'
2589             CALL message( 'netcdf_data_input_mod', 'PA0549', 1, 2, 0, 6, 0 )
2590          ENDIF
2591!
2592!--       Terrain height. First, get variable-related _FillValue attribute
2593          IF ( check_existence( var_names, 'zt' ) )  THEN
2594             terrain_height_f%from_file = .TRUE.
2595             CALL get_attribute( id_topo, char_fill, terrain_height_f%fill,    &
2596                                 .FALSE., 'zt' )
2597!
2598!--          Input 2D terrain height.
2599             ALLOCATE ( terrain_height_f%var(nys:nyn,nxl:nxr)  )
2600             
2601             CALL get_variable( id_topo, 'zt', terrain_height_f%var,           &
2602                                nxl, nxr, nys, nyn )
2603
2604          ELSE
2605             terrain_height_f%from_file = .FALSE.
2606          ENDIF
2607
2608!
2609!--       Read building height. First, read its _FillValue attribute,
2610!--       as well as lod attribute
2611          buildings_f%from_file = .FALSE.
2612          IF ( check_existence( var_names, 'buildings_2d' ) )  THEN
2613             buildings_f%from_file = .TRUE.
2614             CALL get_attribute( id_topo, char_lod, buildings_f%lod,           &
2615                                 .FALSE., 'buildings_2d' )
2616
2617             CALL get_attribute( id_topo, char_fill, buildings_f%fill1,        &
2618                                 .FALSE., 'buildings_2d' )
2619
2620!
2621!--          Read 2D buildings
2622             IF ( buildings_f%lod == 1 )  THEN
2623                ALLOCATE ( buildings_f%var_2d(nys:nyn,nxl:nxr) )
2624
2625                CALL get_variable( id_topo, 'buildings_2d',                    &
2626                                   buildings_f%var_2d,                         &
2627                                   nxl, nxr, nys, nyn )
2628             ELSE
2629                message_string = 'NetCDF attribute lod ' //                    &
2630                                 '(level of detail) is not set ' //            &
2631                                 'properly for buildings_2d.'
2632                CALL message( 'netcdf_data_input_mod', 'PA0540',               &
2633                               1, 2, 0, 6, 0 )
2634             ENDIF
2635          ENDIF
2636!
2637!--       If available, also read 3D building information. If both are
2638!--       available, use 3D information.
2639          IF ( check_existence( var_names, 'buildings_3d' ) )  THEN
2640             buildings_f%from_file = .TRUE.
2641             CALL get_attribute( id_topo, char_lod, buildings_f%lod,           &
2642                                 .FALSE., 'buildings_3d' )     
2643
2644             CALL get_attribute( id_topo, char_fill, buildings_f%fill2,        &
2645                                 .FALSE., 'buildings_3d' )
2646
2647             CALL get_dimension_length( id_topo, buildings_f%nz, 'z' )
2648!
2649!--          Read 3D buildings
2650             IF ( buildings_f%lod == 2 )  THEN
2651                ALLOCATE( buildings_f%z(nzb:buildings_f%nz-1) )
2652                CALL get_variable( id_topo, 'z', buildings_f%z )
2653
2654                ALLOCATE( buildings_f%var_3d(nzb:buildings_f%nz-1,             &
2655                                             nys:nyn,nxl:nxr) )
2656                buildings_f%var_3d = 0
2657               
2658                CALL get_variable( id_topo, 'buildings_3d',                    &
2659                                   buildings_f%var_3d,                         &
2660                                   nxl, nxr, nys, nyn, 0, buildings_f%nz-1 )
2661             ELSE
2662                message_string = 'NetCDF attribute lod ' //                    &
2663                                 '(level of detail) is not set ' //            &
2664                                 'properly for buildings_3d.'
2665                CALL message( 'netcdf_data_input_mod', 'PA0541',               &
2666                               1, 2, 0, 6, 0 )
2667             ENDIF
2668          ENDIF
2669!
2670!--       Read building IDs and its FillValue attribute. Further required
2671!--       for mapping buildings on top of orography.
2672          IF ( check_existence( var_names, 'building_id' ) )  THEN
2673             building_id_f%from_file = .TRUE.
2674             CALL get_attribute( id_topo, char_fill,                           &
2675                                 building_id_f%fill, .FALSE.,                  &
2676                                 'building_id' )
2677
2678             ALLOCATE ( building_id_f%var(nys:nyn,nxl:nxr) )
2679             
2680             CALL get_variable( id_topo, 'building_id', building_id_f%var,     &
2681                                nxl, nxr, nys, nyn )
2682          ELSE
2683             building_id_f%from_file = .FALSE.
2684          ENDIF
2685!
2686!--       Read building_type and required attributes.
2687          IF ( check_existence( var_names, 'building_type' ) )  THEN
2688             building_type_f%from_file = .TRUE.
2689             CALL get_attribute( id_topo, char_fill,                           &
2690                                 building_type_f%fill, .FALSE.,                &
2691                                 'building_type' )
2692
2693             ALLOCATE ( building_type_f%var(nys:nyn,nxl:nxr) )
2694
2695             CALL get_variable( id_topo, 'building_type', building_type_f%var, &
2696                                nxl, nxr, nys, nyn )
2697
2698          ELSE
2699             building_type_f%from_file = .FALSE.
2700          ENDIF
2701!
2702!--       Close topography input file
2703          CALL close_input_file( id_topo )
2704#else
2705          CONTINUE
2706#endif
2707!
2708!--    ASCII input
2709       ELSEIF ( TRIM( topography ) == 'read_from_file' )  THEN
2710             
2711          DO  ii = 0, io_blocks-1
2712             IF ( ii == io_group )  THEN
2713
2714                OPEN( 90, FILE='TOPOGRAPHY_DATA'//TRIM( coupling_char ),       &
2715                      STATUS='OLD', FORM='FORMATTED', ERR=10 )
2716!
2717!--             Read topography PE-wise. Rows are read from nyn to nys, columns
2718!--             are read from nxl to nxr. At first, ny-nyn rows need to be skipped.
2719                skip_n_rows = 0
2720                DO WHILE ( skip_n_rows < ny - nyn )
2721                   READ( 90, * )
2722                   skip_n_rows = skip_n_rows + 1
2723                ENDDO
2724!
2725!--             Read data from nyn to nys and nxl to nxr. Therefore, skip
2726!--             column until nxl-1 is reached
2727                ALLOCATE ( buildings_f%var_2d(nys:nyn,nxl:nxr) )
2728                DO  j = nyn, nys, -1
2729                   READ( 90, *, ERR=11, END=11 )                               &
2730                                   ( dum, i = 0, nxl-1 ),                      &
2731                                   ( buildings_f%var_2d(j,i), i = nxl, nxr )
2732                ENDDO
2733
2734                GOTO 12
2735
2736 10             message_string = 'file TOPOGRAPHY_DATA'//                      &
2737                                 TRIM( coupling_char )// ' does not exist'
2738                CALL message( 'netcdf_data_input_mod', 'PA0208', 1, 2, 0, 6, 0 )
2739
2740 11             message_string = 'errors in file TOPOGRAPHY_DATA'//            &
2741                                 TRIM( coupling_char )
2742                CALL message( 'netcdf_data_input_mod', 'PA0209', 2, 2, 0, 6, 0 )
2743
2744 12             CLOSE( 90 )
2745                buildings_f%from_file = .TRUE.
2746
2747             ENDIF
2748#if defined( __parallel )
2749             CALL MPI_BARRIER( comm2d, ierr )
2750#endif
2751          ENDDO
2752
2753       ENDIF
2754!
2755!--    End of CPU measurement
2756       CALL cpu_log( log_point_s(83), 'NetCDF/ASCII input topo', 'stop' )
2757!
2758!--    Check for minimum requirement to setup building topography. If buildings
2759!--    are provided, also an ID and a type are required.
2760!--    Note, doing this check in check_parameters
2761!--    will be too late (data will be used for grid inititialization before).
2762       IF ( input_pids_static )  THEN
2763          IF ( buildings_f%from_file  .AND.                                    &
2764               .NOT. building_id_f%from_file )  THEN
2765             message_string = 'If building heights are prescribed in ' //      &
2766                              'static input file, also an ID is required.'
2767             CALL message( 'netcdf_data_input_mod', 'PA0542', 1, 2, 0, 6, 0 )
2768          ENDIF
2769       ENDIF
2770!
2771!--    In case no terrain height is provided by static input file, allocate
2772!--    array nevertheless and set terrain height to 0, which simplifies
2773!--    topography initialization.
2774       IF ( .NOT. terrain_height_f%from_file )  THEN
2775          ALLOCATE ( terrain_height_f%var(nys:nyn,nxl:nxr) )
2776          terrain_height_f%var = 0.0_wp
2777       ENDIF
2778!
2779!--    Finally, exchange 1 ghost point for building ID and type.
2780!--    In case of non-cyclic boundary conditions set Neumann conditions at the
2781!--    lateral boundaries.
2782       IF ( building_id_f%from_file )  THEN
2783          CALL resize_array_2d_int32( building_id_f%var, nys, nyn, nxl, nxr )
2784          CALL exchange_horiz_2d_int( building_id_f%var, nys, nyn, nxl, nxr,   &
2785                                      nbgp )
2786       ENDIF
2787
2788       IF ( building_type_f%from_file )  THEN
2789          CALL resize_array_2d_int8( building_type_f%var, nys, nyn, nxl, nxr )
2790          CALL exchange_horiz_2d_byte( building_type_f%var, nys, nyn, nxl, nxr,   &
2791                                       nbgp )
2792       ENDIF
2793
2794    END SUBROUTINE netcdf_data_input_topo
2795
2796!------------------------------------------------------------------------------!
2797! Description:
2798! ------------
2799!> Reads initialization data of u, v, w, pt, q, geostrophic wind components,
2800!> as well as soil moisture and soil temperature, derived from larger-scale
2801!> model (COSMO) by Inifor.
2802!------------------------------------------------------------------------------!
2803    SUBROUTINE netcdf_data_input_init_3d
2804
2805       USE arrays_3d,                                                          &
2806           ONLY:  q, pt, u, v, w, zu, zw
2807
2808       USE control_parameters,                                                 &
2809           ONLY:  air_chemistry, bc_lr_cyc, bc_ns_cyc, humidity,               &
2810                  message_string, neutral
2811
2812       USE indices,                                                            &
2813           ONLY:  nx, nxl, nxlu, nxr, ny, nyn, nys, nysv, nzb, nz, nzt
2814
2815       IMPLICIT NONE
2816
2817       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names
2818
2819       LOGICAL      ::  dynamic_3d = .TRUE. !< flag indicating that 3D data is read from dynamic file
2820       
2821       INTEGER(iwp) ::  id_dynamic !< NetCDF id of dynamic input file
2822       INTEGER(iwp) ::  n          !< running index for chemistry variables
2823       INTEGER(iwp) ::  num_vars   !< number of variables in netcdf input file
2824
2825       LOGICAL      ::  check_passed !< flag indicating if a check passed
2826
2827!
2828!--    Skip routine if no input file with dynamic input data is available.
2829       IF ( .NOT. input_pids_dynamic )  RETURN
2830!
2831!--    Please note, Inifor is designed to provide initial data for u and v for
2832!--    the prognostic grid points in case of lateral Dirichlet conditions.
2833!--    This means that Inifor provides data from nxlu:nxr (for u) and
2834!--    from nysv:nyn (for v) at the left and south domain boundary, respectively.
2835!--    However, as work-around for the moment, PALM will run with cyclic
2836!--    conditions and will be initialized with data provided by Inifor
2837!--    boundaries in case of Dirichlet.
2838!--    Hence, simply set set nxlu/nysv to 1 (will be reset to its original value
2839!--    at the end of this routine.
2840       IF ( bc_lr_cyc  .AND.  nxl == 0 )  nxlu = 1
2841       IF ( bc_ns_cyc  .AND.  nys == 0 )  nysv = 1
2842
2843!
2844!--    CPU measurement
2845       CALL cpu_log( log_point_s(85), 'NetCDF input init', 'start' )
2846
2847#if defined ( __netcdf )
2848!
2849!--    Open file in read-only mode
2850       CALL open_read_file( TRIM( input_file_dynamic ) //                      &
2851                            TRIM( coupling_char ), id_dynamic )
2852
2853!
2854!--    At first, inquire all variable names.
2855       CALL inquire_num_variables( id_dynamic, num_vars )
2856!
2857!--    Allocate memory to store variable names.
2858       ALLOCATE( var_names(1:num_vars) )
2859       CALL inquire_variable_names( id_dynamic, var_names )
2860!
2861!--    Read vertical dimension of scalar und w grid.
2862       CALL get_dimension_length( id_dynamic, init_3d%nzu, 'z'     )
2863       CALL get_dimension_length( id_dynamic, init_3d%nzw, 'zw'    )
2864!
2865!--    Read also the horizontal dimensions. These are used just used fo
2866!--    checking the compatibility with the PALM grid before reading.
2867       CALL get_dimension_length( id_dynamic, init_3d%nx,  'x'  )
2868       CALL get_dimension_length( id_dynamic, init_3d%nxu, 'xu' )
2869       CALL get_dimension_length( id_dynamic, init_3d%ny,  'y'  )
2870       CALL get_dimension_length( id_dynamic, init_3d%nyv, 'yv' )
2871
2872!
2873!--    Check for correct horizontal and vertical dimension. Please note,
2874!--    checks are performed directly here and not called from
2875!--    check_parameters as some varialbes are still not allocated there.
2876!--    Moreover, please note, u- and v-grid has 1 grid point less on
2877!--    Inifor grid.
2878       IF ( init_3d%nx-1 /= nx  .OR.  init_3d%nxu-1 /= nx - 1  .OR.            &
2879            init_3d%ny-1 /= ny  .OR.  init_3d%nyv-1 /= ny - 1 )  THEN
2880          message_string = 'Number of inifor horizontal grid points  '//       &
2881                           'does not match the number of numeric grid '//      &
2882                           'points.'
2883          CALL message( 'netcdf_data_input_mod', 'PA0543', 1, 2, 0, 6, 0 )
2884       ENDIF
2885
2886       IF ( init_3d%nzu /= nz )  THEN
2887          message_string = 'Number of inifor vertical grid points ' //         &
2888                           'does not match the number of numeric grid '//      &
2889                           'points.'
2890          CALL message( 'netcdf_data_input_mod', 'PA0543', 1, 2, 0, 6, 0 )
2891       ENDIF
2892!
2893!--    Read vertical dimensions. Later, these are required for eventual
2894!--    inter- and extrapolations of the initialization data.
2895       IF ( check_existence( var_names, 'z' ) )  THEN
2896          ALLOCATE( init_3d%zu_atmos(1:init_3d%nzu) )
2897          CALL get_variable( id_dynamic, 'z', init_3d%zu_atmos )
2898       ENDIF
2899       IF ( check_existence( var_names, 'zw' ) )  THEN
2900          ALLOCATE( init_3d%zw_atmos(1:init_3d%nzw) )
2901          CALL get_variable( id_dynamic, 'zw', init_3d%zw_atmos )
2902       ENDIF
2903!
2904!--    Check for consistency between vertical coordinates in dynamic
2905!--    driver and numeric grid.
2906!--    Please note, depending on compiler options both may be
2907!--    equal up to a certain threshold, and differences between
2908!--    the numeric grid and vertical coordinate in the driver can built-
2909!--    up to 10E-1-10E-0 m. For this reason, the check is performed not
2910!--    for exactly matching values.
2911       IF ( ANY( ABS( zu(1:nzt)   - init_3d%zu_atmos(1:init_3d%nzu) )    &
2912                      > 10E-1 )  .OR.                                    &
2913            ANY( ABS( zw(1:nzt-1) - init_3d%zw_atmos(1:init_3d%nzw) )    &
2914                      > 10E-1 ) )  THEN
2915          message_string = 'Vertical grid in dynamic driver does not '// &
2916                           'match the numeric grid.'
2917          CALL message( 'netcdf_data_input_mod', 'PA0543', 1, 2, 0, 6, 0 )
2918       ENDIF
2919!
2920!--    Read initial geostrophic wind components at
2921!--    t = 0 (index 1 in file).
2922       IF ( check_existence( var_names, 'ls_forcing_ug' ) )  THEN
2923          ALLOCATE( init_3d%ug_init(nzb:nzt+1) )
2924          init_3d%ug_init = 0.0_wp
2925
2926          CALL get_variable_pr( id_dynamic, 'ls_forcing_ug', 1,          &
2927                                init_3d%ug_init(1:nzt) )
2928!
2929!--       Set top-boundary condition (Neumann)
2930          init_3d%ug_init(nzt+1) = init_3d%ug_init(nzt)
2931
2932          init_3d%from_file_ug = .TRUE.
2933       ELSE
2934          init_3d%from_file_ug = .FALSE.
2935       ENDIF
2936       IF ( check_existence( var_names, 'ls_forcing_vg' ) )  THEN
2937          ALLOCATE( init_3d%vg_init(nzb:nzt+1) )
2938          init_3d%vg_init = 0.0_wp
2939
2940          CALL get_variable_pr( id_dynamic, 'ls_forcing_vg', 1,          &
2941                                init_3d%vg_init(1:nzt) )
2942!
2943!--       Set top-boundary condition (Neumann)
2944          init_3d%vg_init(nzt+1) = init_3d%vg_init(nzt)
2945
2946          init_3d%from_file_vg = .TRUE.
2947       ELSE
2948          init_3d%from_file_vg = .FALSE.
2949       ENDIF
2950!
2951!--    Read inital 3D data of u, v, w, pt and q,
2952!--    derived from COSMO model. Read PE-wise yz-slices.
2953!--    Please note, the u-, v- and w-component are defined on different
2954!--    grids with one element less in the x-, y-,
2955!--    and z-direction, respectively. Hence, reading is subdivided
2956!--    into separate loops. 
2957!--    Read u-component
2958       IF ( check_existence( var_names, 'init_atmosphere_u' ) )  THEN
2959!
2960!--       Read attributes for the fill value and level-of-detail
2961          CALL get_attribute( id_dynamic, char_fill, init_3d%fill_u,           &
2962                              .FALSE., 'init_atmosphere_u' )
2963          CALL get_attribute( id_dynamic, char_lod, init_3d%lod_u,             &
2964                              .FALSE., 'init_atmosphere_u' )
2965!
2966!--       level-of-detail 1 - read initialization profile
2967          IF ( init_3d%lod_u == 1 )  THEN
2968             ALLOCATE( init_3d%u_init(nzb:nzt+1) )
2969             init_3d%u_init = 0.0_wp
2970
2971             CALL get_variable( id_dynamic, 'init_atmosphere_u',               &
2972                                init_3d%u_init(nzb+1:nzt) )
2973!
2974!--          Set top-boundary condition (Neumann)
2975             init_3d%u_init(nzt+1) = init_3d%u_init(nzt)
2976!
2977!--       level-of-detail 2 - read 3D initialization data
2978          ELSEIF ( init_3d%lod_u == 2 )  THEN
2979             CALL get_variable( id_dynamic, 'init_atmosphere_u',               &
2980                                u(nzb+1:nzt,nys:nyn,nxlu:nxr),                 &
2981                                nxlu, nys+1, nzb+1,                            &
2982                                nxr-nxlu+1, nyn-nys+1, init_3d%nzu,            &
2983                                dynamic_3d )
2984!
2985!--          Set value at leftmost model grid point nxl = 0. This is because
2986!--          Inifor provides data only from 1:nx-1 since it assumes non-cyclic
2987!--          conditions.
2988             IF ( nxl == 0 )                                                   &
2989                u(nzb+1:nzt,nys:nyn,nxl) = u(nzb+1:nzt,nys:nyn,nxlu)
2990!
2991!--          Set bottom and top-boundary
2992             u(nzb,:,:)   = u(nzb+1,:,:)
2993             u(nzt+1,:,:) = u(nzt,:,:)
2994             
2995          ENDIF
2996          init_3d%from_file_u = .TRUE.
2997       ELSE
2998          message_string = 'Missing initial data for u-component'
2999          CALL message( 'netcdf_data_input_mod', 'PA0544', 1, 2, 0, 6, 0 )
3000       ENDIF
3001!
3002!--    Read v-component
3003       IF ( check_existence( var_names, 'init_atmosphere_v' ) )  THEN
3004!
3005!--       Read attributes for the fill value and level-of-detail
3006          CALL get_attribute( id_dynamic, char_fill, init_3d%fill_v,           &
3007                              .FALSE., 'init_atmosphere_v' )
3008          CALL get_attribute( id_dynamic, char_lod, init_3d%lod_v,             &
3009                              .FALSE., 'init_atmosphere_v' )
3010!
3011!--       level-of-detail 1 - read initialization profile
3012          IF ( init_3d%lod_v == 1 )  THEN
3013             ALLOCATE( init_3d%v_init(nzb:nzt+1) )
3014             init_3d%v_init = 0.0_wp
3015
3016             CALL get_variable( id_dynamic, 'init_atmosphere_v',               &
3017                                init_3d%v_init(nzb+1:nzt) )
3018!
3019!--          Set top-boundary condition (Neumann)
3020             init_3d%v_init(nzt+1) = init_3d%v_init(nzt)
3021!
3022!--       level-of-detail 2 - read 3D initialization data
3023          ELSEIF ( init_3d%lod_v == 2 )  THEN
3024         
3025             CALL get_variable( id_dynamic, 'init_atmosphere_v',               &
3026                                v(nzb+1:nzt,nysv:nyn,nxl:nxr),                 &
3027                                nxl+1, nysv, nzb+1,                            &
3028                                nxr-nxl+1, nyn-nysv+1, init_3d%nzu,            &
3029                                dynamic_3d )
3030!
3031!--          Set value at southmost model grid point nys = 0. This is because
3032!--          Inifor provides data only from 1:ny-1 since it assumes non-cyclic
3033!--          conditions.
3034             IF ( nys == 0 )                                                   &
3035                v(nzb+1:nzt,nys,nxl:nxr) = v(nzb+1:nzt,nysv,nxl:nxr)                               
3036!
3037!--          Set bottom and top-boundary
3038             v(nzb,:,:)   = v(nzb+1,:,:)
3039             v(nzt+1,:,:) = v(nzt,:,:)
3040             
3041          ENDIF
3042          init_3d%from_file_v = .TRUE.
3043       ELSE
3044          message_string = 'Missing initial data for v-component'
3045          CALL message( 'netcdf_data_input_mod', 'PA0544', 1, 2, 0, 6, 0 )
3046       ENDIF
3047!
3048!--    Read w-component
3049       IF ( check_existence( var_names, 'init_atmosphere_w' ) )  THEN
3050!
3051!--       Read attributes for the fill value and level-of-detail
3052          CALL get_attribute( id_dynamic, char_fill, init_3d%fill_w,           &
3053                              .FALSE., 'init_atmosphere_w' )
3054          CALL get_attribute( id_dynamic, char_lod, init_3d%lod_w,             &
3055                              .FALSE., 'init_atmosphere_w' )
3056!
3057!--       level-of-detail 1 - read initialization profile
3058          IF ( init_3d%lod_w == 1 )  THEN
3059             ALLOCATE( init_3d%w_init(nzb:nzt+1) )
3060             init_3d%w_init = 0.0_wp
3061
3062             CALL get_variable( id_dynamic, 'init_atmosphere_w',               &
3063                                init_3d%w_init(nzb+1:nzt-1) )
3064!
3065!--          Set top-boundary condition (Neumann)
3066             init_3d%w_init(nzt:nzt+1) = init_3d%w_init(nzt-1)
3067!
3068!--       level-of-detail 2 - read 3D initialization data
3069          ELSEIF ( init_3d%lod_w == 2 )  THEN
3070
3071             CALL get_variable( id_dynamic, 'init_atmosphere_w',                &
3072                                w(nzb+1:nzt-1,nys:nyn,nxl:nxr),                 &
3073                                nxl+1, nys+1, nzb+1,                            &
3074                                nxr-nxl+1, nyn-nys+1, init_3d%nzw,              &
3075                                dynamic_3d )
3076!
3077!--          Set bottom and top-boundary                               
3078             w(nzb,:,:)   = 0.0_wp 
3079             w(nzt,:,:)   = w(nzt-1,:,:)
3080             w(nzt+1,:,:) = w(nzt-1,:,:)
3081
3082          ENDIF
3083          init_3d%from_file_w = .TRUE.
3084       ELSE
3085          message_string = 'Missing initial data for w-component'
3086          CALL message( 'netcdf_data_input_mod', 'PA0544', 1, 2, 0, 6, 0 )
3087       ENDIF
3088!
3089!--    Read potential temperature
3090       IF ( .NOT. neutral )  THEN
3091          IF ( check_existence( var_names, 'init_atmosphere_pt' ) )  THEN
3092!
3093!--          Read attributes for the fill value and level-of-detail
3094             CALL get_attribute( id_dynamic, char_fill, init_3d%fill_pt,       &
3095                                 .FALSE., 'init_atmosphere_pt' )
3096             CALL get_attribute( id_dynamic, char_lod, init_3d%lod_pt,         &
3097                                 .FALSE., 'init_atmosphere_pt' )
3098!
3099!--          level-of-detail 1 - read initialization profile
3100             IF ( init_3d%lod_pt == 1 )  THEN
3101                ALLOCATE( init_3d%pt_init(nzb:nzt+1) )
3102
3103                CALL get_variable( id_dynamic, 'init_atmosphere_pt',           &
3104                                   init_3d%pt_init(nzb+1:nzt) )
3105!
3106!--             Set Neumann top and surface boundary condition for initial
3107!--             profil
3108                init_3d%pt_init(nzb)   = init_3d%pt_init(nzb+1)
3109                init_3d%pt_init(nzt+1) = init_3d%pt_init(nzt)
3110!
3111!--          level-of-detail 2 - read 3D initialization data
3112             ELSEIF ( init_3d%lod_pt == 2 )  THEN
3113
3114                CALL get_variable( id_dynamic, 'init_atmosphere_pt',           &
3115                                   pt(nzb+1:nzt,nys:nyn,nxl:nxr),              &
3116                                   nxl+1, nys+1, nzb+1,                        &
3117                                   nxr-nxl+1, nyn-nys+1, init_3d%nzu,          &
3118                                   dynamic_3d )
3119                                   
3120!
3121!--             Set bottom and top-boundary
3122                pt(nzb,:,:)   = pt(nzb+1,:,:)
3123                pt(nzt+1,:,:) = pt(nzt,:,:)             
3124
3125             ENDIF
3126             init_3d%from_file_pt = .TRUE.
3127          ELSE
3128             message_string = 'Missing initial data for ' //                   &
3129                              'potential temperature'
3130             CALL message( 'netcdf_data_input_mod', 'PA0544', 1, 2, 0, 6, 0 )
3131          ENDIF
3132       ENDIF
3133!
3134!--    Read mixing ratio
3135       IF ( humidity )  THEN
3136          IF ( check_existence( var_names, 'init_atmosphere_qv' ) )  THEN
3137!
3138!--          Read attributes for the fill value and level-of-detail
3139             CALL get_attribute( id_dynamic, char_fill, init_3d%fill_q,        &
3140                                 .FALSE., 'init_atmosphere_qv' )
3141             CALL get_attribute( id_dynamic, char_lod, init_3d%lod_q,          &
3142                                 .FALSE., 'init_atmosphere_qv' )
3143!
3144!--          level-of-detail 1 - read initialization profile
3145             IF ( init_3d%lod_q == 1 )  THEN
3146                ALLOCATE( init_3d%q_init(nzb:nzt+1) )
3147
3148                CALL get_variable( id_dynamic, 'init_atmosphere_qv',           &
3149                                    init_3d%q_init(nzb+1:nzt) )
3150!
3151!--             Set bottom and top boundary condition (Neumann)
3152                init_3d%q_init(nzb)   = init_3d%q_init(nzb+1)
3153                init_3d%q_init(nzt+1) = init_3d%q_init(nzt)
3154!
3155!--          level-of-detail 2 - read 3D initialization data
3156             ELSEIF ( init_3d%lod_q == 2 )  THEN
3157             
3158                CALL get_variable( id_dynamic, 'init_atmosphere_qv',           &
3159                                   q(nzb+1:nzt,nys:nyn,nxl:nxr),               &
3160                                   nxl+1, nys+1, nzb+1,                        &
3161                                   nxr-nxl+1, nyn-nys+1, init_3d%nzu,          &
3162                                   dynamic_3d )
3163                                   
3164!
3165!--             Set bottom and top-boundary
3166                q(nzb,:,:)   = q(nzb+1,:,:)
3167                q(nzt+1,:,:) = q(nzt,:,:)
3168               
3169             ENDIF
3170             init_3d%from_file_q = .TRUE.
3171          ELSE
3172             message_string = 'Missing initial data for ' //                   &
3173                              'mixing ratio'
3174             CALL message( 'netcdf_data_input_mod', 'PA0544', 1, 2, 0, 6, 0 )
3175          ENDIF
3176       ENDIF       
3177!
3178!--    Read chemistry variables.
3179!--    Please note, for the moment, only LOD=1 is allowed
3180       IF ( air_chemistry )  THEN
3181!
3182!--       Allocate chemistry input profiles, as well as arrays for fill values
3183!--       and LOD's.
3184          ALLOCATE( init_3d%chem_init(nzb:nzt+1,                               &
3185                                      1:UBOUND(init_3d%var_names_chem, 1 )) )
3186          ALLOCATE( init_3d%fill_chem(1:UBOUND(init_3d%var_names_chem, 1)) )   
3187          ALLOCATE( init_3d%lod_chem(1:UBOUND(init_3d%var_names_chem, 1))  ) 
3188         
3189          DO  n = 1, UBOUND(init_3d%var_names_chem, 1)
3190             IF ( check_existence( var_names,                                  &
3191                                   TRIM( init_3d%var_names_chem(n) ) ) )  THEN
3192!
3193!--             Read attributes for the fill value and level-of-detail
3194                CALL get_attribute( id_dynamic, char_fill,                     &
3195                                    init_3d%fill_chem(n),                      &
3196                                    .FALSE.,                                   &
3197                                    TRIM( init_3d%var_names_chem(n) ) )
3198                CALL get_attribute( id_dynamic, char_lod,                      &
3199                                    init_3d%lod_chem(n),                       &
3200                                    .FALSE.,                                   &
3201                                    TRIM( init_3d%var_names_chem(n) ) )
3202!
3203!--             Give message that only LOD=1 is allowed.
3204                IF ( init_3d%lod_chem(n) /= 1 )  THEN               
3205                   message_string = 'For chemistry variables only LOD=1 is ' //&
3206                                    'allowed.'
3207                   CALL message( 'netcdf_data_input_mod', 'PA0586',            &
3208                                 1, 2, 0, 6, 0 )
3209                ENDIF
3210!
3211!--             level-of-detail 1 - read initialization profile
3212                CALL get_variable( id_dynamic,                                 &
3213                                   TRIM( init_3d%var_names_chem(n) ),          &
3214                                   init_3d%chem_init(nzb+1:nzt,n) )
3215!
3216!--             Set bottom and top boundary condition (Neumann)
3217                init_3d%chem_init(nzb,n)   = init_3d%chem_init(nzb+1,n)
3218                init_3d%chem_init(nzt+1,n) = init_3d%chem_init(nzt,n)
3219               
3220                init_3d%from_file_chem(n) = .TRUE.
3221             ENDIF
3222          ENDDO
3223       ENDIF
3224!
3225!--    Close input file
3226       CALL close_input_file( id_dynamic )
3227#endif
3228!
3229!--    End of CPU measurement
3230       CALL cpu_log( log_point_s(85), 'NetCDF input init', 'stop' )
3231!
3232!--    Finally, check if the input data has any fill values. Please note,
3233!--    checks depend on the LOD of the input data.
3234       IF ( init_3d%from_file_u )  THEN
3235          check_passed = .TRUE.
3236          IF ( init_3d%lod_u == 1 )  THEN
3237             IF ( ANY( init_3d%u_init(nzb+1:nzt+1) == init_3d%fill_u ) )       &
3238                check_passed = .FALSE.
3239          ELSEIF ( init_3d%lod_u == 2 )  THEN
3240             IF ( ANY( u(nzb+1:nzt+1,nys:nyn,nxlu:nxr) == init_3d%fill_u ) )   &
3241                check_passed = .FALSE.
3242          ENDIF
3243          IF ( .NOT. check_passed )  THEN
3244             message_string = 'NetCDF input for init_atmosphere_u must ' //    &
3245                              'not contain any _FillValues'
3246             CALL message( 'netcdf_data_input_mod', 'PA0545', 2, 2, 0, 6, 0 )
3247          ENDIF
3248       ENDIF
3249
3250       IF ( init_3d%from_file_v )  THEN
3251          check_passed = .TRUE.
3252          IF ( init_3d%lod_v == 1 )  THEN
3253             IF ( ANY( init_3d%v_init(nzb+1:nzt+1) == init_3d%fill_v ) )       &
3254                check_passed = .FALSE.
3255          ELSEIF ( init_3d%lod_v == 2 )  THEN
3256             IF ( ANY( v(nzb+1:nzt+1,nysv:nyn,nxl:nxr) == init_3d%fill_v ) )   &
3257                check_passed = .FALSE.
3258          ENDIF
3259          IF ( .NOT. check_passed )  THEN
3260             message_string = 'NetCDF input for init_atmosphere_v must ' //    &
3261                              'not contain any _FillValues'
3262             CALL message( 'netcdf_data_input_mod', 'PA0545', 2, 2, 0, 6, 0 )
3263          ENDIF
3264       ENDIF
3265
3266       IF ( init_3d%from_file_w )  THEN
3267          check_passed = .TRUE.
3268          IF ( init_3d%lod_w == 1 )  THEN
3269             IF ( ANY( init_3d%w_init(nzb+1:nzt) == init_3d%fill_w ) )         &
3270                check_passed = .FALSE.
3271          ELSEIF ( init_3d%lod_w == 2 )  THEN
3272             IF ( ANY( w(nzb+1:nzt,nys:nyn,nxl:nxr) == init_3d%fill_w ) )      &
3273                check_passed = .FALSE.
3274          ENDIF
3275          IF ( .NOT. check_passed )  THEN
3276             message_string = 'NetCDF input for init_atmosphere_w must ' //    &
3277                              'not contain any _FillValues'
3278             CALL message( 'netcdf_data_input_mod', 'PA0545', 2, 2, 0, 6, 0 )
3279          ENDIF
3280       ENDIF
3281
3282       IF ( init_3d%from_file_pt )  THEN
3283          check_passed = .TRUE.
3284          IF ( init_3d%lod_pt == 1 )  THEN
3285             IF ( ANY( init_3d%pt_init(nzb+1:nzt+1) == init_3d%fill_pt ) )     &
3286                check_passed = .FALSE.
3287          ELSEIF ( init_3d%lod_pt == 2 )  THEN
3288             IF ( ANY( pt(nzb+1:nzt+1,nys:nyn,nxl:nxr) == init_3d%fill_pt ) )  &
3289                check_passed = .FALSE.
3290          ENDIF
3291          IF ( .NOT. check_passed )  THEN
3292             message_string = 'NetCDF input for init_atmosphere_pt must ' //   &
3293                              'not contain any _FillValues'
3294             CALL message( 'netcdf_data_input_mod', 'PA0545', 2, 2, 0, 6, 0 )
3295          ENDIF
3296       ENDIF
3297
3298       IF ( init_3d%from_file_q )  THEN
3299          check_passed = .TRUE.
3300          IF ( init_3d%lod_q == 1 )  THEN
3301             IF ( ANY( init_3d%q_init(nzb+1:nzt+1) == init_3d%fill_q ) )       &
3302                check_passed = .FALSE.
3303          ELSEIF ( init_3d%lod_q == 2 )  THEN
3304             IF ( ANY( q(nzb+1:nzt+1,nys:nyn,nxl:nxr) == init_3d%fill_q ) )    &
3305                check_passed = .FALSE.
3306          ENDIF
3307          IF ( .NOT. check_passed )  THEN
3308             message_string = 'NetCDF input for init_atmosphere_q must ' //    &
3309                              'not contain any _FillValues'
3310             CALL message( 'netcdf_data_input_mod', 'PA0545', 2, 2, 0, 6, 0 )
3311          ENDIF
3312       ENDIF
3313!
3314!--    Workaround for cyclic conditions. Please see above for further explanation.
3315       IF ( bc_lr_cyc  .AND.  nxl == 0 )  nxlu = nxl
3316       IF ( bc_ns_cyc  .AND.  nys == 0 )  nysv = nys
3317
3318    END SUBROUTINE netcdf_data_input_init_3d
3319   
3320!------------------------------------------------------------------------------!
3321! Description:
3322! ------------
3323!> Reads initialization data of u, v, w, pt, q, geostrophic wind components,
3324!> as well as soil moisture and soil temperature, derived from larger-scale
3325!> model (COSMO) by Inifor.
3326!------------------------------------------------------------------------------!
3327    SUBROUTINE netcdf_data_input_init_lsm
3328
3329       USE control_parameters,                                                 &
3330           ONLY:  message_string
3331
3332       USE indices,                                                            &
3333           ONLY:  nx, nxl, nxr, ny, nyn, nys
3334
3335       IMPLICIT NONE
3336
3337       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names !< string containing all variables on file
3338     
3339       INTEGER(iwp) ::  id_dynamic !< NetCDF id of dynamic input file
3340       INTEGER(iwp) ::  num_vars   !< number of variables in netcdf input file
3341
3342!
3343!--    Skip routine if no input file with dynamic input data is available.
3344       IF ( .NOT. input_pids_dynamic )  RETURN
3345!
3346!--    CPU measurement
3347       CALL cpu_log( log_point_s(85), 'NetCDF input init', 'start' )
3348
3349#if defined ( __netcdf )
3350!
3351!--    Open file in read-only mode
3352       CALL open_read_file( TRIM( input_file_dynamic ) //                      &
3353                            TRIM( coupling_char ), id_dynamic )
3354
3355!
3356!--    At first, inquire all variable names.
3357       CALL inquire_num_variables( id_dynamic, num_vars )
3358!
3359!--    Allocate memory to store variable names.
3360       ALLOCATE( var_names(1:num_vars) )
3361       CALL inquire_variable_names( id_dynamic, var_names )
3362!
3363!--    Read vertical dimension for soil depth.
3364       IF ( check_existence( var_names, 'zsoil' ) )                            &
3365          CALL get_dimension_length( id_dynamic, init_3d%nzs, 'zsoil' )
3366!
3367!--    Read also the horizontal dimensions required for soil initialization.
3368!--    Please note, in case of non-nested runs or in case of root domain,
3369!--    these data is already available, but will be read again for the sake
3370!--    of clearness.
3371       CALL get_dimension_length( id_dynamic, init_3d%nx, 'x'  )
3372       CALL get_dimension_length( id_dynamic, init_3d%ny, 'y'  )
3373!
3374!--    Check for correct horizontal and vertical dimension. Please note,
3375!--    in case of non-nested runs or in case of root domain, these checks
3376!--    are already performed
3377       IF ( init_3d%nx-1 /= nx  .OR.  init_3d%ny-1 /= ny )  THEN
3378          message_string = 'Number of inifor horizontal grid points  '//       &
3379                           'does not match the number of numeric grid points.'
3380          CALL message( 'netcdf_data_input_mod', 'PA0543', 1, 2, 0, 6, 0 )
3381       ENDIF
3382!
3383!--    Read vertical dimensions. Later, these are required for eventual
3384!--    inter- and extrapolations of the initialization data.
3385       IF ( check_existence( var_names, 'zsoil' ) )  THEN
3386          ALLOCATE( init_3d%z_soil(1:init_3d%nzs) )
3387          CALL get_variable( id_dynamic, 'zsoil', init_3d%z_soil )
3388       ENDIF
3389!
3390!--    Read initial data for soil moisture
3391       IF ( check_existence( var_names, 'init_soil_m' ) )  THEN
3392!
3393!--       Read attributes for the fill value and level-of-detail
3394          CALL get_attribute( id_dynamic, char_fill,                           &
3395                              init_3d%fill_msoil,                              &
3396                              .FALSE., 'init_soil_m' )
3397          CALL get_attribute( id_dynamic, char_lod,                            &
3398                              init_3d%lod_msoil,                               &
3399                              .FALSE., 'init_soil_m' )
3400!
3401!--       level-of-detail 1 - read initialization profile
3402          IF ( init_3d%lod_msoil == 1 )  THEN
3403             ALLOCATE( init_3d%msoil_1d(0:init_3d%nzs-1) )
3404
3405             CALL get_variable( id_dynamic, 'init_soil_m',                     &
3406                                init_3d%msoil_1d(0:init_3d%nzs-1) )
3407!
3408!--       level-of-detail 2 - read 3D initialization data
3409          ELSEIF ( init_3d%lod_msoil == 2 )  THEN
3410             ALLOCATE ( init_3d%msoil_3d(0:init_3d%nzs-1,nys:nyn,nxl:nxr) )
3411
3412            CALL get_variable( id_dynamic, 'init_soil_m',                      &   
3413                             init_3d%msoil_3d(0:init_3d%nzs-1,nys:nyn,nxl:nxr),&
3414                             nxl, nxr, nys, nyn, 0, init_3d%nzs-1 )
3415
3416          ENDIF
3417          init_3d%from_file_msoil = .TRUE.
3418       ENDIF
3419!
3420!--    Read soil temperature
3421       IF ( check_existence( var_names, 'init_soil_t' ) )  THEN
3422!
3423!--       Read attributes for the fill value and level-of-detail
3424          CALL get_attribute( id_dynamic, char_fill,                           &
3425                              init_3d%fill_tsoil,                              &
3426                              .FALSE., 'init_soil_t' )
3427          CALL get_attribute( id_dynamic, char_lod,                            &
3428                              init_3d%lod_tsoil,                               &
3429                              .FALSE., 'init_soil_t' )
3430!
3431!--       level-of-detail 1 - read initialization profile
3432          IF ( init_3d%lod_tsoil == 1 )  THEN
3433             ALLOCATE( init_3d%tsoil_1d(0:init_3d%nzs-1) )
3434
3435             CALL get_variable( id_dynamic, 'init_soil_t',                     &
3436                                init_3d%tsoil_1d(0:init_3d%nzs-1) )
3437
3438!
3439!--       level-of-detail 2 - read 3D initialization data
3440          ELSEIF ( init_3d%lod_tsoil == 2 )  THEN
3441             ALLOCATE ( init_3d%tsoil_3d(0:init_3d%nzs-1,nys:nyn,nxl:nxr) )
3442             
3443             CALL get_variable( id_dynamic, 'init_soil_t',                     &   
3444                             init_3d%tsoil_3d(0:init_3d%nzs-1,nys:nyn,nxl:nxr),&
3445                             nxl, nxr, nys, nyn, 0, init_3d%nzs-1 )
3446          ENDIF
3447          init_3d%from_file_tsoil = .TRUE.
3448       ENDIF
3449!
3450!--    Close input file
3451       CALL close_input_file( id_dynamic )
3452#endif
3453!
3454!--    End of CPU measurement
3455       CALL cpu_log( log_point_s(85), 'NetCDF input init', 'stop' )
3456
3457    END SUBROUTINE netcdf_data_input_init_lsm   
3458
3459!------------------------------------------------------------------------------!
3460! Description:
3461! ------------
3462!> Checks input file for consistency and minimum requirements.
3463!------------------------------------------------------------------------------!
3464    SUBROUTINE netcdf_data_input_check_dynamic
3465
3466       USE control_parameters,                                                 &
3467           ONLY:  initializing_actions, message_string
3468
3469       IMPLICIT NONE
3470!
3471!--    Dynamic input file must also be present if initialization via inifor is
3472!--    prescribed.
3473       IF ( .NOT. input_pids_dynamic  .AND.                                    &
3474            INDEX( initializing_actions, 'inifor' ) /= 0 )  THEN
3475          message_string = 'initializing_actions = inifor requires dynamic ' //&
3476                           'input file ' // TRIM( input_file_dynamic ) //      &
3477                           TRIM( coupling_char )
3478          CALL message( 'netcdf_data_input_mod', 'PA0547', 1, 2, 0, 6, 0 )
3479       ENDIF
3480
3481    END SUBROUTINE netcdf_data_input_check_dynamic
3482
3483!------------------------------------------------------------------------------!
3484! Description:
3485! ------------
3486!> Checks input file for consistency and minimum requirements.
3487!------------------------------------------------------------------------------!
3488    SUBROUTINE netcdf_data_input_check_static
3489
3490       USE arrays_3d,                                                          &
3491           ONLY:  zu
3492
3493       USE control_parameters,                                                 &
3494           ONLY:  land_surface, message_string, urban_surface
3495
3496       USE indices,                                                            &
3497           ONLY:  nxl, nxr, nyn, nys, wall_flags_0
3498
3499       IMPLICIT NONE
3500
3501       INTEGER(iwp) ::  i      !< loop index along x-direction
3502       INTEGER(iwp) ::  j      !< loop index along y-direction
3503       INTEGER(iwp) ::  n_surf !< number of different surface types at given location
3504
3505       LOGICAL      ::  check_passed !< flag indicating if a check passed
3506
3507!
3508!--    Return if no static input file is available
3509       IF ( .NOT. input_pids_static )  RETURN
3510!
3511!--    Check for correct dimension of surface_fractions, should run from 0-2.
3512       IF ( surface_fraction_f%from_file )  THEN
3513          IF ( surface_fraction_f%nf-1 > 2 )  THEN
3514             message_string = 'nsurface_fraction must not be larger than 3.' 
3515             CALL message( 'netcdf_data_input_mod', 'PA0580', 1, 2, 0, 6, 0 )
3516          ENDIF
3517       ENDIF
3518!
3519!--    Check orography for fill-values. For the moment, give an error message.
3520!--    More advanced methods, e.g. a nearest neighbor algorithm as used in GIS
3521!--    systems might be implemented later.
3522!--    Please note, if no terrain height is provided, it is set to 0.
3523       IF ( ANY( terrain_height_f%var == terrain_height_f%fill ) )  THEN
3524          message_string = 'NetCDF variable zt is not ' //                     &
3525                           'allowed to have missing data'
3526          CALL message( 'netcdf_data_input_mod', 'PA0550', 2, 2, myid, 6, 0 )
3527       ENDIF
3528!
3529!--    Check for negative terrain heights
3530       IF ( ANY( terrain_height_f%var < 0.0_wp ) )  THEN
3531          message_string = 'NetCDF variable zt is not ' //                     &
3532                           'allowed to have negative values'
3533          CALL message( 'netcdf_data_input_mod', 'PA0551', 2, 2, myid, 6, 0 )
3534       ENDIF
3535!
3536!--    If 3D buildings are read, check if building information is consistent
3537!--    to numeric grid.
3538       IF ( buildings_f%from_file )  THEN
3539          IF ( buildings_f%lod == 2 )  THEN
3540             IF ( buildings_f%nz > SIZE( zu ) )  THEN
3541                message_string = 'Reading 3D building data - too much ' //     &
3542                                 'data points along the vertical coordinate.'
3543                CALL message( 'netcdf_data_input_mod', 'PA0552', 2, 2, 0, 6, 0 )
3544             ENDIF
3545
3546             IF ( ANY( ABS( buildings_f%z(0:buildings_f%nz-1) -                &
3547                       zu(0:buildings_f%nz-1) ) > 1E-6_wp ) )  THEN
3548                message_string = 'Reading 3D building data - vertical ' //     &
3549                                 'coordinate do not match numeric grid.'
3550                CALL message( 'netcdf_data_input_mod', 'PA0553', 2, 2, myid, 6, 0 )
3551             ENDIF
3552          ENDIF
3553       ENDIF
3554
3555!
3556!--    Skip further checks concerning buildings and natural surface properties
3557!--    if no urban surface and land surface model are applied.
3558       IF (  .NOT. land_surface  .AND.  .NOT. urban_surface )  RETURN
3559!
3560!--    Check for minimum requirement of surface-classification data in case
3561!--    static input file is used.
3562       IF ( ( .NOT. vegetation_type_f%from_file  .OR.                          &
3563              .NOT. pavement_type_f%from_file    .OR.                          &
3564              .NOT. water_type_f%from_file       .OR.                          &
3565              .NOT. soil_type_f%from_file             ) .OR.                   &
3566             ( urban_surface  .AND.  .NOT. building_type_f%from_file ) )  THEN
3567          message_string = 'Minimum requirement for surface classification ' //&
3568                           'is not fulfilled. At least ' //                    &
3569                           'vegetation_type, pavement_type, ' //               &
3570                           'soil_type and water_type are '//                   &
3571                           'required. If urban-surface model is applied, ' //  &
3572                           'also building_type is required'
3573          CALL message( 'netcdf_data_input_mod', 'PA0554', 1, 2, 0, 6, 0 )
3574       ENDIF
3575!
3576!--    Check for general availability of input variables.
3577!--    If vegetation_type is 0 at any location, vegetation_pars as well as
3578!--    root_area_dens_s are required.
3579       IF ( vegetation_type_f%from_file )  THEN
3580          IF ( ANY( vegetation_type_f%var == 0 ) )  THEN
3581             IF ( .NOT. vegetation_pars_f%from_file )  THEN
3582                message_string = 'If vegetation_type = 0 at any location, ' // &
3583                                 'vegetation_pars is required'
3584                CALL message( 'netcdf_data_input_mod', 'PA0555', 2, 2, -1, 6, 0 )
3585             ENDIF
3586             IF ( .NOT. root_area_density_lsm_f%from_file )  THEN
3587                message_string = 'If vegetation_type = 0 at any location, ' // &
3588                                 'root_area_dens_s is required'
3589                CALL message( 'netcdf_data_input_mod', 'PA0556', 2, 2, myid, 6, 0 )
3590             ENDIF
3591          ENDIF
3592       ENDIF
3593!
3594!--    If soil_type is zero at any location, soil_pars is required.
3595       IF ( soil_type_f%from_file )  THEN
3596          check_passed = .TRUE.
3597          IF ( ALLOCATED( soil_type_f%var_2d ) )  THEN
3598             IF ( ANY( soil_type_f%var_2d == 0 ) )  THEN
3599                IF ( .NOT. soil_pars_f%from_file )  check_passed = .FALSE.
3600             ENDIF
3601          ELSE
3602             IF ( ANY( soil_type_f%var_3d == 0 ) )  THEN
3603                IF ( .NOT. soil_pars_f%from_file )  check_passed = .FALSE.
3604             ENDIF
3605          ENDIF
3606          IF ( .NOT. check_passed )  THEN
3607             message_string = 'If soil_type = 0 at any location, ' //          &
3608                              'soil_pars is required'
3609             CALL message( 'netcdf_data_input_mod', 'PA0557', 2, 2, myid, 6, 0 )
3610          ENDIF
3611       ENDIF
3612!
3613!--    Buildings require a type in case of urban-surface model.
3614       IF ( buildings_f%from_file  .AND.  .NOT. building_type_f%from_file  )  THEN
3615          message_string = 'If buildings are provided, also building_type ' // &
3616                           'is required'
3617          CALL message( 'netcdf_data_input_mod', 'PA0581', 2, 2, myid, 6, 0 )
3618       ENDIF
3619!
3620!--    Buildings require an ID.
3621       IF ( buildings_f%from_file  .AND.  .NOT. building_id_f%from_file  )  THEN
3622          message_string = 'If buildings are provided, also building_id ' //   &
3623                           'is required'
3624          CALL message( 'netcdf_data_input_mod', 'PA0582', 2, 2, myid, 6, 0 )
3625       ENDIF
3626!
3627!--    If building_type is zero at any location, building_pars is required.
3628       IF ( building_type_f%from_file )  THEN
3629          IF ( ANY( building_type_f%var == 0 ) )  THEN
3630             IF ( .NOT. building_pars_f%from_file )  THEN
3631                message_string = 'If building_type = 0 at any location, ' //   &
3632                                 'building_pars is required'
3633                CALL message( 'netcdf_data_input_mod', 'PA0558', 2, 2, myid, 6, 0 )
3634             ENDIF
3635          ENDIF
3636       ENDIF
3637!
3638!--    If building_type is provided, also building_id is needed (due to the
3639!--    filtering algorithm).
3640       IF ( building_type_f%from_file  .AND.  .NOT. building_id_f%from_file )  &
3641       THEN
3642          message_string = 'If building_type is provided, also building_id '// &
3643                           'is required'
3644          CALL message( 'netcdf_data_input_mod', 'PA0519', 2, 2, myid, 6, 0 )
3645       ENDIF       
3646!
3647!--    If albedo_type is zero at any location, albedo_pars is required.
3648       IF ( albedo_type_f%from_file )  THEN
3649          IF ( ANY( albedo_type_f%var == 0 ) )  THEN
3650             IF ( .NOT. albedo_pars_f%from_file )  THEN
3651                message_string = 'If albedo_type = 0 at any location, ' //     &
3652                                 'albedo_pars is required'
3653                CALL message( 'netcdf_data_input_mod', 'PA0559', 2, 2, myid, 6, 0 )
3654             ENDIF
3655          ENDIF
3656       ENDIF
3657!
3658!--    If pavement_type is zero at any location, pavement_pars is required.
3659       IF ( pavement_type_f%from_file )  THEN
3660          IF ( ANY( pavement_type_f%var == 0 ) )  THEN
3661             IF ( .NOT. pavement_pars_f%from_file )  THEN
3662                message_string = 'If pavement_type = 0 at any location, ' //   &
3663                                 'pavement_pars is required'
3664                CALL message( 'netcdf_data_input_mod', 'PA0560', 2, 2, myid, 6, 0 )
3665             ENDIF
3666          ENDIF
3667       ENDIF
3668!
3669!--    If pavement_type is zero at any location, also pavement_subsurface_pars
3670!--    is required.
3671       IF ( pavement_type_f%from_file )  THEN
3672          IF ( ANY( pavement_type_f%var == 0 ) )  THEN
3673             IF ( .NOT. pavement_subsurface_pars_f%from_file )  THEN
3674                message_string = 'If pavement_type = 0 at any location, ' //   &
3675                                 'pavement_subsurface_pars is required'
3676                CALL message( 'netcdf_data_input_mod', 'PA0561', 2, 2, myid, 6, 0 )
3677             ENDIF
3678          ENDIF
3679       ENDIF
3680!
3681!--    If water_type is zero at any location, water_pars is required.
3682       IF ( water_type_f%from_file )  THEN
3683          IF ( ANY( water_type_f%var == 0 ) )  THEN
3684             IF ( .NOT. water_pars_f%from_file )  THEN
3685                message_string = 'If water_type = 0 at any location, ' //      &
3686                                 'water_pars is required'
3687                CALL message( 'netcdf_data_input_mod', 'PA0562', 2, 2,myid, 6, 0 )
3688             ENDIF
3689          ENDIF
3690       ENDIF
3691!
3692!--    Check for local consistency of the input data.
3693       DO  i = nxl, nxr
3694          DO  j = nys, nyn
3695!
3696!--          For each (y,x)-location at least one of the parameters
3697!--          vegetation_type, pavement_type, building_type, or water_type
3698!--          must be set to a non­missing value.
3699             IF ( land_surface  .AND.  .NOT. urban_surface )  THEN
3700                IF ( vegetation_type_f%var(j,i) == vegetation_type_f%fill  .AND.&
3701                     pavement_type_f%var(j,i)   == pavement_type_f%fill    .AND.&
3702                     water_type_f%var(j,i)      == water_type_f%fill )  THEN
3703                   WRITE( message_string, * )                                  &
3704                                    'At least one of the parameters '//        &
3705                                    'vegetation_type, pavement_type, '     //  &
3706                                    'or water_type must be set '//             &
3707                                    'to a non-missing value. Grid point: ', j, i
3708                   CALL message( 'netcdf_data_input_mod', 'PA0563', 2, 2, myid, 6, 0 )
3709                ENDIF
3710             ELSEIF ( land_surface  .AND.  urban_surface )  THEN
3711                IF ( vegetation_type_f%var(j,i) == vegetation_type_f%fill  .AND.&
3712                     pavement_type_f%var(j,i)   == pavement_type_f%fill    .AND.&
3713                     building_type_f%var(j,i)   == building_type_f%fill    .AND.&
3714                     water_type_f%var(j,i)      == water_type_f%fill )  THEN
3715                   WRITE( message_string, * )                                  &
3716                                 'At least one of the parameters '//           &
3717                                 'vegetation_type, pavement_type, '  //        &
3718                                 'building_type, or water_type must be set '// &
3719                                 'to a non-missing value. Grid point: ', j, i
3720                   CALL message( 'netcdf_data_input_mod', 'PA0563', 2, 2, myid, 6, 0 )
3721                ENDIF
3722             ENDIF
3723               
3724!
3725!--          Note that a soil_type is required for each location (y,x) where
3726!--          either vegetation_type or pavement_type is a non­missing value.
3727             IF ( ( vegetation_type_f%var(j,i) /= vegetation_type_f%fill  .OR. &
3728                    pavement_type_f%var(j,i)   /= pavement_type_f%fill ) )  THEN
3729                check_passed = .TRUE.
3730                IF ( ALLOCATED( soil_type_f%var_2d ) )  THEN
3731                   IF ( soil_type_f%var_2d(j,i) == soil_type_f%fill )          &
3732                      check_passed = .FALSE.
3733                ELSE
3734                   IF ( ANY( soil_type_f%var_3d(:,j,i) == soil_type_f%fill) )  &
3735                      check_passed = .FALSE.
3736                ENDIF
3737
3738                IF ( .NOT. check_passed )  THEN
3739                   message_string = 'soil_type is required for each '//        &
3740                                 'location (y,x) where vegetation_type or ' // &
3741                                 'pavement_type is a non-missing value.'
3742                   CALL message( 'netcdf_data_input_mod', 'PA0564',            &
3743                                  2, 2, myid, 6, 0 )
3744                ENDIF
3745             ENDIF
3746!
3747!--          Check for consistency of surface fraction. If more than one type
3748!--          is set, surface fraction need to be given and the sum must not
3749!--          be larger than 1.
3750             n_surf = 0
3751             IF ( vegetation_type_f%var(j,i) /= vegetation_type_f%fill )       &
3752                n_surf = n_surf + 1
3753             IF ( water_type_f%var(j,i)      /= water_type_f%fill )            &
3754                n_surf = n_surf + 1
3755             IF ( pavement_type_f%var(j,i)   /= pavement_type_f%fill )         &
3756                n_surf = n_surf + 1
3757
3758             IF ( n_surf > 1 )  THEN
3759                IF ( .NOT. surface_fraction_f%from_file )  THEN
3760                   message_string = 'If more than one surface type is ' //     &
3761                                 'given at a location, surface_fraction ' //   &
3762                                 'must be provided.'
3763                   CALL message( 'netcdf_data_input_mod', 'PA0565',            &
3764                                  2, 2, myid, 6, 0 )
3765                ELSEIF ( ANY ( surface_fraction_f%frac(:,j,i) ==               &
3766                               surface_fraction_f%fill ) )  THEN
3767                   message_string = 'If more than one surface type is ' //     &
3768                                 'given at a location, surface_fraction ' //   &
3769                                 'must be provided.'
3770                   CALL message( 'netcdf_data_input_mod', 'PA0565',            &
3771                                  2, 2, myid, 6, 0 )
3772                ENDIF
3773             ENDIF
3774!
3775!--          Check for further mismatches. e.g. relative fractions exceed 1 or
3776!--          vegetation_type is set but surface vegetation fraction is zero,
3777!--          etc..
3778             IF ( surface_fraction_f%from_file )  THEN
3779!
3780!--             Sum of relative fractions must not exceed 1.
3781                IF ( SUM ( surface_fraction_f%frac(0:2,j,i) ) > 1.0_wp )  THEN
3782                   message_string = 'surface_fraction must not exceed 1'
3783                   CALL message( 'netcdf_data_input_mod', 'PA0566',            &
3784                                  2, 2, myid, 6, 0 )
3785                ENDIF
3786!
3787!--             Relative fraction for a type must not be zero at locations where
3788!--             this type is set.
3789                IF (                                                           &
3790                  ( vegetation_type_f%var(j,i) /= vegetation_type_f%fill  .AND.&
3791                 ( surface_fraction_f%frac(ind_veg_wall,j,i) == 0.0_wp .OR.    &
3792                   surface_fraction_f%frac(ind_veg_wall,j,i) ==                &
3793                                                     surface_fraction_f%fill ) &
3794                  )  .OR.                                                      &
3795                  ( pavement_type_f%var(j,i) /= pavement_type_f%fill     .AND. &
3796                 ( surface_fraction_f%frac(ind_pav_green,j,i) == 0.0_wp .OR.   &
3797                   surface_fraction_f%frac(ind_pav_green,j,i) ==               &
3798                                                     surface_fraction_f%fill ) &
3799                  )  .OR.                                                      &
3800                  ( water_type_f%var(j,i) /= water_type_f%fill           .AND. &
3801                 ( surface_fraction_f%frac(ind_wat_win,j,i) == 0.0_wp .OR.     &
3802                   surface_fraction_f%frac(ind_wat_win,j,i) ==                 &
3803                                                     surface_fraction_f%fill ) &
3804                  ) )  THEN
3805                   WRITE( message_string, * ) 'Mismatch in setting of '     // &
3806                             'surface_fraction. Vegetation-, pavement-, or '// &
3807                             'water surface is given at (i,j) = ( ', i, j,     &
3808                             ' ), but surface fraction is 0 for the given type.'
3809                   CALL message( 'netcdf_data_input_mod', 'PA0567',            &
3810                                  2, 2, myid, 6, 0 )
3811                ENDIF
3812!
3813!--             Relative fraction for a type must not contain non-zero values
3814!--             if this type is not set.
3815                IF (                                                           &
3816                  ( vegetation_type_f%var(j,i) == vegetation_type_f%fill  .AND.&
3817                 ( surface_fraction_f%frac(ind_veg_wall,j,i) /= 0.0_wp .AND.   &
3818                   surface_fraction_f%frac(ind_veg_wall,j,i) /=                &
3819                                                     surface_fraction_f%fill ) &
3820                  )  .OR.                                                      &
3821                  ( pavement_type_f%var(j,i) == pavement_type_f%fill     .AND. &
3822                 ( surface_fraction_f%frac(ind_pav_green,j,i) /= 0.0_wp .AND.  &
3823                   surface_fraction_f%frac(ind_pav_green,j,i) /=               &
3824                                                     surface_fraction_f%fill ) &
3825                  )  .OR.                                                      &
3826                  ( water_type_f%var(j,i) == water_type_f%fill           .AND. &
3827                 ( surface_fraction_f%frac(ind_wat_win,j,i) /= 0.0_wp .AND.    &
3828                   surface_fraction_f%frac(ind_wat_win,j,i) /=                 &
3829                                                     surface_fraction_f%fill ) &
3830                  ) )  THEN
3831                   WRITE( message_string, * ) 'Mismatch in setting of '     // &
3832                             'surface_fraction. Vegetation-, pavement-, or '// &
3833                             'water surface is not given at (i,j) = ( ', i, j, &
3834                             ' ), but surface fraction is not 0 for the ' //   &
3835                             'given type.'
3836                   CALL message( 'netcdf_data_input_mod', 'PA0568',            &
3837                                  2, 2, myid, 6, 0 )
3838                ENDIF
3839             ENDIF
3840!
3841!--          Check vegetation_pars. If vegetation_type is 0, all parameters
3842!--          need to be set, otherwise, single parameters set by
3843!--          vegetation_type can be overwritten.
3844             IF ( vegetation_type_f%from_file )  THEN
3845                IF ( vegetation_type_f%var(j,i) == 0 )  THEN
3846                   IF ( ANY( vegetation_pars_f%pars_xy(:,j,i) ==               &
3847                             vegetation_pars_f%fill ) )  THEN
3848                      message_string = 'If vegetation_type(y,x) = 0, all '  // &
3849                                       'parameters of vegetation_pars at '//   &
3850                                       'this location must be set.'
3851                      CALL message( 'netcdf_data_input_mod', 'PA0569',         &
3852                                     2, 2, myid, 6, 0 )
3853                   ENDIF
3854                ENDIF
3855             ENDIF
3856!
3857!--          Check root distribution. If vegetation_type is 0, all levels must
3858!--          be set.
3859             IF ( vegetation_type_f%from_file )  THEN
3860                IF ( vegetation_type_f%var(j,i) == 0 )  THEN
3861                   IF ( ANY( root_area_density_lsm_f%var(:,j,i) ==             &
3862                             root_area_density_lsm_f%fill ) )  THEN
3863                      message_string = 'If vegetation_type(y,x) = 0, all ' //  &
3864                                       'levels of root_area_dens_s ' //        &
3865                                       'must be set at this location.'
3866                      CALL message( 'netcdf_data_input_mod', 'PA0570',         &
3867                                     2, 2, myid, 6, 0 )
3868                   ENDIF
3869                ENDIF
3870             ENDIF
3871!
3872!--          Check soil parameters. If soil_type is 0, all parameters
3873!--          must be set.
3874             IF ( soil_type_f%from_file )  THEN
3875                check_passed = .TRUE.
3876                IF ( ALLOCATED( soil_type_f%var_2d ) )  THEN
3877                   IF ( soil_type_f%var_2d(j,i) == 0 )  THEN
3878                      IF ( ANY( soil_pars_f%pars_xy(:,j,i) ==                  &
3879                                soil_pars_f%fill ) )  check_passed = .FALSE.
3880                   ENDIF
3881                ELSE
3882                   IF ( ANY( soil_type_f%var_3d(:,j,i) == 0 ) )  THEN
3883                      IF ( ANY( soil_pars_f%pars_xy(:,j,i) ==                  &
3884                                soil_pars_f%fill ) )  check_passed = .FALSE.
3885                   ENDIF
3886                ENDIF
3887                IF ( .NOT. check_passed )  THEN
3888                   message_string = 'If soil_type(y,x) = 0, all levels of '  //&
3889                                    'soil_pars at this location must be set.'
3890                   CALL message( 'netcdf_data_input_mod', 'PA0571',            &
3891                                  2, 2, myid, 6, 0 )
3892                ENDIF
3893             ENDIF
3894
3895!
3896!--          Check building parameters. If building_type is 0, all parameters
3897!--          must be set.
3898             IF ( building_type_f%from_file )  THEN
3899                IF ( building_type_f%var(j,i) == 0 )  THEN
3900                   IF ( ANY( building_pars_f%pars_xy(:,j,i) ==                 &
3901                             building_pars_f%fill ) )  THEN
3902                      message_string = 'If building_type(y,x) = 0, all ' //    &
3903                                       'parameters of building_pars at this '//&
3904                                       'location must be set.'
3905                      CALL message( 'netcdf_data_input_mod', 'PA0572',         &
3906                                     2, 2, myid, 6, 0 )
3907                   ENDIF
3908                ENDIF
3909             ENDIF
3910!
3911!--          Check if building_type is set at each building and vice versa.
3912!--          Please note, buildings are already processed and filtered.
3913!--          For this reason, consistency checks are based on wall_flags_0
3914!--          rather than buildings_f (buildings are represented by bit 6 in
3915!--          wall_flags_0).
3916             IF ( building_type_f%from_file  .AND.  buildings_f%from_file )  THEN
3917                IF ( ANY( BTEST ( wall_flags_0(:,j,i), 6 ) )  .AND.            &
3918                     building_type_f%var(j,i) == building_type_f%fill  .OR.    &
3919               .NOT. ANY( BTEST ( wall_flags_0(:,j,i), 6 ) )  .AND.            &
3920                     building_type_f%var(j,i) /= building_type_f%fill )  THEN
3921                   WRITE( message_string, * ) 'Each location where a ' //      &
3922                                   'building is set requires a type ' //       &
3923                                   '( and vice versa ) in case the ' //        &
3924                                   'urban-surface model is applied. ' //       &
3925                                   'i, j = ', i, j
3926                   CALL message( 'netcdf_data_input_mod', 'PA0573',            &
3927                                  2, 2, myid, 6, 0 )
3928                ENDIF
3929             ENDIF
3930!
3931!--          Check if at each location where a building is present also an ID
3932!--          is set and vice versa.
3933             IF ( buildings_f%from_file )  THEN
3934                IF ( ANY( BTEST ( wall_flags_0(:,j,i), 6 ) )  .AND.           &
3935                     building_id_f%var(j,i) == building_id_f%fill  .OR.       &
3936               .NOT. ANY( BTEST ( wall_flags_0(:,j,i), 6 ) )  .AND.           &
3937                     building_id_f%var(j,i) /= building_id_f%fill )  THEN
3938                   WRITE( message_string, * ) 'Each location where a ' //     &
3939                                   'building is set requires an ID ' //       &
3940                                   '( and vice versa ). i, j = ', i, j
3941                   CALL message( 'netcdf_data_input_mod', 'PA0574',           &
3942                                  2, 2, myid, 6, 0 )
3943                ENDIF
3944             ENDIF
3945!
3946!--          Check if building ID is set where a bulding is defined.
3947             IF ( buildings_f%from_file )  THEN
3948                IF ( ANY( BTEST ( wall_flags_0(:,j,i), 6 ) )  .AND.           &
3949                     building_id_f%var(j,i) == building_id_f%fill )  THEN
3950                   WRITE( message_string, * ) 'Each building grid point '//   &
3951                                              'requires an ID.', i, j
3952                   CALL message( 'netcdf_data_input_mod', 'PA0575',           &
3953                                  2, 2, myid, 6, 0 )
3954                ENDIF
3955             ENDIF
3956!
3957!--          Check albedo parameters. If albedo_type is 0, all parameters
3958!--          must be set.
3959             IF ( albedo_type_f%from_file )  THEN
3960                IF ( albedo_type_f%var(j,i) == 0 )  THEN
3961                   IF ( ANY( albedo_pars_f%pars_xy(:,j,i) ==                   &
3962                             albedo_pars_f%fill ) )  THEN
3963                      message_string = 'If albedo_type(y,x) = 0, all ' //      &
3964                                       'parameters of albedo_pars at this ' // &
3965                                       'location must be set.'
3966                      CALL message( 'netcdf_data_input_mod', 'PA0576',         &
3967                                     2, 2, myid, 6, 0 )
3968                   ENDIF
3969                ENDIF
3970             ENDIF
3971
3972!
3973!--          Check pavement parameters. If pavement_type is 0, all parameters
3974!--          of pavement_pars must be set at this location.
3975             IF ( pavement_type_f%from_file )  THEN
3976                IF ( pavement_type_f%var(j,i) == 0 )  THEN
3977                   IF ( ANY( pavement_pars_f%pars_xy(:,j,i) ==                 &
3978                             pavement_pars_f%fill ) )  THEN
3979                      message_string = 'If pavement_type(y,x) = 0, all ' //    &
3980                                       'parameters of pavement_pars at this '//&
3981                                       'location must be set.'
3982                      CALL message( 'netcdf_data_input_mod', 'PA0577',         &
3983                                     2, 2, myid, 6, 0 )
3984                   ENDIF
3985                ENDIF
3986             ENDIF
3987!
3988!--          Check pavement-subsurface parameters. If pavement_type is 0,
3989!--          all parameters of pavement_subsurface_pars must be set  at this
3990!--          location.
3991             IF ( pavement_type_f%from_file )  THEN
3992                IF ( pavement_type_f%var(j,i) == 0 )  THEN
3993                   IF ( ANY( pavement_subsurface_pars_f%pars_xyz(:,:,j,i) ==   &
3994                             pavement_subsurface_pars_f%fill ) )  THEN
3995                      message_string = 'If pavement_type(y,x) = 0, all ' //    &
3996                                       'parameters of '                  //    &
3997                                       'pavement_subsurface_pars at this '//   &
3998                                       'location must be set.'
3999                      CALL message( 'netcdf_data_input_mod', 'PA0578',         &
4000                                     2, 2, myid, 6, 0 )
4001                   ENDIF
4002                ENDIF
4003             ENDIF
4004
4005!
4006!--          Check water parameters. If water_type is 0, all parameters
4007!--          must be set  at this location.
4008             IF ( water_type_f%from_file )  THEN
4009                IF ( water_type_f%var(j,i) == 0 )  THEN
4010                   IF ( ANY( water_pars_f%pars_xy(:,j,i) ==                    &
4011                             water_pars_f%fill ) )  THEN
4012                      message_string = 'If water_type(y,x) = 0, all ' //       &
4013                                       'parameters of water_pars at this ' //  &
4014                                       'location must be set.'
4015                      CALL message( 'netcdf_data_input_mod', 'PA0579',         &
4016                                     2, 2, myid, 6, 0 )
4017                   ENDIF
4018                ENDIF
4019             ENDIF
4020
4021          ENDDO
4022       ENDDO
4023
4024    END SUBROUTINE netcdf_data_input_check_static
4025
4026!------------------------------------------------------------------------------!
4027! Description:
4028! ------------
4029!> Resize 8-bit 2D Integer array: (nys:nyn,nxl:nxr) -> (nysg:nyng,nxlg:nxrg)
4030!------------------------------------------------------------------------------!
4031    SUBROUTINE resize_array_2d_int8( var, js, je, is, ie )
4032   
4033       IMPLICIT NONE
4034
4035       INTEGER(iwp) ::  je !< upper index bound along y direction
4036       INTEGER(iwp) ::  js !< lower index bound along y direction
4037       INTEGER(iwp) ::  ie !< upper index bound along x direction
4038       INTEGER(iwp) ::  is !< lower index bound along x direction
4039       
4040       INTEGER(KIND=1), DIMENSION(:,:), ALLOCATABLE ::  var     !< treated variable
4041       INTEGER(KIND=1), DIMENSION(:,:), ALLOCATABLE ::  var_tmp !< temporary copy
4042!
4043!--    Allocate temporary variable
4044       ALLOCATE( var_tmp(js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4045!
4046!--    Temporary copy of the variable
4047       var_tmp(js:je,is:ie) = var(js:je,is:ie)
4048!
4049!--    Resize the array
4050       DEALLOCATE( var )
4051       ALLOCATE( var(js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4052!
4053!--    Transfer temporary copy back to original array
4054       var(js:je,is:ie) = var_tmp(js:je,is:ie)
4055
4056    END SUBROUTINE resize_array_2d_int8
4057   
4058!------------------------------------------------------------------------------!
4059! Description:
4060! ------------
4061!> Resize 32-bit 2D Integer array: (nys:nyn,nxl:nxr) -> (nysg:nyng,nxlg:nxrg)
4062!------------------------------------------------------------------------------!
4063    SUBROUTINE resize_array_2d_int32( var, js, je, is, ie )
4064
4065       IMPLICIT NONE
4066       
4067       INTEGER(iwp) ::  je !< upper index bound along y direction
4068       INTEGER(iwp) ::  js !< lower index bound along y direction
4069       INTEGER(iwp) ::  ie !< upper index bound along x direction
4070       INTEGER(iwp) ::  is !< lower index bound along x direction
4071
4072       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  var     !< treated variable
4073       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  var_tmp !< temporary copy
4074!
4075!--    Allocate temporary variable
4076       ALLOCATE( var_tmp(js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4077!
4078!--    Temporary copy of the variable
4079       var_tmp(js:je,is:ie) = var(js:je,is:ie)
4080!
4081!--    Resize the array
4082       DEALLOCATE( var )
4083       ALLOCATE( var(js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4084!
4085!--    Transfer temporary copy back to original array
4086       var(js:je,is:ie) = var_tmp(js:je,is:ie)
4087
4088    END SUBROUTINE resize_array_2d_int32
4089   
4090!------------------------------------------------------------------------------!
4091! Description:
4092! ------------
4093!> Resize 8-bit 3D Integer array: (:,nys:nyn,nxl:nxr) -> (:,nysg:nyng,nxlg:nxrg)
4094!------------------------------------------------------------------------------!
4095    SUBROUTINE resize_array_3d_int8( var, ks, ke, js, je, is, ie )
4096
4097       IMPLICIT NONE
4098
4099       INTEGER(iwp) ::  je !< upper index bound along y direction
4100       INTEGER(iwp) ::  js !< lower index bound along y direction
4101       INTEGER(iwp) ::  ie !< upper index bound along x direction
4102       INTEGER(iwp) ::  is !< lower index bound along x direction
4103       INTEGER(iwp) ::  ke !< upper bound of treated array in z-direction 
4104       INTEGER(iwp) ::  ks !< lower bound of treated array in z-direction 
4105       
4106       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var     !< treated variable
4107       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var_tmp !< temporary copy
4108!
4109!--    Allocate temporary variable
4110       ALLOCATE( var_tmp(ks:ke,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4111!
4112!--    Temporary copy of the variable
4113       var_tmp(ks:ke,js:je,is:ie) = var(ks:ke,js:je,is:ie)
4114!
4115!--    Resize the array
4116       DEALLOCATE( var )
4117       ALLOCATE( var(ks:ke,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4118!
4119!--    Transfer temporary copy back to original array
4120       var(ks:ke,js:je,is:ie) = var_tmp(ks:ke,js:je,is:ie)
4121
4122    END SUBROUTINE resize_array_3d_int8
4123   
4124!------------------------------------------------------------------------------!
4125! Description:
4126! ------------
4127!> Resize 3D Real array: (:,nys:nyn,nxl:nxr) -> (:,nysg:nyng,nxlg:nxrg)
4128!------------------------------------------------------------------------------!
4129    SUBROUTINE resize_array_3d_real( var, ks, ke, js, je, is, ie )
4130
4131       IMPLICIT NONE
4132
4133       INTEGER(iwp) ::  je !< upper index bound along y direction
4134       INTEGER(iwp) ::  js !< lower index bound along y direction
4135       INTEGER(iwp) ::  ie !< upper index bound along x direction
4136       INTEGER(iwp) ::  is !< lower index bound along x direction
4137       INTEGER(iwp) ::  ke !< upper bound of treated array in z-direction 
4138       INTEGER(iwp) ::  ks !< lower bound of treated array in z-direction 
4139       
4140       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  var     !< treated variable
4141       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  var_tmp !< temporary copy
4142!
4143!--    Allocate temporary variable
4144       ALLOCATE( var_tmp(ks:ke,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4145!
4146!--    Temporary copy of the variable
4147       var_tmp(ks:ke,js:je,is:ie) = var(ks:ke,js:je,is:ie)
4148!
4149!--    Resize the array
4150       DEALLOCATE( var )
4151       ALLOCATE( var(ks:ke,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4152!
4153!--    Transfer temporary copy back to original array
4154       var(ks:ke,js:je,is:ie) = var_tmp(ks:ke,js:je,is:ie)
4155
4156    END SUBROUTINE resize_array_3d_real
4157   
4158!------------------------------------------------------------------------------!
4159! Description:
4160! ------------
4161!> Resize 4D Real array: (:,:,nys:nyn,nxl:nxr) -> (:,nysg:nyng,nxlg:nxrg)
4162!------------------------------------------------------------------------------!
4163    SUBROUTINE resize_array_4d_real( var, k1s, k1e, k2s, k2e, js, je, is, ie )
4164
4165       IMPLICIT NONE
4166       
4167       INTEGER(iwp) ::  je  !< upper index bound along y direction
4168       INTEGER(iwp) ::  js  !< lower index bound along y direction
4169       INTEGER(iwp) ::  ie  !< upper index bound along x direction
4170       INTEGER(iwp) ::  is  !< lower index bound along x direction
4171       INTEGER(iwp) ::  k1e !< upper bound of treated array in z-direction 
4172       INTEGER(iwp) ::  k1s !< lower bound of treated array in z-direction
4173       INTEGER(iwp) ::  k2e !< upper bound of treated array along parameter space 
4174       INTEGER(iwp) ::  k2s !< lower bound of treated array along parameter space 
4175       
4176       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  var     !< treated variable
4177       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  var_tmp !< temporary copy
4178!
4179!--    Allocate temporary variable
4180       ALLOCATE( var_tmp(k1s:k1e,k2s:k2e,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4181!
4182!--    Temporary copy of the variable
4183       var_tmp(k1s:k1e,k2s:k2e,js:je,is:ie) = var(k1s:k1e,k2s:k2e,js:je,is:ie)
4184!
4185!--    Resize the array
4186       DEALLOCATE( var )
4187       ALLOCATE( var(k1s:k1e,k2s:k2e,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4188!
4189!--    Transfer temporary copy back to original array
4190       var(k1s:k1e,k2s:k2e,js:je,is:ie) = var_tmp(k1s:k1e,k2s:k2e,js:je,is:ie)
4191
4192    END SUBROUTINE resize_array_4d_real
4193   
4194!------------------------------------------------------------------------------!
4195! Description:
4196! ------------
4197!> Vertical interpolation and extrapolation of 1D variables.
4198!------------------------------------------------------------------------------!
4199    SUBROUTINE netcdf_data_input_interpolate_1d( var, z_grid, z_file)
4200
4201       IMPLICIT NONE
4202
4203       INTEGER(iwp) ::  k       !< running index z-direction file
4204       INTEGER(iwp) ::  kk      !< running index z-direction stretched model grid
4205       INTEGER(iwp) ::  kl      !< lower index bound along z-direction
4206       INTEGER(iwp) ::  ku      !< upper index bound along z-direction
4207
4208       REAL(wp), DIMENSION(:) ::  z_grid                  !< grid levels on numeric grid
4209       REAL(wp), DIMENSION(:) ::  z_file                  !< grid levels on file grid
4210       REAL(wp), DIMENSION(:), INTENT(INOUT) ::  var      !< treated variable
4211       REAL(wp), DIMENSION(:), ALLOCATABLE   ::  var_tmp  !< temporary variable
4212
4213
4214       kl = LBOUND(var,1)
4215       ku = UBOUND(var,1)
4216       ALLOCATE( var_tmp(kl:ku) )
4217
4218       DO  k = kl, ku
4219
4220          kk = MINLOC( ABS( z_file - z_grid(k) ), DIM = 1 )
4221
4222          IF ( kk < ku )  THEN
4223             IF ( z_file(kk) - z_grid(k) <= 0.0_wp )  THEN
4224                var_tmp(k) = var(kk) +                                         &
4225                                       ( var(kk+1)        - var(kk)    ) /     &
4226                                       ( z_file(kk+1)     - z_file(kk) ) *     &
4227                                       ( z_grid(k)        - z_file(kk) )
4228
4229             ELSEIF ( z_file(kk) - z_grid(k) > 0.0_wp )  THEN
4230                var_tmp(k) = var(kk-1) +                                       &
4231                                         ( var(kk)     - var(kk-1)    ) /      &
4232                                         ( z_file(kk)  - z_file(kk-1) ) *      &
4233                                         ( z_grid(k)   - z_file(kk-1) )
4234             ENDIF
4235!
4236!--       Extrapolate
4237          ELSE
4238
4239             var_tmp(k) = var(ku) +   ( var(ku)    - var(ku-1)      ) /        &
4240                                      ( z_file(ku) - z_file(ku-1)   ) *        &
4241                                      ( z_grid(k)  - z_file(ku)     )
4242
4243          ENDIF
4244
4245       ENDDO
4246       var(:) = var_tmp(:)
4247
4248       DEALLOCATE( var_tmp )
4249
4250
4251    END SUBROUTINE netcdf_data_input_interpolate_1d
4252
4253
4254!------------------------------------------------------------------------------!
4255! Description:
4256! ------------
4257!> Vertical interpolation and extrapolation of 1D variables from Inifor grid
4258!> onto Palm grid, where both have same dimension. Please note, the passed
4259!> paramter list in 1D version is different compared to 2D version.
4260!------------------------------------------------------------------------------!
4261    SUBROUTINE netcdf_data_input_interpolate_1d_soil( var, var_file,           &
4262                                                      z_grid, z_file,          &
4263                                                      nzb_var, nzt_var,        &
4264                                                      nzb_file, nzt_file )
4265
4266       IMPLICIT NONE
4267
4268       INTEGER(iwp) ::  k        !< running index z-direction file
4269       INTEGER(iwp) ::  kk       !< running index z-direction stretched model grid
4270       INTEGER(iwp) ::  ku       !< upper index bound along z-direction for varialbe from file
4271       INTEGER(iwp) ::  nzb_var  !< lower bound of final array
4272       INTEGER(iwp) ::  nzt_var  !< upper bound of final array
4273       INTEGER(iwp) ::  nzb_file !< lower bound of file array
4274       INTEGER(iwp) ::  nzt_file !< upper bound of file array
4275
4276!        LOGICAL, OPTIONAL ::  zsoil !< flag indicating reverse z-axis, i.e. zsoil instead of height, e.g. in case of ocean or soil
4277
4278       REAL(wp), DIMENSION(nzb_var:nzt_var)   ::  z_grid   !< grid levels on numeric grid
4279       REAL(wp), DIMENSION(nzb_file:nzt_file) ::  z_file   !< grid levels on file grid
4280       REAL(wp), DIMENSION(nzb_var:nzt_var)   ::  var      !< treated variable
4281       REAL(wp), DIMENSION(nzb_file:nzt_file) ::  var_file !< temporary variable
4282
4283       ku = nzt_file
4284
4285       DO  k = nzb_var, nzt_var
4286!
4287!--       Determine index on Inifor grid which is closest to the actual height
4288          kk = MINLOC( ABS( z_file - z_grid(k) ), DIM = 1 )
4289!
4290!--       If closest index on Inifor grid is smaller than top index,
4291!--       interpolate the data
4292          IF ( kk < nzt_file )  THEN
4293             IF ( z_file(kk) - z_grid(k) <= 0.0_wp )  THEN
4294                var(k) = var_file(kk) + ( var_file(kk+1) - var_file(kk) ) /    &
4295                                        ( z_file(kk+1)   - z_file(kk)   ) *    &
4296                                        ( z_grid(k)      - z_file(kk)   )
4297
4298             ELSEIF ( z_file(kk) - z_grid(k) > 0.0_wp )  THEN
4299                var(k) = var_file(kk-1) + ( var_file(kk) - var_file(kk-1) ) /  &
4300                                          ( z_file(kk)   - z_file(kk-1)   ) *  &
4301                                          ( z_grid(k)    - z_file(kk-1)   )
4302             ENDIF
4303!
4304!--       Extrapolate if actual height is above the highest Inifor level
4305          ELSE
4306             var(k) = var_file(ku) + ( var_file(ku) - var_file(ku-1) ) /       &
4307                                     ( z_file(ku)   - z_file(ku-1)   ) *       &
4308                                     ( z_grid(k)    - z_file(ku)     )
4309
4310          ENDIF
4311
4312       ENDDO
4313
4314    END SUBROUTINE netcdf_data_input_interpolate_1d_soil
4315
4316!------------------------------------------------------------------------------!
4317! Description:
4318! ------------
4319!> Vertical interpolation and extrapolation of 2D variables at lateral boundaries.
4320!------------------------------------------------------------------------------!
4321    SUBROUTINE netcdf_data_input_interpolate_2d( var, z_grid, z_file)
4322
4323       IMPLICIT NONE
4324
4325       INTEGER(iwp) ::  i       !< running index x- or y -direction
4326       INTEGER(iwp) ::  il      !< lower index bound along x- or y-direction
4327       INTEGER(iwp) ::  iu      !< upper index bound along x- or y-direction
4328       INTEGER(iwp) ::  k       !< running index z-direction file
4329       INTEGER(iwp) ::  kk      !< running index z-direction stretched model grid
4330       INTEGER(iwp) ::  kl      !< lower index bound along z-direction
4331       INTEGER(iwp) ::  ku      !< upper index bound along z-direction
4332
4333       REAL(wp), DIMENSION(:) ::  z_grid                  !< grid levels on numeric grid
4334       REAL(wp), DIMENSION(:) ::  z_file                  !< grid levels on file grid
4335       REAL(wp), DIMENSION(:,:), INTENT(INOUT) ::  var    !< treated variable
4336       REAL(wp), DIMENSION(:), ALLOCATABLE   ::  var_tmp  !< temporary variable
4337
4338
4339       il = LBOUND(var,2)
4340       iu = UBOUND(var,2)
4341       kl = LBOUND(var,1)
4342       ku = UBOUND(var,1)
4343       ALLOCATE( var_tmp(kl:ku) )
4344
4345       DO  i = il, iu
4346          DO  k = kl, ku
4347
4348             kk = MINLOC( ABS( z_file - z_grid(k) ), DIM = 1 )
4349
4350             IF ( kk < ku )  THEN
4351                IF ( z_file(kk) - z_grid(k) <= 0.0_wp )  THEN
4352                   var_tmp(k) = var(kk,i) +                                    &
4353                                          ( var(kk+1,i)      - var(kk,i)  ) /  &
4354                                          ( z_file(kk+1)     - z_file(kk) ) *  &
4355                                          ( z_grid(k)        - z_file(kk) )
4356
4357                ELSEIF ( z_file(kk) - z_grid(k) > 0.0_wp )  THEN
4358                   var_tmp(k) = var(kk-1,i) +                                  &
4359                                            ( var(kk,i)   - var(kk-1,i)  ) /   &
4360                                            ( z_file(kk)  - z_file(kk-1) ) *   &
4361                                            ( z_grid(k)   - z_file(kk-1) )
4362                ENDIF
4363!
4364!--          Extrapolate
4365             ELSE
4366
4367                var_tmp(k) = var(ku,i) + ( var(ku,i)  - var(ku-1,i)    ) /     &
4368                                         ( z_file(ku) - z_file(ku-1)   ) *     &
4369                                         ( z_grid(k)  - z_file(ku)     )
4370
4371             ENDIF
4372
4373          ENDDO
4374          var(:,i) = var_tmp(:)
4375
4376       ENDDO
4377
4378       DEALLOCATE( var_tmp )
4379
4380
4381    END SUBROUTINE netcdf_data_input_interpolate_2d
4382
4383!------------------------------------------------------------------------------!
4384! Description:
4385! ------------
4386!> Vertical interpolation and extrapolation of 3D variables.
4387!------------------------------------------------------------------------------!
4388    SUBROUTINE netcdf_data_input_interpolate_3d( var, z_grid, z_file )
4389
4390       IMPLICIT NONE
4391
4392       INTEGER(iwp) ::  i       !< running index x-direction
4393       INTEGER(iwp) ::  il      !< lower index bound along x-direction
4394       INTEGER(iwp) ::  iu      !< upper index bound along x-direction
4395       INTEGER(iwp) ::  j       !< running index y-direction
4396       INTEGER(iwp) ::  jl      !< lower index bound along x-direction
4397       INTEGER(iwp) ::  ju      !< upper index bound along x-direction
4398       INTEGER(iwp) ::  k       !< running index z-direction file
4399       INTEGER(iwp) ::  kk      !< running index z-direction stretched model grid
4400       INTEGER(iwp) ::  kl      !< lower index bound along z-direction
4401       INTEGER(iwp) ::  ku      !< upper index bound along z-direction
4402
4403       REAL(wp), DIMENSION(:) ::  z_grid                      !< grid levels on numeric grid
4404       REAL(wp), DIMENSION(:) ::  z_file                      !< grid levels on file grid
4405       REAL(wp), DIMENSION(:,:,:), INTENT(INOUT) ::  var      !< treated variable
4406       REAL(wp), DIMENSION(:), ALLOCATABLE       ::  var_tmp  !< temporary variable
4407
4408       il = LBOUND(var,3)
4409       iu = UBOUND(var,3)
4410       jl = LBOUND(var,2)
4411       ju = UBOUND(var,2)
4412       kl = LBOUND(var,1)
4413       ku = UBOUND(var,1)
4414
4415       ALLOCATE( var_tmp(kl:ku) )
4416
4417       DO  i = il, iu
4418          DO  j = jl, ju
4419             DO  k = kl, ku
4420
4421                kk = MINLOC( ABS( z_file - z_grid(k) ), DIM = 1 )
4422
4423                IF ( kk < ku )  THEN
4424                   IF ( z_file(kk) - z_grid(k) <= 0.0_wp )  THEN
4425                      var_tmp(k) = var(kk,j,i) +                               &
4426