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

Last change on this file since 2874 was 2874, checked in by knoop, 6 years ago

Bugfix: wrong placement of netcdf cpp-macros fixed

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