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

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

Add read of global attributes of static driver

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