Index: palm/trunk/SOURCE/init_grid.f90
===================================================================
--- palm/trunk/SOURCE/init_grid.f90	(revision 2550)
+++ palm/trunk/SOURCE/init_grid.f90	(revision 2696)
@@ -1,5 +1,5 @@
 !> @file init_grid.f90
 !------------------------------------------------------------------------------!
-! This file is part of PALM.
+! This file is part of the PALM model system.
 !
 ! PALM is free software: you can redistribute it and/or modify it under the
@@ -25,4 +25,13 @@
 ! -----------------
 ! $Id$
+! Revised topography input
+! Set nzb_max not for the entire nest domain, only for boundary PEs
+! Re-organize routine, split-up into several subroutines
+! Modularize poismg_noopt
+! Remove setting bit 26, 27, 28 in wall_flags_0, indicating former '_outer'
+! arrays (not required any more).  
+! Bugfix in generic tunnel setup (MS)
+! 
+! 2550 2017-10-16 17:12:01Z boeske
 ! Set lateral boundary conditions for topography on all three ghost layers
 ! 
@@ -239,9 +248,11 @@
 !
 ! Description:
-! ------------
+! -----------------------------------------------------------------------------!
 !> Creating grid depending constants
-!> To Do: Setting topo flags only based on topo_3d array - flags for former
-!> nzb_outer arrays are still not set properly. 
-!> To Do: Rearrange topo flag list
+!> @todo: Move initialization mixing length
+!> @todo: Rearrange topo flag list
+!> @todo: reference 3D buildings on top of orography is not tested and may need
+!>        further improvement for steep slopes
+!> @todo: Use more advanced setting of building type at filled holes 
 !------------------------------------------------------------------------------!
  SUBROUTINE init_grid
@@ -251,6 +262,5 @@
 
     USE arrays_3d,                                                             &
-        ONLY:  dd2zu, ddzu, ddzu_pres, ddzw, dzu, dzu_mg, dzw, dzw_mg, f1_mg,  &
-               f2_mg, f3_mg, l_grid, l_wall, zu, zw
+        ONLY:  dd2zu, ddzu, ddzu_pres, ddzw, dzu, dzw, zu, zw
         
     USE control_parameters,                                                    &
@@ -259,9 +269,9 @@
                canyon_height, canyon_wall_left, canyon_wall_south,             &
                canyon_width_x, canyon_width_y, constant_flux_layer,            &
-               coupling_char, coupling_mode,                                   &
                dp_level_ind_b, dz, dz_max, dz_stretch_factor,                  &
-               dz_stretch_level, dz_stretch_level_index, grid_level, ibc_uv_b, &
-               io_blocks, io_group, inflow_l, inflow_n, inflow_r, inflow_s,    &
-               lod, masking_method, maximum_grid_level, message_string,        &
+               dz_stretch_level, dz_stretch_level_index, grid_level,           &
+               force_bound_l, force_bound_r, force_bound_n, force_bound_s,     &
+               ibc_uv_b, inflow_l, inflow_n, inflow_r, inflow_s,               &
+               masking_method, maximum_grid_level, message_string,             &
                momentum_advec, nest_domain, nest_bound_l, nest_bound_n,        &
                nest_bound_r, nest_bound_s, ocean, outflow_l, outflow_n,        &
@@ -275,21 +285,15 @@
         
     USE indices,                                                               &
-        ONLY:  advc_flags_1, advc_flags_2, flags, nbgp, nx, nxl, nxlg, nxl_mg, &
-               nxr, nxrg, nxr_mg, ny, nyn, nyng, nyn_mg, nys, nys_mg, nysg, nz,&
+        ONLY:  nbgp, nx, nxl, nxlg, nxr, nxrg, ny, nyn, nyng, nys, nysg, nz,   &
                nzb, nzb_diff, nzb_diff_s_inner, nzb_diff_s_outer,              &
                nzb_max, nzb_s_inner, nzb_s_outer, nzb_u_inner,                 &
                nzb_u_outer, nzb_v_inner, nzb_v_outer, nzb_w_inner,             &
-               nzb_w_outer, nzt, nzt_mg, wall_flags_0, wall_flags_1,           &
-               wall_flags_10, wall_flags_2, wall_flags_3,  wall_flags_4,       &
-               wall_flags_5, wall_flags_6, wall_flags_7, wall_flags_8,         &
-               wall_flags_9
+               nzb_w_outer, nzt
     
     USE kinds
-#if defined ( __netcdf )
-    USE netcdf_interface,                                                      &
-        ONLY:  netcdf_close_file, netcdf_open_read_file, netcdf_get_attribute, &
-               netcdf_get_variable
-#endif
+
     USE pegrid
+
+    USE poismg_noopt_mod
 
     USE surface_mod,                                                           &
