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

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

Bugfix in parallelization of synthetic turbulence generator; revision in control mimic of synthetic turbulence generator in case of RAN-LES nesting; checks for dynamic input file added; control mimic for topography input slightly revised.

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