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

Last change on this file since 4280 was 4280, checked in by monakurppa, 6 years ago

Correct recent bugs in salsa_mod and remove chemistry specific stuff in netcdf_data_iput_mod

  • A boundary conditions bug in salsa_mod: set top boundary to its default value (neumann) if nesting is turned off
  • In salsa_nesting_offl_bc, correct fac_dt to apply time_utc_init
  • Remove chemistry specific parts (inside an if clause id==id_emis) in get_variable_4d_to_3d_real and get_variable_5d_to_4d_real
  • Property svn:keywords set to Id
File size: 258.3 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 4280 2019-10-29 14:34:15Z monakurppa $
27! Remove id_emis flags from get_variable_4d_to_3d_real and
28! get_variable_5d_to_4d_real
29!
30! 4258 2019-10-07 13:29:08Z suehring
31! - Migrate input of soil temperature and moisture to land-surface model.
32! - Remove interpolate routines and move the only required subroutine to
33!   land-surface model.
34!
35! 4247 2019-09-30 10:18:24Z pavelkrc
36! Add reading and processing of building_surface_pars
37!
38! 4226 2019-09-10 17:03:24Z suehring
39! - Netcdf input routine for dimension length renamed
40! - Move offline-nesting-specific checks to nesting_offl_mod
41! - Module-specific input of boundary data for offline nesting moved to
42!   nesting_offl_mod
43! - Define module specific data type for offline nesting in nesting_offl_mod
44!
45! 4190 2019-08-27 15:42:37Z suehring
46! type real_1d changed to real_1d_3d
47!
48! 4186 2019-08-23 16:06:14Z suehring
49! Minor formatting adjustments
50!
51! 4182 2019-08-22 15:20:23Z scharf
52! Corrected "Former revisions" section
53!
54! 4178 2019-08-21 11:13:06Z suehring
55! Implement input of external radiation forcing. Therefore, provide public
56! subroutines and variables.
57!
58! 4150 2019-08-08 20:00:47Z suehring
59! Some variables are given the public attribute, in order to call netcdf input
60! from single routines
61!
62! 4125 2019-07-29 13:31:44Z suehring
63! To enable netcdf-parallel access for lateral boundary data (dynamic input),
64! zero number of elements are passed to the respective get_variable routine
65! for non-boundary cores.
66!
67! 4100 2019-07-17 08:11:29Z forkel
68! Made check for input_pids_dynamic and 'inifor' more general
69!
70! 4012 2019-05-31 15:19:05Z monakurppa
71!
72! 3994 2019-05-22 18:08:09Z suehring
73! Remove single location message
74!
75! 3976 2019-05-15 11:02:34Z hellstea
76! Remove unused variables from last commit
77!
78! 3969 2019-05-13 12:14:33Z suehring
79! - clean-up index notations for emission_values to eliminate magic numbers
80! - introduce temporary variable dum_var_5d as well as subroutines
81!   get_var_5d_real and get_var_5d_real_dynamic
82! - remove emission-specific code in generic get_variable routines
83! - in subroutine netcdf_data_input_chemistry_data change netCDF LOD 1
84!   (default) emission_values to the following index order:
85!   z, y, x, species, category
86! - in subroutine netcdf_data_input_chemistry_data
87!   changed netCDF LOD 2 pre-processed emission_values to the following index
88!   order: time, z, y, x, species
89! - in type chem_emis_att_type replace nspec with n_emiss_species
90!   but retained nspec for backward compatibility with salsa_mod. (E.C. Chan)
91!
92! 3961 2019-05-08 16:12:31Z suehring
93! Revise checks for building IDs and types
94!
95! 3943 2019-05-02 09:50:41Z maronga
96! Temporarily disabled some (faulty) checks for static driver.
97!
98! 3942 2019-04-30 13:08:30Z kanani
99! Fix: increase LEN of all NetCDF attribute values (caused crash in
100! netcdf_create_global_atts due to insufficient length)
101!
102! 3941 2019-04-30 09:48:33Z suehring
103! Move check for grid dimension to an earlier point in time when first array
104! is read.
105! Improve checks for building types / IDs with respect to 2D/3D buildings.
106!
107! 3885 2019-04-11 11:29:34Z kanani
108! Changes related to global restructuring of location messages and introduction
109! of additional debug messages
110!
111! 3864 2019-04-05 09:01:56Z monakurppa
112! get_variable_4d_to_3d_real modified to enable read in data of type
113! data(t,y,x,n) one timestep at a time + some routines made public
114!
115! 3855 2019-04-03 10:00:59Z suehring
116! Typo removed
117!
118! 3854 2019-04-02 16:59:33Z suehring
119! Bugfix in one of the checks. Typo removed.
120!
121! 3744 2019-02-15 18:38:58Z suehring
122! Enable mesoscale offline nesting for chemistry variables as well as
123! initialization of chemistry via dynamic input file.
124!
125! 3705 2019-01-29 19:56:39Z suehring
126! Interface for attribute input of 8-bit and 32-bit integer
127!
128! 3704 2019-01-29 19:51:41Z suehring
129! unused variables removed
130!
131! 2696 2017-12-14 17:12:51Z kanani
132! Initial revision (suehring)
133!
134! Authors:
135! --------
136! @author Matthias Suehring
137! @author Edward C. Chan
138! @author Emanuele Russo
139!
140! Description:
141! ------------
142!> Modulue contains routines to input data according to Palm input data
143!> standart using dynamic and static input files.
144!> @todo - Chemistry: revise reading of netcdf file and ajdust formatting
145!>         according to standard!!! (ecc/done)
146!> @todo - Order input alphabetically
147!> @todo - Revise error messages and error numbers
148!> @todo - Input of missing quantities (chemical species, emission rates)
149!> @todo - Defninition and input of still missing variable attributes
150!>         (ecc/what are they?)
151!> @todo - Input of initial geostrophic wind profiles with cyclic conditions.
152!> @todo - remove z dimension from default_emission_data nad preproc_emission_data
153!          and correpsonding subroutines get_var_5d_real and get_var_5d_dynamic (ecc)
154!> @todo - decpreciate chem_emis_att_type@nspec (ecc)
155!> @todo - depreciate subroutines get_variable_4d_to_3d_real and
156!>         get_variable_5d_to_4d_real (ecc)
157!> @todo - introduce useful debug_message(s)
158!------------------------------------------------------------------------------!
159 MODULE netcdf_data_input_mod
160
161    USE control_parameters,                                                    &
162        ONLY:  coupling_char, io_blocks, io_group
163
164    USE cpulog,                                                                &
165        ONLY:  cpu_log, log_point_s
166
167    USE indices,                                                               &
168        ONLY:  nbgp
169
170    USE kinds
171
172#if defined ( __netcdf )
173    USE NETCDF
174#endif
175
176    USE pegrid
177
178    USE surface_mod,                                                           &
179        ONLY:  ind_pav_green, ind_veg_wall, ind_wat_win
180!
181!-- Define type for dimensions.
182    TYPE dims_xy
183       INTEGER(iwp) :: nx                             !< dimension length in x
184       INTEGER(iwp) :: ny                             !< dimension length in y
185       INTEGER(iwp) :: nz                             !< dimension length in z
186       REAL(wp), DIMENSION(:), ALLOCATABLE :: x       !< dimension array in x
187       REAL(wp), DIMENSION(:), ALLOCATABLE :: y       !< dimension array in y
188       REAL(wp), DIMENSION(:), ALLOCATABLE :: z       !< dimension array in z
189    END TYPE dims_xy
190    TYPE init_type
191
192       CHARACTER(LEN=16) ::  init_char = 'init_atmosphere_'          !< leading substring for init variables
193       CHARACTER(LEN=23) ::  origin_time = '2000-01-01 00:00:00 +00' !< reference time of input data
194       CHARACTER(LEN=100), DIMENSION(:), ALLOCATABLE ::  var_names_chem !< list of chemistry variable names that can potentially be on file
195
196       INTEGER(iwp) ::  lod_msoil !< level of detail - soil moisture
197       INTEGER(iwp) ::  lod_pt    !< level of detail - pt
198       INTEGER(iwp) ::  lod_q     !< level of detail - q
199       INTEGER(iwp) ::  lod_tsoil !< level of detail - soil temperature
200       INTEGER(iwp) ::  lod_u     !< level of detail - u-component
201       INTEGER(iwp) ::  lod_v     !< level of detail - v-component
202       INTEGER(iwp) ::  lod_w     !< level of detail - w-component
203       INTEGER(iwp) ::  nx        !< number of scalar grid points along x in dynamic input file
204       INTEGER(iwp) ::  nxu       !< number of u grid points along x in dynamic input file
205       INTEGER(iwp) ::  ny        !< number of scalar grid points along y in dynamic input file
206       INTEGER(iwp) ::  nyv       !< number of v grid points along y in dynamic input file
207       INTEGER(iwp) ::  nzs       !< number of vertical soil levels in dynamic input file
208       INTEGER(iwp) ::  nzu       !< number of vertical levels on scalar grid in dynamic input file
209       INTEGER(iwp) ::  nzw       !< number of vertical levels on w grid in dynamic input file
210       
211       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  lod_chem !< level of detail - chemistry variables
212
213       LOGICAL ::  from_file_msoil  = .FALSE. !< flag indicating whether soil moisture is already initialized from file
214       LOGICAL ::  from_file_pt     = .FALSE. !< flag indicating whether pt is already initialized from file
215       LOGICAL ::  from_file_q      = .FALSE. !< flag indicating whether q is already initialized from file
216       LOGICAL ::  from_file_tsoil  = .FALSE. !< flag indicating whether soil temperature is already initialized from file
217       LOGICAL ::  from_file_u      = .FALSE. !< flag indicating whether u is already initialized from file
218       LOGICAL ::  from_file_ug     = .FALSE. !< flag indicating whether ug is already initialized from file
219       LOGICAL ::  from_file_v      = .FALSE. !< flag indicating whether v is already initialized from file
220       LOGICAL ::  from_file_vg     = .FALSE. !< flag indicating whether ug is already initialized from file
221       LOGICAL ::  from_file_w      = .FALSE. !< flag indicating whether w is already initialized from file
222       
223       LOGICAL, DIMENSION(:), ALLOCATABLE ::  from_file_chem !< flag indicating whether chemistry variable is read from file
224
225       REAL(wp) ::  fill_msoil              !< fill value for soil moisture
226       REAL(wp) ::  fill_pt                 !< fill value for pt
227       REAL(wp) ::  fill_q                  !< fill value for q
228       REAL(wp) ::  fill_tsoil              !< fill value for soil temperature
229       REAL(wp) ::  fill_u                  !< fill value for u
230       REAL(wp) ::  fill_v                  !< fill value for v
231       REAL(wp) ::  fill_w                  !< fill value for w
232       REAL(wp) ::  latitude = 0.0_wp       !< latitude of the lower left corner
233       REAL(wp) ::  longitude = 0.0_wp      !< longitude of the lower left corner
234       REAL(wp) ::  origin_x = 500000.0_wp  !< UTM easting of the lower left corner
235       REAL(wp) ::  origin_y = 0.0_wp       !< UTM northing of the lower left corner
236       REAL(wp) ::  origin_z = 0.0_wp       !< reference height of input data
237       REAL(wp) ::  rotation_angle = 0.0_wp !< rotation angle of input data
238
239       REAL(wp), DIMENSION(:), ALLOCATABLE ::  fill_chem    !< fill value - chemistry variables
240       REAL(wp), DIMENSION(:), ALLOCATABLE ::  msoil_1d     !< initial vertical profile of soil moisture
241       REAL(wp), DIMENSION(:), ALLOCATABLE ::  pt_init      !< initial vertical profile of pt
242       REAL(wp), DIMENSION(:), ALLOCATABLE ::  q_init       !< initial vertical profile of q
243       REAL(wp), DIMENSION(:), ALLOCATABLE ::  tsoil_1d     !< initial vertical profile of soil temperature
244       REAL(wp), DIMENSION(:), ALLOCATABLE ::  u_init       !< initial vertical profile of u
245       REAL(wp), DIMENSION(:), ALLOCATABLE ::  ug_init      !< initial vertical profile of ug
246       REAL(wp), DIMENSION(:), ALLOCATABLE ::  v_init       !< initial vertical profile of v
247       REAL(wp), DIMENSION(:), ALLOCATABLE ::  vg_init      !< initial vertical profile of ug
248       REAL(wp), DIMENSION(:), ALLOCATABLE ::  w_init       !< initial vertical profile of w
249       REAL(wp), DIMENSION(:), ALLOCATABLE ::  z_soil       !< vertical levels in soil in dynamic input file, used for interpolation
250       REAL(wp), DIMENSION(:), ALLOCATABLE ::  zu_atmos     !< vertical levels at scalar grid in dynamic input file, used for interpolation
251       REAL(wp), DIMENSION(:), ALLOCATABLE ::  zw_atmos     !< vertical levels at w grid in dynamic input file, used for interpolation
252       
253       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  chem_init  !< initial vertical profiles of chemistry variables
254
255
256       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  msoil_3d !< initial 3d soil moisture provide by Inifor and interpolated onto soil grid
257       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  tsoil_3d !< initial 3d soil temperature provide by Inifor and interpolated onto soil grid
258
259    END TYPE init_type
260
261!-- Data type for the general information of chemistry emissions, do not dependent on the particular chemical species
262    TYPE chem_emis_att_type 
263
264       !-DIMENSIONS
265       
266       INTEGER(iwp)                                 :: nspec=0            !< no of chem species provided in emission_values
267       INTEGER(iwp)                                 :: n_emiss_species=0  !< no of chem species provided in emission_values
268                                                                          !< same function as nspec, which will be depreciated (ecc)
269                                                                                 
270       INTEGER(iwp)                                 :: ncat=0             !< number of emission categories
271       INTEGER(iwp)                                 :: nvoc=0             !< number of VOC components
272       INTEGER(iwp)                                 :: npm=0              !< number of PM components
273       INTEGER(iwp)                                 :: nnox=2             !< number of NOx components: NO and NO2
274       INTEGER(iwp)                                 :: nsox=2             !< number of SOX components: SO and SO4
275       INTEGER(iwp)                                 :: nhoursyear         !< number of hours of a specific year in the HOURLY mode
276                                                                          !< of the default mode
277       INTEGER(iwp)                                 :: nmonthdayhour      !< number of month days and hours in the MDH mode
278                                                                          !< of the default mode
279       INTEGER(iwp)                                 :: dt_emission        !< Number of emissions timesteps for one year
280                                                                          !< in the pre-processed emissions case
281       !-- 1d emission input variables
282       CHARACTER (LEN=25),ALLOCATABLE, DIMENSION(:) :: pm_name       !< Names of PM components
283       CHARACTER (LEN=25),ALLOCATABLE, DIMENSION(:) :: cat_name      !< Emission category names
284       CHARACTER (LEN=25),ALLOCATABLE, DIMENSION(:) :: species_name  !< Names of emission chemical species
285       CHARACTER (LEN=25),ALLOCATABLE, DIMENSION(:) :: voc_name      !< Names of VOCs components
286       CHARACTER (LEN=25)                           :: units         !< Units
287
288       INTEGER(iwp)                                 :: i_hour         !< indices for assigning emission values at different timesteps
289       INTEGER(iwp),ALLOCATABLE, DIMENSION(:)       :: cat_index      !< Indices for emission categories
290       INTEGER(iwp),ALLOCATABLE, DIMENSION(:)       :: species_index  !< Indices for emission chem species
291
292       REAL(wp),ALLOCATABLE, DIMENSION(:)           :: xm             !< Molecular masses of emission chem species
293
294       !-- 2d emission input variables
295       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: hourly_emis_time_factor  !< Time factors for HOURLY emissions (DEFAULT mode)
296       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: mdh_emis_time_factor     !< Time factors for MDH emissions (DEFAULT mode)
297       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: nox_comp                 !< Composition of NO and NO2
298       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: sox_comp                 !< Composition of SO2 and SO4
299       REAL(wp),ALLOCATABLE, DIMENSION(:,:)         :: voc_comp                 !< Composition of VOC components (not fixed)
300
301       !-- 3d emission input variables
302       REAL(wp),ALLOCATABLE, DIMENSION(:,:,:)       :: pm_comp                  !< Composition of PM components (not fixed)
303 
304    END TYPE chem_emis_att_type
305
306
307!-- Data type for the values of chemistry emissions
308    TYPE chem_emis_val_type 
309
310       !REAL(wp),ALLOCATABLE, DIMENSION(:,:)     :: stack_height           !< stack height (ecc / to be implemented)
311       REAL(wp),ALLOCATABLE, DIMENSION(:,:,:)    :: default_emission_data  !< Emission input values for LOD1 (DEFAULT mode)
312       REAL(wp),ALLOCATABLE, DIMENSION(:,:,:,:)  :: preproc_emission_data  !< Emission input values for LOD2 (PRE-PROCESSED mode)
313
314    END TYPE chem_emis_val_type
315
316!
317!-- Define data structures for different input data types.
318!-- 8-bit Integer 2D
319    TYPE int_2d_8bit
320       INTEGER(KIND=1) ::  fill = -127                      !< fill value
321       INTEGER(KIND=1), DIMENSION(:,:), ALLOCATABLE ::  var !< respective variable
322
323       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
324    END TYPE int_2d_8bit
325!
326!-- 8-bit Integer 3D
327    TYPE int_3d_8bit
328       INTEGER(KIND=1) ::  fill = -127                           !< fill value
329       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var_3d !< respective variable
330
331       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
332    END TYPE int_3d_8bit
333!
334!-- 32-bit Integer 2D
335    TYPE int_2d_32bit
336       INTEGER(iwp) ::  fill = -9999                      !< fill value
337       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  var  !< respective variable
338
339       LOGICAL ::  from_file = .FALSE. !< flag indicating whether an input variable is available and read from file or default values are used
340    END TYPE int_2d_32bit
341!
342!-- Define data type to read 1D or 3D real variables.
343    TYPE real_1d_3d
344       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
345
346       INTEGER(iwp) ::  lod = -1        !< level-of-detail
347       
348       REAL(wp) ::  fill = -9999.9_wp                  !< fill value
349       
350       REAL(wp), DIMENSION(:),     ALLOCATABLE ::  var1d     !< respective 1D variable
351       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  var3d     !< respective 3D variable
352    END TYPE real_1d_3d   
353!
354!-- Define data type to read 2D real variables
355    TYPE real_2d
356       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
357
358       INTEGER(iwp) ::  lod             !< level-of-detail
359       
360       REAL(wp) ::  fill = -9999.9_wp                !< fill value
361       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  var !< respective variable
362    END TYPE real_2d
363
364!
365!-- Define data type to read 3D real variables
366    TYPE real_3d
367       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
368
369       INTEGER(iwp) ::  nz   !< number of grid points along vertical dimension
370
371       REAL(wp) ::  fill = -9999.9_wp                  !< fill value
372       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  var !< respective variable
373    END TYPE real_3d
374!
375!-- Define data structure where the dimension and type of the input depends
376!-- on the given level of detail.
377!-- For buildings, the input is either 2D float, or 3d byte.
378    TYPE build_in
379       INTEGER(iwp)    ::  lod = 1                               !< level of detail
380       INTEGER(KIND=1) ::  fill2 = -127                          !< fill value for lod = 2
381       INTEGER(iwp)    ::  nz                                    !< number of vertical layers in file
382       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var_3d !< 3d variable (lod = 2)
383
384       REAL(wp), DIMENSION(:), ALLOCATABLE ::  z                 !< vertical coordinate for 3D building, used for consistency check
385
386       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
387
388       REAL(wp)                              ::  fill1 = -9999.9_wp !< fill values for lod = 1
389       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  var_2d             !< 2d variable (lod = 1)
390       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  oro_max            !< terraing height under particular buildings
391    END TYPE build_in
392
393!
394!-- For soil_type, the input is either 2D or 3D one-byte integer.
395    TYPE soil_in
396       INTEGER(iwp)                                   ::  lod = 1      !< level of detail
397       INTEGER(KIND=1)                                ::  fill = -127  !< fill value for lod = 2
398       INTEGER(KIND=1), DIMENSION(:,:), ALLOCATABLE   ::  var_2d       !< 2d variable (lod = 1)
399       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var_3d       !< 3d variable (lod = 2)
400
401       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
402    END TYPE soil_in
403
404!
405!-- Define data type for fractions between surface types
406    TYPE fracs
407       INTEGER(iwp)                            ::  nf             !< total number of fractions
408       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nfracs         !< dimension array for fraction
409
410       LOGICAL ::  from_file = .FALSE. !< flag indicating whether an input variable is available and read from file or default values are used
411
412       REAL(wp)                                ::  fill = -9999.9_wp !< fill value
413       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  frac              !< respective fraction between different surface types
414    END TYPE fracs
415!
416!-- Data type for parameter lists, Depending on the given level of detail,
417!-- the input is 3D or 4D
418    TYPE pars
419       INTEGER(iwp)                            ::  lod = 1         !< level of detail
420       INTEGER(iwp)                            ::  np              !< total number of parameters
421       INTEGER(iwp)                            ::  nz              !< vertical dimension - number of soil layers
422       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  layers          !< dimension array for soil layers
423       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  pars            !< dimension array for parameters
424
425       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
426
427       REAL(wp)                                  ::  fill = -9999.9_wp !< fill value
428       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE   ::  pars_xy           !< respective parameters, level of detail = 1
429       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  pars_xyz          !< respective parameters, level of detail = 2
430    END TYPE pars
431!
432!-- Data type for surface parameter lists
433    TYPE pars_surf
434       INTEGER(iwp)                                ::  np          !< total number of parameters
435       INTEGER(iwp)                                ::  nsurf       !< number of local surfaces
436       INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE ::  index_ji    !< index for beginning and end of surfaces at (j,i)
437       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE   ::  coords      !< (k,j,i,norm_z,norm_y,norm_x)
438                                                                   !< k,j,i:                surface position
439                                                                   !< norm_z,norm_y,norm_x: surface normal vector
440
441       LOGICAL ::  from_file = .FALSE.  !< flag indicating whether an input variable is available and read from file or default values are used
442
443       REAL(wp)                              ::  fill = -9999.9_wp !< fill value
444       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  pars              !< respective parameters per surface
445    END TYPE pars_surf
446!
447!-- Define type for global file attributes
448!-- Please refer to the PALM data standard for a detailed description of each
449!-- attribute.
450    TYPE global_atts_type
451       CHARACTER(LEN=200) ::  acronym = ' '                      !< acronym of institution
452       CHARACTER(LEN=7)   ::  acronym_char = 'acronym'           !< name of attribute
453       CHARACTER(LEN=200) ::  author  = ' '                      !< first name, last name, email adress
454       CHARACTER(LEN=6)   ::  author_char = 'author'             !< name of attribute
455       CHARACTER(LEN=200) ::  campaign = 'PALM-4U'               !< name of campaign
456       CHARACTER(LEN=8)   ::  campaign_char = 'campaign'         !< name of attribute
457       CHARACTER(LEN=200) ::  comment = ' '                      !< comment to data
458       CHARACTER(LEN=7)   ::  comment_char = 'comment'           !< name of attribute
459       CHARACTER(LEN=200) ::  contact_person = ' '               !< first name, last name, email adress
460       CHARACTER(LEN=14)  ::  contact_person_char = 'contact_person'  !< name of attribute
461       CHARACTER(LEN=200) ::  conventions = 'CF-1.7'             !< netCDF convention
462       CHARACTER(LEN=11)  ::  conventions_char = 'Conventions'   !< name of attribute
463       CHARACTER(LEN=23 ) ::  creation_time = ' '                !< creation time of data set
464       CHARACTER(LEN=13)  ::  creation_time_char = 'creation_time'  !< name of attribute
465       CHARACTER(LEN=200) ::  data_content = ' '                 !< content of data set
466       CHARACTER(LEN=12)  ::  data_content_char = 'data_content' !< name of attribute
467       CHARACTER(LEN=200) ::  dependencies = ' '                 !< dependencies of data set
468       CHARACTER(LEN=12)  ::  dependencies_char = 'dependencies' !< name of attribute
469       CHARACTER(LEN=200) ::  history = ' '                      !< information about data processing
470       CHARACTER(LEN=7)   ::  history_char = 'history'           !< name of attribute
471       CHARACTER(LEN=200) ::  institution = ' '                  !< name of responsible institution
472       CHARACTER(LEN=11)  ::  institution_char = 'institution'   !< name of attribute
473       CHARACTER(LEN=200) ::  keywords = ' '                     !< keywords of data set
474       CHARACTER(LEN=8)   ::  keywords_char = 'keywords'         !< name of attribute
475       CHARACTER(LEN=200) ::  licence = ' '                      !< licence of data set
476       CHARACTER(LEN=7)   ::  licence_char = 'licence'           !< name of attribute
477       CHARACTER(LEN=200) ::  location = ' '                     !< place which refers to data set
478       CHARACTER(LEN=8)   ::  location_char = 'location'         !< name of attribute
479       CHARACTER(LEN=10)  ::  origin_lat_char = 'origin_lat'     !< name of attribute
480       CHARACTER(LEN=10)  ::  origin_lon_char = 'origin_lon'     !< name of attribute
481       CHARACTER(LEN=23 ) ::  origin_time = '2000-01-01 00:00:00 +00'  !< reference time
482       CHARACTER(LEN=11)  ::  origin_time_char = 'origin_time'   !< name of attribute
483       CHARACTER(LEN=8)   ::  origin_x_char = 'origin_x'         !< name of attribute
484       CHARACTER(LEN=8)   ::  origin_y_char = 'origin_y'         !< name of attribute
485       CHARACTER(LEN=8)   ::  origin_z_char = 'origin_z'         !< name of attribute
486       CHARACTER(LEN=12)  ::  palm_version_char = 'palm_version' !< name of attribute
487       CHARACTER(LEN=200) ::  references = ' '                   !< literature referring to data set
488       CHARACTER(LEN=10)  ::  references_char = 'references'     !< name of attribute
489       CHARACTER(LEN=14)  ::  rotation_angle_char = 'rotation_angle'  !< name of attribute
490       CHARACTER(LEN=200) ::  site = ' '                         !< name of model domain
491       CHARACTER(LEN=4)   ::  site_char = 'site'                 !< name of attribute
492       CHARACTER(LEN=200) ::  source = ' '                       !< source of data set
493       CHARACTER(LEN=6)   ::  source_char = 'source'             !< name of attribute
494       CHARACTER(LEN=200) ::  title = ' '                        !< title of data set
495       CHARACTER(LEN=5)   ::  title_char = 'title'               !< name of attribute
496       CHARACTER(LEN=7)   ::  version_char = 'version'           !< name of attribute
497
498       INTEGER(iwp) ::  version              !< version of data set
499
500       REAL(wp) ::  origin_lat               !< latitude of lower left corner
501       REAL(wp) ::  origin_lon               !< longitude of lower left corner
502       REAL(wp) ::  origin_x                 !< easting (UTM coordinate) of lower left corner
503       REAL(wp) ::  origin_y                 !< northing (UTM coordinate) of lower left corner
504       REAL(wp) ::  origin_z                 !< reference height
505       REAL(wp) ::  palm_version             !< PALM version of data set
506       REAL(wp) ::  rotation_angle           !< rotation angle of coordinate system of data set
507    END TYPE global_atts_type
508!
509!-- Define type for coordinate reference system (crs)
510    TYPE crs_type
511       CHARACTER(LEN=200) ::  epsg_code = 'EPSG:25831'                   !< EPSG code
512       CHARACTER(LEN=200) ::  grid_mapping_name = 'transverse_mercator'  !< name of grid mapping
513       CHARACTER(LEN=200) ::  long_name = 'coordinate reference system'  !< name of variable crs
514       CHARACTER(LEN=200) ::  units = 'm'                                !< unit of crs
515
516       REAL(wp) ::  false_easting = 500000.0_wp                  !< false easting
517       REAL(wp) ::  false_northing = 0.0_wp                      !< false northing
518       REAL(wp) ::  inverse_flattening = 298.257223563_wp        !< 1/f (default for WGS84)
519       REAL(wp) ::  latitude_of_projection_origin = 0.0_wp       !< latitude of projection origin
520       REAL(wp) ::  longitude_of_central_meridian = 3.0_wp       !< longitude of central meridian of UTM zone (default: zone 31)
521       REAL(wp) ::  longitude_of_prime_meridian = 0.0_wp         !< longitude of prime meridian
522       REAL(wp) ::  scale_factor_at_central_meridian = 0.9996_wp !< scale factor of UTM coordinates
523       REAL(wp) ::  semi_major_axis = 6378137.0_wp               !< length of semi major axis (default for WGS84)
524    END TYPE crs_type
525
526!
527!-- Define variables
528    TYPE(crs_type)   ::  coord_ref_sys  !< coordinate reference system
529
530    TYPE(dims_xy)    ::  dim_static     !< data structure for x, y-dimension in static input file
531
532    TYPE(init_type) ::  init_3d    !< data structure for the initialization of the 3D flow and soil fields
533    TYPE(init_type) ::  init_model !< data structure for the initialization of the model
534
535!
536!-- Define 2D variables of type NC_BYTE
537    TYPE(int_2d_8bit)  ::  albedo_type_f     !< input variable for albedo type
538    TYPE(int_2d_8bit)  ::  building_type_f   !< input variable for building type
539    TYPE(int_2d_8bit)  ::  pavement_type_f   !< input variable for pavenment type
540    TYPE(int_2d_8bit)  ::  street_crossing_f !< input variable for water type
541    TYPE(int_2d_8bit)  ::  street_type_f     !< input variable for water type
542    TYPE(int_2d_8bit)  ::  vegetation_type_f !< input variable for vegetation type
543    TYPE(int_2d_8bit)  ::  water_type_f      !< input variable for water type
544!
545!-- Define 3D variables of type NC_BYTE
546    TYPE(int_3d_8bit)  ::  building_obstruction_f    !< input variable for building obstruction
547    TYPE(int_3d_8bit)  ::  building_obstruction_full !< input variable for building obstruction
548!
549!-- Define 2D variables of type NC_INT
550    TYPE(int_2d_32bit) ::  building_id_f     !< input variable for building ID
551!
552!-- Define 2D variables of type NC_FLOAT
553    TYPE(real_2d) ::  terrain_height_f       !< input variable for terrain height
554    TYPE(real_2d) ::  uvem_irradiance_f      !< input variable for uvem irradiance lookup table
555    TYPE(real_2d) ::  uvem_integration_f     !< input variable for uvem integration
556!
557!-- Define 3D variables of type NC_FLOAT
558    TYPE(real_3d) ::  basal_area_density_f    !< input variable for basal area density - resolved vegetation
559    TYPE(real_3d) ::  leaf_area_density_f     !< input variable for leaf area density - resolved vegetation
560    TYPE(real_3d) ::  root_area_density_lad_f !< input variable for root area density - resolved vegetation
561    TYPE(real_3d) ::  root_area_density_lsm_f !< input variable for root area density - parametrized vegetation
562    TYPE(real_3d) ::  uvem_radiance_f         !< input variable for uvem radiance lookup table
563    TYPE(real_3d) ::  uvem_projarea_f         !< input variable for uvem projection area lookup table
564!
565!-- Define input variable for buildings
566    TYPE(build_in) ::  buildings_f           !< input variable for buildings
567!
568!-- Define input variables for soil_type
569    TYPE(soil_in)  ::  soil_type_f           !< input variable for soil type
570
571    TYPE(fracs) ::  surface_fraction_f       !< input variable for surface fraction
572
573    TYPE(pars)  ::  albedo_pars_f              !< input variable for albedo parameters
574    TYPE(pars)  ::  building_pars_f            !< input variable for building parameters
575    TYPE(pars)  ::  pavement_pars_f            !< input variable for pavement parameters
576    TYPE(pars)  ::  pavement_subsurface_pars_f !< input variable for pavement parameters
577    TYPE(pars)  ::  soil_pars_f                !< input variable for soil parameters
578    TYPE(pars)  ::  vegetation_pars_f          !< input variable for vegetation parameters
579    TYPE(pars)  ::  water_pars_f               !< input variable for water parameters
580
581    TYPE(pars_surf)  ::  building_surface_pars_f  !< input variable for building surface parameters
582
583    TYPE(chem_emis_att_type)                             ::  chem_emis_att    !< Input Information of Chemistry Emission Data from netcdf 
584    TYPE(chem_emis_val_type), ALLOCATABLE, DIMENSION(:)  ::  chem_emis        !< Input Chemistry Emission Data from netcdf 
585
586    CHARACTER(LEN=3)  ::  char_lod  = 'lod'         !< name of level-of-detail attribute in NetCDF file
587
588    CHARACTER(LEN=10) ::  char_fill = '_FillValue'        !< name of fill value attribute in NetCDF file
589
590    CHARACTER(LEN=100) ::  input_file_static  = 'PIDS_STATIC'  !< Name of file which comprises static input data
591    CHARACTER(LEN=100) ::  input_file_dynamic = 'PIDS_DYNAMIC' !< Name of file which comprises dynamic input data
592    CHARACTER(LEN=100) ::  input_file_chem    = 'PIDS_CHEM'    !< Name of file which comprises chemistry input data
593    CHARACTER(LEN=100) ::  input_file_uvem    = 'PIDS_UVEM'    !< Name of file which comprises static uv_exposure model input data
594    CHARACTER(LEN=100) ::  input_file_vm      = 'PIDS_VM'      !< Name of file which comprises virtual measurement data
595   
596    CHARACTER(LEN=25), ALLOCATABLE, DIMENSION(:) ::  string_values  !< output of string variables read from netcdf input files
597    CHARACTER(LEN=50), DIMENSION(:), ALLOCATABLE ::  vars_pids      !< variable in input file
598
599    INTEGER(iwp)                                     ::  id_emis        !< NetCDF id of input file for chemistry emissions: TBD: It has to be removed
600
601    INTEGER(iwp) ::  nc_stat         !< return value of nf90 function call
602    INTEGER(iwp) ::  num_var_pids    !< number of variables in file
603    INTEGER(iwp) ::  pids_id         !< file id
604
605    LOGICAL ::  input_pids_static  = .FALSE.   !< Flag indicating whether Palm-input-data-standard file containing static information exists
606    LOGICAL ::  input_pids_dynamic = .FALSE.   !< Flag indicating whether Palm-input-data-standard file containing dynamic information exists
607    LOGICAL ::  input_pids_chem    = .FALSE.   !< Flag indicating whether Palm-input-data-standard file containing chemistry information exists
608    LOGICAL ::  input_pids_uvem    = .FALSE.   !< Flag indicating whether uv-expoure-model input file containing static information exists
609    LOGICAL ::  input_pids_vm      = .FALSE.   !< Flag indicating whether input file for virtual measurements exist
610
611    LOGICAL ::  collective_read = .FALSE.      !< Enable NetCDF collective read
612
613    TYPE(global_atts_type) ::  input_file_atts !< global attributes of input file
614
615    SAVE
616
617    PRIVATE
618
619    INTERFACE netcdf_data_input_check_dynamic
620       MODULE PROCEDURE netcdf_data_input_check_dynamic
621    END INTERFACE netcdf_data_input_check_dynamic
622
623    INTERFACE netcdf_data_input_check_static
624       MODULE PROCEDURE netcdf_data_input_check_static
625    END INTERFACE netcdf_data_input_check_static
626
627    INTERFACE netcdf_data_input_chemistry_data                       
628       MODULE PROCEDURE netcdf_data_input_chemistry_data
629    END INTERFACE netcdf_data_input_chemistry_data
630   
631    INTERFACE get_dimension_length                       
632       MODULE PROCEDURE get_dimension_length
633    END INTERFACE get_dimension_length
634
635    INTERFACE netcdf_data_input_inquire_file
636       MODULE PROCEDURE netcdf_data_input_inquire_file
637    END INTERFACE netcdf_data_input_inquire_file
638
639    INTERFACE netcdf_data_input_init
640       MODULE PROCEDURE netcdf_data_input_init
641    END INTERFACE netcdf_data_input_init
642   
643    INTERFACE netcdf_data_input_att
644       MODULE PROCEDURE netcdf_data_input_att_int8
645       MODULE PROCEDURE netcdf_data_input_att_int32
646       MODULE PROCEDURE netcdf_data_input_att_real
647       MODULE PROCEDURE netcdf_data_input_att_string
648    END INTERFACE netcdf_data_input_att
649
650    INTERFACE netcdf_data_input_init_3d
651       MODULE PROCEDURE netcdf_data_input_init_3d
652    END INTERFACE netcdf_data_input_init_3d
653   
654    INTERFACE netcdf_data_input_surface_data
655       MODULE PROCEDURE netcdf_data_input_surface_data
656    END INTERFACE netcdf_data_input_surface_data
657
658    INTERFACE netcdf_data_input_var
659       MODULE PROCEDURE netcdf_data_input_var_char
660       MODULE PROCEDURE netcdf_data_input_var_real_1d
661       MODULE PROCEDURE netcdf_data_input_var_real_2d
662    END INTERFACE netcdf_data_input_var
663
664    INTERFACE netcdf_data_input_uvem
665       MODULE PROCEDURE netcdf_data_input_uvem
666    END INTERFACE netcdf_data_input_uvem
667
668    INTERFACE get_variable
669       MODULE PROCEDURE get_variable_1d_char
670       MODULE PROCEDURE get_variable_1d_int
671       MODULE PROCEDURE get_variable_1d_real
672       MODULE PROCEDURE get_variable_2d_int8
673       MODULE PROCEDURE get_variable_2d_int32
674       MODULE PROCEDURE get_variable_2d_real
675       MODULE PROCEDURE get_variable_3d_int8
676       MODULE PROCEDURE get_variable_3d_real
677       MODULE PROCEDURE get_variable_3d_real_dynamic
678       MODULE PROCEDURE get_variable_4d_to_3d_real
679       MODULE PROCEDURE get_variable_4d_real
680       MODULE PROCEDURE get_variable_5d_to_4d_real
681       MODULE PROCEDURE get_variable_5d_real           ! (ecc) temp subroutine 4 reading 5D NC arrays
682       MODULE PROCEDURE get_variable_5d_real_dynamic   ! 2B removed as z is out of emission_values
683       MODULE PROCEDURE get_variable_string
684    END INTERFACE get_variable
685
686    INTERFACE get_variable_pr
687       MODULE PROCEDURE get_variable_pr
688    END INTERFACE get_variable_pr
689
690    INTERFACE get_attribute
691       MODULE PROCEDURE get_attribute_real
692       MODULE PROCEDURE get_attribute_int8
693       MODULE PROCEDURE get_attribute_int32
694       MODULE PROCEDURE get_attribute_string
695    END INTERFACE get_attribute
696
697!
698!-- Public data structures
699    PUBLIC real_1d_3d,                                                         &
700           real_2d
701!
702!-- Public variables
703    PUBLIC albedo_pars_f, albedo_type_f, basal_area_density_f, buildings_f,    &
704           building_id_f, building_pars_f, building_surface_pars_f,            &
705           building_type_f,                                                    &
706           char_fill,                                                          &
707           char_lod,                                                           &
708           chem_emis, chem_emis_att, chem_emis_att_type, chem_emis_val_type,   &
709           coord_ref_sys,                                                      &
710           init_3d, init_model, input_file_atts,                               &
711           input_file_dynamic,                                                 &
712           input_file_static,                                                  &
713           input_pids_static,                                                  &
714           input_pids_dynamic, input_pids_vm, input_file_vm,                   &
715           leaf_area_density_f,                                                &
716           num_var_pids,                                                       &
717           pavement_pars_f, pavement_subsurface_pars_f, pavement_type_f,       &
718           pids_id,                                                            &
719           root_area_density_lad_f, root_area_density_lsm_f, soil_pars_f,      &
720           soil_type_f, street_crossing_f, street_type_f, surface_fraction_f,  &
721           terrain_height_f, vegetation_pars_f, vegetation_type_f,             &
722           vars_pids,                                                          &
723           water_pars_f, water_type_f
724!
725!-- Public uv exposure variables
726    PUBLIC building_obstruction_f, input_file_uvem, input_pids_uvem,           &
727           netcdf_data_input_uvem,                                             &
728           uvem_integration_f, uvem_irradiance_f,                              &
729           uvem_projarea_f, uvem_radiance_f
730
731!
732!-- Public subroutines
733    PUBLIC netcdf_data_input_check_dynamic,                                    &
734           netcdf_data_input_check_static,                                     &
735           netcdf_data_input_chemistry_data,                                   &
736           get_dimension_length,                                               &
737           netcdf_data_input_inquire_file,                                     &
738           netcdf_data_input_init,                                             &
739           netcdf_data_input_init_3d,                                          &
740           netcdf_data_input_att,                                              &
741           netcdf_data_input_surface_data,                                     &
742           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 horizontal grid points in '//            &
2917                           'dynamic input file does not match ' //             &
2918                           'the number of numeric grid 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 vertical grid points in '//              &
2924                           'dynamic input file does not match ' //             &
2925                           'the number of numeric grid 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!> Checks input file for consistency and minimum requirements.
3360!------------------------------------------------------------------------------!
3361    SUBROUTINE netcdf_data_input_check_dynamic
3362
3363       USE control_parameters,                                                 &
3364           ONLY:  initializing_actions, message_string
3365
3366       IMPLICIT NONE
3367!
3368!--    Dynamic input file must also be present if initialization via inifor is
3369!--    prescribed.
3370       IF ( .NOT. input_pids_dynamic  .AND.                                    &
3371            INDEX( initializing_actions, 'inifor' ) /= 0 )  THEN
3372          message_string = 'initializing_actions = inifor requires dynamic ' //&
3373                           'input file ' // TRIM( input_file_dynamic ) //      &
3374                           TRIM( coupling_char )
3375          CALL message( 'netcdf_data_input_mod', 'PA0547', 1, 2, 0, 6, 0 )
3376       ENDIF
3377
3378    END SUBROUTINE netcdf_data_input_check_dynamic
3379
3380!------------------------------------------------------------------------------!
3381! Description:
3382! ------------
3383!> Checks input file for consistency and minimum requirements.
3384!------------------------------------------------------------------------------!
3385    SUBROUTINE netcdf_data_input_check_static
3386
3387       USE arrays_3d,                                                          &
3388           ONLY:  zu
3389
3390       USE control_parameters,                                                 &
3391           ONLY:  land_surface, message_string, urban_surface
3392
3393       USE indices,                                                            &
3394           ONLY:  nxl, nxr, nyn, nys, wall_flags_0
3395
3396       IMPLICIT NONE
3397
3398       INTEGER(iwp) ::  i      !< loop index along x-direction
3399       INTEGER(iwp) ::  j      !< loop index along y-direction
3400       INTEGER(iwp) ::  n_surf !< number of different surface types at given location
3401
3402       LOGICAL      ::  check_passed !< flag indicating if a check passed
3403
3404!
3405!--    Return if no static input file is available
3406       IF ( .NOT. input_pids_static )  RETURN
3407!
3408!--    Check for correct dimension of surface_fractions, should run from 0-2.
3409       IF ( surface_fraction_f%from_file )  THEN
3410          IF ( surface_fraction_f%nf-1 > 2 )  THEN
3411             message_string = 'nsurface_fraction must not be larger than 3.' 
3412             CALL message( 'netcdf_data_input_mod', 'PA0580', 1, 2, 0, 6, 0 )
3413          ENDIF
3414       ENDIF
3415!
3416!--    Check orography for fill-values. For the moment, give an error message.
3417!--    More advanced methods, e.g. a nearest neighbor algorithm as used in GIS
3418!--    systems might be implemented later.
3419!--    Please note, if no terrain height is provided, it is set to 0.
3420       IF ( ANY( terrain_height_f%var == terrain_height_f%fill ) )  THEN
3421          message_string = 'NetCDF variable zt is not ' //                     &
3422                           'allowed to have missing data'
3423          CALL message( 'netcdf_data_input_mod', 'PA0550', 2, 2, myid, 6, 0 )
3424       ENDIF
3425!
3426!--    Check for negative terrain heights
3427       IF ( ANY( terrain_height_f%var < 0.0_wp ) )  THEN
3428          message_string = 'NetCDF variable zt is not ' //                     &
3429                           'allowed to have negative values'
3430          CALL message( 'netcdf_data_input_mod', 'PA0551', 2, 2, myid, 6, 0 )
3431       ENDIF
3432!
3433!--    If 3D buildings are read, check if building information is consistent
3434!--    to numeric grid.
3435       IF ( buildings_f%from_file )  THEN
3436          IF ( buildings_f%lod == 2 )  THEN
3437             IF ( buildings_f%nz > SIZE( zu ) )  THEN
3438                message_string = 'Reading 3D building data - too much ' //     &
3439                                 'data points along the vertical coordinate.'
3440                CALL message( 'netcdf_data_input_mod', 'PA0552', 2, 2, 0, 6, 0 )
3441             ENDIF
3442
3443             IF ( ANY( ABS( buildings_f%z(0:buildings_f%nz-1) -                &
3444                       zu(0:buildings_f%nz-1) ) > 1E-6_wp ) )  THEN
3445                message_string = 'Reading 3D building data - vertical ' //     &
3446                                 'coordinate do not match numeric grid.'
3447                CALL message( 'netcdf_data_input_mod', 'PA0553', 2, 2, 0, 6, 0 )
3448             ENDIF
3449          ENDIF
3450       ENDIF
3451
3452!
3453!--    Skip further checks concerning buildings and natural surface properties
3454!--    if no urban surface and land surface model are applied.
3455       IF (  .NOT. land_surface  .AND.  .NOT. urban_surface )  RETURN
3456!
3457!--    Check for minimum requirement of surface-classification data in case
3458!--    static input file is used.
3459       IF ( ( .NOT. vegetation_type_f%from_file  .OR.                          &
3460              .NOT. pavement_type_f%from_file    .OR.                          &
3461              .NOT. water_type_f%from_file       .OR.                          &
3462              .NOT. soil_type_f%from_file             ) .OR.                   &
3463             ( urban_surface  .AND.  .NOT. building_type_f%from_file ) )  THEN
3464          message_string = 'Minimum requirement for surface classification ' //&
3465                           'is not fulfilled. At least ' //                    &
3466                           'vegetation_type, pavement_type, ' //               &
3467                           'soil_type and water_type are '//                   &
3468                           'required. If urban-surface model is applied, ' //  &
3469                           'also building_type is required'
3470          CALL message( 'netcdf_data_input_mod', 'PA0554', 1, 2, 0, 6, 0 )
3471       ENDIF
3472!
3473!--    Check for general availability of input variables.
3474!--    If vegetation_type is 0 at any location, vegetation_pars as well as
3475!--    root_area_dens_s are required.
3476       IF ( vegetation_type_f%from_file )  THEN
3477          IF ( ANY( vegetation_type_f%var == 0 ) )  THEN
3478             IF ( .NOT. vegetation_pars_f%from_file )  THEN
3479                message_string = 'If vegetation_type = 0 at any location, ' // &
3480                                 'vegetation_pars is required'
3481                CALL message( 'netcdf_data_input_mod', 'PA0555', 2, 2, -1, 6, 0 )
3482             ENDIF
3483             IF ( .NOT. root_area_density_lsm_f%from_file )  THEN
3484                message_string = 'If vegetation_type = 0 at any location, ' // &
3485                                 'root_area_dens_s is required'
3486                CALL message( 'netcdf_data_input_mod', 'PA0556', 2, 2, myid, 6, 0 )
3487             ENDIF
3488          ENDIF
3489       ENDIF
3490!
3491!--    If soil_type is zero at any location, soil_pars is required.
3492       IF ( soil_type_f%from_file )  THEN
3493          check_passed = .TRUE.
3494          IF ( ALLOCATED( soil_type_f%var_2d ) )  THEN
3495             IF ( ANY( soil_type_f%var_2d == 0 ) )  THEN
3496                IF ( .NOT. soil_pars_f%from_file )  check_passed = .FALSE.
3497             ENDIF
3498          ELSE
3499             IF ( ANY( soil_type_f%var_3d == 0 ) )  THEN
3500                IF ( .NOT. soil_pars_f%from_file )  check_passed = .FALSE.
3501             ENDIF
3502          ENDIF
3503          IF ( .NOT. check_passed )  THEN
3504             message_string = 'If soil_type = 0 at any location, ' //          &
3505                              'soil_pars is required'
3506             CALL message( 'netcdf_data_input_mod', 'PA0557', 2, 2, myid, 6, 0 )
3507          ENDIF
3508       ENDIF
3509!
3510!--    Buildings require a type in case of urban-surface model.
3511       IF ( buildings_f%from_file  .AND.  .NOT. building_type_f%from_file  )  THEN
3512          message_string = 'If buildings are provided, also building_type ' // &
3513                           'is required'
3514          CALL message( 'netcdf_data_input_mod', 'PA0581', 2, 2, myid, 6, 0 )
3515       ENDIF
3516!
3517!--    Buildings require an ID.
3518       IF ( buildings_f%from_file  .AND.  .NOT. building_id_f%from_file  )  THEN
3519          message_string = 'If buildings are provided, also building_id ' //   &
3520                           'is required'
3521          CALL message( 'netcdf_data_input_mod', 'PA0582', 2, 2, myid, 6, 0 )
3522       ENDIF
3523!
3524!--    If building_type is zero at any location, building_pars is required.
3525       IF ( building_type_f%from_file )  THEN
3526          IF ( ANY( building_type_f%var == 0 ) )  THEN
3527             IF ( .NOT. building_pars_f%from_file )  THEN
3528                message_string = 'If building_type = 0 at any location, ' //   &
3529                                 'building_pars is required'
3530                CALL message( 'netcdf_data_input_mod', 'PA0558', 2, 2, myid, 6, 0 )
3531             ENDIF
3532          ENDIF
3533       ENDIF
3534!
3535!--    If building_type is provided, also building_id is needed (due to the
3536!--    filtering algorithm).
3537       IF ( building_type_f%from_file  .AND.  .NOT. building_id_f%from_file )  &
3538       THEN
3539          message_string = 'If building_type is provided, also building_id '// &
3540                           'is required'
3541          CALL message( 'netcdf_data_input_mod', 'PA0519', 2, 2, myid, 6, 0 )
3542       ENDIF       
3543!
3544!--    If albedo_type is zero at any location, albedo_pars is required.
3545       IF ( albedo_type_f%from_file )  THEN
3546          IF ( ANY( albedo_type_f%var == 0 ) )  THEN
3547             IF ( .NOT. albedo_pars_f%from_file )  THEN
3548                message_string = 'If albedo_type = 0 at any location, ' //     &
3549                                 'albedo_pars is required'
3550                CALL message( 'netcdf_data_input_mod', 'PA0559', 2, 2, myid, 6, 0 )
3551             ENDIF
3552          ENDIF
3553       ENDIF
3554!
3555!--    If pavement_type is zero at any location, pavement_pars is required.
3556       IF ( pavement_type_f%from_file )  THEN
3557          IF ( ANY( pavement_type_f%var == 0 ) )  THEN
3558             IF ( .NOT. pavement_pars_f%from_file )  THEN
3559                message_string = 'If pavement_type = 0 at any location, ' //   &
3560                                 'pavement_pars is required'
3561                CALL message( 'netcdf_data_input_mod', 'PA0560', 2, 2, myid, 6, 0 )
3562             ENDIF
3563          ENDIF
3564       ENDIF
3565!
3566!--    If pavement_type is zero at any location, also pavement_subsurface_pars
3567!--    is required.
3568       IF ( pavement_type_f%from_file )  THEN
3569          IF ( ANY( pavement_type_f%var == 0 ) )  THEN
3570             IF ( .NOT. pavement_subsurface_pars_f%from_file )  THEN
3571                message_string = 'If pavement_type = 0 at any location, ' //   &
3572                                 'pavement_subsurface_pars is required'
3573                CALL message( 'netcdf_data_input_mod', 'PA0561', 2, 2, myid, 6, 0 )
3574             ENDIF
3575          ENDIF
3576       ENDIF
3577!
3578!--    If water_type is zero at any location, water_pars is required.
3579       IF ( water_type_f%from_file )  THEN
3580          IF ( ANY( water_type_f%var == 0 ) )  THEN
3581             IF ( .NOT. water_pars_f%from_file )  THEN
3582                message_string = 'If water_type = 0 at any location, ' //      &
3583                                 'water_pars is required'
3584                CALL message( 'netcdf_data_input_mod', 'PA0562', 2, 2,myid, 6, 0 )
3585             ENDIF
3586          ENDIF
3587       ENDIF
3588!
3589!--    Check for local consistency of the input data.
3590       DO  i = nxl, nxr
3591          DO  j = nys, nyn
3592!
3593!--          For each (y,x)-location at least one of the parameters
3594!--          vegetation_type, pavement_type, building_type, or water_type
3595!--          must be set to a non­missing value.
3596             IF ( land_surface  .AND.  .NOT. urban_surface )  THEN
3597                IF ( vegetation_type_f%var(j,i) == vegetation_type_f%fill  .AND.&
3598                     pavement_type_f%var(j,i)   == pavement_type_f%fill    .AND.&
3599                     water_type_f%var(j,i)      == water_type_f%fill )  THEN
3600                   WRITE( message_string, * )                                  &
3601                                    'At least one of the parameters '//        &
3602                                    'vegetation_type, pavement_type, '     //  &
3603                                    'or water_type must be set '//             &
3604                                    'to a non-missing value. Grid point: ', j, i
3605                   CALL message( 'netcdf_data_input_mod', 'PA0563', 2, 2, myid, 6, 0 )
3606                ENDIF
3607             ELSEIF ( land_surface  .AND.  urban_surface )  THEN
3608                IF ( vegetation_type_f%var(j,i) == vegetation_type_f%fill  .AND.&
3609                     pavement_type_f%var(j,i)   == pavement_type_f%fill    .AND.&
3610                     building_type_f%var(j,i)   == building_type_f%fill    .AND.&
3611                     water_type_f%var(j,i)      == water_type_f%fill )  THEN
3612                   WRITE( message_string, * )                                  &
3613                                 'At least one of the parameters '//           &
3614                                 'vegetation_type, pavement_type, '  //        &
3615                                 'building_type, or water_type must be set '// &
3616                                 'to a non-missing value. Grid point: ', j, i
3617                   CALL message( 'netcdf_data_input_mod', 'PA0563', 2, 2, myid, 6, 0 )
3618                ENDIF
3619             ENDIF
3620               
3621!
3622!--          Note that a soil_type is required for each location (y,x) where
3623!--          either vegetation_type or pavement_type is a non­missing value.
3624             IF ( ( vegetation_type_f%var(j,i) /= vegetation_type_f%fill  .OR. &
3625                    pavement_type_f%var(j,i)   /= pavement_type_f%fill ) )  THEN
3626                check_passed = .TRUE.
3627                IF ( ALLOCATED( soil_type_f%var_2d ) )  THEN
3628                   IF ( soil_type_f%var_2d(j,i) == soil_type_f%fill )          &
3629                      check_passed = .FALSE.
3630                ELSE
3631                   IF ( ANY( soil_type_f%var_3d(:,j,i) == soil_type_f%fill) )  &
3632                      check_passed = .FALSE.
3633                ENDIF
3634
3635                IF ( .NOT. check_passed )  THEN
3636                   message_string = 'soil_type is required for each '//        &
3637                                 'location (y,x) where vegetation_type or ' // &
3638                                 'pavement_type is a non-missing value.'
3639                   CALL message( 'netcdf_data_input_mod', 'PA0564',            &
3640                                  2, 2, myid, 6, 0 )
3641                ENDIF
3642             ENDIF
3643!
3644!--          Check for consistency of surface fraction. If more than one type
3645!--          is set, surface fraction need to be given and the sum must not
3646!--          be larger than 1.
3647             n_surf = 0
3648             IF ( vegetation_type_f%var(j,i) /= vegetation_type_f%fill )       &
3649                n_surf = n_surf + 1
3650             IF ( water_type_f%var(j,i)      /= water_type_f%fill )            &
3651                n_surf = n_surf + 1
3652             IF ( pavement_type_f%var(j,i)   /= pavement_type_f%fill )         &
3653                n_surf = n_surf + 1
3654
3655             IF ( n_surf > 1 )  THEN
3656                IF ( .NOT. surface_fraction_f%from_file )  THEN
3657                   message_string = 'If more than one surface type is ' //     &
3658                                 'given at a location, surface_fraction ' //   &
3659                                 'must be provided.'
3660                   CALL message( 'netcdf_data_input_mod', 'PA0565',            &
3661                                  2, 2, myid, 6, 0 )
3662                ELSEIF ( ANY ( surface_fraction_f%frac(:,j,i) ==               &
3663                               surface_fraction_f%fill ) )  THEN
3664                   message_string = 'If more than one surface type is ' //     &
3665                                 'given at a location, surface_fraction ' //   &
3666                                 'must be provided.'
3667                   CALL message( 'netcdf_data_input_mod', 'PA0565',            &
3668                                  2, 2, myid, 6, 0 )
3669                ENDIF
3670             ENDIF
3671!
3672!--          Check for further mismatches. e.g. relative fractions exceed 1 or
3673!--          vegetation_type is set but surface vegetation fraction is zero,
3674!--          etc..
3675             IF ( surface_fraction_f%from_file )  THEN
3676!
3677!--             Sum of relative fractions must not exceed 1.
3678                IF ( SUM ( surface_fraction_f%frac(0:2,j,i) ) > 1.0_wp )  THEN
3679                   message_string = 'surface_fraction must not exceed 1'
3680                   CALL message( 'netcdf_data_input_mod', 'PA0566',            &
3681                                  2, 2, myid, 6, 0 )
3682                ENDIF
3683!
3684!--             Relative fraction for a type must not be zero at locations where
3685!--             this type is set.
3686                IF (                                                           &
3687                  ( vegetation_type_f%var(j,i) /= vegetation_type_f%fill  .AND.&
3688                 ( surface_fraction_f%frac(ind_veg_wall,j,i) == 0.0_wp .OR.    &
3689                   surface_fraction_f%frac(ind_veg_wall,j,i) ==                &
3690                                                     surface_fraction_f%fill ) &
3691                  )  .OR.                                                      &
3692                  ( pavement_type_f%var(j,i) /= pavement_type_f%fill     .AND. &
3693                 ( surface_fraction_f%frac(ind_pav_green,j,i) == 0.0_wp .OR.   &
3694                   surface_fraction_f%frac(ind_pav_green,j,i) ==               &
3695                                                     surface_fraction_f%fill ) &
3696                  )  .OR.                                                      &
3697                  ( water_type_f%var(j,i) /= water_type_f%fill           .AND. &
3698                 ( surface_fraction_f%frac(ind_wat_win,j,i) == 0.0_wp .OR.     &
3699                   surface_fraction_f%frac(ind_wat_win,j,i) ==                 &
3700                                                     surface_fraction_f%fill ) &
3701                  ) )  THEN
3702                   WRITE( message_string, * ) 'Mismatch in setting of '     // &
3703                             'surface_fraction. Vegetation-, pavement-, or '// &
3704                             'water surface is given at (i,j) = ( ', i, j,     &
3705                             ' ), but surface fraction is 0 for the given type.'
3706                   CALL message( 'netcdf_data_input_mod', 'PA0567',            &
3707                                  2, 2, myid, 6, 0 )
3708                ENDIF
3709!
3710!--             Relative fraction for a type must not contain non-zero values
3711!--             if this type is not set.
3712                IF (                                                           &
3713                  ( vegetation_type_f%var(j,i) == vegetation_type_f%fill  .AND.&
3714                 ( surface_fraction_f%frac(ind_veg_wall,j,i) /= 0.0_wp .AND.   &
3715                   surface_fraction_f%frac(ind_veg_wall,j,i) /=                &
3716                                                     surface_fraction_f%fill ) &
3717                  )  .OR.                                                      &
3718                  ( pavement_type_f%var(j,i) == pavement_type_f%fill     .AND. &
3719                 ( surface_fraction_f%frac(ind_pav_green,j,i) /= 0.0_wp .AND.  &
3720                   surface_fraction_f%frac(ind_pav_green,j,i) /=               &
3721                                                     surface_fraction_f%fill ) &
3722                  )  .OR.                                                      &
3723                  ( water_type_f%var(j,i) == water_type_f%fill           .AND. &
3724                 ( surface_fraction_f%frac(ind_wat_win,j,i) /= 0.0_wp .AND.    &
3725                   surface_fraction_f%frac(ind_wat_win,j,i) /=                 &
3726                                                     surface_fraction_f%fill ) &
3727                  ) )  THEN
3728                   WRITE( message_string, * ) 'Mismatch in setting of '     // &
3729                             'surface_fraction. Vegetation-, pavement-, or '// &
3730                             'water surface is not given at (i,j) = ( ', i, j, &
3731                             ' ), but surface fraction is not 0 for the ' //   &
3732                             'given type.'
3733                   CALL message( 'netcdf_data_input_mod', 'PA0568',            &
3734                                  2, 2, myid, 6, 0 )
3735                ENDIF
3736             ENDIF
3737!
3738!--          Check vegetation_pars. If vegetation_type is 0, all parameters
3739!--          need to be set, otherwise, single parameters set by
3740!--          vegetation_type can be overwritten.
3741             IF ( vegetation_type_f%from_file )  THEN
3742                IF ( vegetation_type_f%var(j,i) == 0 )  THEN
3743                   IF ( ANY( vegetation_pars_f%pars_xy(:,j,i) ==               &
3744                             vegetation_pars_f%fill ) )  THEN
3745                      message_string = 'If vegetation_type(y,x) = 0, all '  // &
3746                                       'parameters of vegetation_pars at '//   &
3747                                       'this location must be set.'
3748                      CALL message( 'netcdf_data_input_mod', 'PA0569',         &
3749                                     2, 2, myid, 6, 0 )
3750                   ENDIF
3751                ENDIF
3752             ENDIF
3753!
3754!--          Check root distribution. If vegetation_type is 0, all levels must
3755!--          be set.
3756             IF ( vegetation_type_f%from_file )  THEN
3757                IF ( vegetation_type_f%var(j,i) == 0 )  THEN
3758                   IF ( ANY( root_area_density_lsm_f%var(:,j,i) ==             &
3759                             root_area_density_lsm_f%fill ) )  THEN
3760                      message_string = 'If vegetation_type(y,x) = 0, all ' //  &
3761                                       'levels of root_area_dens_s ' //        &
3762                                       'must be set at this location.'
3763                      CALL message( 'netcdf_data_input_mod', 'PA0570',         &
3764                                     2, 2, myid, 6, 0 )
3765                   ENDIF
3766                ENDIF
3767             ENDIF
3768!
3769!--          Check soil parameters. If soil_type is 0, all parameters
3770!--          must be set.
3771             IF ( soil_type_f%from_file )  THEN
3772                check_passed = .TRUE.
3773                IF ( ALLOCATED( soil_type_f%var_2d ) )  THEN
3774                   IF ( soil_type_f%var_2d(j,i) == 0 )  THEN
3775                      IF ( ANY( soil_pars_f%pars_xy(:,j,i) ==                  &
3776                                soil_pars_f%fill ) )  check_passed = .FALSE.
3777                   ENDIF
3778                ELSE
3779                   IF ( ANY( soil_type_f%var_3d(:,j,i) == 0 ) )  THEN
3780                      IF ( ANY( soil_pars_f%pars_xy(:,j,i) ==                  &
3781                                soil_pars_f%fill ) )  check_passed = .FALSE.
3782                   ENDIF
3783                ENDIF
3784                IF ( .NOT. check_passed )  THEN
3785                   message_string = 'If soil_type(y,x) = 0, all levels of '  //&
3786                                    'soil_pars at this location must be set.'
3787                   CALL message( 'netcdf_data_input_mod', 'PA0571',            &
3788                                  2, 2, myid, 6, 0 )
3789                ENDIF
3790             ENDIF
3791
3792!
3793!--          Check building parameters. If building_type is 0, all parameters
3794!--          must be set.
3795             IF ( building_type_f%from_file )  THEN
3796                IF ( building_type_f%var(j,i) == 0 )  THEN
3797                   IF ( ANY( building_pars_f%pars_xy(:,j,i) ==                 &
3798                             building_pars_f%fill ) )  THEN
3799                      message_string = 'If building_type(y,x) = 0, all ' //    &
3800                                       'parameters of building_pars at this '//&
3801                                       'location must be set.'
3802                      CALL message( 'netcdf_data_input_mod', 'PA0572',         &
3803                                     2, 2, myid, 6, 0 )
3804                   ENDIF
3805                ENDIF
3806             ENDIF
3807!
3808!--          Check if building_type is set at each building and vice versa.
3809!--          Please note, buildings are already processed and filtered.
3810!--          For this reason, consistency checks are based on wall_flags_0
3811!--          rather than buildings_f (buildings are represented by bit 6 in
3812!--          wall_flags_0).
3813             IF ( building_type_f%from_file  .AND.  buildings_f%from_file )  THEN
3814                IF ( ANY( BTEST ( wall_flags_0(:,j,i), 6 ) )  .AND.            &
3815                     building_type_f%var(j,i) == building_type_f%fill  .OR.    &
3816               .NOT. ANY( BTEST ( wall_flags_0(:,j,i), 6 ) )  .AND.            &
3817                     building_type_f%var(j,i) /= building_type_f%fill )  THEN
3818                   WRITE( message_string, * ) 'Each location where a ' //      &
3819                                   'building is set requires a type ' //       &
3820                                   '( and vice versa ) in case the ' //        &
3821                                   'urban-surface model is applied. ' //       &
3822                                   'i, j = ', i, j
3823                   CALL message( 'netcdf_data_input_mod', 'PA0573',            &
3824                                  2, 2, myid, 6, 0 )
3825                ENDIF
3826             ENDIF
3827!
3828!--          Check if at each location where a building is present also an ID
3829!--          is set and vice versa.
3830             IF ( buildings_f%from_file )  THEN
3831                IF ( ANY( BTEST ( wall_flags_0(:,j,i), 6 ) )  .AND.           &
3832                     building_id_f%var(j,i) == building_id_f%fill  .OR.       &
3833               .NOT. ANY( BTEST ( wall_flags_0(:,j,i), 6 ) )  .AND.           &
3834                     building_id_f%var(j,i) /= building_id_f%fill )  THEN
3835                   WRITE( message_string, * ) 'Each location where a ' //     &
3836                                   'building is set requires an ID ' //       &
3837                                   '( and vice versa ). i, j = ', i, j
3838                   CALL message( 'netcdf_data_input_mod', 'PA0574',           &
3839                                  2, 2, myid, 6, 0 )
3840                ENDIF
3841             ENDIF
3842!
3843!--          Check if building ID is set where a bulding is defined.
3844             IF ( buildings_f%from_file )  THEN
3845                IF ( ANY( BTEST ( wall_flags_0(:,j,i), 6 ) )  .AND.           &
3846                     building_id_f%var(j,i) == building_id_f%fill )  THEN
3847                   WRITE( message_string, * ) 'Each building grid point '//   &
3848                                              'requires an ID.', i, j
3849                   CALL message( 'netcdf_data_input_mod', 'PA0575',           &
3850                                  2, 2, myid, 6, 0 )
3851                ENDIF
3852             ENDIF
3853!
3854!--          Check albedo parameters. If albedo_type is 0, all parameters
3855!--          must be set.
3856             IF ( albedo_type_f%from_file )  THEN
3857                IF ( albedo_type_f%var(j,i) == 0 )  THEN
3858                   IF ( ANY( albedo_pars_f%pars_xy(:,j,i) ==                   &
3859                             albedo_pars_f%fill ) )  THEN
3860                      message_string = 'If albedo_type(y,x) = 0, all ' //      &
3861                                       'parameters of albedo_pars at this ' // &
3862                                       'location must be set.'
3863                      CALL message( 'netcdf_data_input_mod', 'PA0576',         &
3864                                     2, 2, myid, 6, 0 )
3865                   ENDIF
3866                ENDIF
3867             ENDIF
3868
3869!
3870!--          Check pavement parameters. If pavement_type is 0, all parameters
3871!--          of pavement_pars must be set at this location.
3872             IF ( pavement_type_f%from_file )  THEN
3873                IF ( pavement_type_f%var(j,i) == 0 )  THEN
3874                   IF ( ANY( pavement_pars_f%pars_xy(:,j,i) ==                 &
3875                             pavement_pars_f%fill ) )  THEN
3876                      message_string = 'If pavement_type(y,x) = 0, all ' //    &
3877                                       'parameters of pavement_pars at this '//&
3878                                       'location must be set.'
3879                      CALL message( 'netcdf_data_input_mod', 'PA0577',         &
3880                                     2, 2, myid, 6, 0 )
3881                   ENDIF
3882                ENDIF
3883             ENDIF
3884!
3885!--          Check pavement-subsurface parameters. If pavement_type is 0,
3886!--          all parameters of pavement_subsurface_pars must be set  at this
3887!--          location.
3888             IF ( pavement_type_f%from_file )  THEN
3889                IF ( pavement_type_f%var(j,i) == 0 )  THEN
3890                   IF ( ANY( pavement_subsurface_pars_f%pars_xyz(:,:,j,i) ==   &
3891                             pavement_subsurface_pars_f%fill ) )  THEN
3892                      message_string = 'If pavement_type(y,x) = 0, all ' //    &
3893                                       'parameters of '                  //    &
3894                                       'pavement_subsurface_pars at this '//   &
3895                                       'location must be set.'
3896                      CALL message( 'netcdf_data_input_mod', 'PA0578',         &
3897                                     2, 2, myid, 6, 0 )
3898                   ENDIF
3899                ENDIF
3900             ENDIF
3901
3902!
3903!--          Check water parameters. If water_type is 0, all parameters
3904!--          must be set  at this location.
3905             IF ( water_type_f%from_file )  THEN
3906                IF ( water_type_f%var(j,i) == 0 )  THEN
3907                   IF ( ANY( water_pars_f%pars_xy(:,j,i) ==                    &
3908                             water_pars_f%fill ) )  THEN
3909                      message_string = 'If water_type(y,x) = 0, all ' //       &
3910                                       'parameters of water_pars at this ' //  &
3911                                       'location must be set.'
3912                      CALL message( 'netcdf_data_input_mod', 'PA0579',         &
3913                                     2, 2, myid, 6, 0 )
3914                   ENDIF
3915                ENDIF
3916             ENDIF
3917
3918          ENDDO
3919       ENDDO
3920
3921    END SUBROUTINE netcdf_data_input_check_static
3922
3923!------------------------------------------------------------------------------!
3924! Description:
3925! ------------
3926!> Resize 8-bit 2D Integer array: (nys:nyn,nxl:nxr) -> (nysg:nyng,nxlg:nxrg)
3927!------------------------------------------------------------------------------!
3928    SUBROUTINE resize_array_2d_int8( var, js, je, is, ie )
3929   
3930       IMPLICIT NONE
3931
3932       INTEGER(iwp) ::  je !< upper index bound along y direction
3933       INTEGER(iwp) ::  js !< lower index bound along y direction
3934       INTEGER(iwp) ::  ie !< upper index bound along x direction
3935       INTEGER(iwp) ::  is !< lower index bound along x direction
3936       
3937       INTEGER(KIND=1), DIMENSION(:,:), ALLOCATABLE ::  var     !< treated variable
3938       INTEGER(KIND=1), DIMENSION(:,:), ALLOCATABLE ::  var_tmp !< temporary copy
3939!
3940!--    Allocate temporary variable
3941       ALLOCATE( var_tmp(js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
3942!
3943!--    Temporary copy of the variable
3944       var_tmp(js:je,is:ie) = var(js:je,is:ie)
3945!
3946!--    Resize the array
3947       DEALLOCATE( var )
3948       ALLOCATE( var(js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
3949!
3950!--    Transfer temporary copy back to original array
3951       var(js:je,is:ie) = var_tmp(js:je,is:ie)
3952
3953    END SUBROUTINE resize_array_2d_int8
3954   
3955!------------------------------------------------------------------------------!
3956! Description:
3957! ------------
3958!> Resize 32-bit 2D Integer array: (nys:nyn,nxl:nxr) -> (nysg:nyng,nxlg:nxrg)
3959!------------------------------------------------------------------------------!
3960    SUBROUTINE resize_array_2d_int32( var, js, je, is, ie )
3961
3962       IMPLICIT NONE
3963       
3964       INTEGER(iwp) ::  je !< upper index bound along y direction
3965       INTEGER(iwp) ::  js !< lower index bound along y direction
3966       INTEGER(iwp) ::  ie !< upper index bound along x direction
3967       INTEGER(iwp) ::  is !< lower index bound along x direction
3968
3969       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  var     !< treated variable
3970       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  var_tmp !< temporary copy
3971!
3972!--    Allocate temporary variable
3973       ALLOCATE( var_tmp(js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
3974!
3975!--    Temporary copy of the variable
3976       var_tmp(js:je,is:ie) = var(js:je,is:ie)
3977!
3978!--    Resize the array
3979       DEALLOCATE( var )
3980       ALLOCATE( var(js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
3981!
3982!--    Transfer temporary copy back to original array
3983       var(js:je,is:ie) = var_tmp(js:je,is:ie)
3984
3985    END SUBROUTINE resize_array_2d_int32
3986   
3987!------------------------------------------------------------------------------!
3988! Description:
3989! ------------
3990!> Resize 8-bit 3D Integer array: (:,nys:nyn,nxl:nxr) -> (:,nysg:nyng,nxlg:nxrg)
3991!------------------------------------------------------------------------------!
3992    SUBROUTINE resize_array_3d_int8( var, ks, ke, js, je, is, ie )
3993
3994       IMPLICIT NONE
3995
3996       INTEGER(iwp) ::  je !< upper index bound along y direction
3997       INTEGER(iwp) ::  js !< lower index bound along y direction
3998       INTEGER(iwp) ::  ie !< upper index bound along x direction
3999       INTEGER(iwp) ::  is !< lower index bound along x direction
4000       INTEGER(iwp) ::  ke !< upper bound of treated array in z-direction 
4001       INTEGER(iwp) ::  ks !< lower bound of treated array in z-direction 
4002       
4003       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var     !< treated variable
4004       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE ::  var_tmp !< temporary copy
4005!
4006!--    Allocate temporary variable
4007       ALLOCATE( var_tmp(ks:ke,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4008!
4009!--    Temporary copy of the variable
4010       var_tmp(ks:ke,js:je,is:ie) = var(ks:ke,js:je,is:ie)
4011!
4012!--    Resize the array
4013       DEALLOCATE( var )
4014       ALLOCATE( var(ks:ke,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4015!
4016!--    Transfer temporary copy back to original array
4017       var(ks:ke,js:je,is:ie) = var_tmp(ks:ke,js:je,is:ie)
4018
4019    END SUBROUTINE resize_array_3d_int8
4020   
4021!------------------------------------------------------------------------------!
4022! Description:
4023! ------------
4024!> Resize 3D Real array: (:,nys:nyn,nxl:nxr) -> (:,nysg:nyng,nxlg:nxrg)
4025!------------------------------------------------------------------------------!
4026    SUBROUTINE resize_array_3d_real( var, ks, ke, js, je, is, ie )
4027
4028       IMPLICIT NONE
4029
4030       INTEGER(iwp) ::  je !< upper index bound along y direction
4031       INTEGER(iwp) ::  js !< lower index bound along y direction
4032       INTEGER(iwp) ::  ie !< upper index bound along x direction
4033       INTEGER(iwp) ::  is !< lower index bound along x direction
4034       INTEGER(iwp) ::  ke !< upper bound of treated array in z-direction 
4035       INTEGER(iwp) ::  ks !< lower bound of treated array in z-direction 
4036       
4037       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  var     !< treated variable
4038       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  var_tmp !< temporary copy
4039!
4040!--    Allocate temporary variable
4041       ALLOCATE( var_tmp(ks:ke,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4042!
4043!--    Temporary copy of the variable
4044       var_tmp(ks:ke,js:je,is:ie) = var(ks:ke,js:je,is:ie)
4045!
4046!--    Resize the array
4047       DEALLOCATE( var )
4048       ALLOCATE( var(ks:ke,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4049!
4050!--    Transfer temporary copy back to original array
4051       var(ks:ke,js:je,is:ie) = var_tmp(ks:ke,js:je,is:ie)
4052
4053    END SUBROUTINE resize_array_3d_real
4054   
4055!------------------------------------------------------------------------------!
4056! Description:
4057! ------------
4058!> Resize 4D Real array: (:,:,nys:nyn,nxl:nxr) -> (:,nysg:nyng,nxlg:nxrg)
4059!------------------------------------------------------------------------------!
4060    SUBROUTINE resize_array_4d_real( var, k1s, k1e, k2s, k2e, js, je, is, ie )
4061
4062       IMPLICIT NONE
4063       
4064       INTEGER(iwp) ::  je  !< upper index bound along y direction
4065       INTEGER(iwp) ::  js  !< lower index bound along y direction
4066       INTEGER(iwp) ::  ie  !< upper index bound along x direction
4067       INTEGER(iwp) ::  is  !< lower index bound along x direction
4068       INTEGER(iwp) ::  k1e !< upper bound of treated array in z-direction 
4069       INTEGER(iwp) ::  k1s !< lower bound of treated array in z-direction
4070       INTEGER(iwp) ::  k2e !< upper bound of treated array along parameter space 
4071       INTEGER(iwp) ::  k2s !< lower bound of treated array along parameter space 
4072       
4073       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  var     !< treated variable
4074       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  var_tmp !< temporary copy
4075!
4076!--    Allocate temporary variable
4077       ALLOCATE( var_tmp(k1s:k1e,k2s:k2e,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4078!
4079!--    Temporary copy of the variable
4080       var_tmp(k1s:k1e,k2s:k2e,js:je,is:ie) = var(k1s:k1e,k2s:k2e,js:je,is:ie)
4081!
4082!--    Resize the array
4083       DEALLOCATE( var )
4084       ALLOCATE( var(k1s:k1e,k2s:k2e,js-nbgp:je+nbgp,is-nbgp:ie+nbgp) )
4085!
4086!--    Transfer temporary copy back to original array
4087       var(k1s:k1e,k2s:k2e,js:je,is:ie) = var_tmp(k1s:k1e,k2s:k2e,js:je,is:ie)
4088
4089    END SUBROUTINE resize_array_4d_real
4090
4091!------------------------------------------------------------------------------!
4092! Description:
4093! ------------
4094!> Checks if a given variables is on file
4095!------------------------------------------------------------------------------!
4096    FUNCTION check_existence( vars_in_file, var_name )
4097
4098       IMPLICIT NONE
4099
4100       CHARACTER(LEN=*) ::  var_name                   !< variable to be checked
4101       CHARACTER(LEN=*), DIMENSION(:) ::  vars_in_file !< list of variables in file
4102
4103       INTEGER(iwp) ::  i                              !< loop variable
4104
4105       LOGICAL ::  check_existence                     !< flag indicating whether a variable exist or not - actual return value
4106
4107       i = 1
4108       check_existence = .FALSE.
4109       DO  WHILE ( i <= SIZE( vars_in_file ) )
4110          check_existence = TRIM( vars_in_file(i) ) == TRIM( var_name )  .OR.  &
4111                            check_existence
4112          i = i + 1
4113       ENDDO
4114
4115       RETURN
4116
4117    END FUNCTION check_existence
4118
4119
4120!------------------------------------------------------------------------------!
4121! Description:
4122! ------------
4123!> Closes an existing netCDF file.
4124!------------------------------------------------------------------------------!
4125    SUBROUTINE close_input_file( id )
4126#if defined( __netcdf )
4127
4128       USE pegrid
4129
4130       IMPLICIT NONE
4131
4132       INTEGER(iwp), INTENT(INOUT)        ::  id        !< file id
4133
4134       nc_stat = NF90_CLOSE( id )
4135       CALL handle_error( 'close', 540 )
4136#endif
4137    END SUBROUTINE close_input_file
4138
4139!------------------------------------------------------------------------------!
4140! Description:
4141! ------------
4142!> Opens an existing netCDF file for reading only and returns its id.
4143!------------------------------------------------------------------------------!
4144    SUBROUTINE open_read_file( filename, id )
4145#if defined( __netcdf )
4146
4147       USE pegrid
4148
4149       IMPLICIT NONE
4150
4151       CHARACTER (LEN=*), INTENT(IN) ::  filename  !< filename
4152       INTEGER(iwp), INTENT(INOUT)   ::  id        !< file id
4153
4154#if defined( __netcdf4_parallel )
4155!
4156!--    If __netcdf4_parallel is defined, parrallel NetCDF will be used
4157!--    unconditionally
4158       nc_stat = NF90_OPEN( filename, IOR( NF90_WRITE, NF90_MPIIO ), id,  &
4159                            COMM = comm2d, INFO = MPI_INFO_NULL )
4160!
4161!--    Check for possible Netcdf 3 file.
4162       IF( nc_stat /= NF90_NOERR )  THEN
4163          nc_stat = NF90_OPEN( filename, NF90_NOWRITE, id )
4164          collective_read = .FALSE.
4165       ELSE
4166          collective_read = .TRUE.
4167       ENDIF
4168#else
4169!      All MPI processes open und read
4170       nc_stat = NF90_OPEN( filename, NF90_NOWRITE, id )
4171#endif
4172
4173       CALL handle_error( 'open_read_file', 539 )
4174
4175#endif
4176    END SUBROUTINE open_read_file
4177
4178!------------------------------------------------------------------------------!
4179! Description:
4180! ------------
4181!> Reads global or variable-related attributes of type INTEGER (32-bit)
4182!------------------------------------------------------------------------------!
4183     SUBROUTINE get_attribute_int32( id, attribute_name, value, global,        &
4184                                     variable_name )
4185
4186       USE pegrid
4187
4188       IMPLICIT NONE
4189
4190       CHARACTER(LEN=*)            ::  attribute_name   !< attribute name
4191       CHARACTER(LEN=*), OPTIONAL  ::  variable_name    !< variable name
4192
4193       INTEGER(iwp), INTENT(IN)    ::  id               !< file id
4194       INTEGER(iwp)                ::  id_var           !< variable id
4195       INTEGER(iwp), INTENT(INOUT) ::  value            !< read value
4196
4197       LOGICAL, INTENT(IN) ::  global                   !< flag indicating global attribute
4198#if defined( __netcdf )
4199
4200!
4201!--    Read global attribute
4202       IF ( global )  THEN
4203          nc_stat = NF90_GET_ATT( id, NF90_GLOBAL, TRIM( attribute_name ), value )
4204          CALL handle_error( 'get_attribute_int32 global', 522, attribute_name )
4205!
4206!--    Read attributes referring to a single variable. Therefore, first inquire
4207!--    variable id
4208       ELSE
4209          nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
4210          CALL handle_error( 'get_attribute_int32', 522, attribute_name )
4211          nc_stat = NF90_GET_ATT( id, id_var, TRIM( attribute_name ), value )
4212          CALL handle_error( 'get_attribute_int32', 522, attribute_name )
4213       ENDIF
4214#endif
4215    END SUBROUTINE get_attribute_int32
4216
4217!------------------------------------------------------------------------------!
4218! Description:
4219! ------------
4220!> Reads global or variable-related attributes of type INTEGER (8-bit)
4221!------------------------------------------------------------------------------!
4222     SUBROUTINE get_attribute_int8( id, attribute_name, value, global,         &
4223                                    variable_name )
4224
4225       USE pegrid
4226
4227       IMPLICIT NONE
4228
4229       CHARACTER(LEN=*)            ::  attribute_name   !< attribute name
4230       CHARACTER(LEN=*), OPTIONAL  ::  variable_name    !< variable name
4231
4232       INTEGER(iwp), INTENT(IN)    ::  id               !< file id
4233       INTEGER(iwp)                ::  id_var           !< variable id
4234       INTEGER(KIND=1), INTENT(INOUT) ::  value         !< read value
4235
4236       LOGICAL, INTENT(IN) ::  global                   !< flag indicating global attribute
4237#if defined( __netcdf )
4238
4239!
4240!--    Read global attribute
4241       IF ( global )  THEN
4242          nc_stat = NF90_GET_ATT( id, NF90_GLOBAL, TRIM( attribute_name ), value )
4243          CALL handle_error( 'get_attribute_int8 global', 523, attribute_name )
4244!
4245!--    Read attributes referring to a single variable. Therefore, first inquire
4246!--    variable id
4247       ELSE
4248          nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
4249          CALL handle_error( 'get_attribute_int8', 523, attribute_name )
4250          nc_stat = NF90_GET_ATT( id, id_var, TRIM( attribute_name ), value )
4251          CALL handle_error( 'get_attribute_int8', 523, attribute_name )
4252       ENDIF
4253#endif
4254    END SUBROUTINE get_attribute_int8
4255
4256!------------------------------------------------------------------------------!
4257! Description:
4258! ------------
4259!> Reads global or variable-related attributes of type REAL
4260!------------------------------------------------------------------------------!
4261     SUBROUTINE get_attribute_real( id, attribute_name, value, global,         &
4262                                    variable_name )
4263
4264       USE pegrid
4265
4266       IMPLICIT NONE
4267
4268       CHARACTER(LEN=*)            ::  attribute_name   !< attribute name
4269       CHARACTER(LEN=*), OPTIONAL  ::  variable_name    !< variable name
4270
4271       INTEGER(iwp), INTENT(IN)    ::  id               !< file id
4272       INTEGER(iwp)                ::  id_var           !< variable id
4273
4274       LOGICAL, INTENT(IN) ::  global                   !< flag indicating global attribute
4275
4276       REAL(wp), INTENT(INOUT)     ::  value            !< read value
4277#if defined( __netcdf )
4278
4279
4280!
4281!-- Read global attribute
4282       IF ( global )  THEN
4283          nc_stat = NF90_GET_ATT( id, NF90_GLOBAL, TRIM( attribute_name ), value )
4284          CALL handle_error( 'get_attribute_real global', 524, attribute_name )
4285!
4286!-- Read attributes referring to a single variable. Therefore, first inquire
4287!-- variable id
4288       ELSE
4289          nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
4290          CALL handle_error( 'get_attribute_real', 524, attribute_name )
4291          nc_stat = NF90_GET_ATT( id, id_var, TRIM( attribute_name ), value )
4292          CALL handle_error( 'get_attribute_real', 524, attribute_name )
4293       ENDIF
4294#endif
4295    END SUBROUTINE get_attribute_real
4296
4297!------------------------------------------------------------------------------!
4298! Description:
4299! ------------
4300!> Reads global or variable-related attributes of type CHARACTER
4301!> Remark: reading attributes of type NF_STRING return an error code 56 -
4302!> Attempt to convert between text & numbers.
4303!------------------------------------------------------------------------------!
4304     SUBROUTINE get_attribute_string( id, attribute_name, value, global,       &
4305                                      variable_name, no_abort )
4306
4307       USE pegrid
4308
4309       IMPLICIT NONE
4310
4311       CHARACTER(LEN=*)                ::  attribute_name   !< attribute name
4312       CHARACTER(LEN=*), OPTIONAL      ::  variable_name    !< variable name
4313       CHARACTER(LEN=*), INTENT(INOUT) ::  value            !< read value
4314
4315       INTEGER(iwp), INTENT(IN)    ::  id               !< file id
4316       INTEGER(iwp)                ::  id_var           !< variable id
4317
4318       LOGICAL ::  check_error                          !< flag indicating if handle_error shall be checked
4319       LOGICAL, INTENT(IN) ::  global                   !< flag indicating global attribute
4320       LOGICAL, INTENT(IN), OPTIONAL ::  no_abort       !< flag indicating if errors should be checked
4321#if defined( __netcdf )
4322
4323       IF ( PRESENT( no_abort ) )  THEN
4324          check_error = no_abort
4325       ELSE
4326          check_error = .TRUE.
4327       ENDIF
4328!
4329!--    Read global attribute
4330       IF ( global )  THEN
4331          nc_stat = NF90_GET_ATT( id, NF90_GLOBAL, TRIM( attribute_name ), value )
4332          IF ( check_error)  CALL handle_error( 'get_attribute_string global', 525, attribute_name )
4333!
4334!--    Read attributes referring to a single variable. Therefore, first inquire
4335!--    variable id
4336       ELSE
4337          nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
4338          IF ( check_error)  CALL handle_error( 'get_attribute_string', 525, attribute_name )
4339
4340          nc_stat = NF90_GET_ATT( id, id_var, TRIM( attribute_name ), value )
4341          IF ( check_error)  CALL handle_error( 'get_attribute_string',525, attribute_name )
4342
4343       ENDIF
4344#endif
4345    END SUBROUTINE get_attribute_string
4346
4347
4348
4349!------------------------------------------------------------------------------!
4350! Description:
4351! ------------
4352!> Get dimension array for a given dimension
4353!------------------------------------------------------------------------------!
4354     SUBROUTINE get_dimension_length( id, dim_len, variable_name )
4355       USE pegrid
4356
4357       IMPLICIT NONE
4358
4359       CHARACTER(LEN=*)            ::  variable_name    !< dimension name
4360       CHARACTER(LEN=100)          ::  dum              !< dummy variable to receive return character
4361
4362       INTEGER(iwp)                ::  dim_len          !< dimension size
4363       INTEGER(iwp), INTENT(IN)    ::  id               !< file id
4364       INTEGER(iwp)                ::  id_dim           !< dimension id
4365
4366#if defined( __netcdf )
4367!
4368!--    First, inquire dimension ID
4369       nc_stat = NF90_INQ_DIMID( id, TRIM( variable_name ), id_dim )
4370       CALL handle_error( 'get_dimension_length', 526, variable_name )
4371!
4372!--    Inquire dimension length
4373       nc_stat = NF90_INQUIRE_DIMENSION( id, id_dim, dum, LEN = dim_len )
4374       CALL handle_error( 'get_dimension_length', 526, variable_name )
4375
4376#endif
4377    END SUBROUTINE get_dimension_length
4378
4379!------------------------------------------------------------------------------!
4380! Description:
4381! ------------
4382!> Routine for reading-in a character string from the chem emissions netcdf
4383!> input file. 
4384!------------------------------------------------------------------------------!
4385    SUBROUTINE get_variable_string( id, variable_name, var_string, names_number)
4386#if defined( __netcdf )
4387
4388       USE indices
4389       USE pegrid
4390
4391       IMPLICIT NONE
4392
4393       CHARACTER (LEN=25), ALLOCATABLE, DIMENSION(:), INTENT(INOUT)  :: var_string
4394
4395       CHARACTER(LEN=*)                                              :: variable_name          !> variable name
4396
4397       CHARACTER (LEN=1), ALLOCATABLE, DIMENSION(:,:)                :: tmp_var_string         !> variable to be read
4398
4399
4400       INTEGER(iwp), INTENT(IN)                                      :: id                     !> file id
4401
4402       INTEGER(iwp), INTENT(IN)                                      :: names_number           !> number of names
4403
4404       INTEGER(iwp)                                                  :: id_var                 !> variable id
4405
4406       INTEGER(iwp)                                                  :: i,j                    !> index to go through the length of the dimensions
4407
4408       INTEGER(iwp)                                                  :: max_string_length=25   !> this is both the maximum length of a name, but also 
4409                                                                                            ! the number of the components of the first dimensions
4410                                                                                            ! (rows)
4411
4412
4413       ALLOCATE(tmp_var_string(max_string_length,names_number))
4414
4415       ALLOCATE(var_string(names_number))
4416
4417    !-- Inquire variable id
4418       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
4419
4420
4421    !-- Get variable
4422    !-- Start cycle over the emission species
4423       DO i = 1, names_number 
4424       !-- read the first letter of each component
4425          nc_stat = NF90_GET_VAR( id, id_var, var_string(i), start = (/ 1,i /), &
4426                                 count = (/ 1,1 /) )
4427          CALL handle_error( 'get_variable_string', 701 )
4428
4429       !-- Start cycle over charachters
4430          DO j = 1, max_string_length
4431                       
4432          !-- read the rest of the components of the name
4433             nc_stat = NF90_GET_VAR( id, id_var, tmp_var_string(j,i), start = (/ j,i /),&
4434                                     count = (/ 1,1 /) )
4435             CALL handle_error( 'get_variable_string', 702 )
4436
4437             IF ( iachar(tmp_var_string(j,i) ) == 0 ) THEN
4438                  tmp_var_string(j,i)=''
4439             ENDIF
4440
4441             IF ( j>1 ) THEN
4442             !-- Concatenate first letter of the name and the others
4443                var_string(i)=TRIM(var_string(i)) // TRIM(tmp_var_string(j,i))
4444
4445             ENDIF
4446          ENDDO 
4447       ENDDO
4448
4449#endif
4450    END SUBROUTINE get_variable_string
4451
4452!------------------------------------------------------------------------------!
4453! Description:
4454! ------------
4455!> Reads a character variable in a 1D array
4456!------------------------------------------------------------------------------!
4457     SUBROUTINE get_variable_1d_char( id, variable_name, var )
4458
4459       USE pegrid
4460
4461       IMPLICIT NONE
4462
4463       CHARACTER(LEN=*)            ::  variable_name          !< variable name
4464       CHARACTER(LEN=*), DIMENSION(:), INTENT(INOUT) ::  var  !< variable to be read
4465
4466       INTEGER(iwp)                ::  i                !< running index over variable dimension
4467       INTEGER(iwp), INTENT(IN)    ::  id               !< file id
4468       INTEGER(iwp)                ::  id_var           !< dimension id
4469       
4470       INTEGER(iwp), DIMENSION(2)  ::  dimid            !< dimension IDs
4471       INTEGER(iwp), DIMENSION(2)  ::  dimsize          !< dimension size
4472
4473#if defined( __netcdf )
4474
4475!
4476!--    First, inquire variable ID
4477       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
4478       CALL handle_error( 'get_variable_1d_int', 527, variable_name )
4479!
4480!--    Inquire dimension IDs
4481       nc_stat = NF90_INQUIRE_VARIABLE( id, id_var, dimids = dimid(1:2) )
4482       CALL handle_error( 'get_variable_1d_char', 527, variable_name )
4483!
4484!--    Read dimesnion length
4485       nc_stat = NF90_INQUIRE_DIMENSION( id, dimid(1), LEN = dimsize(1) )
4486       nc_stat = NF90_INQUIRE_DIMENSION( id, dimid(2), LEN = dimsize(2) )
4487       
4488!
4489!--    Read character array. Note, each element is read individually, in order
4490!--    to better separate single strings.
4491       DO  i = 1, dimsize(2)
4492          nc_stat = NF90_GET_VAR( id, id_var, var(i),                          &
4493                                  start = (/ 1, i /),                          &
4494                                  count = (/ dimsize(1), 1 /) )
4495          CALL handle_error( 'get_variable_1d_char', 527, variable_name )
4496       ENDDO     
4497                         
4498#endif
4499    END SUBROUTINE get_variable_1d_char
4500
4501   
4502!------------------------------------------------------------------------------!
4503! Description:
4504! ------------
4505!> Reads a 1D integer variable from file.
4506!------------------------------------------------------------------------------!
4507     SUBROUTINE get_variable_1d_int( id, variable_name, var )
4508
4509       USE pegrid
4510
4511       IMPLICIT NONE
4512
4513       CHARACTER(LEN=*)            ::  variable_name    !< variable name
4514
4515       INTEGER(iwp), INTENT(IN)    ::  id               !< file id
4516       INTEGER(iwp)                ::  id_var           !< dimension id
4517
4518       INTEGER(iwp), DIMENSION(:), INTENT(INOUT) ::  var  !< variable to be read
4519#if defined( __netcdf )
4520
4521!
4522!--    First, inquire variable ID
4523       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
4524       CALL handle_error( 'get_variable_1d_int', 527, variable_name )
4525!
4526!--    Inquire dimension length
4527       nc_stat = NF90_GET_VAR( id, id_var, var )
4528       CALL handle_error( 'get_variable_1d_int', 527, variable_name )
4529
4530#endif
4531    END SUBROUTINE get_variable_1d_int
4532
4533!------------------------------------------------------------------------------!
4534! Description:
4535! ------------
4536!> Reads a 1D float variable from file.
4537!------------------------------------------------------------------------------!
4538     SUBROUTINE get_variable_1d_real( id, variable_name, var )
4539
4540       USE pegrid
4541
4542       IMPLICIT NONE
4543
4544       CHARACTER(LEN=*)            ::  variable_name    !< variable name
4545
4546       INTEGER(iwp), INTENT(IN)    ::  id               !< file id
4547       INTEGER(iwp)                ::  id_var           !< dimension id
4548
4549       REAL(wp), DIMENSION(:), INTENT(INOUT) ::  var    !< variable to be read
4550#if defined( __netcdf )
4551
4552!
4553!--    First, inquire variable ID
4554       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
4555       CALL handle_error( 'get_variable_1d_real', 528, variable_name )
4556!
4557!--    Inquire dimension length
4558       nc_stat = NF90_GET_VAR( id, id_var, var )
4559       CALL handle_error( 'get_variable_1d_real', 528, variable_name )
4560
4561#endif
4562    END SUBROUTINE get_variable_1d_real
4563
4564
4565!------------------------------------------------------------------------------!
4566! Description:
4567! ------------
4568!> Reads a time-dependent 1D float variable from file.
4569!------------------------------------------------------------------------------!
4570    SUBROUTINE get_variable_pr( id, variable_name, t, var )
4571#if defined( __netcdf )
4572
4573       USE pegrid
4574
4575       IMPLICIT NONE
4576
4577       CHARACTER(LEN=*)                      ::  variable_name    !< variable name
4578
4579       INTEGER(iwp), INTENT(IN)              ::  id               !< file id
4580       INTEGER(iwp), DIMENSION(1:2)          ::  id_dim           !< dimension ids
4581       INTEGER(iwp)                          ::  id_var           !< dimension id
4582       INTEGER(iwp)                          ::  n_file           !< number of data-points in file along z dimension
4583       INTEGER(iwp), INTENT(IN)              ::  t                !< timestep number
4584
4585       REAL(wp), DIMENSION(:), INTENT(INOUT) ::  var  !< variable to be read
4586
4587!
4588!--    First, inquire variable ID
4589       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
4590!
4591!--    Inquire dimension size of vertical dimension
4592       nc_stat = NF90_INQUIRE_VARIABLE( id, id_var, DIMIDS = id_dim )
4593       nc_stat = NF90_INQUIRE_DIMENSION( id, id_dim(1), LEN = n_file )
4594!
4595!--    Read variable.
4596       nc_stat = NF90_GET_VAR( id, id_var, var,                                &
4597                               start = (/ 1,      t     /),                    &
4598                               count = (/ n_file, 1     /) )
4599       CALL handle_error( 'get_variable_pr', 529, variable_name )
4600
4601#endif
4602    END SUBROUTINE get_variable_pr
4603
4604
4605!------------------------------------------------------------------------------!
4606! Description:
4607! ------------
4608!> Reads a per-surface pars variable from file. Because all surfaces are stored
4609!> as flat 1-D array, each PE has to scan the data and find the surface indices
4610!> belonging to its subdomain. During this scan, it also builds a necessary
4611!> (j,i) index.
4612!------------------------------------------------------------------------------!
4613    SUBROUTINE get_variable_surf( id, variable_name, surf )
4614#if defined( __netcdf )
4615
4616       USE pegrid
4617
4618       USE indices,                                            &
4619           ONLY:  nxl, nxr, nys, nyn
4620
4621       USE control_parameters,                                 &
4622           ONLY: dz, message_string
4623
4624       USE grid_variables,                                     &
4625           ONLY: dx, dy
4626       
4627       USE basic_constants_and_equations_mod,                  &
4628           ONLY: pi
4629
4630       IMPLICIT NONE
4631
4632       INTEGER, PARAMETER ::  nsurf_pars_read = 1024**2 !< read buffer size
4633
4634       CHARACTER(LEN=*)                          ::  variable_name !< variable name
4635
4636       INTEGER(iwp), DIMENSION(6)                ::  coords        !< integer coordinates of surface
4637       INTEGER(iwp)                              ::  i, j
4638       INTEGER(iwp)                              ::  isurf         !< netcdf surface index
4639       INTEGER(iwp)                              ::  is            !< local surface index
4640       INTEGER(iwp), INTENT(IN)                  ::  id            !< file id
4641       INTEGER(iwp), DIMENSION(2)                ::  id_dim        !< dimension ids
4642       INTEGER(iwp)                              ::  id_var        !< variable id
4643       INTEGER(iwp)                              ::  id_zs         !< zs variable id
4644       INTEGER(iwp)                              ::  id_ys         !< ys variable id
4645       INTEGER(iwp)                              ::  id_xs         !< xs variable id
4646       INTEGER(iwp)                              ::  id_zenith     !< zeith variable id
4647       INTEGER(iwp)                              ::  id_azimuth    !< azimuth variable id
4648       INTEGER(iwp)                              ::  is0, isc      !< read surface start and count
4649       INTEGER(iwp)                              ::  nsurf         !< total number of surfaces in file
4650       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  nsurf_ji      !< numbers of surfaces by coords
4651
4652       TYPE(pars_surf)                           ::  surf          !< parameters variable to be loaded
4653       REAL(wp), DIMENSION(:,:), ALLOCATABLE     ::  pars_read     !< read buffer
4654       REAL(wp), DIMENSION(:), ALLOCATABLE       ::  zs, ys, xs    !< read buffer for zs(s), ys, xs
4655       REAL(wp), DIMENSION(:), ALLOCATABLE       ::  zenith        !< read buffer for zenith(s)
4656       REAL(wp), DIMENSION(:), ALLOCATABLE       ::  azimuth       !< read buffer for azimuth(s)
4657       REAL(wp)                                  ::  oro_max_l     !< maximum terrain height under building
4658
4659!
4660!--    First, inquire variable ID
4661       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
4662       nc_stat = NF90_INQ_VARID( id, 'zs',                  id_zs )
4663       nc_stat = NF90_INQ_VARID( id, 'ys',                  id_ys )
4664       nc_stat = NF90_INQ_VARID( id, 'xs',                  id_xs )
4665       nc_stat = NF90_INQ_VARID( id, 'zenith',              id_zenith )
4666       nc_stat = NF90_INQ_VARID( id, 'azimuth',             id_azimuth )
4667!
4668!--    Inquire dimension sizes
4669       nc_stat = NF90_INQUIRE_VARIABLE( id, id_var, DIMIDS = id_dim )
4670       nc_stat = NF90_INQUIRE_DIMENSION( id, id_dim(1), LEN = nsurf )
4671       nc_stat = NF90_INQUIRE_DIMENSION( id, id_dim(2), LEN = surf%np )
4672
4673       ALLOCATE ( pars_read( nsurf_pars_read, surf%np ),        &
4674                  zs(nsurf_pars_read), ys(nsurf_pars_read),     &
4675                  xs(nsurf_pars_read), zenith(nsurf_pars_read), &
4676                  azimuth(nsurf_pars_read),                     &
4677                  nsurf_ji(nys:nyn, nxl:nxr) )
4678
4679       nsurf_ji(:,:) = 0
4680!
4681!--    Scan surface coordinates, count local
4682       is0 = 1
4683       DO
4684          isc = MIN(nsurf_pars_read, nsurf - is0 + 1)
4685          IF ( isc <= 0 )  EXIT
4686
4687          nc_stat = NF90_GET_VAR( id, id_ys, ys,     &
4688                                  start = (/ is0 /), &
4689                                  count = (/ isc /) )
4690          nc_stat = NF90_GET_VAR( id, id_xs, xs,     &
4691                                  start = (/ is0 /), &
4692                                  count = (/ isc /) )
4693          nc_stat = NF90_GET_VAR( id, id_zenith, zenith,      &
4694                                  start = (/ is0 /), &
4695                                  count = (/ isc /) )
4696          nc_stat = NF90_GET_VAR( id, id_azimuth, azimuth,    &
4697                                  start = (/ is0 /), &
4698                                  count = (/ isc /) )
4699          CALL handle_error( 'get_variable_surf', 682, 'azimuth' )
4700         
4701          DO  isurf = 1, isc
4702!
4703!--          Parse coordinates, detect if belongs to subdomain
4704             coords = transform_coords( xs(isurf), ys(isurf),         &
4705                                        zenith(isurf), azimuth(isurf) )
4706             IF ( coords(2) < nys  .OR.  coords(2) > nyn  .OR.  &
4707                  coords(3) < nxl  .OR.  coords(3) > nxr )  CYCLE
4708
4709             nsurf_ji(coords(2), coords(3)) = nsurf_ji(coords(2), coords(3)) + 1
4710          ENDDO
4711
4712          is0 = is0 + isc
4713       ENDDO
4714!
4715!--    Populate reverse index from surface counts
4716       ALLOCATE ( surf%index_ji( 2, nys:nyn, nxl:nxr ) )
4717
4718       isurf = 1
4719       DO  j = nys, nyn
4720          DO  i = nxl, nxr
4721             surf%index_ji(:,j,i) = (/ isurf, isurf + nsurf_ji(j,i) - 1 /)
4722             isurf = isurf + nsurf_ji(j,i)
4723          ENDDO
4724       ENDDO
4725
4726       surf%nsurf = isurf - 1
4727       ALLOCATE( surf%pars( 0:surf%np-1, surf%nsurf ), &
4728                 surf%coords( 6, surf%nsurf ) )
4729!
4730!--    Scan surfaces again, saving pars into allocated structures
4731       nsurf_ji(:,:) = 0
4732       is0 = 1
4733       DO
4734          isc = MIN(nsurf_pars_read, nsurf - is0 + 1)
4735          IF ( isc <= 0 )  EXIT
4736
4737          nc_stat = NF90_GET_VAR( id, id_var, pars_read(1:isc, 1:surf%np), &
4738                                  start = (/ is0, 1       /),              &
4739                                  count = (/ isc, surf%np /) )
4740          CALL handle_error( 'get_variable_surf', 683, variable_name )
4741
4742          nc_stat = NF90_GET_VAR( id, id_zs, zs,                           &
4743                                  start = (/ is0 /),                       &
4744                                  count = (/ isc /) )
4745          nc_stat = NF90_GET_VAR( id, id_ys, ys,                           &
4746                                  start = (/ is0 /),                       &
4747                                  count = (/ isc /) )
4748          nc_stat = NF90_GET_VAR( id, id_xs, xs,                           &
4749                                  start = (/ is0 /),                       &
4750                                  count = (/ isc /) )
4751          nc_stat = NF90_GET_VAR( id, id_zenith, zenith,                   &
4752                                  start = (/ is0 /),                       &
4753                                  count = (/ isc /) )
4754          nc_stat = NF90_GET_VAR( id, id_azimuth, azimuth,                 &
4755                                  start = (/ is0 /),                       &
4756                                  count = (/ isc /) )
4757         
4758          DO  isurf = 1, isc
4759!
4760!--          Parse coordinates, detect if belongs to subdomain
4761             coords = transform_coords( xs(isurf), ys(isurf),         &
4762                                        zenith(isurf), azimuth(isurf) )
4763             IF ( coords(2) < nys  .OR.  coords(2) > nyn  .OR.  &
4764                  coords(3) < nxl  .OR.  coords(3) > nxr )  CYCLE
4765!
4766!--          Determine maximum terrain under building (base z-coordinate). Using
4767!--          normal vector to locate building inner coordinates.
4768             oro_max_l = buildings_f%oro_max(coords(2)-coords(5), coords(3)-coords(6))
4769             IF  ( oro_max_l == buildings_f%fill1 )  THEN
4770                WRITE( message_string, * ) 'Found building surface on '   // &
4771                   'non-building coordinates (xs, ys, zenith, azimuth): ',   &
4772                   xs(isurf), ys(isurf), zenith(isurf), azimuth(isurf)
4773                CALL message( 'get_variable_surf', 'PA0684', 2, 2, 0, 6, 0 ) 
4774             ENDIF
4775!
4776!--          Urban layer has no stretching, therefore using dz(1) instead of linear
4777!--          searching through zu/zw
4778             coords(1) = NINT((zs(isurf) + oro_max_l) / dz(1) +     &
4779                              0.5_wp + 0.5_wp * coords(4), KIND=iwp)
4780!
4781!--          Save surface entry
4782             is = surf%index_ji(1, coords(2), coords(3)) + nsurf_ji(coords(2), coords(3))
4783             surf%pars(:,is) = pars_read(isurf,:)
4784             surf%coords(:,is) = coords(:)
4785
4786             nsurf_ji(coords(2), coords(3)) = nsurf_ji(coords(2), coords(3)) + 1
4787          ENDDO
4788
4789          is0 = is0 + isc
4790       ENDDO
4791
4792       DEALLOCATE( pars_read, zs, ys, xs, zenith, azimuth, nsurf_ji )
4793
4794    CONTAINS
4795
4796       PURE FUNCTION transform_coords( x, y, zenith, azimuth )
4797
4798          REAL(wp), INTENT(in)       ::  x, y    !< surface centre coordinates in metres from origin
4799          REAL(wp), INTENT(in)       ::  zenith  !< surface normal zenith angle in degrees
4800          REAL(wp), INTENT(in)       ::  azimuth !< surface normal azimuth angle in degrees
4801
4802          INTEGER(iwp), DIMENSION(6) ::  transform_coords !< (k,j,i,norm_z,norm_y,norm_x)
4803
4804          transform_coords(4) = NINT(COS(zenith*pi/180._wp), KIND=iwp)
4805          IF ( transform_coords(4) == 0 )  THEN
4806             transform_coords(5) = NINT(COS(azimuth*pi/180._wp), KIND=iwp)
4807             transform_coords(6) = NINT(SIN(azimuth*pi/180._wp), KIND=iwp)
4808          ELSE
4809             transform_coords(5) = 0._wp
4810             transform_coords(6) = 0._wp
4811          ENDIF
4812
4813          transform_coords(1) = -999._wp ! not calculated here
4814          transform_coords(2) = NINT(y/dy - 0.5_wp + 0.5_wp*transform_coords(5), KIND=iwp)
4815          transform_coords(3) = NINT(x/dx - 0.5_wp + 0.5_wp*transform_coords(6), KIND=iwp)
4816
4817       END FUNCTION transform_coords
4818
4819#endif
4820    END SUBROUTINE get_variable_surf
4821
4822
4823!------------------------------------------------------------------------------!
4824! Description:
4825! ------------
4826!> Reads a 2D REAL variable from a file. Reading is done processor-wise,
4827!> i.e. each core reads its own domain in slices along x.
4828!------------------------------------------------------------------------------!
4829    SUBROUTINE get_variable_2d_real( id, variable_name, var, is, ie, js, je )
4830
4831       USE indices
4832       USE pegrid
4833
4834       IMPLICIT NONE
4835
4836       CHARACTER(LEN=*)              ::  variable_name   !< variable name
4837
4838       INTEGER(iwp)                  ::  i               !< running index along x direction
4839       INTEGER(iwp)                  ::  ie              !< start index for subdomain input along x direction
4840       INTEGER(iwp)                  ::  is              !< end index for subdomain input along x direction
4841       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
4842       INTEGER(iwp)                  ::  id_var          !< variable id
4843       INTEGER(iwp)                  ::  j               !< running index along y direction
4844       INTEGER(iwp)                  ::  je              !< start index for subdomain input along y direction
4845       INTEGER(iwp)                  ::  js              !< end index for subdomain input along y direction
4846       
4847       REAL(wp), DIMENSION(:,:), ALLOCATABLE   ::  tmp   !< temporary variable to read data from file according
4848                                                         !< to its reverse memory access
4849       REAL(wp), DIMENSION(:,:), INTENT(INOUT) ::  var   !< variable to be read
4850#if defined( __netcdf )
4851!
4852!--    Inquire variable id
4853       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
4854!
4855!--    Check for collective read-operation and set respective NetCDF flags if
4856!--    required.
4857       IF ( collective_read )  THEN
4858#if defined( __netcdf4_parallel )
4859          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
4860#endif
4861       ENDIF
4862
4863!
4864!-- Allocate temporary variable according to memory access on file.
4865       ALLOCATE( tmp(is:ie,js:je) )
4866!
4867!-- Get variable
4868       nc_stat = NF90_GET_VAR( id, id_var, tmp,            &
4869                      start = (/ is+1,      js+1 /),       &
4870                      count = (/ ie-is + 1, je-js+1 /) )   
4871          CALL handle_error( 'get_variable_2d_real', 530, variable_name )
4872!
4873!-- Resort data. Please note, dimension subscripts of var all start at 1.
4874          DO  i = is, ie 
4875             DO  j = js, je 
4876                var(j-js+1,i-is+1) = tmp(i,j)
4877             ENDDO
4878          ENDDO
4879       
4880          DEALLOCATE( tmp )
4881
4882#endif
4883    END SUBROUTINE get_variable_2d_real
4884
4885!------------------------------------------------------------------------------!
4886! Description:
4887! ------------
4888!> Reads a 2D 32-bit INTEGER variable from file. Reading is done processor-wise,
4889!> i.e. each core reads its own domain in slices along x.
4890!------------------------------------------------------------------------------!
4891    SUBROUTINE get_variable_2d_int32( id, variable_name, var, is, ie, js, je )
4892
4893       USE indices
4894       USE pegrid
4895
4896       IMPLICIT NONE
4897
4898       CHARACTER(LEN=*)              ::  variable_name   !< variable name
4899
4900       INTEGER(iwp)                  ::  i               !< running index along x direction
4901       INTEGER(iwp)                  ::  ie              !< start index for subdomain input along x direction
4902       INTEGER(iwp)                  ::  is              !< end index for subdomain input along x direction
4903       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
4904       INTEGER(iwp)                  ::  id_var          !< variable id
4905       INTEGER(iwp)                  ::  j               !< running index along y direction
4906       INTEGER(iwp)                  ::  je              !< start index for subdomain input along y direction
4907       INTEGER(iwp)                  ::  js              !< end index for subdomain input along y direction
4908       
4909       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE   ::  tmp  !< temporary variable to read data from file according
4910                                                            !< to its reverse memory access
4911       INTEGER(iwp), DIMENSION(:,:), INTENT(INOUT) ::  var  !< variable to be read
4912#if defined( __netcdf )
4913!
4914!--    Inquire variable id
4915       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
4916!
4917!--    Check for collective read-operation and set respective NetCDF flags if
4918!--    required.
4919       IF ( collective_read )  THEN
4920#if defined( __netcdf4_parallel )       
4921          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
4922#endif
4923       ENDIF
4924!
4925!--    Allocate temporary variable according to memory access on file.
4926       ALLOCATE( tmp(is:ie,js:je) )
4927!
4928!--    Get variable
4929       nc_stat = NF90_GET_VAR( id, id_var, tmp,                                &
4930                               start = (/ is+1,      js+1 /),                  &
4931                               count = (/ ie-is + 1, je-js+1 /) )   
4932                               
4933       CALL handle_error( 'get_variable_2d_int32', 531, variable_name )                             
4934!
4935!--    Resort data. Please note, dimension subscripts of var all start at 1.
4936       DO  i = is, ie 
4937          DO  j = js, je 
4938             var(j-js+1,i-is+1) = tmp(i,j)
4939          ENDDO
4940       ENDDO
4941       
4942       DEALLOCATE( tmp )
4943
4944#endif
4945    END SUBROUTINE get_variable_2d_int32
4946
4947!------------------------------------------------------------------------------!
4948! Description:
4949! ------------
4950!> Reads a 2D 8-bit INTEGER variable from file. Reading is done processor-wise,
4951!> i.e. each core reads its own domain in slices along x.
4952!------------------------------------------------------------------------------!
4953    SUBROUTINE get_variable_2d_int8( id, variable_name, var, is, ie, js, je )
4954
4955       USE indices
4956       USE pegrid
4957
4958       IMPLICIT NONE
4959
4960       CHARACTER(LEN=*)              ::  variable_name   !< variable name
4961
4962       INTEGER(iwp)                  ::  i               !< running index along x direction
4963       INTEGER(iwp)                  ::  ie              !< start index for subdomain input along x direction
4964       INTEGER(iwp)                  ::  is              !< end index for subdomain input along x direction
4965       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
4966       INTEGER(iwp)                  ::  id_var          !< variable id
4967       INTEGER(iwp)                  ::  j               !< running index along y direction
4968       INTEGER(iwp)                  ::  je              !< start index for subdomain input along y direction
4969       INTEGER(iwp)                  ::  js              !< end index for subdomain input along y direction
4970       
4971       INTEGER(KIND=1), DIMENSION(:,:), ALLOCATABLE   ::  tmp  !< temporary variable to read data from file according
4972                                                               !< to its reverse memory access
4973       INTEGER(KIND=1), DIMENSION(:,:), INTENT(INOUT) ::  var  !< variable to be read
4974#if defined( __netcdf )
4975!
4976!--    Inquire variable id
4977       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
4978!
4979!--    Check for collective read-operation and set respective NetCDF flags if
4980!--    required.
4981       IF ( collective_read )  THEN
4982#if defined( __netcdf4_parallel )       
4983          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
4984#endif         
4985       ENDIF
4986!
4987!--    Allocate temporary variable according to memory access on file.
4988       ALLOCATE( tmp(is:ie,js:je) )
4989!
4990!--    Get variable
4991       nc_stat = NF90_GET_VAR( id, id_var, tmp,                                &
4992                               start = (/ is+1,      js+1 /),                  &
4993                               count = (/ ie-is + 1, je-js+1 /) )   
4994                               
4995       CALL handle_error( 'get_variable_2d_int8', 532, variable_name )
4996!
4997!--    Resort data. Please note, dimension subscripts of var all start at 1.
4998       DO  i = is, ie 
4999          DO  j = js, je 
5000             var(j-js+1,i-is+1) = tmp(i,j)
5001          ENDDO
5002       ENDDO
5003       
5004       DEALLOCATE( tmp )
5005
5006#endif
5007    END SUBROUTINE get_variable_2d_int8
5008
5009
5010!------------------------------------------------------------------------------!
5011! Description:
5012! ------------
5013!> Reads a 3D 8-bit INTEGER variable from file.
5014!------------------------------------------------------------------------------!
5015    SUBROUTINE get_variable_3d_int8( id, variable_name, var, is, ie, js, je,   &
5016                                     ks, ke )
5017
5018       USE indices
5019       USE pegrid
5020
5021       IMPLICIT NONE
5022
5023       CHARACTER(LEN=*)              ::  variable_name   !< variable name
5024
5025       INTEGER(iwp)                  ::  i               !< index along x direction
5026       INTEGER(iwp)                  ::  ie              !< start index for subdomain input along x direction
5027       INTEGER(iwp)                  ::  is              !< end index for subdomain input along x direction
5028       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
5029       INTEGER(iwp)                  ::  id_var          !< variable id
5030       INTEGER(iwp)                  ::  j               !< index along y direction
5031       INTEGER(iwp)                  ::  je              !< start index for subdomain input along y direction
5032       INTEGER(iwp)                  ::  js              !< end index for subdomain input along y direction
5033       INTEGER(iwp)                  ::  k               !< index along any 3rd dimension
5034       INTEGER(iwp)                  ::  ke              !< start index of 3rd dimension
5035       INTEGER(iwp)                  ::  ks              !< end index of 3rd dimension
5036
5037       INTEGER(KIND=1), DIMENSION(:,:,:), ALLOCATABLE   ::  tmp  !< temporary variable to read data from file according
5038                                                                 !< to its reverse memory access
5039
5040       INTEGER(KIND=1), DIMENSION(:,:,:), INTENT(INOUT) ::  var  !< variable to be read
5041#if defined( __netcdf )
5042
5043!
5044!--    Inquire variable id
5045       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5046!
5047!--    Check for collective read-operation and set respective NetCDF flags if
5048!--    required.
5049       IF ( collective_read )  THEN
5050#if defined( __netcdf4_parallel )
5051          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
5052#endif
5053       ENDIF
5054!
5055!--    Allocate temporary variable according to memory access on file.
5056       ALLOCATE( tmp(is:ie,js:je,ks:ke) )
5057!
5058!--    Get variable
5059       nc_stat = NF90_GET_VAR( id, id_var, tmp,                                &
5060                               start = (/ is+1,    js+1,    ks+1 /),           &
5061                               count = (/ ie-is+1, je-js+1, ke-ks+1 /) )
5062
5063       CALL handle_error( 'get_variable_3d_int8', 533, variable_name )
5064!
5065!--    Resort data. Please note, dimension subscripts of var all start at 1.
5066       DO  i = is, ie 
5067          DO  j = js, je
5068             DO  k = ks, ke
5069                var(k-ks+1,j-js+1,i-is+1) = tmp(i,j,k)
5070             ENDDO
5071          ENDDO
5072       ENDDO
5073
5074       DEALLOCATE( tmp )
5075
5076#endif
5077    END SUBROUTINE get_variable_3d_int8
5078
5079
5080!------------------------------------------------------------------------------!
5081! Description:
5082! ------------
5083!> Reads a 3D float variable from file.
5084!------------------------------------------------------------------------------!
5085    SUBROUTINE get_variable_3d_real( id, variable_name, var, is, ie, js, je,   &
5086                                     ks, ke )
5087
5088       USE indices
5089       USE pegrid
5090
5091       IMPLICIT NONE
5092
5093       CHARACTER(LEN=*)              ::  variable_name   !< variable name
5094
5095       INTEGER(iwp)                  ::  i               !< index along x direction
5096       INTEGER(iwp)                  ::  ie              !< start index for subdomain input along x direction
5097       INTEGER(iwp)                  ::  is              !< end index for subdomain input along x direction
5098       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
5099       INTEGER(iwp)                  ::  id_var          !< variable id
5100       INTEGER(iwp)                  ::  j               !< index along y direction
5101       INTEGER(iwp)                  ::  je              !< start index for subdomain input along y direction
5102       INTEGER(iwp)                  ::  js              !< end index for subdomain input along y direction
5103       INTEGER(iwp)                  ::  k               !< index along any 3rd dimension
5104       INTEGER(iwp)                  ::  ke              !< start index of 3rd dimension
5105       INTEGER(iwp)                  ::  ks              !< end index of 3rd dimension
5106
5107       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE   ::  tmp !< temporary variable to read data from file according
5108                                                         !< to its reverse memory access
5109
5110       REAL(wp), DIMENSION(:,:,:), INTENT(INOUT) ::  var !< variable to be read
5111#if defined( __netcdf )
5112
5113!
5114!--    Inquire variable id
5115       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var ) 
5116!
5117!--    Check for collective read-operation and set respective NetCDF flags if
5118!--    required.
5119       IF ( collective_read )  THEN
5120#if defined( __netcdf4_parallel )
5121          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
5122#endif
5123       ENDIF
5124!
5125!--    Allocate temporary variable according to memory access on file.
5126       ALLOCATE( tmp(is:ie,js:je,ks:ke) )
5127!
5128!--    Get variable
5129       nc_stat = NF90_GET_VAR( id, id_var, tmp,                                &
5130                               start = (/ is+1,    js+1,    ks+1 /),           &
5131                               count = (/ ie-is+1, je-js+1, ke-ks+1 /) )   
5132
5133       CALL handle_error( 'get_variable_3d_real', 534, variable_name )
5134!
5135!--    Resort data. Please note, dimension subscripts of var all start at 1.
5136       DO  i = is, ie 
5137          DO  j = js, je
5138             DO  k = ks, ke
5139                var(k-ks+1,j-js+1,i-is+1) = tmp(i,j,k)
5140             ENDDO
5141          ENDDO
5142       ENDDO
5143
5144       DEALLOCATE( tmp )
5145
5146#endif
5147    END SUBROUTINE get_variable_3d_real
5148
5149!------------------------------------------------------------------------------!
5150! Description:
5151! ------------
5152!> Reads a 4D float variable from file.
5153!------------------------------------------------------------------------------!
5154    SUBROUTINE get_variable_4d_real( id, variable_name, var, is, ie, js, je,   &
5155                                     k1s, k1e, k2s, k2e )
5156
5157       USE indices
5158       USE pegrid
5159
5160       IMPLICIT NONE
5161
5162       CHARACTER(LEN=*)              ::  variable_name   !< variable name
5163
5164       INTEGER(iwp)                  ::  i               !< index along x direction
5165       INTEGER(iwp)                  ::  ie              !< start index for subdomain input along x direction
5166       INTEGER(iwp)                  ::  is              !< end index for subdomain input along x direction
5167       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
5168       INTEGER(iwp)                  ::  id_var          !< variable id
5169       INTEGER(iwp)                  ::  j               !< index along y direction
5170       INTEGER(iwp)                  ::  je              !< start index for subdomain input along y direction
5171       INTEGER(iwp)                  ::  js              !< end index for subdomain input along y direction
5172       INTEGER(iwp)                  ::  k1              !< index along 3rd direction
5173       INTEGER(iwp)                  ::  k1e             !< start index for 3rd dimension
5174       INTEGER(iwp)                  ::  k1s             !< end index for 3rd dimension
5175       INTEGER(iwp)                  ::  k2              !< index along 4th direction
5176       INTEGER(iwp)                  ::  k2e             !< start index for 4th dimension
5177       INTEGER(iwp)                  ::  k2s             !< end index for 4th dimension
5178
5179       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE   ::  tmp  !< temporary variable to read data from file according
5180                                                            !< to its reverse memory access
5181       REAL(wp), DIMENSION(:,:,:,:), INTENT(INOUT) ::  var  !< variable to be read
5182#if defined( __netcdf )
5183
5184!
5185!--    Inquire variable id
5186       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5187!
5188!--    Check for collective read-operation and set respective NetCDF flags if
5189!--    required.
5190       IF ( collective_read )  THEN
5191#if defined( __netcdf4_parallel )       
5192          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
5193#endif
5194       ENDIF
5195
5196!
5197!-- Allocate temporary variable according to memory access on file.
5198       ALLOCATE( tmp(is:ie,js:je,k1s:k1e,k2s:k2e) )
5199!
5200!-- Get variable
5201       nc_stat = NF90_GET_VAR( id, id_var, tmp,                                &
5202                      start = (/ is+1,    js+1,    k1s+1,     k2s+1 /),        &
5203                      count = (/ ie-is+1, je-js+1, k1e-k1s+1, k2e-k2s+1 /) )
5204
5205          CALL handle_error( 'get_variable_4d_real', 535, variable_name )
5206!
5207!-- Resort data. Please note, dimension subscripts of var all start at 1.
5208       DO  i = is, ie 
5209          DO  j = js, je
5210             DO  k1 = k1s, k1e
5211                DO  k2 = k2s, k2e
5212                   var(k2-k2s+1,k1-k1s+1,j-js+1,i-is+1) = tmp(i,j,k1,k2)
5213                ENDDO
5214             ENDDO
5215          ENDDO
5216       ENDDO
5217
5218       DEALLOCATE( tmp )
5219
5220#endif
5221
5222    END SUBROUTINE get_variable_4d_real
5223
5224!------------------------------------------------------------------------------!
5225! Description:
5226! ------------
5227!> Reads a 4D float variable from file and store it to a 3-d variable.
5228!------------------------------------------------------------------------------!
5229    SUBROUTINE get_variable_4d_to_3d_real( id, variable_name, var, ns, is, ie, js, je,   &
5230                                           ks, ke )
5231
5232       USE indices
5233       USE pegrid
5234
5235       IMPLICIT NONE
5236
5237       CHARACTER(LEN=*)              ::  variable_name   !< variable name
5238
5239       INTEGER(iwp)                  ::  i               !< index along x direction
5240       INTEGER(iwp)                  ::  ie              !< end index for subdomain input along x direction
5241       INTEGER(iwp)                  ::  is              !< start index for subdomain input along x direction
5242       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
5243       INTEGER(iwp)                  ::  id_var          !< variable id
5244       INTEGER(iwp)                  ::  j               !< index along y direction
5245       INTEGER(iwp)                  ::  je              !< end index for subdomain input along y direction
5246       INTEGER(iwp)                  ::  js              !< start index for subdomain input along y direction
5247       INTEGER(iwp)                  ::  k               !< index along any 4th dimension
5248       INTEGER(iwp)                  ::  ke              !< end index of 4th dimension
5249       INTEGER(iwp)                  ::  ks              !< start index of 4th dimension
5250       INTEGER(iwp)                  ::  ns              !< start index for subdomain input along n dimension
5251
5252       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE   ::  tmp !< temporary variable to read data from file according
5253                                                         !< to its reverse memory access
5254
5255       REAL(wp), DIMENSION(:,:,:), INTENT(INOUT) ::  var  !< variable where the read data have to be stored:
5256                                                          !< one dimension is reduced in the process
5257#if defined( __netcdf )
5258
5259!
5260!--    Inquire variable id
5261       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5262!
5263!--    Check for collective read-operation and set respective NetCDF flags if
5264!--    required.
5265       IF ( collective_read )  THEN
5266          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
5267       ENDIF
5268!
5269!--    Allocate temporary variable according to memory access on file.
5270       ALLOCATE( tmp(is:ie,js:je,ks:ke) )
5271!
5272!--    Get variable
5273       nc_stat = NF90_GET_VAR( id, id_var, tmp,                                &
5274                               start = (/ is+1,    js+1,    ks+1,   ns+1 /),   &
5275                               count = (/ ie-is+1, je-js+1, ke-ks+1, 1   /) )
5276
5277       CALL handle_error( 'get_variable_4d_to_3d_real', 536, variable_name )
5278!
5279!--    Resort data. Please note, dimension subscripts of var all start at 1.
5280       DO  i = is, ie
5281          DO  j = js, je
5282             DO  k = ks, ke
5283                var(k-ks+1,j-js+1,i-is+1) = tmp(i,j,k)
5284             ENDDO
5285          ENDDO
5286       ENDDO
5287
5288      DEALLOCATE( tmp )
5289
5290#endif
5291    END SUBROUTINE get_variable_4d_to_3d_real
5292
5293!------------------------------------------------------------------------------!
5294! Description:
5295! ------------
5296!> Reads a 3D float variables from dynamic driver, such as time-dependent xy-,
5297!> xz- or yz-boundary data as well as 3D initialization data. Please note,
5298!> the passed arguments are start indices and number of elements in each
5299!> dimension, which is in contrast to the other 3d versions where start- and
5300!> end indices are passed. The different handling of 3D dynamic variables is
5301!> due to its asymmetry for the u- and v component.
5302!------------------------------------------------------------------------------!
5303    SUBROUTINE get_variable_3d_real_dynamic( id, variable_name, var,           &
5304                                             i1s, i2s, i3s,                    &
5305                                             count_1, count_2, count_3,        &
5306                                             par_access )
5307                               
5308       USE indices
5309       USE pegrid
5310
5311       IMPLICIT NONE
5312
5313       CHARACTER(LEN=*)              ::  variable_name   !< variable name
5314
5315       LOGICAL                       ::  par_access      !< additional flag indicating whether parallel read operations should be performed or not
5316       
5317       INTEGER(iwp)                  ::  count_1         !< number of elements to be read along 1st dimension (with respect to file)
5318       INTEGER(iwp)                  ::  count_2         !< number of elements to be read along 2nd dimension (with respect to file)
5319       INTEGER(iwp)                  ::  count_3         !< number of elements to be read along 3rd dimension (with respect to file)
5320       INTEGER(iwp)                  ::  i1              !< running index along 1st dimension on file
5321       INTEGER(iwp)                  ::  i1s             !< start index for subdomain input along 1st dimension (with respect to file)
5322       INTEGER(iwp)                  ::  i2              !< running index along 2nd dimension on file       
5323       INTEGER(iwp)                  ::  i2s             !< start index for subdomain input along 2nd dimension (with respect to file)
5324       INTEGER(iwp)                  ::  i3              !< running index along 3rd dimension on file
5325       INTEGER(iwp)                  ::  i3s             !< start index of 3rd dimension, in dynamic file this is either time (2D boundary) or z (3D)
5326       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
5327       INTEGER(iwp)                  ::  id_var          !< variable id
5328       INTEGER(iwp)                  ::  lb1             !< lower bound of 1st dimension (with respect to file)
5329       INTEGER(iwp)                  ::  lb2             !< lower bound of 2nd dimension (with respect to file)
5330       INTEGER(iwp)                  ::  lb3             !< lower bound of 3rd dimension (with respect to file)
5331       INTEGER(iwp)                  ::  ub1             !< upper bound of 1st dimension (with respect to file)
5332       INTEGER(iwp)                  ::  ub2             !< upper bound of 2nd dimension (with respect to file)
5333       INTEGER(iwp)                  ::  ub3             !< upper bound of 3rd dimension (with respect to file)
5334
5335       REAL(wp), DIMENSION(:,:,:), ALLOCATABLE   ::  tmp !< temporary variable to read data from file according
5336                                                         !< to its reverse memory access
5337       
5338       REAL(wp), DIMENSION(:,:,:), INTENT(INOUT) ::  var !< input variable
5339       
5340#if defined( __netcdf )
5341!
5342!--    Inquire variable id.
5343       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5344!
5345!--    Check for collective read-operation and set respective NetCDF flags if
5346!--    required.
5347!--    Please note, in contrast to the other input routines where each PEs
5348!--    reads its subdomain data, dynamic input data not by all PEs, only
5349!--    by those which encompass lateral model boundaries. Hence, collective
5350!--    read operations are only enabled for top-boundary data.
5351       IF ( collective_read  .AND.  par_access )  THEN
5352#if defined( __netcdf4_parallel )       
5353          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
5354#endif
5355       ENDIF   
5356!
5357!--    Allocate temporary variable according to memory access on file.
5358!--    Therefore, determine dimension bounds of input array.
5359       lb1 = LBOUND(var,3)
5360       ub1 = UBOUND(var,3)
5361       lb2 = LBOUND(var,2)
5362       ub2 = UBOUND(var,2)
5363       lb3 = LBOUND(var,1)
5364       ub3 = UBOUND(var,1)
5365       ALLOCATE( tmp(lb1:ub1,lb2:ub2,lb3:ub3) )
5366!
5367!--    Get variable
5368       nc_stat = NF90_GET_VAR( id, id_var, tmp,                                &
5369                               start = (/ i1s,     i2s,     i3s /),            &
5370                               count = (/ count_1, count_2, count_3 /) )
5371
5372       CALL handle_error( 'get_variable_3d_real_dynamic', 537, variable_name )
5373!
5374!--    Resort data. Please note, dimension subscripts of var all start at 1.
5375       DO  i3 = lb3, ub3
5376          DO i2 = lb2, ub2
5377             DO  i1 = lb1, ub1
5378                var(i3,i2,i1) = tmp(i1,i2,i3)
5379             ENDDO
5380          ENDDO
5381       ENDDO
5382       
5383       DEALLOCATE( tmp )       
5384#endif
5385    END SUBROUTINE get_variable_3d_real_dynamic
5386
5387!------------------------------------------------------------------------------!
5388! Description:
5389! ------------
5390!> Reads a 5D float variable from file and store it to a 4-d variable.
5391!------------------------------------------------------------------------------!
5392    SUBROUTINE get_variable_5d_to_4d_real( id, variable_name, var,              &
5393                                           ns, ts, te, is, ie, js, je, ks, ke )
5394
5395       USE indices
5396       USE pegrid
5397
5398       IMPLICIT NONE
5399
5400       CHARACTER(LEN=*)              ::  variable_name   !< variable name
5401
5402       INTEGER(iwp)                  ::  ns              !< start index for subdomain input along n dimension:
5403                                                         !< ns coincides here with ne, since, we select only one
5404                                                         !< value along the 1st dimension n
5405
5406       INTEGER(iwp)                  ::  t               !< index along t direction
5407       INTEGER(iwp)                  ::  te              !< end index for subdomain input along t direction
5408       INTEGER(iwp)                  ::  ts              !< start index for subdomain input along t direction
5409
5410       INTEGER(iwp)                  ::  i               !< index along x direction
5411       INTEGER(iwp)                  ::  ie              !< end index for subdomain input along x direction
5412       INTEGER(iwp)                  ::  is              !< start index for subdomain input along x direction
5413       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
5414       INTEGER(iwp)                  ::  id_var          !< variable id
5415       INTEGER(iwp)                  ::  j               !< index along y direction
5416       INTEGER(iwp)                  ::  je              !< end index for subdomain input along y direction
5417       INTEGER(iwp)                  ::  js              !< start index for subdomain input along y direction
5418       INTEGER(iwp)                  ::  k               !< index along any 5th dimension
5419       INTEGER(iwp)                  ::  ke              !< end index of 5th dimension
5420       INTEGER(iwp)                  ::  ks              !< start index of 5th dimension
5421
5422       REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE   ::  tmp !< temporary variable to read data from file according
5423                                                           ! to its reverse memory access
5424       REAL(wp), DIMENSION(:,:,:,:), INTENT(INOUT) ::  var !< variable to be read
5425#if defined( __netcdf )
5426!
5427!--    Inquire variable id
5428       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5429!
5430!--    Check for collective read-operation and set respective NetCDF flags if
5431!--    required.
5432       IF ( collective_read )  THEN
5433          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
5434       ENDIF
5435!
5436!--    Allocate temporary variable according to memory access on file.
5437       ALLOCATE( tmp(ks:ke,js:je,is:is,ts:te) )
5438!
5439!--    Get variable
5440       nc_stat = NF90_GET_VAR( id, id_var, tmp,                                &
5441                               start = (/ ks+1, js+1, is+1, ts+1, ns /),       &
5442                               count = (/ ke-ks+1, je-js+1, ie-is+1, te-ts+1, 1 /) )
5443
5444       CALL handle_error( 'get_variable_5d_to_4d_real', 538, variable_name )
5445!
5446!--    Resort data. Please note, dimension subscripts of var all start at 1.
5447
5448       DO  t = ts, te 
5449          DO  i = is, ie 
5450             DO  j = js, je
5451                DO  k = ks, ke
5452                   var(t-ts+1,i-is+1,j-js+1,k-ks+1) = tmp(k,j,i,t)
5453                ENDDO
5454             ENDDO
5455          ENDDO
5456       ENDDO 
5457
5458       DEALLOCATE( tmp )
5459#endif
5460    END SUBROUTINE get_variable_5d_to_4d_real
5461
5462   
5463!------------------------------------------------------------------------------!
5464! Description:
5465! ------------
5466!> Reads a 5D float variable from file.
5467!> NOTE - This subroutine is used specific for reading NC variable
5468!>        emission_values having a "z" dimension.  Said dimension
5469!>        is to be removed in the future and this subroutine shall
5470!>        be depreciated accordingly (ecc 20190418)
5471!------------------------------------------------------------------------------!
5472    SUBROUTINE get_variable_5d_real( id, variable_name, var, is, ie, js, je,   &
5473                                     k1s, k1e, k2s, k2e, k3s, k3e )
5474
5475       USE indices
5476       USE pegrid
5477
5478       IMPLICIT NONE
5479
5480       CHARACTER(LEN=*)          ::  variable_name  !< variable name
5481
5482       INTEGER(iwp)              :: i       !< i index
5483       INTEGER(iwp)              :: ie      !< i index start
5484       INTEGER(iwp)              :: is      !< i index end
5485       INTEGER(iwp)              :: id_var  !< netCDF variable ID (varid)
5486       INTEGER(iwp)              :: j       !< j index
5487       INTEGER(iwp)              :: je      !< j index start
5488       INTEGER(iwp)              :: js      !< j index end
5489       INTEGER(iwp)              :: k1      !< k1 index
5490       INTEGER(iwp)              :: k1e     !< k1 index start
5491       INTEGER(iwp)              :: k1s     !< k1 index end
5492       INTEGER(iwp)              :: k2      !< k2 index
5493       INTEGER(iwp)              :: k2e     !< k2 index start
5494       INTEGER(iwp)              :: k2s     !< k2 index end
5495       INTEGER(iwp)              :: k3      !< k3 index
5496       INTEGER(iwp)              :: k3e     !< k3 index start
5497       INTEGER(iwp)              :: k3s     !< k3 index end
5498       INTEGER(iwp), INTENT(IN)  :: id      !< netCDF file ID (ncid)
5499
5500       REAL(wp), DIMENSION(:,:,:,:,:), ALLOCATABLE    :: tmp  !< temp array to read data from file
5501       REAL(wp), DIMENSION(:,:,:,:,:), INTENT(INOUT)  :: var  !< variable to be read
5502
5503#if defined( __netcdf )
5504
5505!
5506!-- Inquire variable id
5507
5508       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5509
5510!
5511!-- Check for collective read-operation and set respective NetCDF flags if required.
5512 
5513       IF ( collective_read )  THEN
5514
5515#if defined( __netcdf4_parallel )       
5516          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
5517#endif
5518
5519       ENDIF
5520
5521!
5522!-- Allocate temporary variable according to memory access on file.
5523
5524       ALLOCATE( tmp(is:ie,js:je,k1s:k1e,k2s:k2e,k3s:k3e) )
5525
5526!
5527!-- Get variable from file
5528
5529       nc_stat = NF90_GET_VAR ( id, id_var, tmp,                                         &
5530                      start = (/ is+1,    js+1,    k1s+1,     k2s+1,     k3s+1 /),       &
5531                      count = (/ ie-is+1, je-js+1, k1e-k1s+1, k2e-k2s+1, k3e-k3s+1 /) )
5532
5533       CALL handle_error( 'get_variable_5d_real', 535, variable_name )
5534
5535!
5536!-- Resort (reverse index order) and standardize (from 1 to N) output array
5537
5538       DO  i = is, ie 
5539          DO  j = js, je
5540             DO  k1 = k1s, k1e
5541                DO  k2 = k2s, k2e
5542                   DO k3 = k3s, k3e
5543                      var(k3-k3s+1,k2-k2s+1,k1-k1s+1,j-js+1,i-is+1) = tmp(i,j,k1,k2,k3)
5544                   ENDDO
5545                ENDDO
5546             ENDDO
5547          ENDDO
5548       ENDDO
5549
5550       DEALLOCATE( tmp )
5551
5552#endif
5553
5554    END SUBROUTINE get_variable_5d_real
5555
5556
5557!------------------------------------------------------------------------------!
5558! Description:
5559! ------------
5560!> Reads a 5D float variables from dynamic driver, such as time-dependent xy-,
5561!> xz- or yz-boundary data as well as 5D initialization data. Please note,
5562!> the passed arguments are start indices and number of elements in each
5563!> dimension, which is in contrast to the other 3d versions where start- and
5564!> end indices are passed. The different handling of 5D dynamic variables is
5565!> due to its asymmetry for the u- and v component.
5566!> NOTE(1) - This subroutine is more flexible than get_variable_xd_real as it
5567!>           provides much better control over starting and count indices
5568!>           (ecc 20190418)
5569!> NOTE(2) - This subroutine is used specific for reading NC variable
5570!>           emission_values having a "z" dimension.  Said dimension
5571!>           is to be removed in the future and this subroutine shall
5572!>           be depreciated accordingly (ecc 20190418)
5573!------------------------------------------------------------------------------!
5574
5575    SUBROUTINE get_variable_5d_real_dynamic( id, variable_name, var,                       &
5576                                             i1s, i2s, i3s, i4s, i5s,                      &
5577                                             count_1, count_2, count_3, count_4, count_5,  &
5578                                             par_access )
5579
5580       USE indices
5581       USE pegrid
5582
5583       IMPLICIT NONE
5584
5585       CHARACTER(LEN=*)          ::  variable_name  !< variable name
5586
5587       LOGICAL                   ::  par_access     !< additional flag indicating parallel read
5588
5589       INTEGER(iwp)              ::  count_1  !< # elements read in dimension 1 wrt file
5590       INTEGER(iwp)              ::  count_2  !< # elements read in dimension 2 wrt file
5591       INTEGER(iwp)              ::  count_3  !< # elements read in dimension 3 wrt file
5592       INTEGER(iwp)              ::  count_4  !< # elements read in dimension 4 wrt file
5593       INTEGER(iwp)              ::  count_5  !< # elements read in dimension 5 wrt file
5594       INTEGER(iwp)              ::  i1       !< index for dimension 1 on file
5595       INTEGER(iwp)              ::  i1s      !< starting index for dimension 1 hyperslab
5596       INTEGER(iwp)              ::  i2       !< index for dimension 2 on file
5597       INTEGER(iwp)              ::  i2s      !< starting index for dimension 2 hyperslab
5598       INTEGER(iwp)              ::  i3       !< index for dimension 3 on file
5599       INTEGER(iwp)              ::  i3s      !< starting index for dimension 3 hyperslab
5600       INTEGER(iwp)              ::  i4       !< index for dimension 4 on file
5601       INTEGER(iwp)              ::  i4s      !< starting index for dimension 4 hyperslab
5602       INTEGER(iwp)              ::  i5       !< index for dimension 5 on file
5603       INTEGER(iwp)              ::  i5s      !< starting index for dimension 5 hyperslab
5604       INTEGER(iwp)              ::  id_var   !< netCDF variable id (varid)
5605       INTEGER(iwp)              ::  lb1      !< lower bound of dimension 1 wrt file
5606       INTEGER(iwp)              ::  lb2      !< lower bound of dimension 2 wrt file
5607       INTEGER(iwp)              ::  lb3      !< lower bound of dimension 3 wrt file
5608       INTEGER(iwp)              ::  lb4      !< lower bound of dimension 4 wrt file
5609       INTEGER(iwp)              ::  lb5      !< lower bound of dimension 5 wrt file
5610       INTEGER(iwp)              ::  ub1      !< upper bound of dimension 1 wrt file
5611       INTEGER(iwp)              ::  ub2      !< upper bound of dimension 2 wrt file
5612       INTEGER(iwp)              ::  ub3      !< upper bound of dimension 3 wrt file
5613       INTEGER(iwp)              ::  ub4      !< upper bound of dimension 4 wrt file
5614       INTEGER(iwp)              ::  ub5      !< upper bound of dimension 5 wrt file
5615       INTEGER(iwp), INTENT(IN)  ::  id       !< netCDF file id (ncid)
5616
5617       REAL(wp), DIMENSION(:,:,:,:,:), ALLOCATABLE    ::  tmp  !< temporary variable to read data
5618                                                               !< from file according is reverse
5619                                                               !< array index order
5620       REAL(wp), DIMENSION(:,:,:,:,:), INTENT(INOUT)  ::  var  !< input variable
5621       
5622#if defined( __netcdf )
5623
5624!
5625!-- Inquire variable id.
5626
5627       nc_stat = NF90_INQ_VARID( id, TRIM( variable_name ), id_var )
5628
5629!
5630!-- Check for collective read-operation and set respective NetCDF flags if required.
5631!-- Please note, in contrast to the other input routines where each PEs
5632!-- reads its subdomain data, dynamic input data not by all PEs, only
5633!-- by those which encompass lateral model boundaries. Hence, collective
5634!-- read operations are only enabled for top-boundary data.
5635
5636       IF ( collective_read  .AND.  par_access )  THEN
5637
5638#if defined( __netcdf4_parallel )       
5639          nc_stat = NF90_VAR_PAR_ACCESS (id, id_var, NF90_COLLECTIVE)
5640#endif
5641
5642       ENDIF
5643
5644!
5645!-- Allocate temporary variable according to memory access on file.
5646!-- Therefore, determine dimension bounds of input array.
5647
5648       lb1 = LBOUND(var,5)
5649       ub1 = UBOUND(var,5)
5650       lb2 = LBOUND(var,4)
5651       ub2 = UBOUND(var,4)
5652       lb3 = LBOUND(var,3)
5653       ub3 = UBOUND(var,3)
5654       lb4 = LBOUND(var,2)
5655       ub4 = UBOUND(var,2)
5656       lb5 = LBOUND(var,1)
5657       ub5 = UBOUND(var,1)
5658       ALLOCATE ( tmp(lb1:ub1,lb2:ub2,lb3:ub3,lb4:ub4,lb5:ub5) )
5659
5660!
5661!-- Get variable
5662
5663       nc_stat = NF90_GET_VAR(  id, id_var, tmp,                                         &
5664                      start = (/ i1s,     i2s,     i3s,     i4s,     i5s     /),         &
5665                      count = (/ count_1, count_2, count_3, count_4, count_5 /) )
5666
5667       CALL handle_error( 'get_variable_3d_real_dynamic', 537, variable_name )
5668
5669!
5670!-- Assign temp array to output.  Note reverse index order
5671
5672       DO  i5 = lb5, ub5
5673          DO  i4 = lb4, ub4
5674             DO  i3 = lb3, ub3
5675                DO i2 = lb2, ub2
5676                   DO  i1 = lb1, ub1
5677                      var(i5,i4,i3,i2,i1) = tmp(i1,i2,i3,i4,i5)
5678                   ENDDO
5679                ENDDO
5680             ENDDO
5681          ENDDO
5682       ENDDO
5683
5684       DEALLOCATE( tmp )
5685
5686#endif
5687
5688    END SUBROUTINE get_variable_5d_real_dynamic
5689
5690
5691!------------------------------------------------------------------------------!
5692! Description:
5693! ------------
5694!> Inquires the number of variables in a file
5695!------------------------------------------------------------------------------!
5696    SUBROUTINE inquire_num_variables( id, num_vars )
5697
5698       USE indices
5699       USE pegrid
5700
5701       IMPLICIT NONE
5702
5703       INTEGER(iwp), INTENT(IN)      ::  id              !< file id
5704       INTEGER(iwp), INTENT(INOUT)   ::  num_vars        !< number of variables in a file
5705#if defined( __netcdf )
5706
5707       nc_stat = NF90_INQUIRE( id, NVARIABLES = num_vars )
5708       CALL handle_error( 'inquire_num_variables', 539 )
5709
5710#endif
5711    END SUBROUTINE inquire_num_variables
5712
5713
5714!------------------------------------------------------------------------------!
5715! Description:
5716! ------------
5717!> Inquires the variable names belonging to a file.
5718!------------------------------------------------------------------------------!
5719    SUBROUTINE inquire_variable_names( id, var_names )
5720
5721       USE indices
5722       USE pegrid
5723
5724       IMPLICIT NONE
5725
5726       CHARACTER(LEN=*), DIMENSION(:), INTENT(INOUT) ::  var_names   !< return variable - variable names
5727       INTEGER(iwp)                                  ::  i           !< loop variable
5728       INTEGER(iwp), INTENT(IN)                      ::  id          !< file id
5729       INTEGER(iwp)                                  ::  num_vars    !< number of variables (unused return parameter)
5730       INTEGER(iwp), DIMENSION(:), ALLOCATABLE       ::  varids      !< dummy array to strore variable ids temporarily
5731#if defined( __netcdf )
5732
5733       ALLOCATE( varids(1:SIZE(var_names)) )
5734       nc_stat = NF90_INQ_VARIDS( id, NVARS = num_vars, VARIDS = varids )
5735       CALL handle_error( 'inquire_variable_names', 540 )
5736
5737       DO  i = 1, SIZE(var_names)
5738          nc_stat = NF90_INQUIRE_VARIABLE( id, varids(i), NAME = var_names(i) )
5739          CALL handle_error( 'inquire_variable_names', 540 )
5740       ENDDO
5741
5742       DEALLOCATE( varids )
5743#endif
5744    END SUBROUTINE inquire_variable_names
5745
5746!------------------------------------------------------------------------------!
5747! Description:
5748! ------------
5749!> Prints out a text message corresponding to the current status.
5750!------------------------------------------------------------------------------!
5751    SUBROUTINE handle_error( routine_name, errno, name )
5752
5753       USE control_parameters,                                                 &
5754           ONLY:  message_string
5755
5756       IMPLICIT NONE
5757
5758       CHARACTER(LEN=6) ::  message_identifier !< string for the error number
5759       CHARACTER(LEN=*) ::  routine_name       !< routine name where the error happened
5760       CHARACTER(LEN=*), OPTIONAL ::  name     !< name of variable where reading failed
5761
5762       INTEGER(iwp) ::  errno
5763#if defined( __netcdf )
5764       
5765       IF ( nc_stat /= NF90_NOERR )  THEN
5766
5767          WRITE( message_identifier, '(''NC'',I4.4)' )  errno
5768         
5769          IF ( PRESENT( name ) )  THEN
5770             message_string = "Problem reading attribute/variable - " //       &
5771                              TRIM(name) // ": " //                            &
5772                              TRIM( NF90_STRERROR( nc_stat ) )
5773          ELSE
5774             message_string = TRIM( NF90_STRERROR( nc_stat ) )
5775          ENDIF
5776
5777          CALL message( routine_name, message_identifier, 2, 2, myid, 6, 1 )
5778
5779       ENDIF
5780
5781#endif
5782    END SUBROUTINE handle_error
5783
5784
5785 END MODULE netcdf_data_input_mod
Note: See TracBrowser for help on using the repository browser.