@@ -298,4 +302,1174 @@
     USE vertical_nesting_mod,                                                  &
         ONLY:  vnested, vnest_init_grid
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  i             !< index variable along x 
+    INTEGER(iwp) ::  j             !< index variable along y
+    INTEGER(iwp) ::  k             !< index variable along z
+    INTEGER(iwp) ::  k_top         !< topography top index on local PE
+    INTEGER(iwp) ::  l             !< loop variable 
+    INTEGER(iwp) ::  nzb_local_max !< vertical grid index of maximum topography height
+    INTEGER(iwp) ::  nzb_local_min !< vertical grid index of minimum topography height
+                                      
+    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  nzb_local      !< index for topography top at cell-center
+    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  nzb_tmp        !< dummy to calculate topography indices on u- and v-grid
+
+    INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE ::  topo !< input array for 3D topography and dummy array for setting "outer"-flags
+
+    REAL(wp) ::  dz_stretched  !< stretched vertical grid spacing
+
+
+!
+!-- Calculation of horizontal array bounds including ghost layers
+    nxlg = nxl - nbgp
+    nxrg = nxr + nbgp
+    nysg = nys - nbgp
+    nyng = nyn + nbgp
+
+!
+!-- Allocate grid arrays
+    ALLOCATE( ddzu(1:nzt+1), ddzw(1:nzt+1), dd2zu(1:nzt), dzu(1:nzt+1),        &
+              dzw(1:nzt+1), zu(nzb:nzt+1), zw(nzb:nzt+1) )
+
+!
+!-- Compute height of u-levels from constant grid length and dz stretch factors
+    IF ( dz == -1.0_wp )  THEN
+       message_string = 'missing dz'
+       CALL message( 'init_grid', 'PA0200', 1, 2, 0, 6, 0 ) 
+    ELSEIF ( dz <= 0.0_wp )  THEN
+       WRITE( message_string, * ) 'dz=',dz,' <= 0.0'
+       CALL message( 'init_grid', 'PA0201', 1, 2, 0, 6, 0 )
+    ENDIF
+
+!
+!-- Define the vertical grid levels
+    IF ( .NOT. ocean )  THEN
+!
+!--    Grid for atmosphere with surface at z=0 (k=0, w-grid).
+!--    The second u-level (k=1) corresponds to the top of the 
+!--    Prandtl-layer.
+
+       IF ( ibc_uv_b == 0 .OR. ibc_uv_b == 2 ) THEN
+          zu(0) = 0.0_wp
+      !    zu(0) = - dz * 0.5_wp
+       ELSE
+          zu(0) = - dz * 0.5_wp
+       ENDIF
+       zu(1) =   dz * 0.5_wp
+
+       dz_stretch_level_index = nzt+1
+       dz_stretched = dz
+       DO  k = 2, nzt+1
+          IF ( dz_stretch_level <= zu(k-1)  .AND.  dz_stretched < dz_max )  THEN
+             dz_stretched = dz_stretched * dz_stretch_factor
+             dz_stretched = MIN( dz_stretched, dz_max )
+             IF ( dz_stretch_level_index == nzt+1 ) dz_stretch_level_index = k-1
+          ENDIF
+          zu(k) = zu(k-1) + dz_stretched
+       ENDDO
+
+!
+!--    Compute the w-levels. They are always staggered half-way between the 
+!--    corresponding u-levels. In case of dirichlet bc for u and v at the 
+!--    ground the first u- and w-level (k=0) are defined at same height (z=0). 
+!--    The top w-level is extrapolated linearly.
+       zw(0) = 0.0_wp
+       DO  k = 1, nzt
+          zw(k) = ( zu(k) + zu(k+1) ) * 0.5_wp
+       ENDDO
+       zw(nzt+1) = zw(nzt) + 2.0_wp * ( zu(nzt+1) - zw(nzt) )
+
+    ELSE
+!
+!--    Grid for ocean with free water surface is at k=nzt (w-grid).
+!--    In case of neumann bc at the ground the first first u-level (k=0) lies
+!--    below the first w-level (k=0). In case of dirichlet bc the first u- and
+!--    w-level are defined at same height, but staggered from the second level.
+!--    The second u-level (k=1) corresponds to the top of the Prandtl-layer.
+       zu(nzt+1) =   dz * 0.5_wp
+       zu(nzt)   = - dz * 0.5_wp
+
+       dz_stretch_level_index = 0
+       dz_stretched = dz
+       DO  k = nzt-1, 0, -1
+!
+!--       The default value of dz_stretch_level is positive, thus the first 
+!--       condition is always true. Hence, the second condition is necessary.
+          IF ( dz_stretch_level >= zu(k+1)  .AND.  dz_stretch_level <= 0.0  &
+               .AND.  dz_stretched < dz_max )  THEN
+             dz_stretched = dz_stretched * dz_stretch_factor
+             dz_stretched = MIN( dz_stretched, dz_max )
+             IF ( dz_stretch_level_index == 0 ) dz_stretch_level_index = k+1
+          ENDIF
+          zu(k) = zu(k+1) - dz_stretched
+       ENDDO
+
+!
+!--    Compute the w-levels. They are always staggered half-way between the 
+!--    corresponding u-levels, except in case of dirichlet bc for u and v
+!--    at the ground. In this case the first u- and w-level are defined at 
+!--    same height. The top w-level (nzt+1) is not used but set for 
+!--    consistency, since w and all scalar variables are defined up tp nzt+1.
+       zw(nzt+1) = dz
+       zw(nzt)   = 0.0_wp
+       DO  k = 0, nzt
+          zw(k) = ( zu(k) + zu(k+1) ) * 0.5_wp
+       ENDDO
+
+!
+!--    In case of dirichlet bc for u and v the first u- and w-level are defined
+!--    at same height.
+       IF ( ibc_uv_b == 0 ) THEN
+          zu(0) = zw(0)
+       ENDIF
+
+    ENDIF
+
+!
+!-- Compute grid lengths.
+    DO  k = 1, nzt+1
+       dzu(k)  = zu(k) - zu(k-1)
+       ddzu(k) = 1.0_wp / dzu(k)
+       dzw(k)  = zw(k) - zw(k-1)
+       ddzw(k) = 1.0_wp / dzw(k)
+    ENDDO
+
+    DO  k = 1, nzt
+       dd2zu(k) = 1.0_wp / ( dzu(k) + dzu(k+1) )
+    ENDDO
+    
+!    
+!-- The FFT- SOR-pressure solvers assume grid spacings of a staggered grid
+!-- everywhere. For the actual grid, the grid spacing at the lowest level
+!-- is only dz/2, but should be dz. Therefore, an additional array
+!-- containing with appropriate grid information is created for these
+!-- solvers.
+    IF ( psolver(1:9) /= 'multigrid' )  THEN
+       ALLOCATE( ddzu_pres(1:nzt+1) )
+       ddzu_pres = ddzu
+       ddzu_pres(1) = ddzu_pres(2)  ! change for lowest level
+    ENDIF
+
+!
+!-- Compute the reciprocal values of the horizontal grid lengths.
+    ddx = 1.0_wp / dx
+    ddy = 1.0_wp / dy
+    dx2 = dx * dx
+    dy2 = dy * dy
+    ddx2 = 1.0_wp / dx2
+    ddy2 = 1.0_wp / dy2
+
+!
+!-- Allocate 3D array to set topography
+    ALLOCATE( topo(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+    topo = 0
+!
+!-- Initialize topography by generic topography or read from topography from file.  
+    CALL init_topo( topo )
+!
+!-- Set flags to mask topography on the grid. 
+    CALL set_topo_flags( topo )    
+!
+!-- Calculate wall flag arrays for the multigrid method.
+!-- Please note, wall flags are only applied in the non-optimized version.
+    IF ( psolver == 'multigrid_noopt' )  CALL poismg_noopt_init 
+
+!
+!-- Init flags for ws-scheme to degrade order of the numerics near walls, i.e. 
+!-- to decrease the numerical stencil appropriately.
+    IF ( momentum_advec == 'ws-scheme'  .OR.  scalar_advec == 'ws-scheme' )    &
+       CALL ws_init_flags
+
+!
+!-- Calculated grid-dependent as well as near-wall mixing length 
+    CALL init_mixing_length
+
+!
+!-- Determine the maximum level of topography. It is used for
+!-- steering the degradation of order of the applied advection scheme, 
+!-- as well in the lpm. 
+!-- In case of non-cyclic lateral boundaries, the order of the advection
+!-- scheme has to be reduced up to nzt (required at the lateral boundaries).
+    k_top = 0
+    DO  i = nxl, nxr
+       DO  j = nys, nyn
+          DO  k = nzb, nzt + 1
+             k_top = MAX( k_top, MERGE( k, 0,                                  &
+                                        .NOT. BTEST( topo(k,j,i), 0 ) ) )
+          ENDDO
+       ENDDO
+    ENDDO
+#if defined( __parallel )
+    CALL MPI_ALLREDUCE( k_top + 1, nzb_max, 1, MPI_INTEGER,                    & !is +1 really necessary here?
+                        MPI_MAX, comm2d, ierr )
+#else
+    nzb_max = k_top + 1
+#endif
+    IF ( inflow_l  .OR.  outflow_l  .OR.  force_bound_l  .OR.  nest_bound_l  .OR.&
+         inflow_r  .OR.  outflow_r  .OR.  force_bound_r  .OR.  nest_bound_r  .OR.&
+         inflow_n  .OR.  outflow_n  .OR.  force_bound_n  .OR.  nest_bound_n  .OR.&
+         inflow_s  .OR.  outflow_s  .OR.  force_bound_s  .OR.  nest_bound_s )    &
+         nzb_max = nzt
+!   
+!-- Finally, if topography extents up to the model top, limit nzb_max to nzt.
+    nzb_max = MIN( nzb_max, nzt ) 
+
+!
+!-- Initialize boundary conditions via surface type
+    CALL init_bc
+
+!
+!-- Allocate and set topography height arrays required for data output
+    IF ( TRIM( topography ) /= 'flat' )  THEN
+!
+!--    Allocate and set the arrays containing the topography height
+       IF ( nxr == nx  .AND.  nyn /= ny )  THEN
+          ALLOCATE( zu_s_inner(nxl:nxr+1,nys:nyn),                             &
+                    zw_w_inner(nxl:nxr+1,nys:nyn) )
+       ELSEIF ( nxr /= nx  .AND.  nyn == ny )  THEN
+          ALLOCATE( zu_s_inner(nxl:nxr,nys:nyn+1),                             &
+                    zw_w_inner(nxl:nxr,nys:nyn+1) )
+       ELSEIF ( nxr == nx  .AND.  nyn == ny )  THEN
+          ALLOCATE( zu_s_inner(nxl:nxr+1,nys:nyn+1),                           &
+                    zw_w_inner(nxl:nxr+1,nys:nyn+1) )
+       ELSE
+          ALLOCATE( zu_s_inner(nxl:nxr,nys:nyn),                               &
+                    zw_w_inner(nxl:nxr,nys:nyn) )
+       ENDIF
+
+       zu_s_inner   = 0.0_wp
+       zw_w_inner   = 0.0_wp
+!
+!--    Determine local topography height on scalar and w-grid. Note, setting
+!--    lateral boundary values is not necessary, realized via wall_flags_0 
+!--    array. Further, please note that loop bounds are different from 
+!--    nxl to nxr and nys to nyn on south and right model boundary, hence, 
+!--    use intrinsic lbound and ubound functions to infer array bounds.
+       DO  i = LBOUND(zu_s_inner, 1), UBOUND(zu_s_inner, 1)
+          DO  j = LBOUND(zu_s_inner, 2), UBOUND(zu_s_inner, 2)
+!
+!--          Topography height on scalar grid. Therefore, determine index of 
+!--          upward-facing surface element on scalar grid. 
+             zu_s_inner(i,j) = zu( get_topography_top_index( j, i, 's' ) )
+!
+!--          Topography height on w grid. Therefore, determine index of 
+!--          upward-facing surface element on w grid.
+             zw_w_inner(i,j) = zw( get_topography_top_index( j, i, 's' ) )
+          ENDDO
+       ENDDO
+    ENDIF
+
+!
+!-- In the following, calculate 2D index arrays. Note, these will be removed
+!-- soon. 
+!-- Allocate outer and inner index arrays for topography and set
+!-- defaults.                    
+    ALLOCATE( nzb_s_inner(nysg:nyng,nxlg:nxrg),                                &
+              nzb_s_outer(nysg:nyng,nxlg:nxrg),                                &
+              nzb_u_inner(nysg:nyng,nxlg:nxrg),                                &
+              nzb_u_outer(nysg:nyng,nxlg:nxrg),                                &
+              nzb_v_inner(nysg:nyng,nxlg:nxrg),                                &
+              nzb_v_outer(nysg:nyng,nxlg:nxrg),                                &
+              nzb_w_inner(nysg:nyng,nxlg:nxrg),                                &
+              nzb_w_outer(nysg:nyng,nxlg:nxrg),                                &
+              nzb_diff_s_inner(nysg:nyng,nxlg:nxrg),                           &
+              nzb_diff_s_outer(nysg:nyng,nxlg:nxrg),                           &
+              nzb_local(nysg:nyng,nxlg:nxrg),                                  &
+              nzb_tmp(nysg:nyng,nxlg:nxrg) )
+!
+!-- Initialize 2D-index arrays. Note, these will be removed soon! 
+    nzb_local(nys:nyn,nxl:nxr) = get_topography_top_index( 's' )
+    CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
+
+    IF ( TRIM( topography ) /= 'flat' )  THEN
+#if defined( __parallel )
+       CALL MPI_ALLREDUCE( MAXVAL( get_topography_top_index( 's' ) ),          &
+                           nzb_local_max, 1, MPI_INTEGER, MPI_MAX, comm2d, ierr )
+       CALL MPI_ALLREDUCE( MINVAL( get_topography_top_index( 's' ) ),          &
+                           nzb_local_min, 1, MPI_INTEGER, MPI_MIN, comm2d, ierr )                   
+#else
+       nzb_local_max = MAXVAL( get_topography_top_index( 's' ) )
+       nzb_local_min = MINVAL( get_topography_top_index( 's' ) )
+#endif
+!
+!--    Consistency checks
+       IF ( nzb_local_min < 0  .OR.  nzb_local_max  > nz + 1 )  THEN
+          WRITE( message_string, * ) 'nzb_local values are outside the',       &
+                                'model domain',                                &
+                                '&MINVAL( nzb_local ) = ', nzb_local_min,      &
+                                '&MAXVAL( nzb_local ) = ', nzb_local_max
+          CALL message( 'init_grid', 'PA0210', 1, 2, 0, 6, 0 )
+       ENDIF
+    ENDIF
+
+    nzb_s_inner = nzb;  nzb_s_outer = nzb
+    nzb_u_inner = nzb;  nzb_u_outer = nzb
+    nzb_v_inner = nzb;  nzb_v_outer = nzb
+    nzb_w_inner = nzb;  nzb_w_outer = nzb
+
+!
+!-- Define vertical gridpoint from (or to) which on the usual finite difference
+!-- form (which does not use surface fluxes) is applied 
+    IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
+       nzb_diff = nzb + 2
+    ELSE
+       nzb_diff = nzb + 1
+    ENDIF
+
+    nzb_diff_s_inner = nzb_diff;  nzb_diff_s_outer = nzb_diff
+!
+!-- Set Neumann conditions for topography. Will be removed soon.
+    IF ( .NOT. bc_ns_cyc )  THEN
+       IF ( nys == 0  )  THEN
+          nzb_local(-1,:)   = nzb_local(0,:)
+       ELSEIF ( nyn == ny )  THEN
+          nzb_local(ny+1,:) = nzb_local(ny,:)
+       ENDIF
+    ENDIF
+
+    IF ( .NOT. bc_lr_cyc )  THEN
+       IF ( nxl == 0  )  THEN
+          nzb_local(:,-1)   = nzb_local(:,0)
+          nzb_local(:,-2)   = nzb_local(:,0)
+          nzb_local(:,-3)   = nzb_local(:,0)
+       ELSEIF ( nxr == nx )  THEN
+          nzb_local(:,nx+1) = nzb_local(:,nx)
+          nzb_local(:,nx+2) = nzb_local(:,nx)          
+          nzb_local(:,nx+3) = nzb_local(:,nx)          
+       ENDIF          
+    ENDIF
+!
+!-- Initialization of 2D index arrays, will be removed soon!
+!-- Initialize nzb_s_inner and nzb_w_inner
+    nzb_s_inner = nzb_local
+    nzb_w_inner = nzb_local
+
+!
+!-- Initialize remaining index arrays:
+!-- first pre-initialize them with nzb_s_inner...
+    nzb_u_inner = nzb_s_inner
+    nzb_u_outer = nzb_s_inner
+    nzb_v_inner = nzb_s_inner
+    nzb_v_outer = nzb_s_inner
+    nzb_w_outer = nzb_s_inner
+    nzb_s_outer = nzb_s_inner
+
+!
+!-- nzb_s_outer: 
+!-- extend nzb_local east-/westwards first, then north-/southwards
+    nzb_tmp = nzb_local
+    DO  j = nys, nyn
+       DO  i = nxl, nxr
+          nzb_tmp(j,i) = MAX( nzb_local(j,i-1), nzb_local(j,i),             &
+                              nzb_local(j,i+1) )
+       ENDDO
+    ENDDO
+       
+    CALL exchange_horiz_2d_int( nzb_tmp, nys, nyn, nxl, nxr, nbgp )
+     
+    DO  i = nxl, nxr
+       DO  j = nys, nyn
+          nzb_s_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i),             &
+                                  nzb_tmp(j+1,i) )
+       ENDDO
+!
+!--    non-cyclic boundary conditions (overwritten by call of
+!--    exchange_horiz_2d_int below in case of cyclic boundary conditions)
+       IF ( nys == 0 )  THEN
+          j = -1
+          nzb_s_outer(j,i) = MAX( nzb_tmp(j+1,i), nzb_tmp(j,i) )
+       ENDIF
+       IF ( nyn == ny )  THEN
+          j = ny + 1
+          nzb_s_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i) )
+       ENDIF
+    ENDDO
+!
+!-- nzb_w_outer: 
+!-- identical to nzb_s_outer
+    nzb_w_outer = nzb_s_outer
+!
+!-- nzb_u_inner: 
+!-- extend nzb_local rightwards only
+    nzb_tmp = nzb_local
+    DO  j = nys, nyn
+       DO  i = nxl, nxr
+          nzb_tmp(j,i) = MAX( nzb_local(j,i-1), nzb_local(j,i) )
+       ENDDO
+    ENDDO
+       
+    CALL exchange_horiz_2d_int( nzb_tmp, nys, nyn, nxl, nxr, nbgp )
+       
+    nzb_u_inner = nzb_tmp
+!
+!-- nzb_u_outer: 
+!-- extend current nzb_tmp (nzb_u_inner) north-/southwards
+    DO  i = nxl, nxr
+       DO  j = nys, nyn
+          nzb_u_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i),             &
+                                  nzb_tmp(j+1,i) )
+       ENDDO
+!
+!--    non-cyclic boundary conditions (overwritten by call of
+!--    exchange_horiz_2d_int below in case of cyclic boundary conditions)
+       IF ( nys == 0 )  THEN
+          j = -1
+          nzb_u_outer(j,i) = MAX( nzb_tmp(j+1,i), nzb_tmp(j,i) )
+       ENDIF
+       IF ( nyn == ny )  THEN
+          j = ny + 1
+          nzb_u_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i) )
+       ENDIF
+    ENDDO
+
+!
+!-- nzb_v_inner:
+!-- extend nzb_local northwards only
+    nzb_tmp = nzb_local
+    DO  i = nxl, nxr
+       DO  j = nys, nyn
+          nzb_tmp(j,i) = MAX( nzb_local(j-1,i), nzb_local(j,i) )
+       ENDDO
+    ENDDO
+       
+    CALL exchange_horiz_2d_int( nzb_tmp, nys, nyn, nxl, nxr, nbgp )      
+    nzb_v_inner = nzb_tmp
+
+!
+!-- nzb_v_outer: 
+!-- extend current nzb_tmp (nzb_v_inner) right-/leftwards
+    DO  j = nys, nyn
+       DO  i = nxl, nxr
+          nzb_v_outer(j,i) = MAX( nzb_tmp(j,i-1), nzb_tmp(j,i),                &
+                                  nzb_tmp(j,i+1) )
+       ENDDO
+!
+!--    non-cyclic boundary conditions (overwritten by call of
+!--    exchange_horiz_2d_int below in case of cyclic boundary conditions)
+       IF ( nxl == 0 )  THEN
+          i = -1
+          nzb_v_outer(j,i) = MAX( nzb_tmp(j,i+1), nzb_tmp(j,i) )
+       ENDIF
+       IF ( nxr == nx )  THEN
+          i = nx + 1
+          nzb_v_outer(j,i) = MAX( nzb_tmp(j,i-1), nzb_tmp(j,i) )
+       ENDIF
+    ENDDO
+
+!
+!-- Exchange of lateral boundary values (parallel computers) and cyclic
+!-- boundary conditions, if applicable.
+!-- Since nzb_s_inner and nzb_w_inner are derived directly from nzb_local
+!-- they do not require exchange and are not included here.
+    CALL exchange_horiz_2d_int( nzb_u_inner, nys, nyn, nxl, nxr, nbgp )
+    CALL exchange_horiz_2d_int( nzb_u_outer, nys, nyn, nxl, nxr, nbgp )
+    CALL exchange_horiz_2d_int( nzb_v_inner, nys, nyn, nxl, nxr, nbgp )
+    CALL exchange_horiz_2d_int( nzb_v_outer, nys, nyn, nxl, nxr, nbgp )
+    CALL exchange_horiz_2d_int( nzb_w_outer, nys, nyn, nxl, nxr, nbgp )
+    CALL exchange_horiz_2d_int( nzb_s_outer, nys, nyn, nxl, nxr, nbgp )
+
+!
+!-- Set the individual index arrays which define the k index from which on
+!-- the usual finite difference form (which does not use surface fluxes) is
+!-- applied 
+    IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
+       nzb_diff_s_inner   = nzb_s_inner + 2
+       nzb_diff_s_outer   = nzb_s_outer + 2
+    ELSE
+       nzb_diff_s_inner   = nzb_s_inner + 1
+       nzb_diff_s_outer   = nzb_s_outer + 1
+    ENDIF
+!
+!-- Vertical nesting: communicate vertical grid level arrays between fine and
+!-- coarse grid
+    IF ( vnested )  CALL vnest_init_grid
+
+ END SUBROUTINE init_grid
+
+! Description:
+! -----------------------------------------------------------------------------!
+!> Set temporary topography flags and reference buildings on top of underlying
+!> orography. 
+!------------------------------------------------------------------------------!
+ SUBROUTINE process_topography( topo_3d )
+
+    USE arrays_3d,                                                             &
+        ONLY:  zw
+
+    USE control_parameters,                                                    &
+        ONLY:  bc_lr_cyc, bc_ns_cyc, land_surface, ocean, urban_surface
+
+    USE indices,                                                               &
+        ONLY:  nbgp, nx, nxl, nxlg, nxr, nxrg, ny, nyn, nyng, nys, nysg, nzb,  &
+               nzt
+
+    USE netcdf_data_input_mod,                                                 &
+        ONLY:  buildings_f, building_id_f, input_pids_static,                  &
+               terrain_height_f
+
+    USE kinds
+
+    USE pegrid
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  i         !< running index along x-direction
+    INTEGER(iwp) ::  j         !< running index along y-direction
+    INTEGER(iwp) ::  k         !< running index along z-direction with respect to numeric grid
+    INTEGER(iwp) ::  k2        !< running index along z-direction with respect to netcdf grid
+    INTEGER(iwp) ::  k_surf    !< orography top index, used to map 3D buildings onto terrain
+    INTEGER(iwp) ::  nr        !< index variable indication maximum terrain height for respective building ID
+    INTEGER(iwp) ::  num_build !< counter for number of buildings
+
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  displace_dum        !< displacements of start addresses, used for MPI_ALLGATHERV
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  build_ids           !< building IDs on entire model domain
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  build_ids_final     !< building IDs on entire model domain, multiple occurences are sorted out 
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  build_ids_final_tmp !< temporary array used for resizing
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  build_ids_l         !< building IDs on local subdomain
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  build_ids_l_tmp     !< temporary array used to resize array of building IDs
+
+    INTEGER(iwp), DIMENSION(0:numprocs-1) ::  num_buildings     !< number of buildings with different ID on entire model domain
+    INTEGER(iwp), DIMENSION(0:numprocs-1) ::  num_buildings_l   !< number of buildings with different ID on local subdomain
+
+    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  topo_3d !< input array for 3D topography and dummy array for setting "outer"-flags
+
+    REAL(wp)                            ::  ocean_offset        !< offset to consider inverse vertical coordinate at topography definition
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  oro_max             !< maximum terrain height occupied by an building with certain id
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  oro_max_l           !< maximum terrain height occupied by an building with certain id, on local subdomain
+
+!
+!-- In the following, buildings and orography are further preprocessed 
+!-- before they are mapped on the LES grid.
+!-- Buildings are mapped on top of the orography by maintaining the roof 
+!-- shape of the building. This can be achieved by referencing building on 
+!-- top of the maximum terrain height within the area occupied by the 
+!-- respective building. As buildings and terrain height are defined PE-wise,
+!-- parallelization of this referencing is required (a building can be 
+!-- distributed between different PEs).  
+!-- In a first step, determine the number of buildings with different 
+!-- building id on each PE. In a next step, all building ids are gathered
+!-- into one array which is present to all PEs. For each building ID, 
+!-- the maximum terrain height occupied by the respective building is 
+!-- computed and distributed to each PE.  
+!-- Finally, for each building id and its respective reference orography, 
+!-- builidings are mapped on top.   
+!-- 
+!-- First, pre-set topography flags, bit 1 indicates orography, bit 2 
+!-- buildings 
+!-- classify the respective surfaces.
+    topo_3d          = IBSET( topo_3d, 0 )
+    topo_3d(nzb,:,:) = IBCLR( topo_3d(nzb,:,:), 0 )
+!
+!-- Reference buildings on top of orography. This is not necessary
+!-- if topography is read from ASCII file as no distinction between buildings
+!-- and terrain height can be made. Moreover, this is also not necessary if
+!-- urban-surface and land-surface model are used at the same time. 
+    IF ( input_pids_static )  THEN 
+       num_buildings_l = 0
+       num_buildings   = 0
+!
+!--    Allocate at least one element for building ids, 
+       ALLOCATE( build_ids_l(1) )
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             IF ( building_id_f%var(j,i) /= building_id_f%fill )  THEN
+                IF ( num_buildings_l(myid) > 0 )  THEN
+                   IF ( ANY( building_id_f%var(j,i) .EQ.  build_ids_l ) )  THEN
+                      CYCLE
+                   ELSE
+                      num_buildings_l(myid) = num_buildings_l(myid) + 1
+!
+!--                   Resize array with different local building ids
+                      ALLOCATE( build_ids_l_tmp(1:SIZE(build_ids_l)) )
+                      build_ids_l_tmp = build_ids_l
+                      DEALLOCATE( build_ids_l )
+                      ALLOCATE( build_ids_l(1:num_buildings_l(myid)) )
+                      build_ids_l(1:num_buildings_l(myid)-1) =                 &
+                                  build_ids_l_tmp(1:num_buildings_l(myid)-1)
+                      build_ids_l(num_buildings_l(myid)) = building_id_f%var(j,i)
+                      DEALLOCATE( build_ids_l_tmp )
+                   ENDIF
+!
+!--             First occuring building id on PE 
+                ELSE 
+                   num_buildings_l(myid) = num_buildings_l(myid) + 1
+                   build_ids_l(1) = building_id_f%var(j,i)
+                ENDIF
+             ENDIF
+          ENDDO
+       ENDDO
+!
+!--    Determine number of different building ids for the entire domain 
+#if defined( __parallel ) 
+       CALL MPI_ALLREDUCE( num_buildings_l, num_buildings, numprocs,              &
+                           MPI_INTEGER, MPI_SUM, comm2d, ierr ) 
+#else
+       num_buildings = num_buildings_l
+#endif
+!
+!--    Gather all buildings ids on each PEs. 
+!--    First, allocate array encompassing all building ids in model domain.  
+       ALLOCATE( build_ids(1:SUM(num_buildings)) )
+#if defined( __parallel ) 
+!
+!--    Allocate array for displacements. 
+!--    As each PE may has a different number of buildings, so that
+!--    the block sizes send by each PE may not be equal. Hence, 
+!--    information about the respective displacement is required, indicating 
+!--    the respective adress where each MPI-task writes into the receive 
+!--    buffer array  
+       ALLOCATE( displace_dum(0:numprocs-1) )
+       displace_dum(0) = 0
+       DO i = 1, numprocs-1
+          displace_dum(i) = displace_dum(i-1) + num_buildings(i-1)
+       ENDDO
+
+       CALL MPI_ALLGATHERV( build_ids_l(1:num_buildings_l(myid)),                 &
+                            num_buildings(myid),                                  &
+                            MPI_INTEGER,                                          &
+                            build_ids,                                            &
+                            num_buildings,                                        &
+                            displace_dum,                                         & 
+                            MPI_INTEGER,                                          &
+                            comm2d, ierr )   
+
+       DEALLOCATE( displace_dum )
+
+#else
+       build_ids = build_ids_l
+#endif
+
+!
+!--    Note, in parallel mode building ids can occure mutliple times, as 
+!--    each PE has send its own ids. Therefore, sort out building ids which 
+!--    appear more than one time. 
+       num_build = 0
+       DO  nr = 1, SIZE(build_ids)
+
+          IF ( ALLOCATED(build_ids_final) )  THEN
+             IF ( ANY( build_ids(nr) .EQ. build_ids_final ) )  THEN
+                CYCLE
+             ELSE
+                num_build = num_build + 1
+!
+!--             Resize
+                ALLOCATE( build_ids_final_tmp(1:num_build) )
+                build_ids_final_tmp(1:num_build-1) = build_ids_final(1:num_build-1)
+                DEALLOCATE( build_ids_final )
+                ALLOCATE( build_ids_final(1:num_build) )
+                build_ids_final(1:num_build-1) = build_ids_final_tmp(1:num_build-1)
+                build_ids_final(num_build) = build_ids(nr)
+                DEALLOCATE( build_ids_final_tmp )
+             ENDIF             
+          ELSE
+             num_build = num_build + 1
+             ALLOCATE( build_ids_final(1:num_build) )
+             build_ids_final(num_build) = build_ids(nr)
+          ENDIF
+       ENDDO
+
+!
+!--    Finally, determine maximumum terrain height occupied by the 
+!--    respective building. First, on each PE locally. 
+       ALLOCATE( oro_max_l(1:SIZE(build_ids_final)) )
+       ALLOCATE( oro_max(1:SIZE(build_ids_final))   )
+       oro_max_l = 0.0_wp
+
+       DO  nr = 1, SIZE(build_ids_final)
+          oro_max_l(nr) = MAXVAL(                                              &
+                           MERGE( terrain_height_f%var, 0.0_wp,                &
+                                  building_id_f%var(nys:nyn,nxl:nxr) .EQ.      &
+                                  build_ids_final(nr) ) )
+       ENDDO
+   
+#if defined( __parallel )    
+       IF ( SIZE(build_ids_final) >= 1 ) THEN
+          CALL MPI_ALLREDUCE( oro_max_l, oro_max, SIZE( oro_max ), MPI_REAL,   &
+                              MPI_MAX, comm2d, ierr ) 
+       ENDIF
+#else
+       oro_max = oro_max_l
+#endif
+!
+!--    Map orography as well as buildings on grid. 
+!--    In case of ocean simulations, add an offset.  
+       ocean_offset = MERGE( zw(0), 0.0_wp, ocean )
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             DO  k = nzb, nzt
+!
+!--             In a first step, if grid point is below or equal the given 
+!--             terrain height, grid point is flagged to be of type natural. 
+!--             Please note, in case there is also a building which is lower
+!--             than the vertical grid spacing, initialization of surface
+!--             attributes will not be correct as given surface information
+!--             will not be in accordance to the classified grid points. 
+!--             Hence, in this case, de-flag the grid point and give it 
+!--             urban type instead. 
+                IF ( zw(k) - ocean_offset <= terrain_height_f%var(j,i) )  THEN
+                    topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 0 )
+                    topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 1 ) 
+                ENDIF
+!
+!--             Set building grid points. Here, only consider 2D buildings. 
+!--             3D buildings require separate treatment. 
+                IF ( buildings_f%lod == 1 )  THEN
+                   IF ( building_id_f%var(j,i) /= building_id_f%fill )  THEN
+!
+!--                   Determine index where maximum terrain height occupied by 
+!--                   the respective building height is stored.
+                      nr = MINLOC( ABS( build_ids_final -                      &
+                                        building_id_f%var(j,i) ), DIM = 1 )
+        
+                      IF ( zw(k) - ocean_offset <=                             &
+                           oro_max(nr) + buildings_f%var_2d(j,i) )  THEN
+                         topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 0 )
+                         topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 2 )
+!
+!--                      De-flag grid point of type natural. See comment above.
+                         topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 1 ) 
+                      ENDIF
+                   ENDIF
+                ENDIF
+             ENDDO
+!
+!--          Map 3D buildings onto terrain height.  
+             IF ( buildings_f%lod == 2 )  THEN
+                IF ( building_id_f%var(j,i) /= building_id_f%fill )  THEN
+!
+!--                Determine topo index occupied by terrain
+                   k_surf = MAXLOC( MERGE( 1, 0,                               &
+                                           .NOT. BTEST( topo_3d(:,j,i), 0 ) ), &
+                                    DIM = 1 ) - 1
+                   k2 = nzb+1
+                   DO k = k_surf + 1, nzt + 1
+                      IF ( k2 <= buildings_f%nz )  THEN
+                         IF ( buildings_f%var_3d(k2,j,i) == 1 )  THEN
+                            topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 0 )
+                            topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 2 )
+                         ENDIF
+                      ENDIF
+                      k2 = k2 + 1
+                   ENDDO
+                ENDIF
+             ENDIF
+          ENDDO
+       ENDDO
+!
+!--    Deallocate temporary arrays required for processing and reading data
+       IF ( ALLOCATED( oro_max         ) )  DEALLOCATE( oro_max         )
+       IF ( ALLOCATED( oro_max_l       ) )  DEALLOCATE( oro_max_l       )
+       IF ( ALLOCATED( build_ids_final ) )  DEALLOCATE( build_ids_final )
+!
+!-- Topography input via ASCII format. 
+    ELSE
+       ocean_offset     = MERGE( zw(0), 0.0_wp, ocean )
+       topo_3d          = IBSET( topo_3d, 0 )
+       topo_3d(nzb,:,:) = IBCLR( topo_3d(nzb,:,:), 0 )
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             DO  k = nzb, nzt
+                IF ( zw(k) - ocean_offset <= buildings_f%var_2d(j,i) )  THEN
+                    topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 0 )
+                    topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 2 ) !indicates terrain
+                ENDIF
+             ENDDO
+          ENDDO
+       ENDDO
+    ENDIF
+
+    CALL exchange_horiz_int( topo_3d, nys, nyn, nxl, nxr, nzt, nbgp )
+
+    IF ( .NOT. bc_ns_cyc )  THEN
+       IF ( nys == 0  )  topo_3d(:,-1,:)   = topo_3d(:,0,:)
+       IF ( nyn == ny )  topo_3d(:,ny+1,:) = topo_3d(:,ny,:)
+    ENDIF
+
+    IF ( .NOT. bc_lr_cyc )  THEN
+       IF ( nxl == 0  )  topo_3d(:,:,-1)   = topo_3d(:,:,0)
+       IF ( nxr == nx )  topo_3d(:,:,nx+1) = topo_3d(:,:,nx)          
+    ENDIF
+
+ END SUBROUTINE process_topography
+
+
+! Description:
+! -----------------------------------------------------------------------------!
+!> Filter topography, i.e. fill holes resolved by only one grid point.  
+!> Such holes are suspected to lead to velocity blow-ups as continuity 
+!> equation on discrete grid cannot be fulfilled in such case.
+!------------------------------------------------------------------------------!
+ SUBROUTINE filter_topography( topo_3d )
+
+    USE control_parameters,                                                    &
+        ONLY:  bc_lr_cyc, bc_ns_cyc, message_string
+
+    USE indices,                                                               &
+        ONLY:  nbgp, nx, nxl, nxlg, nxr, nxrg, ny, nyn, nyng, nys, nysg, nzb, nzt
+
+    USE netcdf_data_input_mod,                                                 &
+        ONLY:  building_id_f, building_type_f 
+
+    USE  pegrid
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  i          !< running index along x-direction
+    INTEGER(iwp) ::  it         !< counter for number of iterations
+    INTEGER(iwp) ::  j          !< running index along y-direction
+    INTEGER(iwp) ::  k          !< running index along z-direction
+    INTEGER(iwp) ::  num_hole   !< number of holes (in topography) resolved by only one grid point 
+    INTEGER(iwp) ::  num_hole_l !< number of holes (in topography) resolved by only one grid point on local PE     
+    INTEGER(iwp) ::  num_wall   !< number of surrounding vertical walls for a single grid point
+
+    INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE ::  topo_tmp      !< temporary 3D-topography used to fill holes
+    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  topo_3d       !< 3D-topography array merging buildings and orography
+!
+!-- Before checking for holes, set lateral boundary conditions for 
+!-- topography. After hole-filling, boundary conditions must be set again.
+!-- Several iterations are performed, in order to fill holes which might 
+!-- emerge by the filling-algorithm itself.
+    ALLOCATE( topo_tmp(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+    topo_tmp = 0
+
+    num_hole = 99999
+    it = 0
+    DO WHILE ( num_hole > 0 )       
+
+       num_hole = 0    
+       CALL exchange_horiz_int( topo_3d, nys, nyn, nxl, nxr, nzt, nbgp )
+
+       topo_tmp = topo_3d
+!
+!--    In case of non-cyclic lateral boundaries, assume lateral boundary to be 
+!--    a solid wall. Thus, intermediate spaces of one grid point between 
+!--    boundary and some topographic structure will be filled.           
+       IF ( .NOT. bc_ns_cyc )  THEN
+          IF ( nys == 0  )  topo_tmp(:,-1,:)   = IBCLR( topo_tmp(:,0,:),  0 )
+          IF ( nyn == ny )  topo_tmp(:,ny+1,:) = IBCLR( topo_tmp(:,ny,:), 0 )
+       ENDIF
+
+       IF ( .NOT. bc_lr_cyc )  THEN
+          IF ( nxl == 0  )  topo_tmp(:,:,-1)   = IBCLR( topo_tmp(:,:,0),  0 )
+          IF ( nxr == nx )  topo_tmp(:,:,nx+1) = IBCLR( topo_tmp(:,:,nx), 0 )          
+       ENDIF
+
+       it = it + 1
+       num_hole_l = 0
+       DO i = nxl, nxr
+          DO j = nys, nyn
+             DO  k = nzb+1, nzt
+                IF ( BTEST( topo_tmp(k,j,i), 0 ) )  THEN
+                   num_wall = 0
+                   IF ( .NOT. BTEST( topo_tmp(k,j-1,i), 0 ) )                  &
+                      num_wall = num_wall + 1
+                   IF ( .NOT. BTEST( topo_tmp(k,j+1,i), 0 ) )                  &
+                      num_wall = num_wall + 1
+                   IF ( .NOT. BTEST( topo_tmp(k,j,i-1), 0 ) )                  &
+                      num_wall = num_wall + 1
+                   IF ( .NOT. BTEST( topo_tmp(k,j,i+1), 0 ) )                  &
+                      num_wall = num_wall + 1
+                   IF ( .NOT. BTEST( topo_tmp(k-1,j,i), 0 ) )                  &
+                      num_wall = num_wall + 1   
+                   IF ( .NOT. BTEST( topo_tmp(k+1,j,i), 0 ) )                  &
+                      num_wall = num_wall + 1
+
+                   IF ( num_wall >= 4 )  THEN
+                      num_hole_l     = num_hole_l + 1
+!
+!--                   Clear flag 0 and set special flag ( bit 3) to indicate 
+!--                   that new topography point is a result of filtering process.
+                      topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 0 )
+                      topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 3 )
+!
+!--                   If filled grid point is occupied by a building, classify
+!--                   it as building grid point.
+                      IF ( building_type_f%from_file )  THEN
+                         IF ( building_type_f%var(j,i)   /=                    &  
+                              building_type_f%fill            .OR.             &       
+                              building_type_f%var(j+1,i) /=                    &  
+                              building_type_f%fill            .OR.             &                
+                              building_type_f%var(j-1,i) /=                    &                
+                              building_type_f%fill            .OR.             &                
+                              building_type_f%var(j,i+1) /=                    &                
+                              building_type_f%fill            .OR.             &                
+                              building_type_f%var(j,i-1) /=                    &                
+                              building_type_f%fill )  THEN
+!
+!--                         Set flag indicating building surfaces
+                            topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 2 )
+!
+!--                         Set building_type and ID at this position if not 
+!--                         already set. This is required for proper 
+!--                         initialization of urban-surface energy balance 
+!--                         solver.
+                            IF ( building_type_f%var(j,i) ==                   &
+                                 building_type_f%fill )  THEN
+
+                               IF ( building_type_f%var(j+1,i) /=              &
+                                    building_type_f%fill )  THEN
+                                  building_type_f%var(j,i) =                   &
+                                                    building_type_f%var(j+1,i)
+                                  building_id_f%var(j,i) =                     &
+                                                    building_id_f%var(j+1,i)
+                               ELSEIF ( building_type_f%var(j-1,i) /=          &
+                                        building_type_f%fill )  THEN
+                                  building_type_f%var(j,i) =                   &
+                                                    building_type_f%var(j-1,i)
+                                  building_id_f%var(j,i) =                     &
+                                                    building_id_f%var(j-1,i)
+                               ELSEIF ( building_type_f%var(j,i+1) /=          &
+                                        building_type_f%fill )  THEN
+                                  building_type_f%var(j,i) =                   &
+                                                    building_type_f%var(j,i+1)
+                                  building_id_f%var(j,i) =                     &
+                                                    building_id_f%var(j,i+1)
+                               ELSEIF ( building_type_f%var(j,i-1) /=          &
+                                        building_type_f%fill )  THEN
+                                  building_type_f%var(j,i) =                   &
+                                                    building_type_f%var(j,i-1)
+                                  building_id_f%var(j,i) =                     &
+                                                    building_id_f%var(j,i-1)
+                               ENDIF
+                            ENDIF
+                         ENDIF
+                      ENDIF
+!
+!--                   If filled grid point is already classified as building
+!--                   everything is fine, else classify this grid point as
+!--                   natural type grid point. This case, values for the 
+!--                   surface type are already set.
+                      IF ( .NOT. BTEST( topo_3d(k,j,i), 2 ) )  THEN
+                         topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 1 )
+                      ENDIF
+                   ENDIF
+                ENDIF
+             ENDDO
+          ENDDO
+       ENDDO
+!
+!--    Count the total number of holes, required for informative message.
+#if defined( __parallel )
+       CALL MPI_ALLREDUCE( num_hole_l, num_hole, 1, MPI_INTEGER, MPI_SUM,      &
+                           comm2d, ierr )
+#else
+       num_hole = num_hole_l
+#endif    
+
+!
+!--    Create an informative message if any holes were filled.
+       IF ( num_hole > 0 )  THEN
+          WRITE( message_string, * ) num_hole, 'hole(s) resolved by only '//   &
+                                     'one grid point were filled at ', it,     &
+                                     'th iteration '
+          CALL message( 'init_grid', 'PA0430', 0, 0, 0, 6, 0 )
+       ENDIF
+
+
+    ENDDO
+
+    DEALLOCATE( topo_tmp )
+!
+!-- Finally, exchange topo_3d array again and if necessary set Neumann boundary
+!-- condition in case of non-cyclic lateral boundaries. 
+    CALL exchange_horiz_int( topo_3d, nys, nyn, nxl, nxr, nzt, nbgp )
+
+    IF ( .NOT. bc_ns_cyc )  THEN
+       IF ( nys == 0  )  topo_3d(:,-1,:)   = topo_3d(:,0,:)
+       IF ( nyn == ny )  topo_3d(:,ny+1,:) = topo_3d(:,ny,:)
+    ENDIF
+
+    IF ( .NOT. bc_lr_cyc )  THEN
+       IF ( nxl == 0  )  topo_3d(:,:,-1)   = topo_3d(:,:,0)
+       IF ( nxr == nx )  topo_3d(:,:,nx+1) = topo_3d(:,:,nx)          
+    ENDIF
+
+ END SUBROUTINE filter_topography
+
+! Description:
+! -----------------------------------------------------------------------------!
+!> Pre-computation of grid-dependent and near-wall mixing length.
+!> @todo: move subroutine to turbulence module developed by M1 (Tobi).
+!------------------------------------------------------------------------------!
+ SUBROUTINE init_mixing_length
+
+    USE arrays_3d,                                                             &
+        ONLY:  dzw, l_grid, l_wall, zu, zw
+
+    USE control_parameters,                                                    &
+        ONLY:  message_string, wall_adjustment_factor
+
+    USE grid_variables,                                                        &
+        ONLY:  dx, dy
+
+    USE indices,                                                               &
+        ONLY:  nbgp, nxl, nxlg, nxr, nxrg, nyn, nyng, nys, nysg, nzb, nzt,     &
+               wall_flags_0
+    
+    USE kinds
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  i             !< index variable along x
+    INTEGER(iwp) ::  j             !< index variable along y
+    INTEGER(iwp) ::  k             !< index variable along z
+
+    ALLOCATE( l_grid(1:nzt) )
+    ALLOCATE( l_wall(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+!
+!-- Compute the grid-dependent mixing length.
+    DO  k = 1, nzt
+       l_grid(k)  = ( dx * dy * dzw(k) )**0.33333333333333_wp
+    ENDDO
+!
+!-- Initialize near-wall mixing length l_wall only in the vertical direction
+!-- for the moment, multiplication with wall_adjustment_factor further below
+    l_wall(nzb,:,:)   = l_grid(1)
+    DO  k = nzb+1, nzt
+       l_wall(k,:,:)  = l_grid(k)
+    ENDDO
+    l_wall(nzt+1,:,:) = l_grid(nzt)
+
+    DO  k = 1, nzt
+       IF ( l_grid(k) > 1.5_wp * dx * wall_adjustment_factor .OR.  &
+            l_grid(k) > 1.5_wp * dy * wall_adjustment_factor )  THEN
+          WRITE( message_string, * ) 'grid anisotropy exceeds ', &
+                                     'threshold given by only local', &
+                                     ' &horizontal reduction of near_wall ', &
+                                     'mixing length l_wall', &
+                                     ' &starting from height level k = ', k, '.'
+          CALL message( 'init_grid', 'PA0202', 0, 1, 0, 6, 0 )
+          EXIT
+       ENDIF
+    ENDDO
+!
+!-- In case of topography: limit near-wall mixing length l_wall further:
+!-- Go through all points of the subdomain one by one and look for the closest
+!-- surface.
+!-- Is this correct in the ocean case?
+    DO  i = nxl, nxr
+       DO  j = nys, nyn
+          DO  k = nzb+1, nzt
+!
+!--          Check if current gridpoint belongs to the atmosphere
+             IF ( BTEST( wall_flags_0(k,j,i), 0 ) )  THEN
+!
+!--             Check for neighbouring grid-points.
+!--             Vertical distance, down
+                IF ( .NOT. BTEST( wall_flags_0(k-1,j,i), 0 ) )                 &
+                   l_wall(k,j,i) = MIN( l_grid(k), zu(k) - zw(k-1) )
+!
+!--             Vertical distance, up
+                IF ( .NOT. BTEST( wall_flags_0(k+1,j,i), 0 ) )                 &
+                   l_wall(k,j,i) = MIN( l_grid(k), zw(k) - zu(k) )
+!
+!--             y-distance
+                IF ( .NOT. BTEST( wall_flags_0(k,j-1,i), 0 )  .OR.             &
+                     .NOT. BTEST( wall_flags_0(k,j+1,i), 0 ) )                 &
+                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k), 0.5_wp * dy )
+!
+!--             x-distance
+                IF ( .NOT. BTEST( wall_flags_0(k,j,i-1), 0 )  .OR.             &
+                     .NOT. BTEST( wall_flags_0(k,j,i+1), 0 ) )                 &
+                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k), 0.5_wp * dx )
+!
+!--              yz-distance (vertical edges, down)
+                 IF ( .NOT. BTEST( wall_flags_0(k-1,j-1,i), 0 )  .OR.          &
+                      .NOT. BTEST( wall_flags_0(k-1,j+1,i), 0 )  )             &
+                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
+                                        SQRT( 0.25_wp * dy**2 +                &
+                                       ( zu(k) - zw(k-1) )**2 ) )
+!
+!--              yz-distance (vertical edges, up)
+                 IF ( .NOT. BTEST( wall_flags_0(k+1,j-1,i), 0 )  .OR.          &
+                      .NOT. BTEST( wall_flags_0(k+1,j+1,i), 0 )  )             &
+                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
+                                        SQRT( 0.25_wp * dy**2 +                &
+                                       ( zw(k) - zu(k) )**2 ) )
+!   
+!--              xz-distance (vertical edges, down)
+                 IF ( .NOT. BTEST( wall_flags_0(k-1,j,i-1), 0 )  .OR.          &
+                      .NOT. BTEST( wall_flags_0(k-1,j,i+1), 0 )  )             &
+                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
+                                        SQRT( 0.25_wp * dx**2 +                &
+                                       ( zu(k) - zw(k-1) )**2 ) )
+!
+!--              xz-distance (vertical edges, up)
+                 IF ( .NOT. BTEST( wall_flags_0(k+1,j,i-1), 0 )  .OR.          &
+                      .NOT. BTEST( wall_flags_0(k+1,j,i+1), 0 )  )             &
+                  l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
+                                        SQRT( 0.25_wp * dx**2 +                &
+                                       ( zw(k) - zu(k) )**2 ) )
+!
+!--             xy-distance (horizontal edges)
+                IF ( .NOT. BTEST( wall_flags_0(k,j-1,i-1), 0 )  .OR.           &
+                     .NOT. BTEST( wall_flags_0(k,j+1,i-1), 0 )  .OR.           &
+                     .NOT. BTEST( wall_flags_0(k,j-1,i+1), 0 )  .OR.           &
+                     .NOT. BTEST( wall_flags_0(k,j+1,i+1), 0 ) )               &
+                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
+                                        SQRT( 0.25_wp * ( dx**2 + dy**2 ) ) )
+!
+!--             xyz distance (vertical and horizontal edges, down)
+                IF ( .NOT. BTEST( wall_flags_0(k-1,j-1,i-1), 0 )  .OR.         &
+                     .NOT. BTEST( wall_flags_0(k-1,j+1,i-1), 0 )  .OR.         &
+                     .NOT. BTEST( wall_flags_0(k-1,j-1,i+1), 0 )  .OR.         &
+                     .NOT. BTEST( wall_flags_0(k-1,j+1,i+1), 0 ) )             &
+                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
+                                        SQRT( 0.25_wp * ( dx**2 + dy**2 )      &
+                                              +  ( zu(k) - zw(k-1) )**2  ) )
+!
+!--             xyz distance (vertical and horizontal edges, up)
+                IF ( .NOT. BTEST( wall_flags_0(k+1,j-1,i-1), 0 )  .OR.         &
+                     .NOT. BTEST( wall_flags_0(k+1,j+1,i-1), 0 )  .OR.         &
+                     .NOT. BTEST( wall_flags_0(k+1,j-1,i+1), 0 )  .OR.         &
+                     .NOT. BTEST( wall_flags_0(k+1,j+1,i+1), 0 ) )             &
+                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
+                                        SQRT( 0.25_wp * ( dx**2 + dy**2 )      &
+                                              +  ( zw(k) - zu(k) )**2  ) )
+                  
+             ENDIF
+          ENDDO
+       ENDDO
+    ENDDO
+!
+!-- Set lateral boundary conditions for l_wall
+    CALL exchange_horiz( l_wall, nbgp )     
+
+ END SUBROUTINE init_mixing_length
+
+! Description:
+! -----------------------------------------------------------------------------!
+!> Reads topography information from file or sets generic topography. Moreover, 
+!> all topography-relevant topography arrays are initialized, and grid flags
+!> are set.  
+!------------------------------------------------------------------------------!
+ SUBROUTINE init_topo( topo )
+
+    USE arrays_3d,                                                             &
+        ONLY:  zw
+        
+    USE control_parameters,                                                    &
+        ONLY:  bc_lr_cyc, bc_ns_cyc, building_height, building_length_x,       &
+               building_length_y, building_wall_left, building_wall_south,     &
+               canyon_height, canyon_wall_left, canyon_wall_south,             &
+               canyon_width_x, canyon_width_y, dp_level_ind_b, dz,             &
+               message_string, ocean, topography, topography_grid_convention,  &
+               tunnel_height, tunnel_length, tunnel_width_x, tunnel_width_y,   &
+               tunnel_wall_depth
+         
+    USE grid_variables,                                                        &
+        ONLY:  dx, dy
+        
+    USE indices,                                                               &
+        ONLY:  nbgp, nx, nxl, nxlg, nxr, nxrg, ny, nyn, nyng, nys, nysg, nz,   &
+               nzb, nzt
+    
+    USE kinds
+
+    USE pegrid
+
+    USE surface_mod,                                                           &
+        ONLY:  get_topography_top_index
 
     IMPLICIT NONE
