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

Last change on this file since 4245 was 4245, checked in by pavelkrc, 3 years ago

Merge branch resler into trunk

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