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

Last change on this file since 2338 was 2338, checked in by gronemeier, 7 years ago

modularized 1d model

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