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

Last change on this file since 2320 was 2318, checked in by suehring, 7 years ago

get topograpyhy top index via function call

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