@@ -316,22 +1490,7 @@
     INTEGER(iwp) ::  cys           !< index for south canyon wall
     INTEGER(iwp) ::  i             !< index variable along x
-    INTEGER(iwp) ::  id_topo       !< NetCDF id of topograhy input file
-    INTEGER(iwp) ::  ii            !< loop variable for reading topography file
-    INTEGER(iwp) ::  inc           !< incremental parameter for coarsening grid level
+    INTEGER(iwp) ::  it            !< Number of iterations required to fill all problematic holes
     INTEGER(iwp) ::  j             !< index variable along y
     INTEGER(iwp) ::  k             !< index variable along z
-    INTEGER(iwp) ::  k_top         !< topography top index on local PE
-    INTEGER(iwp) ::  l             !< loop variable 
-    INTEGER(iwp) ::  nxl_l         !< index of left PE boundary for multigrid level
-    INTEGER(iwp) ::  nxr_l         !< index of right PE boundary for multigrid level
-    INTEGER(iwp) ::  nyn_l         !< index of north PE boundary for multigrid level
-    INTEGER(iwp) ::  nys_l         !< index of south PE boundary for multigrid level
-    INTEGER(iwp) ::  nzb_local_max !< vertical grid index of maximum topography height
-    INTEGER(iwp) ::  nzb_local_min !< vertical grid index of minimum topography height
-    INTEGER(iwp) ::  nzt_l         !< index of top PE boundary for multigrid level
-    INTEGER(iwp) ::  num_hole      !< number of holes (in topography) resolved by only one grid point 
-    INTEGER(iwp) ::  num_hole_l    !< number of holes (in topography) resolved by only one grid point on local PE     
-    INTEGER(iwp) ::  num_wall      !< number of surrounding vertical walls for a single grid point
-    INTEGER(iwp) ::  skip_n_rows   !< counting variable to skip rows while reading topography file    
     INTEGER(iwp) ::  hv_in         !< heavyside function to model inner tunnel surface 
     INTEGER(iwp) ::  hv_out        !< heavyside function to model outer tunnel surface 
