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

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

last commit documented

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