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

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

Reference lowest terrain height to zero level; new check for negative terrain heights in static input file

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