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

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

Remove default surfaces from radiation model and add check for this; revise checks for surface_fraction

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