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

Last change on this file since 2716 was 2716, checked in by kanani, 6 years ago

Correction of "Former revisions" section

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