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

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

Revision history corrected

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