@@ -346,226 +1505,9 @@
     INTEGER(iwp) ::  td            !< tunnel wall depth
     INTEGER(iwp) ::  th            !< height of outer tunnel wall
-                                      
-    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  nzb_local      !< index for topography top at cell-center
-    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  nzb_tmp        !< dummy to calculate topography indices on u- and v-grid
-
-    INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE ::  topo_3d !< input array for 3D topography and dummy array for setting "outer"-flags
-
-    LOGICAL  ::  netcdf_extend = .FALSE. !< Flag indicating wether netcdf topography input file or not
-
-    REAL(wp) ::  dum           !< dummy variable to skip columns while reading topography file    
-    REAL(wp) ::  dz_stretched  !< stretched vertical grid spacing
-
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  oro_height    !< input variable for terrain height
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  topo_height   !< input variable for topography height
-
-    INTEGER( KIND=1 ), DIMENSION(:,:,:), ALLOCATABLE ::  topo_3d_read !< input variable for 3D topography
-
-!
-!-- Calculation of horizontal array bounds including ghost layers
-    nxlg = nxl - nbgp
-    nxrg = nxr + nbgp
-    nysg = nys - nbgp
-    nyng = nyn + nbgp
-
-!
-!-- Allocate grid arrays
-    ALLOCATE( ddzu(1:nzt+1), ddzw(1:nzt+1), dd2zu(1:nzt), dzu(1:nzt+1),        &
-              dzw(1:nzt+1), l_grid(1:nzt), zu(nzb:nzt+1), zw(nzb:nzt+1) )
-
-!
-!-- Compute height of u-levels from constant grid length and dz stretch factors
-    IF ( dz == -1.0_wp )  THEN
-       message_string = 'missing dz'
-       CALL message( 'init_grid', 'PA0200', 1, 2, 0, 6, 0 ) 
-    ELSEIF ( dz <= 0.0_wp )  THEN
-       WRITE( message_string, * ) 'dz=',dz,' <= 0.0'
-       CALL message( 'init_grid', 'PA0201', 1, 2, 0, 6, 0 )
-    ENDIF
-
-!
-!-- Define the vertical grid levels
-    IF ( .NOT. ocean )  THEN
-!
-!--    Grid for atmosphere with surface at z=0 (k=0, w-grid).
-!--    The second u-level (k=1) corresponds to the top of the 
-!--    Prandtl-layer.
-
-       IF ( ibc_uv_b == 0 .OR. ibc_uv_b == 2 ) THEN
-          zu(0) = 0.0_wp
-      !    zu(0) = - dz * 0.5_wp
-       ELSE
-          zu(0) = - dz * 0.5_wp
-       ENDIF
-       zu(1) =   dz * 0.5_wp
-
-       dz_stretch_level_index = nzt+1
-       dz_stretched = dz
-       DO  k = 2, nzt+1
-          IF ( dz_stretch_level <= zu(k-1)  .AND.  dz_stretched < dz_max )  THEN
-             dz_stretched = dz_stretched * dz_stretch_factor
-             dz_stretched = MIN( dz_stretched, dz_max )
-             IF ( dz_stretch_level_index == nzt+1 ) dz_stretch_level_index = k-1
-          ENDIF
-          zu(k) = zu(k-1) + dz_stretched
-       ENDDO
-
-!
-!--    Compute the w-levels. They are always staggered half-way between the 
-!--    corresponding u-levels. In case of dirichlet bc for u and v at the 
-!--    ground the first u- and w-level (k=0) are defined at same height (z=0). 
-!--    The top w-level is extrapolated linearly.
-       zw(0) = 0.0_wp
-       DO  k = 1, nzt
-          zw(k) = ( zu(k) + zu(k+1) ) * 0.5_wp
-       ENDDO
-       zw(nzt+1) = zw(nzt) + 2.0_wp * ( zu(nzt+1) - zw(nzt) )
-
-    ELSE
-!
-!--    Grid for ocean with free water surface is at k=nzt (w-grid).
-!--    In case of neumann bc at the ground the first first u-level (k=0) lies
-!--    below the first w-level (k=0). In case of dirichlet bc the first u- and
-!--    w-level are defined at same height, but staggered from the second level.
-!--    The second u-level (k=1) corresponds to the top of the Prandtl-layer.
-       zu(nzt+1) =   dz * 0.5_wp
-       zu(nzt)   = - dz * 0.5_wp
-
-       dz_stretch_level_index = 0
-       dz_stretched = dz
-       DO  k = nzt-1, 0, -1
-!
-!--       The default value of dz_stretch_level is positive, thus the first 
-!--       condition is always true. Hence, the second condition is necessary.
-          IF ( dz_stretch_level >= zu(k+1)  .AND.  dz_stretch_level <= 0.0  &
-               .AND.  dz_stretched < dz_max )  THEN
-             dz_stretched = dz_stretched * dz_stretch_factor
-             dz_stretched = MIN( dz_stretched, dz_max )
-             IF ( dz_stretch_level_index == 0 ) dz_stretch_level_index = k+1
-          ENDIF
-          zu(k) = zu(k+1) - dz_stretched
-       ENDDO
-
-!
-!--    Compute the w-levels. They are always staggered half-way between the 
-!--    corresponding u-levels, except in case of dirichlet bc for u and v
-!--    at the ground. In this case the first u- and w-level are defined at 
-!--    same height. The top w-level (nzt+1) is not used but set for 
-!--    consistency, since w and all scalar variables are defined up tp nzt+1.
-       zw(nzt+1) = dz
-       zw(nzt)   = 0.0_wp
-       DO  k = 0, nzt
-          zw(k) = ( zu(k) + zu(k+1) ) * 0.5_wp
-       ENDDO
-
-!
-!--    In case of dirichlet bc for u and v the first u- and w-level are defined
-!--    at same height.
-       IF ( ibc_uv_b == 0 ) THEN
-          zu(0) = zw(0)
-       ENDIF
-
-    ENDIF
-
-!
-!-- Compute grid lengths.
-    DO  k = 1, nzt+1
-       dzu(k)  = zu(k) - zu(k-1)
-       ddzu(k) = 1.0_wp / dzu(k)
-       dzw(k)  = zw(k) - zw(k-1)
-       ddzw(k) = 1.0_wp / dzw(k)
-    ENDDO
-
-    DO  k = 1, nzt
-       dd2zu(k) = 1.0_wp / ( dzu(k) + dzu(k+1) )
-    ENDDO
-    
-!    
-!-- The FFT- SOR-pressure solvers assume grid spacings of a staggered grid
-!-- everywhere. For the actual grid, the grid spacing at the lowest level
-!-- is only dz/2, but should be dz. Therefore, an additional array
-!-- containing with appropriate grid information is created for these
-!-- solvers.
-    IF ( psolver(1:9) /= 'multigrid' )  THEN
-       ALLOCATE( ddzu_pres(1:nzt+1) )
-       ddzu_pres = ddzu
-       ddzu_pres(1) = ddzu_pres(2)  ! change for lowest level
-    ENDIF
-
-!
-!-- Compute the reciprocal values of the horizontal grid lengths.
-    ddx = 1.0_wp / dx
-    ddy = 1.0_wp / dy
-    dx2 = dx * dx
-    dy2 = dy * dy
-    ddx2 = 1.0_wp / dx2
-    ddy2 = 1.0_wp / dy2
-
-!
-!-- Compute the grid-dependent mixing length.
-    DO  k = 1, nzt
-       l_grid(k)  = ( dx * dy * dzw(k) )**0.33333333333333_wp
-    ENDDO
-
-!
-!-- Allocate outer and inner index arrays for topography and set
-!-- defaults.                    
-    ALLOCATE( nzb_s_inner(nysg:nyng,nxlg:nxrg),                             &
-              nzb_s_outer(nysg:nyng,nxlg:nxrg),                             &
-              nzb_u_inner(nysg:nyng,nxlg:nxrg),                             &
-              nzb_u_outer(nysg:nyng,nxlg:nxrg),                             &
-              nzb_v_inner(nysg:nyng,nxlg:nxrg),                             &
-              nzb_v_outer(nysg:nyng,nxlg:nxrg),                             &
-              nzb_w_inner(nysg:nyng,nxlg:nxrg),                             &
-              nzb_w_outer(nysg:nyng,nxlg:nxrg),                             &
-              nzb_diff_s_inner(nysg:nyng,nxlg:nxrg),                        &
-              nzb_diff_s_outer(nysg:nyng,nxlg:nxrg),                        &
-              nzb_local(nysg:nyng,nxlg:nxrg),                               &
-              nzb_tmp(nysg:nyng,nxlg:nxrg),                                 &
-              wall_flags_0(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
-
-    ALLOCATE( topo_3d(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
-    topo_3d    = 0
-
-    ALLOCATE( l_wall(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
-
-    nzb_s_inner = nzb;  nzb_s_outer = nzb
-    nzb_u_inner = nzb;  nzb_u_outer = nzb
-    nzb_v_inner = nzb;  nzb_v_outer = nzb
-    nzb_w_inner = nzb;  nzb_w_outer = nzb
-
-!
-!-- Define vertical gridpoint from (or to) which on the usual finite difference
-!-- form (which does not use surface fluxes) is applied 
-    IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
-       nzb_diff = nzb + 2
-    ELSE
-       nzb_diff = nzb + 1
-    ENDIF
-
-    nzb_diff_s_inner = nzb_diff;  nzb_diff_s_outer = nzb_diff
-
-!
-!-- Initialize near-wall mixing length l_wall only in the vertical direction
-!-- for the moment,
-!-- multiplication with wall_adjustment_factor near the end of this routine
-    l_wall(nzb,:,:)   = l_grid(1)
-    DO  k = nzb+1, nzt
-       l_wall(k,:,:)  = l_grid(k)
-    ENDDO
-    l_wall(nzt+1,:,:) = l_grid(nzt)
-
-    DO  k = 1, nzt
-       IF ( l_grid(k) > 1.5_wp * dx * wall_adjustment_factor .OR.  &
-            l_grid(k) > 1.5_wp * dy * wall_adjustment_factor )  THEN
-          WRITE( message_string, * ) 'grid anisotropy exceeds ', &
-                                     'threshold given by only local', &
-                                     ' &horizontal reduction of near_wall ', &
-                                     'mixing length l_wall', &
-                                     ' &starting from height level k = ', k, '.'
-          CALL message( 'init_grid', 'PA0202', 0, 1, 0, 6, 0 )
-          EXIT
-       ENDIF
-    ENDDO
+
+    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  nzb_local         !< index for topography top at cell-center
+    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  topo !< input array for 3D topography and dummy array for setting "outer"-flags
+
+
 !
 !-- Set outer and inner index arrays for non-flat topography.
@@ -578,13 +1520,7 @@
 
        CASE ( 'flat' )
-!
-!--       nzb_local is required for the multigrid solver
-          nzb_local = 0
-!
+!   
 !--       Initialilize 3D topography array, used later for initializing flags
-          topo_3d(nzb+1:nzt+1,:,:) = IBSET( topo_3d(nzb+1:nzt+1,:,:), 0 ) 
-!
-!--       level of detail is required for output routines
-          lod       = 1
+          topo(nzb+1:nzt+1,:,:) = IBSET( topo(nzb+1:nzt+1,:,:), 0 ) 
 
        CASE ( 'single_building' )
@@ -597,5 +1533,4 @@
           IF ( ABS( zw(bh)   - building_height ) == &
                ABS( zw(bh+1) - building_height )    )  bh = bh + 1
-
           IF ( building_wall_left == 9999999.9_wp )  THEN
              building_wall_left = ( nx + 1 - blx ) / 2 * dx
@@ -605,5 +1540,5 @@
 
           IF ( building_wall_south == 9999999.9_wp )  THEN
-             building_wall_south = ( ny + 1 - bly ) / 2 * dy
+              building_wall_south = ( ny + 1 - bly ) / 2 * dy
           ENDIF
           bys = NINT( building_wall_south / dy )
@@ -612,5 +1547,5 @@
 !
 !--       Building size has to meet some requirements
-          IF ( ( bxl < 1 ) .OR. ( bxr > nx-1 ) .OR. ( bxr < bxl+3 ) .OR.  &
+          IF ( ( bxl < 1 ) .OR. ( bxr > nx-1 ) .OR. ( bxr < bxl+3 ) .OR.       &
                ( bys < 1 ) .OR. ( byn > ny-1 ) .OR. ( byn < bys+3 ) )  THEN
              WRITE( message_string, * ) 'inconsistent building parameters:',   &
@@ -620,26 +1555,22 @@
           ENDIF
 
+          ALLOCATE( nzb_local(nysg:nyng,nxlg:nxrg) )
 !
 !--       Define the building. 
-          nzb_local = 0
           IF ( bxl <= nxr  .AND.  bxr >= nxl  .AND.                            &
-               bys <= nyn  .AND.  byn >= nys )                                 &        
+               bys <= nyn  .AND.  byn >= nys )                                 & 
              nzb_local(MAX(nys,bys):MIN(nyn,byn),MAX(nxl,bxl):MIN(nxr,bxr)) = bh
-
-          CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
-!
-!--       Set bit array to mask topography
-          DO  i = nxlg, nxrg
-             DO  j = nysg, nyng
-
-                topo_3d(nzb_local(j,i)+1:nzt+1,j,i) =                          &
-                                 IBSET( topo_3d(nzb_local(j,i)+1:nzt+1,j,i), 0 ) 
+!
+!--       Set bit array on basis of nzb_local
+          DO  i = nxl, nxr
+             DO  j = nys, nyn
+                topo(nzb_local(j,i)+1:nzt+1,j,i) =                             &
+                                 IBSET( topo(nzb_local(j,i)+1:nzt+1,j,i), 0 ) 
              ENDDO
           ENDDO
-!
-!--       level of detail is required for output routines. Here, 2D topography. 
-          lod = 1
-
-          CALL exchange_horiz_int( topo_3d, nbgp )
+        
+          DEALLOCATE( nzb_local )
+
+          CALL exchange_horiz_int( topo, nys, nyn, nxl, nxr, nzt, nbgp )
 
        CASE ( 'single_street_canyon' )
@@ -656,5 +1587,4 @@
              cxl = NINT( canyon_wall_left / dx )
              cxr = cxl + cwx
-
           ELSEIF ( canyon_width_y /= 9999999.9_wp )  THEN
 !
@@ -666,5 +1596,5 @@
              cys = NINT( canyon_wall_south / dy )
              cyn = cys + cwy
-
+      
           ELSE
              
@@ -677,5 +1607,4 @@
           IF ( ABS( zw(ch)   - canyon_height ) == &
                ABS( zw(ch+1) - canyon_height )    )  ch = ch + 1
-
           dp_level_ind_b = ch
 !
@@ -683,5 +1612,5 @@
           IF ( canyon_width_x /= 9999999.9_wp )  THEN
              IF ( ( cxl < 1 ) .OR. ( cxr > nx-1 ) .OR. ( cwx < 3 ) .OR.        &
-               ( ch < 3 ) )  THEN
+                  ( ch < 3 ) )  THEN
                 WRITE( message_string, * ) 'inconsistent canyon parameters:',  &
                                            '&cxl=', cxl, 'cxr=', cxr,          &
@@ -692,5 +1621,5 @@
           ELSEIF ( canyon_width_y /= 9999999.9_wp )  THEN
              IF ( ( cys < 1 ) .OR. ( cyn > ny-1 ) .OR. ( cwy < 3 ) .OR.        &
-               ( ch < 3 ) )  THEN
+                  ( ch < 3 ) )  THEN
                 WRITE( message_string, * ) 'inconsistent canyon parameters:',  &
                                            '&cys=', cys, 'cyn=', cyn,          &
@@ -708,4 +1637,5 @@
           ENDIF
 
+          ALLOCATE( nzb_local(nysg:nyng,nxlg:nxrg) )
           nzb_local = ch
           IF ( canyon_width_x /= 9999999.9_wp )  THEN
@@ -716,19 +1646,15 @@
                 nzb_local(MAX(nys,cys+1):MIN(nyn,cyn-1),:) = 0
           ENDIF
-
-          CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
-!
-!--       Set bit array to mask topography
-          DO  i = nxlg, nxrg
-             DO  j = nysg, nyng
-                topo_3d(nzb_local(j,i)+1:nzt+1,j,i) =                          &
-                                 IBSET( topo_3d(nzb_local(j,i)+1:nzt+1,j,i), 0 ) 
+!
+!--       Set bit array on basis of nzb_local
+          DO  i = nxl, nxr
+             DO  j = nys, nyn
+                topo(nzb_local(j,i)+1:nzt+1,j,i) =                             &
+                                 IBSET( topo(nzb_local(j,i)+1:nzt+1,j,i), 0 ) 
              ENDDO
           ENDDO
-!
-!--       level of detail is required for output routines. Here, 2D topography. 
-          lod = 1
-
-          CALL exchange_horiz_int( topo_3d, nbgp )
+          DEALLOCATE( nzb_local )
+
+          CALL exchange_horiz_int( topo, nys, nyn, nxl, nxr, nzt, nbgp )
 
        CASE ( 'tunnel' )
@@ -743,5 +1669,5 @@
 !
 !--       Tunnel-wall depth
-          IF ( tunnel_wall_depth == 9999999.9_wp )  THEN
+          IF ( tunnel_wall_depth == 9999999.9_wp )  THEN  
              td = MAX ( dx, dy, dz )
           ELSE
@@ -775,5 +1701,5 @@
                                       ( tunnel_width_x * 0.5_wp - td ) )
              txe_in  = INT( ( nx + 1 ) * 0.5_wp * dx +                         &
-                                      ( tunnel_width_x * 0.5_wp - td ) )
+                                   ( tunnel_width_x * 0.5_wp - td ) )
 
              tys_out = INT( ( ny + 1 ) * 0.5_wp * dy - tunnel_length * 0.5_wp )
@@ -782,11 +1708,13 @@
              tye_in  = tye_out
           ENDIF
-          IF ( tunnel_width_x /= 9999999.9_wp  .AND.                           &
-               tunnel_width_x - 2.0_wp * tunnel_wall_depth <= 2.0_wp * dx )  THEN
+          IF ( tunnel_width_x /= 9999999.9_wp  .AND.                           &   
+               tunnel_width_x - 2.0_wp * td <= 2.0_wp * dx )                   &
+          THEN
              message_string = 'Tunnel width too small'
              CALL message( 'init_grid', 'PA0175', 1, 2, 0, 6, 0 )
           ENDIF
           IF ( tunnel_width_y /= 9999999.9_wp  .AND.                           &
-               tunnel_width_y - 2.0_wp * tunnel_wall_depth <= 2.0_wp * dy )  THEN
+               tunnel_width_y - 2.0_wp * td <= 2.0_wp * dy )                   &
+          THEN
              message_string = 'Tunnel width too small'
              CALL message( 'init_grid', 'PA0455', 1, 2, 0, 6, 0 )
@@ -808,10 +1736,10 @@
              tye_out = INT( ( ny + 1 ) * 0.5_wp * dy + tunnel_width_y * 0.5_wp )
              tys_in  = INT( ( ny + 1 ) * 0.5_wp * dy -                         &
-                                     ( tunnel_width_y * 0.5_wp - td ) )
+                                        ( tunnel_width_y * 0.5_wp - td ) )
              tye_in  = INT( ( ny + 1 ) * 0.5_wp * dy +                         &
                                      ( tunnel_width_y * 0.5_wp - td ) )
           ENDIF
 
