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

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

Check if 3D building input is consistent to numeric grid.

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