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

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

Revise call for message routine in case of local data inconsistencies

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