-          topo_3d = 0
+          topo = 0
           DO  i = nxl, nxr
              DO  j = nys, nyn
@@ -825,5 +1753,5 @@
                             ( ( SIGN( 1.0_wp, j * dy - tys_out ) + 1.0_wp )    &
                             - ( SIGN( 1.0_wp, j * dy - tye_out ) + 1.0_wp ) )
-! 
+!    
 !--             Use heaviside function to model inner tunnel surface
                 hv_in  = ( th - td ) * 0.5_wp *                                &
@@ -837,5 +1765,5 @@
 !--             Set flags at x-y-positions without any tunnel surface
                 IF ( hv_out - hv_in == 0.0_wp )  THEN
-                   topo_3d(nzb+1:nzt+1,j,i) = IBSET( topo_3d(nzb+1:nzt+1,j,i), 0 )
+                   topo(nzb+1:nzt+1,j,i) = IBSET( topo(nzb+1:nzt+1,j,i), 0 )
 !
 !--             Set flags at x-y-positions with tunnel surfaces
@@ -846,7 +1774,7 @@
                       IF ( hv_out - hv_in == th )  THEN
                          IF ( zw(k) <= hv_out )  THEN
-                            topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 0 )
+                            topo(k,j,i) = IBCLR( topo(k,j,i), 0 )
                          ELSE
-                            topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 0 )
+                            topo(k,j,i) = IBSET( topo(k,j,i), 0 )
                          ENDIF
                       ENDIF
@@ -855,9 +1783,9 @@
                       IF ( hv_out - hv_in == td )  THEN
                          IF ( zw(k) <= hv_in )  THEN 
-                            topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 0 )
+                            topo(k,j,i) = IBSET( topo(k,j,i), 0 )
                          ELSEIF ( zw(k) > hv_in  .AND.  zw(k) <= hv_out )  THEN 
-                            topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 0 )
+                            topo(k,j,i) = IBCLR( topo(k,j,i), 0 )
                          ELSEIF ( zw(k) > hv_out )  THEN 
-                            topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 0 )
+                            topo(k,j,i) = IBSET( topo(k,j,i), 0 )
                          ENDIF
                       ENDIF
@@ -867,189 +1795,34 @@
           ENDDO
 
-          nzb_local = 0
-!
-!--       level of detail is required for output routines. Here, 3D topography. 
-          lod = 2
-
-          CALL exchange_horiz_int( topo_3d, nbgp )
+          CALL exchange_horiz_int( topo, nys, nyn, nxl, nxr, nzt, nbgp )
 
        CASE ( 'read_from_file' )
