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

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

New Inifor features: grid stretching, improved command-interface, support start dates in different formats in both YYYYMMDD and YYYYMMDDHH, Ability to manually control input file prefixes (--radiation-prefix, --soil-preifx, --flow-prefix, --soilmoisture-prefix) for compatiblity with DWD forcast naming scheme; GNU-style short and long option; Prepared output of large-scale forcing profiles (no computation yet); Added preprocessor flag netcdf4 to switch output format between netCDF 3 and 4; Updated netCDF variable names and attributes to comply with PIDS v1.9; Inifor bugfixes: Improved compatibility with older Intel Intel compilers by avoiding implicit array allocation; Added origin_lon/_lat values and correct reference time in dynamic driver global attributes; corresponding PALM changes: adjustments to revised Inifor; variables names in dynamic driver adjusted; enable geostrophic forcing also in offline nested mode; variable names in LES-LES and COSMO offline nesting changed; lateral boundary flags for nesting, in- and outflow conditions renamed

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