Index: /palm/trunk/SOURCE/pmc_interface_mod.f90
===================================================================
--- /palm/trunk/SOURCE/pmc_interface_mod.f90	(revision 2983)
+++ /palm/trunk/SOURCE/pmc_interface_mod.f90	(revision 2984)
@@ -25,4 +25,8 @@
 ! -----------------
 ! $Id$
+! Bugfix in the log-law correction initialization. Pivot node cannot any more be 
+! selected from outside the subdomain in order to prevent array under/overflows.
+! 
+! 2967 2018-04-13 11:22:08Z raasch
 ! bugfix: missing parallel cpp-directives added
 ! 
@@ -2116,9 +2120,16 @@
                            k, j, inc, wall_index, z0_topo, kb, direction,      &
                            ncorr )
-!
-!--                   The direction of the wall-normal index is stored as the
-!--                   sign of the logc-element.
-                      logc_u_l(2,k,j) = inc * lc
-                      logc_ratio_u_l(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
+                      IF ( lc == -99 )  THEN
+!                         
+!--                      The pivot point is out of subdomain, skip the correction. 
+                         logc_u_l(2,k,j) = 0
+                         logc_ratio_u_l(2,0:ncorr-1,k,j) = 1.0_wp
+                      ELSE
+!
+!--                      The direction of the wall-normal index is stored as the
+!--                      sign of the logc-element.
+                         logc_u_l(2,k,j) = inc * lc
+                         logc_ratio_u_l(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
+                      ENDIF
                       lcr(0:ncorr-1) = 1.0_wp
                    ENDDO
@@ -2138,9 +2149,16 @@
                            k, j, inc, wall_index, z0_topo, kb, direction,      &
                            ncorr )
-!
-!--                   The direction of the wall-normal index is stored as the
-!--                   sign of the logc-element.
-                      logc_u_l(2,k,j) = inc * lc
-                      logc_ratio_u_l(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
+                      IF ( lc == -99 )  THEN
+!                         
+!--                      The pivot point is out of subdomain, skip the correction. 
+                         logc_u_l(2,k,j) = 0
+                         logc_ratio_u_l(2,0:ncorr-1,k,j) = 1.0_wp
+                      ELSE
+!
+!--                      The direction of the wall-normal index is stored as the
+!--                      sign of the logc-element.
+                         logc_u_l(2,k,j) = inc * lc
+                         logc_ratio_u_l(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
+                      ENDIF
                       lcr(0:ncorr-1) = 1.0_wp
                    ENDDO
@@ -2166,9 +2184,16 @@
                            k, j, inc, wall_index, z0_topo, kb, direction,      &
                            ncorr )
-!
-!--                   The direction of the wall-normal index is stored as the
-!--                   sign of the logc-element.
-                      logc_w_l(2,k,j) = inc * lc
-                      logc_ratio_w_l(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
+                      IF ( lc == -99 )  THEN
+!                         
+!--                      The pivot point is out of subdomain, skip the correction. 
+                         logc_w_l(2,k,j) = 0
+                         logc_ratio_w_l(2,0:ncorr-1,k,j) = 1.0_wp
+                      ELSE
+!
+!--                      The direction of the wall-normal index is stored as the
+!--                      sign of the logc-element.
+                         logc_w_l(2,k,j) = inc * lc
+                         logc_ratio_w_l(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
+                      ENDIF
                       lcr(0:ncorr-1) = 1.0_wp
                    ENDDO
@@ -2188,9 +2213,16 @@
                            k, j, inc, wall_index, z0_topo, kb, direction,      &
                            ncorr )
-!
-!--                   The direction of the wall-normal index is stored as the
-!--                   sign of the logc-element.
-                      logc_w_l(2,k,j) = inc * lc
-                      logc_ratio_w_l(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
+                      IF ( lc == -99 )  THEN
+!                         
+!--                      The pivot point is out of subdomain, skip the correction. 
+                         logc_w_l(2,k,j) = 0
+                         logc_ratio_w_l(2,0:ncorr-1,k,j) = 1.0_wp
+                      ELSE
+!
+!--                      The direction of the wall-normal index is stored as the
+!--                      sign of the logc-element.
+                         logc_w_l(2,k,j) = inc * lc
+                         logc_ratio_w_l(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
+                      ENDIF
                       lcr(0:ncorr-1) = 1.0_wp
                    ENDDO
@@ -2229,9 +2261,16 @@
                       CALL pmci_define_loglaw_correction_parameters( lc, lcr,  &
                            k, j, inc, wall_index, z0_topo, kb, direction, ncorr )
-!
-!--                   The direction of the wall-normal index is stored as the
-!--                   sign of the logc-element.
-                      logc_u_r(2,k,j) = inc * lc
-                      logc_ratio_u_r(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
+                      IF ( lc == -99 )  THEN
+!                         
+!--                      The pivot point is out of subdomain, skip the correction. 
+                         logc_u_r(2,k,j) = 0
+                         logc_ratio_u_r(2,0:ncorr-1,k,j) = 1.0_wp
+                      ELSE
+!
+!--                      The direction of the wall-normal index is stored as the
+!--                      sign of the logc-element.
+                         logc_u_r(2,k,j) = inc * lc
+                         logc_ratio_u_r(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
+                      ENDIF
                       lcr(0:ncorr-1) = 1.0_wp
                    ENDDO
@@ -2251,9 +2290,16 @@
                            k, j, inc, wall_index, z0_topo, kb, direction,      &
                            ncorr )
-!
-!--                   The direction of the wall-normal index is stored as the
-!--                   sign of the logc-element.
-                      logc_u_r(2,k,j) = inc * lc
-                      logc_ratio_u_r(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
+                      IF ( lc == -99 )  THEN
+!                         
+!--                      The pivot point is out of subdomain, skip the correction. 
+                         logc_u_r(2,k,j) = 0
+                         logc_ratio_u_r(2,0:ncorr-1,k,j) = 1.0_wp
+                      ELSE
+!
+!--                      The direction of the wall-normal index is stored as the
+!--                      sign of the logc-element.
+                         logc_u_r(2,k,j) = inc * lc
+                         logc_ratio_u_r(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
+                      ENDIF
                       lcr(0:ncorr-1) = 1.0_wp
                    ENDDO
@@ -2278,9 +2324,16 @@
                            k, j, inc, wall_index, z0_topo, kb, direction,      &
                            ncorr )
-!
-!--                   The direction of the wall-normal index is stored as the
-!--                   sign of the logc-element.
-                      logc_w_r(2,k,j) = inc * lc
-                      logc_ratio_w_r(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
+                      IF ( lc == -99 )  THEN
+!                         
+!--                      The pivot point is out of subdomain, skip the correction. 
+                         logc_w_r(2,k,j) = 0
+                         logc_ratio_w_r(2,0:ncorr-1,k,j) = 1.0_wp
+                      ELSE
+!
+!--                      The direction of the wall-normal index is stored as the
+!--                      sign of the logc-element.
+                         logc_w_r(2,k,j) = inc * lc
+                         logc_ratio_w_r(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
+                      ENDIF
                       lcr(0:ncorr-1) = 1.0_wp
                    ENDDO
@@ -2300,9 +2353,16 @@
                            k, j, inc, wall_index, z0_topo, kb, direction,      &
                            ncorr )
-!
-!--                   The direction of the wall-normal index is stored as the
-!--                   sign of the logc-element.
-                      logc_w_r(2,k,j) = inc * lc
-                      logc_ratio_w_r(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
+                      IF ( lc == -99 )  THEN
+!                         
+!--                      The pivot point is out of subdomain, skip the correction. 
+                         logc_w_r(2,k,j) = 0
+                         logc_ratio_w_r(2,0:ncorr-1,k,j) = 1.0_wp
+                      ELSE
+!
+!--                      The direction of the wall-normal index is stored as the
+!--                      sign of the logc-element.
+                         logc_w_r(2,k,j) = inc * lc
+                         logc_ratio_w_r(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
+                      ENDIF
                       lcr(0:ncorr-1) = 1.0_wp
                    ENDDO
@@ -2342,9 +2402,16 @@
                            k, i, inc, wall_index, z0_topo, kb, direction,      &
                            ncorr )
-!
-!--                   The direction of the wall-normal index is stored as the
-!--                   sign of the logc-element.
-                      logc_v_s(2,k,i) = inc * lc
-                      logc_ratio_v_s(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
+                      IF ( lc == -99 )  THEN
+!                         
+!--                      The pivot point is out of subdomain, skip the correction. 
+                         logc_v_s(2,k,i) = 0
+                         logc_ratio_v_s(2,0:ncorr-1,k,i) = 1.0_wp
+                      ELSE
+!
+!--                      The direction of the wall-normal index is stored as the
+!--                      sign of the logc-element.
+                         logc_v_s(2,k,i) = inc * lc
+                         logc_ratio_v_s(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
+                      ENDIF
                       lcr(0:ncorr-1) = 1.0_wp
                    ENDDO
@@ -2364,9 +2431,16 @@
                            k, i, inc, wall_index, z0_topo, kb, direction,      &
                            ncorr )
-!
-!--                   The direction of the wall-normal index is stored as the
-!--                   sign of the logc-element.
-                      logc_v_s(2,k,i) = inc * lc
-                      logc_ratio_v_s(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
+                      IF ( lc == -99 )  THEN
+!                         
+!--                      The pivot point is out of subdomain, skip the correction. 
+                         logc_v_s(2,k,i) = 0
+                         logc_ratio_v_s(2,0:ncorr-1,k,i) = 1.0_wp
+                      ELSE
+!
+!--                      The direction of the wall-normal index is stored as the
+!--                      sign of the logc-element.
+                         logc_v_s(2,k,i) = inc * lc
+                         logc_ratio_v_s(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
+                      ENDIF
                       lcr(0:ncorr-1) = 1.0_wp
                    ENDDO
@@ -2392,9 +2466,16 @@
                            k, i, inc, wall_index, z0_topo, kb, direction,      &
                            ncorr )
-!
-!--                   The direction of the wall-normal index is stored as the
-!--                   sign of the logc-element.
-                      logc_w_s(2,k,i) = inc * lc
-                      logc_ratio_w_s(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
+                      IF ( lc == -99 )  THEN
+!                         
+!--                      The pivot point is out of subdomain, skip the correction. 
+                         logc_w_s(2,k,i) = 0
+                         logc_ratio_w_s(2,0:ncorr-1,k,i) = 1.0_wp
+                      ELSE
+!
+!--                      The direction of the wall-normal index is stored as the
+!--                      sign of the logc-element.
+                         logc_w_s(2,k,i) = inc * lc
+                         logc_ratio_w_s(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
+                      ENDIF
                       lcr(0:ncorr-1) = 1.0_wp
                    ENDDO
@@ -2414,9 +2495,16 @@
                            k, i, inc, wall_index, z0_topo, kb, direction,      &
                            ncorr )
-!
-!--                   The direction of the wall-normal index is stored as the
-!--                   sign of the logc-element.
-                      logc_w_s(2,k,i) = inc * lc
-                      logc_ratio_w_s(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
+                      IF ( lc == -99 )  THEN
+!                         
+!--                      The pivot point is out of subdomain, skip the correction. 
+                         logc_w_s(2,k,i) = 0
+                         logc_ratio_w_s(2,0:ncorr-1,k,i) = 1.0_wp
+                      ELSE
+!
+!--                      The direction of the wall-normal index is stored as the
+!--                      sign of the logc-element.
+                         logc_w_s(2,k,i) = inc * lc
+                         logc_ratio_w_s(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
+                      ENDIF
                       lcr(0:ncorr-1) = 1.0_wp
                    ENDDO
@@ -2456,9 +2544,16 @@
                            k, i, inc, wall_index, z0_topo, kb, direction,      &
                            ncorr )
-!
-!--                   The direction of the wall-normal index is stored as the
-!--                   sign of the logc-element.
-                      logc_v_n(2,k,i) = inc * lc
-                      logc_ratio_v_n(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
+                      IF ( lc == -99 )  THEN
+!                         
+!--                      The pivot point is out of subdomain, skip the correction. 
+                         logc_v_n(2,k,i) = 0
+                         logc_ratio_v_n(2,0:ncorr-1,k,i) = 1.0_wp
+                      ELSE
+!
+!--                      The direction of the wall-normal index is stored as the
+!--                      sign of the logc-element.
+                         logc_v_n(2,k,i) = inc * lc
+                         logc_ratio_v_n(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
+                      ENDIF
                       lcr(0:ncorr-1) = 1.0_wp
                    ENDDO
@@ -2478,9 +2573,16 @@
                            k, i, inc, wall_index, z0_topo, kb, direction,      &
                            ncorr )
-!
-!--                   The direction of the wall-normal index is stored as the
-!--                   sign of the logc-element.
-                      logc_v_n(2,k,i) = inc * lc
-                      logc_ratio_v_n(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
+                      IF ( lc == -99 )  THEN
+!                         
+!--                      The pivot point is out of subdomain, skip the correction. 
+                         logc_v_n(2,k,i) = 0
+                         logc_ratio_v_n(2,0:ncorr-1,k,i) = 1.0_wp
+                      ELSE
+!
+!--                      The direction of the wall-normal index is stored as the
+!--                      sign of the logc-element.
+                         logc_v_n(2,k,i) = inc * lc
+                         logc_ratio_v_n(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
+                      ENDIF
                       lcr(0:ncorr-1) = 1.0_wp
                    ENDDO
@@ -2505,9 +2607,16 @@
                            k, i, inc, wall_index, z0_topo, kb, direction,      &
                            ncorr )
-!
-!--                   The direction of the wall-normal index is stored as the
-!--                   sign of the logc-element.
-                      logc_w_n(2,k,i) = inc * lc
-                      logc_ratio_w_n(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
+                      IF ( lc == -99 )  THEN
+!                         
+!--                      The pivot point is out of subdomain, skip the correction. 
+                         logc_w_n(2,k,i) = 0
+                         logc_ratio_w_n(2,0:ncorr-1,k,i) = 1.0_wp
+                      ELSE
+!
+!--                      The direction of the wall-normal index is stored as the
+!--                      sign of the logc-element.
+                         logc_w_n(2,k,i) = inc * lc
+                         logc_ratio_w_n(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
+                      ENDIF
                       lcr(0:ncorr-1) = 1.0_wp
                    ENDDO
@@ -2527,9 +2636,16 @@
                            k, i, inc, wall_index, z0_topo, kb, direction,      &
                            ncorr )
-!
-!--                   The direction of the wall-normal index is stored as the
-!--                   sign of the logc-element.
-                      logc_w_n(2,k,i) = inc * lc
-                      logc_ratio_w_n(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
+                      IF ( lc == -99 )  THEN
+!                         
+!--                      The pivot point is out of subdomain, skip the correction. 
+                         logc_w_n(2,k,i) = 0
+                         logc_ratio_w_n(2,0:ncorr-1,k,i) = 1.0_wp
+                      ELSE
+!
+!--                      The direction of the wall-normal index is stored as the
+!--                      sign of the logc-element.
+                         logc_w_n(2,k,i) = inc * lc
+                         logc_ratio_w_n(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
+                      ENDIF
                       lcr(0:ncorr-1) = 1.0_wp
                    ENDDO
@@ -2646,4 +2762,7 @@
 
        END SELECT
+          
+       !write(9,"('pmci_define_loglaw_correction_parameters: ', 6(i3,2x))")    &
+       !     direction, ij, k, wall_index, inc, lc
 
     END SUBROUTINE pmci_define_loglaw_correction_parameters
@@ -2670,8 +2789,7 @@
        REAL(wp) ::  zuc1   !<
 
-
+!
+!--    kbc is the first coarse-grid point above the surface
        kbc = nzb + 1
-!
-!--    kbc is the first coarse-grid point above the surface
        DO  WHILE ( cg%zu(kbc) < zu(kb) )
           kbc = kbc + 1
@@ -2691,9 +2809,11 @@
     SUBROUTINE pmci_find_logc_pivot_j( lc, logyc1, j, jw, z0_l, inc )
 !
-!--    Finds the pivot node and te log-law factor for near-wall nodes for
+!--    Finds the pivot node and the log-law factor for near-wall nodes for
 !--    which the wall-parallel velocity components will be log-law corrected
 !--    after interpolation. This subroutine is only for vertical walls on
-!--    south/north sides of the node.
-
+!--    south/north sides of the node. If the pivot node is found to be outside 
+!--    the subdomain, a marker value of -99 is set to lc and this tells 
+!--    pmci_init_loglaw_correction to not do the correction in this case.
+      
        IMPLICIT NONE
 
@@ -2703,4 +2823,5 @@
        INTEGER(iwp), INTENT(OUT) ::  lc     !<
 
+       INTEGER(iwp) ::  jwc      !<
        INTEGER(iwp) ::  j1       !<
 
@@ -2708,22 +2829,35 @@
 
        REAL(wp) ::  logyc1   !<
+       REAL(wp) ::  ycb      !<       
        REAL(wp) ::  yc1      !<
        
 !
 !--    yc1 is the y-coordinate of the first coarse-grid u- and w-nodes out from
-!--    the wall
-       yc1  = coord_y(jw) + 0.5_wp * inc * cg%dy
-!
-!--    j1 is the first fine-grid index further away from the wall than yc1
-       j1 = j
-!
-!--    Important: must be <, not <=
-       DO  WHILE ( inc * ( coord_y(j1) + 0.5_wp * dy ) < inc * yc1 )
-          j1 = j1 + inc
-       ENDDO
-
-       logyc1 = LOG( ABS( coord_y(j1) + 0.5_wp * dy - coord_y(jw) ) / z0_l )
-       lc = j1
-
+!--    the wall. Here we assume that the wall index in the coarse grid is the
+!--    closest one if they don't match.
+       jwc  = pmci_find_nearest_coarse_grid_index_j( jw )
+       yc1  = cg%coord_y(jwc) - lower_left_coord_y + 0.5_wp * inc * cg%dy
+!       
+!--    Check if yc1 is out of the subdomain y-range. In such case set the marker 
+!--    value -99 for lc in order to skip the loglaw-correction locally.
+       IF ( yc1 < ( REAL( nysg, KIND=wp ) + 0.5_wp ) * dy  )  THEN
+          lc = -99
+       ELSE IF ( yc1 > ( REAL( nyng, KIND=wp ) + 0.5_wp ) * dy )  THEN
+          lc = -99
+       ELSE
+!
+!--       j1 is the first fine-grid index further away from the wall than yc1
+          j1 = j
+!
+!--       Important: the binary relation must be <, not <=
+          ycb = 0.5_wp * dy - lower_left_coord_y
+          DO  WHILE ( inc * ( coord_y(j1) + ycb ) < inc * yc1 )
+             j1 = j1 + inc
+          ENDDO
+          
+          logyc1 = LOG( ABS( coord_y(j1) + 0.5_wp * dy - coord_y(jw) ) / z0_l )
+          lc = j1
+       ENDIF
+       
     END SUBROUTINE pmci_find_logc_pivot_j
 
@@ -2735,5 +2869,7 @@
 !--    which the wall-parallel velocity components will be log-law corrected
 !--    after interpolation. This subroutine is only for vertical walls on
-!--    south/north sides of the node.
+!--    left/right sides of the node. If the pivot node is found to be outside 
+!--    the subdomain, a marker value of -99 is set to lc and this tells 
+!--    pmci_init_loglaw_correction to not do the correction in this case.
 
        IMPLICIT NONE
@@ -2744,4 +2880,5 @@
        INTEGER(iwp), INTENT(OUT) ::  lc     !<
 
+       INTEGER(iwp) ::  iwc      !<
        INTEGER(iwp) ::  i1       !<
 
@@ -2749,27 +2886,87 @@
 
        REAL(wp) ::  logxc1   !<
+       REAL(wp) ::  xcb      !<
        REAL(wp) ::  xc1      !<
 
 !
 !--    xc1 is the x-coordinate of the first coarse-grid v- and w-nodes out from
-!--    the wall
-       xc1  = coord_x(iw) + 0.5_wp * inc * cg%dx
-!
-!--    i1 is the first fine-grid index futher away from the wall than xc1.
-       i1 = i
-!
-!--    Important: must be <, not <=
-       DO  WHILE ( inc * ( coord_x(i1) + 0.5_wp *dx ) < inc * xc1 )
-          i1 = i1 + inc
-       ENDDO
+!--    the wall. Here we assume that the wall index in the coarse grid is the
+!--    closest one if they don't match.
+       iwc  = pmci_find_nearest_coarse_grid_index_i( iw )
+       xc1  = cg%coord_x(iwc) - lower_left_coord_x + 0.5_wp * inc * cg%dx
+!       
+!--    Check if xc1 is out of the subdomain x-range. In such case set the marker 
+!--    value -99 for lc in order to skip the loglaw-correction locally.        
+       IF ( xc1 < ( REAL( nxlg, KIND=wp ) + 0.5_wp ) * dx  )  THEN
+          lc = -99
+       ELSE IF ( xc1 > ( REAL( nxrg, KIND=wp ) + 0.5_wp ) * dx )  THEN
+          lc = -99
+       ELSE    
+!
+!--       i1 is the first fine-grid index futher away from the wall than xc1.
+          i1 = i
+!
+!--       Important: the binary relation must be <, not <= 
+          xcb = 0.5_wp * dx - lower_left_coord_x
+          DO  WHILE ( inc * ( coord_x(i1) + xcb ) < inc * xc1 )
+             i1 = i1 + inc
+          ENDDO
       
-       logxc1 = LOG( ABS( coord_x(i1) + 0.5_wp*dx - coord_x(iw) ) / z0_l )
-       lc = i1
-
+          logxc1 = LOG( ABS( coord_x(i1) + 0.5_wp*dx - coord_x(iw) ) / z0_l )
+          lc = i1
+       ENDIF
+       
     END SUBROUTINE pmci_find_logc_pivot_i
 
 
-
-
+    
+    FUNCTION pmci_find_nearest_coarse_grid_index_j( jw ) 
+
+      IMPLICIT NONE
+      INTEGER(iwp) :: jw         !< Fine-grid wall index
+
+      INTEGER(iwp) :: jc
+      INTEGER(iwp) :: pmci_find_nearest_coarse_grid_index_j
+      REAL(wp) :: dist
+      REAL(wp) :: newdist
+
+      
+      dist = coord_y(nyn) - coord_y(nys)
+      DO jc = jcs, jcn
+         newdist = ABS( cg%coord_y(jc) - coord_y(jw) )
+         IF ( newdist < dist )  THEN
+            pmci_find_nearest_coarse_grid_index_j = jc
+            dist = newdist
+         ENDIF
+      ENDDO
+      
+    END FUNCTION pmci_find_nearest_coarse_grid_index_j
+
+
+    
+    FUNCTION pmci_find_nearest_coarse_grid_index_i( iw ) 
+
+      IMPLICIT NONE
+      INTEGER(iwp) :: iw         !< Fine-grid wall index
+
+      INTEGER(iwp) :: ic
+      INTEGER(iwp) :: pmci_find_nearest_coarse_grid_index_i
+      REAL(wp) :: dist
+      REAL(wp) :: newdist
+
+      
+      dist = coord_x(nxr) - coord_x(nxl)
+      DO ic = icl, icr
+         newdist = ABS( cg%coord_x(ic) - coord_x(iw) )
+         IF ( newdist < dist )  THEN
+            pmci_find_nearest_coarse_grid_index_i = ic
+            dist = newdist
+         ENDIF
+      ENDDO
+      
+    END FUNCTION pmci_find_nearest_coarse_grid_index_i
+
+    
+      
     SUBROUTINE pmci_init_anterp_tophat
 !
Index: /palm/trunk/SOURCE/time_integration.f90
===================================================================
--- /palm/trunk/SOURCE/time_integration.f90	(revision 2983)
+++ /palm/trunk/SOURCE/time_integration.f90	(revision 2984)
@@ -25,7 +25,6 @@
 ! -----------------
 ! $Id$
-! Implement changes from branch radiation (r2948-2971) with minor modifications.
-! (moh.hefny)
-! Fixed bug in if statement
+! CALL pmci_ensure_nest_mass_conservation is removed (so far only commented out)
+! as seemingly unnecessary.
 ! 
 ! 2941 2018-04-03 11:54:58Z kanani
@@ -812,7 +811,9 @@
 !--          This action must never be done for the root domain. Vertical 
 !--          nesting implies mass conservation.
-             IF ( nest_domain )  THEN
-                CALL pmci_ensure_nest_mass_conservation
-             ENDIF
+!--          Commented out April 18, 2018 as seemingly unnecessary. 
+!--          Will later be completely removed.
+!--             IF ( nest_domain )  THEN
+!--                CALL pmci_ensure_nest_mass_conservation
+!--             ENDIF
 
              CALL cpu_log( log_point(60), 'nesting', 'stop' )
@@ -1039,5 +1040,5 @@
                 CALL cpu_log( log_point(50), 'radiation', 'stop' )
 
-                IF ( ( urban_surface  .OR.  land_surface )  .AND.               &
+                IF ( urban_surface  .OR.  land_surface  .AND.                  &
                      radiation_interactions )  THEN
                    CALL cpu_log( log_point(75), 'radiation_interaction', 'start' )
