source: palm/trunk/SOURCE/surface_mod.f90 @ 2514

Last change on this file since 2514 was 2508, checked in by suehring, 7 years ago

Bugfixes in SGS-TKE buoyancy production; revised initialization of vertical-gradient levels in case of ocean runs

  • Property svn:keywords set to Id
File size: 162.0 KB
Line 
1!> @file surface_mod.f90
2!------------------------------------------------------------------------------!
3! This file is part of PALM.
4!
5! PALM is free software: you can redistribute it and/or modify it under the
6! terms of the GNU General Public License as published by the Free Software
7! Foundation, either version 3 of the License, or (at your option) any later
8! version.
9!
10! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13!
14! You should have received a copy of the GNU General Public License along with
15! PALM. If not, see <http://www.gnu.org/licenses/>.
16!
17! Copyright 1997-2017 Leibniz Universitaet Hannover
18!
19!------------------------------------------------------------------------------!
20! Current revisions:
21! ------------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: surface_mod.f90 2508 2017-10-02 08:57:09Z suehring $
27! Minor formatting adjustment
28!
29! 2478 2017-09-18 13:37:24Z suehring
30! Bugfixes in initializing model top
31!
32! 2378 2017-08-31 13:57:27Z suehring
33! Bugfix in write restart data
34!
35! 2339 2017-08-07 13:55:26Z gronemeier
36! corrected timestamp in header
37!
38! 2338 2017-08-07 12:15:38Z gronemeier
39! Modularize 1D model
40!
41! 2318 2017-07-20 17:27:44Z suehring
42! New function to obtain topography top index.
43!
44! 2317 2017-07-20 17:27:19Z suehring
45! Implementation of new microphysic scheme: cloud_scheme = 'morrison'
46! includes two more prognostic equations for cloud drop concentration (nc) 
47! and cloud water content (qc).
48!
49! 2270 2017-06-09 12:18:47Z maronga
50! Parameters removed/added due to changes in the LSM
51!
52! 2269 2017-06-09 11:57:32Z suehring
53! Formatting and description adjustments
54!
55! 2256 2017-06-07 13:58:08Z suehring
56! Enable heating at downward-facing surfaces
57!
58! 2233 2017-05-30 18:08:54Z suehring
59! Initial revision
60!
61!
62! Description:
63! ------------
64!> Surface module defines derived data structures to treat surface-
65!> bounded grid cells. Three different types of surfaces are defined:
66!> default surfaces, natural surfaces, and urban surfaces. The module
67!> encompasses the allocation and initialization of surface arrays, and handles
68!> reading and writing restart data.
69!> In addition, a further derived data structure is defined, in order to set
70!> boundary conditions at surfaces. 
71!------------------------------------------------------------------------------!
72 MODULE surface_mod
73
74    USE arrays_3d,                                                             &
75        ONLY:  zu, zw, heatflux_input_conversion, waterflux_input_conversion,  &
76               momentumflux_input_conversion
77
78    USE control_parameters               
79
80    USE indices,                                                               &
81        ONLY:  nxl, nxlg, nxr, nxrg, nys, nysg, nyn, nyng, nzb, nzt, wall_flags_0
82
83    USE grid_variables,                                                        &
84        ONLY:  dx, dy
85
86    USE kinds
87
88    USE model_1d_mod,                                                          &
89        ONLY:  rif1d, us1d, usws1d, vsws1d
90
91
92    IMPLICIT NONE
93
94!
95!-- Data type used to identify grid-points where horizontal boundary conditions
96!-- are applied
97    TYPE bc_type
98
99       INTEGER(iwp) :: ns                                  !< number of surface elements on the PE
100
101       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  i       !< x-index linking to the PALM 3D-grid 
102       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  j       !< y-index linking to the PALM 3D-grid   
103       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  k       !< z-index linking to the PALM 3D-grid   
104
105       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE :: start_index !< start index within surface data type for given (j,i)
106       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE :: end_index   !< end index within surface data type for given (j,i) 
107
108    END TYPE bc_type
109!
110!-- Data type used to identify and treat surface-bounded grid points 
111    TYPE surf_type
112
113       INTEGER(iwp) :: ns                                  !< number of surface elements on the PE
114       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  i       !< x-index linking to the PALM 3D-grid 
115       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  j       !< y-index linking to the PALM 3D-grid   
116       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  k       !< z-index linking to the PALM 3D-grid       
117
118       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  facing  !< Bit indicating surface orientation
119     
120       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE :: start_index !< Start index within surface data type for given (j,i)
121       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE :: end_index   !< End index within surface data type for given (j,i) 
122
123       REAL(wp), DIMENSION(:), ALLOCATABLE ::  z_mo      !< surface-layer height
124       REAL(wp), DIMENSION(:), ALLOCATABLE ::  uvw_abs   !< absolute surface-parallel velocity
125       REAL(wp), DIMENSION(:), ALLOCATABLE ::  us        !< friction velocity
126       REAL(wp), DIMENSION(:), ALLOCATABLE ::  ts        !< scaling parameter temerature
127       REAL(wp), DIMENSION(:), ALLOCATABLE ::  qs        !< scaling parameter humidity
128       REAL(wp), DIMENSION(:), ALLOCATABLE ::  ss        !< scaling parameter passive scalar
129       REAL(wp), DIMENSION(:), ALLOCATABLE ::  qcs       !< scaling parameter qc
130       REAL(wp), DIMENSION(:), ALLOCATABLE ::  ncs       !< scaling parameter nc
131       REAL(wp), DIMENSION(:), ALLOCATABLE ::  qrs       !< scaling parameter qr
132       REAL(wp), DIMENSION(:), ALLOCATABLE ::  nrs       !< scaling parameter nr
133
134       REAL(wp), DIMENSION(:), ALLOCATABLE ::  ol        !< Obukhov length
135       REAL(wp), DIMENSION(:), ALLOCATABLE ::  rib       !< Richardson bulk number
136
137       REAL(wp), DIMENSION(:), ALLOCATABLE ::  z0        !< roughness length for momentum
138       REAL(wp), DIMENSION(:), ALLOCATABLE ::  z0h       !< roughness length for heat
139       REAL(wp), DIMENSION(:), ALLOCATABLE ::  z0q       !< roughness length for humidity
140
141       REAL(wp), DIMENSION(:), ALLOCATABLE ::  pt1       !< Specific humidity at first grid level (required for cloud_physics = .T.)
142       REAL(wp), DIMENSION(:), ALLOCATABLE ::  qv1       !< Potential temperature at first grid level (required for cloud_physics = .T.)
143!
144!--    Define arrays for surface fluxes
145       REAL(wp), DIMENSION(:), ALLOCATABLE ::  usws      !< vertical momentum flux for u-component at horizontal surfaces
146       REAL(wp), DIMENSION(:), ALLOCATABLE ::  vsws      !< vertical momentum flux for v-component at horizontal surfaces
147
148       REAL(wp), DIMENSION(:), ALLOCATABLE ::  shf       !< surface flux sensible heat
149       REAL(wp), DIMENSION(:), ALLOCATABLE ::  qsws      !< surface flux latent heat
150       REAL(wp), DIMENSION(:), ALLOCATABLE ::  ssws      !< surface flux passive scalar
151       REAL(wp), DIMENSION(:), ALLOCATABLE ::  qcsws     !< surface flux qc
152       REAL(wp), DIMENSION(:), ALLOCATABLE ::  ncsws     !< surface flux nc
153       REAL(wp), DIMENSION(:), ALLOCATABLE ::  qrsws     !< surface flux qr
154       REAL(wp), DIMENSION(:), ALLOCATABLE ::  nrsws     !< surface flux nr
155       REAL(wp), DIMENSION(:), ALLOCATABLE ::  sasws     !< surface flux salinity
156!
157!--    Required for horizontal walls in production_e
158       REAL(wp), DIMENSION(:), ALLOCATABLE ::  u_0       !< virtual velocity component (see production_e_init for further explanation)
159       REAL(wp), DIMENSION(:), ALLOCATABLE ::  v_0       !< virtual velocity component (see production_e_init for further explanation)
160
161       REAL(wp), DIMENSION(:), ALLOCATABLE   ::  mom_flux_uv  !< momentum flux usvs and vsus at vertical surfaces (used in diffusion_u and diffusion_v)
162       REAL(wp), DIMENSION(:), ALLOCATABLE   ::  mom_flux_w   !< momentum flux wsus and wsvs at vertical surfaces (used in diffusion_w)
163       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  mom_flux_tke !< momentum flux usvs, vsus, wsus, wsvs at vertical surfaces at grid center (used in production_e)
164!
165!--    Variables required for LSM as well as for USM
166       REAL(wp), DIMENSION(:), ALLOCATABLE   ::  pt_surface        !< skin-surface temperature
167       REAL(wp), DIMENSION(:), ALLOCATABLE   ::  rad_net_l         !< net radiation
168       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  lambda_h          !< heat conductivity of soil (W/m/K)
169       REAL(wp), DIMENSION(:), ALLOCATABLE   ::  lambda_h_def      !< default heat conductivity of soil (W/m/K)   
170       
171       LOGICAL, DIMENSION(:), ALLOCATABLE  ::  building_surface    !< flag parameter indicating that the surface element is covered by buildings (no LSM actions, not implemented yet)
172       LOGICAL, DIMENSION(:), ALLOCATABLE  ::  pavement_surface    !< flag parameter for pavements
173       LOGICAL, DIMENSION(:), ALLOCATABLE  ::  water_surface       !< flag parameter for water surfaces
174       LOGICAL, DIMENSION(:), ALLOCATABLE  ::  vegetation_surface     !< flag parameter for natural land surfaces
175
176       REAL(wp), DIMENSION(:), ALLOCATABLE ::  c_liq               !< liquid water coverage (of vegetated area)
177       REAL(wp), DIMENSION(:), ALLOCATABLE ::  c_veg               !< vegetation coverage
178       REAL(wp), DIMENSION(:), ALLOCATABLE ::  f_sw_in             !< fraction of absorbed shortwave radiation by the surface layer (not implemented yet)
179       REAL(wp), DIMENSION(:), ALLOCATABLE ::  ghf              !< ground heat flux
180       REAL(wp), DIMENSION(:), ALLOCATABLE ::  g_d                 !< coefficient for dependence of r_canopy on water vapour pressure deficit
181       REAL(wp), DIMENSION(:), ALLOCATABLE ::  lai                 !< leaf area index
182       REAL(wp), DIMENSION(:), ALLOCATABLE ::  lambda_surface_u    !< coupling between surface and soil (depends on vegetation type) (W/m2/K)
183       REAL(wp), DIMENSION(:), ALLOCATABLE ::  lambda_surface_s    !< coupling between surface and soil (depends on vegetation type) (W/m2/K)
184       REAL(wp), DIMENSION(:), ALLOCATABLE ::  qsws_liq         !< surface flux of latent heat (liquid water portion)
185       REAL(wp), DIMENSION(:), ALLOCATABLE ::  qsws_soil        !< surface flux of latent heat (soil portion)
186       REAL(wp), DIMENSION(:), ALLOCATABLE ::  qsws_veg         !< surface flux of latent heat (vegetation portion)
187       REAL(wp), DIMENSION(:), ALLOCATABLE ::  r_a                 !< aerodynamic resistance
188       REAL(wp), DIMENSION(:), ALLOCATABLE ::  r_canopy            !< canopy resistance
189       REAL(wp), DIMENSION(:), ALLOCATABLE ::  r_soil              !< soil resistance
190       REAL(wp), DIMENSION(:), ALLOCATABLE ::  r_soil_min          !< minimum soil resistance
191       REAL(wp), DIMENSION(:), ALLOCATABLE ::  r_s                 !< total surface resistance (combination of r_soil and r_canopy)
192       REAL(wp), DIMENSION(:), ALLOCATABLE ::  r_canopy_min        !< minimum canopy (stomatal) resistance
193
194       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  alpha_vg          !< coef. of Van Genuchten
195       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  lambda_w          !< hydraulic diffusivity of soil (?)
196       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  gamma_w           !< hydraulic conductivity of soil (W/m/K)
197       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  gamma_w_sat       !< hydraulic conductivity at saturation
198       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  l_vg              !< coef. of Van Genuchten
199       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  m_fc              !< soil moisture at field capacity (m3/m3)
200       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  m_res             !< residual soil moisture
201       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  m_sat             !< saturation soil moisture (m3/m3)
202       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  m_wilt            !< soil moisture at permanent wilting point (m3/m3)
203       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  n_vg              !< coef. Van Genuchten 
204       REAL(wp), DIMENSION(:),   ALLOCATABLE ::  rho_c_def         !< default volumetric heat capacity of the (soil) layer (J/m3/K)
205       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  rho_c_total       !< volumetric heat capacity of the actual soil matrix (J/m3/K)
206       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  root_fr           !< root fraction within the soil layers
207!
208!--    Urban surface variables
209       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  surface_types   !< array of types of wall parameters
210
211       LOGICAL, DIMENSION(:), ALLOCATABLE  ::  isroof_surf         !< flag indication roof surfaces
212
213       REAL(wp), DIMENSION(:), ALLOCATABLE ::  albedo_surf         !< albedo of the surface
214       REAL(wp), DIMENSION(:), ALLOCATABLE ::  c_surface           !< heat capacity of the wall surface skin (J/m2/K)
215       REAL(wp), DIMENSION(:), ALLOCATABLE ::  emiss_surf          !< emissivity of the wall surface
216       REAL(wp), DIMENSION(:), ALLOCATABLE ::  lambda_surf         !< heat conductivity between air and surface (W/m2/K)
217       REAL(wp), DIMENSION(:), ALLOCATABLE ::  roughness_wall      !< roughness relative to concrete
218       REAL(wp), DIMENSION(:), ALLOCATABLE ::  thickness_wall      !< thickness of the wall, roof and soil layers
219
220       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfoutsl           !< reflected shortwave radiation for local surface in i-th reflection
221       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfoutll           !< reflected + emitted longwave radiation for local surface in i-th reflection
222       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfhf              !< total radiation flux incoming to minus outgoing from local surface
223
224       REAL(wp), DIMENSION(:), ALLOCATABLE ::  tt_surface_m        !< surface temperature tendency (K)
225       REAL(wp), DIMENSION(:), ALLOCATABLE ::  wshf                !< kinematic wall heat flux of sensible heat (actually no longer needed)
226       REAL(wp), DIMENSION(:), ALLOCATABLE ::  wshf_eb             !< wall heat flux of sensible heat in wall normal direction
227
228
229       REAL(wp), DIMENSION(:), ALLOCATABLE ::  wghf_eb             !< wall ground heat flux
230
231       REAL(wp), DIMENSION(:), ALLOCATABLE ::  rad_in_sw           !< incoming shortwave radiation
232       REAL(wp), DIMENSION(:), ALLOCATABLE ::  rad_out_sw          !< emitted shortwave radiation
233       REAL(wp), DIMENSION(:), ALLOCATABLE ::  rad_in_lw           !< incoming longwave radiation
234       REAL(wp), DIMENSION(:), ALLOCATABLE ::  rad_out_lw          !< emitted longwave radiation
235
236       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfinsw            !< shortwave radiation falling to local surface including radiation from reflections
237       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfoutsw           !< total shortwave radiation outgoing from nonvirtual surfaces surfaces after all reflection
238       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfinlw            !< longwave radiation falling to local surface including radiation from reflections
239       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfoutlw           !< total longwave radiation outgoing from nonvirtual surfaces surfaces after all reflection
240
241
242       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  rho_c_wall        !< volumetric heat capacity of the material ( J m-3 K-1 ) (= 2.19E6)
243       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  dz_wall           !< wall grid spacing (center-center)
244       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  ddz_wall          !< 1/dz_wall
245       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  dz_wall_stag      !< wall grid spacing (edge-edge)
246       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  ddz_wall_stag     !< 1/dz_wall_stag
247       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  tt_wall_m         !< t_wall prognostic array
248       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  zw                !< wall layer depths (m)
249
250
251!-- arrays for time averages
252       REAL(wp), DIMENSION(:), ALLOCATABLE ::  rad_net_av       !< average of rad_net_l
253       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfinsw_av      !< average of sw radiation falling to local surface including radiation from reflections
254       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfinlw_av      !< average of lw radiation falling to local surface including radiation from reflections
255       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfinswdir_av   !< average of direct sw radiation falling to local surface
256       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfinswdif_av   !< average of diffuse sw radiation from sky and model boundary falling to local surface
257       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfinlwdif_av   !< average of diffuse lw radiation from sky and model boundary falling to local surface
258       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfinswref_av   !< average of sw radiation falling to surface from reflections
259       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfinlwref_av   !< average of lw radiation falling to surface from reflections
260       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfoutsw_av     !< average of total sw radiation outgoing from nonvirtual surfaces surfaces after all reflection
261       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfoutlw_av     !< average of total lw radiation outgoing from nonvirtual surfaces surfaces after all reflection
262       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfins_av       !< average of array of residua of sw radiation absorbed in surface after last reflection
263       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfinl_av       !< average of array of residua of lw radiation absorbed in surface after last reflection
264       REAL(wp), DIMENSION(:), ALLOCATABLE ::  surfhf_av        !< average of total radiation flux incoming to minus outgoing from local surface 
265       REAL(wp), DIMENSION(:), ALLOCATABLE ::  wghf_eb_av       !< average of wghf_eb
266       REAL(wp), DIMENSION(:), ALLOCATABLE ::  wshf_eb_av       !< average of wshf_eb
267       REAL(wp), DIMENSION(:), ALLOCATABLE ::  t_surf_av        !< average of wall surface temperature (K)
268
269       REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  t_wall_av      !< Average of t_wall
270
271    END TYPE surf_type
272
273    TYPE (bc_type), DIMENSION(0:1)           ::  bc_h        !< boundary condition data type, horizontal upward- and downward facing surfaces
274
275    TYPE (surf_type), DIMENSION(0:2), TARGET ::  surf_def_h  !< horizontal default surfaces (Up, Down, and Top)
276    TYPE (surf_type), DIMENSION(0:3), TARGET ::  surf_def_v  !< vertical default surfaces (North, South, West, East)
277    TYPE (surf_type)                , TARGET ::  surf_lsm_h  !< horizontal natural land surfaces, so far only upward-facing
278    TYPE (surf_type), DIMENSION(0:3), TARGET ::  surf_lsm_v  !< vertical land surfaces (North, South, West, East)
279    TYPE (surf_type)                , TARGET ::  surf_usm_h  !< horizontal urban surfaces, so far only upward-facing
280    TYPE (surf_type), DIMENSION(0:3), TARGET ::  surf_usm_v  !< vertical urban surfaces (North, South, West, East)
281
282    INTEGER(iwp) ::  ns_h_on_file(0:2)                       !< total number of horizontal surfaces with the same facing, required for writing restart data
283    INTEGER(iwp) ::  ns_v_on_file(0:3)                       !< total number of vertical surfaces with the same facing, required for writing restart data
284
285
286    SAVE
287
288    PRIVATE
289
290    INTERFACE get_topography_top_index
291       MODULE PROCEDURE get_topography_top_index
292    END  INTERFACE get_topography_top_index
293
294    INTERFACE init_bc
295       MODULE PROCEDURE init_bc
296    END INTERFACE init_bc
297
298    INTERFACE init_surfaces
299       MODULE PROCEDURE init_surfaces
300    END INTERFACE init_surfaces
301
302    INTERFACE init_surface_arrays
303       MODULE PROCEDURE init_surface_arrays
304    END INTERFACE init_surface_arrays
305
306    INTERFACE surface_read_restart_data
307       MODULE PROCEDURE surface_read_restart_data
308    END INTERFACE surface_read_restart_data
309
310    INTERFACE surface_write_restart_data
311       MODULE PROCEDURE surface_write_restart_data
312    END INTERFACE surface_write_restart_data
313
314    INTERFACE surface_last_actions
315       MODULE PROCEDURE surface_last_actions
316    END INTERFACE surface_last_actions
317
318!
319!-- Public variables
320    PUBLIC bc_h, ns_h_on_file, ns_v_on_file, surf_def_h, surf_def_v,           &
321           surf_lsm_h, surf_lsm_v, surf_usm_h, surf_usm_v, surf_type
322!
323!-- Public subroutines and functions
324    PUBLIC get_topography_top_index, init_bc, init_surfaces,                   &
325           init_surface_arrays, surface_read_restart_data,                     &
326           surface_write_restart_data, surface_last_actions
327
328
329 CONTAINS
330
331!------------------------------------------------------------------------------!
332! Description:
333! ------------
334!> Initialize data type for setting boundary conditions at horizontal surfaces.
335!------------------------------------------------------------------------------!
336    SUBROUTINE init_bc
337
338       IMPLICIT NONE
339
340       INTEGER(iwp) ::  i         !<
341       INTEGER(iwp) ::  j         !<
342       INTEGER(iwp) ::  k         !<
343
344       INTEGER(iwp), DIMENSION(0:1) ::  num_h         !<
345       INTEGER(iwp), DIMENSION(0:1) ::  num_h_kji     !<
346       INTEGER(iwp), DIMENSION(0:1) ::  start_index_h !<
347
348!
349!--    First of all, count the number of upward- and downward-facing surfaces
350       num_h = 0
351       DO  i = nxlg, nxrg
352          DO  j = nysg, nyng
353             DO  k = nzb+1, nzt
354!
355!--             Check if current gridpoint belongs to the atmosphere
356                IF ( BTEST( wall_flags_0(k,j,i), 0 ) )  THEN
357!
358!--                Upward-facing
359                   IF ( .NOT. BTEST( wall_flags_0(k-1,j,i), 0 ) )              &
360                      num_h(0) = num_h(0) + 1
361!
362!--                Downward-facing
363                   IF ( .NOT. BTEST( wall_flags_0(k+1,j,i), 0 ) )              &
364                      num_h(1) = num_h(1) + 1
365                ENDIF
366             ENDDO
367          ENDDO
368       ENDDO
369!
370!--    Save the number of surface elements
371       bc_h(0)%ns = num_h(0)
372       bc_h(1)%ns = num_h(1)
373!
374!--    ALLOCATE data type variables
375!--    Upward facing
376       ALLOCATE( bc_h(0)%i(1:bc_h(0)%ns) )
377       ALLOCATE( bc_h(0)%j(1:bc_h(0)%ns) )
378       ALLOCATE( bc_h(0)%k(1:bc_h(0)%ns) )
379       ALLOCATE( bc_h(0)%start_index(nysg:nyng,nxlg:nxrg) )
380       ALLOCATE( bc_h(0)%end_index(nysg:nyng,nxlg:nxrg)   )
381       bc_h(0)%start_index = 1
382       bc_h(0)%end_index   = 0
383!
384!--    Downward facing
385       ALLOCATE( bc_h(1)%i(1:bc_h(1)%ns) )
386       ALLOCATE( bc_h(1)%j(1:bc_h(1)%ns) )
387       ALLOCATE( bc_h(1)%k(1:bc_h(1)%ns) )
388       ALLOCATE( bc_h(1)%start_index(nysg:nyng,nxlg:nxrg) )
389       ALLOCATE( bc_h(1)%end_index(nysg:nyng,nxlg:nxrg)   )
390       bc_h(1)%start_index = 1
391       bc_h(1)%end_index   = 0
392!
393!--    Store the respective indices on data type
394       num_h(0:1)         = 1
395       start_index_h(0:1) = 1
396       DO  i = nxlg, nxrg
397          DO  j = nysg, nyng
398
399             num_h_kji(0:1) = 0
400             DO  k = nzb+1, nzt
401!
402!--             Check if current gridpoint belongs to the atmosphere
403                IF ( BTEST( wall_flags_0(k,j,i), 0 ) )  THEN
404!
405!--                Upward-facing
406                   IF ( .NOT. BTEST( wall_flags_0(k-1,j,i), 0 ) )  THEN
407                      bc_h(0)%i(num_h(0)) = i
408                      bc_h(0)%j(num_h(0)) = j
409                      bc_h(0)%k(num_h(0)) = k
410                      num_h_kji(0)        = num_h_kji(0) + 1
411                      num_h(0)            = num_h(0) + 1
412                   ENDIF
413!
414!--                Downward-facing
415                   IF ( .NOT. BTEST( wall_flags_0(k+1,j,i), 0 ) )  THEN
416                      bc_h(1)%i(num_h(1)) = i
417                      bc_h(1)%j(num_h(1)) = j
418                      bc_h(1)%k(num_h(1)) = k
419                      num_h_kji(1)        = num_h_kji(1) + 1
420                      num_h(1)            = num_h(1) + 1
421                   ENDIF
422                ENDIF
423             ENDDO
424             bc_h(0)%start_index(j,i) = start_index_h(0)
425             bc_h(0)%end_index(j,i)   = bc_h(0)%start_index(j,i) + num_h_kji(0) - 1
426             start_index_h(0)         = bc_h(0)%end_index(j,i) + 1
427
428             bc_h(1)%start_index(j,i) = start_index_h(1)
429             bc_h(1)%end_index(j,i)   = bc_h(1)%start_index(j,i) + num_h_kji(1) - 1
430             start_index_h(1)         = bc_h(1)%end_index(j,i) + 1
431          ENDDO
432       ENDDO
433
434
435    END SUBROUTINE init_bc
436
437
438!------------------------------------------------------------------------------!
439! Description:
440! ------------
441!> Initialize horizontal and vertical surfaces. Counts the number of default-,
442!> natural and urban surfaces and allocates memory, respectively.
443!------------------------------------------------------------------------------!
444    SUBROUTINE init_surface_arrays
445
446       IMPLICIT NONE
447
448       INTEGER(iwp)                 ::  i         !< running index x-direction
449       INTEGER(iwp)                 ::  j         !< running index y-direction
450       INTEGER(iwp)                 ::  k         !< running index z-direction
451       INTEGER(iwp)                 ::  l         !< index variable for surface facing
452       INTEGER(iwp)                 ::  num_lsm_h !< number of horizontally-aligned natural surfaces
453       INTEGER(iwp)                 ::  num_usm_h !< number of horizontally-aligned urban surfaces
454
455       INTEGER(iwp), DIMENSION(0:2) ::  num_def_h !< number of horizontally-aligned default surfaces
456       INTEGER(iwp), DIMENSION(0:3) ::  num_def_v !< number of vertically-aligned default surfaces
457       INTEGER(iwp), DIMENSION(0:3) ::  num_lsm_v !< number of vertically-aligned natural surfaces
458       INTEGER(iwp), DIMENSION(0:3) ::  num_usm_v !< number of vertically-aligned urban surfaces
459
460
461       num_def_h = 0
462       num_def_v = 0
463       num_lsm_h = 0
464       num_lsm_v = 0
465       num_usm_h = 0
466       num_usm_v = 0
467!
468!--    Count number of horizontal surfaces on local domain
469       DO  i = nxl, nxr
470          DO  j = nys, nyn
471             DO  k = nzb+1, nzt
472!
473!--             Check if current gridpoint belongs to the atmosphere
474                IF ( BTEST( wall_flags_0(k,j,i), 0 ) )  THEN
475!
476!--                Check if grid point adjoins to any upward-facing horizontal
477!--                surface, e.g. the Earth surface, plane roofs, or ceilings.
478                   IF ( .NOT. BTEST( wall_flags_0(k-1,j,i), 0 ) )  THEN
479!
480!--                   Land-surface type
481                      IF ( land_surface )  THEN
482                         num_lsm_h    = num_lsm_h    + 1 
483!
484!--                   Urban surface tpye
485                      ELSEIF ( urban_surface )  THEN
486                         num_usm_h    = num_usm_h    + 1 
487!
488!--                   Default-surface type
489                      ELSE
490                         num_def_h(0) = num_def_h(0) + 1 
491                      ENDIF
492
493                   ENDIF
494!
495!--                Check for top-fluxes
496                   IF ( k == nzt  .AND.  use_top_fluxes )  THEN
497                      num_def_h(2) = num_def_h(2) + 1
498!
499!--                Check for any other downward-facing surface. So far only for
500!--                default surface type.
501                   ELSEIF ( .NOT. BTEST( wall_flags_0(k+1,j,i), 0 ) )  THEN
502                      num_def_h(1) = num_def_h(1) + 1
503                   ENDIF
504
505                ENDIF
506             ENDDO
507          ENDDO
508       ENDDO
509!
510!--    Count number of vertical surfaces on local domain
511       DO  i = nxl, nxr
512          DO  j = nys, nyn
513             DO  k = nzb+1, nzt
514                IF ( BTEST( wall_flags_0(k,j,i), 0 ) )  THEN
515!
516!--                Northward-facing
517                   IF ( .NOT. BTEST( wall_flags_0(k,j-1,i), 0 ) )  THEN
518                      IF ( urban_surface )  THEN
519                         num_usm_v(0) = num_usm_v(0) + 1 
520                      ELSEIF ( land_surface )  THEN
521                         num_lsm_v(0) = num_lsm_v(0) + 1 
522                      ELSE
523                         num_def_v(0) = num_def_v(0) + 1 
524                      ENDIF
525                   ENDIF
526!
527!--                Southward-facing
528                   IF ( .NOT. BTEST( wall_flags_0(k,j+1,i), 0 ) )  THEN
529                      IF ( urban_surface )  THEN
530                         num_usm_v(1) = num_usm_v(1) + 1 
531                      ELSEIF ( land_surface )  THEN
532                         num_lsm_v(1) = num_lsm_v(1) + 1 
533                      ELSE
534                         num_def_v(1) = num_def_v(1) + 1 
535                      ENDIF
536                   ENDIF
537!
538!--                Eastward-facing
539                   IF ( .NOT. BTEST( wall_flags_0(k,j,i-1), 0 ) )  THEN
540                      IF ( urban_surface )  THEN
541                         num_usm_v(2) = num_usm_v(2) + 1 
542                      ELSEIF ( land_surface )  THEN
543                         num_lsm_v(2) = num_lsm_v(2) + 1 
544                      ELSE
545                         num_def_v(2) = num_def_v(2) + 1 
546                      ENDIF
547                   ENDIF
548!
549!--                Westward-facing
550                   IF ( .NOT. BTEST( wall_flags_0(k,j,i+1), 0 ) )  THEN
551                      IF ( urban_surface )  THEN
552                         num_usm_v(3) = num_usm_v(3) + 1
553                      ELSEIF ( land_surface )  THEN
554                         num_lsm_v(3) = num_lsm_v(3) + 1 
555                      ELSE
556                         num_def_v(3) = num_def_v(3) + 1 
557                      ENDIF
558                   ENDIF
559                ENDIF
560             ENDDO
561          ENDDO
562       ENDDO
563
564!
565!--    Store number of surfaces per core.
566!--    Horizontal surface, default type, upward facing
567       surf_def_h(0)%ns = num_def_h(0)
568!
569!--    Horizontal surface, default type, downward facing
570       surf_def_h(1)%ns = num_def_h(1)
571!
572!--    Horizontal surface, default type, top downward facing
573       surf_def_h(2)%ns = num_def_h(2)
574!
575!--    Horizontal surface, natural type, so far only upward-facing
576       surf_lsm_h%ns    = num_lsm_h 
577!
578!--    Horizontal surface, urban type, so far only upward-facing
579       surf_usm_h%ns    = num_usm_h   
580!
581!--    Vertical surface, default type, northward facing
582       surf_def_v(0)%ns = num_def_v(0)
583!
584!--    Vertical surface, default type, southward facing
585       surf_def_v(1)%ns = num_def_v(1)
586!
587!--    Vertical surface, default type, eastward facing
588       surf_def_v(2)%ns = num_def_v(2)
589!
590!--    Vertical surface, default type, westward facing
591       surf_def_v(3)%ns = num_def_v(3)
592!
593!--    Vertical surface, natural type, northward facing
594       surf_lsm_v(0)%ns = num_lsm_v(0)
595!
596!--    Vertical surface, natural type, southward facing
597       surf_lsm_v(1)%ns = num_lsm_v(1)
598!
599!--    Vertical surface, natural type, eastward facing
600       surf_lsm_v(2)%ns = num_lsm_v(2)
601!
602!--    Vertical surface, natural type, westward facing
603       surf_lsm_v(3)%ns = num_lsm_v(3)
604!
605!--    Vertical surface, urban type, northward facing
606       surf_usm_v(0)%ns = num_usm_v(0)
607!
608!--    Vertical surface, urban type, southward facing
609       surf_usm_v(1)%ns = num_usm_v(1)
610!
611!--    Vertical surface, urban type, eastward facing
612       surf_usm_v(2)%ns = num_usm_v(2)
613!
614!--    Vertical surface, urban type, westward facing
615       surf_usm_v(3)%ns = num_usm_v(3)
616!
617!--    Allocate required attributes for horizontal surfaces - default type.
618!--    Upward-facing (l=0) and downward-facing (l=1).
619       DO  l = 0, 1
620          CALL allocate_surface_attributes_h ( surf_def_h(l), nys, nyn, nxl, nxr )
621       ENDDO
622!
623!--    Allocate required attributes for model top
624       CALL allocate_surface_attributes_h_top ( surf_def_h(2), nys, nyn, nxl, nxr )
625!
626!--    Allocate required attributes for horizontal surfaces - natural type.
627       CALL allocate_surface_attributes_h ( surf_lsm_h, nys, nyn, nxl, nxr )
628!
629!--    Allocate required attributes for horizontal surfaces - urban type.
630       CALL allocate_surface_attributes_h ( surf_usm_h, nys, nyn, nxl, nxr )
631
632!
633!--    Allocate required attributes for vertical surfaces.
634!--    Northward-facing (l=0), southward-facing (l=1), eastward-facing (l=2)
635!--    and westward-facing (l=3).
636!--    Default type.
637       DO  l = 0, 3
638          CALL allocate_surface_attributes_v ( surf_def_v(l), .FALSE.,         &
639                                               nys, nyn, nxl, nxr )
640       ENDDO
641!
642!--    Natural type
643       DO  l = 0, 3
644          CALL allocate_surface_attributes_v ( surf_lsm_v(l), .TRUE.,          &
645                                               nys, nyn, nxl, nxr )
646       ENDDO
647!
648!--    Urban type
649       DO  l = 0, 3
650          CALL allocate_surface_attributes_v ( surf_usm_v(l), .FALSE.,         &
651                                               nys, nyn, nxl, nxr )
652       ENDDO
653
654    END SUBROUTINE init_surface_arrays
655
656!------------------------------------------------------------------------------!
657! Description:
658! ------------
659!> Allocating memory for upward and downward-facing horizontal surface types,
660!> except for top fluxes.
661!------------------------------------------------------------------------------!
662    SUBROUTINE allocate_surface_attributes_h( surfaces,                        &
663                                              nys_l, nyn_l, nxl_l, nxr_l )
664
665       IMPLICIT NONE
666
667       INTEGER(iwp) ::  nyn_l  !< north bound of local 2d array start/end_index, is equal to nyn, except for restart-array
668       INTEGER(iwp) ::  nys_l  !< south bound of local 2d array start/end_index, is equal to nyn, except for restart-array
669       INTEGER(iwp) ::  nxl_l  !< west bound of local 2d array start/end_index, is equal to nyn, except for restart-array
670       INTEGER(iwp) ::  nxr_l  !< east bound of local 2d array start/end_index, is equal to nyn, except for restart-array
671
672       TYPE(surf_type) ::  surfaces  !< respective surface type
673
674!
675!--    Allocate arrays for start and end index of horizontal surface type
676!--    for each (j,i)-grid point. This is required e.g. in diffion_x, which is
677!--    called for each (j,i). In order to find the location where the
678!--    respective flux is store within the surface-type, start- and end-
679!--    index are stored for each (j,i). For example, each (j,i) can have
680!--    several entries where fluxes for horizontal surfaces might be stored,
681!--    e.g. for overhanging structures where several upward-facing surfaces
682!--    might exist for given (j,i).
683!--    If no surface of respective type exist at current (j,i), set indicies
684!--    such that loop in diffusion routines will not be entered.
685       ALLOCATE ( surfaces%start_index(nys_l:nyn_l,nxl_l:nxr_l) )
686       ALLOCATE ( surfaces%end_index(nys_l:nyn_l,nxl_l:nxr_l)   )
687       surfaces%start_index = 0
688       surfaces%end_index   = -1
689!
690!--    Indices to locate surface element
691       ALLOCATE ( surfaces%i(1:surfaces%ns)  )
692       ALLOCATE ( surfaces%j(1:surfaces%ns)  )
693       ALLOCATE ( surfaces%k(1:surfaces%ns)  )
694!
695!--    Surface-layer height
696       ALLOCATE ( surfaces%z_mo(1:surfaces%ns) )
697!
698!--    Surface orientation
699       ALLOCATE ( surfaces%facing(1:surfaces%ns) )
700!
701!--    Surface-parallel wind velocity
702       ALLOCATE ( surfaces%uvw_abs(1:surfaces%ns) )
703!      ALLOCATE ( surfaces%pt_surface(1:surfaces%ns) )
704!
705!--    Roughness
706       ALLOCATE ( surfaces%z0(1:surfaces%ns)  )
707       ALLOCATE ( surfaces%z0h(1:surfaces%ns) )
708       ALLOCATE ( surfaces%z0q(1:surfaces%ns) )
709!
710!--    Friction velocity
711       ALLOCATE ( surfaces%us(1:surfaces%ns) )
712!
713!--    Stability parameter
714       ALLOCATE ( surfaces%ol(1:surfaces%ns) )
715!
716!--    Bulk Richardson number
717       ALLOCATE ( surfaces%rib(1:surfaces%ns) )
718!
719!--    Vertical momentum fluxes of u and v
720       ALLOCATE ( surfaces%usws(1:surfaces%ns) ) 
721       ALLOCATE ( surfaces%vsws(1:surfaces%ns) ) 
722!
723!--    Required in production_e
724       IF ( .NOT. constant_diffusion )  THEN   
725          ALLOCATE ( surfaces%u_0(1:surfaces%ns) ) 
726          ALLOCATE ( surfaces%v_0(1:surfaces%ns) )
727       ENDIF 
728!
729!--    Characteristic temperature and surface flux of sensible heat
730       ALLOCATE ( surfaces%ts(1:surfaces%ns)  )   
731       ALLOCATE ( surfaces%shf(1:surfaces%ns) )   
732!
733!--    Characteristic humidity and surface flux of latent heat
734       IF ( humidity )  THEN
735          ALLOCATE ( surfaces%qs(1:surfaces%ns)   ) 
736          ALLOCATE ( surfaces%qsws(1:surfaces%ns) )     
737       ENDIF 
738!
739!--    Characteristic scalar and surface flux of scalar
740       IF ( passive_scalar )  THEN
741          ALLOCATE ( surfaces%ss(1:surfaces%ns)   )   
742          ALLOCATE ( surfaces%ssws(1:surfaces%ns) ) 
743       ENDIF 
744!
745!--    When cloud physics is used, arrays for storing potential temperature and
746!--    specific humidity at first grid level are required.
747       IF ( cloud_physics )  THEN
748          ALLOCATE ( surfaces%pt1(1:surfaces%ns) )
749          ALLOCATE ( surfaces%qv1(1:surfaces%ns) )
750       ENDIF
751!
752!--       
753       IF ( cloud_physics .AND. microphysics_morrison)  THEN
754          ALLOCATE ( surfaces%qcs(1:surfaces%ns)   )
755          ALLOCATE ( surfaces%ncs(1:surfaces%ns)   )
756          ALLOCATE ( surfaces%qcsws(1:surfaces%ns) )
757          ALLOCATE ( surfaces%ncsws(1:surfaces%ns) )
758       ENDIF
759!
760!--       
761       IF ( cloud_physics .AND. microphysics_seifert)  THEN
762          ALLOCATE ( surfaces%qrs(1:surfaces%ns)   )
763          ALLOCATE ( surfaces%nrs(1:surfaces%ns)   )
764          ALLOCATE ( surfaces%qrsws(1:surfaces%ns) )
765          ALLOCATE ( surfaces%nrsws(1:surfaces%ns) )
766       ENDIF
767!
768!--    Salinity surface flux
769       IF ( ocean )  ALLOCATE ( surfaces%sasws(1:surfaces%ns) )
770
771    END SUBROUTINE allocate_surface_attributes_h
772
773
774!------------------------------------------------------------------------------!
775! Description:
776! ------------
777!> Allocating memory for model-top fluxes 
778!------------------------------------------------------------------------------!
779    SUBROUTINE allocate_surface_attributes_h_top( surfaces,                    &
780                                                  nys_l, nyn_l, nxl_l, nxr_l )
781
782       IMPLICIT NONE
783
784       INTEGER(iwp) ::  nyn_l  !< north bound of local 2d array start/end_index, is equal to nyn, except for restart-array
785       INTEGER(iwp) ::  nys_l  !< south bound of local 2d array start/end_index, is equal to nyn, except for restart-array
786       INTEGER(iwp) ::  nxl_l  !< west bound of local 2d array start/end_index, is equal to nyn, except for restart-array
787       INTEGER(iwp) ::  nxr_l  !< east bound of local 2d array start/end_index, is equal to nyn, except for restart-array
788
789       TYPE(surf_type) ::  surfaces !< respective surface type
790
791       ALLOCATE ( surfaces%start_index(nys_l:nyn_l,nxl_l:nxr_l) )
792       ALLOCATE ( surfaces%end_index(nys_l:nyn_l,nxl_l:nxr_l)   )
793       surfaces%start_index = 0
794       surfaces%end_index   = -1
795!
796!--    Indices to locate surface (model-top) element
797       ALLOCATE ( surfaces%i(1:surfaces%ns)  )
798       ALLOCATE ( surfaces%j(1:surfaces%ns)  )
799       ALLOCATE ( surfaces%k(1:surfaces%ns)  )
800
801       IF ( .NOT. constant_diffusion )  THEN   
802          ALLOCATE ( surfaces%u_0(1:surfaces%ns) ) 
803          ALLOCATE ( surfaces%v_0(1:surfaces%ns) )
804       ENDIF 
805!
806!--    Vertical momentum fluxes of u and v
807       ALLOCATE ( surfaces%usws(1:surfaces%ns) ) 
808       ALLOCATE ( surfaces%vsws(1:surfaces%ns) ) 
809!
810!--    Sensible heat flux
811       ALLOCATE ( surfaces%shf(1:surfaces%ns) )   
812!
813!--    Latent heat flux
814       IF ( humidity )  THEN
815          ALLOCATE ( surfaces%qsws(1:surfaces%ns) )     
816       ENDIF 
817!
818!--    Scalar flux
819       IF ( passive_scalar )  THEN
820          ALLOCATE ( surfaces%ssws(1:surfaces%ns) ) 
821       ENDIF 
822!
823!--       
824       IF ( cloud_physics .AND. microphysics_morrison)  THEN
825          ALLOCATE ( surfaces%qcsws(1:surfaces%ns) )
826          ALLOCATE ( surfaces%ncsws(1:surfaces%ns) )
827       ENDIF
828!
829!--       
830       IF ( cloud_physics .AND. microphysics_seifert)  THEN
831          ALLOCATE ( surfaces%qrsws(1:surfaces%ns) )
832          ALLOCATE ( surfaces%nrsws(1:surfaces%ns) )
833       ENDIF
834!
835!--    Salinity flux
836       IF ( ocean )  ALLOCATE ( surfaces%sasws(1:surfaces%ns) )
837
838    END SUBROUTINE allocate_surface_attributes_h_top
839
840!------------------------------------------------------------------------------!
841! Description:
842! ------------
843!> Allocating memory for vertical surface types.
844!------------------------------------------------------------------------------!
845    SUBROUTINE allocate_surface_attributes_v( surfaces, lsm,                   &
846                                              nys_l, nyn_l, nxl_l, nxr_l )
847
848       IMPLICIT NONE
849
850       INTEGER(iwp) ::  nyn_l  !< north bound of local 2d array start/end_index, is equal to nyn, except for restart-array
851       INTEGER(iwp) ::  nys_l  !< south bound of local 2d array start/end_index, is equal to nyn, except for restart-array
852       INTEGER(iwp) ::  nxl_l  !< west bound of local 2d array start/end_index, is equal to nyn, except for restart-array
853       INTEGER(iwp) ::  nxr_l  !< east bound of local 2d array start/end_index, is equal to nyn, except for restart-array
854
855       LOGICAL         ::  lsm      !< flag indicating data type of natural land surface
856
857       TYPE(surf_type) ::  surfaces !< respective surface type
858
859!
860!--    Allocate arrays for start and end index of vertical surface type
861!--    for each (j,i)-grid point. This is required in diffion_x, which is
862!--    called for each (j,i). In order to find the location where the
863!--    respective flux is store within the surface-type, start- and end-
864!--    index are stored for each (j,i). For example, each (j,i) can have
865!--    several entries where fluxes for vertical surfaces might be stored. 
866!--    In the flat case, where no vertical walls exit, set indicies such
867!--    that loop in diffusion routines will not be entered.
868       ALLOCATE ( surfaces%start_index(nys_l:nyn_l,nxl_l:nxr_l) )
869       ALLOCATE ( surfaces%end_index(nys_l:nyn_l,nxl_l:nxr_l)   )
870       surfaces%start_index = 0
871       surfaces%end_index   = -1
872!
873!--    Indices to locate surface element.
874       ALLOCATE ( surfaces%i(1:surfaces%ns) )
875       ALLOCATE ( surfaces%j(1:surfaces%ns) )
876       ALLOCATE ( surfaces%k(1:surfaces%ns) )
877!
878!--    Surface-layer height
879       ALLOCATE ( surfaces%z_mo(1:surfaces%ns) )
880!
881!--    Surface orientation
882       ALLOCATE ( surfaces%facing(1:surfaces%ns) )
883!
884!--    Surface parallel wind velocity
885       ALLOCATE ( surfaces%uvw_abs(1:surfaces%ns) )
886!
887!--    Roughness
888       ALLOCATE ( surfaces%z0(1:surfaces%ns)  )
889       ALLOCATE ( surfaces%z0h(1:surfaces%ns) )
890       ALLOCATE ( surfaces%z0q(1:surfaces%ns) )
891
892!
893!--    Friction velocity
894       ALLOCATE ( surfaces%us(1:surfaces%ns) )
895!
896!--    Allocate Obukhov length and bulk Richardson number. Only required
897!--    for natural land surfaces
898       IF ( lsm )  THEN
899          ALLOCATE( surfaces%ol(1:surfaces%ns)  ) 
900          ALLOCATE( surfaces%rib(1:surfaces%ns) ) 
901       ENDIF
902!
903!--    Allocate arrays for surface momentum fluxes for u and v. For u at north-
904!--    and south-facing surfaces, for v at east- and west-facing surfaces.
905       ALLOCATE ( surfaces%mom_flux_uv(1:surfaces%ns) )
906!
907!--    Allocate array for surface momentum flux for w - wsus and wsvs
908       ALLOCATE ( surfaces%mom_flux_w(1:surfaces%ns) ) 
909!
910!--    Allocate array for surface momentum flux for subgrid-scale tke wsus and
911!--    wsvs; first index usvs or vsws, second index for wsus or wsvs, depending
912!--    on surface.
913       ALLOCATE ( surfaces%mom_flux_tke(0:1,1:surfaces%ns) ) 
914!
915!--    Characteristic temperature and surface flux of sensible heat
916       ALLOCATE ( surfaces%ts(1:surfaces%ns)  )   
917       ALLOCATE ( surfaces%shf(1:surfaces%ns) )   
918!
919!--    Characteristic humidity and surface flux of latent heat
920       IF ( humidity )  THEN
921          ALLOCATE ( surfaces%qs(1:surfaces%ns)   ) 
922          ALLOCATE ( surfaces%qsws(1:surfaces%ns) )     
923       ENDIF 
924!
925!--    Characteristic scalar and surface flux of scalar
926       IF ( passive_scalar )  THEN
927          ALLOCATE ( surfaces%ss(1:surfaces%ns)   )   
928          ALLOCATE ( surfaces%ssws(1:surfaces%ns) ) 
929       ENDIF
930
931       IF ( cloud_physics .AND. microphysics_seifert)  THEN
932          ALLOCATE ( surfaces%qcs(1:surfaces%ns)   )
933          ALLOCATE ( surfaces%ncs(1:surfaces%ns)   )
934          ALLOCATE ( surfaces%qcsws(1:surfaces%ns) )
935          ALLOCATE ( surfaces%ncsws(1:surfaces%ns) )
936       ENDIF
937
938       IF ( cloud_physics .AND. microphysics_seifert)  THEN
939          ALLOCATE ( surfaces%qrs(1:surfaces%ns)   )
940          ALLOCATE ( surfaces%nrs(1:surfaces%ns)   )
941          ALLOCATE ( surfaces%qrsws(1:surfaces%ns) )
942          ALLOCATE ( surfaces%nrsws(1:surfaces%ns) )
943       ENDIF
944!
945!--    Salinity surface flux
946       IF ( ocean )  ALLOCATE ( surfaces%sasws(1:surfaces%ns) )
947
948    END SUBROUTINE allocate_surface_attributes_v
949
950!------------------------------------------------------------------------------!
951! Description:
952! ------------
953!> Initialize surface elements.
954!------------------------------------------------------------------------------!
955    SUBROUTINE init_surfaces
956
957       IMPLICIT NONE
958
959       INTEGER(iwp) ::  i         !< running index x-direction
960       INTEGER(iwp) ::  j         !< running index y-direction
961       INTEGER(iwp) ::  k         !< running index z-direction
962       INTEGER(iwp) ::  l         !< index variable used to distinguish surface facing
963       INTEGER(iwp) ::  m         !< running index surface elements
964
965       INTEGER(iwp)                 ::  start_index_lsm_h !< dummy to determing local start index in surface type for given (j,i), for horizontal natural surfaces
966       INTEGER(iwp)                 ::  start_index_usm_h !< dummy to determing local start index in surface type for given (j,i), for horizontal urban surfaces
967
968       INTEGER(iwp)                 ::  num_lsm_h     !< current number of horizontal surface element, natural type
969       INTEGER(iwp)                 ::  num_lsm_h_kji !< dummy to determing local end index in surface type for given (j,i), for for horizonal natural surfaces
970       INTEGER(iwp)                 ::  num_usm_h     !< current number of horizontal surface element, urban type
971       INTEGER(iwp)                 ::  num_usm_h_kji !< dummy to determing local end index in surface type for given (j,i), for for horizonal urban surfaces
972
973       INTEGER(iwp), DIMENSION(0:2) ::  num_def_h     !< current number of horizontal surface element, default type
974       INTEGER(iwp), DIMENSION(0:2) ::  num_def_h_kji !< dummy to determing local end index in surface type for given (j,i), for horizonal default surfaces
975       INTEGER(iwp), DIMENSION(0:2) ::  start_index_def_h !< dummy to determing local start index in surface type for given (j,i), for horizontal default surfaces
976     
977       INTEGER(iwp), DIMENSION(0:3) ::  num_def_v     !< current number of vertical surface element, default type
978       INTEGER(iwp), DIMENSION(0:3) ::  num_def_v_kji !< dummy to determing local end index in surface type for given (j,i), for vertical default surfaces
979       INTEGER(iwp), DIMENSION(0:3) ::  num_lsm_v     !< current number of vertical surface element, natural type
980       INTEGER(iwp), DIMENSION(0:3) ::  num_lsm_v_kji !< dummy to determing local end index in surface type for given (j,i), for vertical natural surfaces
981       INTEGER(iwp), DIMENSION(0:3) ::  num_usm_v     !< current number of vertical surface element, urban type
982       INTEGER(iwp), DIMENSION(0:3) ::  num_usm_v_kji !< dummy to determing local end index in surface type for given (j,i), for vertical urban surfaces
983
984       INTEGER(iwp), DIMENSION(0:3) ::  start_index_def_v !< dummy to determing local start index in surface type for given (j,i), for vertical default surfaces
985       INTEGER(iwp), DIMENSION(0:3) ::  start_index_lsm_v !< dummy to determing local start index in surface type for given (j,i), for vertical natural surfaces
986       INTEGER(iwp), DIMENSION(0:3) ::  start_index_usm_v !< dummy to determing local start index in surface type for given (j,i), for vertical urban surfaces
987
988
989!
990!--    Initialize surface attributes, store indicies, surfaces orientation, etc.,
991       num_def_h(0:2) = 1
992       num_def_v(0:3) = 1
993
994       num_lsm_h      = 1
995       num_lsm_v(0:3) = 1
996
997       num_usm_h      = 1
998       num_usm_v(0:3) = 1
999
1000       start_index_def_h(0:2) = 1
1001       start_index_def_v(0:3) = 1
1002
1003       start_index_lsm_h      = 1
1004       start_index_lsm_v(0:3) = 1
1005
1006       start_index_usm_h      = 1
1007       start_index_usm_v(0:3) = 1
1008
1009       DO  i = nxl, nxr
1010          DO  j = nys, nyn
1011
1012             num_def_h_kji = 0
1013             num_def_v_kji = 0
1014             num_lsm_h_kji = 0
1015             num_lsm_v_kji = 0
1016             num_usm_h_kji = 0
1017             num_usm_v_kji = 0
1018
1019             DO  k = nzb+1, nzt
1020!
1021!--             Check if current gridpoint belongs to the atmosphere
1022                IF ( BTEST( wall_flags_0(k,j,i), 0 ) )  THEN
1023!
1024!--                Upward-facing surface. Distinguish between differet surface types.
1025!--                To do, think about method to flag natural and non-natural
1026!--                surfaces. Only to ask for land_surface or urban surface
1027!--                is just a work-around.
1028                   IF ( .NOT. BTEST( wall_flags_0(k-1,j,i), 0 ) )  THEN 
1029!
1030!--                   Natural surface type         
1031                      IF ( land_surface )  THEN
1032                         CALL initialize_horizontal_surfaces( k, j, i,         &
1033                                                              surf_lsm_h,      &
1034                                                              num_lsm_h,       &
1035                                                              num_lsm_h_kji,   &
1036                                                              .TRUE., .FALSE. ) 
1037!
1038!--                   Urban surface tpye
1039                      ELSEIF ( urban_surface )  THEN
1040                         CALL initialize_horizontal_surfaces( k, j, i,         &
1041                                                              surf_usm_h,      &
1042                                                              num_usm_h,       &
1043                                                              num_usm_h_kji,   &
1044                                                              .TRUE., .FALSE. ) 
1045!
1046!--                   Default surface type
1047                      ELSE
1048                         CALL initialize_horizontal_surfaces( k, j, i,         &
1049                                                              surf_def_h(0),   &
1050                                                              num_def_h(0),    &
1051                                                              num_def_h_kji(0),&
1052                                                              .TRUE., .FALSE. ) 
1053                      ENDIF
1054                   ENDIF 
1055!
1056!--                downward-facing surface, first, model top
1057                   IF ( k == nzt  .AND.  use_top_fluxes )  THEN
1058                      CALL initialize_top( k, j, i, surf_def_h(2),             &
1059                                           num_def_h(2), num_def_h_kji(2) )
1060!
1061!--                Check for any other downward-facing surface. So far only for
1062!--                default surface type.
1063                   ELSEIF ( .NOT. BTEST( wall_flags_0(k+1,j,i), 0 ) )  THEN
1064                      CALL initialize_horizontal_surfaces( k, j, i,            &
1065                                                           surf_def_h(1),      &
1066                                                           num_def_h(1),       &
1067                                                           num_def_h_kji(1),   &
1068                                                           .FALSE., .TRUE. )   
1069                   ENDIF 
1070!
1071!--                Check for vertical walls and, if required, initialize it.
1072!                  Start with northward-facing surface.
1073                   IF ( .NOT. BTEST( wall_flags_0(k,j-1,i), 0 ) )  THEN
1074                      IF ( urban_surface )  THEN
1075                         CALL initialize_vertical_surfaces( 0, k, j, i,        &
1076                                                            surf_usm_v(0),     &
1077                                                            num_usm_v(0),      &
1078                                                            num_usm_v_kji(0),  &
1079                                                            .FALSE., .FALSE.,  &             
1080                                                            .FALSE., .TRUE. ) 
1081                      ELSEIF ( land_surface )  THEN
1082                         CALL initialize_vertical_surfaces( 0, k, j, i,        &
1083                                                            surf_lsm_v(0),     &
1084                                                            num_lsm_v(0),      &
1085                                                            num_lsm_v_kji(0),  &
1086                                                            .FALSE., .FALSE.,  &             
1087                                                            .FALSE., .TRUE. ) 
1088                      ELSE
1089                         CALL initialize_vertical_surfaces( 0, k, j, i,        &
1090                                                            surf_def_v(0),     &
1091                                                            num_def_v(0),      &
1092                                                            num_def_v_kji(0),  &
1093                                                            .FALSE., .FALSE.,  &             
1094                                                            .FALSE., .TRUE. ) 
1095                      ENDIF
1096                   ENDIF
1097!
1098!--                southward-facing surface
1099                   IF ( .NOT. BTEST( wall_flags_0(k,j+1,i), 0 ) )  THEN
1100                      IF ( urban_surface )  THEN
1101                         CALL initialize_vertical_surfaces( 1, k, j, i,        &
1102                                                            surf_usm_v(1),     &
1103                                                            num_usm_v(1),      &
1104                                                            num_usm_v_kji(1),  &
1105                                                            .FALSE., .FALSE.,  &
1106                                                            .TRUE., .FALSE. )
1107                      ELSEIF ( land_surface )  THEN
1108                         CALL initialize_vertical_surfaces( 1, k, j, i,        &
1109                                                            surf_lsm_v(1),     &
1110                                                            num_lsm_v(1),      &
1111                                                            num_lsm_v_kji(1),  &
1112                                                            .FALSE., .FALSE.,  &
1113                                                            .TRUE., .FALSE. ) 
1114                      ELSE
1115                         CALL initialize_vertical_surfaces( 1, k, j, i,        &
1116                                                            surf_def_v(1),     &
1117                                                            num_def_v(1),      &
1118                                                            num_def_v_kji(1),  &
1119                                                            .FALSE., .FALSE.,  &
1120                                                            .TRUE., .FALSE. ) 
1121                      ENDIF
1122                   ENDIF
1123!
1124!--                eastward-facing surface
1125                   IF ( .NOT. BTEST( wall_flags_0(k,j,i-1), 0 ) )  THEN
1126                      IF ( urban_surface )  THEN
1127                         CALL initialize_vertical_surfaces( 2, k, j, i,        &
1128                                                            surf_usm_v(2),     &
1129                                                            num_usm_v(2),      &
1130                                                            num_usm_v_kji(2),  &
1131                                                            .TRUE., .FALSE.,   &
1132                                                            .FALSE., .FALSE. ) 
1133                      ELSEIF ( land_surface )  THEN
1134                         CALL initialize_vertical_surfaces( 2, k, j, i,        &
1135                                                            surf_lsm_v(2),     &
1136                                                            num_lsm_v(2),      &
1137                                                            num_lsm_v_kji(2),  &
1138                                                            .TRUE., .FALSE.,   &
1139                                                            .FALSE., .FALSE. ) 
1140                      ELSE
1141                         CALL initialize_vertical_surfaces( 2, k, j, i,        &
1142                                                            surf_def_v(2),     &
1143                                                            num_def_v(2),      &
1144                                                            num_def_v_kji(2),  &
1145                                                            .TRUE., .FALSE.,   &
1146                                                            .FALSE., .FALSE. ) 
1147                      ENDIF
1148                   ENDIF 
1149!   
1150!--                westward-facing surface
1151                   IF ( .NOT. BTEST( wall_flags_0(k,j,i+1), 0 ) )  THEN
1152                      IF ( urban_surface )  THEN
1153                         CALL initialize_vertical_surfaces( 3, k, j, i,        &
1154                                                            surf_usm_v(3),     &
1155                                                            num_usm_v(3),      &
1156                                                            num_usm_v_kji(3),  &
1157                                                           .FALSE., .TRUE.,    &
1158                                                           .FALSE., .FALSE. ) 
1159                      ELSEIF ( land_surface )  THEN
1160                         CALL initialize_vertical_surfaces( 3, k, j, i,        &
1161                                                            surf_lsm_v(3),     &
1162                                                            num_lsm_v(3),      &
1163                                                            num_lsm_v_kji(3),  &
1164                                                           .FALSE., .TRUE.,    &
1165                                                           .FALSE., .FALSE. ) 
1166                      ELSE
1167                         CALL initialize_vertical_surfaces( 3, k, j, i,        &
1168                                                            surf_def_v(3),     &
1169                                                            num_def_v(3),      &
1170                                                            num_def_v_kji(3),  &
1171                                                           .FALSE., .TRUE.,    &
1172                                                           .FALSE., .FALSE. ) 
1173                      ENDIF
1174                   ENDIF
1175                ENDIF
1176
1177 
1178             ENDDO
1179!
1180!--          Determine start- and end-index at grid point (j,i). Also, for
1181!--          horizontal surfaces more than 1 horizontal surface element can
1182!--          exist at grid point (j,i) if overhanging structures are present.
1183!--          Upward-facing surfaces
1184             surf_def_h(0)%start_index(j,i) = start_index_def_h(0)
1185             surf_def_h(0)%end_index(j,i)   = surf_def_h(0)%start_index(j,i) + &
1186                                                 num_def_h_kji(0) - 1
1187             start_index_def_h(0)           = surf_def_h(0)%end_index(j,i) + 1
1188!
1189!--          Downward-facing surfaces, except model top
1190             surf_def_h(1)%start_index(j,i) = start_index_def_h(1)                                                 
1191             surf_def_h(1)%end_index(j,i)   = surf_def_h(1)%start_index(j,i) + &
1192                                                 num_def_h_kji(1) - 1
1193             start_index_def_h(1)           = surf_def_h(1)%end_index(j,i) + 1
1194!
1195!--          Downward-facing surfaces -- model top fluxes
1196             surf_def_h(2)%start_index(j,i) = start_index_def_h(2)                                                 
1197             surf_def_h(2)%end_index(j,i)   = surf_def_h(2)%start_index(j,i) + &
1198                                                 num_def_h_kji(2) - 1
1199             start_index_def_h(2)           = surf_def_h(2)%end_index(j,i) + 1
1200!
1201!--          Horizontal natural land surfaces
1202             surf_lsm_h%start_index(j,i)    = start_index_lsm_h
1203             surf_lsm_h%end_index(j,i)      = surf_lsm_h%start_index(j,i) +    &
1204                                                 num_lsm_h_kji - 1
1205             start_index_lsm_h              = surf_lsm_h%end_index(j,i) + 1
1206!
1207!--          Horizontal urban surfaces
1208             surf_usm_h%start_index(j,i)    = start_index_usm_h
1209             surf_usm_h%end_index(j,i)      = surf_usm_h%start_index(j,i) +    &
1210                                                 num_usm_h_kji - 1
1211             start_index_usm_h              = surf_usm_h%end_index(j,i) + 1
1212
1213!
1214!--          Vertical surfaces - Default type
1215             surf_def_v(0)%start_index(j,i) = start_index_def_v(0)
1216             surf_def_v(1)%start_index(j,i) = start_index_def_v(1)
1217             surf_def_v(2)%start_index(j,i) = start_index_def_v(2)
1218             surf_def_v(3)%start_index(j,i) = start_index_def_v(3)
1219             surf_def_v(0)%end_index(j,i)   = start_index_def_v(0) +           & 
1220                                              num_def_v_kji(0) - 1
1221             surf_def_v(1)%end_index(j,i)   = start_index_def_v(1) +           &
1222                                              num_def_v_kji(1) - 1
1223             surf_def_v(2)%end_index(j,i)   = start_index_def_v(2) +           &
1224                                              num_def_v_kji(2) - 1
1225             surf_def_v(3)%end_index(j,i)   = start_index_def_v(3) +           &
1226                                              num_def_v_kji(3) - 1
1227             start_index_def_v(0)           = surf_def_v(0)%end_index(j,i) + 1
1228             start_index_def_v(1)           = surf_def_v(1)%end_index(j,i) + 1
1229             start_index_def_v(2)           = surf_def_v(2)%end_index(j,i) + 1
1230             start_index_def_v(3)           = surf_def_v(3)%end_index(j,i) + 1
1231!
1232!--          Natural type
1233             surf_lsm_v(0)%start_index(j,i) = start_index_lsm_v(0)
1234             surf_lsm_v(1)%start_index(j,i) = start_index_lsm_v(1)
1235             surf_lsm_v(2)%start_index(j,i) = start_index_lsm_v(2)
1236             surf_lsm_v(3)%start_index(j,i) = start_index_lsm_v(3)
1237             surf_lsm_v(0)%end_index(j,i)   = start_index_lsm_v(0) +           & 
1238                                              num_lsm_v_kji(0) - 1
1239             surf_lsm_v(1)%end_index(j,i)   = start_index_lsm_v(1) +           &
1240                                              num_lsm_v_kji(1) - 1
1241             surf_lsm_v(2)%end_index(j,i)   = start_index_lsm_v(2) +           &
1242                                              num_lsm_v_kji(2) - 1
1243             surf_lsm_v(3)%end_index(j,i)   = start_index_lsm_v(3) +           &
1244                                              num_lsm_v_kji(3) - 1
1245             start_index_lsm_v(0)           = surf_lsm_v(0)%end_index(j,i) + 1
1246             start_index_lsm_v(1)           = surf_lsm_v(1)%end_index(j,i) + 1
1247             start_index_lsm_v(2)           = surf_lsm_v(2)%end_index(j,i) + 1
1248             start_index_lsm_v(3)           = surf_lsm_v(3)%end_index(j,i) + 1
1249!
1250!--          Urban type
1251             surf_usm_v(0)%start_index(j,i) = start_index_usm_v(0)
1252             surf_usm_v(1)%start_index(j,i) = start_index_usm_v(1)
1253             surf_usm_v(2)%start_index(j,i) = start_index_usm_v(2)
1254             surf_usm_v(3)%start_index(j,i) = start_index_usm_v(3)
1255             surf_usm_v(0)%end_index(j,i)   = start_index_usm_v(0) +           & 
1256                                              num_usm_v_kji(0) - 1
1257             surf_usm_v(1)%end_index(j,i)   = start_index_usm_v(1) +           &
1258                                              num_usm_v_kji(1) - 1
1259             surf_usm_v(2)%end_index(j,i)   = start_index_usm_v(2) +           &
1260                                              num_usm_v_kji(2) - 1
1261             surf_usm_v(3)%end_index(j,i)   = start_index_usm_v(3) +           &
1262                                              num_usm_v_kji(3) - 1
1263             start_index_usm_v(0)           = surf_usm_v(0)%end_index(j,i) + 1
1264             start_index_usm_v(1)           = surf_usm_v(1)%end_index(j,i) + 1
1265             start_index_usm_v(2)           = surf_usm_v(2)%end_index(j,i) + 1
1266             start_index_usm_v(3)           = surf_usm_v(3)%end_index(j,i) + 1
1267
1268
1269          ENDDO
1270       ENDDO
1271
1272       CONTAINS
1273
1274!------------------------------------------------------------------------------!
1275! Description:
1276! ------------
1277!> Initialize horizontal surface elements, upward- and downward-facing.
1278!> Note, horizontal surface type alsw comprises model-top fluxes, which are,
1279!> initialized in a different routine.
1280!------------------------------------------------------------------------------!
1281          SUBROUTINE initialize_horizontal_surfaces( k, j, i, surf, num_h,     &
1282                                                     num_h_kji, upward_facing, &
1283                                                     downward_facing )       
1284
1285             IMPLICIT NONE
1286
1287             INTEGER(iwp)  ::  i                !< running index x-direction
1288             INTEGER(iwp)  ::  j                !< running index y-direction
1289             INTEGER(iwp)  ::  k                !< running index z-direction
1290             INTEGER(iwp)  ::  num_h            !< current number of surface element
1291             INTEGER(iwp)  ::  num_h_kji        !< dummy increment
1292
1293             LOGICAL       ::  upward_facing    !< flag indicating upward-facing surface
1294             LOGICAL       ::  downward_facing  !< flag indicating downward-facing surface
1295
1296             TYPE( surf_type ) :: surf          !< respective surface type
1297!
1298!--          Store indices of respective surface element
1299             surf%i(num_h) = i
1300             surf%j(num_h) = j
1301             surf%k(num_h) = k
1302!
1303!--          Surface orientation, bit 0 is set to 1 for upward-facing surfaces,
1304!--          bit 1 is for downward-facing surfaces.
1305             IF ( upward_facing   )  surf%facing(num_h) = IBSET( surf%facing(num_h), 0 )
1306             IF ( downward_facing )  surf%facing(num_h) = IBSET( surf%facing(num_h), 1 )
1307!
1308!--          Initialize surface-layer height
1309             IF ( upward_facing )  THEN
1310                surf%z_mo(num_h)  = zu(k) - zw(k-1)
1311             ELSE
1312                surf%z_mo(num_h)  = zw(k) - zu(k)
1313             ENDIF
1314 
1315             surf%z0(num_h)    = roughness_length
1316             surf%z0h(num_h)   = z0h_factor * roughness_length
1317             surf%z0q(num_h)   = z0h_factor * roughness_length         
1318!
1319!--          Initialization in case of 1D pre-cursor run
1320             IF ( INDEX( initializing_actions, 'set_1d-model_profiles' ) /= 0 )&
1321             THEN
1322                IF ( .NOT. constant_diffusion )  THEN
1323                   IF ( constant_flux_layer )  THEN
1324                      surf%ol(num_h)   = surf%z_mo(num_h) /                    &
1325                                            ( rif1d(nzb+1) + 1.0E-20_wp )
1326                      surf%us(num_h)   = us1d
1327                      surf%usws(num_h) = usws1d
1328                      surf%vsws(num_h) = vsws1d
1329                   ELSE
1330                      surf%ol(num_h)   = surf%z_mo(num_h) / zeta_min
1331                      surf%us(num_h)   = 0.0_wp
1332                      surf%usws(num_h) = 0.0_wp
1333                      surf%vsws(num_h) = 0.0_wp
1334                   ENDIF
1335                ELSE
1336                   surf%ol(num_h)   = surf%z_mo(num_h) / zeta_min
1337                   surf%us(num_h)   = 0.0_wp
1338                   surf%usws(num_h) = 0.0_wp
1339                   surf%vsws(num_h) = 0.0_wp
1340                ENDIF
1341!
1342!--          Initialization in case of constant profiles
1343             ELSEIF ( INDEX(initializing_actions, 'set_constant_profiles') /= 0 )&
1344             THEN
1345
1346                surf%ol(num_h)   = surf%z_mo(num_h) / zeta_min
1347!
1348!--             Very small number is required for calculation of Obukhov length
1349!--             at first timestep     
1350                surf%us(num_h)    = 1E-30_wp 
1351                surf%usws(num_h)  = 0.0_wp
1352                surf%vsws(num_h)  = 0.0_wp
1353       
1354             ENDIF
1355
1356             surf%rib(num_h)   = 0.0_wp 
1357             surf%uvw_abs(num_h) = 0.0_wp
1358
1359             IF ( .NOT. constant_diffusion )  THEN   
1360                surf%u_0(num_h)     = 0.0_wp 
1361                surf%v_0(num_h)     = 0.0_wp
1362             ENDIF
1363
1364             surf%ts(num_h)   = 0.0_wp
1365
1366             IF ( humidity )  THEN
1367                surf%qs(num_h)   = 0.0_wp
1368                IF ( cloud_physics .AND. microphysics_morrison)  THEN
1369                   surf%qcs(num_h) = 0.0_wp
1370                   surf%ncs(num_h) = 0.0_wp
1371   
1372                   surf%qcsws(num_h) = 0.0_wp
1373                   surf%ncsws(num_h) = 0.0_wp
1374
1375                ENDIF
1376                IF ( cloud_physics .AND. microphysics_seifert)  THEN
1377                   surf%qrs(num_h) = 0.0_wp
1378                   surf%nrs(num_h) = 0.0_wp
1379   
1380                   surf%qrsws(num_h) = 0.0_wp
1381                   surf%nrsws(num_h) = 0.0_wp
1382
1383                   surf%pt1(num_h) = 0.0_wp
1384                   surf%qv1(num_h) = 0.0_wp
1385
1386                ENDIF
1387             ENDIF
1388
1389             IF ( passive_scalar )  surf%ss(num_h) = 0.0_wp
1390!
1391!--          Inititalize surface fluxes of sensible and latent heat, as well as
1392!--          passive scalar
1393             IF ( use_surface_fluxes )  THEN
1394
1395                IF ( upward_facing )  THEN
1396                   IF ( constant_heatflux )  THEN
1397!   
1398!--                   Initialize surface heatflux. However, skip this for now if
1399!--                   if random_heatflux is set. This case, shf is initialized later.
1400                      IF ( .NOT. random_heatflux )  THEN
1401                         surf%shf(num_h) = surface_heatflux *               &
1402                                                 heatflux_input_conversion(nzb)
1403!
1404!--                      Check if surface heat flux might be replaced by
1405!--                      prescribed wall heatflux
1406                         IF ( k-1 /= 0 )  THEN
1407                            surf%shf(num_h) = wall_heatflux(0) *            &
1408                                                 heatflux_input_conversion(k-1)
1409                         ENDIF
1410!
1411!--                      Initialize shf with data from external file LSF_DATA. Will
1412!--                      be done directly in ls_foring_surf
1413!--                      Attention: Just a workaround, need to be revised!!!
1414                         IF ( large_scale_forcing .AND. lsf_surf )  THEN
1415!                             CALL ls_forcing_surf ( simulated_time )
1416!                             surf%shf(num_h) = shf(j,i) 
1417                         ENDIF
1418                      ENDIF
1419                   ELSE
1420                      surf%shf(num_h) = 0.0_wp
1421                   ENDIF
1422!
1423!--             Set heat-flux at downward-facing surfaces
1424                ELSE
1425                   surf%shf(num_h) = wall_heatflux(5) *                        &
1426                                             heatflux_input_conversion(k)
1427                ENDIF
1428
1429                IF ( humidity )  THEN
1430                   IF ( upward_facing )  THEN
1431                      IF ( constant_waterflux )  THEN
1432                         surf%qsws(num_h) = surface_waterflux *                &
1433                                                 waterflux_input_conversion(nzb)
1434                         IF ( k-1 /= 0 )  THEN
1435                            surf%qsws(num_h) = wall_humidityflux(0) *          &
1436                                                 waterflux_input_conversion(k-1)
1437                         ENDIF
1438                      ELSE
1439                         surf%qsws(num_h) = 0.0_wp
1440                      ENDIF
1441                   ELSE
1442                      surf%qsws(num_h) = wall_humidityflux(5) *                &
1443                                             heatflux_input_conversion(k)
1444                   ENDIF
1445                ENDIF
1446
1447                IF ( passive_scalar )  THEN
1448                   IF ( upward_facing )  THEN
1449                      IF ( constant_scalarflux )  THEN
1450                         surf%ssws(num_h) = surface_scalarflux
1451
1452                         IF ( k-1 /= 0 )                                       &
1453                            surf%ssws(num_h) = wall_scalarflux(0)
1454
1455                      ELSE
1456                         surf%ssws(num_h) = 0.0_wp
1457                      ENDIF
1458                   ELSE
1459                      surf%ssws(num_h) = wall_scalarflux(5)
1460                   ENDIF
1461                ENDIF
1462
1463                IF ( ocean )  THEN
1464                   IF ( upward_facing )  THEN
1465                      surf%sasws(num_h) = bottom_salinityflux
1466                   ELSE
1467                      surf%sasws(num_h) = 0.0_wp
1468                   ENDIF
1469                ENDIF
1470             ENDIF
1471!
1472!--          Increment surface indices
1473             num_h     = num_h + 1
1474             num_h_kji = num_h_kji + 1     
1475
1476
1477          END SUBROUTINE initialize_horizontal_surfaces
1478       
1479
1480!------------------------------------------------------------------------------!
1481! Description:
1482! ------------
1483!> Initialize model-top fluxes. Currently, only the heatflux and salinity flux
1484!> can be prescribed, latent flux is zero in this case!
1485!------------------------------------------------------------------------------!
1486          SUBROUTINE initialize_top( k, j, i, surf, num_h, num_h_kji )       
1487
1488             IMPLICIT NONE
1489
1490             INTEGER(iwp)  ::  i                !< running index x-direction
1491             INTEGER(iwp)  ::  j                !< running index y-direction
1492             INTEGER(iwp)  ::  k                !< running index z-direction
1493             INTEGER(iwp)  ::  num_h            !< current number of surface element
1494             INTEGER(iwp)  ::  num_h_kji        !< dummy increment
1495
1496             TYPE( surf_type ) :: surf          !< respective surface type
1497!
1498!--          Store indices of respective surface element
1499             surf%i(num_h) = i
1500             surf%j(num_h) = j
1501             surf%k(num_h) = k
1502!
1503!--          Initialize top heat flux
1504             IF ( constant_top_heatflux )                                      &
1505                surf%shf(num_h) = top_heatflux * heatflux_input_conversion(nzt+1)
1506!
1507!--          Initialization in case of a coupled model run
1508             IF ( coupling_mode == 'ocean_to_atmosphere' )  THEN
1509                surf%shf(num_h) = 0.0_wp
1510             ENDIF
1511!
1512!--          Prescribe latent heat flux at the top     
1513             IF ( humidity )  THEN
1514                surf%qsws(num_h) = 0.0_wp
1515                IF ( cloud_physics  .AND.  microphysics_morrison ) THEN
1516                   surf%ncsws(num_h) = 0.0_wp
1517                   surf%qcsws(num_h) = 0.0_wp
1518                ENDIF
1519                IF ( cloud_physics  .AND.  microphysics_seifert ) THEN
1520                   surf%nrsws(num_h) = 0.0_wp
1521                   surf%qrsws(num_h) = 0.0_wp
1522                ENDIF
1523             ENDIF
1524!
1525!--          Prescribe top scalar flux
1526             IF ( passive_scalar .AND. constant_top_scalarflux )               &
1527                surf%ssws(num_h) = top_scalarflux
1528!
1529!--          Prescribe top salinity flux
1530             IF ( ocean .AND. constant_top_salinityflux)                       &
1531                surf%sasws(num_h) = top_salinityflux
1532!
1533!--          Initialization in case of a coupled model run
1534             IF ( coupling_mode == 'ocean_to_atmosphere' )  THEN
1535                surf%shf(num_h) = 0.0_wp
1536             ENDIF
1537!
1538!--          Top momentum fluxes
1539             surf%usws(num_h) = top_momentumflux_u *                           &
1540                                            momentumflux_input_conversion(nzt+1)
1541             surf%vsws(num_h) = top_momentumflux_v *                           &
1542                                            momentumflux_input_conversion(nzt+1)
1543!
1544!--          Increment surface indices
1545             num_h     = num_h + 1
1546             num_h_kji = num_h_kji + 1     
1547
1548
1549          END SUBROUTINE initialize_top
1550
1551
1552!------------------------------------------------------------------------------!
1553! Description:
1554! ------------
1555!> Initialize vertical surface elements.
1556!------------------------------------------------------------------------------!
1557          SUBROUTINE initialize_vertical_surfaces( l, k, j, i, surf, num_v,    &
1558                                                num_v_kji, east_facing,        &
1559                                                west_facing, south_facing,     &
1560                                                north_facing )       
1561
1562             IMPLICIT NONE
1563
1564             INTEGER(iwp)  ::  component !<
1565             INTEGER(iwp)  ::  i               !< running index x-direction
1566             INTEGER(iwp)  ::  j               !< running index x-direction
1567             INTEGER(iwp)  ::  k               !< running index x-direction
1568             INTEGER(iwp)  ::  l               !< index variable for the surface type, indicating the facing
1569             INTEGER(iwp)  ::  num_v           !< current number of surface element
1570             INTEGER(iwp)  ::  num_v_kji       !< current number of surface element at (j,i)
1571
1572
1573             LOGICAL       ::  east_facing     !< flag indicating east-facing surfaces
1574             LOGICAL       ::  north_facing    !< flag indicating north-facing surfaces
1575             LOGICAL       ::  south_facing    !< flag indicating south-facing surfaces
1576             LOGICAL       ::  west_facing     !< flag indicating west-facing surfaces
1577
1578             TYPE( surf_type ) :: surf         !< respective surface type
1579
1580!
1581!--          Store indices of respective wall element
1582             surf%i(num_v)   = i
1583             surf%j(num_v)   = j
1584             surf%k(num_v)   = k
1585!
1586!--          Initialize surface-layer height, or more precisely, distance to surface
1587             IF ( north_facing  .OR.  south_facing )  THEN
1588                surf%z_mo(num_v)  = 0.5_wp * dy
1589             ELSE
1590                surf%z_mo(num_v)  = 0.5_wp * dx
1591             ENDIF
1592
1593             surf%facing(num_v)  = 0
1594!
1595!--          Surface orientation. Moreover, set component id to map wall_heatflux,
1596!--          etc., on surface type (further below)
1597             IF ( north_facing )  THEN
1598                surf%facing(num_v) = IBSET( surf%facing(num_v), 0 ) 
1599                component          = 4
1600             ENDIF
1601
1602             IF ( south_facing )  THEN
1603                surf%facing(num_v) = IBSET( surf%facing(num_v), 1 ) 
1604                component          = 3
1605             ENDIF
1606
1607             IF ( east_facing )  THEN
1608                surf%facing(num_v) = IBSET( surf%facing(num_v), 2 )
1609                component          = 2
1610             ENDIF
1611
1612             IF ( west_facing )  THEN
1613                surf%facing(num_v) = IBSET( surf%facing(num_v), 3 ) 
1614                component          = 1
1615             ENDIF
1616
1617 
1618             surf%z0(num_v)  = roughness_length
1619             surf%z0h(num_v) = z0h_factor * roughness_length
1620             surf%z0q(num_v) = z0h_factor * roughness_length
1621
1622             surf%us(num_v)  = 0.0_wp
1623!
1624!--          If required, initialize Obukhov length
1625             IF ( ALLOCATED( surf%ol ) )                                       &
1626                surf%ol(num_v) = surf%z_mo(num_v) / zeta_min
1627
1628             surf%uvw_abs(num_v)   = 0.0_wp
1629
1630             surf%mom_flux_uv(num_v) = 0.0_wp
1631             surf%mom_flux_w(num_v)  = 0.0_wp
1632             surf%mom_flux_tke(0:1,num_v) = 0.0_wp
1633
1634             surf%ts(num_v)    = 0.0_wp
1635             surf%shf(num_v)   = wall_heatflux(component)
1636
1637             IF ( humidity )  THEN
1638                surf%qs(num_v)   = 0.0_wp
1639                surf%qsws(num_v) = wall_humidityflux(component)
1640!
1641!--             Following wall fluxes are assumed to be zero
1642                IF ( cloud_physics .AND. microphysics_morrison)  THEN
1643                   surf%qcs(num_v) = 0.0_wp
1644                   surf%ncs(num_v) = 0.0_wp
1645   
1646                   surf%qcsws(num_v) = 0.0_wp
1647                   surf%ncsws(num_v) = 0.0_wp
1648                ENDIF
1649                IF ( cloud_physics .AND. microphysics_seifert)  THEN
1650                   surf%qrs(num_v) = 0.0_wp
1651                   surf%nrs(num_v) = 0.0_wp
1652   
1653                   surf%qrsws(num_v) = 0.0_wp
1654                   surf%nrsws(num_v) = 0.0_wp
1655                ENDIF
1656             ENDIF
1657
1658             IF ( passive_scalar )  THEN
1659                surf%ss(num_v)   = 0.0_wp
1660                surf%ssws(num_v) = wall_scalarflux(component)
1661             ENDIF
1662!
1663!--          So far, salinityflux at vertical surfaces is simply zero
1664!--          at the moment 
1665             IF ( ocean )  surf%sasws(num_v) = wall_salinityflux(component)
1666!
1667!--          Increment wall indices
1668             num_v                 = num_v + 1
1669             num_v_kji             = num_v_kji + 1
1670
1671          END SUBROUTINE initialize_vertical_surfaces
1672
1673    END SUBROUTINE init_surfaces
1674
1675
1676!------------------------------------------------------------------------------!
1677! Description:
1678! ------------
1679!> Determines topography-top index at given (j,i)-position. 
1680!------------------------------------------------------------------------------!
1681    FUNCTION get_topography_top_index( j, i, grid )
1682
1683       USE kinds
1684
1685       IMPLICIT NONE
1686
1687       CHARACTER(LEN=*) ::  grid                      !< flag to distinquish between staggered grids
1688       INTEGER(iwp)     ::  i                         !< grid index in x-dimension
1689       INTEGER(iwp)     ::  ibit                      !< bit position where topography information is stored on respective grid
1690       INTEGER(iwp)     ::  j                         !< grid index in y-dimension
1691       INTEGER(iwp)     ::  get_topography_top_index  !< topography top index
1692
1693       SELECT CASE ( TRIM( grid ) )
1694
1695          CASE ( 's'     )
1696             ibit = 12
1697          CASE ( 'u'     )
1698             ibit = 14
1699          CASE ( 'v'     )
1700             ibit = 16
1701          CASE ( 'w'     )
1702             ibit = 18
1703          CASE ( 's_out' )
1704             ibit = 24
1705          CASE ( 'u_out' )
1706             ibit = 26
1707          CASE ( 'v_out' )
1708             ibit = 27
1709          CASE ( 'w_out' )
1710             ibit = 28
1711          CASE DEFAULT
1712!
1713!--          Set default to scalar grid
1714             ibit = 12 
1715
1716       END SELECT
1717
1718       get_topography_top_index = MAXLOC(                                      &
1719                                     MERGE( 1, 0,                              &
1720                                            BTEST( wall_flags_0(:,j,i), ibit ) &
1721                                          ), DIM = 1                           &
1722                                        ) - 1
1723
1724       RETURN
1725
1726    END FUNCTION get_topography_top_index
1727
1728!------------------------------------------------------------------------------!
1729! Description:
1730! ------------
1731!> Gathers all surface elements with the same facing (but possibly different
1732!> type) onto a surface type, and writes binary data into restart files.
1733!------------------------------------------------------------------------------!
1734    SUBROUTINE surface_write_restart_data
1735
1736       IMPLICIT NONE
1737
1738       CHARACTER(LEN=1)             ::  dum  !< dummy string to create output-variable name
1739
1740       INTEGER(iwp)                 ::  i    !< running index x-direction
1741       INTEGER(iwp)                 ::  j    !< running index y-direction
1742       INTEGER(iwp)                 ::  l    !< index surface type orientation
1743       INTEGER(iwp)                 ::  m    !< running index for surface elements on individual surface array
1744       INTEGER(iwp), DIMENSION(0:3) ::  mm   !< running index for surface elements on gathered surface array
1745
1746       TYPE(surf_type), DIMENSION(0:2) ::  surf_h !< gathered horizontal surfaces, contains all surface types
1747       TYPE(surf_type), DIMENSION(0:3) ::  surf_v !< gathered vertical surfaces, contains all surface types
1748
1749!
1750!--    Determine total number of horizontal and vertical surface elements before
1751!--    writing var_list
1752       CALL surface_last_actions
1753!
1754!--    Count number of grid points with same facing and allocate attributes respectively
1755!--    Horizontal upward facing
1756       surf_h(0)%ns = ns_h_on_file(0)
1757       CALL allocate_surface_attributes_h( surf_h(0), nys, nyn, nxl, nxr )
1758!
1759!--    Horizontal downward facing
1760       surf_h(1)%ns = ns_h_on_file(1)
1761       CALL allocate_surface_attributes_h( surf_h(1), nys, nyn, nxl, nxr )
1762!
1763!--    Model top
1764       surf_h(2)%ns = ns_h_on_file(2)
1765       CALL allocate_surface_attributes_h_top( surf_h(2), nys, nyn, nxl, nxr )
1766!
1767!--    Vertical surfaces
1768       DO  l = 0, 3
1769          surf_v(l)%ns = ns_v_on_file(l)
1770          CALL allocate_surface_attributes_v( surf_v(l), .FALSE.,              &
1771                                              nys, nyn, nxl, nxr )
1772       ENDDO
1773!
1774!--    In the following, gather data from surfaces elements with the same
1775!--    facing (but possibly differt type) on 1 data-type array.
1776       mm(0:2) = 1
1777       DO  l = 0, 2
1778          DO  i = nxl, nxr
1779             DO  j = nys, nyn
1780                DO  m = surf_def_h(l)%start_index(j,i),                        &
1781                        surf_def_h(l)%end_index(j,i)
1782                   IF ( ALLOCATED( surf_def_h(l)%us ) )                        &
1783                      surf_h(l)%us(mm(l))      = surf_def_h(l)%us(m)
1784                   IF ( ALLOCATED( surf_def_h(l)%ts ) )                        &
1785                      surf_h(l)%ts(mm(l))      = surf_def_h(l)%ts(m)
1786                   IF ( ALLOCATED( surf_def_h(l)%qs ) )                        &
1787                      surf_h(l)%qs(mm(l))      = surf_def_h(l)%qs(m)
1788                   IF ( ALLOCATED( surf_def_h(l)%ss ) )                        &
1789                      surf_h(l)%ss(mm(l))      = surf_def_h(l)%ss(m)
1790                   IF ( ALLOCATED( surf_def_h(l)%qcs ) )                       &
1791                      surf_h(l)%qcs(mm(l))     = surf_def_h(l)%qcs(m)
1792                   IF ( ALLOCATED( surf_def_h(l)%ncs ) )                       &
1793                      surf_h(l)%ncs(mm(l))     = surf_def_h(l)%ncs(m)
1794                   IF ( ALLOCATED( surf_def_h(l)%qrs ) )                       &
1795                      surf_h(l)%qrs(mm(l))     = surf_def_h(l)%qrs(m)
1796                   IF ( ALLOCATED( surf_def_h(l)%nrs ) )                       &
1797                      surf_h(l)%nrs(mm(l))     = surf_def_h(l)%nrs(m)
1798                   IF ( ALLOCATED( surf_def_h(l)%ol ) )                        &
1799                      surf_h(l)%ol(mm(l))      = surf_def_h(l)%ol(m)
1800                   IF ( ALLOCATED( surf_def_h(l)%rib ) )                       &
1801                      surf_h(l)%rib(mm(l))     = surf_def_h(l)%rib(m)
1802                   IF ( ALLOCATED( surf_def_h(l)%usws ) )                      &
1803                      surf_h(l)%usws(mm(l))    = surf_def_h(l)%usws(m)
1804                   IF ( ALLOCATED( surf_def_h(l)%vsws ) )                      &
1805                      surf_h(l)%vsws(mm(l))    = surf_def_h(l)%vsws(m)
1806                   IF ( ALLOCATED( surf_def_h(l)%shf ) )                       &
1807                      surf_h(l)%shf(mm(l))     = surf_def_h(l)%shf(m)
1808                   IF ( ALLOCATED( surf_def_h(l)%qsws ) )                      &
1809                      surf_h(l)%qsws(mm(l))    = surf_def_h(l)%qsws(m)
1810                   IF ( ALLOCATED( surf_def_h(l)%ssws ) )                      &
1811                      surf_h(l)%ssws(mm(l))    = surf_def_h(l)%ssws(m)
1812                   IF ( ALLOCATED( surf_def_h(l)%ncsws ) )                     &
1813                      surf_h(l)%ncsws(mm(l))   = surf_def_h(l)%ncsws(m)
1814                   IF ( ALLOCATED( surf_def_h(l)%nrsws ) )                     &
1815                      surf_h(l)%nrsws(mm(l))   = surf_def_h(l)%nrsws(m)
1816                   IF ( ALLOCATED( surf_def_h(l)%sasws ) )                     &
1817                      surf_h(l)%sasws(mm(l))   = surf_def_h(l)%sasws(m)
1818               
1819                   mm(l) = mm(l) + 1
1820                ENDDO
1821
1822                IF ( l == 0 )  THEN
1823                   DO  m = surf_lsm_h%start_index(j,i),                        &
1824                           surf_lsm_h%end_index(j,i)
1825                      IF ( ALLOCATED( surf_lsm_h%us ) )                        &
1826                         surf_h(0)%us(mm(0))      = surf_lsm_h%us(m)
1827                      IF ( ALLOCATED( surf_lsm_h%ts ) )                        &
1828                         surf_h(0)%ts(mm(0))      = surf_lsm_h%ts(m)
1829                      IF ( ALLOCATED( surf_lsm_h%qs ) )                        &
1830                         surf_h(0)%qs(mm(0))      = surf_lsm_h%qs(m)
1831                      IF ( ALLOCATED( surf_lsm_h%ss ) )                        &
1832                         surf_h(0)%ss(mm(0))      = surf_lsm_h%ss(m)
1833                      IF ( ALLOCATED( surf_lsm_h%qcs ) )                       &
1834                         surf_h(0)%qcs(mm(0))     = surf_lsm_h%qcs(m)
1835                      IF ( ALLOCATED( surf_lsm_h%ncs ) )                       &
1836                         surf_h(0)%ncs(mm(0))     = surf_lsm_h%ncs(m)
1837                      IF ( ALLOCATED( surf_lsm_h%qrs ) )                       &
1838                         surf_h(0)%qrs(mm(0))     = surf_lsm_h%qrs(m)
1839                      IF ( ALLOCATED( surf_lsm_h%nrs ) )                       &
1840                         surf_h(0)%nrs(mm(0))     = surf_lsm_h%nrs(m)
1841                      IF ( ALLOCATED( surf_lsm_h%ol ) )                        &
1842                         surf_h(0)%ol(mm(0))      = surf_lsm_h%ol(m)
1843                      IF ( ALLOCATED( surf_lsm_h%rib ) )                       &
1844                         surf_h(0)%rib(mm(0))     = surf_lsm_h%rib(m)
1845                      IF ( ALLOCATED( surf_lsm_h%usws ) )                      &
1846                         surf_h(0)%usws(mm(0))    = surf_lsm_h%usws(m)
1847                      IF ( ALLOCATED( surf_lsm_h%vsws ) )                      &
1848                         surf_h(0)%vsws(mm(0))    = surf_lsm_h%vsws(m)
1849                      IF ( ALLOCATED( surf_lsm_h%shf ) )                       &
1850                         surf_h(0)%shf(mm(0))     = surf_lsm_h%shf(m)
1851                      IF ( ALLOCATED( surf_lsm_h%qsws ) )                      &
1852                         surf_h(0)%qsws(mm(0))    = surf_lsm_h%qsws(m)
1853                      IF ( ALLOCATED( surf_lsm_h%ssws ) )                      &
1854                         surf_h(0)%ssws(mm(0))    = surf_lsm_h%ssws(m)
1855                      IF ( ALLOCATED( surf_lsm_h%ncsws ) )                     &
1856                         surf_h(0)%ncsws(mm(0))   = surf_lsm_h%ncsws(m)
1857                      IF ( ALLOCATED( surf_lsm_h%nrsws ) )                     &
1858                         surf_h(0)%nrsws(mm(0))   = surf_lsm_h%nrsws(m)
1859                      IF ( ALLOCATED( surf_lsm_h%sasws ) )                     &
1860                        surf_h(0)%sasws(mm(0))   = surf_lsm_h%sasws(m)
1861               
1862                      mm(0) = mm(0) + 1
1863             
1864                   ENDDO
1865
1866                   DO  m = surf_usm_h%start_index(j,i),                        &
1867                           surf_usm_h%end_index(j,i)
1868                      IF ( ALLOCATED( surf_usm_h%us ) )                        &
1869                         surf_h(0)%us(mm(0))      = surf_usm_h%us(m)
1870                      IF ( ALLOCATED( surf_usm_h%ts ) )                        &
1871                         surf_h(0)%ts(mm(0))      = surf_usm_h%ts(m)
1872                      IF ( ALLOCATED( surf_usm_h%qs ) )                        &
1873                         surf_h(0)%qs(mm(0))      = surf_usm_h%qs(m)
1874                      IF ( ALLOCATED( surf_usm_h%ss ) )                        &
1875                         surf_h(0)%ss(mm(0))      = surf_usm_h%ss(m)
1876                      IF ( ALLOCATED( surf_usm_h%qcs ) )                       &
1877                         surf_h(0)%qcs(mm(0))     = surf_usm_h%qcs(m)
1878                      IF ( ALLOCATED( surf_usm_h%ncs ) )                       &
1879                         surf_h(0)%ncs(mm(0))     = surf_usm_h%ncs(m)
1880                      IF ( ALLOCATED( surf_usm_h%qrs ) )                       &
1881                         surf_h(0)%qrs(mm(0))     = surf_usm_h%qrs(m)
1882                      IF ( ALLOCATED( surf_usm_h%nrs ) )                       &
1883                         surf_h(0)%nrs(mm(0))     = surf_usm_h%nrs(m)
1884                      IF ( ALLOCATED( surf_usm_h%ol ) )                        &
1885                         surf_h(0)%ol(mm(0))      = surf_usm_h%ol(m)
1886                      IF ( ALLOCATED( surf_usm_h%rib ) )                       &
1887                         surf_h(0)%rib(mm(0))     = surf_usm_h%rib(m)
1888                      IF ( ALLOCATED( surf_usm_h%usws ) )                      &
1889                         surf_h(0)%usws(mm(0))    = surf_usm_h%usws(m)
1890                      IF ( ALLOCATED( surf_usm_h%vsws ) )                      &
1891                         surf_h(0)%vsws(mm(0))    = surf_usm_h%vsws(m)
1892                      IF ( ALLOCATED( surf_usm_h%shf ) )                       &
1893                         surf_h(0)%shf(mm(0))     = surf_usm_h%shf(m)
1894                      IF ( ALLOCATED( surf_usm_h%qsws ) )                      &
1895                         surf_h(0)%qsws(mm(0))    = surf_usm_h%qsws(m)
1896                      IF ( ALLOCATED( surf_usm_h%ssws ) )                      &
1897                         surf_h(0)%ssws(mm(0))    = surf_usm_h%ssws(m)
1898                      IF ( ALLOCATED( surf_usm_h%ncsws ) )                     &
1899                         surf_h(0)%ncsws(mm(0))   = surf_usm_h%ncsws(m)
1900                      IF ( ALLOCATED( surf_usm_h%nrsws ) )                     &
1901                         surf_h(0)%nrsws(mm(0))   = surf_usm_h%nrsws(m)
1902                      IF ( ALLOCATED( surf_usm_h%sasws ) )                     &
1903                        surf_h(0)%sasws(mm(0))   = surf_usm_h%sasws(m)
1904               
1905                      mm(0) = mm(0) + 1
1906             
1907                   ENDDO
1908
1909
1910                ENDIF
1911
1912             ENDDO
1913
1914          ENDDO
1915          IF ( l == 0 )  THEN
1916             surf_h(l)%start_index = MAX( surf_def_h(l)%start_index,           &
1917                                          surf_lsm_h%start_index,              &
1918                                          surf_usm_h%start_index )
1919             surf_h(l)%end_index   = MAX( surf_def_h(l)%end_index,             &
1920                                          surf_lsm_h%end_index,                &
1921                                          surf_usm_h%end_index )
1922          ELSE
1923             surf_h(l)%start_index = surf_def_h(l)%start_index
1924             surf_h(l)%end_index   = surf_def_h(l)%end_index
1925          ENDIF
1926       ENDDO
1927
1928
1929       mm(0:3) = 1
1930       DO  l = 0, 3
1931          DO  i = nxl, nxr
1932             DO  j = nys, nyn
1933                DO  m = surf_def_v(l)%start_index(j,i),                        &
1934                        surf_def_v(l)%end_index(j,i)
1935                   IF ( ALLOCATED( surf_def_v(l)%us ) )                        &
1936                      surf_v(l)%us(mm(l))      = surf_def_v(l)%us(m)
1937                   IF ( ALLOCATED( surf_def_v(l)%ts ) )                        &
1938                      surf_v(l)%ts(mm(l))      = surf_def_v(l)%ts(m)
1939                   IF ( ALLOCATED( surf_def_v(l)%qs ) )                        &
1940                      surf_v(l)%qs(mm(l))      = surf_def_v(l)%qs(m)
1941                   IF ( ALLOCATED( surf_def_v(l)%ss ) )                        &
1942                      surf_v(l)%ss(mm(l))      = surf_def_v(l)%ss(m)
1943                   IF ( ALLOCATED( surf_def_v(l)%qcs ) )                       &
1944                      surf_v(l)%qcs(mm(l))     = surf_def_v(l)%qcs(m)
1945                   IF ( ALLOCATED( surf_def_v(l)%ncs ) )                       &
1946                      surf_v(l)%ncs(mm(l))     = surf_def_v(l)%ncs(m)
1947                   IF ( ALLOCATED( surf_def_v(l)%qrs ) )                       &
1948                      surf_v(l)%qrs(mm(l))     = surf_def_v(l)%qrs(m)
1949                   IF ( ALLOCATED( surf_def_v(l)%nrs ) )                       &
1950                      surf_v(l)%nrs(mm(l))     = surf_def_v(l)%nrs(m)
1951                   IF ( ALLOCATED( surf_def_v(l)%ol ) )                        &
1952                      surf_v(l)%ol(mm(l))      = surf_def_v(l)%ol(m)
1953                   IF ( ALLOCATED( surf_def_v(l)%rib ) )                       &
1954                      surf_v(l)%rib(mm(l))     = surf_def_v(l)%rib(m)
1955                   IF ( ALLOCATED( surf_def_v(l)%shf ) )                       &
1956                      surf_v(l)%shf(mm(l))     = surf_def_v(l)%shf(m)
1957                   IF ( ALLOCATED( surf_def_v(l)%qsws ) )                      &
1958                      surf_v(l)%qsws(mm(l))    = surf_def_v(l)%qsws(m)
1959                   IF ( ALLOCATED( surf_def_v(l)%ssws ) )                      &
1960                      surf_v(l)%ssws(mm(l))    = surf_def_v(l)%ssws(m)
1961                   IF ( ALLOCATED( surf_def_v(l)%ncsws ) )                     &
1962                      surf_v(l)%ncsws(mm(l))   = surf_def_v(l)%ncsws(m)
1963                   IF ( ALLOCATED( surf_def_v(l)%nrsws ) )                     &
1964                      surf_v(l)%nrsws(mm(l))   = surf_def_v(l)%nrsws(m)
1965                   IF ( ALLOCATED( surf_def_v(l)%sasws ) )                     &
1966                      surf_v(l)%sasws(mm(l))   = surf_def_v(l)%sasws(m)
1967                   IF ( ALLOCATED( surf_def_v(l)%mom_flux_uv) )                &
1968                      surf_v(l)%mom_flux_uv(mm(l))  = surf_def_v(l)%mom_flux_uv(m)
1969                   IF ( ALLOCATED( surf_def_v(l)%mom_flux_w) )                 &
1970                      surf_v(l)%mom_flux_w(mm(l))   = surf_def_v(l)%mom_flux_w(m)
1971                   IF ( ALLOCATED( surf_def_v(l)%mom_flux_tke) )               &
1972                      surf_v(l)%mom_flux_tke(0:1,mm(l)) = surf_def_v(l)%mom_flux_tke(0:1,m)
1973               
1974                   mm(l) = mm(l) + 1
1975                ENDDO
1976
1977                DO  m = surf_lsm_v(l)%start_index(j,i),                        &
1978                        surf_lsm_v(l)%end_index(j,i)
1979                   IF ( ALLOCATED( surf_lsm_v(l)%us ) )                        &
1980                      surf_v(l)%us(mm(l))      = surf_lsm_v(l)%us(m)
1981                   IF ( ALLOCATED( surf_lsm_v(l)%ts ) )                        &
1982                      surf_v(l)%ts(mm(l))      = surf_lsm_v(l)%ts(m)
1983                   IF ( ALLOCATED( surf_lsm_v(l)%qs ) )                        &
1984                      surf_v(l)%qs(mm(l))      = surf_lsm_v(l)%qs(m)
1985                   IF ( ALLOCATED( surf_lsm_v(l)%ss ) )                        &
1986                      surf_v(l)%ss(mm(l))      = surf_lsm_v(l)%ss(m)
1987                   IF ( ALLOCATED( surf_lsm_v(l)%qcs ) )                       &
1988                      surf_v(l)%qcs(mm(l))     = surf_lsm_v(l)%qcs(m)
1989                   IF ( ALLOCATED( surf_lsm_v(l)%ncs ) )                       &
1990                      surf_v(l)%ncs(mm(l))     = surf_lsm_v(l)%ncs(m)
1991                   IF ( ALLOCATED( surf_lsm_v(l)%qrs ) )                       &
1992                      surf_v(l)%qrs(mm(l))     = surf_lsm_v(l)%qrs(m)
1993                   IF ( ALLOCATED( surf_lsm_v(l)%nrs ) )                       &
1994                      surf_v(l)%nrs(mm(l))     = surf_lsm_v(l)%nrs(m)
1995                   IF ( ALLOCATED( surf_lsm_v(l)%ol ) )                        &
1996                      surf_v(l)%ol(mm(l))      = surf_lsm_v(l)%ol(m)
1997                   IF ( ALLOCATED( surf_lsm_v(l)%rib ) )                       &
1998                      surf_v(l)%rib(mm(l))     = surf_lsm_v(l)%rib(m)
1999                   IF ( ALLOCATED( surf_lsm_v(l)%usws ) )                      &
2000                      surf_v(l)%usws(mm(l))    = surf_lsm_v(l)%usws(m)
2001                   IF ( ALLOCATED( surf_lsm_v(l)%vsws ) )                      &
2002                      surf_v(l)%vsws(mm(l))    = surf_lsm_v(l)%vsws(m)
2003                   IF ( ALLOCATED( surf_lsm_v(l)%shf ) )                       &
2004                      surf_v(l)%shf(mm(l))     = surf_lsm_v(l)%shf(m)
2005                   IF ( ALLOCATED( surf_lsm_v(l)%qsws ) )                      &
2006                      surf_v(l)%qsws(mm(l))    = surf_lsm_v(l)%qsws(m)
2007                   IF ( ALLOCATED( surf_lsm_v(l)%ssws ) )                      &
2008                      surf_v(l)%ssws(mm(l))    = surf_lsm_v(l)%ssws(m)
2009                   IF ( ALLOCATED( surf_lsm_v(l)%ncsws ) )                     &
2010                      surf_v(l)%ncsws(mm(l))   = surf_lsm_v(l)%ncsws(m)
2011                   IF ( ALLOCATED( surf_lsm_v(l)%nrsws ) )                     &
2012                      surf_v(l)%nrsws(mm(l))   = surf_lsm_v(l)%nrsws(m)
2013                   IF ( ALLOCATED( surf_lsm_v(l)%sasws ) )                     &
2014                      surf_v(l)%sasws(mm(l))   = surf_lsm_v(l)%sasws(m)
2015                   IF ( ALLOCATED( surf_lsm_v(l)%mom_flux_uv) )                &
2016                      surf_v(l)%mom_flux_uv(mm(l))  = surf_lsm_v(l)%mom_flux_uv(m)
2017                   IF ( ALLOCATED( surf_lsm_v(l)%mom_flux_w) )                 &
2018                      surf_v(l)%mom_flux_w(mm(l))   = surf_lsm_v(l)%mom_flux_w(m)
2019                   IF ( ALLOCATED( surf_lsm_v(l)%mom_flux_tke) )               &
2020                      surf_v(l)%mom_flux_tke(0:1,mm(l)) = surf_lsm_v(l)%mom_flux_tke(0:1,m)
2021               
2022                   mm(l) = mm(l) + 1
2023                ENDDO
2024
2025                DO  m = surf_usm_v(l)%start_index(j,i),                        &
2026                        surf_usm_v(l)%end_index(j,i)
2027                   IF ( ALLOCATED( surf_usm_v(l)%us ) )                        &
2028                      surf_v(l)%us(mm(l))      = surf_usm_v(l)%us(m)
2029                   IF ( ALLOCATED( surf_usm_v(l)%ts ) )                        &
2030                      surf_v(l)%ts(mm(l))      = surf_usm_v(l)%ts(m)
2031                   IF ( ALLOCATED( surf_usm_v(l)%qs ) )                        &
2032                      surf_v(l)%qs(mm(l))      = surf_usm_v(l)%qs(m)
2033                   IF ( ALLOCATED( surf_usm_v(l)%ss ) )                        &
2034                      surf_v(l)%ss(mm(l))      = surf_usm_v(l)%ss(m)
2035                   IF ( ALLOCATED( surf_usm_v(l)%qcs ) )                       &
2036                      surf_v(l)%qcs(mm(l))     = surf_usm_v(l)%qcs(m)
2037                   IF ( ALLOCATED( surf_usm_v(l)%ncs ) )                       &
2038                      surf_v(l)%ncs(mm(l))     = surf_usm_v(l)%ncs(m)
2039                   IF ( ALLOCATED( surf_usm_v(l)%qrs ) )                       &
2040                      surf_v(l)%qrs(mm(l))     = surf_usm_v(l)%qrs(m)
2041                   IF ( ALLOCATED( surf_usm_v(l)%nrs ) )                       &
2042                      surf_v(l)%nrs(mm(l))     = surf_usm_v(l)%nrs(m)
2043                   IF ( ALLOCATED( surf_usm_v(l)%ol ) )                        &
2044                      surf_v(l)%ol(mm(l))      = surf_usm_v(l)%ol(m)
2045                   IF ( ALLOCATED( surf_usm_v(l)%rib ) )                       &
2046                      surf_v(l)%rib(mm(l))     = surf_usm_v(l)%rib(m)
2047                   IF ( ALLOCATED( surf_usm_v(l)%usws ) )                      &
2048                      surf_v(l)%usws(mm(l))    = surf_usm_v(l)%usws(m)
2049                   IF ( ALLOCATED( surf_usm_v(l)%vsws ) )                      &
2050                      surf_v(l)%vsws(mm(l))    = surf_usm_v(l)%vsws(m)
2051                   IF ( ALLOCATED( surf_usm_v(l)%shf ) )                       &
2052                      surf_v(l)%shf(mm(l))     = surf_usm_v(l)%shf(m)
2053                   IF ( ALLOCATED( surf_usm_v(l)%qsws ) )                      &
2054                      surf_v(l)%qsws(mm(l))    = surf_usm_v(l)%qsws(m)
2055                   IF ( ALLOCATED( surf_usm_v(l)%ssws ) )                      &
2056                      surf_v(l)%ssws(mm(l))    = surf_usm_v(l)%ssws(m)
2057                   IF ( ALLOCATED( surf_usm_v(l)%ncsws ) )                     &
2058                      surf_v(l)%ncsws(mm(l))   = surf_usm_v(l)%ncsws(m)
2059                   IF ( ALLOCATED( surf_usm_v(l)%nrsws ) )                     &
2060                      surf_v(l)%nrsws(mm(l))   = surf_usm_v(l)%nrsws(m)
2061                   IF ( ALLOCATED( surf_usm_v(l)%sasws ) )                     &
2062                      surf_v(l)%sasws(mm(l))   = surf_usm_v(l)%sasws(m)
2063                   IF ( ALLOCATED( surf_usm_v(l)%mom_flux_uv) )                &
2064                      surf_v(l)%mom_flux_uv(mm(l))  = surf_usm_v(l)%mom_flux_uv(m)
2065                   IF ( ALLOCATED( surf_usm_v(l)%mom_flux_w) )                 &
2066                      surf_v(l)%mom_flux_w(mm(l))   = surf_usm_v(l)%mom_flux_w(m)
2067                   IF ( ALLOCATED( surf_usm_v(l)%mom_flux_tke) )               &
2068                      surf_v(l)%mom_flux_tke(0:1,mm(l)) = surf_usm_v(l)%mom_flux_tke(0:1,m)
2069               
2070                   mm(l) = mm(l) + 1
2071                ENDDO
2072             
2073             ENDDO
2074          ENDDO
2075!
2076!--       Finally, determine start- and end-index for the respective surface
2077          surf_v(l)%start_index = MAX( surf_def_v(l)%start_index,              &
2078                                       surf_lsm_v(l)%start_index,              &
2079                                       surf_usm_v(l)%start_index )
2080          surf_v(l)%end_index   = MAX( surf_def_v(l)%end_index,                &
2081                                       surf_lsm_v(l)%end_index,                &
2082                                       surf_usm_v(l)%end_index   )
2083       ENDDO
2084
2085       WRITE ( 14 )  'ns_h_on_file                  '
2086       WRITE ( 14 )   ns_h_on_file
2087       WRITE ( 14 )  'ns_v_on_file                  '
2088       WRITE ( 14 )   ns_v_on_file
2089!
2090!--    Write required restart data.
2091!--    Start with horizontal surfaces (upward-, downward-facing, and model top)
2092       DO  l = 0, 2
2093          WRITE( dum, '(I1)')  l
2094         
2095          WRITE ( 14 )  'surf_h(' // dum // ')%start_index         ' 
2096          WRITE ( 14 )   surf_h(l)%start_index
2097          WRITE ( 14 )  'surf_h(' // dum // ')%end_index           ' 
2098          WRITE ( 14 )   surf_h(l)%end_index
2099
2100          WRITE ( 14 )  'surf_h(' // dum // ')%us                  ' 
2101          IF ( ALLOCATED ( surf_h(l)%us ) )  THEN
2102             WRITE ( 14 )  surf_h(l)%us
2103          ENDIF
2104          WRITE ( 14 )  'surf_h(' // dum // ')%ts                  ' 
2105          IF ( ALLOCATED ( surf_h(l)%ts ) )  THEN
2106             WRITE ( 14 )  surf_h(l)%ts
2107          ENDIF
2108          WRITE ( 14 )  'surf_h(' // dum // ')%qs                  ' 
2109          IF ( ALLOCATED ( surf_h(l)%qs ) )  THEN
2110             WRITE ( 14 )  surf_h(l)%qs
2111          ENDIF
2112          WRITE ( 14 )  'surf_h(' // dum // ')%ss                  ' 
2113          IF ( ALLOCATED ( surf_h(l)%ss ) )  THEN
2114             WRITE ( 14 )  surf_h(l)%ss
2115          ENDIF
2116          WRITE ( 14 )  'surf_h(' // dum // ')%qcs                 '
2117          IF ( ALLOCATED ( surf_h(l)%qcs ) )  THEN 
2118             WRITE ( 14 )  surf_h(l)%qcs
2119          ENDIF
2120          WRITE ( 14 )  'surf_h(' // dum // ')%ncs                 ' 
2121          IF ( ALLOCATED ( surf_h(l)%ncs ) )  THEN
2122             WRITE ( 14 )  surf_h(l)%ncs
2123          ENDIF
2124          WRITE ( 14 )  'surf_h(' // dum // ')%qrs                 '
2125          IF ( ALLOCATED ( surf_h(l)%qrs ) )  THEN 
2126             WRITE ( 14 )  surf_h(l)%qrs
2127          ENDIF
2128          WRITE ( 14 )  'surf_h(' // dum // ')%nrs                 ' 
2129          IF ( ALLOCATED ( surf_h(l)%nrs ) )  THEN
2130             WRITE ( 14 )  surf_h(l)%nrs
2131          ENDIF
2132          WRITE ( 14 )  'surf_h(' // dum // ')%ol                  ' 
2133          IF ( ALLOCATED ( surf_h(l)%ol ) )  THEN
2134             WRITE ( 14 )  surf_h(l)%ol
2135          ENDIF
2136          WRITE ( 14 )  'surf_h(' // dum // ')%rib                 ' 
2137          IF ( ALLOCATED ( surf_h(l)%rib ) )  THEN
2138             WRITE ( 14 )  surf_h(l)%rib
2139          ENDIF
2140          WRITE ( 14 )  'surf_h(' // dum // ')%usws                ' 
2141          IF ( ALLOCATED ( surf_h(l)%usws ) )  THEN
2142             WRITE ( 14 )  surf_h(l)%usws
2143          ENDIF
2144          WRITE ( 14 )  'surf_h(' // dum // ')%vsws                ' 
2145          IF ( ALLOCATED ( surf_h(l)%vsws ) )  THEN
2146             WRITE ( 14 )  surf_h(l)%vsws
2147          ENDIF
2148          WRITE ( 14 )  'surf_h(' // dum // ')%shf                 ' 
2149          IF ( ALLOCATED ( surf_h(l)%shf ) )  THEN
2150             WRITE ( 14 )  surf_h(l)%shf
2151          ENDIF
2152          WRITE ( 14 )  'surf_h(' // dum // ')%qsws                ' 
2153          IF ( ALLOCATED ( surf_h(l)%qsws ) )  THEN
2154             WRITE ( 14 )  surf_h(l)%qsws
2155          ENDIF
2156          WRITE ( 14 )  'surf_h(' // dum // ')%ssws                ' 
2157          IF ( ALLOCATED ( surf_h(l)%ssws ) )  THEN
2158             WRITE ( 14 )  surf_h(l)%ssws
2159          ENDIF
2160          WRITE ( 14 )  'surf_h(' // dum // ')%qcsws               ' 
2161          IF ( ALLOCATED ( surf_h(l)%qcsws ) )  THEN
2162             WRITE ( 14 )  surf_h(l)%qcsws
2163          ENDIF
2164          WRITE ( 14 )  'surf_h(' // dum // ')%ncsws               ' 
2165          IF ( ALLOCATED ( surf_h(l)%ncsws ) )  THEN
2166             WRITE ( 14 )  surf_h(l)%ncsws
2167          ENDIF
2168          WRITE ( 14 )  'surf_h(' // dum // ')%qrsws               ' 
2169          IF ( ALLOCATED ( surf_h(l)%qrsws ) )  THEN
2170             WRITE ( 14 )  surf_h(l)%qrsws
2171          ENDIF
2172          WRITE ( 14 )  'surf_h(' // dum // ')%nrsws               ' 
2173          IF ( ALLOCATED ( surf_h(l)%nrsws ) )  THEN
2174             WRITE ( 14 )  surf_h(l)%nrsws
2175          ENDIF
2176          WRITE ( 14 )  'surf_h(' // dum // ')%sasws               ' 
2177          IF ( ALLOCATED ( surf_h(l)%sasws ) )  THEN
2178             WRITE ( 14 )  surf_h(l)%sasws
2179          ENDIF
2180       ENDDO
2181!
2182!--    Write vertical surfaces
2183       DO  l = 0, 3
2184          WRITE( dum, '(I1)')  l
2185
2186          WRITE ( 14 )  'surf_v(' // dum // ')%start_index         ' 
2187          WRITE ( 14 )   surf_v(l)%start_index
2188          WRITE ( 14 )  'surf_v(' // dum // ')%end_index           ' 
2189          WRITE ( 14 )   surf_v(l)%end_index
2190
2191          WRITE ( 14 )  'surf_v(' // dum // ')%us                  ' 
2192          IF ( ALLOCATED ( surf_v(l)%us ) )  THEN
2193             WRITE ( 14 )  surf_v(l)%us
2194          ENDIF
2195          WRITE ( 14 )  'surf_v(' // dum // ')%ts                  ' 
2196          IF ( ALLOCATED ( surf_v(l)%ts ) )  THEN
2197             WRITE ( 14 )  surf_v(l)%ts
2198          ENDIF
2199          WRITE ( 14 )  'surf_v(' // dum // ')%qs                  ' 
2200          IF ( ALLOCATED ( surf_v(l)%qs ) )  THEN
2201             WRITE ( 14 )  surf_v(l)%qs
2202          ENDIF
2203          WRITE ( 14 )  'surf_v(' // dum // ')%ss                  ' 
2204          IF ( ALLOCATED ( surf_v(l)%ss ) )  THEN
2205             WRITE ( 14 )  surf_v(l)%ss
2206          ENDIF
2207          WRITE ( 14 )  'surf_v(' // dum // ')%qcs                 ' 
2208          IF ( ALLOCATED ( surf_v(l)%qcs ) )  THEN
2209             WRITE ( 14 )  surf_v(l)%qcs
2210          ENDIF
2211          WRITE ( 14 )  'surf_v(' // dum // ')%ncs                 ' 
2212          IF ( ALLOCATED ( surf_v(l)%ncs ) )  THEN
2213             WRITE ( 14 )  surf_v(l)%ncs
2214          ENDIF
2215          WRITE ( 14 )  'surf_v(' // dum // ')%qrs                 ' 
2216          IF ( ALLOCATED ( surf_v(l)%qrs ) )  THEN
2217             WRITE ( 14 )  surf_v(l)%qrs
2218          ENDIF
2219          WRITE ( 14 )  'surf_v(' // dum // ')%nrs                 ' 
2220          IF ( ALLOCATED ( surf_v(l)%nrs ) )  THEN
2221             WRITE ( 14 )  surf_v(l)%nrs
2222          ENDIF
2223          WRITE ( 14 )  'surf_v(' // dum // ')%ol                  ' 
2224          IF ( ALLOCATED ( surf_v(l)%ol ) )  THEN
2225             WRITE ( 14 )  surf_v(l)%ol
2226          ENDIF
2227          WRITE ( 14 )  'surf_v(' // dum // ')%rib                 ' 
2228          IF ( ALLOCATED ( surf_v(l)%rib ) )  THEN
2229             WRITE ( 14 )  surf_v(l)%rib
2230          ENDIF
2231          WRITE ( 14 )  'surf_v(' // dum // ')%shf                 ' 
2232          IF ( ALLOCATED ( surf_v(l)%shf ) )  THEN
2233             WRITE ( 14 )  surf_v(l)%shf
2234          ENDIF
2235          WRITE ( 14 )  'surf_v(' // dum // ')%qsws                ' 
2236          IF ( ALLOCATED ( surf_v(l)%qsws ) )  THEN
2237             WRITE ( 14 )  surf_v(l)%qsws
2238          ENDIF
2239          WRITE ( 14 )  'surf_v(' // dum // ')%ssws                ' 
2240          IF ( ALLOCATED ( surf_v(l)%ssws ) )  THEN
2241             WRITE ( 14 )  surf_v(l)%ssws
2242          ENDIF
2243          WRITE ( 14 )  'surf_v(' // dum // ')%qcsws               ' 
2244          IF ( ALLOCATED ( surf_v(l)%qcsws ) )  THEN
2245             WRITE ( 14 )  surf_v(l)%qcsws
2246          ENDIF
2247          WRITE ( 14 )  'surf_v(' // dum // ')%ncsws               ' 
2248          IF ( ALLOCATED ( surf_v(l)%ncsws ) )  THEN
2249             WRITE ( 14 )  surf_v(l)%ncsws
2250          ENDIF
2251          WRITE ( 14 )  'surf_v(' // dum // ')%qrsws               ' 
2252          IF ( ALLOCATED ( surf_v(l)%qrsws ) )  THEN
2253             WRITE ( 14 )  surf_v(l)%qrsws
2254          ENDIF
2255          WRITE ( 14 )  'surf_v(' // dum // ')%nrsws               ' 
2256          IF ( ALLOCATED ( surf_v(l)%nrsws ) )  THEN
2257             WRITE ( 14 )  surf_v(l)%nrsws
2258          ENDIF
2259          WRITE ( 14 )  'surf_v(' // dum // ')%sasws               ' 
2260          IF ( ALLOCATED ( surf_v(l)%sasws ) )  THEN
2261             WRITE ( 14 )  surf_v(l)%sasws
2262          ENDIF
2263          WRITE ( 14 )  'surf_v(' // dum // ')%mom_uv              ' 
2264          IF ( ALLOCATED ( surf_v(l)%mom_flux_uv ) )  THEN
2265             WRITE ( 14 )  surf_v(l)%mom_flux_uv
2266          ENDIF
2267          WRITE ( 14 )  'surf_v(' // dum // ')%mom_w               ' 
2268          IF ( ALLOCATED ( surf_v(l)%mom_flux_w ) )  THEN
2269             WRITE ( 14 )  surf_v(l)%mom_flux_w
2270          ENDIF
2271          WRITE ( 14 )  'surf_v(' // dum // ')%mom_tke             ' 
2272          IF ( ALLOCATED ( surf_v(l)%mom_flux_tke ) )  THEN
2273             WRITE ( 14 )  surf_v(l)%mom_flux_tke
2274          ENDIF
2275
2276       ENDDO
2277
2278       WRITE ( 14 )  '*** end surf ***              '
2279
2280    END SUBROUTINE surface_write_restart_data
2281
2282
2283!------------------------------------------------------------------------------!
2284! Description:
2285! ------------
2286!> Reads surface-related restart data. Please note, restart data for a certain
2287!> surface orientation (e.g. horizontal upward-facing) is stored in one
2288!> array, even if surface elements may belong to different surface types
2289!> natural or urban for example). Surface elements are redistributed into its
2290!> respective surface types within this routine. This allows e.g. changing the
2291!> surface type after reading the restart data, which might be required in case
2292!> of cyclic_fill mode.
2293!------------------------------------------------------------------------------!
2294    SUBROUTINE surface_read_restart_data( ii,                                  &
2295                                       nxlfa, nxl_on_file, nxrfa, nxr_on_file, &
2296                                       nynfa, nyn_on_file, nysfa, nys_on_file, &
2297                                       offset_xa, offset_ya, overlap_count )
2298
2299       USE pegrid,                                                             &
2300           ONLY: numprocs_previous_run
2301
2302       CHARACTER (LEN=1)  ::  dum         !< dummy to create correct string for reading input variable
2303       CHARACTER (LEN=30) ::  field_chr   !< input variable
2304
2305       INTEGER(iwp)       ::  i           !< running index along x-direction, refers to former domain size
2306       INTEGER(iwp)       ::  ic          !< running index along x-direction, refers to current domain size
2307       INTEGER(iwp)       ::  j           !< running index along y-direction, refers to former domain size
2308       INTEGER(iwp)       ::  jc          !< running index along y-direction, refers to former domain size
2309       INTEGER(iwp)       ::  k           !< running index along z-direction
2310       INTEGER(iwp)       ::  l           !< index variable for surface type
2311       INTEGER(iwp)       ::  m           !< running index for surface elements, refers to gathered array encompassing all surface types
2312       INTEGER(iwp)       ::  mm          !< running index for surface elements, refers to individual surface types
2313
2314       INTEGER(iwp)       ::  ii               !< running index over input files
2315       INTEGER(iwp)       ::  kk               !< running index over previous input files covering current local domain
2316       INTEGER(iwp)       ::  nxlc             !< index of left boundary on current subdomain
2317       INTEGER(iwp)       ::  nxlf             !< index of left boundary on former subdomain
2318       INTEGER(iwp)       ::  nxl_on_file      !< index of left boundary on former local domain
2319       INTEGER(iwp)       ::  nxrc             !< index of right boundary on current subdomain
2320       INTEGER(iwp)       ::  nxrf             !< index of right boundary on former subdomain
2321       INTEGER(iwp)       ::  nxr_on_file      !< index of right boundary on former local domain 
2322       INTEGER(iwp)       ::  nync             !< index of north boundary on current subdomain
2323       INTEGER(iwp)       ::  nynf             !< index of north boundary on former subdomain
2324       INTEGER(iwp)       ::  nyn_on_file      !< index of norht boundary on former local domain 
2325       INTEGER(iwp)       ::  nysc             !< index of south boundary on current subdomain
2326       INTEGER(iwp)       ::  nysf             !< index of south boundary on former subdomain
2327       INTEGER(iwp)       ::  nys_on_file      !< index of south boundary on former local domain 
2328       INTEGER(iwp)       ::  overlap_count    !< number of overlaps
2329 
2330       INTEGER(iwp), DIMENSION(numprocs_previous_run,1000) ::  nxlfa       !<
2331       INTEGER(iwp), DIMENSION(numprocs_previous_run,1000) ::  nxrfa       !<
2332       INTEGER(iwp), DIMENSION(numprocs_previous_run,1000) ::  nynfa       !<
2333       INTEGER(iwp), DIMENSION(numprocs_previous_run,1000) ::  nysfa       !<
2334       INTEGER(iwp), DIMENSION(numprocs_previous_run,1000) ::  offset_xa   !<
2335       INTEGER(iwp), DIMENSION(numprocs_previous_run,1000) ::  offset_ya   !<
2336
2337
2338       LOGICAL                         ::  horizontal_surface !< flag indicating horizontal surfaces
2339       LOGICAL                         ::  surf_match_def     !< flag indicating that surface element is of default type
2340       LOGICAL                         ::  surf_match_lsm     !< flag indicating that surface element is of natural type
2341       LOGICAL                         ::  surf_match_usm     !< flag indicating that surface element is of urban type
2342       LOGICAL                         ::  vertical_surface   !< flag indicating vertical surfaces
2343
2344       TYPE(surf_type), DIMENSION(0:2) ::  surf_h             !< horizontal surface type on file
2345       TYPE(surf_type), DIMENSION(0:3) ::  surf_v             !< vertical surface type on file
2346
2347!
2348!--    Read number of respective surface elements on file
2349       READ ( 13 )  field_chr
2350       IF ( TRIM( field_chr ) /= 'ns_h_on_file' )  THEN
2351!
2352!--       Add a proper error message
2353       ENDIF
2354       READ ( 13 ) ns_h_on_file
2355
2356       READ ( 13 )  field_chr
2357       IF ( TRIM( field_chr ) /= 'ns_v_on_file' )  THEN
2358!
2359!--       Add a proper error message
2360       ENDIF
2361       READ ( 13 ) ns_v_on_file
2362!
2363!--    Allocate memory for number of surface elements on file. Please note,
2364!--    these number is not necessarily the same as the final number of surface
2365!--    elements on local domain, which is the case if processor topology changes
2366!--    during restart runs.
2367!--    Horizontal upward facing
2368       surf_h(0)%ns = ns_h_on_file(0)
2369       CALL allocate_surface_attributes_h( surf_h(0),                          &
2370                                           nys_on_file, nyn_on_file,           &
2371                                           nxl_on_file, nxr_on_file )
2372!
2373!--    Horizontal downward facing
2374       surf_h(1)%ns = ns_h_on_file(1)
2375       CALL allocate_surface_attributes_h( surf_h(1),                          &
2376                                           nys_on_file, nyn_on_file,           &
2377                                           nxl_on_file, nxr_on_file )
2378!
2379!--    Model top
2380       surf_h(2)%ns = ns_h_on_file(2)
2381       CALL allocate_surface_attributes_h_top( surf_h(2),                      &
2382                                               nys_on_file, nyn_on_file,       &
2383                                               nxl_on_file, nxr_on_file )
2384!
2385!--    Vertical surfaces
2386       DO  l = 0, 3
2387          surf_v(l)%ns = ns_v_on_file(l)
2388          CALL allocate_surface_attributes_v( surf_v(l), .FALSE.,              &
2389                                              nys_on_file, nyn_on_file,        &
2390                                              nxl_on_file, nxr_on_file )
2391       ENDDO
2392
2393       IF ( initializing_actions == 'read_restart_data'  .OR.                  &
2394            initializing_actions == 'cyclic_fill' )  THEN
2395!
2396!--       Initial setting of flags for horizontal and vertical surfaces, will
2397!--       be set after start- and end-indices are read.
2398          horizontal_surface = .FALSE.
2399          vertical_surface   = .FALSE.
2400
2401          READ ( 13 )  field_chr
2402
2403          DO  WHILE ( TRIM( field_chr ) /= '*** end surf ***' )
2404!
2405!--          Map data on file as often as needed (data are read only for k=1)
2406             DO  kk = 1, overlap_count
2407!
2408!--             Get the index range of the subdomain on file which overlap with the
2409!--             current subdomain
2410                nxlf = nxlfa(ii,kk)
2411                nxlc = nxlfa(ii,kk) + offset_xa(ii,kk)
2412                nxrf = nxrfa(ii,kk)
2413                nxrc = nxrfa(ii,kk) + offset_xa(ii,kk)
2414                nysf = nysfa(ii,kk)
2415                nysc = nysfa(ii,kk) + offset_ya(ii,kk)
2416                nynf = nynfa(ii,kk)
2417                nync = nynfa(ii,kk) + offset_ya(ii,kk)
2418
2419                SELECT CASE ( TRIM( field_chr ) )
2420
2421                   CASE ( 'surf_h(0)%start_index' )
2422                      IF ( kk == 1 )                                           &
2423                         READ ( 13 )  surf_h(0)%start_index
2424                      l = 0
2425                   CASE ( 'surf_h(0)%end_index' )   
2426                      IF ( kk == 1 )                                           &
2427                         READ ( 13 )  surf_h(0)%end_index
2428                      horizontal_surface = .TRUE.
2429                      vertical_surface   = .FALSE.
2430                   CASE ( 'surf_h(0)%us' )         
2431                      IF ( ALLOCATED( surf_h(0)%us )  .AND.  kk == 1 )         &
2432                         READ ( 13 )  surf_h(0)%us
2433                   CASE ( 'surf_h(0)%ts' )         
2434                      IF ( ALLOCATED( surf_h(0)%ts )  .AND.  kk == 1 )         &
2435                         READ ( 13 )  surf_h(0)%ts
2436                   CASE ( 'surf_h(0)%qs' )         
2437                      IF ( ALLOCATED( surf_h(0)%qs )  .AND.  kk == 1 )         &
2438                         READ ( 13 )  surf_h(0)%qs
2439                   CASE ( 'surf_h(0)%ss' )         
2440                      IF ( ALLOCATED( surf_h(0)%ss )  .AND.  kk == 1 )         &
2441                         READ ( 13 )  surf_h(0)%ss
2442                   CASE ( 'surf_h(0)%qcs' )         
2443                      IF ( ALLOCATED( surf_h(0)%qcs )  .AND.  kk == 1 )        &
2444                         READ ( 13 )  surf_h(0)%qcs
2445                   CASE ( 'surf_h(0)%ncs' )         
2446                      IF ( ALLOCATED( surf_h(0)%ncs )  .AND.  kk == 1 )        &
2447                         READ ( 13 )  surf_h(0)%ncs
2448                   CASE ( 'surf_h(0)%qrs' )         
2449                      IF ( ALLOCATED( surf_h(0)%qrs )  .AND.  kk == 1 )        &
2450                         READ ( 13 )  surf_h(0)%qrs
2451                   CASE ( 'surf_h(0)%nrs' )         
2452                      IF ( ALLOCATED( surf_h(0)%nrs )  .AND.  kk == 1 )        &
2453                         READ ( 13 )  surf_h(0)%nrs
2454                   CASE ( 'surf_h(0)%ol' )         
2455                      IF ( ALLOCATED( surf_h(0)%ol )  .AND.  kk == 1 )         &
2456                         READ ( 13 )  surf_h(0)%ol
2457                   CASE ( 'surf_h(0)%rib' )         
2458                      IF ( ALLOCATED( surf_h(0)%rib )  .AND.  kk == 1 )        &
2459                         READ ( 13 )  surf_h(0)%rib
2460                   CASE ( 'surf_h(0)%usws' )         
2461                      IF ( ALLOCATED( surf_h(0)%usws )  .AND.  kk == 1 )       &
2462                         READ ( 13 )  surf_h(0)%usws
2463                   CASE ( 'surf_h(0)%vsws' )         
2464                      IF ( ALLOCATED( surf_h(0)%vsws )  .AND.  kk == 1 )       &
2465                         READ ( 13 )  surf_h(0)%vsws
2466                   CASE ( 'surf_h(0)%shf' )         
2467                      IF ( ALLOCATED( surf_h(0)%shf )  .AND.  kk == 1 )        &
2468                         READ ( 13 )  surf_h(0)%shf
2469                   CASE ( 'surf_h(0)%qsws' )         
2470                      IF ( ALLOCATED( surf_h(0)%qsws )  .AND.  kk == 1 )       &
2471                         READ ( 13 )  surf_h(0)%qsws
2472                   CASE ( 'surf_h(0)%ssws' )         
2473                      IF ( ALLOCATED( surf_h(0)%ssws )  .AND.  kk == 1 )       &
2474                         READ ( 13 )  surf_h(0)%ssws
2475                   CASE ( 'surf_h(0)%qcsws' )         
2476                      IF ( ALLOCATED( surf_h(0)%qcsws )  .AND.  kk == 1 )      &
2477                         READ ( 13 )  surf_h(0)%qcsws
2478                   CASE ( 'surf_h(0)%ncsws' )         
2479                      IF ( ALLOCATED( surf_h(0)%ncsws )  .AND.  kk == 1 )      &
2480                         READ ( 13 )  surf_h(0)%ncsws
2481                   CASE ( 'surf_h(0)%qrsws' )         
2482                      IF ( ALLOCATED( surf_h(0)%qrsws )  .AND.  kk == 1 )      &
2483                         READ ( 13 )  surf_h(0)%qrsws
2484                   CASE ( 'surf_h(0)%nrsws' )         
2485                      IF ( ALLOCATED( surf_h(0)%nrsws )  .AND.  kk == 1 )      &
2486                         READ ( 13 )  surf_h(0)%nrsws
2487                   CASE ( 'surf_h(0)%sasws' )         
2488                      IF ( ALLOCATED( surf_h(0)%sasws )  .AND.  kk == 1 )      &
2489                         READ ( 13 )  surf_h(0)%sasws
2490
2491                   CASE ( 'surf_h(1)%start_index' )   
2492                      IF ( kk == 1 )                                           &
2493                         READ ( 13 )  surf_h(1)%start_index
2494                      l = 1
2495                   CASE ( 'surf_h(1)%end_index' )   
2496                      IF ( kk == 1 )                                           &
2497                         READ ( 13 )  surf_h(1)%end_index
2498                   CASE ( 'surf_h(1)%us' )         
2499                      IF ( ALLOCATED( surf_h(1)%us )  .AND.  kk == 1 )         &
2500                         READ ( 13 )  surf_h(1)%us
2501                   CASE ( 'surf_h(1)%ts' )         
2502                      IF ( ALLOCATED( surf_h(1)%ts )  .AND.  kk == 1 )         &
2503                         READ ( 13 )  surf_h(1)%ts
2504                   CASE ( 'surf_h(1)%qs' )         
2505                      IF ( ALLOCATED( surf_h(1)%qs )  .AND.  kk == 1 )         &
2506                         READ ( 13 )  surf_h(1)%qs
2507                   CASE ( 'surf_h(1)%ss' )         
2508                      IF ( ALLOCATED( surf_h(1)%ss )  .AND.  kk == 1 )         &
2509                         READ ( 13 )  surf_h(1)%ss
2510                   CASE ( 'surf_h(1)%qcs' )         
2511                      IF ( ALLOCATED( surf_h(1)%qcs )  .AND.  kk == 1 )        &
2512                         READ ( 13 )  surf_h(1)%qcs
2513                   CASE ( 'surf_h(1)%ncs' )         
2514                      IF ( ALLOCATED( surf_h(1)%ncs )  .AND.  kk == 1 )        &
2515                         READ ( 13 )  surf_h(1)%ncs
2516                   CASE ( 'surf_h(1)%qrs' )         
2517                      IF ( ALLOCATED( surf_h(1)%qrs )  .AND.  kk == 1 )        &
2518                         READ ( 13 )  surf_h(1)%qrs
2519                   CASE ( 'surf_h(1)%nrs' )         
2520                      IF ( ALLOCATED( surf_h(1)%nrs )  .AND.  kk == 1 )        &
2521                         READ ( 13 )  surf_h(1)%nrs
2522                   CASE ( 'surf_h(1)%ol' )         
2523                      IF ( ALLOCATED( surf_h(1)%ol )  .AND.  kk == 1 )         &
2524                         READ ( 13 )  surf_h(1)%ol
2525                   CASE ( 'surf_h(1)%rib' )         
2526                      IF ( ALLOCATED( surf_h(1)%rib )  .AND.  kk == 1 )        &
2527                         READ ( 13 )  surf_h(1)%rib
2528                   CASE ( 'surf_h(1)%usws' )         
2529                      IF ( ALLOCATED( surf_h(1)%usws )  .AND.  kk == 1 )       &
2530                         READ ( 13 )  surf_h(1)%usws
2531                   CASE ( 'surf_h(1)%vsws' )         
2532                      IF ( ALLOCATED( surf_h(1)%vsws )  .AND.  kk == 1 )       &
2533                         READ ( 13 )  surf_h(1)%vsws
2534                   CASE ( 'surf_h(1)%shf' )         
2535                      IF ( ALLOCATED( surf_h(1)%shf )  .AND.  kk == 1 )        &
2536                         READ ( 13 )  surf_h(1)%shf
2537                   CASE ( 'surf_h(1)%qsws' )         
2538                      IF ( ALLOCATED( surf_h(1)%qsws )  .AND.  kk == 1 )       &
2539                         READ ( 13 )  surf_h(1)%qsws
2540                   CASE ( 'surf_h(1)%ssws' )         
2541                      IF ( ALLOCATED( surf_h(1)%ssws )  .AND.  kk == 1 )       &
2542                         READ ( 13 )  surf_h(1)%ssws
2543                   CASE ( 'surf_h(1)%qcsws' )         
2544                      IF ( ALLOCATED( surf_h(1)%qcsws )  .AND.  kk == 1 )      &
2545                         READ ( 13 )  surf_h(1)%qcsws
2546                   CASE ( 'surf_h(1)%ncsws' )         
2547                      IF ( ALLOCATED( surf_h(1)%ncsws )  .AND.  kk == 1 )      &
2548                         READ ( 13 )  surf_h(1)%ncsws
2549                   CASE ( 'surf_h(1)%qrsws' )         
2550                      IF ( ALLOCATED( surf_h(1)%qrsws )  .AND.  kk == 1 )      &
2551                         READ ( 13 )  surf_h(1)%qrsws
2552                   CASE ( 'surf_h(1)%nrsws' )         
2553                      IF ( ALLOCATED( surf_h(1)%nrsws )  .AND.  kk == 1 )      &
2554                         READ ( 13 )  surf_h(1)%nrsws
2555                   CASE ( 'surf_h(1)%sasws' )         
2556                      IF ( ALLOCATED( surf_h(1)%sasws )  .AND.  kk == 1 )      &
2557                         READ ( 13 )  surf_h(1)%sasws
2558
2559                   CASE ( 'surf_h(2)%start_index' )   
2560                      IF ( kk == 1 )                                           &
2561                         READ ( 13 )  surf_h(2)%start_index
2562                      l = 2
2563                   CASE ( 'surf_h(2)%end_index' )   
2564                      IF ( kk == 1 )                                           &
2565                         READ ( 13 )  surf_h(2)%end_index
2566                   CASE ( 'surf_h(2)%us' )         
2567                      IF ( ALLOCATED( surf_h(2)%us )  .AND.  kk == 1 )         &
2568                         READ ( 13 )  surf_h(2)%us
2569                   CASE ( 'surf_h(2)%ts' )         
2570                      IF ( ALLOCATED( surf_h(2)%ts )  .AND.  kk == 1 )         &
2571                         READ ( 13 )  surf_h(2)%ts
2572                   CASE ( 'surf_h(2)%qs' )       
2573                      IF ( ALLOCATED( surf_h(2)%qs )  .AND.  kk == 1 )         &
2574                         READ ( 13 )  surf_h(2)%qs
2575                   CASE ( 'surf_h(2)%ss' )         
2576                      IF ( ALLOCATED( surf_h(2)%ss )  .AND.  kk == 1 )         &
2577                         READ ( 13 )  surf_h(2)%ss
2578                   CASE ( 'surf_h(2)%qcs' )         
2579                      IF ( ALLOCATED( surf_h(2)%qcs )  .AND.  kk == 1 )        &
2580                         READ ( 13 )  surf_h(2)%qcs
2581                   CASE ( 'surf_h(2)%ncs' )         
2582                      IF ( ALLOCATED( surf_h(2)%ncs )  .AND.  kk == 1 )        &
2583                         READ ( 13 )  surf_h(2)%ncs
2584                   CASE ( 'surf_h(2)%qrs' )         
2585                      IF ( ALLOCATED( surf_h(2)%qrs )  .AND.  kk == 1 )        &
2586                         READ ( 13 )  surf_h(2)%qrs
2587                   CASE ( 'surf_h(2)%nrs' )         
2588                      IF ( ALLOCATED( surf_h(2)%nrs )  .AND.  kk == 1 )        &
2589                         READ ( 13 )  surf_h(2)%nrs
2590                   CASE ( 'surf_h(2)%ol' )         
2591                      IF ( ALLOCATED( surf_h(2)%ol )  .AND.  kk == 1 )         &
2592                         READ ( 13 )  surf_h(2)%ol
2593                   CASE ( 'surf_h(2)%rib' )         
2594                      IF ( ALLOCATED( surf_h(2)%rib )  .AND.  kk == 1 )        &
2595                         READ ( 13 )  surf_h(2)%rib
2596                   CASE ( 'surf_h(2)%usws' )         
2597                      IF ( ALLOCATED( surf_h(2)%usws )  .AND.  kk == 1 )       &
2598                         READ ( 13 )  surf_h(2)%usws
2599                   CASE ( 'surf_h(2)%vsws' )         
2600                      IF ( ALLOCATED( surf_h(2)%vsws )  .AND.  kk == 1 )       &
2601                         READ ( 13 )  surf_h(2)%vsws
2602                   CASE ( 'surf_h(2)%shf' )         
2603                      IF ( ALLOCATED( surf_h(2)%shf )  .AND.  kk == 1 )        &
2604                         READ ( 13 )  surf_h(2)%shf
2605                   CASE ( 'surf_h(2)%qsws' )         
2606                      IF ( ALLOCATED( surf_h(2)%qsws )  .AND.  kk == 1 )       &
2607                         READ ( 13 )  surf_h(2)%qsws
2608                   CASE ( 'surf_h(2)%ssws' )         
2609                      IF ( ALLOCATED( surf_h(2)%ssws )  .AND.  kk == 1 )       &
2610                         READ ( 13 )  surf_h(2)%ssws
2611                   CASE ( 'surf_h(2)%qcsws' )         
2612                      IF ( ALLOCATED( surf_h(2)%qcsws )  .AND.  kk == 1 )      &
2613                         READ ( 13 )  surf_h(2)%qcsws
2614                   CASE ( 'surf_h(2)%ncsws' )         
2615                      IF ( ALLOCATED( surf_h(2)%ncsws )  .AND.  kk == 1 )      &
2616                         READ ( 13 )  surf_h(2)%ncsws
2617                   CASE ( 'surf_h(2)%qrsws' )         
2618                      IF ( ALLOCATED( surf_h(2)%qrsws )  .AND.  kk == 1 )      &
2619                         READ ( 13 )  surf_h(2)%qrsws
2620                   CASE ( 'surf_h(2)%nrsws' )         
2621                      IF ( ALLOCATED( surf_h(2)%nrsws )  .AND.  kk == 1 )      &
2622                         READ ( 13 )  surf_h(2)%nrsws
2623                   CASE ( 'surf_h(2)%sasws' )         
2624                      IF ( ALLOCATED( surf_h(2)%sasws )  .AND.  kk == 1 )      &
2625                         READ ( 13 )  surf_h(2)%sasws
2626
2627                   CASE ( 'surf_v(0)%start_index' )   
2628                      IF ( kk == 1 )                                           &
2629                         READ ( 13 )  surf_v(0)%start_index
2630                      l = 0
2631                      horizontal_surface = .FALSE.
2632                      vertical_surface   = .TRUE.
2633                   CASE ( 'surf_v(0)%end_index' )   
2634                      IF ( kk == 1 )                                           &
2635                         READ ( 13 )  surf_v(0)%end_index
2636                   CASE ( 'surf_v(0)%us' )         
2637                      IF ( ALLOCATED( surf_v(0)%us )  .AND.  kk == 1 )         &
2638                         READ ( 13 )  surf_v(0)%us
2639                   CASE ( 'surf_v(0)%ts' )         
2640                      IF ( ALLOCATED( surf_v(0)%ts )  .AND.  kk == 1 )         &
2641                         READ ( 13 )  surf_v(0)%ts
2642                   CASE ( 'surf_v(0)%qs' )         
2643                      IF ( ALLOCATED( surf_v(0)%qs )  .AND.  kk == 1 )         &
2644                         READ ( 13 )  surf_v(0)%qs
2645                   CASE ( 'surf_v(0)%ss' )         
2646                      IF ( ALLOCATED( surf_v(0)%ss )  .AND.  kk == 1 )         &
2647                         READ ( 13 )  surf_v(0)%ss
2648                   CASE ( 'surf_v(0)%qcs' )         
2649                      IF ( ALLOCATED( surf_v(0)%qcs )  .AND.  kk == 1 )        &
2650                         READ ( 13 )  surf_v(0)%qcs
2651                   CASE ( 'surf_v(0)%ncs' )         
2652                      IF ( ALLOCATED( surf_v(0)%ncs )  .AND.  kk == 1 )        &
2653                         READ ( 13 )  surf_v(0)%ncs
2654                   CASE ( 'surf_v(0)%qrs' )         
2655                      IF ( ALLOCATED( surf_v(0)%qrs )  .AND.  kk == 1 )        &
2656                         READ ( 13 )  surf_v(0)%qrs
2657                   CASE ( 'surf_v(0)%nrs' )         
2658                      IF ( ALLOCATED( surf_v(0)%nrs )  .AND.  kk == 1 )        &
2659                         READ ( 13 )  surf_v(0)%nrs
2660                   CASE ( 'surf_v(0)%ol' )         
2661                      IF ( ALLOCATED( surf_v(0)%ol )  .AND.  kk == 1 )         &
2662                         READ ( 13 )  surf_v(0)%ol
2663                   CASE ( 'surf_v(0)%rib' )         
2664                      IF ( ALLOCATED( surf_v(0)%rib )  .AND.  kk == 1 )        &
2665                         READ ( 13 )  surf_v(0)%rib
2666                   CASE ( 'surf_v(0)%shf' )         
2667                      IF ( ALLOCATED( surf_v(0)%shf )  .AND.  kk == 1 )        &
2668                         READ ( 13 )  surf_v(0)%shf
2669                   CASE ( 'surf_v(0)%qsws' )         
2670                      IF ( ALLOCATED( surf_v(0)%qsws )  .AND.  kk == 1 )       &
2671                         READ ( 13 )  surf_v(0)%qsws
2672                   CASE ( 'surf_v(0)%ssws' )         
2673                      IF ( ALLOCATED( surf_v(0)%ssws )  .AND.  kk == 1 )       &
2674                         READ ( 13 )  surf_v(0)%ssws
2675                   CASE ( 'surf_v(0)%qcsws' )         
2676                      IF ( ALLOCATED( surf_v(0)%qcsws )  .AND.  kk == 1 )      &
2677                         READ ( 13 )  surf_v(0)%qcsws
2678                   CASE ( 'surf_v(0)%ncsws' )         
2679                      IF ( ALLOCATED( surf_v(0)%ncsws )  .AND.  kk == 1 )      &
2680                         READ ( 13 )  surf_v(0)%ncsws
2681                   CASE ( 'surf_v(0)%qrsws' )         
2682                      IF ( ALLOCATED( surf_v(0)%qrsws )  .AND.  kk == 1 )      &
2683                         READ ( 13 )  surf_v(0)%qrsws
2684                   CASE ( 'surf_v(0)%nrsws' )         
2685                      IF ( ALLOCATED( surf_v(0)%nrsws )  .AND.  kk == 1 )      &
2686                         READ ( 13 )  surf_v(0)%nrsws
2687                   CASE ( 'surf_v(0)%sasws' )         
2688                      IF ( ALLOCATED( surf_v(0)%sasws )  .AND.  kk == 1 )      &
2689                         READ ( 13 )  surf_v(0)%sasws
2690                   CASE ( 'surf_v(0)%mom_uv' )         
2691                      IF ( ALLOCATED( surf_v(0)%mom_flux_uv )  .AND.  kk == 1 )&
2692                         READ ( 13 )  surf_v(0)%mom_flux_uv
2693                   CASE ( 'surf_v(0)%mom_w' )         
2694                      IF ( ALLOCATED( surf_v(0)%mom_flux_w )  .AND.  kk == 1 ) &
2695                         READ ( 13 )  surf_v(0)%mom_flux_w
2696                   CASE ( 'surf_v(0)%mom_tke' )         
2697                      IF ( ALLOCATED( surf_v(0)%mom_flux_tke )  .AND.  kk == 1 )&
2698                         READ ( 13 )  surf_v(0)%mom_flux_tke
2699
2700                   CASE ( 'surf_v(1)%start_index' )   
2701                      IF ( kk == 1 )                                           &
2702                         READ ( 13 )  surf_v(1)%start_index
2703                      l = 1
2704                   CASE ( 'surf_v(1)%end_index' )   
2705                      IF ( kk == 1 )                                           &
2706                         READ ( 13 )  surf_v(1)%end_index
2707                   CASE ( 'surf_v(1)%us' )         
2708                      IF ( ALLOCATED( surf_v(1)%us )  .AND.  kk == 1 )         &
2709                         READ ( 13 )  surf_v(1)%us
2710                   CASE ( 'surf_v(1)%ts' )         
2711                      IF ( ALLOCATED( surf_v(1)%ts )  .AND.  kk == 1 )         &
2712                         READ ( 13 )  surf_v(1)%ts
2713                   CASE ( 'surf_v(1)%qs' )         
2714                      IF ( ALLOCATED( surf_v(1)%qs )  .AND.  kk == 1 )         &
2715                         READ ( 13 )  surf_v(1)%qs
2716                   CASE ( 'surf_v(1)%ss' )         
2717                      IF ( ALLOCATED( surf_v(1)%ss )  .AND.  kk == 1 )         &
2718                         READ ( 13 )  surf_v(1)%ss
2719                   CASE ( 'surf_v(1)%qcs' )         
2720                      IF ( ALLOCATED( surf_v(1)%qcs )  .AND.  kk == 1 )        &
2721                         READ ( 13 )  surf_v(1)%qcs
2722                   CASE ( 'surf_v(1)%ncs' )         
2723                      IF ( ALLOCATED( surf_v(1)%ncs )  .AND.  kk == 1 )        &
2724                         READ ( 13 )  surf_v(1)%ncs
2725                   CASE ( 'surf_v(1)%qrs' )         
2726                      IF ( ALLOCATED( surf_v(1)%qrs )  .AND.  kk == 1 )        &
2727                         READ ( 13 )  surf_v(1)%qrs
2728                   CASE ( 'surf_v(1)%nrs' )         
2729                      IF ( ALLOCATED( surf_v(1)%nrs )  .AND.  kk == 1 )        &
2730                         READ ( 13 )  surf_v(1)%nrs
2731                   CASE ( 'surf_v(1)%ol' )         
2732                      IF ( ALLOCATED( surf_v(1)%ol )  .AND.  kk == 1 )         &
2733                         READ ( 13 )  surf_v(1)%ol
2734                   CASE ( 'surf_v(1)%rib' )         
2735                      IF ( ALLOCATED( surf_v(1)%rib )  .AND.  kk == 1 )        &
2736                         READ ( 13 )  surf_v(1)%rib
2737                   CASE ( 'surf_v(1)%shf' )         
2738                      IF ( ALLOCATED( surf_v(1)%shf )  .AND.  kk == 1 )        &
2739                         READ ( 13 )  surf_v(1)%shf
2740                   CASE ( 'surf_v(1)%qsws' )         
2741                      IF ( ALLOCATED( surf_v(1)%qsws )  .AND.  kk == 1 )       &
2742                         READ ( 13 )  surf_v(1)%qsws
2743                   CASE ( 'surf_v(1)%ssws' )         
2744                      IF ( ALLOCATED( surf_v(1)%ssws )  .AND.  kk == 1 )       &
2745                         READ ( 13 )  surf_v(1)%ssws
2746                   CASE ( 'surf_v(1)%qcsws' )         
2747                      IF ( ALLOCATED( surf_v(1)%qcsws )  .AND.  kk == 1 )      &
2748                         READ ( 13 )  surf_v(1)%qcsws
2749                   CASE ( 'surf_v(1)%ncsws' )         
2750                      IF ( ALLOCATED( surf_v(1)%ncsws )  .AND.  kk == 1 )      &
2751                         READ ( 13 )  surf_v(1)%ncsws
2752                   CASE ( 'surf_v(1)%qrsws' )         
2753                      IF ( ALLOCATED( surf_v(1)%qrsws )  .AND.  kk == 1 )      &
2754                         READ ( 13 )  surf_v(1)%qrsws
2755                   CASE ( 'surf_v(1)%nrsws' )         
2756                      IF ( ALLOCATED( surf_v(1)%nrsws )  .AND.  kk == 1 )      &
2757                         READ ( 13 )  surf_v(1)%nrsws
2758                   CASE ( 'surf_v(1)%sasws' )         
2759                      IF ( ALLOCATED( surf_v(1)%sasws )  .AND.  kk == 1 )      &
2760                         READ ( 13 )  surf_v(1)%sasws
2761                   CASE ( 'surf_v(1)%mom_uv' )         
2762                      IF ( ALLOCATED( surf_v(1)%mom_flux_uv )  .AND.  kk == 1 )&
2763                         READ ( 13 )  surf_v(1)%mom_flux_uv
2764                   CASE ( 'surf_v(1)%mom_w' )         
2765                      IF ( ALLOCATED( surf_v(1)%mom_flux_w )  .AND.  kk == 1 ) &
2766                         READ ( 13 )  surf_v(1)%mom_flux_w
2767                   CASE ( 'surf_v(1)%mom_tke' )         
2768                      IF ( ALLOCATED( surf_v(1)%mom_flux_tke )  .AND.  kk == 1 )&
2769                         READ ( 13 )  surf_v(1)%mom_flux_tke
2770
2771                   CASE ( 'surf_v(2)%start_index' )   
2772                      IF ( kk == 1 )                                           &
2773                         READ ( 13 )  surf_v(2)%start_index
2774                      l = 2
2775                   CASE ( 'surf_v(2)%end_index' )   
2776                      IF ( kk == 1 )                                           &
2777                         READ ( 13 )  surf_v(2)%end_index
2778                   CASE ( 'surf_v(2)%us' )         
2779                      IF ( ALLOCATED( surf_v(2)%us )  .AND.  kk == 1 )         &
2780                         READ ( 13 )  surf_v(2)%us
2781                   CASE ( 'surf_v(2)%ts' )         
2782                      IF ( ALLOCATED( surf_v(2)%ts )  .AND.  kk == 1 )         &
2783                         READ ( 13 )  surf_v(2)%ts
2784                   CASE ( 'surf_v(2)%qs' )         
2785                      IF ( ALLOCATED( surf_v(2)%qs )  .AND.  kk == 1 )         &
2786                         READ ( 13 )  surf_v(2)%qs
2787                   CASE ( 'surf_v(2)%ss' )         
2788                      IF ( ALLOCATED( surf_v(2)%ss )  .AND.  kk == 1 )         &
2789                         READ ( 13 )  surf_v(2)%ss
2790                   CASE ( 'surf_v(2)%qcs' )         
2791                      IF ( ALLOCATED( surf_v(2)%qcs )  .AND.  kk == 1 )        &
2792                         READ ( 13 )  surf_v(2)%qcs
2793                   CASE ( 'surf_v(2)%ncs' )         
2794                      IF ( ALLOCATED( surf_v(2)%ncs )  .AND.  kk == 1 )        &
2795                         READ ( 13 )  surf_v(2)%ncs
2796                   CASE ( 'surf_v(2)%qrs' )         
2797                      IF ( ALLOCATED( surf_v(2)%qrs )  .AND.  kk == 1 )        &
2798                         READ ( 13 )  surf_v(2)%qrs
2799                   CASE ( 'surf_v(2)%nrs' )         
2800                      IF ( ALLOCATED( surf_v(2)%nrs )  .AND.  kk == 1 )        &
2801                         READ ( 13 )  surf_v(2)%nrs
2802                   CASE ( 'surf_v(2)%ol' )         
2803                      IF ( ALLOCATED( surf_v(2)%ol )  .AND.  kk == 1 )         &
2804                         READ ( 13 )  surf_v(2)%ol
2805                   CASE ( 'surf_v(2)%rib' )         
2806                      IF ( ALLOCATED( surf_v(2)%rib )  .AND.  kk == 1 )        &
2807                         READ ( 13 )  surf_v(2)%rib
2808                   CASE ( 'surf_v(2)%shf' )         
2809                      IF ( ALLOCATED( surf_v(2)%shf )  .AND.  kk == 1 )        &
2810                         READ ( 13 )  surf_v(2)%shf
2811                   CASE ( 'surf_v(2)%qsws' )         
2812                      IF ( ALLOCATED( surf_v(2)%qsws )  .AND.  kk == 1 )       &
2813                         READ ( 13 )  surf_v(2)%qsws
2814                   CASE ( 'surf_v(2)%ssws' )         
2815                      IF ( ALLOCATED( surf_v(2)%ssws )  .AND.  kk == 1 )       &
2816                         READ ( 13 )  surf_v(2)%ssws
2817                   CASE ( 'surf_v(2)%qcsws' )         
2818                      IF ( ALLOCATED( surf_v(2)%qcsws )  .AND.  kk == 1 )      &
2819                         READ ( 13 )  surf_v(2)%qcsws
2820                   CASE ( 'surf_v(2)%ncsws' )         
2821                      IF ( ALLOCATED( surf_v(2)%ncsws )  .AND.  kk == 1 )      &
2822                         READ ( 13 )  surf_v(2)%ncsws
2823                   CASE ( 'surf_v(2)%qrsws' )         
2824                      IF ( ALLOCATED( surf_v(2)%qrsws )  .AND.  kk == 1 )      &
2825                         READ ( 13 )  surf_v(2)%qrsws
2826                   CASE ( 'surf_v(2)%nrsws' )         
2827                      IF ( ALLOCATED( surf_v(2)%nrsws )  .AND.  kk == 1 )      &
2828                         READ ( 13 )  surf_v(2)%nrsws
2829                   CASE ( 'surf_v(2)%sasws' )         
2830                      IF ( ALLOCATED( surf_v(2)%sasws )  .AND.  kk == 1 )      &
2831                         READ ( 13 )  surf_v(2)%sasws
2832                   CASE ( 'surf_v(2)%mom_uv' )         
2833                      IF ( ALLOCATED( surf_v(2)%mom_flux_uv )  .AND.  kk == 1 )&
2834                         READ ( 13 )  surf_v(2)%mom_flux_uv
2835                   CASE ( 'surf_v(2)%mom_w' )         
2836                      IF ( ALLOCATED( surf_v(2)%mom_flux_w )  .AND.  kk == 1 ) &
2837                         READ ( 13 )  surf_v(2)%mom_flux_w
2838                   CASE ( 'surf_v(2)%mom_tke' )         
2839                      IF ( ALLOCATED( surf_v(2)%mom_flux_tke )  .AND.  kk == 1 )&
2840                         READ ( 13 )  surf_v(2)%mom_flux_tke
2841
2842                   CASE ( 'surf_v(3)%start_index' )   
2843                      IF ( kk == 1 )                                           &
2844                         READ ( 13 )  surf_v(3)%start_index
2845                      l = 3
2846                   CASE ( 'surf_v(3)%end_index' )   
2847                      IF ( kk == 1 )                                           &
2848                         READ ( 13 )  surf_v(3)%end_index
2849                   CASE ( 'surf_v(3)%us' )         
2850                      IF ( ALLOCATED( surf_v(3)%us )  .AND.  kk == 1 )         &
2851                         READ ( 13 )  surf_v(3)%us
2852                   CASE ( 'surf_v(3)%ts' )         
2853                      IF ( ALLOCATED( surf_v(3)%ts )  .AND.  kk == 1 )         &
2854                         READ ( 13 )  surf_v(3)%ts
2855                   CASE ( 'surf_v(3)%qs' )       
2856                      IF ( ALLOCATED( surf_v(3)%qs )  .AND.  kk == 1 )         &
2857                         READ ( 13 )  surf_v(3)%qs
2858                   CASE ( 'surf_v(3)%ss' )         
2859                      IF ( ALLOCATED( surf_v(3)%ss )  .AND.  kk == 1 )         &
2860                         READ ( 13 )  surf_v(3)%ss
2861                   CASE ( 'surf_v(3)%qcs' )         
2862                      IF ( ALLOCATED( surf_v(3)%qcs )  .AND.  kk == 1 )        &
2863                         READ ( 13 )  surf_v(3)%qcs
2864                   CASE ( 'surf_v(3)%ncs' )         
2865                      IF ( ALLOCATED( surf_v(3)%ncs )  .AND.  kk == 1 )        &
2866                         READ ( 13 )  surf_v(3)%ncs
2867                   CASE ( 'surf_v(3)%qrs' )         
2868                      IF ( ALLOCATED( surf_v(3)%qrs )  .AND.  kk == 1 )        &
2869                         READ ( 13 )  surf_v(3)%qrs
2870                   CASE ( 'surf_v(3)%nrs' )         
2871                      IF ( ALLOCATED( surf_v(3)%nrs )  .AND.  kk == 1 )        &
2872                         READ ( 13 )  surf_v(3)%nrs
2873                   CASE ( 'surf_v(3)%ol' )         
2874                      IF ( ALLOCATED( surf_v(3)%ol )  .AND.  kk == 1 )         &
2875                         READ ( 13 )  surf_v(3)%ol
2876                   CASE ( 'surf_v(3)%rib' )         
2877                      IF ( ALLOCATED( surf_v(3)%rib )  .AND.  kk == 1 )        &
2878                         READ ( 13 )  surf_v(3)%rib
2879                   CASE ( 'surf_v(3)%shf' )         
2880                      IF ( ALLOCATED( surf_v(3)%shf )  .AND.  kk == 1 )        &
2881                         READ ( 13 )  surf_v(3)%shf
2882                   CASE ( 'surf_v(3)%qsws' )         
2883                      IF ( ALLOCATED( surf_v(3)%qsws )  .AND.  kk == 1 )       &
2884                         READ ( 13 )  surf_v(3)%qsws
2885                   CASE ( 'surf_v(3)%ssws' )         
2886                      IF ( ALLOCATED( surf_v(3)%ssws )  .AND.  kk == 1 )       &
2887                         READ ( 13 )  surf_v(3)%ssws
2888                   CASE ( 'surf_v(3)%qcsws' )         
2889                      IF ( ALLOCATED( surf_v(3)%qcsws )  .AND.  kk == 1 )      &
2890                         READ ( 13 )  surf_v(3)%qcsws
2891                   CASE ( 'surf_v(3)%ncsws' )         
2892                      IF ( ALLOCATED( surf_v(3)%ncsws )  .AND.  kk == 1 )      &
2893                         READ ( 13 )  surf_v(3)%ncsws
2894                   CASE ( 'surf_v(3)%qrsws' )         
2895                      IF ( ALLOCATED( surf_v(3)%qrsws )  .AND.  kk == 1 )      &
2896                         READ ( 13 )  surf_v(3)%qrsws
2897                   CASE ( 'surf_v(3)%nrsws' )         
2898                      IF ( ALLOCATED( surf_v(3)%nrsws )  .AND.  kk == 1 )      &
2899                         READ ( 13 )  surf_v(3)%nrsws
2900                   CASE ( 'surf_v(3)%sasws' )         
2901                      IF ( ALLOCATED( surf_v(3)%sasws )  .AND.  kk == 1 )      &
2902                         READ ( 13 )  surf_v(3)%sasws
2903                   CASE ( 'surf_v(3)%mom_uv' )         
2904                      IF ( ALLOCATED( surf_v(3)%mom_flux_uv )  .AND.  kk == 1 )&
2905                         READ ( 13 )  surf_v(3)%mom_flux_uv
2906                   CASE ( 'surf_v(3)%mom_w' )         
2907                      IF ( ALLOCATED( surf_v(3)%mom_flux_w )  .AND.  kk == 1 ) &
2908                         READ ( 13 )  surf_v(3)%mom_flux_w
2909                   CASE ( 'surf_v(3)%mom_tke' )         
2910                      IF ( ALLOCATED( surf_v(3)%mom_flux_tke )  .AND.  kk == 1 )&
2911                         READ ( 13 )  surf_v(3)%mom_flux_tke
2912
2913                END SELECT
2914!
2915!--             Redistribute surface elements on its respective type.
2916                IF ( horizontal_surface )  THEN
2917                   ic = nxlc
2918                   DO  i = nxlf, nxrf
2919                      jc = nysc
2920                      DO  j = nysf, nynf
2921
2922                         surf_match_def  = surf_def_h(l)%end_index(jc,ic) >=   &
2923                                           surf_def_h(l)%start_index(jc,ic)
2924                         surf_match_lsm  = surf_lsm_h%end_index(jc,ic)    >=   &
2925                                           surf_lsm_h%start_index(jc,ic)
2926                         surf_match_usm  = surf_usm_h%end_index(jc,ic)    >=   &
2927                                           surf_usm_h%start_index(jc,ic)
2928
2929                         IF ( surf_match_def )  THEN
2930                            mm = surf_def_h(l)%start_index(jc,ic)
2931                            DO  m = surf_h(l)%start_index(j,i),                &
2932                                    surf_h(l)%end_index(j,i)
2933                               CALL restore_surface_elements( surf_def_h(l),   &
2934                                                              mm, surf_h(l), m )
2935                               mm = mm + 1
2936                            ENDDO
2937                         ENDIF
2938
2939                         IF ( surf_match_lsm )  THEN
2940                            mm = surf_lsm_h%start_index(jc,ic)
2941                            DO  m = surf_h(l)%start_index(j,i),                &
2942                                    surf_h(l)%end_index(j,i)
2943                               CALL restore_surface_elements( surf_lsm_h,      &
2944                                                              mm, surf_h(l), m )
2945                               mm = mm + 1
2946                            ENDDO
2947                         ENDIF
2948
2949                         IF ( surf_match_usm )  THEN
2950                            mm = surf_usm_h%start_index(jc,ic)
2951                            DO  m = surf_h(l)%start_index(j,i),                &
2952                                    surf_h(l)%end_index(j,i)
2953                               CALL restore_surface_elements( surf_usm_h,      &
2954                                                              mm, surf_h(l), m )
2955                               mm = mm + 1
2956                            ENDDO
2957                         ENDIF
2958
2959                         jc = jc + 1
2960                      ENDDO
2961                      ic = ic + 1
2962                   ENDDO
2963                ELSEIF ( vertical_surface )  THEN
2964                   ic = nxlc
2965                   DO  i = nxlf, nxrf
2966                      jc = nysc
2967                      DO  j = nysf, nynf
2968
2969                         surf_match_def  = surf_def_v(l)%end_index(jc,ic) >=   &
2970                                           surf_def_v(l)%start_index(jc,ic)
2971                         surf_match_lsm  = surf_lsm_v(l)%end_index(jc,ic) >=   &
2972                                           surf_lsm_v(l)%start_index(jc,ic)
2973                         surf_match_usm  = surf_usm_v(l)%end_index(jc,ic) >=   &
2974                                           surf_usm_v(l)%start_index(jc,ic)
2975
2976
2977
2978                         IF ( surf_match_def )  THEN
2979                            mm = surf_def_v(l)%start_index(jc,ic)
2980                            DO  m = surf_v(l)%start_index(j,i),                &
2981                                    surf_v(l)%end_index(j,i)
2982                               CALL restore_surface_elements( surf_def_v(l),   &
2983                                                              mm, surf_v(l), m )
2984                               mm = mm + 1
2985                            ENDDO
2986                         ENDIF
2987
2988                         IF ( surf_match_lsm )  THEN
2989                            mm = surf_lsm_v(l)%start_index(jc,ic)
2990                            DO  m = surf_v(l)%start_index(j,i),                &
2991                                    surf_v(l)%end_index(j,i)
2992                               CALL restore_surface_elements( surf_lsm_v(l),   &
2993                                                              mm, surf_v(l), m )
2994                               mm = mm + 1
2995                            ENDDO
2996                         ENDIF
2997   
2998                         IF ( surf_match_usm )  THEN
2999                            mm = surf_usm_v(l)%start_index(jc,ic)
3000                            DO  m = surf_v(l)%start_index(j,i),                &
3001                                    surf_v(l)%end_index(j,i)
3002                               CALL restore_surface_elements( surf_usm_v(l),   &
3003                                                              mm, surf_v(l), m )
3004                               mm = mm + 1
3005                            ENDDO
3006                         ENDIF
3007
3008                         jc = jc + 1
3009                      ENDDO
3010                      ic = ic + 1
3011                   ENDDO
3012                ENDIF
3013
3014             ENDDO
3015
3016             READ ( 13 )  field_chr
3017
3018          ENDDO
3019
3020       ENDIF
3021
3022
3023       CONTAINS
3024!------------------------------------------------------------------------------!
3025! Description:
3026! ------------
3027!> Restores surfacle elements back on its respective type.
3028!------------------------------------------------------------------------------!
3029          SUBROUTINE restore_surface_elements( surf_target, m_target,          &
3030                                               surf_file,   m_file )
3031
3032             IMPLICIT NONE
3033
3034             INTEGER(iwp)      ::  m_file      !< respective surface-element index of current surface array
3035             INTEGER(iwp)      ::  m_target    !< respecitve surface-element index of surface array on file
3036
3037             TYPE( surf_type ) ::  surf_target !< target surface type
3038             TYPE( surf_type ) ::  surf_file   !< surface type on file
3039
3040             IF ( SCAN( TRIM( field_chr ), '%us' ) /= 0 )  THEN
3041                IF ( ALLOCATED( surf_target%us )  .AND.                        &
3042                     ALLOCATED( surf_file%us   ) )                             & 
3043                   surf_target%us(m_target) = surf_file%us(m_file)
3044             ENDIF
3045
3046             IF ( SCAN( TRIM( field_chr ), '%ol' ) /= 0 )  THEN
3047                IF ( ALLOCATED( surf_target%ol )  .AND.                        &
3048                     ALLOCATED( surf_file%ol   ) )                             & 
3049                   surf_target%ol(m_target) = surf_file%ol(m_file)
3050             ENDIF
3051
3052             IF ( SCAN( TRIM( field_chr ), '%usws' ) /= 0 )  THEN
3053                IF ( ALLOCATED( surf_target%usws )  .AND.                      &
3054                     ALLOCATED( surf_file%usws   ) )                           & 
3055                   surf_target%usws(m_target) = surf_file%usws(m_file)
3056             ENDIF
3057
3058             IF ( SCAN( TRIM( field_chr ), '%vsws' ) /= 0 )  THEN
3059                IF ( ALLOCATED( surf_target%vsws )  .AND.                      &
3060                     ALLOCATED( surf_file%vsws   ) )                           & 
3061                   surf_target%vsws(m_target) = surf_file%vsws(m_file)
3062             ENDIF
3063
3064             IF ( SCAN( TRIM( field_chr ), '%ts' ) /= 0 )  THEN
3065                IF ( ALLOCATED( surf_target%ts )  .AND.                        &
3066                     ALLOCATED( surf_file%ts   ) )                             & 
3067                   surf_target%ts(m_target) = surf_file%ts(m_file)
3068             ENDIF
3069
3070             IF ( SCAN( TRIM( field_chr ), '%shf' ) /= 0 )  THEN
3071                IF ( ALLOCATED( surf_target%shf )  .AND.                       &
3072                     ALLOCATED( surf_file%shf   ) )                            & 
3073                   surf_target%shf(m_target) = surf_file%shf(m_file)
3074             ENDIF
3075
3076             IF ( SCAN( TRIM( field_chr ), '%qs' ) /= 0 )  THEN
3077                IF ( ALLOCATED( surf_target%qs )  .AND.                        &
3078                     ALLOCATED( surf_file%qs   ) )                             & 
3079                   surf_target%qs(m_target) = surf_file%qs(m_file)
3080             ENDIF
3081
3082             IF ( SCAN( TRIM( field_chr ), '%qsws' ) /= 0 )  THEN
3083                IF ( ALLOCATED( surf_target%qsws )  .AND.                      &
3084                     ALLOCATED( surf_file%qsws   ) )                           & 
3085                   surf_target%qsws(m_target) = surf_file%qsws(m_file)
3086             ENDIF
3087
3088             IF ( SCAN( TRIM( field_chr ), '%ss' ) /= 0 )  THEN
3089                IF ( ALLOCATED( surf_target%ss )  .AND.                        &
3090                     ALLOCATED( surf_file%ss   ) )                             & 
3091                   surf_target%ss(m_target) = surf_file%ss(m_file)
3092             ENDIF
3093
3094             IF ( SCAN( TRIM( field_chr ), '%ssws' ) /= 0 )  THEN
3095                IF ( ALLOCATED( surf_target%ssws )  .AND.                      &
3096                     ALLOCATED( surf_file%ssws   ) )                           & 
3097                   surf_target%ssws(m_target) = surf_file%ssws(m_file)
3098             ENDIF
3099
3100             IF ( SCAN( TRIM( field_chr ), '%qcs' ) /= 0 )  THEN
3101                IF ( ALLOCATED( surf_target%qcs )  .AND.                       &
3102                     ALLOCATED( surf_file%qcs   ) )                            & 
3103                  surf_target%qcs(m_target) = surf_file%qcs(m_file)
3104             ENDIF
3105
3106             IF ( SCAN( TRIM( field_chr ), '%qcsws' ) /= 0 )  THEN
3107                IF ( ALLOCATED( surf_target%qcsws )  .AND.                     &
3108                     ALLOCATED( surf_file%qcsws   ) )                          & 
3109                   surf_target%qcsws(m_target) = surf_file%qcsws(m_file)
3110             ENDIF
3111
3112             IF ( SCAN( TRIM( field_chr ), '%ncs' ) /= 0 )  THEN
3113                IF ( ALLOCATED( surf_target%ncs )  .AND.                       &
3114                     ALLOCATED( surf_file%ncs   ) )                            & 
3115                   surf_target%ncs(m_target) = surf_file%ncs(m_file)
3116             ENDIF
3117
3118             IF ( SCAN( TRIM( field_chr ), '%ncsws' ) /= 0 )  THEN
3119                IF ( ALLOCATED( surf_target%ncsws )  .AND.                     &
3120                     ALLOCATED( surf_file%ncsws   ) )                          & 
3121                   surf_target%ncsws(m_target) = surf_file%ncsws(m_file)
3122             ENDIF
3123
3124             IF ( SCAN( TRIM( field_chr ), '%qrs' ) /= 0 )  THEN
3125                IF ( ALLOCATED( surf_target%qrs )  .AND.                       &
3126                     ALLOCATED( surf_file%qrs   ) )                            & 
3127                  surf_target%qrs(m_target) = surf_file%qrs(m_file)
3128             ENDIF
3129
3130             IF ( SCAN( TRIM( field_chr ), '%qrsws' ) /= 0 )  THEN
3131                IF ( ALLOCATED( surf_target%qrsws )  .AND.                     &
3132                     ALLOCATED( surf_file%qrsws   ) )                          & 
3133                   surf_target%qrsws(m_target) = surf_file%qrsws(m_file)
3134             ENDIF
3135
3136             IF ( SCAN( TRIM( field_chr ), '%nrs' ) /= 0 )  THEN
3137                IF ( ALLOCATED( surf_target%nrs )  .AND.                       &
3138                     ALLOCATED( surf_file%nrs   ) )                            & 
3139                   surf_target%nrs(m_target) = surf_file%nrs(m_file)
3140             ENDIF
3141
3142             IF ( SCAN( TRIM( field_chr ), '%nrsws' ) /= 0 )  THEN
3143                IF ( ALLOCATED( surf_target%nrsws )  .AND.                     &
3144                     ALLOCATED( surf_file%nrsws   ) )                          & 
3145                   surf_target%nrsws(m_target) = surf_file%nrsws(m_file)
3146             ENDIF
3147
3148             IF ( SCAN( TRIM( field_chr ), '%sasws' ) /= 0 )  THEN
3149                IF ( ALLOCATED( surf_target%sasws )  .AND.                     &
3150                     ALLOCATED( surf_file%sasws   ) )                          & 
3151                   surf_target%sasws(m_target) = surf_file%sasws(m_file)
3152             ENDIF
3153
3154             IF ( SCAN( TRIM( field_chr ), '%mom_uv' ) /= 0 )  THEN
3155                IF ( ALLOCATED( surf_target%mom_flux_uv )  .AND.               &
3156                     ALLOCATED( surf_file%mom_flux_uv   ) )                    & 
3157                   surf_target%mom_flux_uv(m_target) =                         &
3158                                           surf_file%mom_flux_uv(m_file)
3159             ENDIF
3160
3161             IF ( SCAN( TRIM( field_chr ), '%mom_w' ) /= 0 )  THEN
3162                IF ( ALLOCATED( surf_target%mom_flux_w )  .AND.                &
3163                     ALLOCATED( surf_file%mom_flux_w   ) )                     & 
3164                   surf_target%mom_flux_w(m_target) =                          &
3165                                           surf_file%mom_flux_w(m_file)
3166             ENDIF
3167
3168             IF ( SCAN( TRIM( field_chr ), '%mom_tke' ) /= 0 )  THEN
3169                IF ( ALLOCATED( surf_target%mom_flux_tke )  .AND.              &
3170                     ALLOCATED( surf_file%mom_flux_tke   ) )                   & 
3171                   surf_target%mom_flux_tke(0:1,m_target) =                    &
3172                                           surf_file%mom_flux_tke(0:1,m_file)
3173             ENDIF
3174
3175          END SUBROUTINE restore_surface_elements
3176
3177    END SUBROUTINE surface_read_restart_data
3178
3179 
3180!------------------------------------------------------------------------------!
3181! Description:
3182! ------------
3183!> Counts the number of surface elements with the same facing, required for
3184!> reading and writing restart data.
3185!------------------------------------------------------------------------------!
3186    SUBROUTINE surface_last_actions
3187
3188       IMPLICIT NONE
3189!
3190!--    Horizontal surfaces
3191       ns_h_on_file(0) = surf_def_h(0)%ns + surf_lsm_h%ns + surf_usm_h%ns
3192       ns_h_on_file(1) = surf_def_h(1)%ns
3193       ns_h_on_file(2) = surf_def_h(2)%ns
3194!
3195!--    Vertical surfaces
3196       ns_v_on_file(0) = surf_def_v(0)%ns + surf_lsm_v(0)%ns + surf_usm_v(0)%ns
3197       ns_v_on_file(1) = surf_def_v(1)%ns + surf_lsm_v(1)%ns + surf_usm_v(1)%ns
3198       ns_v_on_file(2) = surf_def_v(2)%ns + surf_lsm_v(2)%ns + surf_usm_v(2)%ns
3199       ns_v_on_file(3) = surf_def_v(3)%ns + surf_lsm_v(3)%ns + surf_usm_v(3)%ns
3200
3201    END SUBROUTINE surface_last_actions
3202
3203
3204 END MODULE surface_mod
Note: See TracBrowser for help on using the repository browser.