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

Last change on this file since 3046 was 3046, checked in by Giersch, 6 years ago

Remaining error messages revised, comments extended

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