Index: palm/trunk/SOURCE/init_grid.f90
===================================================================
--- palm/trunk/SOURCE/init_grid.f90	(revision 4159)
+++ palm/trunk/SOURCE/init_grid.f90	(revision 4168)
@@ -25,4 +25,8 @@
 ! -----------------
 ! $Id$
+! Pre-calculate topography top index and store it on an array (replaces former
+! functions get_topography_top_index)
+! 
+! 4159 2019-08-15 13:31:35Z suehring
 ! Revision of topography processing. This was not consistent between 2D and 3D
 ! buildings. 
@@ -420,5 +424,5 @@
                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, topo_min_level
+               nzb_w_outer, nzt, topo_top_ind, topo_min_level
     
     USE kinds
@@ -429,5 +433,5 @@
 
     USE surface_mod,                                                           &
-        ONLY:  get_topography_top_index, get_topography_top_index_ji, init_bc
+        ONLY:  init_bc
 
     USE vertical_nesting_mod,                                                  &
@@ -919,8 +923,8 @@
     topo_min_level   = 0
 #if defined( __parallel )
-    CALL MPI_ALLREDUCE( MINVAL( get_topography_top_index( 's' ) ),             &
+    CALL MPI_ALLREDUCE( MINVAL( topo_top_ind(nys:nyn,nxl:nxr,0) ),             &
                         topo_min_level, 1, MPI_INTEGER, MPI_MIN, comm2d, ierr )
 #else
-    topo_min_level = MINVAL( get_topography_top_index( 's' ) )
+    topo_min_level = MINVAL( topo_top_ind(nys:nyn,nxl:nxr,0) )
 #endif
 !
@@ -960,9 +964,9 @@
 !--          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_ji( j, i, 's' ) )
+             zu_s_inner(i,j) = zu(topo_top_ind(j,i,0))
 !
 !--          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_ji( j, i, 's' ) )
+             zw_w_inner(i,j) = zw(topo_top_ind(j,i,3))
           ENDDO
        ENDDO
@@ -988,5 +992,5 @@
 !
 !-- Initialize 2D-index arrays. Note, these will be removed soon! 
-    nzb_local(nys:nyn,nxl:nxr) = get_topography_top_index( 's' )
+    nzb_local(nys:nyn,nxl:nxr) = topo_top_ind(nys:nyn,nxl:nxr,0)
     CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
 !
@@ -996,8 +1000,8 @@
     IF ( TRIM( topography ) /= 'flat' )  THEN
 #if defined( __parallel )
-       CALL MPI_ALLREDUCE( MAXVAL( get_topography_top_index( 's' ) ),          &
+       CALL MPI_ALLREDUCE( MAXVAL( topo_top_ind(nys:nyn,nxl:nxr,0) ),          &
                            nzb_local_max, 1, MPI_INTEGER, MPI_MAX, comm2d, ierr )               
 #else
-       nzb_local_max = MAXVAL( get_topography_top_index( 's' ) )
+       nzb_local_max = MAXVAL( topo_top_ind(nys:nyn,nxl:nxr,0) )
 #endif
        nzb_local_min = topo_min_level
@@ -2017,7 +2021,4 @@
     USE pegrid
 
-    USE surface_mod,                                                           &
-        ONLY:  get_topography_top_index, get_topography_top_index_ji
-
     IMPLICIT NONE
 
@@ -2572,5 +2573,5 @@
     USE indices,                                                               &
         ONLY:  nbgp, nx, nxl, nxlg, nxr, nxrg, ny, nyn, nyng, nys, nysg, nzb,  &
-               nzt, wall_flags_0
+               nzt, topo_top_ind, wall_flags_0
 
     USE kinds
@@ -2579,4 +2580,5 @@
 
     INTEGER(iwp) ::  i             !< index variable along x
+    INTEGER(iwp) ::  ibit          !< integer bit position of topgraphy masking array
     INTEGER(iwp) ::  j             !< index variable along y
     INTEGER(iwp) ::  k             !< index variable along z
@@ -2885,5 +2887,48 @@
        ENDIF      
     ENDIF
-
+!
+!-- Pre-calculate topography top indices (former get_topography_top_index 
+!-- function)
+    ALLOCATE( topo_top_ind(nysg:nyng,nxlg:nxrg,0:4) )
+!
+!-- Uppermost topography index on scalar grid
+    ibit = 12
+    topo_top_ind(:,:,0) = MAXLOC(                                              &
+                                  MERGE( 1, 0,                                 &
+                                          BTEST( wall_flags_0(:,:,:), ibit )   &
+                                       ), DIM = 1                              &
+                                ) - 1 
+!
+!-- Uppermost topography index on u grid 
+    ibit = 14
+    topo_top_ind(:,:,1) = MAXLOC(                                              &
+                                  MERGE( 1, 0,                                 &
+                                          BTEST( wall_flags_0(:,:,:), ibit )   &
+                                       ), DIM = 1                              &
+                                ) - 1 
+!
+!-- Uppermost topography index on v grid 
+    ibit = 16
+    topo_top_ind(:,:,2) = MAXLOC(                                              &
+                                  MERGE( 1, 0,                                 &
+                                          BTEST( wall_flags_0(:,:,:), ibit )   &
+                                       ), DIM = 1                              &
+                                ) - 1 
+!
+!-- Uppermost topography index on w grid
+    ibit = 18
+    topo_top_ind(:,:,3) = MAXLOC(                                              &
+                                  MERGE( 1, 0,                                 &
+                                          BTEST( wall_flags_0(:,:,:), ibit )   &
+                                       ), DIM = 1                              &
+                                ) - 1 
+!
+!-- Uppermost topography index on scalar outer grid
+    ibit = 24
+    topo_top_ind(:,:,4) = MAXLOC(                                              &
+                                  MERGE( 1, 0,                                 &
+                                          BTEST( wall_flags_0(:,:,:), ibit )   &
+                                       ), DIM = 1                              &
+                                ) - 1                            
 
  END SUBROUTINE set_topo_flags
