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

Last change on this file since 3083 was 3054, checked in by gronemeier, 6 years ago

bugfix: force MPI abort if errors occur while reading bld heights from ASCII

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