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

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

Speed-up NetCDF input; Revise NetCDF-input routines and remove input via io-blocks; Temporarily revoke renaming of input variables in dynamic driver; More detailed error messages created; Bugfix in mapping 3D buildings; Bugfix in land-surface model at pavement surfaces; Bugfix in initialization with inifor

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