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

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

Bugfix in checks for dynamic input file initialization data

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