-
-          ALLOCATE ( oro_height(nys:nyn,nxl:nxr)  )
-          ALLOCATE ( topo_height(nys:nyn,nxl:nxr) )
-          oro_height  = 0.0_wp
-          topo_height = 0.0_wp
-
-          DO  ii = 0, io_blocks-1
-             IF ( ii == io_group )  THEN
-
-!
-!--             Arbitrary irregular topography data in PALM format (exactly
-!--             matching the grid size and total domain size). 
-!--             First, check if NetCDF file for topography exist or not.
-!--             This case, read topography from NetCDF, else read it from 
-!--             ASCII file.
-#if defined ( __netcdf )
-                INQUIRE( FILE='TOPOGRAPHY_DATA_NC'//TRIM( coupling_char ),     &
-                         EXIST=netcdf_extend )
-!
-!--             NetCDF branch    
-                IF ( netcdf_extend )  THEN
-!
-!--                Open file in read-only mode
-                   CALL netcdf_open_read_file( 'TOPOGRAPHY_DATA_NC',           &
-                                               id_topo, 20 )  !Error number still need to be set properly 
-
-!
-!--                Read terrain height. Reading is done PE-wise, i.e. each
-!--                processor reads its own domain. Reading is realized
-!--                via looping over x-dimension, i.e. calling 
-!--                netcdf_get_variable reads topography along y for given x. 
-!--                Orography is 2D. 
-                   DO  i = nxl, nxr
-                      CALL netcdf_get_variable( id_topo, 'orography_0',        &
-                                                i, oro_height(:,i), 20 )  !Error number still need to be set properly 
-                   ENDDO
-!
-!--                Read attribute lod (level of detail), required for variable 
-!--                buildings_0 
-                   CALL netcdf_get_attribute( id_topo, "lod", lod, .FALSE.,    &
-                                              20, 'buildings_0' ) !Error number still need to be set properly 
-!
-!--                Read building height
-!--                2D for lod = 1, 3D for lod = 2
-                   IF ( lod == 1 )  THEN
-                      DO  i = nxl, nxr
-                         CALL netcdf_get_variable( id_topo, 'buildings_0',     &
-                                                   i, topo_height(:,i), 20 )  !Error number still need to be set properly 
-                      ENDDO
-
-                   ELSEIF ( lod == 2 )  THEN
-!
-!--                   Allocate 1-byte integer dummy array to read 3D topography
-                      ALLOCATE( topo_3d_read(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
-!
-!--                   Read data PE-wise. Read yz-slices.
-                      DO  i = nxl, nxr
-                         DO  j = nys, nyn
-                            CALL netcdf_get_variable( id_topo, 'buildings_0',  &
-                                                      i, j, topo_3d_read(:,j,i), 20 )  !Error number still need to be set properly 
-                         ENDDO
-                      ENDDO
-                   ELSE 
-                      message_string = 'NetCDF attribute lod ' //              &
-                                       '(level of detail) is not set properly.'
-                      CALL message( 'init_grid', 'PA0457', 1, 2, 0, 6, 0 )
-                   ENDIF
-!
-!--                On file, 3D topography grid points is classified with 1, 
-!--                atmosphere is classified with 0, contrary to the internal 
-!--                treatment. Hence, conversion is required. Moreover, set 
-!--                topography array to zero at lowest grid level. 
-                   topo_3d = MERGE( 0, 1, topo_3d_read == 1 )
-                   topo_3d(nzb,:,:) = 0 
-!
-!--                Deallocate dummy array
-                   DEALLOCATE( topo_3d_read )
-!
-!--                Close topography input file
-                   CALL netcdf_close_file( id_topo, 20 )
-#endif
-
-!
-!--             ASCII branch. Please note, reading of 3D topography is not
-!--             supported in ASCII format. Further, no distinction is made 
-!--             between orography and buildings
-                ELSE
-
-                   OPEN( 90, FILE='TOPOGRAPHY_DATA'//TRIM( coupling_char ),    &
-                         STATUS='OLD', FORM='FORMATTED', ERR=10 )
-!
-!--                Read topography PE-wise. Rows are read from nyn to nys, columns
-!--                are read from nxl to nxr. At first, ny-nyn rows need to be skipped.
-                   skip_n_rows = 0
-                   DO WHILE ( skip_n_rows < ny - nyn )
-                      READ( 90, * )  
-                      skip_n_rows = skip_n_rows + 1
-                   ENDDO
-!
-!--                Read data from nyn to nys and nxl to nxr. Therefore, skip 
-!--                column until nxl-1 is reached
-                   DO  j = nyn, nys, -1
-                      READ( 90, *, ERR=11, END=11 )                            &
-                                              ( dum, i = 0, nxl-1 ),           &
-                                              ( topo_height(j,i), i = nxl, nxr )
-                   ENDDO
-
-                   GOTO 12
-          
- 10                message_string = 'file TOPOGRAPHY'//TRIM( coupling_char )// &
-                                    ' does not exist'
-                   CALL message( 'init_grid', 'PA0208', 1, 2, 0, 6, 0 )
-
- 11                message_string = 'errors in file TOPOGRAPHY_DATA'//         &
-                                    TRIM( coupling_char )
-                   CALL message( 'init_grid', 'PA0209', 1, 2, 0, 6, 0 )
-
- 12                CLOSE( 90 )
-
-                ENDIF
-
-             ENDIF
-#if defined( __parallel )
-             CALL MPI_BARRIER( comm2d, ierr )
-#endif
-          ENDDO
-
-!
-!--       Calculate the index height of the topography
-          nzb_local = 0
-          DO  i = nxl, nxr
-             DO  j = nys, nyn
-                IF ( .NOT. ocean )  THEN
-                   nzb_local(j,i) = MINLOC( ABS( zw - topo_height(j,i)         &
-                                                    - oro_height(j,i) ), 1 ) - 1
-                   IF ( ABS( zw(nzb_local(j,i)  ) - topo_height(j,i)           &
-                                                  - oro_height(j,i)  ) ==      &
-                        ABS( zw(nzb_local(j,i)+1) - topo_height(j,i)           &
-                                                  - oro_height(j,i)  )    )    &
-                      nzb_local(j,i) = nzb_local(j,i) + 1
-                ELSE
-                   nzb_local(j,i) = MINLOC( ABS( zw - zw(0)                    &
-                                                    - topo_height(j,i)         &
-                                                    - oro_height(j,i) ), 1 ) - 1
-                   IF ( ABS( zw(nzb_local(j,i)  ) - zw(0)                      &
-                                                  - topo_height(j,i)           &
-                                                  - oro_height(j,i)  )  ==     &
-                        ABS( zw(nzb_local(j,i)+1) - zw(0)                      &
-                                                  - topo_height(j,i)           &
-                                                  - oro_height(j,i)  )    )    &
-                      nzb_local(j,i) = nzb_local(j,i) + 1
-                ENDIF
-
-             ENDDO
-          ENDDO
-
-          DEALLOCATE ( oro_height  )
-          DEALLOCATE ( topo_height )
-!
-!--       Filter topography, i.e. fill holes resolved by only one grid point.  
-!--       Such holes are suspected to lead to velocity blow-ups as continuity 
-!--       equation on discrete grid cannot be fulfilled in such case.
-!--       For now, check only for holes and fill them to the lowest height level
-!--       of the directly adjoining grid points along x- and y- direction. 
-!--       Before checking for holes, set lateral boundary conditions for 
-!--       topography. After hole-filling, boundary conditions must be set again!
-          CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
-          
+!
+!--       Note, topography information have been already read.  
+!--       If required, further process topography, i.e. reference buildings on
+!--       top of orography and set temporary 3D topography array, which is 
+!--       used later to set grid flags. Calling of this rouinte is also 
+!--       required in case of ASCII input, even though no distinction between 
+!--       terrain- and building height is made in this case.  
+          CALL process_topography( topo )
+!
+!--       Filter holes resolved by only one grid-point
+          CALL filter_topography( topo )
+!
+!--       Exchange ghost-points, as well as add cyclic or Neumann boundary 
+!--       conditions.
+          CALL exchange_horiz_int( topo, nys, nyn, nxl, nxr, nzt, nbgp )
+!
+!--       Set lateral boundary conditions for topography on all ghost layers          
           IF ( .NOT. bc_ns_cyc )  THEN
              IF ( nys == 0  )  THEN
-                nzb_local(-1,:)   = nzb_local(0,:)
-                nzb_local(-2,:)   = nzb_local(0,:)
-                nzb_local(-3,:)   = nzb_local(0,:)
-             ELSEIF ( nyn == ny )  THEN
-                nzb_local(ny+1,:) = nzb_local(ny,:)
-                nzb_local(ny+2,:) = nzb_local(ny,:)
-                nzb_local(ny+3,:) = nzb_local(ny,:)
+                DO  i = 1, nbgp         
+                   topo(:,nys-i,:) = topo(:,nys,:)
+                ENDDO
+             ENDIF
+             IF ( nyn == ny )  THEN
+                DO  i = 1, nbgp         
+                   topo(:,nyn+i,:) = topo(:,nyn,:)
+                ENDDO
              ENDIF
           ENDIF
@@ -1057,118 +1830,30 @@
           IF ( .NOT. bc_lr_cyc )  THEN
              IF ( nxl == 0  )  THEN
-                nzb_local(:,-1)   = nzb_local(:,0)
-                nzb_local(:,-2)   = nzb_local(:,0)
-                nzb_local(:,-3)   = nzb_local(:,0)
-             ELSEIF ( nxr == nx )  THEN
-                nzb_local(:,nx+1) = nzb_local(:,nx)
-                nzb_local(:,nx+2) = nzb_local(:,nx)          
-                nzb_local(:,nx+3) = nzb_local(:,nx)          
-             ENDIF          
+                DO  i = 1, nbgp 
+                   topo(:,:,nxl-i) = topo(:,:,nxl)
+                ENDDO
+             ENDIF
+             IF ( nxr == nx )  THEN
+                DO  i = 1, nbgp 
+                   topo(:,:,nxr+i) = topo(:,:,nxr)
+                ENDDO
+             ENDIF
           ENDIF
 
-          num_hole_l = 0
-          DO i = nxl, nxr
-             DO j = nys, nyn
-
-                num_wall = 0
-
-                IF ( nzb_local(j-1,i) > nzb_local(j,i) )                       &
-                   num_wall = num_wall + 1
-                IF ( nzb_local(j+1,i) > nzb_local(j,i) )                       &
-                   num_wall = num_wall + 1
-                IF ( nzb_local(j,i-1) > nzb_local(j,i) )                       &
-                   num_wall = num_wall + 1
-                IF ( nzb_local(j,i+1) > nzb_local(j,i) )                       &
-                   num_wall = num_wall + 1
-
-                IF ( num_wall == 4 )  THEN
-                   nzb_local(j,i) = MIN( nzb_local(j-1,i), nzb_local(j+1,i),   &
-                                         nzb_local(j,i-1), nzb_local(j,i+1) )
-                   num_hole_l     = num_hole_l + 1
-                ENDIF
-             ENDDO
-          ENDDO
-!
-!--       Count the total number of holes, required for informative message.
-#if defined( __parallel )
-          CALL MPI_ALLREDUCE( num_hole_l, num_hole, 1, MPI_INTEGER, MPI_SUM,   &
-                              comm2d, ierr )
-#else
-          num_hole = num_hole_l
-#endif    
-!
-!--       Create an informative message if any hole was removed.
-          IF ( num_hole > 0 )  THEN
-             WRITE( message_string, * ) num_hole, 'hole(s) resolved by only '//&
-                                                  'one grid point were filled'
-             CALL message( 'init_grid', 'PA0430', 0, 0, 0, 6, 0 )
-          ENDIF
-
-!
-!--       Set bit array to mask topography. Only required for lod = 1
-          IF ( lod == 1 )  THEN
-             DO  i = nxlg, nxrg
-                DO  j = nysg, nyng
-                   topo_3d(nzb_local(j,i)+1:nzt+1,j,i) =                          &
-                                 IBSET( topo_3d(nzb_local(j,i)+1:nzt+1,j,i), 0 ) 
-                ENDDO
-             ENDDO
-          ENDIF
-!
-!--       Exchange ghost-points, as well as add cyclic or Neumann boundary 
-!--       conditions.
-          CALL exchange_horiz_int( topo_3d, nbgp )
-          CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
-          
-          IF ( .NOT. bc_ns_cyc )  THEN
-             IF ( nys == 0  )  topo_3d(:,-1,:)   = topo_3d(:,0,:)
-             IF ( nyn == ny )  topo_3d(:,ny+1,:) = topo_3d(:,ny,:)
-
-             IF ( nys == 0  )  nzb_local(-1,:)   = nzb_local(0,:)
-             IF ( nyn == ny )  nzb_local(ny+1,:) = nzb_local(ny,:)
-          ENDIF
-
-          IF ( .NOT. bc_lr_cyc )  THEN
-             IF ( nxl == 0  )  topo_3d(:,:,-1)   = topo_3d(:,:,0)
-             IF ( nxr == nx )  topo_3d(:,:,nx+1) = topo_3d(:,:,nx)     
-
-             IF ( nxl == 0  )  nzb_local(:,-1)   = nzb_local(:,0)
-             IF ( nxr == nx )  nzb_local(:,nx+1) = nzb_local(:,nx)          
-          ENDIF
-
 
        CASE DEFAULT
-!
+!   
 !--       The DEFAULT case is reached either if the parameter topography
 !--       contains a wrong character string or if the user has defined a special
 !--       case in the user interface. There, the subroutine user_init_grid 
 !--       checks which of these two conditions applies.
-          CALL user_init_grid( topo_3d )
+          CALL user_init_grid( topo )
+          CALL filter_topography( topo )
 
     END SELECT
 !
 !-- Consistency checks and index array initialization are only required for
-!-- non-flat topography, also the initialization of topography height arrays
-!-- zu_s_inner and zw_w_inner
+!-- non-flat topography.
     IF ( TRIM( topography ) /= 'flat' )  THEN
-#if defined( __parallel )
-       CALL MPI_ALLREDUCE( MAXVAL( nzb_local ), nzb_local_max, 1, MPI_INTEGER, &
-                           MPI_MAX, comm2d, ierr )
-       CALL MPI_ALLREDUCE( MINVAL( nzb_local ), nzb_local_min, 1, MPI_INTEGER, &
-                           MPI_MIN, comm2d, ierr )                           
-#else
-       nzb_local_max = MAXVAL( nzb_local )
-       nzb_local_min = MINVAL( nzb_local )
-#endif
-
-!
-!--    Consistency checks
-       IF ( nzb_local_min < 0  .OR.  nzb_local_max  > nz + 1 )  THEN
-          WRITE( message_string, * ) 'nzb_local values are outside the',       &
-                                'model domain',                                &
-                                '&MINVAL( nzb_local ) = ', nzb_local_min,      &
-                                '&MAXVAL( nzb_local ) = ', nzb_local_max
-          CALL message( 'init_grid', 'PA0210', 1, 2, 0, 6, 0 )
-       ENDIF
 !
 !--    In case of non-flat topography, check whether the convention how to 
@@ -1184,9 +1869,9 @@
 !--          defined in init_grid.
              WRITE( message_string, * )                                        &
-                  'The value for "topography_grid_convention" ',               &
-                  'is not set. Its default value is & only valid for ',        &
-                  '"topography" = ''single_building'', ',                      &
-                  '''single_street_canyon'' & or ''read_from_file''.',         &
-                  ' & Choose ''cell_edge'' or ''cell_center''.'
+               'The value for "topography_grid_convention" ',                  &
+               'is not set. Its default value is & only valid for ',           &
+               '"topography" = ''single_building'', ',                         &
+               '''single_street_canyon'' & or ''read_from_file''.',            &
+               ' & Choose ''cell_edge'' or ''cell_center''.'
              CALL message( 'init_grid', 'PA0239', 1, 2, 0, 6, 0 )
           ELSE
@@ -1204,44 +1889,9 @@
                 TRIM( topography_grid_convention ) /= 'cell_center' )  THEN
           WRITE( message_string, * )                                           &
-               'The value for "topography_grid_convention" is ',               &
-               'not recognized. & Choose ''cell_edge'' or ''cell_center''.'
+            'The value for "topography_grid_convention" is ',                  &
+            'not recognized. & Choose ''cell_edge'' or ''cell_center''.'
           CALL message( 'init_grid', 'PA0240', 1, 2, 0, 6, 0 )
        ENDIF
 
-!
-!--    In case of non-flat topography, check whether the convention how to 
-!--    define the topography grid has been set correctly, or whether the default
-!--    is applicable. If this is not possible, abort.
-       IF ( TRIM( topography_grid_convention ) == ' ' )  THEN
-          IF ( TRIM( topography ) /= 'single_building' .AND.                   &
-               TRIM( topography ) /= 'single_street_canyon' .AND.              &
-               TRIM( topography ) /= 'read_from_file' )  THEN
-!--          The default value is not applicable here, because it is only valid
-!--          for the two standard cases 'single_building' and 'read_from_file'
-!--          defined in init_grid.
-             WRITE( message_string, * )                                        &
-                  'The value for "topography_grid_convention" ',               &
-                  'is not set. Its default value is & only valid for ',        &
-                  '"topography" = ''single_building'', ',                      &
-                  '''single_street_canyon'' & or ''read_from_file''.',         &
-                  ' & Choose ''cell_edge'' or ''cell_center''.'
-             CALL message( 'init_grid', 'PA0239', 1, 2, 0, 6, 0 )
-          ELSE
-!--          The default value is applicable here.
-!--          Set convention according to topography.
-             IF ( TRIM( topography ) == 'single_building' .OR.                 &
-                  TRIM( topography ) == 'single_street_canyon' )  THEN
-                topography_grid_convention = 'cell_edge'
-             ELSEIF ( TRIM( topography ) == 'read_from_file' )  THEN
-                topography_grid_convention = 'cell_center'
-             ENDIF
-          ENDIF
-       ELSEIF ( TRIM( topography_grid_convention ) /= 'cell_edge' .AND.        &
-                TRIM( topography_grid_convention ) /= 'cell_center' )  THEN
-          WRITE( message_string, * )                                           &
-               'The value for "topography_grid_convention" is ',               &
-               'not recognized. & Choose ''cell_edge'' or ''cell_center''.'
-          CALL message( 'init_grid', 'PA0240', 1, 2, 0, 6, 0 )
-       ENDIF
 
        IF ( topography_grid_convention == 'cell_edge' )  THEN
@@ -1259,269 +1909,80 @@
           DO  j = nys+1, nyn+1
              DO  i = nxl-1, nxr
-                nzb_local(j,i) = MIN( nzb_local(j,i), nzb_local(j,i+1) )
-             ENDDO
-          ENDDO
-!
-!--       Exchange ghost points
-          CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
-
-          DO  i = nxl, nxr+1
-             DO  j = nys-1, nyn
-                nzb_local(j,i) = MIN( nzb_local(j,i), nzb_local(j+1,i) )
-             ENDDO
-          ENDDO  
-!
-!--       Exchange ghost points          
-          CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
-!
-!--       Apply cell-edge convention also for 3D topo array. The former setting
-!--       of nzb_local will be removed later. 
-          DO  j = nys+1, nyn+1
-             DO  i = nxl-1, nxr
                 DO  k = nzb, nzt+1
-                   IF ( BTEST( topo_3d(k,j,i), 0 )  .OR.                       &
-                        BTEST( topo_3d(k,j,i+1), 0 ) )                         &
-                      topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 0 )
+                   IF ( BTEST( topo(k,j,i), 0 )  .OR.                          &
+                        BTEST( topo(k,j,i+1), 0 ) )                            &
+                       topo(k,j,i) = IBSET( topo(k,j,i), 0 )
                 ENDDO
              ENDDO
           ENDDO      
-          CALL exchange_horiz_int( topo_3d, nbgp )   
+          CALL exchange_horiz_int( topo, nys, nyn, nxl, nxr, nzt, nbgp )
 
           DO  i = nxl, nxr+1
              DO  j = nys-1, nyn
                 DO  k = nzb, nzt+1
-                   IF ( BTEST( topo_3d(k,j,i), 0 )  .OR.                       &
-                        BTEST( topo_3d(k,j+1,i), 0 ) )                         &
-                      topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 0 )
+                   IF ( BTEST( topo(k,j,i), 0 )  .OR.                          &
+                        BTEST( topo(k,j+1,i), 0 ) )                            &
+                      topo(k,j,i) = IBSET( topo(k,j,i), 0 )
                 ENDDO
              ENDDO
           ENDDO  
-          CALL exchange_horiz_int( topo_3d, nbgp )
+          CALL exchange_horiz_int( topo, nys, nyn, nxl, nxr, nzt, nbgp )
    
        ENDIF
-      
-!
-!--    Initialize index arrays nzb_s_inner and nzb_w_inner
-
-       nzb_s_inner = nzb_local
-       nzb_w_inner = nzb_local
-
-!
-!--    Initialize remaining index arrays:
-!--    first pre-initialize them with nzb_s_inner...
-       nzb_u_inner = nzb_s_inner
-       nzb_u_outer = nzb_s_inner
-       nzb_v_inner = nzb_s_inner
-       nzb_v_outer = nzb_s_inner
-       nzb_w_outer = nzb_s_inner
-       nzb_s_outer = nzb_s_inner
-
-!
-!--    ...then extend pre-initialized arrays in their according directions
-!--    based on nzb_local using nzb_tmp as a temporary global index array
-
-!
-!--    nzb_s_outer: 
-!--    extend nzb_local east-/westwards first, then north-/southwards
-       nzb_tmp = nzb_local
-       DO  j = nys, nyn
-          DO  i = nxl, nxr
-             nzb_tmp(j,i) = MAX( nzb_local(j,i-1), nzb_local(j,i),             &
-                                 nzb_local(j,i+1) )
-          ENDDO
-       ENDDO
-       
-       CALL exchange_horiz_2d_int( nzb_tmp, nys, nyn, nxl, nxr, nbgp )
-       
-       DO  i = nxl, nxr
-          DO  j = nys, nyn
-             nzb_s_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i),             &
-                                     nzb_tmp(j+1,i) )
-          ENDDO
-!
-!--       non-cyclic boundary conditions (overwritten by call of
-!--       exchange_horiz_2d_int below in case of cyclic boundary conditions)
-          IF ( nys == 0 )  THEN
-             j = -1
-             nzb_s_outer(j,i) = MAX( nzb_tmp(j+1,i), nzb_tmp(j,i) )
-          ENDIF
-          IF ( nyn == ny )  THEN
-             j = ny + 1
-             nzb_s_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i) )
-          ENDIF
-       ENDDO
-!
-!--    nzb_w_outer: 
-!--    identical to nzb_s_outer
-       nzb_w_outer = nzb_s_outer
-
-!
-!--    nzb_u_inner: 
-!--    extend nzb_local rightwards only
-       nzb_tmp = nzb_local
-       DO  j = nys, nyn
-          DO  i = nxl, nxr
-             nzb_tmp(j,i) = MAX( nzb_local(j,i-1), nzb_local(j,i) )
-          ENDDO
-       ENDDO
-       
-       CALL exchange_horiz_2d_int( nzb_tmp, nys, nyn, nxl, nxr, nbgp )
-       
-       nzb_u_inner = nzb_tmp
-!
-!--    nzb_u_outer: 
-!--    extend current nzb_tmp (nzb_u_inner) north-/southwards
-       DO  i = nxl, nxr
-          DO  j = nys, nyn
-             nzb_u_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i),             &
-                                     nzb_tmp(j+1,i) )
-          ENDDO
-!
-!--       non-cyclic boundary conditions (overwritten by call of
-!--       exchange_horiz_2d_int below in case of cyclic boundary conditions)
-          IF ( nys == 0 )  THEN
-             j = -1
-             nzb_u_outer(j,i) = MAX( nzb_tmp(j+1,i), nzb_tmp(j,i) )
-          ENDIF
-          IF ( nyn == ny )  THEN
-             j = ny + 1
-             nzb_u_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i) )
-          ENDIF
-       ENDDO
-
-!
-!--    nzb_v_inner:
-!--    extend nzb_local northwards only
-       nzb_tmp = nzb_local
-       DO  i = nxl, nxr
-          DO  j = nys, nyn
-             nzb_tmp(j,i) = MAX( nzb_local(j-1,i), nzb_local(j,i) )
-          ENDDO
-       ENDDO
-       
-       CALL exchange_horiz_2d_int( nzb_tmp, nys, nyn, nxl, nxr, nbgp )      
-       nzb_v_inner = nzb_tmp
-
-!
-!--    nzb_v_outer: 
-!--    extend current nzb_tmp (nzb_v_inner) right-/leftwards
-       DO  j = nys, nyn
-          DO  i = nxl, nxr
-             nzb_v_outer(j,i) = MAX( nzb_tmp(j,i-1), nzb_tmp(j,i),             &
-                                     nzb_tmp(j,i+1) )
-          ENDDO
-!
-!--       non-cyclic boundary conditions (overwritten by call of
-!--       exchange_horiz_2d_int below in case of cyclic boundary conditions)
-          IF ( nxl == 0 )  THEN
-             i = -1
-             nzb_v_outer(j,i) = MAX( nzb_tmp(j,i+1), nzb_tmp(j,i) )
-          ENDIF
-          IF ( nxr == nx )  THEN
-             i = nx + 1
-             nzb_v_outer(j,i) = MAX( nzb_tmp(j,i-1), nzb_tmp(j,i) )
-          ENDIF
-       ENDDO
-
-!
-!--    Exchange of lateral boundary values (parallel computers) and cyclic
-!--    boundary conditions, if applicable.
-!--    Since nzb_s_inner and nzb_w_inner are derived directly from nzb_local
-!--    they do not require exchange and are not included here.
-       CALL exchange_horiz_2d_int( nzb_u_inner, nys, nyn, nxl, nxr, nbgp )
-       CALL exchange_horiz_2d_int( nzb_u_outer, nys, nyn, nxl, nxr, nbgp )
-       CALL exchange_horiz_2d_int( nzb_v_inner, nys, nyn, nxl, nxr, nbgp )
-       CALL exchange_horiz_2d_int( nzb_v_outer, nys, nyn, nxl, nxr, nbgp )
-       CALL exchange_horiz_2d_int( nzb_w_outer, nys, nyn, nxl, nxr, nbgp )
-       CALL exchange_horiz_2d_int( nzb_s_outer, nys, nyn, nxl, nxr, nbgp )
-
     ENDIF
-!
-!-- Deallocate temporary array, as it might be reused for different
-!-- grid-levels further below.
-    DEALLOCATE( nzb_tmp )
-
-!
-!-- Determine the maximum level of topography. It is used for
-!-- steering the degradation of order of the applied advection scheme.
-!-- In case of non-cyclic lateral boundaries, the order of the advection
-!-- scheme has to be reduced up to nzt (required at the lateral boundaries).
-    k_top = 0
+
+
+ END SUBROUTINE init_topo
+
+ SUBROUTINE set_topo_flags(topo)
+
+    USE control_parameters,                                                    &
+        ONLY:  bc_lr_cyc, bc_ns_cyc, constant_flux_layer, land_surface,        &
+               use_surface_fluxes, use_top_fluxes, urban_surface
+
+    USE indices,                                                               &
+        ONLY:  nbgp, nx, nxl, nxlg, nxr, nxrg, ny, nyn, nyng, nys, nysg, nz,   &
+               nzb, nzt, wall_flags_0
+
+    USE kinds
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  i             !< index variable along x
+    INTEGER(iwp) ::  j             !< index variable along y
+    INTEGER(iwp) ::  k             !< index variable along z
+
+    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  topo !< input array for 3D topography and dummy array for setting "outer"-flags
+
+    ALLOCATE( wall_flags_0(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+    wall_flags_0 = 0
+!
+!-- Set-up topography flags. First, set flags only for s, u, v and w-grid.
+!-- Further special flags will be set in following loops. 
     DO  i = nxl, nxr
        DO  j = nys, nyn
-          DO  k = nzb, nzt + 1
-             k_top = MAX( k_top, MERGE( k, 0,                                  &
-                                        .NOT. BTEST( topo_3d(k,j,i), 0 ) ) )
+          DO  k = nzb, nzt+1
+!
+!--          scalar grid
+             IF ( BTEST( topo(k,j,i), 0 ) )                                 &
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 0 )
+!
+!--          u grid
+             IF ( BTEST( topo(k,j,i),   0 )  .AND.                          &
+                  BTEST( topo(k,j,i-1), 0 ) )                               &
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 1 )
+!
+!--          v grid
+             IF ( BTEST( topo(k,j,i),   0 )  .AND.                          &
+                  BTEST( topo(k,j-1,i), 0 ) )                               &
+                 wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 2 )
+
           ENDDO
-       ENDDO
-    ENDDO
-#if defined( __parallel )
-    CALL MPI_ALLREDUCE( k_top + 1, nzb_max, 1, MPI_INTEGER,                    & !is +1 really necessary here?
-                        MPI_MAX, comm2d, ierr )
-#else
-    nzb_max = k_top + 1
-#endif
-    IF ( inflow_l .OR. outflow_l .OR. inflow_r .OR. outflow_r .OR.             &
-         inflow_n .OR. outflow_n .OR. inflow_s .OR. outflow_s .OR.             &
-         nest_domain )                                                         &
-    THEN
-       nzb_max = nzt
-    ENDIF
-!
-!-- Finally, if topography extents up to the model top, limit nzb_max to nzt.
-    nzb_max = MIN( nzb_max, nzt ) 
-
-!
-!-- Set the individual index arrays which define the k index from which on
-!-- the usual finite difference form (which does not use surface fluxes) is
-!-- applied 
-    IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
-       nzb_diff_s_inner   = nzb_s_inner + 2
-       nzb_diff_s_outer   = nzb_s_outer + 2
-    ELSE
-       nzb_diff_s_inner   = nzb_s_inner + 1
-       nzb_diff_s_outer   = nzb_s_outer + 1
-    ENDIF
-
-!
-!-- Set-up topography flags. First, set flags only for s, u, v and w-grid.
-!-- Further special flags will be set in following loops. 
-    wall_flags_0 = 0
-    DO  j = nys, nyn
-       DO  i = nxl, nxr
-          DO  k = nzb, nzt+1
-!
-!--          scalar grid
-             IF ( BTEST( topo_3d(k,j,i), 0 ) )                                 &
-                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 0 )
-!
-!--          v grid
-             IF ( BTEST( topo_3d(k,j,i),   0 )  .AND.                          &
-                  BTEST( topo_3d(k,j-1,i), 0 ) )                               &
-                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 2 )
-!
-!--     To do: set outer arrays on basis of topo_3d array, adjust for downward-facing walls
-!--          s grid outer array
-             IF ( k >= nzb_s_outer(j,i) )                                      &
-                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 24 )
-!
-!--          s grid outer array
-             IF ( k >= nzb_u_outer(j,i) )                                      &
-                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 26 )
-!
-!--          s grid outer array
-             IF ( k >= nzb_v_outer(j,i) )                                      &
-                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 27 )
-!
-!--          w grid outer array
-             IF ( k >= nzb_w_outer(j,i) )                                      &
-                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 28 )
-          ENDDO
 
           DO k = nzb, nzt
 !
 !--          w grid
-             IF ( BTEST( topo_3d(k,j,i),   0 )  .AND.                          &
-                  BTEST( topo_3d(k+1,j,i), 0 ) )                               &
+             IF ( BTEST( topo(k,j,i),   0 )  .AND.                          &
+                  BTEST( topo(k+1,j,i), 0 ) )                               &
                 wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 3 )
           ENDDO
@@ -1530,13 +1991,20 @@
        ENDDO
     ENDDO
-!
-!-- u grid. Note, reverse 
-!-- memory access is required for setting flag on u-grid
-    DO  j = nys, nyn
-       DO  i = nxl, nxr
+
+    CALL exchange_horiz_int ( wall_flags_0, nys, nyn, nxl, nxr, nzt, nbgp )
+!
+!-- Set outer array for scalars to mask near-surface grid points in 
+!-- production_e
+    DO i = nxl, nxr
+       DO j = nys, nyn
           DO k = nzb, nzt+1
-             IF ( BTEST( topo_3d(k,j,i),   0 )  .AND.                          &
-                  BTEST( topo_3d(k,j,i-1), 0 ) )                               &
-                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 1 )
+             IF ( BTEST( wall_flags_0(k,j-1,i), 0 )  .AND.                       &
+                  BTEST( wall_flags_0(k,j+1,i), 0 )  .AND.                       &
+                  BTEST( wall_flags_0(k,j,i-1), 0 )  .AND.                       &
+                  BTEST( wall_flags_0(k,j-1,i-1), 0 )  .AND.                       &
+                  BTEST( wall_flags_0(k,j+1,i-1), 0 )  .AND.                       &
+                  BTEST( wall_flags_0(k,j-1,i+1), 0 )  .AND.                       &
+                  BTEST( wall_flags_0(k,j+1,i+1), 0 ) )                            &
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 24 )
           ENDDO
        ENDDO
@@ -1576,4 +2044,5 @@
              wall_flags_0(nzt+1,j,i) = IBCLR( wall_flags_0(nzt+1,j,i), 9 )
 
+
           DO k = nzb+1, nzt
 !
@@ -1634,5 +2103,5 @@
              IF ( BTEST( wall_flags_0(k-1,j,i), 0 )  .AND.                     &
             .NOT. BTEST( wall_flags_0(k,j,i), 0   ) )                          & 
-                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 13 ) 
+                 wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 13 ) 
 !
 !--          Downward facing wall on u grid
@@ -1665,10 +2134,10 @@
                 wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 14 )
 
-!
+!   
 !--          Upward facing wall on v grid
              IF ( .NOT. BTEST( wall_flags_0(k,j,i),   2 )  .AND.               &
                         BTEST( wall_flags_0(k+1,j,i), 2 ) )                    & 
                 wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 16 )
-
+   
 !
 !--          Upward facing wall on w grid
@@ -1681,5 +2150,5 @@
                   BTEST( wall_flags_0(k,j,i), 12 ) .OR.                        &
                   BTEST( wall_flags_0(k,j,i), 13 ) )                           &
-                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 22 )
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 22 )
 !
 !--          Special flag on scalar grid, nzb_diff_s_inner - 1, required for 
@@ -1688,10 +2157,10 @@
                 IF ( BTEST( wall_flags_0(k,j,i),   0 )  .AND.                  &
                      BTEST( wall_flags_0(k+1,j,i), 0 ) )                       &
-                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 23 )
+                  wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 23 )
              ELSE
                 IF ( BTEST( wall_flags_0(k,j,i), 22 ) )                        &
                    wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 23 )
              ENDIF
-
+   
 
           ENDDO
@@ -1701,334 +2170,65 @@
     ENDDO
 !
+!-- Finally, set identification flags indicating natural terrain or buildings.
+!-- Natural terrain grid points.
+    IF ( land_surface )  THEN
+       DO i = nxl, nxr
+          DO j = nys, nyn
+             DO k = nzb, nzt+1
+!
+!--             Natural terrain grid point
+                IF ( BTEST( topo(k,j,i), 1 ) )                                 &
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 5 )
+             ENDDO
+          ENDDO
+       ENDDO
+    ENDIF
+!
+!-- Building grid points.
+    IF ( urban_surface )  THEN
+       DO i = nxl, nxr
+          DO j = nys, nyn
+             DO k = nzb, nzt+1
+                IF ( BTEST( topo(k,j,i), 2 ) )                                 &
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 6 )
+             ENDDO
+          ENDDO
+       ENDDO
+    ENDIF
+
+!
 !-- Exchange ghost points for wall flags
-    CALL exchange_horiz_int( wall_flags_0, nbgp )
+    CALL exchange_horiz_int( wall_flags_0, nys, nyn, nxl, nxr, nzt, nbgp )
 !
 !-- Set boundary conditions also for flags. Can be interpreted as Neumann
 !-- boundary conditions for topography. 
     IF ( .NOT. bc_ns_cyc )  THEN
-       IF ( nys == 0  )  wall_flags_0(:,-1,:)   = wall_flags_0(:,0,:)
-       IF ( nyn == ny )  wall_flags_0(:,ny+1,:) = wall_flags_0(:,ny,:)
+       IF ( nys == 0  )  THEN
+          DO  i = 1, nbgp     
+             wall_flags_0(:,nys-i,:)   = wall_flags_0(:,nys,:)
+          ENDDO
+       ENDIF
+       IF ( nyn == ny )  THEN
+          DO  i = 1, nbgp  
+             wall_flags_0(:,nyn+i,:) = wall_flags_0(:,nyn,:)
+          ENDDO
+       ENDIF
     ENDIF
     IF ( .NOT. bc_lr_cyc )  THEN
-       IF ( nxl == 0  )  wall_flags_0(:,:,-1)   = wall_flags_0(:,:,0)
-       IF ( nxr == nx )  wall_flags_0(:,:,nx+1) = wall_flags_0(:,:,nx)           
+       IF ( nxl == 0  )  THEN
+          DO  i = 1, nbgp   
+             wall_flags_0(:,:,nxl-i)   = wall_flags_0(:,:,nxl)
+          ENDDO
+       ENDIF
+       IF ( nxr == nx )  THEN 
+          DO  i = 1, nbgp   
+             wall_flags_0(:,:,nxr+i) = wall_flags_0(:,:,nxr)     
+          ENDDO
+       ENDIF      
     ENDIF
 
-!
-!-- Initialize boundary conditions via surface type
-    CALL init_bc
-!
-!-- Allocate and set topography height arrays required for data output
-    IF ( TRIM( topography ) /= 'flat' )  THEN
-!
-!--    Allocate and set the arrays containing the topography height
-
-       IF ( nxr == nx  .AND.  nyn /= ny )  THEN
-          ALLOCATE( zu_s_inner(nxl:nxr+1,nys:nyn),                             &
-                    zw_w_inner(nxl:nxr+1,nys:nyn) )
-       ELSEIF ( nxr /= nx  .AND.  nyn == ny )  THEN
-          ALLOCATE( zu_s_inner(nxl:nxr,nys:nyn+1),                             &
-                    zw_w_inner(nxl:nxr,nys:nyn+1) )
-       ELSEIF ( nxr == nx  .AND.  nyn == ny )  THEN
-          ALLOCATE( zu_s_inner(nxl:nxr+1,nys:nyn+1),                           &
-                    zw_w_inner(nxl:nxr+1,nys:nyn+1) )
-       ELSE
-          ALLOCATE( zu_s_inner(nxl:nxr,nys:nyn),                               &
-                    zw_w_inner(nxl:nxr,nys:nyn) )
-       ENDIF
-
-       zu_s_inner   = 0.0_wp
-       zw_w_inner   = 0.0_wp
-!
-!--    Determine local topography height on scalar and w-grid. Note, setting
-!--    lateral boundary values is not necessary, realized via wall_flags_0 
-!--    array. Further, please note that loop bounds are different from 
-!--    nxl to nxr and nys to nyn on south and right model boundary, hence, 
-!--    use intrinsic lbound and ubound functions to infer array bounds.
-       DO  i = LBOUND(zu_s_inner, 1), UBOUND(zu_s_inner, 1)
-          DO  j = LBOUND(zu_s_inner, 2), UBOUND(zu_s_inner, 2)
-!
-!--          Topography height on scalar grid. Therefore, determine index of 
-!--          upward-facing surface element on scalar grid. 
-             zu_s_inner(i,j) = zu( get_topography_top_index( j, i, 's' ) )
-!
-!--          Topography height on w grid. Therefore, determine index of 
-!--          upward-facing surface element on w grid.
-             zw_w_inner(i,j) = zw( get_topography_top_index( j, i, 's' ) )
-          ENDDO
-       ENDDO
-
-    ENDIF
-
-!
-!-- Calculate wall flag arrays for the multigrid method.
-!-- Please note, wall flags are only applied in the not cache-optimized 
-!-- version.
-    IF ( psolver == 'multigrid_noopt' )  THEN
-
-!
-!--    Gridpoint increment of the current level. 
-       inc = 1
-       DO  l = maximum_grid_level, 1 , -1
-!
-!--       Set grid_level as it is required for exchange_horiz_2d_int
-          grid_level = l
-
-          nxl_l = nxl_mg(l)
-          nxr_l = nxr_mg(l)
-          nys_l = nys_mg(l)
-          nyn_l = nyn_mg(l)
-          nzt_l = nzt_mg(l)
-!
-!--       Assign the flag level to be calculated
-          SELECT CASE ( l )
-             CASE ( 1 )
-                flags => wall_flags_1
-             CASE ( 2 )
-                flags => wall_flags_2
-             CASE ( 3 )
-                flags => wall_flags_3
-             CASE ( 4 )
-                flags => wall_flags_4
-             CASE ( 5 )
-                flags => wall_flags_5
-             CASE ( 6 )
-                flags => wall_flags_6
-             CASE ( 7 )
-                flags => wall_flags_7
-             CASE ( 8 )
-                flags => wall_flags_8
-             CASE ( 9 )
-                flags => wall_flags_9
-             CASE ( 10 )
-                flags => wall_flags_10
-          END SELECT
-
-!
-!--       Depending on the grid level, set the respective bits in case of
-!--       neighbouring walls
-!--       Bit 0:  wall to the bottom
-!--       Bit 1:  wall to the top (not realized in remaining PALM code so far)
-!--       Bit 2:  wall to the south
-!--       Bit 3:  wall to the north
-!--       Bit 4:  wall to the left
-!--       Bit 5:  wall to the right
-!--       Bit 6:  inside building
-
-          flags = 0
-
-!
-!--       In case of masking method, flags are not set and multigrid method
-!--       works like FFT-solver
-          IF ( .NOT. masking_method )  THEN
-
-!
-!--          Allocate temporary array for topography heights on coarser grid
-!--          level. Please note, 2 ghoist points are required, in order to
-!--          calculate flags() on the interior ghost point. 
-             ALLOCATE( nzb_tmp(nys_l-2:nyn_l+2,nxl_l-2:nxr_l+2) )
-             nzb_tmp = 0
-             
-             DO  i = nxl_l, nxr_l
-                DO  j = nys_l, nyn_l
-                   nzb_tmp(j,i) = nzb_local(j*inc,i*inc)
-                ENDDO
-             ENDDO
-!
-!--          Exchange ghost points on respective multigrid level. 2 ghost points
-!--          are required, in order to calculate flags on 
-!--          nys_l-1 / nyn_l+1 / nxl_l-1 / nxr_l+1. The alternative would be to 
-!--          exchange 3D-INTEGER array flags on the respective multigrid level.
-             CALL exchange_horiz_2d_int( nzb_tmp, nys_l, nyn_l, nxl_l, nxr_l, 2 )
-!
-!--          Set non-cyclic boundary conditions on respective multigrid level
-             IF ( .NOT. bc_ns_cyc )  THEN
-                IF ( inflow_s .OR. outflow_s .OR. nest_bound_s  )  THEN
-                   nzb_tmp(-2,:) = nzb_tmp(0,:)
-                   nzb_tmp(-1,:) = nzb_tmp(0,:)
-                ENDIF
-                IF ( inflow_n .OR. outflow_n .OR. nest_bound_n )  THEN
-                   nzb_tmp(nyn_l+2,:) = nzb_tmp(nyn_l,:)
-                   nzb_tmp(nyn_l+1,:) = nzb_tmp(nyn_l,:)
-                ENDIF
-             ENDIF
-             IF ( .NOT. bc_lr_cyc )  THEN
-                IF ( inflow_l .OR. outflow_l .OR. nest_bound_l  )  THEN
-                   nzb_tmp(:,-2) = nzb_tmp(:,0)
-                   nzb_tmp(:,-1) = nzb_tmp(:,0)
-                ENDIF
-                IF ( inflow_r .OR. outflow_r .OR. nest_bound_r )  THEN
-                   nzb_tmp(:,nxr_l+1) = nzb_tmp(:,nxr_l)    
-                   nzb_tmp(:,nxr_l+2) = nzb_tmp(:,nxr_l)      
-                ENDIF        
-             ENDIF
-                       
-             DO  i = nxl_l-1, nxr_l+1
-                DO  j = nys_l-1, nyn_l+1
-                   DO  k = nzb, nzt_l+1     
-!
-!--                   Inside/outside building (inside building does not need
-!--                   further tests for walls)
-                      IF ( k*inc <= nzb_tmp(j,i) )  THEN
-
-                         flags(k,j,i) = IBSET( flags(k,j,i), 6 )
-
-                      ELSE
-!
-!--                      Bottom wall
-                         IF ( (k-1)*inc <= nzb_tmp(j,i) )  THEN
-                            flags(k,j,i) = IBSET( flags(k,j,i), 0 )
-                         ENDIF
-!
-!--                      South wall
-                         IF ( k*inc <= nzb_tmp(j-1,i) )  THEN
-                            flags(k,j,i) = IBSET( flags(k,j,i), 2 )
-                         ENDIF
-!
-!--                      North wall
-                         IF ( k*inc <= nzb_tmp(j+1,i) )  THEN
-                            flags(k,j,i) = IBSET( flags(k,j,i), 3 )
-                         ENDIF
-!
-!--                      Left wall
-                         IF ( k*inc <= nzb_tmp(j,i-1) )  THEN
-                            flags(k,j,i) = IBSET( flags(k,j,i), 4 )
-                         ENDIF
-!
-!--                      Right wall
-                         IF ( k*inc <= nzb_tmp(j,i+1) )  THEN
-                            flags(k,j,i) = IBSET( flags(k,j,i), 5 )
-                         ENDIF
-
-                      ENDIF
-                            
-                   ENDDO
-                ENDDO
-             ENDDO
-
-             DEALLOCATE( nzb_tmp )
-
-          ENDIF
-
-          inc = inc * 2
-
-       ENDDO
-!
-!--    Reset grid_level to "normal" grid
-       grid_level = 0
-       
-    ENDIF
-!
-!-- Allocate flags needed for masking walls. Even though these flags are only
-!-- required in the ws-scheme, the arrays need to be allocated here as they are 
-!-- used in OpenACC directives. 
-    ALLOCATE( advc_flags_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                     &
-              advc_flags_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
-    advc_flags_1 = 0
-    advc_flags_2 = 0
-!
-!-- Init flags for ws-scheme to degrade order of the numerics near walls, i.e. 
-!-- to decrease the numerical stencil appropriately.
-    IF ( momentum_advec == 'ws-scheme'  .OR.  scalar_advec == 'ws-scheme'      &
-       )  THEN
-       CALL ws_init_flags
-    ENDIF
-
-!
-!-- In case of topography: limit near-wall mixing length l_wall further:
-!-- Go through all points of the subdomain one by one and look for the closest
-!-- surface
-    DO  i = nxl, nxr
-       DO  j = nys, nyn
-          DO  k = nzb+1, nzt
-!
-!--          Check if current gridpoint belongs to the atmosphere
-             IF ( BTEST( wall_flags_0(k,j,i), 0 ) )  THEN
-!
-!--             Check for neighbouring grid-points.
-!--             Vertical distance, down
-                IF ( .NOT. BTEST( wall_flags_0(k-1,j,i), 0 ) )                 &
-                   l_wall(k,j,i) = MIN( l_grid(k), zu(k) - zw(k-1) )
-!
-!--             Vertical distance, up
-                IF ( .NOT. BTEST( wall_flags_0(k+1,j,i), 0 ) )                 &
-                   l_wall(k,j,i) = MIN( l_grid(k), zw(k) - zu(k) )
-!
-!--             y-distance
-                IF ( .NOT. BTEST( wall_flags_0(k,j-1,i), 0 )  .OR.             &
-                     .NOT. BTEST( wall_flags_0(k,j+1,i), 0 ) )                 &
-                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k), 0.5_wp * dy )
-!
-!--             x-distance
-                IF ( .NOT. BTEST( wall_flags_0(k,j,i-1), 0 )  .OR.             &
-                     .NOT. BTEST( wall_flags_0(k,j,i+1), 0 ) )                 &
-                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k), 0.5_wp * dx )
-!
-!--              yz-distance (vertical edges, down)
-                 IF ( .NOT. BTEST( wall_flags_0(k-1,j-1,i), 0 )  .OR.          &
-                      .NOT. BTEST( wall_flags_0(k-1,j+1,i), 0 )  )             &
-                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
-                                        SQRT( 0.25_wp * dy**2 +                &
-                                       ( zu(k) - zw(k-1) )**2 ) )
-!
-!--              yz-distance (vertical edges, up)
-                 IF ( .NOT. BTEST( wall_flags_0(k+1,j-1,i), 0 )  .OR.          &
-                      .NOT. BTEST( wall_flags_0(k+1,j+1,i), 0 )  )             &
-                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
-                                        SQRT( 0.25_wp * dy**2 +                &
-                                       ( zw(k) - zu(k) )**2 ) )
-!
-!--              xz-distance (vertical edges, down)
-                 IF ( .NOT. BTEST( wall_flags_0(k-1,j,i-1), 0 )  .OR.          &
-                      .NOT. BTEST( wall_flags_0(k-1,j,i+1), 0 )  )             &
-                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
-                                        SQRT( 0.25_wp * dx**2 +                &
-                                       ( zu(k) - zw(k-1) )**2 ) )
-!
-!--              xz-distance (vertical edges, up)
-                 IF ( .NOT. BTEST( wall_flags_0(k+1,j,i-1), 0 )  .OR.          &
-                      .NOT. BTEST( wall_flags_0(k+1,j,i+1), 0 )  )             &
-                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
-                                        SQRT( 0.25_wp * dx**2 +                &
-                                       ( zw(k) - zu(k) )**2 ) )
-!
-!--             xy-distance (horizontal edges)
-                IF ( .NOT. BTEST( wall_flags_0(k,j-1,i-1), 0 )  .OR.           &
-                     .NOT. BTEST( wall_flags_0(k,j+1,i-1), 0 )  .OR.           &
-                     .NOT. BTEST( wall_flags_0(k,j-1,i+1), 0 )  .OR.           &
-                     .NOT. BTEST( wall_flags_0(k,j+1,i+1), 0 ) )               &
-                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
-                                        SQRT( 0.25_wp * ( dx**2 + dy**2 ) ) )
-!
-!--             xyz distance (vertical and horizontal edges, down)
-                IF ( .NOT. BTEST( wall_flags_0(k-1,j-1,i-1), 0 )  .OR.         &
-                     .NOT. BTEST( wall_flags_0(k-1,j+1,i-1), 0 )  .OR.         &
-                     .NOT. BTEST( wall_flags_0(k-1,j-1,i+1), 0 )  .OR.         &
-                     .NOT. BTEST( wall_flags_0(k-1,j+1,i+1), 0 ) )             &
-                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
-                                        SQRT( 0.25_wp * ( dx**2 + dy**2 )      &
-                                              +  ( zu(k) - zw(k-1) )**2  ) )
-!
-!--             xyz distance (vertical and horizontal edges, up)
-                IF ( .NOT. BTEST( wall_flags_0(k+1,j-1,i-1), 0 )  .OR.         &
-                     .NOT. BTEST( wall_flags_0(k+1,j+1,i-1), 0 )  .OR.         &
-                     .NOT. BTEST( wall_flags_0(k+1,j-1,i+1), 0 )  .OR.         &
-                     .NOT. BTEST( wall_flags_0(k+1,j+1,i+1), 0 ) )             &
-                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
-                                        SQRT( 0.25_wp * ( dx**2 + dy**2 )      &
-                                              +  ( zw(k) - zu(k) )**2  ) )
-                  
-             ENDIF
-          ENDDO
-       ENDDO
-    ENDDO
-!
-!-- Set lateral boundary conditions for l_wall
-    CALL exchange_horiz( l_wall, nbgp )     
-
-!
-!-- Vertical nesting: communicate vertical grid level arrays between fine and
-!-- coarse grid
-    IF ( vnested )  CALL vnest_init_grid
-
- END SUBROUTINE init_grid
+
+ END SUBROUTINE set_topo_flags
+
+
+
