Index: /palm/trunk/SOURCE/Makefile
===================================================================
--- /palm/trunk/SOURCE/Makefile	(revision 666)
+++ /palm/trunk/SOURCE/Makefile	(revision 667)
@@ -4,5 +4,5 @@
 # Current revisions:
 # ------------------
-# 
+# +advec_ws
 #
 # Former revisions:
@@ -57,18 +57,18 @@
 
 RCS = advec_particles.f90 advec_s_bc.f90 advec_s_pw.f90 advec_s_up.f90 \
-	advec_s_ups.f90 advec_u_pw.f90 advec_u_up.f90 advec_u_ups.f90 \
-	advec_v_pw.f90 advec_v_up.f90 advec_v_ups.f90 advec_w_pw.f90 \
-	advec_w_up.f90 advec_w_ups.f90 asselin_filter.f90 average_3d_data.f90 \
-	boundary_conds.f90 buoyancy.f90 calc_liquid_water_content.f90 \
-	calc_precipitation.f90 calc_radiation.f90 calc_spectra.f90 \
-	check_for_restart.f90 check_open.f90 check_parameters.f90 \
-	close_file.f90 compute_vpt.f90 coriolis.f90 cpu_log.f90 \
-	cpu_statistics.f90 data_log.f90 data_output_dvrp.f90 \
-	data_output_mask.f90 data_output_profiles.f90 data_output_ptseries.f90 \
-	data_output_spectra.f90 data_output_tseries.f90 data_output_2d.f90 \
-	data_output_3d.f90 diffusion_e.f90 diffusion_s.f90 diffusion_u.f90 \
-	diffusion_v.f90 diffusion_w.f90 diffusivities.f90 disturb_field.f90 \
-	disturb_heatflux.f90 eqn_state_seawater.f90 exchange_horiz.f90 \
-	exchange_horiz_2d.f90 \
+        advec_ws.f90 advec_s_ups.f90 advec_u_pw.f90 advec_u_up.f90 \
+        advec_u_ups.f90 advec_v_pw.f90 advec_v_up.f90 advec_v_ups.f90 \
+        advec_w_pw.f90 advec_w_up.f90 advec_w_ups.f90 asselin_filter.f90 \
+        average_3d_data.f90 boundary_conds.f90 buoyancy.f90 \
+        calc_liquid_water_content.f90 calc_precipitation.f90 \
+        calc_radiation.f90 calc_spectra.f90 check_for_restart.f90 \
+        check_open.f90 check_parameters.f90 close_file.f90 compute_vpt.f90 \
+        coriolis.f90 cpu_log.f90 cpu_statistics.f90 data_log.f90 \
+        data_output_dvrp.f90 data_output_mask.f90 data_output_profiles.f90 \
+        data_output_ptseries.f90 data_output_spectra.f90 data_output_tseries.f90 \
+        data_output_2d.f90 data_output_3d.f90 diffusion_e.f90 diffusion_s.f90 \
+        diffusion_u.f90 diffusion_v.f90 diffusion_w.f90 diffusivities.f90 \
+        disturb_field.f90 disturb_heatflux.f90 eqn_state_seawater.f90 \
+        exchange_horiz.f90 exchange_horiz_2d.f90 \
 	fft_xy.f90 flow_statistics.f90 global_min_max.f90 \
 	header.f90 impact_of_latent_heat.f90 inflow_turbulence.f90 \
@@ -77,6 +77,6 @@
 	init_masks.f90 init_ocean.f90 init_particles.f90 init_pegrid.f90 \
 	init_pt_anomaly.f90 init_rankine.f90 init_slope.f90 \
-	interaction_droplets_ptq.f90 local_flush.f90 local_getenv.f90 \
-	local_stop.f90 local_system.f90 local_tremain.f90 \
+	interaction_droplets_ptq.f90 local_flush.f90 \
+        local_getenv.f90 local_stop.f90 local_system.f90 local_tremain.f90 \
 	local_tremain_ini.f90 message.f90 modules.f90 netcdf.f90 \
 	package_parin.f90 palm.f90 parin.f90 particle_boundary_conds.f90 \
@@ -106,5 +106,5 @@
 OBJS =  advec_particles.o advec_s_bc.o advec_s_pw.o advec_s_up.o \
 	advec_s_ups.o advec_u_pw.o advec_u_up.o advec_u_ups.o \
-	advec_v_pw.o advec_v_up.o advec_v_ups.o advec_w_pw.o \
+        advec_ws.o advec_v_pw.o advec_v_up.o advec_v_ups.o advec_w_pw.o \
 	advec_w_up.o advec_w_ups.o asselin_filter.o average_3d_data.o \
 	boundary_conds.o buoyancy.o calc_liquid_water_content.o \
@@ -184,4 +184,5 @@
 advec_v_up.o: modules.o
 advec_v_ups.o: modules.o
+advec_ws.o: modules.o
 advec_w_pw.o: modules.o
 advec_w_up.o: modules.o
@@ -230,5 +231,5 @@
 inflow_turbulence.o: modules.o
 init_1d_model.o: modules.o
-init_3d_model.o: modules.o random_function.o
+init_3d_model.o: modules.o random_function.o advec_ws.o
 init_advec.o: modules.o
 init_cloud_physics.o: modules.o
@@ -264,4 +265,5 @@
 production_e.o: modules.o wall_fluxes.o
 prognostic_equations.o: modules.o advec_s_pw.o advec_s_up.o advec_u_pw.o \
+        advec_ws.o \
 	advec_u_up.o advec_v_pw.o advec_v_up.o advec_w_pw.o advec_w_up.o  \
 	buoyancy.o calc_precipitation.o calc_radiation.o coriolis.o \
Index: /palm/trunk/SOURCE/advec_particles.f90
===================================================================
--- /palm/trunk/SOURCE/advec_particles.f90	(revision 666)
+++ /palm/trunk/SOURCE/advec_particles.f90	(revision 667)
@@ -4,4 +4,6 @@
 ! Current revisions:
 ! -----------------
+! Declaration of de_dx, de_dy, de_dz adapted to additional 
+! ghost points. Furthermore the calls of exchange_horiz were modified.
 ! 
 ! TEST: PRINT statements on unit 9 (commented out)
@@ -153,5 +155,5 @@
     REAL    ::  location(1:30,1:3)
 
-    REAL, DIMENSION(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) ::  de_dx, de_dy, de_dz
+    REAL, DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  de_dx, de_dy, de_dz
 
     REAL, DIMENSION(:,:,:), ALLOCATABLE ::  trlpt, trnpt, trrpt, trspt
@@ -768,8 +770,8 @@
 !
 !--    Lateral boundary conditions
-       CALL exchange_horiz( de_dx )
-       CALL exchange_horiz( de_dy )
-       CALL exchange_horiz( de_dz )
-       CALL exchange_horiz( diss  )
+       CALL exchange_horiz( de_dx, nbgp )
+       CALL exchange_horiz( de_dy, nbgp )
+       CALL exchange_horiz( de_dz, nbgp )
+       CALL exchange_horiz( diss, nbgp  )
 
 !
Index: /palm/trunk/SOURCE/advec_w_pw.f90
===================================================================
--- /palm/trunk/SOURCE/advec_w_pw.f90	(revision 666)
+++ /palm/trunk/SOURCE/advec_w_pw.f90	(revision 667)
@@ -89,5 +89,4 @@
        REAL    ::  gu, gv
 
-
        gu = 2.0 * u_gtrans
        gv = 2.0 * v_gtrans
@@ -103,5 +102,4 @@
                                                 )
        ENDDO
-
     END SUBROUTINE advec_w_pw_ij
 
Index: /palm/trunk/SOURCE/advec_ws.f90
===================================================================
--- /palm/trunk/SOURCE/advec_ws.f90	(revision 667)
+++ /palm/trunk/SOURCE/advec_ws.f90	(revision 667)
@@ -0,0 +1,3074 @@
+ MODULE advec_ws
+
+
+!-----------------------------------------------------------------------------!
+! Current revisions:
+! -----------------
+! 
+!
+! Former revisions:
+! -----------------
+! $Id: advec_s_ws.f90 411 2009-12-11 12:31:43 Z suehring $
+!
+!
+! Initial revision
+!
+! Description:
+! ------------
+! Advection scheme for scalars and momentum using the flux formulation of
+! Wicker and Skamarock 5th order. Additionally the module contains of a 
+! routine using for initialisation and steering of the statical evaluation. 
+! The computation of turbulent fluxes takes place inside the advection
+! routines. 
+! In case of vector architectures Dirichlet and Radiation boundary conditions
+! are outstanding and not available.
+! A further routine local_diss_ij is available (next weeks) and is used if a 
+! control of dissipative fluxes is desired.
+! In case of vertical grid stretching the order of advection scheme is
+! degraded. This is also realized for the ocean where the stretching is 
+! downwards.
+!
+! OUTSTANDING: - Dirichlet and Radiation boundary conditions for 
+!                vector architectures
+!              - dissipation control for cache architectures ( next weeks )
+!              - Topography ( next weeks )
+!-----------------------------------------------------------------------------!
+
+
+    PRIVATE
+    PUBLIC   advec_s_ws, advec_u_ws, advec_v_ws, advec_w_ws, &
+             local_diss, ws_init, ws_statistics
+
+    INTERFACE ws_init
+       MODULE PROCEDURE ws_init
+    END INTERFACE ws_init
+    INTERFACE ws_statistics
+       MODULE PROCEDURE ws_statistics
+    END INTERFACE ws_statistics
+    INTERFACE advec_s_ws
+       MODULE PROCEDURE advec_s_ws
+       MODULE PROCEDURE advec_s_ws_ij
+    END INTERFACE advec_s_ws
+    INTERFACE advec_u_ws
+       MODULE PROCEDURE advec_u_ws
+       MODULE PROCEDURE advec_u_ws_ij
+    END INTERFACE advec_u_ws
+    INTERFACE advec_v_ws
+       MODULE PROCEDURE advec_v_ws
+       MODULE PROCEDURE advec_v_ws_ij
+    END INTERFACE advec_v_ws
+    INTERFACE advec_w_ws
+       MODULE PROCEDURE advec_w_ws
+       MODULE PROCEDURE advec_w_ws_ij
+    END INTERFACE advec_w_ws
+    INTERFACE local_diss
+       MODULE PROCEDURE local_diss
+       MODULE PROCEDURE local_diss_ij
+    END INTERFACE local_diss
+
+ CONTAINS
+
+!
+!-- Initialisation
+
+    SUBROUTINE ws_init
+       USE arrays_3d
+       USE constants
+       USE control_parameters
+       USE indices
+       USE statistics
+
+!       
+!--    Set the LOGICALS to enhance the performance.
+       IF ( momentum_advec == 'ws-scheme' )  ws_scheme_mom = .TRUE.
+       IF ( scalar_advec == 'ws-scheme' )  ws_scheme_sca = .TRUE.
+!
+!--    Allocate arrays needed for dissipation control.
+       IF ( dissipation_control )  THEN
+   !          ALLOCATE(var_x(nzb+1:nzt,nys:nyn,nxl:nxr),  &
+    !                  var_y(nzb+1:nzt,nys:nyn,nxl:nxr),  &
+     !                 var_z(nzb+1:nzt,nys:nyn,nxl:nxr),  &
+      !                gamma_x(nzb:nzt+1,nysg:nyng,nxlg:nxrg),  &
+       !               gamma_y(nzb:nzt+1,nysg:nyng,nxlg:nxrg),  &
+        !              gamma_z(nzb:nzt+1,nysg:nyng,nxlg:nxrg))
+       ENDIF
+              
+!--    Set the appropriate factors for scalar and momentum advection.
+       adv_sca_5 = 0.016666666666666
+       adv_sca_3 = 0.083333333333333 
+       adv_mom_5 = 0.0083333333333333
+       adv_mom_3 = 0.041666666666666
+!
+!--    Allocate arrays needed for statistical evaluation of fluxes when 
+!--    ws-scheme is used.
+!--    The following array contains the weighting factors of the RK3 average
+!--    of tendecies. 
+       ALLOCATE (weight_substep(1:intermediate_timestep_count_max) )
+       IF ( intermediate_timestep_count_max == 3)  THEN   !RK3
+          weight_substep(1) = 0.166666666666666
+          weight_substep(2) = 0.3
+          weight_substep(3) = 0.533333333333333
+       ENDIF
+!         
+!--    Arrays needed for statical evaluation of fluxes.
+       IF ( ws_scheme_mom )  THEN
+          ALLOCATE( sums_wsus_ws_l(nzb:nzt+1,0:statistic_regions),         &
+              sums_wsvs_ws_l(nzb:nzt+1,0:statistic_regions),               &
+              sums_us2_ws_l(nzb:nzt+1,0:statistic_regions),                &
+              sums_vs2_ws_l(nzb:nzt+1,0:statistic_regions),                &
+              sums_ws2_ws_l(nzb:nzt+1,0:statistic_regions))
+
+          sums_wsus_ws_l = 0.0
+          sums_wsvs_ws_l = 0.0
+          sums_us2_ws_l  = 0.0
+          sums_vs2_ws_l  = 0.0
+          sums_ws2_ws_l  = 0.0
+       ENDIF
+
+       IF ( ws_scheme_sca )  THEN
+          ALLOCATE( sums_wspts_ws_l(nzb:nzt+1,0:statistic_regions))
+          sums_wspts_ws_l = 0.0
+          IF ( humidity  .OR.  passive_scalar )  THEN
+             ALLOCATE( sums_wsqs_ws_l(nzb:nzt+1,0:statistic_regions))
+             sums_wsqs_ws_l = 0.0
+          ENDIF
+          IF ( ocean )  THEN
+             ALLOCATE( sums_wssas_ws_l(nzb:nzt+1,0:statistic_regions))
+             sums_wssas_ws_l = 0.0
+          ENDIF
+       ENDIF
+!
+!--    Arrays needed for reasons of speed optimization for cache and noopt 
+!--    version. For the vector version the buffer arrays are not necessary, 
+!--    because the the fluxes can swapped directly inside the loops of the 
+!--    advection routines.
+       IF ( loop_optimization /= 'vector' )  THEN
+          IF ( ws_scheme_mom )  THEN
+             ALLOCATE(flux_s_u(nzb+1:nzt), flux_s_v(nzb+1:nzt),            &
+                flux_s_w(nzb+1:nzt), diss_s_u(nzb+1:nzt),                  &
+                diss_s_v(nzb+1:nzt), diss_s_w(nzb+1:nzt))
+             ALLOCATE(flux_l_u(nzb+1:nzt,nys:nyn),                         &
+                flux_l_v(nzb+1:nzt,nys:nyn), flux_l_w(nzb+1:nzt,nys:nyn),  & 
+                diss_l_u(nzb+1:nzt,nys:nyn), diss_l_v(nzb+1:nzt,nys:nyn),  & 
+                diss_l_w(nzb+1:nzt,nys:nyn)) 
+          ENDIF
+          IF ( ws_scheme_sca )  THEN
+             ALLOCATE(flux_s_pt(nzb+1:nzt), flux_s_e(nzb+1:nzt),           &
+                diss_s_pt(nzb+1:nzt), diss_s_e(nzb+1:nzt))
+             ALLOCATE(flux_l_pt(nzb+1:nzt,nys:nyn),                        &
+                flux_l_e(nzb+1:nzt,nys:nyn), diss_l_pt(nzb+1:nzt,nys:nyn), & 
+                diss_l_e(nzb+1:nzt,nys:nyn))
+             IF ( humidity  .OR.  passive_scalar )  THEN
+                ALLOCATE(flux_s_q(nzb+1:nzt), diss_s_q(nzb+1:nzt))
+                ALLOCATE(flux_l_q(nzb+1:nzt,nys:nyn),                      &
+                   diss_l_q(nzb+1:nzt,nys:nyn))
+             ENDIF
+             IF ( ocean )  THEN
+                ALLOCATE(flux_s_sa(nzb+1:nzt), diss_s_sa(nzb+1:nzt))
+                ALLOCATE(flux_l_sa(nzb+1:nzt,nys:nyn),                     &
+                   diss_l_sa(nzb+1:nzt,nys:nyn))
+             ENDIF
+          ENDIF
+       ENDIF
+!
+!--    Determine the flags where the order of the scheme for horizontal 
+!--    advection should be degraded.
+       ALLOCATE( boundary_flags(nys:nyn,nxl:nxr) )
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             boundary_flags(j,i) = 0
+             IF ( outflow_l )  THEN
+                IF ( i == nxlu )  boundary_flags(j,i) = 5
+                IF ( i == nxl )  boundary_flags(j,i) = 6
+             ELSEIF ( outflow_r )  THEN
+                IF ( i == nxr-1 )  boundary_flags(j,i) = 1
+                IF ( i == nxr )  boundary_flags(j,i) = 2
+             ELSEIF ( outflow_n )  THEN
+                IF ( j == nyn-1 )  boundary_flags(j,i) = 3
+                IF ( j == nyn )  boundary_flags(j,i) = 4
+             ELSEIF ( outflow_s )  THEN
+                IF ( j == nysv )  boundary_flags(j,i) = 7
+                IF ( j == nys )  boundary_flags(j,i) = 8
+             ENDIF
+          ENDDO
+       ENDDO
+       
+    END SUBROUTINE ws_init
+    
+    SUBROUTINE ws_statistics
+    
+       USE control_parameters
+       USE statistics
+
+!       
+!--      The arrays needed for statistical evaluation are set to to 0 at the 
+!--      begin of prognostic_equations.
+          IF ( ws_scheme_mom )  THEN
+             sums_wsus_ws_l = 0.0
+             sums_wsvs_ws_l = 0.0
+             sums_us2_ws_l = 0.0
+             sums_vs2_ws_l = 0.0
+             sums_ws2_ws_l = 0.0
+          ENDIF
+          IF ( ws_scheme_sca )  THEN
+             sums_wspts_ws_l = 0.0
+             IF ( humidity  .OR.  passive_scalar )  sums_wsqs_ws_l = 0.0
+             IF ( ocean )  sums_wssas_ws_l = 0.0
+          ENDIF
+
+    END SUBROUTINE ws_statistics
+
+
+
+!------------------------------------------------------------------------------!
+! Scalar advection - Call for grid point i,j
+!------------------------------------------------------------------------------!
+    SUBROUTINE advec_s_ws_ij( i, j, sk, sk_char,swap_flux_y_local,  &
+               swap_diss_y_local, swap_flux_x_local, swap_diss_x_local  )
+
+       USE arrays_3d
+       USE constants
+       USE control_parameters
+       USE grid_variables
+       USE indices
+       USE statistics
+
+       IMPLICIT NONE
+
+       INTEGER ::  i, j, k
+       LOGICAL :: degraded_l = .FALSE., degraded_s = .FALSE.
+       REAL    ::  flux_d, diss_d, u_comp, v_comp
+       REAL, DIMENSION(:,:,:), POINTER    :: sk
+       REAL, DIMENSION(nzb:nzt+1)         :: flux_t, diss_t, flux_r, diss_r,  &
+                                             flux_n, diss_n
+       REAL, DIMENSION(nzb+1:nzt)         :: swap_flux_y_local,               &       
+                                             swap_diss_y_local
+       REAL, DIMENSION(nzb+1:nzt,nys:nyn) :: swap_flux_x_local,               &
+                                             swap_diss_x_local
+       CHARACTER (LEN = *), INTENT(IN)    :: sk_char
+
+       IF ( boundary_flags(j,i) /= 0 )  THEN
+!       
+!--      Degrade the order for Dirichlet bc. at the outflow boundary.
+         SELECT CASE ( boundary_flags(j,i) )
+         
+            CASE ( 1 )
+               DO  k = nzb_s_inner(j,i) + 1, nzt
+                  u_comp    = u(k,j,i+1) - u_gtrans
+                  flux_r(k) = u_comp * (                                      &
+                              7. * ( sk(k,j,i+1) + sk(k,j,i)   )              &
+                              -    ( sk(k,j,i+2) + sk(k,j,i-1) ) ) * adv_sca_3
+                  diss_r(k) = - abs(u_comp) * (                               &
+                              3. * ( sk(k,j,i+1)-sk(k,j,i)     )              & 
+                              -    ( sk(k,j,i+2) - sk(k,j,i-1) ) ) * adv_sca_3
+               ENDDO
+               
+            CASE ( 2 )
+               DO  k = nzb_s_inner(j,i) + 1, nzt
+                  u_comp    = u(k,j,i+1) - u_gtrans
+                  flux_r(k) = u_comp * ( sk(k,j,i+1) + sk(k,j,i) ) * 0.5
+                  diss_r(k) = diss_2nd( sk(k,j,i+1), sk(k,j,i+1), sk(k,j,i),  &
+                                        sk(k,j,i-1), sk(k,j,i-2), u_comp,     &
+                                        0.5, ddx )
+              ENDDO
+              
+            CASE ( 3 )
+               DO  k = nzb_s_inner(j,i) + 1, nzt
+                  v_comp = v(k,j+1,i) - v_gtrans
+                  flux_n(k) = v_comp * (                                      &
+                              7. * ( sk(k,j+1,i) + sk(k,j,i)   )              &
+                              -    ( sk(k,j+2,i) + sk(k,j-1,i) ) ) * adv_sca_3
+                  diss_n(k) = - abs(v_comp) * (                               &
+                              3. * ( sk(k,j+1,i)-sk(k,j,i)    )               & 
+                              -    (sk(k,j+2,i) - sk(k,j-1,i) ) ) * adv_sca_3
+              ENDDO
+            CASE ( 4 )
+               DO  k = nzb_s_inner(j,i) + 1, nzt
+                  v_comp    = v(k,j+1,i) - v_gtrans
+                  flux_n(k) = v_comp* ( sk(k,j+1,i) + sk(k,j,i) ) * 0.5
+                  diss_n(k) =  diss_2nd( sk(k,j+1,i), sk(k,j+1,i), sk(k,j,i), &
+                                         sk(k,j-1,i), sk(k,j-2,i), v_comp,    &
+                                         0.5, ddy )     
+               ENDDO
+               
+            CASE ( 5 )
+               DO  k = nzb_w_inner(j,i) + 1, nzt
+                  u_comp    = u(k,j,i+1) - u_gtrans 
+                  flux_r(k) = u_comp  * (                                     &
+                              7. * ( sk(k,j,i+1) + sk(k,j,i)   )              &
+                              -    ( sk(k,j,i+2) + sk(k,j,i-1) ) ) * adv_sca_3
+                  diss_r(k) = - abs(u_comp) * (                               &
+                              3. * ( sk(k,j,i+1)-sk(k,j,i) )                  & 
+                              -    ( sk(k,j,i+2) - sk(k,j,i-1) ) ) * adv_sca_3
+               ENDDO  
+               
+            CASE ( 6 )
+               DO  k = nzb_s_inner(j,i) + 1, nzt
+                  u_comp    = u(k,j,i+1) - u_gtrans
+                  flux_r(k) = u_comp * (                                      &
+                              7. * ( sk(k,j,i+1) + sk(k,j,i)   )              &
+                              -    ( sk(k,j,i+2) + sk(k,j,i-1) ) ) * adv_sca_3
+                  diss_r(k) = - abs(u_comp) * (                               &
+                              3. * ( sk(k,j,i+1)-sk(k,j,i)     )              & 
+                              -    ( sk(k,j,i+2) - sk(k,j,i-1) ) ) * adv_sca_3
+!                              
+!--               Compute leftside fluxes for the left boundary of PE domain.
+                  u_comp                 = u(k,j,i) - u_gtrans
+                  swap_flux_x_local(k,j) = u_comp * (                         &
+                                           sk(k,j,i) + sk(k,j,i-1) ) * 0.5
+                  swap_diss_x_local(k,j) = diss_2nd( sk(k,j,i+2),sk(k,j,i+1), &
+                                                     sk(k,j,i), sk(k,j,i-1),  &
+                                                     sk(k,j,i-1), u_comp,     &
+                                                     0.5, ddx ) 
+               ENDDO
+               degraded_l = .TRUE.
+               
+            CASE ( 7 )
+               DO  k = nzb_s_inner(j,i) + 1, nzt
+                  v_comp    = v(k,j+1,i)-v_gtrans
+                  flux_n(k) = v_comp * (                                      &
+                              7. * ( sk(k,j+1,i) + sk(k,j,i)   )              &
+                              -    ( sk(k,j+2,i) + sk(k,j-1,i) ) ) * adv_sca_3
+                  diss_n(k) = - abs(v_comp) * (                               &
+                              3. * ( sk(k,j+1,i) - sk(k,j,i)   )              & 
+                              -    ( sk(k,j+2,i) - sk(k,j-1,i) ) ) * adv_sca_3
+               ENDDO
+               
+            CASE ( 8 )
+               DO  k = nzb_s_inner(j,i) + 1, nzt
+                  v_comp    = v(k,j+1,i) - v_gtrans 
+                  flux_n(k) = v_comp * (                                      &
+                              7. * ( sk(k,j+1,i) + sk(k,j,i)   )              &
+                              -    ( sk(k,j+2,i) + sk(k,j-1,i) ) ) * adv_sca_3
+                  diss_n(k) = - abs(v_comp) * (                               &
+                              3. * ( sk(k,j+1,i) - sk(k,j,i) )                & 
+                              -    ( sk(k,j+2,i) - sk(k,j-1,i) ) ) * adv_sca_3
+!                              
+!--               Compute southside fluxes for the south boundary of PE domain           
+                  v_comp               = v(k,j,i) - v_gtrans
+                  swap_flux_y_local(k) = v_comp * (                           &
+                                         sk(k,j,i)+ sk(k,j-1,i) ) * 0.5 
+                  swap_diss_y_local(k) = diss_2nd( sk(k,j+2,i), sk(k,j+1,i),  &
+                                                   sk(k,j,i), sk(k,j-1,i),    &
+                                                   sk(k,j-1,i), v_comp,       &
+                                                   0.5, ddy )
+               ENDDO
+               degraded_s = .TRUE.
+               
+            CASE DEFAULT
+            
+         END SELECT
+!         
+!--      Compute the crosswise 5th order fluxes at the outflow
+         IF ( boundary_flags(j,i) == 1 .OR. boundary_flags(j,i) == 2          &
+         .OR. boundary_flags(j,i) == 5 .OR. boundary_flags(j,i) == 6 )  THEN
+            DO  k = nzb_s_inner(j,i) + 1, nzt
+               v_comp    = v(k,j+1,i) - v_gtrans
+               flux_n(k) = v_comp * (                                         &
+                           37.  * ( sk(k,j+1,i) + sk(k,j,i)   )               &
+                           - 8. * ( sk(k,j+2,i) + sk(k,j-1,i) )               &
+                           +      ( sk(k,j+3,i) + sk(k,j-2,i) ) ) * adv_sca_5
+               diss_n(k) = - abs(v_comp) * (                                  &
+                           10. *  ( sk(k,j+1,i) - sk(k,j,i)   )               &
+                           - 5. * ( sk(k,j+2,i) - sk(k,j-1,i) )               &
+                           +      ( sk(k,j+3,i) - sk(k,j-2,i) ) ) * adv_sca_5
+            ENDDO
+            
+         ELSE
+         
+            DO  k = nzb_s_inner(j,i) + 1, nzt
+               u_comp    = u(k,j,i+1) - u_gtrans  
+               flux_r(k) = u_comp * (                                         &
+                           37.  * ( sk(k,j,i+1) + sk(k,j,i)   )               &
+                           - 8. * ( sk(k,j,i+2) + sk(k,j,i-1) )               &
+                           +      ( sk(k,j,i+3) + sk(k,j,i-2) ) ) * adv_sca_5
+               diss_r(k) = - abs(u_comp) * (                                  &
+                           10.  * ( sk(k,j,i+1) - sk(k,j,i)   )               &
+                           - 5. * ( sk(k,j,i+2) - sk(k,j,i-1) )               &
+                           +      ( sk(k,j,i+3) - sk(k,j,i-2) ) ) * adv_sca_5
+            ENDDO
+            
+         ENDIF
+!
+!--    Compute the fifth order fluxes for the interior of PE domain.
+       ELSE
+               
+          DO  k = nzb_u_inner(j,i) + 1, nzt
+             u_comp    = u(k,j,i+1) - u_gtrans
+             flux_r(k) = u_comp * (                                           &
+                         37.  * ( sk(k,j,i+1) + sk(k,j,i)   )                 &
+                         - 8. * ( sk(k,j,i+2) + sk(k,j,i-1) )                 &
+                         +      ( sk(k,j,i+3) + sk(k,j,i-2) ) ) * adv_sca_5
+             diss_r(k) = - abs(u_comp) * (                                    &
+                         10.  * ( sk(k,j,i+1) - sk(k,j,i)   )                 &
+                         - 5. * ( sk(k,j,i+2) - sk(k,j,i-1) )                 &
+                         +      ( sk(k,j,i+3) - sk(k,j,i-2) ) ) * adv_sca_5
+
+             v_comp    = v(k,j+1,i) - v_gtrans
+             flux_n(k) = v_comp * (                                           &
+                         37.  * ( sk(k,j+1,i) + sk(k,j,i)   )                 &
+                         - 8. * ( sk(k,j+2,i) + sk(k,j-1,i) )                 &
+                         +      ( sk(k,j+3,i) + sk(k,j-2,i) ) ) * adv_sca_5
+             diss_n(k) = - abs(v_comp) * (                                    &
+                         10.  * ( sk(k,j+1,i) - sk(k,j,i)   )                 &                      
+                         - 5. * ( sk(k,j+2,i) - sk(k,j-1,i) )                 &
+                         +      ( sk(k,j+3,i) - sk(k,j-2,i) ) ) * adv_sca_5               
+          ENDDO
+          
+       ENDIF
+!       
+!--    Compute left- and southside fluxes of the respective PE bounds.        
+       IF ( j == nys .AND. ( .NOT. degraded_s ) )  THEN
+       
+           DO  k = nzb_s_inner(j,i) + 1, nzt
+              v_comp               = v(k,j,i) - v_gtrans
+              swap_flux_y_local(k) = v_comp * (                               &
+                                     37. *    ( sk(k,j,i) + sk(k,j-1,i)   )   &
+                                     - 8. *   ( sk(k,j+1,i) + sk(k,j-2,i) )   &
+                                     +        ( sk(k,j+2,i) + sk(k,j-3,i) )   &
+                                     ) * adv_sca_5
+              swap_diss_y_local(k) = - abs(v_comp) * (                        &
+                                     10. *  ( sk(k,j,i) - sk(k,j-1,i)   )     &
+                                     - 5. * ( sk(k,j+1,i) - sk(k,j-2,i) )     &
+                                     +        sk(k,j+2,i) - sk(k,j-3,i)       &
+                                     ) * adv_sca_5
+           ENDDO           
+          
+        ENDIF
+        
+        IF ( i == nxl .AND. ( .NOT. degraded_l ) )  THEN
+        
+           DO  k = nzb_s_inner(j,i) + 1, nzt
+              u_comp                 = u(k,j,i) - u_gtrans
+              swap_flux_x_local(k,j) = u_comp * (                             &
+                                       37.  * ( sk(k,j,i) + sk(k,j,i-1)   )   &
+                                       - 8. * ( sk(k,j,i+1) + sk(k,j,i-2) )   &
+                                       +      ( sk(k,j,i+2) + sk(k,j,i-3) )   &
+                                       ) * adv_sca_5
+              swap_diss_x_local(k,j) = - abs(u_comp) * (                      &
+                                       10.  * ( sk(k,j,i) - sk(k,j,i-1)   )   &
+                                       - 5. * ( sk(k,j,i+1) - sk(k,j,i-2) )   &
+                                       +      ( sk(k,j,i+2) - sk(k,j,i-3) )   &
+                                       ) * adv_sca_5
+           ENDDO
+           
+        ENDIF
+!        
+!--    Now compute the tendency terms for the horizontal parts.
+       DO  k = nzb_s_inner(j,i) + 1, nzt
+          
+          tend(k,j,i) = tend(k,j,i) - (                                       &
+               ( flux_r(k) + diss_r(k)                                        &
+             - ( swap_flux_x_local(k,j) + swap_diss_x_local(k,j) ) ) * ddx    &
+             + (flux_n(k)+diss_n(k)                                           &
+             - (swap_flux_y_local(k)    + swap_diss_y_local(k) ) )   * ddy )
+
+          swap_flux_y_local(k)   = flux_n(k)
+          swap_diss_y_local(k)   = diss_n(k)
+          swap_flux_x_local(k,j) = flux_r(k)
+          swap_diss_x_local(k,j) = diss_r(k)
+          
+       ENDDO
+!
+!--    Vertical advection, degradation of order near surface and top. 
+!--    The fluxes flux_d and diss_d at the surface are 0. Due to reasons of
+!--    statistical evaluation the top flux at the surface should be 0
+
+       flux_t(nzb_s_inner(j,i)) = 0.
+       diss_t(nzb_s_inner(j,i)) = 0.
+!       
+!--    2nd order scheme       
+       k         = nzb_s_inner(j,i) + 1
+       flux_d    = flux_t(k-1)
+       diss_d    = diss_t(k-1)
+       flux_t(k) = w(k,j,i) * ( sk(k+1,j,i) + sk(k,j,i) ) * 0.5
+!       
+!--    sk(k,j,i) is referenced three times to avoid a access below surface.
+       diss_t(k) = diss_2nd( sk(k+2,j,i), sk(k+1,j,i), sk(k,j,i), sk(k,j,i),  &
+                             sk(k,j,i), w(k,j,i), 0.5, ddzw(k) )
+                   
+       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
+                                 - ( flux_d + diss_d )     ) * ddzw(k)
+!
+!--    WS3 as an intermediate step       
+       k         = nzb_s_inner(j,i) + 2
+       flux_d    = flux_t(k-1)
+       diss_d    = diss_t(k-1)
+       flux_t(k) = w(k,j,i) * (                                               &
+                   7. * ( sk(k+1,j,i) + sk(k,j,i)   )                         &
+                   -    ( sk(k+2,j,i) + sk(k-1,j,i) ) ) * adv_sca_3
+       diss_t(k) = - abs(w(k,j,i)) * (                                        &
+                   3. * ( sk(k+1,j,i) - sk(k,j,i)   )                         & 
+                   -    ( sk(k+2,j,i) - sk(k-1,j,i) ) ) * adv_sca_3
+
+       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
+                                 - ( flux_d + diss_d ) ) * ddzw(k)
+!
+!--    WS5
+       DO  k = nzb_s_inner(j,i) + 3, nzt - 2
+       
+          flux_d    = flux_t(k-1)
+          diss_d    = diss_t(k-1)
+          
+          flux_t(k) = w(k,j,i) * (                                            &  
+                      37.  * ( sk(k+1,j,i) + sk(k,j,i)   )                    &
+                      - 8. * ( sk(k+2,j,i) + sk(k-1,j,i) )                    &
+                      +      ( sk(k+3,j,i) + sk(k-2,j,i) ) ) *adv_sca_5
+          diss_t(k) = - abs(w(k,j,i)) * (                                     & 
+                      10.  * ( sk(k+1,j,i) - sk(k,j,i)   )                    &      
+                      - 5. * ( sk(k+2,j,i) - sk(k-1,j,i) )                    &
+                      +      ( sk(k+3,j,i) - sk(k-2,j,i) ) ) * adv_sca_5
+
+          tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                 &
+                                    - ( flux_d + diss_d ) ) * ddzw(k)
+       ENDDO
+!
+!--    WS3 as an intermediate step 
+       k         = nzt - 1
+       flux_d    = flux_t(k-1)
+       diss_d    = diss_t(k-1)
+       flux_t(k) = w(k,j,i) * (                                               &
+                   7. * ( sk(k+1,j,i) + sk(k,j,i) )                           &
+                   -    ( sk(k+2,j,i) + sk(k-1,j,i) ) ) * adv_sca_3
+       diss_t(k) = - abs(w(k,j,i)) * (                                        &
+                   3. * ( sk(k+1,j,i) - sk(k,j,i) )                           & 
+                   -    ( sk(k+2,j,i) - sk(k-1,j,i) ) ) * adv_sca_3
+
+       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
+                                 - ( flux_d + diss_d ) ) * ddzw(k)
+!                                 
+!--    2nd
+       k         = nzt 
+       flux_d    = flux_t(k-1)
+       diss_d    = diss_t(k-1)
+       flux_t(k) = w(k,j,i) * ( sk(k+1,j,i) + sk(k,j,i) ) *0.5
+!       
+!--    sk(k+1) is referenced two times to avoid a segmentation fault at top.
+       diss_t(k) = diss_2nd( sk(k+1,j,i), sk(k+1,j,i), sk(k,j,i), sk(k-1,j,i),&
+                             sk(k-2,j,i), w(k,j,i), 0.5, ddzw(k) )
+
+       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
+                                 - ( flux_d + diss_d ) ) * ddzw(k)
+!       
+!--    evaluation of statistics
+       SELECT CASE ( sk_char )
+
+          CASE ( 'pt' )
+            DO  k = nzb_s_inner(j,i), nzt
+               sums_wspts_ws_l(k,:) = sums_wspts_ws_l(k,:)                     &
+                 + (flux_t(k)+diss_t(k))                                       &
+                 * weight_substep(intermediate_timestep_count) * rmask(j,i,:)
+             ENDDO
+             
+          CASE ( 'sa' )
+            DO  k = nzb_s_inner(j,i), nzt
+               sums_wssas_ws_l(k,:) = sums_wssas_ws_l(k,:)                     &
+                 +(flux_t(k)+diss_t(k))                                        &
+                 * weight_substep(intermediate_timestep_count) * rmask(j,i,:)
+             ENDDO
+             
+          CASE ( 'q' )
+             DO  k = nzb_s_inner(j,i), nzt
+                sums_wsqs_ws_l(k,:) = sums_wsqs_ws_l(k,:)                      &
+                  + (flux_t(k)+diss_t(k))                                      &
+                  * weight_substep(intermediate_timestep_count) * rmask(j,i,:)
+             ENDDO
+
+         END SELECT
+
+    END SUBROUTINE advec_s_ws_ij
+
+
+
+
+!------------------------------------------------------------------------------!
+! Advection of u-component - Call for grid point i,j
+!------------------------------------------------------------------------------!
+    SUBROUTINE advec_u_ws_ij( i, j )
+
+       USE arrays_3d
+       USE constants
+       USE control_parameters
+       USE grid_variables
+       USE indices
+       USE statistics
+
+       IMPLICIT NONE
+
+       INTEGER ::  i, j, k
+       LOGICAL ::  degraded_l = .FALSE., degraded_s = .FALSE.
+       REAL    ::  gu, gv, flux_d, diss_d, u_comp_l, v_comp, w_comp
+       REAL, DIMENSION(nzb:nzt+1)  ::  flux_t, diss_t, flux_r, diss_r,        &
+                                       flux_n, diss_n, u_comp
+
+       gu = 2.0 * u_gtrans
+       gv = 2.0 * v_gtrans
+         
+       IF ( boundary_flags(j,i) /= 0 )  THEN
+!       
+!--      Degrade the order for Dirichlet bc. at the outflow boundary
+         SELECT CASE ( boundary_flags(j,i) )
+         
+            CASE ( 1 )
+               DO  k = nzb_u_inner(j,i) + 1, nzt
+                  u_comp(k) = u(k,j,i+1) + u(k,j,i)
+                  flux_r(k) = ( u_comp(k) - gu ) * (                          &
+                              7. * (u(k,j,i+1) + u(k,j,i)    )                &
+                              -    ( u(k,j,i+2) + u(k,j,i-1) ) ) * adv_mom_3
+                  diss_r(k) = - abs(u_comp(k) - gu) * (                       &
+                              3. * (u(k,j,i+1) - u(k,j,i)    )                & 
+                              -    ( u(k,j,i+2) - u(k,j,i-1) ) ) * adv_mom_3
+              ENDDO
+              
+            CASE ( 2 )
+               DO  k = nzb_u_inner(j,i) + 1, nzt
+                  u_comp(k) = u(k,j,i+1) + u(k,j,i)
+                  flux_r(k) = (u_comp(k) - gu) * ( u(k,j,i+1) + u(k,j,i) )*0.25 
+                  diss_r(k) = diss_2nd( u(k,j,i+1) ,u(k,j,i+1), u(k,j,i),     &
+                                        u(k,j,i-1), u(k,j,i-2), u_comp(k),    &
+                                        0.25, ddx )
+               ENDDO
+               
+            CASE ( 3 )
+               DO  k = nzb_u_inner(j,i) + 1, nzt
+                  v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
+                  flux_n(k) = v_comp * (                                      &
+                              7. * ( u(k,j+1,i) + u(k,j,i) )                  &
+                              -    ( u(k,j+2,i) + u(k,j-1,i) ) ) * adv_mom_3
+                  diss_n(k) = - abs(v_comp) * (                               &
+                              3. * ( u(k,j+1,i) - u(k,j,i)   )                & 
+                              -    ( u(k,j+2,i) - u(k,j-1,i) ) ) * adv_mom_3
+               ENDDO
+               
+            CASE ( 4 )
+               DO  k = nzb_u_inner(j,i) + 1, nzt
+                  v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
+                  flux_n(k) = v_comp * ( u(k,j+1,i) + u(k,j,i) ) * 0.25
+                  diss_n(k) = diss_2nd( u(k,j+1,i), u(k,j+1,i), u(k,j,i),     &
+                                        u(k,j-1,i), u(k,j-2,i), v_comp,       &
+                                        0.25, ddy )
+               ENDDO
+               
+            CASE ( 5 )
+               DO  k = nzb_u_inner(j,i) + 1, nzt
+!               
+!--               Compute leftside fluxes for the left boundary of PE domain
+                  u_comp(k)     = u(k,j,i) + u(k,j,i-1) - gu
+                  flux_l_u(k,j) = u_comp(k) * ( u(k,j,i) + u(k,j,i-1) ) * 0.25
+                  diss_l_u(k,j) = diss_2nd(u(k,j,i+2), u(k,j,i+1), u(k,j,i),  &
+                                           u(k,j,i-1), u(k,j,i-1), u_comp(k), &
+                                           0.25, ddx )
+                 
+                  u_comp(k) = u(k,j,i+1) + u(k,j,i)
+                  flux_r(k) = ( u_comp(k) - gu ) * (                          &
+                              7. * (u(k,j,i+1) + u(k,j,i)    )                &
+                              -    ( u(k,j,i+2) + u(k,j,i-1) ) ) * adv_mom_3
+                  diss_r(k) = - abs( u_comp(k) -gu ) * (                      &
+                              3. * ( u(k,j,i+1) - u(k,j,i)     )              & 
+                              -    ( u(k,j,i+2) - u(k,j,i-1) ) ) * adv_mom_3
+               ENDDO
+               degraded_l = .TRUE.
+               
+            CASE ( 7 )
+               DO  k = nzb_u_inner(j,i) + 1, nzt                           
+                  v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
+                  flux_n(k) = v_comp * (                                      &
+                              7. * ( u(k,j+1,i) + u(k,j,i)   )                &
+                              -    ( u(k,j+2,i) + u(k,j-1,i) ) ) * adv_mom_3
+                  diss_n(k) = - abs(v_comp) * (                               &
+                              3. * ( u(k,j+1,i) - u(k,j,i)   )                & 
+                              -    ( u(k,j+2,i) - u(k,j-1,i) ) ) * adv_mom_3
+               ENDDO 
+               
+            CASE ( 8 )
+               DO  k = nzb_u_inner(j,i) + 1, nzt
+!               
+!--              Compute southside fluxes for the south boundary of PE domain           
+                  v_comp      = v(k,j,i) + v(k,j,i-1) - gv
+                  flux_s_u(k) = v_comp * ( u(k,j,i) + u(k,j-1,i) ) * 0.25 
+                  diss_s_u(k) = diss_2nd( u(k,j+2,i), u(k,j+1,i), u(k,j,i),   &
+                                          u(k,j-1,i), u(k,j-1,i), v_comp,     &
+                                          0.25, ddy )
+                                  
+                  v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
+                  flux_n(k) = v_comp * (                                      &
+                              7. * ( u(k,j+1,i) + u(k,j,i)   )                &
+                              -    ( u(k,j+2,i) + u(k,j-1,i) ) ) * adv_mom_3
+                  diss_n(k) = - abs(v_comp) * (                               &
+                              3. * ( u(k,j+1,i) - u(k,j,i)   )                & 
+                              -    ( u(k,j+2,i) - u(k,j-1,i) ) ) * adv_mom_3
+               ENDDO  
+               degraded_s = .TRUE.
+               
+            CASE DEFAULT
+            
+         END SELECT
+!         
+!--      Compute the crosswise 5th order fluxes at the outflow
+         IF ( boundary_flags(j,i) == 1 .OR. boundary_flags(j,i) == 2          &
+         .OR. boundary_flags(j,i) == 5 )  THEN
+         
+             DO  k = nzb_u_inner(j,i)+1, nzt
+               v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
+               flux_n(k) = v_comp * (                                         &
+                           37.  * ( u(k,j+1,i) + u(k,j,i)   )                 &
+                           - 8. * ( u(k,j+2,i) + u(k,j-1,i) )                 &
+                           +      ( u(k,j+3,i) + u(k,j-2,i) ) ) *adv_mom_5
+               diss_n(k) = - abs(v_comp) * (                                  &
+                           10.  * ( u(k,j+1,i) - u(k,j,i)   )                 &
+                           - 5. * ( u(k,j+2,i) - u(k,j-1,i) )                 &
+                           +      ( u(k,j+3,i) - u(k,j-2,i) ) ) * adv_mom_5
+             ENDDO
+             
+         ELSE
+         
+            DO  k = nzb_u_inner(j,i) + 1, nzt
+               u_comp(k) = u(k,j,i+1) + u(k,j,i)
+               flux_r(k) = ( u_comp(k) - gu ) * (                             &
+                           37.  * ( u(k,j,i+1) + u(k,j,i)   )                 &
+                           - 8. * ( u(k,j,i+2) + u(k,j,i-1) )                 &
+                           +      ( u(k,j,i+3) + u(k,j,i-2) ) ) * adv_mom_5
+               diss_r(k) = - abs(u_comp(k) - gu) * (                          &
+                           10.  * ( u(k,j,i+1) - u(k,j,i) )                   &
+                           - 5. * ( u(k,j,i+2) - u(k,j,i-1) )                 &
+                           +      ( u(k,j,i+3) - u(k,j,i-2) ) ) *adv_mom_5
+            ENDDO
+            
+         ENDIF
+                  
+       ELSE
+!       
+!--       Compute the fifth order fluxes for the interior of PE domain.                  
+          DO  k = nzb_u_inner(j,i) + 1, nzt
+             u_comp(k) = u(k,j,i+1) + u(k,j,i)
+             flux_r(k) = ( u_comp(k) - gu ) * (                               &
+                         37.  * ( u(k,j,i+1) + u(k,j,i)   )                   &
+                         - 8. * ( u(k,j,i+2) + u(k,j,i-1) )                   &
+                         +      ( u(k,j,i+3) + u(k,j,i-2) ) ) * adv_mom_5
+             diss_r(k) = - abs(u_comp(k) - gu) * (                            &
+                         10.  * ( u(k,j,i+1) - u(k,j,i)   )                   &
+                         - 5. * ( u(k,j,i+2) - u(k,j,i-1) )                   &
+                         +      ( u(k,j,i+3) - u(k,j,i-2) ) ) * adv_mom_5
+
+             v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
+             flux_n(k) = v_comp * (                                           &
+                         37.  * ( u(k,j+1,i) + u(k,j,i)   )                   &
+                         - 8. * ( u(k,j+2,i) + u(k,j-1,i) )                   &
+                         +      ( u(k,j+3,i) + u(k,j-2,i) ) ) * adv_mom_5
+             diss_n(k) = - abs(v_comp) * (                                    &
+                         10.  * ( u(k,j+1,i) - u(k,j,i)   )                   &
+                         - 5. * ( u(k,j+2,i) - u(k,j-1,i) )                   &
+                         +      ( u(k,j+3,i) - u(k,j-2,i) ) ) * adv_mom_5
+          ENDDO 
+         
+       ENDIF
+!       
+!--    Compute left- and southside fluxes for the respective boundary of PE      
+       IF ( j == nys .AND. ( .NOT. degraded_s ) )  THEN
+       
+          DO  k = nzb_u_inner(j,i) + 1, nzt
+             v_comp      = v(k,j,i) + v(k,j,i-1) - gv
+             flux_s_u(k) = v_comp * (                                         &
+                           37.  * ( u(k,j,i) + u(k,j-1,i)   )                 &
+                           - 8. * ( u(k,j+1,i) + u(k,j-2,i) )                 &
+                           +      ( u(k,j+2,i) + u(k,j-3,i) ) ) * adv_mom_5
+             diss_s_u(k) = - abs(v_comp) * (                                  &
+                           10.  * ( u(k,j,i) - u(k,j-1,i)    )                &
+                           - 5. * ( u(k,j+1,i) - u(k,j-2,i)  )                &
+                           +      (  u(k,j+2,i) - u(k,j-3,i) ) ) * adv_mom_5
+          ENDDO
+          
+       ENDIF
+       
+       IF ( i == nxlu .AND. ( .NOT. degraded_l ) )  THEN
+       
+          DO  k = nzb_u_inner(j,i)+1, nzt
+             u_comp_l      = u(k,j,i)+u(k,j,i-1)-gu
+             flux_l_u(k,j) = u_comp_l * (                                     &
+                             37.  * ( u(k,j,i) + u(k,j,i-1)   )               &
+                             - 8. * ( u(k,j,i+1) + u(k,j,i-2) )               &
+                             +      ( u(k,j,i+2) + u(k,j,i-3) ) ) * adv_mom_5
+             diss_l_u(k,j) = - abs(u_comp_l) * (                              &
+                             10.  * ( u(k,j,i) - u(k,j,i-1)   )               &
+                             - 5. * ( u(k,j,i+1) - u(k,j,i-2) )               &
+                             +      ( u(k,j,i+2) - u(k,j,i-3) ) ) * adv_mom_5
+          ENDDO
+          
+       ENDIF
+!       
+!--    Now compute the tendency terms for the horizontal parts.
+       DO  k = nzb_u_inner(j,i) + 1, nzt                   
+          tend(k,j,i) = tend(k,j,i) - (                                       &
+                            ( flux_r(k) + diss_r(k)                           &
+                          - ( flux_l_u(k,j) + diss_l_u(k,j) ) ) * ddx         &
+                          + ( flux_n(k) + diss_n(k)                           &
+                          - ( flux_s_u(k) + diss_s_u(k) )     ) * ddy  )
+
+           flux_l_u(k,j) = flux_r(k)
+           diss_l_u(k,j) = diss_r(k)
+           flux_s_u(k)   = flux_n(k)
+           diss_s_u(k)   = diss_n(k)
+!
+!--        Statistical Evaluation of u'u'. The factor has to be applied for 
+!--        right evaluation when gallilei_trans = .T. .
+           sums_us2_ws_l(k,:) = sums_us2_ws_l(k,:)                            &
+             + ( flux_r(k) *                                                  &
+             ( u_comp(k) - 2. * hom(k,1,1,:) ) / ( u_comp(k) - gu + 1.0E-20 ) &
+             + diss_r(k)                                                      &
+             * abs(u_comp(k) - 2. * hom(k,1,1,:) )                            & 
+             / (abs(u_comp(k) - gu) + 1.0E-20)         )                      &
+             * weight_substep(intermediate_timestep_count) * rmask(j,i,:)
+       ENDDO    
+!
+!--    Vertical advection, degradation of order near surface and top. 
+!--    The fluxes flux_d and diss_d at the surface are 0. Due to reasons of
+!--    statistical evaluation the top flux at the surface should be 0
+       flux_t(nzb_u_inner(j,i)) = 0. !statistical reasons
+       diss_t(nzb_u_inner(j,i)) = 0.
+!
+!--    2nd order scheme
+       k         = nzb_u_inner(j,i) + 1
+       flux_d    = flux_t(k-1)
+       diss_d    = diss_t(k-1)
+       w_comp    = w(k,j,i) + w(k,j,i-1)
+       flux_t(k) = w_comp * ( u(k+1,j,i) + u(k,j,i) ) *0.25
+       diss_t(k) = diss_2nd( u(k+2,j,i), u(k+1,j,i), u(k,j,i), 0., 0.,        &
+                             w_comp, 0.25, ddzw(k) )
+             
+       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
+                                 - ( flux_d + diss_d )     ) * ddzw(k)
+!
+!--    WS3 as an intermediate step
+       k         = nzb_u_inner(j,i) + 2
+       flux_d    = flux_t(k-1)
+       diss_d    = diss_t(k-1)
+       w_comp    = w(k,j,i) + w(k,j,i-1)
+       flux_t(k) = w_comp*(                                                   &
+                   7. * ( u(k+1,j,i) + u(k,j,i)   )                           &
+                   -    ( u(k+2,j,i) + u(k-1,j,i) ) ) * adv_mom_3
+       diss_t(k) = -abs(w_comp)*(                                             &
+                   3. * ( u(k+1,j,i) - u(k,j,i)   )                           & 
+                   -    ( u(k+2,j,i) - u(k-1,j,i) ) ) * adv_mom_3
+
+       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
+                                 - ( flux_d + diss_d )     ) * ddzw(k)
+
+       DO  k = nzb_u_inner(j,i) + 3, nzt - 2
+          flux_d    = flux_t(k-1)
+          diss_d    = diss_t(k-1)
+          w_comp    = w(k,j,i) + w(k,j,i-1)
+          flux_t(k) = w_comp*(                                                &
+                      37.*   ( u(k+1,j,i) + u(k,j,i)   )                      &
+                      - 8. * ( u(k+2,j,i) + u(k-1,j,i) )                      &
+                      +      ( u(k+3,j,i) + u(k-2,j,i) ) ) * adv_mom_5
+          diss_t(k) = - abs(w_comp) * (                                       &
+                      10. *  ( u(k+1,j,i) - u(k,j,i)   )                      & 
+                      - 5. * ( u(k+2,j,i) - u(k-1,j,i) )                      &
+                      +      ( u(k+3,j,i) - u(k-2,j,i) ) ) * adv_mom_5
+
+          tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                 &
+                                 - ( flux_d + diss_d )     ) * ddzw(k)
+       ENDDO
+!
+!--    WS3 as an intermediate step
+       k         = nzt - 1
+       flux_d    = flux_t(k-1)
+       diss_d    = diss_t(k-1)
+       w_comp    = w(k,j,i) + w(k,j,i-1)
+       flux_t(k) = w_comp * (                                                 &
+                   7. * ( u(k+1,j,i) + u(k,j,i)   )                           &
+                   -    ( u(k+2,j,i) + u(k-1,j,i) ) ) * adv_mom_3
+       diss_t(k) = - abs(w_comp) * (                                          &
+                   3. * ( u(k+1,j,i) - u(k,j,i)   )                           & 
+                   -    ( u(k+2,j,i) - u(k-1,j,i) ) ) * adv_mom_3
+                   
+       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
+                                 - ( flux_d + diss_d )     ) * ddzw(k)
+       
+!
+!--    2nd order scheme
+       k         = nzt
+       flux_d    = flux_t(k-1)
+       diss_d    = diss_t(k-1)
+       w_comp    = w(k,j,i)+w(k,j,i-1)
+       flux_t(k) = w_comp * ( u(k+1,j,i) + u(k,j,i) ) * 0.25
+       diss_t(k) = diss_2nd( u(k+1,j,i), u(k+1,j,i), u(k,j,i), u(k-1,j,i),    &
+                             u(k-2,j,i), w_comp, 0.25, ddzw(k) )
+
+       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
+                                 - ( flux_d + diss_d )     ) * ddzw(k)
+
+!
+!--    sum up the vertical momentum fluxes
+       DO  k = nzb_u_inner(j,i), nzt
+          sums_wsus_ws_l(k,:) = sums_wsus_ws_l(k,:)                           &
+              + ( flux_t(k) + diss_t(k) )                                     &
+              * weight_substep(intermediate_timestep_count) * rmask(j,i,:)
+       ENDDO
+
+
+    END SUBROUTINE advec_u_ws_ij
+
+
+
+
+!------------------------------------------------------------------------------!
+! Advection of v-component - Call for grid point i,j
+!------------------------------------------------------------------------------!
+   SUBROUTINE advec_v_ws_ij( i, j )
+
+       USE arrays_3d
+       USE constants
+       USE control_parameters
+       USE grid_variables
+       USE indices
+       USE statistics
+
+       IMPLICIT NONE
+
+       INTEGER  ::  i, j, k
+       LOGICAL  ::  degraded_l = .FALSE., degraded_s = .FALSE.
+       REAL     ::  gu, gv, flux_d, diss_d, u_comp, v_comp_l, w_comp
+       REAL, DIMENSION(nzb:nzt+1)  ::  flux_t, diss_t, flux_n,                &
+                                       diss_n, flux_r, diss_r, v_comp
+      
+       gu = 2.0 * u_gtrans
+       gv = 2.0 * v_gtrans
+       
+       IF ( boundary_flags(j,i) /= 0 )  THEN
+!       
+!--       Degrade the order for Dirichlet bc. at the outflow boundary
+          SELECT CASE ( boundary_flags(j,i) )
+          
+             CASE ( 1 )
+                DO  k = nzb_v_inner(j,i) + 1, nzt
+                   u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
+                   flux_r(k) = u_comp * (                                     &
+                               7. * (v(k,j,i+1) + v(k,j,i)    )               &
+                               -    ( v(k,j,i+2) + v(k,j,i-1) ) ) * adv_mom_3
+                   diss_r(k) = - abs(u_comp) * (                              &
+                               3. * ( v(k,j,i+1) - v(k,j,i)   )               & 
+                               -    ( v(k,j,i+2) - v(k,j,i-1) ) ) * adv_mom_3
+                ENDDO
+                
+             CASE ( 2 )
+                DO  k = nzb_v_inner(j,i) + 1, nzt
+                   u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
+                   flux_r(k) = u_comp * ( v(k,j,i+1) + v(k,j,i) ) * 0.25 
+                   diss_r(k) = diss_2nd( v(k,j,i+1), v(k,j,i+1), v(k,j,i),    &
+                                         v(k,j,i-1), v(k,j,i-2), u_comp,      &
+                                         0.25, ddx )
+                ENDDO
+                
+             CASE ( 3 )
+                DO  k = nzb_v_inner(j,i) + 1, nzt
+                   v_comp(k) = v(k,j+1,i) + v(k,j,i)
+                   flux_n(k) = ( v_comp(k)- gv ) * (                          &
+                               7. * ( v(k,j+1,i) + v(k,j,i)   )               &
+                               -    ( v(k,j+2,i) + v(k,j-1,i) ) ) * adv_mom_3
+                   diss_n(k) = - abs(v_comp(k) - gv) * (                      &
+                               3. * ( v(k,j+1,i) - v(k,j,i)   )               & 
+                               -    ( v(k,j+2,i) - v(k,j-1,i) ) ) * adv_mom_3
+                ENDDO
+                
+             CASE ( 4 )
+                DO  k = nzb_v_inner(j,i) + 1, nzt
+                   v_comp(k) = v(k,j+1,i) + v(k,j,i)
+                   flux_n(k) = ( v_comp(k) - gv ) *                           &
+                               ( v(k,j+1,i) + v(k,j,i) ) * 0.25 
+                   diss_n(k) = diss_2nd( v(k,j+1,i), v(k,j+1,i), v(k,j,i),    &
+                                         v(k,j-1,i), v(k,j-2,i), v_comp(k),   &
+                                         0.25, ddy )
+                ENDDO
+                
+             CASE ( 5 )
+                DO  k = nzb_w_inner(j,i) + 1, nzt
+                   u_comp    = u(k,j-1,i) + u(k,j,i) - gu 
+                   flux_r(k) = u_comp * (                                     &
+                               7. * ( v(k,j,i+1) + v(k,j,i)   )               &
+                               -    ( v(k,j,i+2) + v(k,j,i-1) ) ) * adv_mom_3
+                   diss_r(k) = - abs(u_comp) * (                              &
+                               3. * ( w(k,j,i+1) - w(k,j,i)   )               & 
+                               -    ( v(k,j,i+2) - v(k,j,i-1) ) ) * adv_mom_3
+                ENDDO
+                
+             CASE ( 6 )
+                DO  k = nzb_v_inner(j,i) + 1, nzt
+                   u_comp        = u(k,j-1,i) + u(k,j,i) - gu
+                   flux_l_v(k,j) = u_comp * ( v(k,j,i) + v(k,j,i-1) ) * 0.25
+                   diss_l_v(k,j) = diss_2nd( v(k,j,i+2), v(k,j,i+1), v(k,j,i),&
+                                             v(k,j,i-1), v(k,j,i-1), u_comp,  &
+                                             0.25, ddx )
+                                  
+                   u_comp = u(k,j-1,i+1) + u(k,j,i+1) - gu
+                   flux_r(k) = u_comp * (                                     &
+                               7. * ( v(k,j,i+1) + v(k,j,i)   )               &
+                               -    ( v(k,j,i+2) + v(k,j,i-1) ) ) * adv_mom_3
+                   diss_r(k) = - abs(u_comp) * (                              &
+                               3. * ( v(k,j,i+1) - v(k,j,i)   )               & 
+                               -    ( v(k,j,i+2) - v(k,j,i-1) ) ) * adv_mom_3 
+                ENDDO
+                degraded_l = .TRUE.
+                
+             CASE ( 7 )
+                DO  k = nzb_v_inner(j,i) + 1, nzt
+                   v_comp(k)   = v(k,j,i) + v(k,j-1,i) - gv
+                   flux_s_v(k) = v_comp(k) * ( v(k,j,i) + v(k,j-1,i) ) * 0.25
+                   diss_s_v(k) = diss_2nd( v(k,j+2,i), v(k,j+1,i), v(k,j,i),  &
+                                           v(k,j-1,i), v(k,j-1,i), v_comp(k), &
+                                           0.25, ddy )
+                                  
+                   v_comp(k) = v(k,j+1,i) + v(k,j,i)
+                   flux_n(k) = ( v_comp(k) - gv ) * (                         &
+                               7. * ( v(k,j+1,i) + v(k,j,i)   )               &
+                               -    ( v(k,j+2,i) + v(k,j-1,i) ) ) * adv_mom_3
+                   diss_n(k) = - abs(v_comp(k) - gv) * (                      &
+                               3. * ( v(k,j+1,i) - v(k,j,i)   )               & 
+                               -    ( v(k,j+2,i) - v(k,j-1,i) ) ) * adv_mom_3
+                ENDDO
+                degraded_s = .TRUE.
+                
+              CASE DEFAULT
+              
+          END SELECT
+!          
+!--       Compute the crosswise 5th order fluxes at the outflow
+          IF ( boundary_flags(j,i) == 1 .OR. boundary_flags(j,i) == 2         &
+          .OR. boundary_flags(j,i) == 5 .OR. boundary_flags(j,i) == 6 )  THEN
+          
+             DO  k = nzb_v_inner(j,i) + 1, nzt
+                v_comp(k) = v(k,j+1,i) + v(k,j,i)
+                flux_n(k) = ( v_comp(k) - gv ) * (                            &
+                            37.  * ( v(k,j+1,i) + v(k,j,i)   )                &
+                            - 8. * ( v(k,j+2,i) + v(k,j-1,i) )                &
+                            +      ( v(k,j+3,i) + v(k,j-2,i) ) ) * adv_mom_5
+                diss_n(k) = - abs(v_comp(k) - gv ) * (                        &
+                             10.  * ( v(k,j+1,i) - v(k,j,i)   )               &
+                             - 5. * ( v(k,j+2,i) - v(k,j-1,i) )               &
+                             +      ( v(k,j+3,i) - v(k,j-2,i) ) ) *adv_mom_5
+              ENDDO
+              
+          ELSE
+          
+             DO  k = nzb_v_inner(j,i) + 1, nzt
+                u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
+                flux_r(k) = u_comp * (                                        &
+                            37.  * ( v(k,j,i+1) + v(k,j,i)   )                &
+                            - 8. * ( v(k,j,i+2) + v(k,j,i-1) )                &
+                            +      ( v(k,j,i+3) + v(k,j,i-2) ) ) * adv_mom_5
+                diss_r(k) = - abs(u_comp) * (                                 &
+                            10. * ( v(k,j,i+1) - v(k,j,i)   )                 &
+                            -5. * ( v(k,j,i+2) - v(k,j,i-1) )                 &
+                            +     ( v(k,j,i+3) - v(k,j,i-2) ) ) * adv_mom_5
+             ENDDO
+             
+          ENDIF
+         
+       ELSE
+!       
+!--       Compute the fifth order fluxes for the interior of PE domain.                 
+          DO  k = nzb_v_inner(j,i) + 1, nzt
+             u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
+             flux_r(k) = u_comp * (                                           &
+                         37.  * ( v(k,j,i+1) + v(k,j,i)   )                   &
+                         - 8. * ( v(k,j,i+2) + v(k,j,i-1) )                   &
+                         +      ( v(k,j,i+3) + v(k,j,i-2) ) ) * adv_mom_5
+             diss_r(k) = - abs(u_comp) * (                                    &
+                         10.  * ( v(k,j,i+1) - v(k,j,i) )                     &
+                         - 5. * ( v(k,j,i+2) - v(k,j,i-1) )                   &
+                         +      ( v(k,j,i+3) - v(k,j,i-2) ) ) * adv_mom_5
+
+             v_comp(k) = v(k,j+1,i) + v(k,j,i)
+             flux_n(k) = ( v_comp(k) - gv ) * (                               &
+                         37.  * ( v(k,j+1,i) + v(k,j,i)   )                   &
+                         - 8. * ( v(k,j+2,i) + v(k,j-1,i) )                   &
+                         +      ( v(k,j+3,i) + v(k,j-2,i) ) ) * adv_mom_5
+             diss_n(k) = - abs(v_comp(k) - gv) * (                            &
+                         10.  * ( v(k,j+1,i) - v(k,j,i)   )                   &
+                         - 5. * ( v(k,j+2,i) - v(k,j-1,i) )                   &
+                         +      ( v(k,j+3,i) - v(k,j-2,i) ) ) * adv_mom_5
+          ENDDO 
+          
+       ENDIF
+!       
+!--    Compute left- and southside fluxes for the respective boundary       
+       IF ( i == nxl .AND. ( .NOT. degraded_l ) )  THEN
+          DO  k = nzb_v_inner(j,i) + 1, nzt
+             u_comp        = u(k,j-1,i) + u(k,j,i) - gu
+             flux_l_v(k,j) = u_comp * (                                       &
+                             37.  * ( v(k,j,i) + v(k,j,i-1)   )               &
+                             - 8. * ( v(k,j,i+1) + v(k,j,i-2) )               &
+                             +      ( v(k,j,i+2) + v(k,j,i-3) ) ) * adv_mom_5
+             diss_l_v(k,j) = - abs(u_comp) * (                                &
+                             10.  * ( v(k,j,i) - v(k,j,i-1)   )               &
+                             - 5. * ( v(k,j,i+1) - v(k,j,i-2) )               &
+                             +      ( v(k,j,i+2) - v(k,j,i-3) ) ) * adv_mom_5
+          ENDDO
+          
+       ENDIF
+       
+       IF ( j == nysv .AND. ( .NOT. degraded_s ) )  THEN
+       
+          DO  k = nzb_v_inner(j,i) + 1, nzt
+             v_comp_l    = v(k,j,i) + v(k,j-1,i) - gv
+             flux_s_v(k) = v_comp_l * (                                       &
+                           37.  * ( v(k,j,i) + v(k,j-1,i)   )                 &
+                           - 8. * ( v(k,j+1,i) + v(k,j-2,i) )                 &
+                           +      ( v(k,j+2,i) + v(k,j-3,i) ) ) * adv_mom_5
+             diss_s_v(k) = - abs(v_comp_l) * (                                &
+                           10.  * ( v(k,j,i) - v(k,j-1,i)   )                 &
+                           - 5. * ( v(k,j+1,i) - v(k,j-2,i) )                 &
+                           +      ( v(k,j+2,i) - v(k,j-3,i) ) ) * adv_mom_5
+          ENDDO
+          
+       ENDIF
+!       
+!--    Now compute the tendency terms for the horizontal parts.         
+       DO  k = nzb_v_inner(j,i) + 1, nzt                 
+          tend(k,j,i) = tend(k,j,i) - (                                       &
+                         ( flux_r(k) + diss_r(k)                              &
+                       - ( flux_l_v(k,j) + diss_l_v(k,j) ) ) * ddx            &
+                       + ( flux_n(k) + diss_n(k)                              &
+                       - ( flux_s_v(k) + diss_s_v(k)     ) ) * ddy     )
+        
+           flux_l_v(k,j) = flux_r(k)
+           diss_l_v(k,j) = diss_r(k)
+           flux_s_v(k)   = flux_n(k)
+           diss_s_v(k)   = diss_n(k)
+
+!
+!--        Statistical Evaluation of v'v'. The factor has to be applied for 
+!--        right evaluation when gallilei_trans = .T. .
+
+           sums_vs2_ws_l(k,:) = sums_vs2_ws_l(k,:)                            &
+             + ( flux_n(k)                                                    &
+             *  ( v_comp(k) - 2. * hom(k,1,2,:) )                             &
+             / ( v_comp(k) - gv + 1.0E-20 )                                   &
+             + diss_n(k)                                                      &
+             * abs( v_comp(k) - 2. * hom(k,1,2,:) )                           &
+             / ( abs( v_comp(k) - gv ) +1.0E-20 ) )                           &
+             * weight_substep(intermediate_timestep_count) * rmask(j,i,:)
+
+       ENDDO
+!
+!--    Vertical advection, degradation of order near surface and top. 
+!--    The fluxes flux_d and diss_d at the surface are 0. Due to reasons of
+!--    statistical evaluation the top flux at the surface should be 0
+       flux_t(nzb_v_inner(j,i)) = 0.  !statistical reasons
+       diss_t(nzb_v_inner(j,i)) = 0.
+!
+!--    2nd order scheme       
+       k         = nzb_v_inner(j,i) + 1
+       flux_d    = flux_t(k-1)
+       diss_d    = diss_t(k-1) 
+       w_comp    = w(k,j-1,i) + w(k,j,i)
+       flux_t(k) = w_comp * ( v(k+1,j,i) + v(k,j,i) ) * 0.25
+       diss_t(k) = diss_2nd( v(k+2,j,i), v(k+1,j,i), v(k,j,i), 0., 0., w_comp,&
+                             0.25, ddzw(k) ) 
+
+       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
+                                 - ( flux_d + diss_d )     ) * ddzw(k)
+        
+!
+!--    WS3 as an intermediate step
+       k         = nzb_v_inner(j,i) + 2
+       flux_d    = flux_t(k-1)
+       diss_d    = diss_t(k-1)
+       w_comp    = w(k,j-1,i) + w(k,j,i)
+       flux_t(k) = w_comp * (                                                 &
+                   7. * ( v(k+1,j,i) + v(k,j,i)   )                           &
+                   -    ( v(k+2,j,i) + v(k-1,j,i) ) ) * adv_mom_3
+       diss_t(k) = - abs(w_comp) * (                                          &
+                   3. * ( v(k+1,j,i) - v(k,j,i)   )                           & 
+                   -    ( v(k+2,j,i) - v(k-1,j,i) ) ) * adv_mom_3
+
+       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
+                                 - ( flux_d + diss_d )     ) * ddzw(k)
+!--    WS5
+       DO  k = nzb_v_inner(j,i) + 3, nzt - 2
+          flux_d    = flux_t(k-1)
+          diss_d    = diss_t(k-1)
+          w_comp    = w(k,j-1,i) + w(k,j,i)
+          flux_t(k) = w_comp * (                                              &
+                      37.  * ( v(k+1,j,i) + v(k,j,i)   )                      &           
+                      - 8. * ( v(k+2,j,i) + v(k-1,j,i) )                      &
+                      +      ( v(k+3,j,i) + v(k-2,j,i) ) ) * adv_mom_5
+          diss_t(k) = - abs(w_comp) * (                                       &
+                      10.  * ( v(k+1,j,i) - v(k,j,i)   )                      & 
+                      - 5. * ( v(k+2,j,i) - v(k-1,j,i) )                      &
+                      +      ( v(k+3,j,i) - v(k-2,j,i) ) ) * adv_mom_5
+
+          tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                 &
+                                 - ( flux_d + diss_d )     ) * ddzw(k)
+       ENDDO
+!
+!--    WS3 as an intermediate step
+       k         = nzt - 1
+       flux_d    = flux_t(k-1)
+       diss_d    = diss_t(k-1)
+       w_comp    = w(k,j-1,i) + w(k,j,i)
+       flux_t(k) = w_comp * (                                                 &
+                   7. * ( v(k+1,j,i) + v(k,j,i)   )                           &    
+                   -    ( v(k+2,j,i) + v(k-1,j,i) ) ) * adv_mom_3
+       diss_t(k) = - abs(w_comp) * (                                          &
+                   3. * ( v(k+1,j,i) - v(k,j,i) )                             & 
+                   -    ( v(k+2,j,i) - v(k-1,j,i) ) ) * adv_mom_3
+       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
+                                 - ( flux_d + diss_d )     ) * ddzw(k)
+!
+!--    2nd order scheme 
+       k         = nzt
+       flux_d    = flux_t(k-1)
+       diss_d    = diss_t(k-1)
+       w_comp    = w(k,j-1,i)+w(k,j,i)
+       flux_t(k) = w_comp * ( v(k+1,j,i) + v(k,j,i) ) * 0.25
+       diss_t(k) = diss_2nd( v(k+1,j,i), v(k+1,j,i), v(k,j,i), v(k-1,j,i),    &
+                             v(k-2,j,i), w_comp, 0.25, ddzw(k) )
+ 
+       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
+                                 - ( flux_d + diss_d )     ) * ddzw(k)
+              
+       DO  k = nzb_v_inner(j,i), nzt
+          sums_wsvs_ws_l(k,:) = sums_wsvs_ws_l(k,:)                           &
+            + ( flux_t(k) + diss_t(k) )                                       &
+            * weight_substep(intermediate_timestep_count) * rmask(j,i,:)
+       ENDDO
+
+    END SUBROUTINE advec_v_ws_ij
+
+
+
+!------------------------------------------------------------------------------!
+! Advection of w-component - Call for grid point i,j
+!------------------------------------------------------------------------------!
+    SUBROUTINE advec_w_ws_ij( i, j )
+
+       USE arrays_3d
+       USE constants
+       USE control_parameters
+       USE grid_variables
+       USE indices
+       USE statistics
+
+       IMPLICIT NONE
+
+       INTEGER ::  i, j, k
+       LOGICAL ::  degraded_l = .FALSE., degraded_s = .FALSE.
+       REAL    ::  gu, gv, flux_d, diss_d, u_comp, v_comp, w_comp
+       REAL, DIMENSION(nzb:nzt+1)  ::  flux_t, diss_t, flux_r, diss_r, flux_n, &
+                                       diss_n
+
+       gu = 2.0 * u_gtrans
+       gv = 2.0 * v_gtrans
+       
+       IF ( boundary_flags(j,i) /= 0 )  THEN
+!       
+!--      Degrade the order for Dirichlet bc. at the outflow boundary
+         SELECT CASE ( boundary_flags(j,i) )
+         
+            CASE ( 1 )
+               DO  k = nzb_w_inner(j,i) + 1, nzt
+                  u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
+                  flux_r(k) = u_comp * (                                      &
+                              7. * ( w(k,j,i+1) + w(k,j,i)   )                &
+                              -    ( w(k,j,i+2) + w(k,j,i-1) ) ) * adv_mom_3
+                  diss_r(k) = -abs(u_comp) * (                                &
+                              3. * ( w(k,j,i+1) - w(k,j,i)   )                & 
+                              -    ( w(k,j,i+2) - w(k,j,i-1) ) ) * adv_mom_3  
+               ENDDO
+               
+            CASE ( 2 )
+               DO  k = nzb_w_inner(j,i) + 1, nzt
+                  u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
+                  flux_r(k) = u_comp * ( w(k,j,i+1) + w(k,j,i) ) * 0.25 
+                  diss_r(k) = diss_2nd( w(k,j,i+1), w(k,j,i+1), w(k,j,i),     &
+                                        w(k,j,i-1), w(k,j,i-2), u_comp,       &
+                                        0.25, ddx )
+               ENDDO
+               
+            CASE ( 3 )
+               DO  k = nzb_w_inner(j,i) + 1, nzt
+                  v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
+                  flux_n(k) = v_comp * (                                      &
+                              7. * ( w(k,j+1,i) + w(k,j,i)   )                &
+                              -    ( w(k,j+2,i) + w(k,j-1,i) ) ) * adv_mom_3
+                  diss_n(k) = -abs(v_comp) * (                                &
+                              3. * ( w(k,j+1,i) - w(k,j,i)   )                & 
+                              -    ( w(k,j+2,i) - w(k,j-1,i) ) ) * adv_mom_3
+               ENDDO
+               
+            CASE ( 4 )
+               DO  k = nzb_w_inner(j,i) + 1, nzt
+                  v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
+                  flux_n(k) = v_comp * ( w(k,j+1,i) + w(k,j,i) ) * 0.25 
+                  diss_n(k) = diss_2nd( w(k,j+1,i), w(k,j+1,i), w(k,j,i),     &
+                                        w(k,j-1,i), w(k,j-2,i), v_comp,       &
+                                        0.25, ddy )
+               ENDDO
+               
+            CASE ( 5 )
+               DO  k = nzb_w_inner(j,i) + 1, nzt
+                  u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu 
+                  flux_r(k) = u_comp * (                                      &
+                              7. * ( w(k,j,i+1) + w(k,j,i)   )                &
+                              -    ( w(k,j,i+2) + w(k,j,i-1) ) ) * adv_mom_3
+                  diss_r(k) = - abs(u_comp) * (                               &
+                              3. * ( w(k,j,i+1) - w(k,j,i) )                  & 
+                              -    ( w(k,j,i+2) - w(k,j,i-1) ) ) * adv_mom_3
+               ENDDO
+               
+            CASE ( 6 )
+               DO  k = nzb_w_inner(j,i) + 1, nzt
+!               
+!--               Compute leftside fluxes for the left boundary of PE domain
+                  u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
+                  flux_r(k) = u_comp *(                                       &
+                              7. * ( w(k,j,i+1) + w(k,j,i)   )                &
+                              -    ( w(k,j,i+2) + w(k,j,i-1) ) )  * adv_mom_3
+                  diss_r(k) = - abs(u_comp) * (                               &
+                              3. * ( w(k,j,i+1) - w(k,j,i) )                  & 
+                              -    ( w(k,j,i+2) - w(k,j,i-1) ) ) * adv_mom_3
+                  
+                  u_comp        = u(k+1,j,i) + u(k,j,i) - gu
+                  flux_l_w(k,j) = u_comp * ( w(k,j,i) + w(k,j,i-1) ) * 0.25
+                  diss_l_w(k,j) = diss_2nd( w(k,j,i+2), w(k,j,i+1), w(k,j,i), &
+                                            w(k,j,i-1), w(k,j,i-1), u_comp,   &
+                                            0.25,ddx)
+               ENDDO
+               degraded_l = .TRUE.
+               
+            CASE ( 7 )
+               DO  k = nzb_w_inner(j,i) + 1, nzt
+                  v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
+                  flux_n(k) = v_comp *(                                       &
+                              7. * ( w(k,j+1,i) + w(k,j,i)   )                &
+                              -    ( w(k,j+2,i) + w(k,j-1,i) ) ) * adv_mom_3
+                  diss_n(k) = - abs(v_comp) * (                               &
+                              3. * ( w(k,j+1,i) - w(k,j,i)   )                & 
+                              -    ( w(k,j+2,i) - w(k,j-1,i) ) ) * adv_mom_3
+                ENDDO
+                
+            CASE ( 8 )
+               DO  k = nzb_w_inner(j,i) + 1, nzt
+                  v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
+                  flux_n(k) = v_comp * (                                      &
+                              7. * ( w(k,j+1,i) + w(k,j,i)   )                &
+                             -     ( w(k,j+2,i) + w(k,j-1,i) ) ) * adv_mom_3
+                  diss_n(k) = - abs(v_comp) * (                               &
+                              3. * ( w(k,j+1,i) - w(k,j,i) )                  & 
+                              -    ( w(k,j+2,i) - w(k,j-1,i) ) ) * adv_mom_3
+!                    
+!--              Compute southside fluxes for the south boundary of PE domain           
+                  v_comp      = v(k+1,j,i) + v(k,j,i) - gv
+                  flux_s_w(k) = v_comp * ( w(k,j,i) + w(k,j-1,i) ) * 0.25
+                  diss_s_w(k) = diss_2nd( w(k,j+2,i), w(k,j+1,i), w(k,j,i),   &
+                                          w(k,j-1,i), w(k,j-1,i), v_comp,     &
+                                          0.25, ddy )                 
+               ENDDO  
+               degraded_s = .TRUE.
+               
+            CASE DEFAULT
+            
+         END SELECT
+!         
+!--      Compute the crosswise 5th order fluxes at the outflow
+         IF ( boundary_flags(j,i) == 1 .OR. boundary_flags(j,i) == 2          &
+         .OR. boundary_flags(j,i) == 5 .OR. boundary_flags(j,i) == 6 )  THEN
+         
+            DO  k = nzb_w_inner(j,i) + 1, nzt
+               v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
+               flux_n(k) = v_comp * (                                         &
+                           37.  * ( w(k,j+1,i) + w(k,j,i)   )                 &
+                           - 8. * ( w(k,j+2,i) + w(k,j-1,i) )                 &
+                           +      ( w(k,j+3,i) + w(k,j-2,i) ) ) * adv_mom_5
+               diss_n(k) = - abs(v_comp) * (                                  &
+                           10.  * ( w(k,j+1,i) - w(k,j,i)   )                 &
+                           - 5. * ( w(k,j+2,i) - w(k,j-1,i) )                 &
+                           +      ( w(k,j+3,i) - w(k,j-2,i) ) ) * adv_mom_5
+            ENDDO
+            
+         ELSE
+         
+            DO  k = nzb_w_inner(j,i) + 1, nzt         
+               u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
+               flux_r(k) = u_comp * (                                         &
+                           37.  * ( w(k,j,i+1) + w(k,j,i) )                   &
+                           - 8. * ( w(k,j,i+2) + w(k,j,i-1) )                 &
+                           +      ( w(k,j,i+3) + w(k,j,i-2) ) ) * adv_mom_5
+               diss_r(k) = - abs(u_comp) * (                                  &
+                           10.  * ( w(k,j,i+1) - w(k,j,i)   )                 &
+                           - 5. * ( w(k,j,i+2) - w(k,j,i-1) )                 &
+                           +      ( w(k,j,i+3) - w(k,j,i-2) ) ) * adv_mom_5
+            ENDDO
+            
+         ENDIF
+         
+       ELSE
+!       
+!--      Compute the fifth order fluxes for the interior of PE domain.               
+         DO  k = nzb_w_inner(j,i) + 1, nzt
+            u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
+            flux_r(k) = u_comp * (                                            &
+                        37.  * ( w(k,j,i+1) + w(k,j,i)   )                    &
+                        - 8. * ( w(k,j,i+2) + w(k,j,i-1) )                    &
+                        +      ( w(k,j,i+3) + w(k,j,i-2) ) ) * adv_mom_5
+            diss_r(k) = - abs(u_comp) * (                                     &
+                        10.  * ( w(k,j,i+1) - w(k,j,i)   )                    &
+                        - 5. * ( w(k,j,i+2) - w(k,j,i-1) )                    &
+                        +      ( w(k,j,i+3) - w(k,j,i-2) ) ) * adv_mom_5
+                 
+            v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
+            flux_n(k) = v_comp * (                                            &
+                        37.  * ( w(k,j+1,i) + w(k,j,i)   )                    &
+                        - 8. * ( w(k,j+2,i) + w(k,j-1,i) )                    &
+                        +      ( w(k,j+3,i) + w(k,j-2,i) ) ) * adv_mom_5
+            diss_n(k) = - abs(v_comp) * (                                     &
+                        10.  * ( w(k,j+1,i) - w(k,j,i)   )                    &
+                        - 5. * ( w(k,j+2,i) - w(k,j-1,i) )                    &
+                        +      ( w(k,j+3,i) - w(k,j-2,i) ) ) * adv_mom_5
+         ENDDO 
+         
+       ENDIF
+!       
+!--    Compute left- and southside fluxes for the respective boundary      
+       IF ( j == nys .AND. ( .NOT. degraded_s ) )  THEN
+       
+          DO  k = nzb_w_inner(j,i) + 1, nzt
+             v_comp      = v(k+1,j,i) + v(k,j,i) - gv
+             flux_s_w(k) = v_comp * (                                         &
+                           37.  * ( w(k,j,i) + w(k,j-1,i)   )                 &
+                           - 8. * ( w(k,j+1,i) +w(k,j-2,i)  )                 &
+                           +      ( w(k,j+2,i) + w(k,j-3,i) ) ) * adv_mom_5
+             diss_s_w(k) = - abs(v_comp) * (                                  &
+                           10.  * ( w(k,j,i) - w(k,j-1,i)   )                 &
+                           - 5. * ( w(k,j+1,i) - w(k,j-2,i) )                 &
+                           +      ( w(k,j+2,i) - w(k,j-3,i) ) ) * adv_mom_5
+          ENDDO
+          
+       ENDIF
+       
+       IF ( i == nxl .AND. ( .NOT. degraded_l ) ) THEN
+       
+          DO  k = nzb_w_inner(j,i) + 1, nzt
+            u_comp        = u(k+1,j,i) + u(k,j,i) - gu
+            flux_l_w(k,j) = u_comp * (                                        &
+                            37.  * ( w(k,j,i) + w(k,j,i-1)   )                &
+                            - 8. * ( w(k,j,i+1) + w(k,j,i-2) )                &
+                            +      ( w(k,j,i+2) + w(k,j,i-3) ) ) * adv_mom_5
+            diss_l_w(k,j) = - abs(u_comp) * (                                 &
+                            10.  * ( w(k,j,i) - w(k,j,i-1)   )                &
+                            - 5. * ( w(k,j,i+1) - w(k,j,i-2) )                &
+                            +      ( w(k,j,i+2) - w(k,j,i-3) ) ) * adv_mom_5 
+          ENDDO
+          
+       ENDIF
+!       
+!--    Now compute the tendency terms for the horizontal parts.         
+       DO  k = nzb_w_inner(j,i) + 1, nzt
+          tend(k,j,i) = tend(k,j,i) - (                                       &
+                      ( flux_r(k) + diss_r(k)                                 &
+                    - ( flux_l_w(k,j) + diss_l_w(k,j) ) ) * ddx               &
+                    + ( flux_n(k) + diss_n(k)                                 &
+                    - ( flux_s_w(k) + diss_s_w(k)     ) ) * ddy  )
+
+          flux_l_w(k,j) = flux_r(k)
+          diss_l_w(k,j) = diss_r(k)
+          flux_s_w(k) = flux_n(k)
+          diss_s_w(k) = diss_n(k)
+       ENDDO
+
+!
+!--    Vertical advection, degradation of order near surface and top. 
+!--    The fluxes flux_d and diss_d at the surface are 0. Due to reasons of
+!--    statistical evaluation the top flux at the surface should be 0
+       flux_t(nzb_w_inner(j,i)) = 0.  !statistical reasons
+       diss_t(nzb_w_inner(j,i)) = 0.
+!
+!--    2nd order scheme        
+       k         = nzb_w_inner(j,i) + 1
+       flux_d    = flux_t(k-1)
+       diss_d    = diss_t(k-1)
+       w_comp    = w(k+1,j,i) + w(k,j,i)
+       flux_t(k) = w_comp * ( w(k+1,j,i) + w(k,j,i) ) * 0.25
+       diss_t(k) = diss_2nd( w(k+2,j,i), w(k+1,j,i), w(k,j,i), 0., 0.,        &
+                             w_comp, 0.25, ddzu(k+1) )
+                      
+       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
+                                 - ( flux_d + diss_d )     ) * ddzu(k+1)               
+!
+!--    WS3 as an intermediate step
+       k         = nzb_w_inner(j,i) + 2
+       flux_d    = flux_t(k-1)
+       diss_d    = diss_t(k-1)
+       w_comp    = w(k+1,j,i) + w(k,j,i)
+       flux_t(k) = w_comp * (                                                 &
+                   7. * ( w(k+1,j,i) + w(k,j,i) )                             &
+                   -    ( w(k+2,j,i) + w(k-1,j,i) ) ) * adv_mom_3
+       diss_t(k) = - abs(w_comp) * (                                          &
+                   3. * ( w(k+1,j,i) - w(k,j,i) )                             & 
+                   -    ( w(k+2,j,i) - w(k-1,j,i) ) ) * adv_mom_3
+
+       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
+                                 - ( flux_d + diss_d )     ) * ddzu(k+1)
+!                                 
+!--    WS5
+       DO  k = nzb_w_inner(j,i) + 3, nzt -2 
+          flux_d    = flux_t(k-1)
+          diss_d    = diss_t(k-1)
+          w_comp    = w(k+1,j,i) + w(k,j,i)
+          flux_t(k) = w_comp * (                                              &
+                      37.  * ( w(k+1,j,i) + w(k,j,i)   )                      &
+                      - 8. * ( w(k+2,j,i) + w(k-1,j,i) )                      &
+                      +      ( w(k+3,j,i) + w(k-2,j,i) ) ) * adv_mom_5
+          diss_t(k) = - abs(w_comp) * (                                       &
+                      10.  * ( w(k+1,j,i) - w(k,j,i)   )                      & 
+                      - 5. * ( w(k+2,j,i) - w(k-1,j,i) )                      & 
+                      +      ( w(k+3,j,i) - w(k-2,j,i) ) ) * adv_mom_5
+
+          tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                 &
+                                 - ( flux_d + diss_d )     ) * ddzu(k+1)
+       ENDDO
+!--    WS3 as an intermediate step
+       k         = nzt - 1
+       flux_d    = flux_t(k-1)
+       diss_d    = diss_t(k-1)
+       w_comp    = w(k+1,j,i) + w(k,j,i)
+       flux_t(k) = w_comp * (                                                 &
+                   7. * ( w(k+1,j,i) + w(k,j,i)   )                           &
+                   -    ( w(k+2,j,i) + w(k-1,j,i) ) ) *adv_mom_3
+       diss_t(k) = - abs(w_comp) * (                                          &
+                   3. * ( w(k+1,j,i) - w(k,j,i)     )                         & 
+                   -    ( w(k+2,j,i) - w(k-1,j,i) ) ) * adv_mom_3
+                   
+       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
+                                 - ( flux_d + diss_d )     ) * ddzu(k+1)
+!
+!--    2nd order scheme 
+       k         = nzt
+       flux_d    = flux_t(k-1)
+       diss_d    = diss_t(k-1)
+       w_comp    = w(k+1,j,i) + w(k,j,i)
+       flux_t(k) = w_comp * ( w(k+1,j,i) + w(k,j,i) ) * 0.25
+       diss_t(k) = diss_2nd( w(k+1,j,i), w(k+1,j,i), w(k,j,i), w(k-1,j,i),    &
+                             w(k-2,j,i), w_comp, 0.25, ddzu(k+1) )
+
+       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
+                                 - ( flux_d + diss_d )     ) * ddzu(k+1)
+       
+       DO  k = nzb_w_inner(j,i), nzt
+           sums_ws2_ws_l(k,:)  = sums_ws2_ws_l(k,:)                           &
+               + ( flux_t(k) + diss_t(k) )                                    &
+               * weight_substep(intermediate_timestep_count) * rmask(j,i,:)
+       ENDDO
+
+    END SUBROUTINE advec_w_ws_ij
+    
+
+!
+! Scalar advection - Call for all grid points
+!------------------------------------------------------------------------------!
+    SUBROUTINE advec_s_ws( sk, sk_char )
+
+       USE arrays_3d
+       USE constants
+       USE control_parameters
+       USE grid_variables
+       USE indices
+       USE statistics
+
+       IMPLICIT NONE
+
+       INTEGER ::  i, j, k
+
+       REAL, DIMENSION(:,:,:), POINTER ::  sk
+       REAL    :: flux_d, diss_d, u_comp, v_comp
+       REAL, DIMENSION(nzb:nzt+1)  ::  flux_r, diss_r, flux_n, diss_n
+       REAL, DIMENSION(nzb+1:nzt) :: swap_flux_y_local, swap_diss_y_local,    &
+                                     flux_t, diss_t
+       REAL, DIMENSION(nzb+1:nzt,nys:nyn) :: swap_flux_x_local,               &
+                                             swap_diss_x_local
+       CHARACTER (LEN = *), INTENT(IN) :: sk_char
+
+!
+!--    Compute the fluxes for the whole left boundary of the processor domain.
+       i = nxl
+       DO  j = nys, nyn
+          IF ( boundary_flags(j,i) == 6 )  THEN
+          
+             DO  k = nzb_s_inner(j,i) + 1, nzt
+                u_comp                 = u(k,j,i) - u_gtrans
+                swap_flux_x_local(k,j) = u_comp * (                           &
+                                         sk(k,j,i) + sk(k,j,i-1)) * 0.5
+                swap_diss_x_local(k,j) = diss_2nd( sk(k,j,i+2), sk(k,j,i+1),  &
+                                                   sk(k,j,i), sk(k,j,i-1),    &
+                                                   sk(k,j,i-1), u_comp,       &
+                                                   0.5, ddx ) 
+             ENDDO
+             
+          ELSE
+          
+             DO  k = nzb_s_inner(j,i) + 1, nzt
+                u_comp                 = u(k,j,i) - u_gtrans
+                swap_flux_x_local(k,j) = u_comp*(                             &
+                                         37.  * (sk(k,j,i)+sk(k,j,i-1)    )   &
+                                         -8. * ( sk(k,j,i+1) + sk(k,j,i-2) )  &
+                                         +     ( sk(k,j,i+2) + sk(k,j,i-3) ) )&
+                                         * adv_sca_5
+                swap_diss_x_local(k,j) = - abs(u_comp) * (                    &
+                                         10. * (sk(k,j,i) - sk(k,j,i-1)    )  &
+                                         -5. * ( sk(k,j,i+1) - sk(k,j,i-2) )  &
+                                         +     ( sk(k,j,i+2) - sk(k,j,i-3) ) )&
+                                         * adv_sca_5 
+             ENDDO
+          ENDIF
+       ENDDO
+!
+!--    The following loop computes the horizontal fluxes for the interior of the
+!--    processor domain plus south boundary points. Furthermore tendency terms
+!--    are computed.
+       DO  i = nxl, nxr
+          j = nys
+          IF ( boundary_flags(j,i) == 8 )  THEN
+          
+             DO  k = nzb_s_inner(j,i) + 1, nzt
+                v_comp               = v(k,j,i) - v_gtrans
+                swap_flux_y_local(k) = v_comp *                               &
+                                       ( sk(k,j,i) + sk(k,j-1,i) ) * 0.5 
+                swap_diss_y_local(k) = diss_2nd( sk(k,j+2,i), sk(k,j+1,i),    &
+                                                 sk(k,j,i), sk(k,j-1,i),      &
+                                                 sk(k,j-1,i), v_comp, 0.5, ddy)
+             ENDDO
+             
+          ELSE
+          
+             DO  k = nzb_s_inner(j,i) + 1, nzt
+                v_comp               = v(k,j,i) - v_gtrans
+                swap_flux_y_local(k) = v_comp * (                             &
+                                       37.  * ( sk(k,j,i) + sk(k,j-1,i)   )   &
+                                       - 8. * ( sk(k,j+1,i) + sk(k,j-2,i) )   &
+                                       +      ( sk(k,j+2,i) + sk(k,j-3,i) ) ) &  
+                                       * adv_sca_5
+                swap_diss_y_local(k)= - abs(v_comp) * (                       &
+                                      10.  * ( sk(k,j,i) - sk(k,j-1,i)   )    &
+                                      - 5. * ( sk(k,j+1,i) - sk(k,j-2,i) )    &
+                                      +      ( sk(k,j+2,i)-sk(k,j-3,i) ) )    &
+                                      * adv_sca_5
+             ENDDO
+             
+          ENDIF
+
+          DO  j = nys, nyn
+            IF ( boundary_flags(j,i) /= 0 )  THEN 
+!
+!--            Degrade the order for Dirichlet bc. at the outflow boundary
+               SELECT CASE ( boundary_flags(j,i) )
+               
+                  CASE ( 1 )
+                     DO  k = nzb_s_inner(j,i) + 1, nzt
+                        u_comp    = u(k,j,i+1) - u_gtrans
+                        flux_r(k) = u_comp * (                                &
+                                  7. * ( sk(k,j,i+1) + sk(k,j,i) )            &
+                                  - ( sk(k,j,i+2) + sk(k,j,i-1) ) ) * adv_sca_3
+                        diss_r(k) = - abs(u_comp) * (                         &
+                                  3. * ( sk(k,j,i+1) - sk(k,j,i) )            & 
+                                  - ( sk(k,j,i+2) - sk(k,j,i-1) ) ) * adv_sca_3
+                     ENDDO
+                     
+                  CASE ( 2 )
+                     DO  k = nzb_s_inner(j,i) + 1, nzt
+                        u_comp    = u(k,j,i+1) - u_gtrans
+                        flux_r(k) = u_comp * ( sk(k,j,i+1) + sk(k,j,i) ) * 0.5
+                        diss_r(k) = diss_2nd( sk(k,j,i+1), sk(k,j,i+1),       &
+                                              sk(k,j,i), sk(k,j,i-1),         &
+                                              sk(k,j,i-2), u_comp, 0.5, ddx )
+                     ENDDO
+                     
+                  CASE ( 3 )
+                     DO  k = nzb_s_inner(j,i) + 1, nzt
+                        v_comp    = v(k,j+1,i) - v_gtrans
+                        flux_n(k) = v_comp * (                                &
+                                  7. * ( sk(k,j+1,i) + sk(k,j,i) )            &
+                                  - ( sk(k,j+2,i) + sk(k,j-1,i) ) ) * adv_sca_3
+                        diss_n(k) = - abs(v_comp) * (                         &
+                                  3. * ( sk(k,j+1,i) - sk(k,j,i) )            & 
+                                  - ( sk(k,j+2,i) - sk(k,j-1,i) ) ) * adv_sca_3
+                     ENDDO
+                     
+                  CASE ( 4 )
+                     DO  k = nzb_s_inner(j,i) + 1, nzt
+                        v_comp    = v(k,j+1,i) - v_gtrans
+                        flux_n(k) = v_comp * ( sk(k,j+1,i) + sk(k,j,i) ) * 0.5
+                        diss_n(k) = diss_2nd( sk(k,j+1,i), sk(k,j+1,i),       &
+                                              sk(k,j,i), sk(k,j-1,i),         &
+                                              sk(k,j-2,i), v_comp, 0.5, ddy )     
+                     ENDDO
+                     
+                  CASE ( 5 )
+                     DO  k = nzb_w_inner(j,i) + 1, nzt
+                        u_comp    = u(k,j,i+1) - u_gtrans 
+                        flux_r(k) = u_comp * (                                &
+                                  7. * ( sk(k,j,i+1) + sk(k,j,i) )            &
+                                  - ( sk(k,j,i+2) + sk(k,j,i-1) ) ) * adv_sca_3
+                        diss_r(k) = - abs(u_comp) * (                         &
+                                  3. * ( sk(k,j,i+1) - sk(k,j,i) )            & 
+                                  - ( sk(k,j,i+2) - sk(k,j,i-1) ) ) * adv_sca_3
+                     ENDDO  
+                     
+                  CASE ( 6 )
+                     DO  k = nzb_s_inner(j,i) + 1, nzt
+                        u_comp    = u(k,j,i+1) - u_gtrans
+                        flux_r(k) = u_comp * (                                &
+                                  7. * ( sk(k,j,i+1) + sk(k,j,i) )            &
+                                  - ( sk(k,j,i+2) + sk(k,j,i-1) ) ) * adv_sca_3
+                        diss_r(k) = - abs(u_comp) * (                         &
+                                  3. * ( sk(k,j,i+1) - sk(k,j,i) )            & 
+                                  - ( sk(k,j,i+2) - sk(k,j,i-1) ) ) * adv_sca_3
+                     ENDDO
+                     
+                  CASE ( 7 )
+                     DO  k = nzb_s_inner(j,i) + 1, nzt
+                        v_comp    = v(k,j+1,i) - v_gtrans
+                        flux_n(k) = v_comp * (                                &
+                                  7. * ( sk(k,j+1,i) + sk(k,j,i) )            &
+                                  - ( sk(k,j+2,i) + sk(k,j-1,i) ) ) * adv_sca_3
+                        diss_n(k) = - abs(v_comp) * (                         &
+                                  3. * ( sk(k,j+1,i) - sk(k,j,i) )            & 
+                                  - ( sk(k,j+2,i) - sk(k,j-1,i) ) ) * adv_sca_3
+                     ENDDO
+                     
+                  CASE ( 8 )
+                     DO  k = nzb_s_inner(j,i) + 1, nzt
+                        v_comp    = v(k,j+1,i) - v_gtrans
+                        flux_n(k) = v_comp * (                                &
+                                  7. * ( sk(k,j+1,i) + sk(k,j,i) )            &
+                                  - ( sk(k,j+2,i) + sk(k,j-1,i) ) ) * adv_sca_3
+                        diss_n(k) = -  abs(v_comp) * (                        &
+                                  3. * ( sk(k,j+1,i) - sk(k,j,i) )            & 
+                                  - ( sk(k,j+2,i) - sk(k,j-1,i) ) ) * adv_sca_3
+                     ENDDO  
+                     
+                  CASE DEFAULT
+                  
+               END SELECT
+               
+               IF (boundary_flags(j,i) == 1 .OR. boundary_flags(j,i) == 2     &
+             .OR. boundary_flags(j,i) == 5 .OR. boundary_flags(j,i) == 6)  THEN
+              
+                  DO  k = nzb_s_inner(j,i) + 1, nzt
+                     v_comp    = v(k,j+1,i) - v_gtrans
+                     flux_n(k) = v_comp * (                                   &
+                                 37.  * ( sk(k,j+1,i) + sk(k,j,i)  )          &
+                                 - 8. * (sk(k,j+2,i) + sk(k,j-1,i) )          &
+                                 +      ( sk(k,j+3,i) + sk(k,j-2,i) ) )       &
+                                 * adv_sca_5
+                     diss_n(k) = - abs(v_comp) * (                            &
+                                 10.  * ( sk(k,j+1,i) - sk(k,j,i)   )         &
+                                 - 5. * ( sk(k,j+2,i) - sk(k,j-1,i) )         &
+                                 +      ( sk(k,j+3,i) - sk(k,j-2,i) ) )       &
+                                 * adv_sca_5
+                  ENDDO
+                  
+               ELSE
+               
+                  DO  k = nzb_s_inner(j,i) + 1, nzt
+                     u_comp    = u(k,j,i+1) - u_gtrans  
+                     flux_r(k) = u_comp * (                                   & 
+                                 37.  * ( sk(k,j,i+1) + sk(k,j,i)   )         &
+                                 - 8. * ( sk(k,j,i+2) + sk(k,j,i-1) )         &
+                                 +      ( sk(k,j,i+3) + sk(k,j,i-2) ) )       &
+                                 * adv_sca_5
+                     diss_r(k) = - abs(u_comp) * (                            &
+                                 10.  * ( sk(k,j,i+1) - sk(k,j,i)   )         &
+                                 - 5. * ( sk(k,j,i+2) - sk(k,j,i-1) )         &
+                                 +      ( sk(k,j,i+3) - sk(k,j,i-2) ) )       &
+                                 * adv_sca_5
+                  ENDDO
+                  
+               ENDIF      
+            
+            ELSE
+            
+               DO  k = nzb_s_inner(j,i) + 1, nzt
+                  u_comp    = u(k,j,i+1) - u_gtrans
+                  flux_r(k) = u_comp * (                                      &
+                              37.  * ( sk(k,j,i+1) + sk(k,j,i)   )            &
+                              - 8. * ( sk(k,j,i+2) + sk(k,j,i-1) )            &
+                              +      ( sk(k,j,i+3) + sk(k,j,i-2) ) )          &
+                              * adv_sca_5
+                  diss_r(k) = - abs(u_comp) * (                               &
+                              10.  * ( sk(k,j,i+1) - sk(k,j,i)   )            &
+                              - 5. * ( sk(k,j,i+2) - sk(k,j,i-1) )            &
+                              +      ( sk(k,j,i+3) - sk(k,j,i-2) ) )          &
+                              * adv_sca_5
+                  
+                  v_comp    = v(k,j+1,i) - v_gtrans
+                  flux_n(k) = v_comp * (                                      &
+                              37.  * ( sk(k,j+1,i) + sk(k,j,i)   )            &
+                              - 8. * ( sk(k,j+2,i) + sk(k,j-1,i) )            &
+                              +      ( sk(k,j+3,i) + sk(k,j-2,i) ) )          &
+                              * adv_sca_5
+                  diss_n(k) = - abs(v_comp) * (                               &
+                              10.  * ( sk(k,j+1,i) - sk(k,j,i)   )            &
+                              - 5. * ( sk(k,j+2,i) - sk(k,j-1,i) )            &
+                              +      ( sk(k,j+3,i) - sk(k,j-2,i) ) )          &
+                              * adv_sca_5                  
+               ENDDO
+               
+            ENDIF
+            
+            DO  k = nzb_s_inner(j,i) + 1, nzt
+               tend(k,j,i) = tend(k,j,i) - (                                  &
+                  ( flux_r(k) + diss_r(k)                                     &
+                - ( swap_flux_x_local(k,j) + swap_diss_x_local(k,j) ) ) * ddx &
+                + ( flux_n(k) + diss_n(k)                                     &
+                - ( swap_flux_y_local(k) + swap_diss_y_local(k) )     ) * ddy)
+                   
+                swap_flux_x_local(k,j) = flux_r(k)
+                swap_diss_x_local(k,j) = diss_r(k)
+                swap_flux_y_local(k)   = flux_n(k)
+                swap_diss_y_local(k)   = diss_n(k)
+            ENDDO
+         ENDDO
+      ENDDO
+
+!
+!--   Vertical advection, degradation of order near surface and top. 
+!--   The fluxes flux_d and diss_d at the surface are 0. Due to reasons of
+!--   statistical evaluation the top flux at the surface should be 0
+      DO  i = nxl, nxr
+         DO  j = nys, nyn
+!
+!--         2nd order scheme
+            k=nzb_s_inner(j,i) + 1
+!            
+!--         The fluxes flux_d and diss_d at the surface are 0. Due to static
+!--         reasons the top flux at the surface should be 0.
+            flux_t(nzb_s_inner(j,i)) = 0.
+            diss_t(nzb_s_inner(j,i)) = 0.
+            flux_d = flux_t(k-1)
+            diss_d = diss_t(k-1)
+            flux_t(k) = w(k,j,i) * (sk(k+1,j,i) + sk(k,j,i) ) *0.5
+            diss_t(k) = diss_2nd( sk(k+2,j,i), sk(k+1,j,i), sk(k,j,i),        &
+                                  sk(k,j,i), sk(k,j,i), w(k,j,i),             &
+                                  0.5, ddzw(k) )
+            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
+                                      - ( flux_d + diss_d )     ) * ddzw(k)    
+!
+!--         WS3 as an intermediate step
+            k         = nzb_s_inner(j,i) + 2
+            flux_d    = flux_t(k-1)
+            diss_d    = diss_t(k-1)
+            flux_t(k) = w(k,j,i) * (                                          &
+                        7. * ( sk(k+1,j,i) + sk(k,j,i) )                      &
+                       - ( sk(k+2,j,i) + sk(k-1,j,i) ) ) * adv_sca_3 
+            diss_t(k) = - abs(w(k,j,i)) * (                                   &
+                        3. * ( sk(k+1,j,i) - sk(k,j,i) )                      & 
+                        - ( sk(k+2,j,i) - sk(k-1,j,i) ) ) * adv_sca_3
+            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
+                                      - ( flux_d + diss_d )     ) * ddzw(k)
+!
+!--         WS5
+            DO  k = nzb_s_inner(j,i) + 3, nzt - 2
+               flux_d    = flux_t(k-1)
+               diss_d    = diss_t(k-1)
+               flux_t(k) = w(k,j,i) * (                                       &
+                           37.  * ( sk(k+1,j,i) + sk(k,j,i)   )               &
+                           - 8. * ( sk(k+2,j,i) + sk(k-1,j,i) )               &
+                           +      ( sk(k+3,j,i) + sk(k-2,j,i) ) ) * adv_sca_5
+               diss_t(k) = - abs(w(k,j,i)) * (                                &
+                           10.  * ( sk(k+1,j,i) -sk(k,j,i) )                  &
+                           - 5. * ( sk(k+2,j,i) - sk(k-1,j,i) )               &
+                           +      ( sk(k+3,j,i) - sk(k-2,j,i) ) ) * adv_sca_5
+
+               tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)            &
+                                      - ( flux_d + diss_d )     ) * ddzw(k)
+            ENDDO
+!
+!--         WS3 as an intermediate step
+            k         = nzt - 1
+            flux_d    = flux_t(k-1)
+            diss_d    = diss_t(k-1)
+            flux_t(k) = w(k,j,i) * (                                          &
+                        7. * ( sk(k+1,j,i) + sk(k,j,i)  )                     &
+                        - ( sk(k+2,j,i) + sk(k-1,j,i) ) ) * adv_sca_3
+            diss_t(k) = - abs(w(k,j,i)) * (                                   &
+                        3. * ( sk(k+1,j,i) - sk(k,j,i)   )                    & 
+                        -    ( sk(k+2,j,i) - sk(k-1,j,i) ) ) * adv_sca_3
+                        
+            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
+                                      - ( flux_d + diss_d )     ) * ddzw(k)
+!
+!--         2nd order scheme
+            k         = nzt
+            flux_d    = flux_t(k-1)
+            diss_d    = diss_t(k-1)
+            flux_t(k) = w(k,j,i) * ( sk(k+1,j,i) + sk(k,j,i) ) * 0.5
+            diss_t(k) = diss_2nd( sk(k+1,j,i), sk(k+1,j,i), sk(k,j,i),        &
+                                  sk(k-1,j,i), sk(k-2,j,i), w(k,j,i),         &
+                                  0.5, ddzw(k) )
+
+            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
+                                      - ( flux_d + diss_d )     ) * ddzw(k)
+!
+!--         evaluation of statistics
+            SELECT CASE ( sk_char )
+
+               CASE ( 'pt' )
+                 DO  k = nzb_s_inner(j,i), nzt
+                   sums_wspts_ws_l(k,:) = sums_wspts_ws_l(k,:)                 &
+                      + ( flux_t(k) + diss_t(k) )                              &
+                      * weight_substep(intermediate_timestep_count)            &
+                      * rmask(j,i,:)
+                 ENDDO
+               CASE ( 'sa' )
+                 DO  k = nzb_s_inner(j,i), nzt
+                   sums_wssas_ws_l(k,:) = sums_wssas_ws_l(k,:)                 &
+                      + ( flux_t(k) + diss_t(k) )                              &
+                      * weight_substep(intermediate_timestep_count)            &
+                      * rmask(j,i,:)
+                 ENDDO
+               CASE ( 'q' )
+                 DO  k = nzb_s_inner(j,i), nzt
+                   sums_wsqs_ws_l(k,:) = sums_wsqs_ws_l(k,:)                   &
+                      + ( flux_t(k) + diss_t(k) )                              &
+                      * weight_substep(intermediate_timestep_count)            &
+                      * rmask(j,i,:)
+                 ENDDO
+
+            END SELECT
+         ENDDO
+      ENDDO
+
+
+    END SUBROUTINE advec_s_ws
+
+
+!
+! Advection of u - Call for all grid points
+!------------------------------------------------------------------------------!
+    SUBROUTINE advec_u_ws
+
+       USE arrays_3d
+       USE constants
+       USE control_parameters
+       USE grid_variables
+       USE indices
+       USE statistics
+
+       IMPLICIT NONE
+
+       INTEGER ::  i, j, k
+       REAL    ::  gu, gv, flux_d, diss_d, v_comp, w_comp
+       REAL, DIMENSION(nzb+1:nzt) :: swap_flux_y_local_u, swap_diss_y_local_u
+       REAL, DIMENSION(nzb+1:nzt,nys:nyn) :: swap_flux_x_local_u,             &
+                                             swap_diss_x_local_u
+       REAL, DIMENSION(nzb:nzt+1) :: flux_t, diss_t, flux_r, diss_r, flux_n,  &
+                                     diss_n, u_comp
+ 
+       gu = 2.0 * u_gtrans
+       gv = 2.0 * v_gtrans
+
+!
+!--    Compute the fluxes for the whole left boundary of the processor domain.
+       i = nxlu
+       DO  j = nys, nyn
+          IF( boundary_flags(j,i) == 5 )  THEN
+          
+             DO  k = nzb_u_inner(j,i) + 1, nzt
+                u_comp(k)                = u(k,j,i) + u(k,j,i-1) - gu
+                swap_flux_x_local_u(k,j) = u_comp(k) *                        &
+                                           ( u(k,j,i) + u(k,j,i-1) ) * 0.25
+                swap_diss_x_local_u(k,j) = diss_2nd( u(k,j,i+2), u(k,j,i+1),  &
+                                                     u(k,j,i), u(k,j,i-1),    &
+                                                     u(k,j,i-1), u_comp(k),   &
+                                                     0.25, ddx )
+             ENDDO
+             
+          ELSE
+          
+             DO  k = nzb_u_inner(j,i) + 1, nzt
+                u_comp(k)                = u(k,j,i) + u(k,j,i-1) - gu
+                swap_flux_x_local_u(k,j) = u_comp(k) * (                      &
+                                         37.  * ( u(k,j,i) + u(k,j,i-1)   )   &
+                                         - 8. * ( u(k,j,i+1) + u(k,j,i-2) )   &
+                                         +      (u(k,j,i+2)+u(k,j,i-3) )  )   &
+                                         * adv_mom_5
+                swap_diss_x_local_u(k,j) = - abs(u_comp(k)) * (               &
+                                         10.  * ( u(k,j,i) - u(k,j,i-1)   )   &
+                                         - 5. * ( u(k,j,i+1) - u(k,j,i-2) )   &
+                                         +      ( u(k,j,i+2) - u(k,j,i-3) ) ) &
+                                         * adv_mom_5
+             ENDDO
+             
+          ENDIF
+          
+       ENDDO
+
+       DO i = nxlu, nxr
+!       
+!--      The following loop computes the fluxes for the south boundary points
+         j = nys
+         IF ( boundary_flags(j,i) == 8 )  THEN
+!         
+!--         Compute southside fluxes for the south boundary of PE domain 
+            DO  k = nzb_u_inner(j,i) + 1, nzt
+               v_comp                 = v(k,j,i) + v(k,j,i-1) - gv
+               swap_flux_y_local_u(k) = v_comp *                              &
+                                        ( u(k,j,i) + u(k,j-1,i) ) * 0.25 
+               swap_diss_y_local_u(k) = diss_2nd( u(k,j+2,i), u(k,j+1,i),     &
+                                                  u(k,j,i), u(k,j-1,i),       &
+                                                  u(k,j-1,i), v_comp,         &
+                                                  0.25, ddy )
+            ENDDO
+            
+         ELSE
+         
+            DO  k = nzb_u_inner(j,i) + 1, nzt
+               v_comp                 = v(k,j,i) + v(k,j,i-1) - gv
+               swap_flux_y_local_u(k) = v_comp * (                            &
+                                      37.  * ( u(k,j,i) + u(k,j-1,i)   )      &
+                                      - 8. * ( u(k,j+1,i) + u(k,j-2,i) )      &
+                                      +      ( u(k,j+2,i) + u(k,j-3,i) ) )    &
+                                      * adv_mom_5
+               swap_diss_y_local_u(k) = - abs(v_comp) * (                     &
+                                      10.  * ( u(k,j,i) - u(k,j-1,i)   )      &
+                                      - 5. * ( u(k,j+1,i) - u(k,j-2,i) )      &
+                                      +      ( u(k,j+2,i) - u(k,j-3,i) ) )    &
+                                      * adv_mom_5
+            ENDDO
+            
+         ENDIF
+!         
+!--      Computation of interior fluxes and tendency terms
+         DO  j = nys, nyn
+            IF ( boundary_flags(j,i) /= 0 )  THEN
+!            
+!--            Degrade the order for Dirichlet bc. at the outflow boundary
+               SELECT CASE ( boundary_flags(j,i) )
+               
+                  CASE ( 1 )
+                     DO  k = nzb_u_inner(j,i) + 1, nzt
+                        u_comp(k) = u(k,j,i+1) + u(k,j,i)
+                        flux_r(k) = ( u_comp(k) - gu ) * (                    &
+                                    7. * ( u(k,j,i+1) + u(k,j,i) )            &
+                                    - ( u(k,j,i+2) + u(k,j,i-1) ) ) * adv_mom_3
+                        diss_r(k) = - abs(u_comp(k) - gu) * (                 &
+                                    3. * ( u(k,j,i+1) - u(k,j,i) )            & 
+                                    - ( u(k,j,i+2) - u(k,j,i-1) ) ) * adv_mom_3
+                     ENDDO
+                     
+                  CASE ( 2 )
+                     DO  k = nzb_u_inner(j,i) + 1, nzt
+                        u_comp(k) = u(k,j,i+1) + u(k,j,i)
+                        flux_r(k) = ( u_comp(k) - gu ) *                      &
+                                    ( u(k,j,i+1) + u(k,j,i) ) * 0.25 
+                        diss_r(k) = diss_2nd( u(k,j,i+1), u(k,j,i+1),         &
+                                              u(k,j,i), u(k,j,i-1),           &
+                                              u(k,j,i-2), u_comp(k) ,0.25 ,ddx)
+                     ENDDO
+                     
+                  CASE ( 3 )
+                     DO  k = nzb_u_inner(j,i) + 1, nzt
+                        v_comp   = v(k,j+1,i) + v(k,j+1,i-1) - gv
+                        flux_n(k) = v_comp * (                                &
+                                    7. * ( u(k,j+1,i) + u(k,j,i) )            &
+                                    - ( u(k,j+2,i) + u(k,j-1,i) ) ) * adv_mom_3
+                        diss_n(k) = - abs(v_comp) * (                         &
+                                    3. * ( u(k,j+1,i) - u(k,j,i) )            & 
+                                    - ( u(k,j+2,i) - u(k,j-1,i) ) ) * adv_mom_3
+                     ENDDO
+                     
+                  CASE ( 4 )
+                     DO  k = nzb_u_inner(j,i) + 1, nzt
+                        v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
+                        flux_n(k) = v_comp * ( u(k,j+1,i) + u(k,j,i) ) * 0.25
+                        diss_n(k) = diss_2nd( u(k,j+1,i), u(k,j+1,i),         &
+                                              u(k,j,i), u(k,j-1,i),           &
+                                              u(k,j-2,i), v_comp, 0.25, ddy )
+                     ENDDO
+                     
+                  CASE ( 5 )
+                     DO  k = nzb_u_inner(j,i) + 1, nzt        
+                        u_comp(k) = u(k,j,i+1) + u(k,j,i)
+                        flux_r(k) = ( u_comp(k) - gu ) * (                    &
+                                    7. * ( u(k,j,i+1) + u(k,j,i) )            &
+                                    - ( u(k,j,i+2) + u(k,j,i-1) ) ) * adv_mom_3
+                        diss_r(k) = - abs(u_comp(k) - gu) * (                 &
+                                    3. * ( u(k,j,i+1) - u(k,j,i) )            & 
+                                    - ( u(k,j,i+2) - u(k,j,i-1) ) ) * adv_mom_3
+                     ENDDO
+                     
+                  CASE ( 7 )
+                     DO  k = nzb_u_inner(j,i) + 1, nzt                           
+                        v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
+                        flux_n(k) = v_comp * (                                &
+                                    7. * ( u(k,j+1,i) + u(k,j,i) )            &
+                                    - ( u(k,j+2,i) + u(k,j-1,i) ) ) * adv_mom_3
+                        diss_n(k) = - abs(v_comp) * (                         &
+                                    3. * ( u(k,j+1,i) - u(k,j,i) )            & 
+                                    - ( u(k,j+2,i) - u(k,j-1,i) ) ) * adv_mom_3
+                     ENDDO 
+                     
+                  CASE ( 8 )
+                     DO  k = nzb_u_inner(j,i) + 1, nzt
+                        v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
+                        flux_n(k) = v_comp * (                                &
+                                    7. * ( u(k,j+1,i) + u(k,j,i) )            &
+                                    - ( u(k,j+2,i) + u(k,j-1,i) ) ) * adv_mom_3
+                        diss_n(k) = - abs(v_comp) * (                         &
+                                    3. * ( u(k,j+1,i) - u(k,j,i) )            & 
+                                    - ( u(k,j+2,i) - u(k,j-1,i) ) ) * adv_mom_3
+                     ENDDO  
+                     
+                  CASE DEFAULT
+                  
+               END SELECT
+!               
+!--            Compute the crosswise 5th order fluxes at the outflow
+               IF (boundary_flags(j,i) == 1 .OR. boundary_flags(j,i) == 2     &
+              .OR. boundary_flags(j,i) == 5)  THEN
+              
+                  DO  k = nzb_u_inner(j,i) + 1, nzt
+                     v_comp = v(k,j+1,i) + v(k,j+1,i-1) - gv
+                     flux_n(k) = v_comp * (                                   &
+                               37.  * ( u(k,j+1,i) + u(k,j,i)   )             &
+                               - 8. * ( u(k,j+2,i) +u(k,j-1,i)  )             &
+                               +      ( u(k,j+3,i) + u(k,j-2,i) ) ) * adv_mom_5
+                     diss_n(k) = - abs(v_comp) * (                            &
+                               10.  * ( u(k,j+1,i) - u(k,j,i) )               &
+                               - 5. * ( u(k,j+2,i) - u(k,j-1,i) )             &
+                               +      ( u(k,j+3,i) - u(k,j-2,i) ) ) * adv_mom_5
+                  ENDDO
+                  
+               ELSE
+               
+                  DO  k = nzb_u_inner(j,i) + 1, nzt
+                     u_comp(k) = u(k,j,i+1) + u(k,j,i)
+                     flux_r(k) = ( u_comp(k) - gu ) * (                       &
+                               37.  * ( u(k,j,i+1) + u(k,j,i)   )             &
+                               - 8. * ( u(k,j,i+2) + u(k,j,i-1) )             &
+                               +      ( u(k,j,i+3) + u(k,j,i-2) ) ) * adv_mom_5
+                     diss_r(k) = - abs(u_comp(k) - gu) * (                    &
+                               10.  * ( u(k,j,i+1) - u(k,j,i)   )             &
+                               - 5. * ( u(k,j,i+2) - u(k,j,i-1) )             &
+                               +      ( u(k,j,i+3) - u(k,j,i-2) ) ) * adv_mom_5
+                  ENDDO
+                  
+               ENDIF
+               
+            ELSE
+            
+               DO  k = nzb_u_inner(j,i) + 1, nzt
+                  u_comp(k) = u(k,j,i+1) + u(k,j,i)
+                  flux_r(k) = ( u_comp(k) - gu ) * (                          &
+                            37.  * ( u(k,j,i+1) + u(k,j,i)   )                &
+                            - 8. * ( u(k,j,i+2) + u(k,j,i-1) )                &
+                            +      ( u(k,j,i+3) + u(k,j,i-2) ) ) * adv_mom_5
+                  diss_r(k) = - abs(u_comp(k) - gu) * (                       &
+                            10.  * ( u(k,j,i+1) - u(k,j,i)   )                &
+                            - 5. * ( u(k,j,i+2) - u(k,j,i-1) )                &
+                            +      ( u(k,j,i+3) - u(k,j,i-2) ) ) * adv_mom_5
+
+                  v_comp = v(k,j+1,i) + v(k,j+1,i-1) - gv
+                  flux_n(k) = v_comp * (                                      &
+                            37.  * ( u(k,j+1,i) + u(k,j,i)   )                &
+                            - 8. * ( u(k,j+2,i) + u(k,j-1,i) )                &
+                            +      ( u(k,j+3,i) + u(k,j-2,i) ) ) * adv_mom_5
+                  diss_n(k) = - abs(v_comp) * (                               &
+                            10.  * ( u(k,j+1,i) - u(k,j,i)   )                &
+                            - 5. * ( u(k,j+2,i) - u(k,j-1,i) )                &
+                            +      ( u(k,j+3,i) - u(k,j-2,i) ) ) * adv_mom_5
+                                
+               ENDDO
+               
+            ENDIF
+            
+            DO  k = nzb_u_inner(j,i) + 1, nzt
+            
+               tend(k,j,i) = tend(k,j,i) - (                                  &
+            ( flux_r(k) + diss_r(k)               &
+            - ( swap_flux_x_local_u(k,j) + swap_diss_x_local_u(k,j) ) ) * ddx &
+            + ( flux_n(k) + diss_n(k)               &
+            - ( swap_flux_y_local_u(k) + swap_diss_y_local_u(k)     ) ) * ddy )
+                  
+               swap_flux_x_local_u(k,j) = flux_r(k)   
+               swap_diss_x_local_u(k,j) = diss_r(k)
+               swap_flux_y_local_u(k)   = flux_n(k)     
+               swap_diss_y_local_u(k)   = diss_n(k)
+                      
+               sums_us2_ws_l(k,:)  = sums_us2_ws_l(k,:)                       &
+                 + ( flux_r(k)                                                &
+                 * ( u_comp(k) - 2. * hom(k,1,1,:) )                          &
+                 / ( u_comp(k) - gu + 1.0E-20 )                               & 
+                 + diss_r(k)                                                  &
+                 * abs(u_comp(k) - 2. * hom(k,1,1,:) )                        &
+                 / (abs(u_comp(k) - gu) + 1.0E-20) )                          &
+                 * weight_substep(intermediate_timestep_count) * rmask(j,i,:)
+            ENDDO
+         ENDDO
+       ENDDO
+
+!
+!--   Vertical advection, degradation of order near surface and top. 
+!--   The fluxes flux_d and diss_d at the surface are 0. Due to reasons of
+!--   statistical evaluation the top flux at the surface should be 0
+       DO i = nxlu, nxr
+          DO  j = nys, nyn
+             k = nzb_u_inner(j,i) + 1
+!             
+!--         The fluxes flux_d and diss_d at the surface are 0. Due to static
+!--         reasons the top flux at the surface should be 0.
+            flux_t(nzb_u_inner(j,i)) = 0.
+            diss_t(nzb_u_inner(j,i)) = 0.
+            flux_d = flux_t(k-1)
+            diss_d = diss_t(k-1)             
+!
+!--         2nd order scheme
+             w_comp    = w(k,j,i) + w(k,j,i-1)
+             flux_t(k) = w_comp * ( u(k+1,j,i) + u(k,j,i) ) * 0.25
+             diss_t(k) = diss_2nd( u(k+2,j,i), u(k+1,j,i), u(k,j,i),          &
+                                   0., 0., w_comp, 0.25, ddzw(k) )
+                                   
+             tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
+                                      - ( flux_d + diss_d )     ) * ddzw(k)
+!
+!--         WS3 as an intermediate step
+            k         = nzb_u_inner(j,i) + 2
+            flux_d    = flux_t(k-1)
+            diss_d    = diss_t(k-1)
+            w_comp    = w(k,j,i) + w(k,j,i-1)
+            flux_t(k) = w_comp * (                                            &
+                        7. * (u(k+1,j,i) + u(k,j,i)    )                      &
+                        -    ( u(k+2,j,i) + u(k-1,j,i) ) ) * adv_mom_3
+            diss_t(k) = - abs(w_comp) * (                                     &
+                        3. * ( u(k+1,j,i) - u(k,j,i) )                        & 
+                        -    ( u(k+2,j,i) - u(k-1,j,i) ) ) * adv_mom_3
+
+            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
+                                      - ( flux_d + diss_d )     ) * ddzw(k)
+!
+!WS5
+             DO  k = nzb_u_inner(j,i) + 3, nzt - 3
+
+                flux_d    = flux_t(k-1)
+                diss_d    = diss_t(k-1)
+                w_comp    = w(k,j,i) + w(k,j,i-1)
+                flux_t(k) = w_comp * (                                        &
+                            37.  * ( u(k+1,j,i) + u(k,j,i)   )                &
+                            - 8. * ( u(k+2,j,i) + u(k-1,j,i) )                &
+                            +      ( u(k+3,j,i) + u(k-2,j,i) ) ) * adv_mom_5
+                diss_t(k) = - abs(w_comp) * (                                 &
+                            10.  * ( u(k+1,j,i) - u(k,j,i)   )                & 
+                            - 5. * ( u(k+2,j,i) - u(k-1,j,i) )                & 
+                            +      ( u(k+3,j,i) - u(k-2,j,i) ) ) * adv_mom_5
+
+                tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)           &
+                                      - ( flux_d + diss_d )     ) * ddzw(k)
+
+             ENDDO
+!
+!--         WS3 as an intermediate step
+             DO k = nzt - 2, nzt - 1
+                flux_d = flux_t(k-1)
+                diss_d = diss_t(k-1)
+                w_comp = w(k,j,i) + w(k,j,i-1)
+                flux_t(k) = w_comp * (                                        &
+                            7. * ( u(k+1,j,i) + u(k,j,i) )                    &
+                            -    ( u(k+2,j,i) + u(k-1,j,i) ) ) * adv_mom_3
+                diss_t(k) = - abs(w_comp) * (                                 &
+                            3. * ( u(k+1,j,i) - u(k,j,i) )                    & 
+                            -    ( u(k+2,j,i) - u(k-1,j,i) ) ) * adv_mom_3
+                            
+                tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)           &
+                                      - ( flux_d + diss_d )     ) * ddzw(k)
+             ENDDO
+!
+!--         2nd order scheme
+             k         = nzt
+             flux_d    = flux_t(k-1)
+             diss_d    = diss_t(k-1)
+             w_comp    = w(k,j,i) + w(k,j,i-1)
+             flux_t(k) = w_comp * ( u(k+1,j,i) + u(k,j,i) ) * 0.25
+             diss_t(k) = diss_2nd( u(nzt+1,j,i), u(nzt+1,j,i), u(k,j,i),      &
+                                   u(k-1,j,i), u(k-2,j,i), w_comp,            &
+                                   0.25, ddzw(k)) 
+
+             tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)              &
+                                      - ( flux_d + diss_d )     ) * ddzw(k)
+!
+!-- at last vertical momentum flux is accumulated
+            DO  k = nzb_u_inner(j,i), nzt
+               sums_wsus_ws_l(k,:) = sums_wsus_ws_l(k,:)                       &
+                   + ( flux_t(k) + diss_t(k) )                                 &
+                   * weight_substep(intermediate_timestep_count)               &
+                   * rmask(j,i,:)
+            ENDDO
+          ENDDO
+       ENDDO
+
+
+    END SUBROUTINE advec_u_ws
+    
+    
+!
+! Advection of v - Call for all grid points
+!------------------------------------------------------------------------------!
+    SUBROUTINE advec_v_ws
+
+       USE arrays_3d
+       USE constants
+       USE control_parameters
+       USE grid_variables
+       USE indices
+       USE statistics
+
+       IMPLICIT NONE
+
+
+       INTEGER ::  i, j, k
+       REAL    ::  gu, gv, flux_l, flux_s, flux_d, diss_l, diss_s, diss_d,    &
+                   u_comp, w_comp
+       REAL, DIMENSION(nzb+1:nzt) :: swap_flux_y_local_v, swap_diss_y_local_v
+       REAL, DIMENSION(nzb+1:nzt,nys:nyn) :: swap_flux_x_local_v,             &
+                                             swap_diss_x_local_v
+       REAL, DIMENSION(nzb:nzt+1) :: flux_t, diss_t, flux_n, diss_n, flux_r,  &
+                                     diss_r, v_comp
+
+       gu = 2.0 * u_gtrans
+       gv = 2.0 * v_gtrans
+!
+!-- First compute the whole left boundary of the processor domain
+       i = nxl
+       DO  j = nysv, nyn
+       
+          IF ( boundary_flags(j,i) == 6 )  THEN
+             DO  k = nzb_v_inner(j,i) + 1, nzt
+                u_comp                   = u(k,j-1,i) + u(k,j,i) - gu
+                swap_flux_x_local_v(k,j) = u_comp *                           &
+                                           ( v(k,j,i) + v(k,j,i-1)) * 0.25
+                swap_diss_x_local_v(k,j) = diss_2nd( v(k,j,i+2), v(k,j,i+1),  &
+                                                     v(k,j,i), v(k,j,i-1),    &
+                                                     v(k,j,i-1), u_comp,      &
+                                                     0.25, ddx )
+             ENDDO
+             
+          ELSE
+          
+             DO  k = nzb_v_inner(j,i)+1, nzt
+                u_comp                   = u(k,j-1,i) + u(k,j,i) - gu
+                swap_flux_x_local_v(k,j) = u_comp * (                         &
+                                         37.  * ( v(k,j,i) + v(k,j,i-1)   )   &
+                                         - 8. * ( v(k,j,i+1) + v(k,j,i-2) )   &
+                                         +      ( v(k,j,i+2) + v(k,j,i-3) ) ) &
+                                         * adv_mom_5
+                swap_diss_x_local_v(k,j) = - abs(u_comp) * (                  &
+                                         10. * ( v(k,j,i) - v(k,j,i-1)   )    &
+                                         -5. * ( v(k,j,i+1) - v(k,j,i-2) )    &
+                                         +     ( v(k,j,i+2) - v(k,j,i-3) ) )  &
+                                         * adv_mom_5
+             ENDDO
+             
+          ENDIF
+          
+       ENDDO
+
+       DO i = nxl, nxr
+         j = nysv
+         IF ( boundary_flags(j,i) == 7 )  THEN
+         
+            DO  k = nzb_v_inner(j,i) + 1, nzt
+               v_comp(k)              = v(k,j,i) + v(k,j-1,i) - gv
+               swap_flux_y_local_v(k) = v_comp(k) *                           &
+                                        ( v(k,j,i) + v(k,j-1,i) ) * 0.25
+               swap_diss_y_local_v(k) = diss_2nd( v(k,j+2,i), v(k,j+1,i),     &
+                                                  v(k,j,i), v(k,j-1,i),       &
+                                                  v(k,j-1,i), v_comp(k),      &
+                                                  0.25, ddy )                                
+            ENDDO
+            
+         ELSE
+         
+            DO  k = nzb_v_inner(j,i) + 1, nzt
+               v_comp(k)              = v(k,j,i) + v(k,j-1,i) - gv
+               swap_flux_y_local_v(k) = v_comp(k) * (                         &
+                                      37.  * ( v(k,j,i) + v(k,j-1,i)   )      &
+                                      - 8. * ( v(k,j+1,i) + v(k,j-2,i) )      &
+                                      +      ( v(k,j+2,i) + v(k,j-3,i) ) )    &
+                                      * adv_mom_5
+               swap_diss_y_local_v(k) = - abs(v_comp(k)) * (                  &
+                                      10.  * ( v(k,j,i) - v(k,j-1,i)   )      &
+                                      - 5. * ( v(k,j+1,i) - v(k,j-2,i) )      &
+                                      +      ( v(k,j+2,i) - v(k,j-3,i) ) )    &
+                                      * adv_mom_5
+            ENDDO
+            
+         ENDIF
+         
+         DO  j = nysv, nyn
+            IF ( boundary_flags(j,i) /= 0 )  THEN
+!       
+!--       Degrade the order for Dirichlet bc. at the outflow boundary
+               SELECT CASE ( boundary_flags(j,i) )
+          
+                  CASE ( 1 )
+                     DO  k = nzb_v_inner(j,i) + 1, nzt
+                        u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
+                        flux_r(k) = u_comp * (                                &
+                                 7. * (v(k,j,i+1) + v(k,j,i)    )             &
+                                 -    ( v(k,j,i+2) + v(k,j,i-1) ) ) * adv_mom_3
+                        diss_r(k) = - abs(u_comp) * (                         &
+                               3. * ( v(k,j,i+1) - v(k,j,i)   )               & 
+                               -    ( v(k,j,i+2) - v(k,j,i-1) ) ) * adv_mom_3
+                     ENDDO
+                
+                  CASE ( 2 )
+                     DO  k = nzb_v_inner(j,i) + 1, nzt
+                        u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
+                        flux_r(k) = u_comp * ( v(k,j,i+1) + v(k,j,i) ) * 0.25 
+                        diss_r(k) = diss_2nd( v(k,j,i+1), v(k,j,i+1),         &
+                                              v(k,j,i), v(k,j,i-1),           &
+                                              v(k,j,i-2), u_comp, 0.25, ddx )
+                     ENDDO
+                
+                  CASE ( 3 )
+                     DO  k = nzb_v_inner(j,i) + 1, nzt
+                        v_comp(k) = v(k,j+1,i) + v(k,j,i)
+                        flux_n(k) = ( v_comp(k)- gv ) * (                     &
+                                 7. * ( v(k,j+1,i) + v(k,j,i)   )             &
+                                 -    ( v(k,j+2,i) + v(k,j-1,i) ) ) * adv_mom_3
+                        diss_n(k) = - abs(v_comp(k) - gv) * (                 &
+                                 3. * ( v(k,j+1,i) - v(k,j,i)   )             & 
+                                 -    ( v(k,j+2,i) - v(k,j-1,i) ) ) * adv_mom_3
+                     ENDDO
+                
+                  CASE ( 4 )
+                     DO  k = nzb_v_inner(j,i) + 1, nzt
+                        v_comp(k) = v(k,j+1,i) + v(k,j,i)
+                        flux_n(k) = ( v_comp(k) - gv ) *                      &
+                                   ( v(k,j+1,i) + v(k,j,i) ) * 0.25 
+                        diss_n(k) = diss_2nd( v(k,j+1,i), v(k,j+1,i),         &
+                                              v(k,j,i), v(k,j-1,i),           &
+                                              v(k,j-2,i), v_comp(k), 0.25, ddy)
+                     ENDDO
+                
+                  CASE ( 5 )
+                     DO  k = nzb_w_inner(j,i) + 1, nzt
+                        u_comp    = u(k,j-1,i) + u(k,j,i) - gu 
+                        flux_r(k) = u_comp * (                                &
+                               7. * ( v(k,j,i+1) + v(k,j,i)   )               &
+                               -    ( v(k,j,i+2) + v(k,j,i-1) ) ) * adv_mom_3
+                        diss_r(k) = - abs(u_comp) * (                         &
+                               3. * ( w(k,j,i+1) - w(k,j,i)   )               & 
+                               -    ( v(k,j,i+2) - v(k,j,i-1) ) ) * adv_mom_3
+                     ENDDO
+                 
+                  CASE ( 6 )
+                     DO  k = nzb_v_inner(j,i) + 1, nzt
+                                  
+                        u_comp = u(k,j-1,i+1) + u(k,j,i+1) - gu
+                        flux_r(k) = u_comp * (                                &
+                                 7. * ( v(k,j,i+1) + v(k,j,i)   )             &
+                                 -    ( v(k,j,i+2) + v(k,j,i-1) ) ) * adv_mom_3
+                        diss_r(k) = - abs(u_comp) * (                         &
+                                 3. * ( v(k,j,i+1) - v(k,j,i)   )             & 
+                                 -    ( v(k,j,i+2) - v(k,j,i-1) ) ) * adv_mom_3 
+                     ENDDO
+                
+                  CASE ( 7 )
+                     DO  k = nzb_v_inner(j,i) + 1, nzt                                 
+                        v_comp(k) = v(k,j+1,i) + v(k,j,i)
+                        flux_n(k) = ( v_comp(k) - gv ) * (                    &
+                                 7. * ( v(k,j+1,i) + v(k,j,i)   )             &
+                                 -    ( v(k,j+2,i) + v(k,j-1,i) ) ) * adv_mom_3
+                        diss_n(k) = - abs(v_comp(k) - gv) * (                 &
+                                 3. * ( v(k,j+1,i) - v(k,j,i)   )             & 
+                                 -    ( v(k,j+2,i) - v(k,j-1,i) ) ) * adv_mom_3
+                     ENDDO
+                
+                  CASE DEFAULT
+              
+               END SELECT
+!          
+!--         Compute the crosswise 5th order fluxes at the outflow
+               IF ( boundary_flags(j,i) == 1 .OR. boundary_flags(j,i) == 2    &
+            .OR. boundary_flags(j,i) == 5 .OR. boundary_flags(j,i) == 6 )  THEN
+          
+                  DO  k = nzb_v_inner(j,i) + 1, nzt
+                     v_comp(k) = v(k,j+1,i) + v(k,j,i)
+                     flux_n(k) = ( v_comp(k) - gv ) * (                       &
+                               37.  * ( v(k,j+1,i) + v(k,j,i)   )             &
+                               - 8. * ( v(k,j+2,i) + v(k,j-1,i) )             &
+                               +      ( v(k,j+3,i) + v(k,j-2,i) ) ) * adv_mom_5
+                     diss_n(k) = - abs(v_comp(k) - gv ) * (                   &
+                               10.  * ( v(k,j+1,i) - v(k,j,i)   )             &
+                               - 5. * ( v(k,j+2,i) - v(k,j-1,i) )             &
+                               +      ( v(k,j+3,i) - v(k,j-2,i) ) ) *adv_mom_5
+                   ENDDO
+              
+               ELSE
+           
+                  DO  k = nzb_v_inner(j,i) + 1, nzt
+                     u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
+                     flux_r(k) = u_comp * (                                   &
+                               37.  * ( v(k,j,i+1) + v(k,j,i)   )             &
+                              - 8. * ( v(k,j,i+2) + v(k,j,i-1) )              &
+                            +      ( v(k,j,i+3) + v(k,j,i-2) ) ) * adv_mom_5
+                     diss_r(k) = - abs(u_comp) * (                            &
+                               10. * ( v(k,j,i+1) - v(k,j,i)   )              &
+                               -5. * ( v(k,j,i+2) - v(k,j,i-1) )              &
+                               +     ( v(k,j,i+3) - v(k,j,i-2) ) ) * adv_mom_5
+                  ENDDO
+               
+               ENDIF
+         
+         
+            ELSE
+         
+               DO  k = nzb_v_inner(j,i) + 1, nzt
+                  u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
+                  flux_r(k) = u_comp * (                                      &
+                            37.  * ( v(k,j,i+1) + v(k,j,i)   )                &
+                            - 8. * ( v(k,j,i+2) + v(k,j,i-1) )                &
+                            +      ( v(k,j,i+3) + v(k,j,i-2) ) ) * adv_mom_5
+                  diss_r(k) = - abs(u_comp) * (                               &
+                            10. * ( v(k,j,i+1) - v(k,j,i)   )                 &
+                            -5. * ( v(k,j,i+2) - v(k,j,i-1) )                 &
+                            +     ( v(k,j,i+3) - v(k,j,i-2) ) ) * adv_mom_5
+
+                  v_comp(k) = v(k,j+1,i) + v(k,j,i)
+                  flux_n(k) = ( v_comp(k) - gv ) * (                          &
+                            37.  * ( v(k,j+1,i) + v(k,j,i)   )                &
+                            - 8. * ( v(k,j+2,i) + v(k,j-1,i) )                &
+                            +      ( v(k,j+3,i) + v(k,j-2,i) ) ) * adv_mom_5
+                  diss_n(k) = - abs(v_comp(k) - gv ) * (                      &
+                            10.  * ( v(k,j+1,i) - v(k,j,i)   )                &
+                            - 5. * ( v(k,j+2,i) - v(k,j-1,i) )                &
+                            +      ( v(k,j+3,i) - v(k,j-2,i) ) ) *adv_mom_5
+
+               ENDDO
+            ENDIF
+            
+            DO  k = nzb_v_inner(j,i) + 1, nzt
+               tend(k,j,i) = tend(k,j,i) - (                                  &
+              ( flux_r(k) + diss_r(k)                                         &
+            - ( swap_flux_x_local_v(k,j) + swap_diss_x_local_v(k,j) ) ) * ddx &
+            + ( flux_n(k) + diss_n(k)                                         &
+            - ( swap_flux_y_local_v(k) + swap_diss_y_local_v(k) )     ) * ddy )
+                  
+               swap_flux_x_local_v(k,j) = flux_r(k)   
+               swap_diss_x_local_v(k,j) = diss_r(k)
+               swap_flux_y_local_v(k)   = flux_n(k)     
+               swap_diss_y_local_v(k)   = diss_n(k)   
+
+               sums_vs2_ws_l(k,:) = sums_vs2_ws_l(k,:)                        &
+                  + (flux_n(k) *( v_comp(k) - 2. * hom(k,1,2,:) )             &
+                  / ( v_comp(k) - gv + 1.0E-20)                               &
+                  + diss_n(k) * abs(v_comp(k) - 2. * hom(k,1,2,:) )           &
+                  /(abs(v_comp(k) - gv) + 1.0E-20 ) )                         &
+                  * weight_substep(intermediate_timestep_count) * rmask(j,i,:)
+            ENDDO
+         ENDDO
+       ENDDO
+!
+!--   Vertical advection, degradation of order near surface and top. 
+!--   The fluxes flux_d and diss_d at the surface are 0. Due to reasons of
+!--   statistical evaluation the top flux at the surface should be 0
+       DO i = nxl, nxr
+          DO  j = nysv, nyn
+!          
+!--         The fluxes flux_d and diss_d at the surface are 0. Due to static
+!--         reasons the top flux at the surface should be 0.
+             flux_t(nzb_v_inner(j,i)) = 0.
+             diss_t(nzb_v_inner(j,i)) = 0.
+             k      = nzb_v_inner(j,i) + 1             
+             flux_d = flux_t(k-1)
+             diss_d = diss_t(k-1)
+!
+!--          2nd order scheme
+             w_comp    = w(k,j-1,i) + w(k,j,i)
+             flux_t(k) = w_comp * ( v(k+1,j,i) + v(k,j,i) ) * 0.25
+             diss_t(k) = diss_2nd( v(k+2,j,i), v(k+1,j,i), v(k,j,i),          &
+                                   0., 0., w_comp, 0.25, ddzw(k) )
+
+             tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)              &
+                                      - ( flux_d + diss_d )     ) * ddzw(k)
+!
+!--         WS3 as an intermediate step
+             k          = nzb_v_inner(j,i) + 2
+             flux_d     = flux_t(k-1)
+             diss_d     = diss_t(k-1)
+             w_comp     = w(k,j-1,i) + w(k,j,i)
+             flux_t(k) = w_comp * (                                           &
+                         7. * ( v(k+1,j,i) + v(k,j,i) )                       &
+                         -    ( v(k+2,j,i) + v(k-1,j,i) ) ) * adv_mom_3
+             diss_t(k) = - abs(w_comp) * (                                    &
+                         3. * ( v(k+1,j,i) - v(k,j,i) )                       & 
+                         -    ( v(k+2,j,i) - v(k-1,j,i) ) ) * adv_mom_3
+                         
+             tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)              &
+                                      - ( flux_d + diss_d )     ) * ddzw(k)
+
+!
+!-- WS5
+             DO  k = nzb_v_inner(j,i) + 3, nzt - 3
+                flux_d = flux_t(k-1)
+                diss_d = diss_t(k-1)
+                w_comp = w(k,j-1,i) + w(k,j,i)
+                flux_t(k) = w_comp * (                                        &
+                            37.  * ( v(k+1,j,i) + v(k,j,i)   )                &
+                            - 8. * ( v(k+2,j,i) + v(k-1,j,i) )                &
+                            +      ( v(k+3,j,i) + v(k-2,j,i) ) ) * adv_mom_5
+                diss_t(k) = - abs(w_comp) * (                                 &
+                            10. * ( v(k+1,j,i) - v(k,j,i)   )                 & 
+                            -5. * ( v(k+2,j,i) - v(k-1,j,i) )                 &
+                            +     ( v(k+3,j,i) - v(k-2,j,i) ) ) * adv_mom_5
+                            
+                tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)              &
+                                      - ( flux_d + diss_d )     ) * ddzw(k)
+            ENDDO
+!
+!--         WS3 as an intermediate step
+            DO k = nzt - 2, nzt - 1 
+               flux_d     = flux_t(k-1)
+               diss_d     = diss_t(k-1)
+               w_comp     = w(k,j-1,i) + w(k,j,i)
+               flux_t(k) = w_comp * (                                         &
+                           7. * ( v(k+1,j,i) + v(k,j,i) )                     &
+                           -    ( v(k+2,j,i) + v(k-1,j,i) ) ) * adv_mom_3
+               diss_t(k) = - abs(w_comp) * (                                  &
+                           3. * ( v(k+1,j,i) - v(k,j,i) )                     & 
+                           -    ( v(k+2,j,i) - v(k-1,j,i) ) ) * adv_mom_3
+                         
+             tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)              &
+                                      - ( flux_d + diss_d )     ) * ddzw(k)
+            ENDDO
+!
+!--          2nd order scheme
+            k         = nzt
+            flux_d    = flux_t(k-1)
+            diss_d    = diss_t(k-1)
+            w_comp    = w(k,j-1,i) + w(k,j,i)
+            flux_t(k) = w_comp * ( v(k+1,j,i) + v(k,j,i) ) * 0.25
+            diss_t(k) = diss_2nd( v(nzt+1,j,i), v(nzt+1,j,i), v(k,j,i),       &
+                                  v(k-1,j,i), v(k-2,j,i), w_comp, 0.25,ddzw(k))
+
+            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
+                                      - ( flux_d + diss_d )     ) * ddzw(k)
+!
+!- at last vertical momentum flux is accumulated
+            DO  k = nzb_v_inner(j,i), nzt
+               sums_wsvs_ws_l(k,:) = sums_wsvs_ws_l(k,:)                       &
+                   + ( flux_t(k) + diss_t(k) )                                 &
+                   * weight_substep(intermediate_timestep_count)               &
+                   * rmask(j,i,:)
+            ENDDO
+            sums_vs2_ws_l(nzb_v_inner(j,i),:) =                                &
+                   sums_vs2_ws_l(nzb_v_inner(j,i)+1,:)
+          ENDDO
+       ENDDO
+
+    END SUBROUTINE advec_v_ws
+    
+    
+!
+! Advection of w - Call for all grid points
+!------------------------------------------------------------------------------!
+    SUBROUTINE advec_w_ws
+
+       USE arrays_3d
+       USE constants
+       USE control_parameters
+       USE grid_variables
+       USE indices
+       USE statistics
+
+       IMPLICIT NONE
+
+       INTEGER ::  i, j, k
+       REAL    ::  gu, gv, flux_d, diss_d, u_comp, v_comp, w_comp
+       REAL    ::  flux_t(nzb:nzt+1), diss_t(nzb:nzt+1)
+       REAL, DIMENSION(nzb:nzt+1)  ::  flux_r, diss_r, flux_n, diss_n
+       REAL, DIMENSION(nzb+1:nzt) :: swap_flux_y_local_w, swap_diss_y_local_w
+       REAL, DIMENSION(nzb+1:nzt,nys:nyn) :: swap_flux_x_local_w,             &
+                                             swap_diss_x_local_w
+ 
+       gu = 2.0 * u_gtrans
+       gv = 2.0 * v_gtrans
+!
+!--   compute the whole left boundary of the processor domain
+       i = nxl
+       DO  j = nys, nyn
+       
+          IF ( boundary_flags(j,i) == 6 )  THEN
+          
+             DO  k = nzb_v_inner(j,i) + 1, nzt
+                u_comp                   = u(k+1,j,i) + u(k,j,i) - gu
+                swap_flux_x_local_w(k,j) = u_comp *                           &
+                                           (w(k,j,i)+w(k,j,i-1)) * 0.25
+                swap_flux_x_local_w(k,j) = diss_2nd( w(k,j,i+2), w(k,j,i+1),  &
+                                                     w(k,j,i), w(k,j,i-1),    &
+                                                     w(k,j,i-1), u_comp,      &
+                                                     0.25, ddx )
+             ENDDO
+             
+          ELSE
+          
+             DO  k = nzb_v_inner(j,i) + 1, nzt
+                u_comp                   = u(k+1,j,i) + u(k,j,i) - gu
+                swap_flux_x_local_w(k,j) = u_comp * (                         &
+                                         37.  * ( w(k,j,i) + w(k,j,i-1)   )   &
+                                         - 8. * ( w(k,j,i+1) + w(k,j,i-2) )   &
+                                         +      ( w(k,j,i+2) + w(k,j,i-3) ) ) &
+                                         * adv_mom_5
+                swap_diss_x_local_w(k,j) = - abs(u_comp) * (                  &
+                                         10.  * ( w(k,j,i) - w(k,j,i-1)   )   &
+                                         - 5. * ( w(k,j,i+1) - w(k,j,i-2) )   &
+                                         +      ( w(k,j,i+2) - w(k,j,i-3) ) ) &
+                                         * adv_mom_5
+             ENDDO
+             
+          ENDIF
+          
+       ENDDO
+
+       DO i = nxl, nxr
+         j = nys
+         IF ( boundary_flags(j,i) == 8 )  THEN
+         
+            DO  k = nzb_v_inner(j,i) + 1, nzt
+               v_comp                 = v(k+1,j,i) + v(k,j,i) - gv
+               swap_flux_y_local_w(k) = v_comp *                              &
+                                        ( w(k,j,i) + w(k,j-1,i) ) * 0.25
+               swap_diss_y_local_w(k) = diss_2nd( w(k,j+2,i), w(k,j+1,i),     &
+                                                  w(k,j,i), w(k,j-1,i),       &
+                                                  w(k,j-1,i), v_comp, 0.25,ddy)
+            ENDDO
+            
+         ELSE
+         
+            DO  k = nzb_v_inner(j,i) + 1, nzt            
+               v_comp                 = v(k+1,j,i) + v(k,j,i) - gv
+               swap_flux_y_local_w(k) = v_comp * (                            &
+                                      37. *  ( w(k,j,i) + w(k,j-1,i)  )       &
+                                      - 8. * ( w(k,j+1,i) +w(k,j-2,i) )       &
+                                      +      ( w(k,j+2,i) +w(k,j-3,i) ) )     &
+                                      * adv_mom_5
+               swap_diss_y_local_w(k) = - abs(v_comp) * (                     &
+                                      10.  * ( w(k,j,i) - w(k,j-1,i)   )      &
+                                      - 5. * ( w(k,j+1,i) - w(k,j-2,i) )      &
+                                      +      ( w(k,j+2,i) - w(k,j-3,i) ) )    &
+                                      * adv_mom_5
+            ENDDO
+            
+         ENDIF
+         
+         DO  j = nys, nyn
+
+            IF ( boundary_flags(j,i) /= 0 )  THEN
+!       
+!--            Degrade the order for Dirichlet bc. at the outflow boundary
+               SELECT CASE ( boundary_flags(j,i) )
+         
+                  CASE ( 1 )
+                     DO  k = nzb_w_inner(j,i) + 1, nzt
+                        u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
+                        flux_r(k) = u_comp * (                                &
+                              7. * ( w(k,j,i+1) + w(k,j,i)   )                &
+                              -    ( w(k,j,i+2) + w(k,j,i-1) ) ) * adv_mom_3
+                        diss_r(k) = -abs(u_comp) * (                          &
+                              3. * ( w(k,j,i+1) - w(k,j,i)   )                & 
+                              -    ( w(k,j,i+2) - w(k,j,i-1) ) ) * adv_mom_3  
+                     ENDDO
+               
+                  CASE ( 2 )
+                     DO  k = nzb_w_inner(j,i) + 1, nzt
+                        u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
+                        flux_r(k) = u_comp * ( w(k,j,i+1) + w(k,j,i) ) * 0.25 
+                        diss_r(k) = diss_2nd( w(k,j,i+1), w(k,j,i+1),         &
+                                              w(k,j,i), w(k,j,i-1),           &
+                                              w(k,j,i-2), u_comp, 0.25, ddx )
+                     ENDDO
+               
+                  CASE ( 3 )
+                     DO  k = nzb_w_inner(j,i) + 1, nzt
+                        v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
+                        flux_n(k) = v_comp * (                                &
+                              7. * ( w(k,j+1,i) + w(k,j,i)   )                &
+                              -    ( w(k,j+2,i) + w(k,j-1,i) ) ) * adv_mom_3
+                        diss_n(k) = -abs(v_comp) * (                          &
+                              3. * ( w(k,j+1,i) - w(k,j,i)   )                & 
+                              -    ( w(k,j+2,i) - w(k,j-1,i) ) ) * adv_mom_3
+                     ENDDO
+               
+                  CASE ( 4 )
+                     DO  k = nzb_w_inner(j,i) + 1, nzt
+                        v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
+                        flux_n(k) = v_comp * ( w(k,j+1,i) + w(k,j,i) ) * 0.25 
+                        diss_n(k) = diss_2nd( w(k,j+1,i), w(k,j+1,i),         &
+                                              w(k,j,i), w(k,j-1,i),           &
+                                              w(k,j-2,i), v_comp, 0.25, ddy )
+                     ENDDO
+               
+                  CASE ( 5 )
+                     DO  k = nzb_w_inner(j,i) + 1, nzt
+                        u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu 
+                        flux_r(k) = u_comp * (                                &
+                              7. * ( w(k,j,i+1) + w(k,j,i)   )                &
+                              -    ( w(k,j,i+2) + w(k,j,i-1) ) ) * adv_mom_3
+                        diss_r(k) = - abs(u_comp) * (                         &
+                              3. * ( w(k,j,i+1) - w(k,j,i) )                  & 
+                              -    ( w(k,j,i+2) - w(k,j,i-1) ) ) * adv_mom_3
+                     ENDDO
+               
+                  CASE ( 6 )
+                     DO  k = nzb_w_inner(j,i) + 1, nzt
+                        u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
+                        flux_r(k) = u_comp *(                                 &
+                              7. * ( w(k,j,i+1) + w(k,j,i)   )                &
+                              -    ( w(k,j,i+2) + w(k,j,i-1) ) )  * adv_mom_3
+                        diss_r(k) = - abs(u_comp) * (                         &
+                              3. * ( w(k,j,i+1) - w(k,j,i) )                  & 
+                              -    ( w(k,j,i+2) - w(k,j,i-1) ) ) * adv_mom_3
+                     ENDDO
+               
+                  CASE ( 7 )
+                     DO  k = nzb_w_inner(j,i) + 1, nzt
+                        v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
+                        flux_n(k) = v_comp *(                                 &
+                              7. * ( w(k,j+1,i) + w(k,j,i)   )                &
+                              -    ( w(k,j+2,i) + w(k,j-1,i) ) ) * adv_mom_3
+                        diss_n(k) = - abs(v_comp) * (                         &
+                              3. * ( w(k,j+1,i) - w(k,j,i)   )                & 
+                              -    ( w(k,j+2,i) - w(k,j-1,i) ) ) * adv_mom_3
+                      ENDDO
+                
+                  CASE ( 8 )
+                     DO  k = nzb_w_inner(j,i) + 1, nzt
+                        v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
+                        flux_n(k) = v_comp * (                                &
+                              7. * ( w(k,j+1,i) + w(k,j,i)   )                &
+                             -     ( w(k,j+2,i) + w(k,j-1,i) ) ) * adv_mom_3
+                        diss_n(k) = - abs(v_comp) * (                         &
+                              3. * ( w(k,j+1,i) - w(k,j,i) )                  & 
+                              -    ( w(k,j+2,i) - w(k,j-1,i) ) ) * adv_mom_3
+                
+                     ENDDO  
+               
+                  CASE DEFAULT
+            
+               END SELECT
+!         
+!--      Compute the crosswise 5th order fluxes at the outflow
+               IF ( boundary_flags(j,i) == 1 .OR. boundary_flags(j,i) == 2    &
+            .OR. boundary_flags(j,i) == 5 .OR. boundary_flags(j,i) == 6 )  THEN
+         
+                  DO  k = nzb_w_inner(j,i) + 1, nzt
+                     v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
+                     flux_n(k) = v_comp * (                                   &
+                           37.  * ( w(k,j+1,i) + w(k,j,i)   )                 &
+                           - 8. * ( w(k,j+2,i) + w(k,j-1,i) )                 &
+                           +      ( w(k,j+3,i) + w(k,j-2,i) ) ) * adv_mom_5
+                     diss_n(k) = - abs(v_comp) * (                            &
+                           10.  * ( w(k,j+1,i) - w(k,j,i)   )                 &
+                           - 5. * ( w(k,j+2,i) - w(k,j-1,i) )                 &
+                           +      ( w(k,j+3,i) - w(k,j-2,i) ) ) * adv_mom_5
+                  ENDDO
+            
+               ELSE
+         
+                  DO  k = nzb_w_inner(j,i) + 1, nzt         
+                     u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
+                     flux_r(k) = u_comp * (                                   &
+                           37.  * ( w(k,j,i+1) + w(k,j,i) )                   &
+                           - 8. * ( w(k,j,i+2) + w(k,j,i-1) )                 &
+                           +      ( w(k,j,i+3) + w(k,j,i-2) ) ) * adv_mom_5
+                     diss_r(k) = - abs(u_comp) * (                            &
+                           10.  * ( w(k,j,i+1) - w(k,j,i)   )                 &
+                           - 5. * ( w(k,j,i+2) - w(k,j,i-1) )                 &
+                            +      ( w(k,j,i+3) - w(k,j,i-2) ) ) * adv_mom_5
+                  ENDDO
+            
+               ENDIF
+         
+             ELSE
+!       
+!--            Compute the fifth order fluxes for the interior of PE domain.               
+               DO  k = nzb_w_inner(j,i) + 1, nzt
+                  u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
+                  flux_r(k) = u_comp * (                                      &
+                        37.  * ( w(k,j,i+1) + w(k,j,i)   )                    &
+                        - 8. * ( w(k,j,i+2) + w(k,j,i-1) )                    &
+                        +      ( w(k,j,i+3) + w(k,j,i-2) ) ) * adv_mom_5
+                  diss_r(k) = - abs(u_comp) * (                               &
+                        10.  * ( w(k,j,i+1) - w(k,j,i)   )                    &
+                        - 5. * ( w(k,j,i+2) - w(k,j,i-1) )                    &
+                        +      ( w(k,j,i+3) - w(k,j,i-2) ) ) * adv_mom_5
+                 
+                  v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
+                  flux_n(k) = v_comp * (                                      &
+                        37.  * ( w(k,j+1,i) + w(k,j,i)   )                    &
+                        - 8. * ( w(k,j+2,i) + w(k,j-1,i) )                    &
+                        +      ( w(k,j+3,i) + w(k,j-2,i) ) ) * adv_mom_5
+                  diss_n(k) = - abs(v_comp) * (                               &
+                        10.  * ( w(k,j+1,i) - w(k,j,i)   )                    &
+                        - 5. * ( w(k,j+2,i) - w(k,j-1,i) )                    &
+                        +      ( w(k,j+3,i) - w(k,j-2,i) ) ) * adv_mom_5
+               ENDDO 
+         
+            ENDIF
+            
+            DO  k = nzb_v_inner(j,i)+1, nzt
+               tend(k,j,i) = tend(k,j,i) - (                                  &
+              ( flux_r(k) + diss_r(k)                                         &
+            - ( swap_flux_x_local_w(k,j) + swap_diss_x_local_w(k,j) ) ) * ddx &
+            + ( flux_n(k) + diss_n(k)                                         &
+            - ( swap_flux_y_local_w(k) + swap_diss_y_local_w(k)     ) ) * ddy )
+               
+               swap_flux_x_local_w(k,j) = flux_r(k)
+               swap_diss_x_local_w(k,j) = diss_r(k)
+               swap_flux_y_local_w(k)   = flux_n(k)  
+               swap_diss_y_local_w(k)   = diss_n(k)
+            ENDDO
+         ENDDO
+       ENDDO
+
+!
+!--    Vertical advection, degradation of order near surface and top. 
+!--    The fluxes flux_d and diss_d at the surface are 0. Due to reasons of
+!--    statistical evaluation the top flux at the surface should be 0
+       DO i = nxl, nxr
+          DO  j = nys, nyn
+            k      = nzb_w_inner(j,i) + 1
+            flux_d = w(k-1,j,i) * ( w(k,j,i) + w(k-1,j,i))
+            flux_t(k-1) = flux_d
+            diss_d = 0.0
+            diss_t(k-1) = diss_d
+!
+!--         2nd order scheme            
+            w_comp = w(k+1,j,i) + w(k,j,i)
+            flux_t(k) = w_comp * ( w(k+1,j,i) + w(k,j,i) ) * 0.25
+            diss_t(k) = diss_2nd( w(k+2,j,i), w(k+1,j,i), w(k,j,i), 0., 0.,   &
+                                  w_comp, 0.25, ddzu(k+1) )
+
+            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
+                                 - ( flux_d + diss_d )     ) * ddzu(k+1)
+!                                 
+!--        WS3 as an intermediate step
+            k         = nzb_w_inner(j,i) + 2
+            flux_d    = flux_t(k-1)
+            diss_d    = diss_t(k-1)
+            w_comp    = w(k+1,j,i)+w(k,j,i)
+            flux_t(k) = w_comp * (                                            &
+                        7. * ( w(k+1,j,i) + w(k,j,i) )                        &
+                        - ( w(k+2,j,i) + w(k-1,j,i) ) ) * adv_mom_3
+            diss_t(k) = - abs(w_comp) * (                                     &
+                        3. * ( w(k+1,j,i) - w(k,j,i) )                        & 
+                        - ( w(k+2,j,i) - w(k-1,j,i) ) ) * adv_mom_3
+                        
+            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
+                                 - ( flux_d + diss_d )     ) * ddzu(k+1)
+!
+!--         WS5
+            DO  k = nzb_v_inner(j,i) + 3, nzt
+               flux_d = flux_t(k-1)
+               diss_d = diss_t(k-1)
+
+               w_comp = w(k+1,j,i) + w(k,j,i)
+               flux_t(k) = w_comp * (                                         &
+                           37.  * ( w(k+1,j,i) + w(k,j,i)   )                 &
+                           - 8. * ( w(k+2,j,i) + w(k-1,j,i) )                 &
+                           +      ( w(k+3,j,i) + w(k-2,j,i) ) ) * adv_mom_5
+               diss_t(k) = - abs(w_comp) * (                                  &
+                           10.  * ( w(k+1,j,i) - w(k,j,i)   )                 & 
+                           - 5. * ( w(k+2,j,i) - w(k-1,j,i) )                 &
+                           +      ( w(k+3,j,i) - w(k-2,j,i) ) ) * adv_mom_5
+
+               tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)            &
+                                 - ( flux_d + diss_d )     ) * ddzu(k+1)
+            ENDDO
+!                                 
+!--        WS3 as an intermediate step
+            DO k = nzt - 2, nzt - 1
+               flux_d    = flux_t(k-1)
+               diss_d    = diss_t(k-1)
+               w_comp    = w(k+1,j,i)+w(k,j,i)
+               flux_t(k) = w_comp * (                                         &
+                           7. * ( w(k+1,j,i) + w(k,j,i) )                     &
+                           - ( w(k+2,j,i) + w(k-1,j,i) ) ) * adv_mom_3
+               diss_t(k) = - abs(w_comp) * (                                  &
+                           3. * ( w(k+1,j,i) - w(k,j,i) )                     & 
+                           - ( w(k+2,j,i) - w(k-1,j,i) ) ) * adv_mom_3
+                        
+               tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)            &
+                                 - ( flux_d + diss_d )     ) * ddzu(k+1)
+            ENDDO
+!
+!--         2nd order scheme  
+            k = nzt
+            flux_d = flux_t(k-1)
+            diss_d = diss_t(k-1)
+            w_comp = w(k+1,j,i) + w(k,j,i)
+            flux_t(k) = w_comp * ( w(k+1,j,i) + w(k,j,i) ) * 0.25
+            diss_t(k) = diss_2nd( w(nzt+1,j,i), w(nzt+1,j,i), w(k,j,i),       &
+                                  w(k-1,j,i), w(k-2,j,i), w_comp,             &
+                                  0.25, ddzu(k+1) ) 
+
+            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
+                                 - ( flux_d + diss_d )     ) * ddzu(k+1)
+!
+!--         at last vertical momentum flux is accumulated
+            DO  k = nzb_w_inner(j,i), nzt
+               sums_ws2_ws_l(k,:)  = sums_ws2_ws_l(k,:)                        &
+                   + ( flux_t(k) + diss_t(k) )                                 &
+                   * weight_substep(intermediate_timestep_count)               &
+                   * rmask(j,i,:)
+            ENDDO
+
+          ENDDO
+       ENDDO
+
+    END SUBROUTINE advec_w_ws
+    
+ 
+ 
+    SUBROUTINE local_diss_ij( i, j, ar, var_char )
+    
+    END SUBROUTINE local_diss_ij
+
+    SUBROUTINE local_diss( ar )
+
+
+    END SUBROUTINE local_diss
+    
+!   
+!-- Computation of 2nd order dissipation. This numerical dissipation is 
+!-- necessary to keep a stable numerical solution in region where the
+!-- order of the schemes degraded.
+ 
+     REAL FUNCTION diss_2nd( v2, v1, v0, vm1, vm2, vel_comp, factor, grid )   &
+                            RESULT(dissip)
+        
+       IMPLICIT NONE 
+        
+       REAL, INTENT(IN)  :: v2, v1, v0, vm1, vm2, vel_comp, factor,            &
+                            grid
+       REAL :: value_min_m, value_max_m, value_min, value_max,                 &
+               value_min_p, value_max_p, diss_m, diss_0, diss_p
+             
+       value_min_m = MIN(v0, vm1, vm2)
+       value_max_m = MAX(v0, vm1, vm2)
+       value_min = MIN(v1, v0, vm2)
+       value_max = MAX(v1, v0, vm2)
+       value_min_p = MIN(v2, v1, v0)
+       value_max_p = MAX(v2, v1, v0)
+       
+       diss_m = MAX(0.,vm1 - value_max_m) + MIN(0.,vm1 - value_min_m)
+       diss_0 = MAX(0.,v0 - value_max) + MIN(0.,v0 - value_min)
+       diss_p = MAX(0.,v1 - value_max_p) + MIN(0.,v1 - value_min_p)
+       
+       dissip = abs(vel_comp) * (diss_p - 2.*diss_0 + diss_m)         &
+                         * grid**2 * factor
+              
+    END FUNCTION diss_2nd
+
+ END MODULE advec_ws
Index: /palm/trunk/SOURCE/asselin_filter.f90
===================================================================
--- /palm/trunk/SOURCE/asselin_filter.f90	(revision 666)
+++ /palm/trunk/SOURCE/asselin_filter.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng
 !
 ! Former revisions:
@@ -49,6 +49,6 @@
 !$OMP PARALLEL PRIVATE (i,j,k)
 !$OMP DO
-    DO  i = nxl-1, nxr+1
-       DO  j = nys-1, nyn+1
+    DO  i = nxlg, nxrg
+       DO  j = nysg, nyng
 
           DO  k = nzb_2d(j,i), nzt+1
Index: /palm/trunk/SOURCE/average_3d_data.f90
===================================================================
--- /palm/trunk/SOURCE/average_3d_data.f90	(revision 666)
+++ /palm/trunk/SOURCE/average_3d_data.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng
 !
 ! Former revisions:
@@ -58,6 +58,6 @@
 
           CASE ( 'e' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       e_av(k,j,i) = e_av(k,j,i) / REAL( average_count_3d )
@@ -67,6 +67,6 @@
 
           CASE ( 'qsws*' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    qsws_av(j,i) = qsws_av(j,i) / REAL( average_count_3d )
                 ENDDO
@@ -74,6 +74,6 @@
 
           CASE ( 'lwp*' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    lwp_av(j,i) = lwp_av(j,i) / REAL( average_count_3d )
                 ENDDO
@@ -81,6 +81,6 @@
 
           CASE ( 'p' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       p_av(k,j,i) = p_av(k,j,i) / REAL( average_count_3d )
@@ -108,6 +108,6 @@
 
           CASE ( 'prr*' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    precipitation_rate_av(j,i) = precipitation_rate_av(j,i) / &
                                                 REAL( average_count_3d )
@@ -116,6 +116,6 @@
 
           CASE ( 'pt' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       pt_av(k,j,i) = pt_av(k,j,i) / REAL( average_count_3d )
@@ -125,6 +125,6 @@
 
           CASE ( 'q' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       q_av(k,j,i) = q_av(k,j,i) / REAL( average_count_3d )
@@ -134,6 +134,6 @@
 
           CASE ( 'ql' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       ql_av(k,j,i) = ql_av(k,j,i) / REAL( average_count_3d )
@@ -143,6 +143,6 @@
 
           CASE ( 'ql_c' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       ql_c_av(k,j,i) = ql_c_av(k,j,i) / REAL( average_count_3d )
@@ -152,6 +152,6 @@
 
           CASE ( 'ql_v' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       ql_v_av(k,j,i) = ql_v_av(k,j,i) / REAL( average_count_3d )
@@ -161,6 +161,6 @@
 
           CASE ( 'ql_vp' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       ql_vp_av(k,j,i) = ql_vp_av(k,j,i) / &
@@ -171,6 +171,6 @@
 
           CASE ( 'qv' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       qv_av(k,j,i) = qv_av(k,j,i) / REAL( average_count_3d )
@@ -180,6 +180,6 @@
 
           CASE ( 'rho' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       rho_av(k,j,i) = rho_av(k,j,i) / REAL( average_count_3d )
@@ -189,6 +189,6 @@
 
           CASE ( 's' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       s_av(k,j,i) = s_av(k,j,i) / REAL( average_count_3d )
@@ -198,6 +198,6 @@
 
           CASE ( 'sa' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       sa_av(k,j,i) = sa_av(k,j,i) / REAL( average_count_3d )
@@ -207,6 +207,6 @@
 
          CASE ( 'shf*' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    shf_av(j,i) = shf_av(j,i) / REAL( average_count_3d )
                 ENDDO
@@ -214,6 +214,6 @@
 
           CASE ( 't*' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    ts_av(j,i) = ts_av(j,i) / REAL( average_count_3d )
                 ENDDO
@@ -221,6 +221,6 @@
 
           CASE ( 'u' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       u_av(k,j,i) = u_av(k,j,i) / REAL( average_count_3d )
@@ -230,6 +230,6 @@
 
           CASE ( 'u*' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    us_av(j,i) = us_av(j,i) / REAL( average_count_3d )
                 ENDDO
@@ -237,6 +237,6 @@
 
           CASE ( 'v' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       v_av(k,j,i) = v_av(k,j,i) / REAL( average_count_3d )
@@ -246,6 +246,6 @@
 
           CASE ( 'vpt' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       vpt_av(k,j,i) = vpt_av(k,j,i) / REAL( average_count_3d )
@@ -255,6 +255,6 @@
 
           CASE ( 'w' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       w_av(k,j,i) = w_av(k,j,i) / REAL( average_count_3d )
@@ -264,6 +264,6 @@
 
           CASE ( 'z0*' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    z0_av(j,i) = z0_av(j,i) / REAL( average_count_3d )
                 ENDDO
Index: /palm/trunk/SOURCE/boundary_conds.f90
===================================================================
--- /palm/trunk/SOURCE/boundary_conds.f90	(revision 666)
+++ /palm/trunk/SOURCE/boundary_conds.f90	(revision 667)
@@ -4,6 +4,12 @@
 ! Current revisions:
 ! -----------------
+!
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng
+!
 ! 
-!
+! Removed mirror boundary conditions for u and v at the bottom in case of 
+! ibc_uv_b == 0. Instead, dirichelt boundary conditions (u=v=0) are set
+! in init_3d_model 
+!  
 ! Former revisions:
 ! -----------------
@@ -70,17 +76,11 @@
     IF ( range == 'main')  THEN
 !
-!--    Bottom boundary
-       IF ( ibc_uv_b == 0 )  THEN
-!
-!--       Satisfying the Dirichlet condition with an extra layer below the
-!--       surface where the u and v component change their sign
-          u_p(nzb,:,:) = -u_p(nzb+1,:,:)
-          v_p(nzb,:,:) = -v_p(nzb+1,:,:)
-       ELSE
+!--    Bottom boundary 
+       IF ( ibc_uv_b == 1 )  THEN
           u_p(nzb,:,:) = u_p(nzb+1,:,:)
           v_p(nzb,:,:) = v_p(nzb+1,:,:)
        ENDIF
-       DO  i = nxl-1, nxr+1
-          DO  j = nys-1, nyn+1
+       DO  i = nxlg, nxrg
+          DO  j = nysg, nyng
              w_p(nzb_w_inner(j,i),j,i) = 0.0
           ENDDO
@@ -90,9 +90,9 @@
 !--    Top boundary
        IF ( ibc_uv_t == 0 )  THEN
-          u_p(nzt+1,:,:) = ug(nzt+1)
-          v_p(nzt+1,:,:) = vg(nzt+1)
+           u_p(nzt+1,:,:) = ug(nzt+1)
+           v_p(nzt+1,:,:) = vg(nzt+1)
        ELSE
-          u_p(nzt+1,:,:) = u_p(nzt,:,:)
-          v_p(nzt+1,:,:) = v_p(nzt,:,:)
+           u_p(nzt+1,:,:) = u_p(nzt,:,:)
+           v_p(nzt+1,:,:) = v_p(nzt,:,:)
        ENDIF
        w_p(nzt:nzt+1,:,:) = 0.0  ! nzt is not a prognostic level (but cf. pres)
@@ -103,12 +103,12 @@
 !--    the sea surface temperature of the coupled ocean model.
        IF ( ibc_pt_b == 0 )  THEN
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
                 pt_p(nzb_s_inner(j,i),j,i) = pt(nzb_s_inner(j,i),j,i)
              ENDDO
           ENDDO
        ELSEIF ( ibc_pt_b == 1 )  THEN
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
                 pt_p(nzb_s_inner(j,i),j,i) = pt_p(nzb_s_inner(j,i)+1,j,i)
              ENDDO
@@ -119,9 +119,9 @@
 !--    Temperature at top boundary
        IF ( ibc_pt_t == 0 )  THEN
-          pt_p(nzt+1,:,:) = pt(nzt+1,:,:)
+           pt_p(nzt+1,:,:) = pt(nzt+1,:,:)
        ELSEIF ( ibc_pt_t == 1 )  THEN
-          pt_p(nzt+1,:,:) = pt_p(nzt,:,:)
+           pt_p(nzt+1,:,:) = pt_p(nzt,:,:)
        ELSEIF ( ibc_pt_t == 2 )  THEN
-          pt_p(nzt+1,:,:) = pt_p(nzt,:,:)   + bc_pt_t_val * dzu(nzt+1)
+           pt_p(nzt+1,:,:) = pt_p(nzt,:,:)   + bc_pt_t_val * dzu(nzt+1)
        ENDIF
 
@@ -130,6 +130,6 @@
 !--    Generally Neumann conditions with de/dz=0 are assumed
        IF ( .NOT. constant_diffusion )  THEN
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
                 e_p(nzb_s_inner(j,i),j,i) = e_p(nzb_s_inner(j,i)+1,j,i)
              ENDDO
@@ -144,6 +144,6 @@
 !--       Bottom boundary: Neumann condition because salinity flux is always
 !--       given
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
                 sa_p(nzb_s_inner(j,i),j,i) = sa_p(nzb_s_inner(j,i)+1,j,i)
              ENDDO
@@ -153,7 +153,7 @@
 !--       Top boundary: Dirichlet or Neumann
           IF ( ibc_sa_t == 0 )  THEN
-             sa_p(nzt+1,:,:) = sa(nzt+1,:,:)
+              sa_p(nzt+1,:,:) = sa(nzt+1,:,:)
           ELSEIF ( ibc_sa_t == 1 )  THEN
-             sa_p(nzt+1,:,:) = sa_p(nzt,:,:)
+              sa_p(nzt+1,:,:) = sa_p(nzt,:,:)
           ENDIF
 
@@ -167,12 +167,12 @@
 !--       Surface conditions for constant_humidity_flux
           IF ( ibc_q_b == 0 ) THEN
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    q_p(nzb_s_inner(j,i),j,i) = q(nzb_s_inner(j,i),j,i)
                 ENDDO
              ENDDO
           ELSE
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    q_p(nzb_s_inner(j,i),j,i) = q_p(nzb_s_inner(j,i)+1,j,i)
                 ENDDO
@@ -182,4 +182,6 @@
 !--       Top boundary
           q_p(nzt+1,:,:) = q_p(nzt,:,:)   + bc_q_t_val * dzu(nzt+1)
+
+
        ENDIF
 
@@ -226,5 +228,5 @@
        c_max = dy / dt_3d
 
-       DO i = nxl-1, nxr+1
+       DO i = nxlg, nxrg
           DO k = nzb+1, nzt+1
 
@@ -299,6 +301,6 @@
 !--    Bottom boundary at the outflow
        IF ( ibc_uv_b == 0 )  THEN
-          u_p(nzb,-1,:) = -u_p(nzb+1,-1,:) 
-          v_p(nzb,0,:)  = -v_p(nzb+1,0,:)  
+          u_p(nzb,-1,:) = 0.0 
+          v_p(nzb,0,:)  = 0.0  
        ELSE                    
           u_p(nzb,-1,:) =  u_p(nzb+1,-1,:)
@@ -324,5 +326,5 @@
        c_max = dy / dt_3d
 
-       DO i = nxl-1, nxr+1
+       DO i = nxlg, nxrg
           DO k = nzb+1, nzt+1
 
@@ -397,6 +399,6 @@
 !--    Bottom boundary at the outflow
        IF ( ibc_uv_b == 0 )  THEN
-          u_p(nzb,ny+1,:) = -u_p(nzb+1,ny+1,:) 
-          v_p(nzb,ny+1,:) = -v_p(nzb+1,ny+1,:)  
+          u_p(nzb,ny+1,:) = 0.0 
+          v_p(nzb,ny+1,:) = 0.0   
        ELSE                    
           u_p(nzb,ny+1,:) =  u_p(nzb+1,ny+1,:)
@@ -422,5 +424,5 @@
        c_max = dx / dt_3d
 
-       DO j = nys-1, nyn+1
+       DO j = nysg, nyng
           DO k = nzb+1, nzt+1
 
@@ -495,8 +497,8 @@
 !--    Bottom boundary at the outflow
        IF ( ibc_uv_b == 0 )  THEN
-          u_p(nzb,:,-1) = -u_p(nzb+1,:,-1) 
-          v_p(nzb,:,-1) = -v_p(nzb+1,:,-1)  
+          u_p(nzb,:,0)  = 0.0 
+          v_p(nzb,:,-1) = 0.0
        ELSE                    
-          u_p(nzb,:,-1) =  u_p(nzb+1,:,-1)
+          u_p(nzb,:,0)  =  u_p(nzb+1,:,0)
           v_p(nzb,:,-1) =  v_p(nzb+1,:,-1)
        ENDIF
@@ -520,5 +522,5 @@
        c_max = dx / dt_3d
 
-       DO j = nys-1, nyn+1
+       DO j = nysg, nyng
           DO k = nzb+1, nzt+1
 
@@ -593,6 +595,6 @@
 !--    Bottom boundary at the outflow
        IF ( ibc_uv_b == 0 )  THEN
-          u_p(nzb,:,nx+1) = -u_p(nzb+1,:,nx+1) 
-          v_p(nzb,:,nx+1) = -v_p(nzb+1,:,nx+1)  
+          u_p(nzb,:,nx+1) = 0.0
+          v_p(nzb,:,nx+1) = 0.0 
        ELSE                    
           u_p(nzb,:,nx+1) =  u_p(nzb+1,:,nx+1)
Index: /palm/trunk/SOURCE/calc_liquid_water_content.f90
===================================================================
--- /palm/trunk/SOURCE/calc_liquid_water_content.f90	(revision 666)
+++ /palm/trunk/SOURCE/calc_liquid_water_content.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng
 !
 ! Former revisions:
@@ -47,6 +47,6 @@
     REAL :: alpha, e_s, q_s, t_l
 
-    DO  i = nxl-1, nxr+1
-       DO  j = nys-1, nyn+1
+    DO  i = nxlg, nxrg
+       DO  j = nysg, nyng
           DO  k = nzb_2d(j,i)+1, nzt
 
Index: /palm/trunk/SOURCE/calc_precipitation.f90
===================================================================
--- /palm/trunk/SOURCE/calc_precipitation.f90	(revision 666)
+++ /palm/trunk/SOURCE/calc_precipitation.f90	(revision 667)
@@ -8,5 +8,5 @@
 ! Former revisions:
 ! -----------------
-! $Id$
+! $Id: calc_precipitation.f90 484 2010-02-05 07:36:54Z raasch 
 !
 ! 403 2009-10-22 13:57:16Z franke
Index: /palm/trunk/SOURCE/calc_spectra.f90
===================================================================
--- /palm/trunk/SOURCE/calc_spectra.f90	(revision 666)
+++ /palm/trunk/SOURCE/calc_spectra.f90	(revision 667)
@@ -4,5 +4,6 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng for allocation
+! of tend
 !
 ! Former revisions:
@@ -152,5 +153,5 @@
     IF ( nxra > nxr  .OR.  nyna > nyn  .OR.  nza > nz )  THEN
        DEALLOCATE( tend )
-       ALLOCATE( tend(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+       ALLOCATE( tend(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
     ENDIF
 
Index: /palm/trunk/SOURCE/check_for_restart.f90
===================================================================
--- /palm/trunk/SOURCE/check_for_restart.f90	(revision 666)
+++ /palm/trunk/SOURCE/check_for_restart.f90	(revision 667)
@@ -5,4 +5,5 @@
 ! -----------------
 ! 
+! Exchange of terminate_coupled between ocean and atmosphere by PE0
 !
 ! Former revisions:
@@ -93,9 +94,12 @@
        terminate_coupled = 3
 #if defined( __parallel )
-       CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER,          &
-                          target_id, 0,                                      &
-                          terminate_coupled_remote, 1, MPI_INTEGER,          &
-                          target_id, 0,                                      &
-                          comm_inter, status, ierr )
+       IF ( myid == 0 ) THEN
+          CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER,          &
+                             target_id, 0,                                      &
+                             terminate_coupled_remote, 1, MPI_INTEGER,          &
+                             target_id, 0,                                      &
+                             comm_inter, status, ierr )
+       ENDIF
+       CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_INTEGER, 0, comm2d, ierr)
 #endif
     ENDIF
@@ -140,9 +144,13 @@
              ENDIF
 #if defined( __parallel )
-             CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER,    &
-                                target_id,  0,                               &
-                                terminate_coupled_remote, 1, MPI_INTEGER,    &
-                                target_id,  0,                               &
-                                comm_inter, status, ierr )
+             IF ( myid == 0 ) THEN
+                CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER,    &
+                                   target_id,  0,                               &
+                                   terminate_coupled_remote, 1, MPI_INTEGER,    &
+                                   target_id,  0,                               &
+                                   comm_inter, status, ierr )   
+             ENDIF
+             CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_INTEGER, 0, comm2d, ierr)  
+            
 #endif
           ENDIF
Index: /palm/trunk/SOURCE/check_open.f90
===================================================================
--- /palm/trunk/SOURCE/check_open.f90	(revision 666)
+++ /palm/trunk/SOURCE/check_open.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! Output of total array size was adapted to nbgp.
 !
 ! Former revisions:
@@ -278,5 +278,5 @@
 !--          Output for combine_plot_fields
              IF ( data_output_2d_on_each_pe  .AND.  myid_char /= '' )  THEN
-                WRITE (21)  -1, nx+1, -1, ny+1    ! total array size
+                WRITE (21)  -nbgp, nx+nbgp, -nbgp, ny+nbgp    ! total array size
                 WRITE (21)   0, nx+1,  0, ny+1    ! output part
              ENDIF
@@ -319,5 +319,5 @@
 !--          Output for combine_plot_fields
              IF ( data_output_2d_on_each_pe  .AND.  myid_char /= '' )  THEN
-                WRITE (22)  -1, nx+1, 0, nz+1    ! total array size
+                WRITE (22)  -nbgp, nx+nbgp, 0, nz+1    ! total array size
                 WRITE (22)   0, nx+1, 0, nz+1    ! output part
              ENDIF
@@ -357,5 +357,5 @@
 !--          Output for combine_plot_fields
              IF ( data_output_2d_on_each_pe  .AND.  myid_char /= '' )  THEN
-                WRITE (23)  -1, ny+1, 0, nz+1    ! total array size
+                WRITE (23)  -nbgp, ny+nbgp, 0, nz+1    ! total array size
                 WRITE (23)   0, ny+1, 0, nz+1    ! output part
              ENDIF
@@ -392,5 +392,5 @@
 !--          Specifications for combine_plot_fields
              IF ( .NOT. do3d_compress )  THEN
-                WRITE ( 30 )  -1,nx+1,-1,ny+1,0,nz_do3d
+                WRITE ( 30 )  -nbgp,nx+nbgp,-nbgp,ny+nbgp, 0 ,nz_do3d
                 WRITE ( 30 )  0,nx+1,0,ny+1,0,nz_do3d
              ENDIF
@@ -503,6 +503,6 @@
                 openfile(33)%opened = .TRUE.
                 WRITE ( 33, 3300 )  TRIM( avs_coor_file ), &
-                                    TRIM( avs_coor_file ), (nx+2)*4, &
-                                    TRIM( avs_coor_file ), (nx+2)*4+(ny+2)*4
+                                    TRIM( avs_coor_file ), (nx+2*nbgp)*4, &
+                                    TRIM( avs_coor_file ), (nx+2*nbgp)*4+(ny+2*nbgp)*4
             
 
@@ -548,5 +548,5 @@
 !--       corresponding partial array of a PE only once at the top of the file
           IF ( avs_output  .AND.  do3d_compress )  THEN
-             WRITE ( 30 )  nxl-1, nxr+1, nys-1, nyn+1, nzb, nz_do3d
+             WRITE ( 30 )  nxlg, nxrg, nysg, nyng, nzb, nz_do3d
           ENDIF
 
@@ -929,5 +929,5 @@
 #endif
              ENDIF
-
+             
              CALL handle_netcdf_error( 'check_open', 26 )
 !
@@ -1325,5 +1325,5 @@
 #endif
              ENDIF
-
+             
              CALL handle_netcdf_error( 'check_open', 43 )  
 
Index: /palm/trunk/SOURCE/check_parameters.f90
===================================================================
--- /palm/trunk/SOURCE/check_parameters.f90	(revision 666)
+++ /palm/trunk/SOURCE/check_parameters.f90	(revision 667)
@@ -4,5 +4,18 @@
 ! Current revisions:
 ! -----------------
+!
 ! 
+! Exchange of parameters between ocean and atmosphere via PE0
+! Check for illegal combination of ws-scheme and timestep scheme.
+! Check for topography and ws-scheme.
+! Check for not cyclic boundary conditions in combination with ws-scheme and
+! loop_optimization = 'vector'.
+! Check for call_psolver_at_all_substeps and ws-scheme for momentum_advec.
+!
+! Different processor/grid topology in atmosphere and ocean is now allowed!
+!
+! Bugfixes in checking for conserve_volume_flow_mode
+!
+! Adapt error messages.
 !
 ! Former revisions:
@@ -180,5 +193,5 @@
 !
 !-- Check dt_coupling, restart_time, dt_restart, end_time, dx, dy, nx and ny
-    IF ( coupling_mode /= 'uncoupled' )  THEN
+    IF ( coupling_mode /= 'uncoupled')  THEN
 
        IF ( dt_coupling == 9999999.9 )  THEN
@@ -189,8 +202,12 @@
 
 #if defined( __parallel )
-       CALL MPI_SEND( dt_coupling, 1, MPI_REAL, target_id, 11, comm_inter, &
-                      ierr )
-       CALL MPI_RECV( remote, 1, MPI_REAL, target_id, 11, comm_inter, &
-                      status, ierr )
+       IF ( myid == 0 ) THEN
+          CALL MPI_SEND( dt_coupling, 1, MPI_REAL, target_id, 11, comm_inter, &
+                         ierr )
+          CALL MPI_RECV( remote, 1, MPI_REAL, target_id, 11, comm_inter, &
+                         status, ierr )
+       ENDIF
+       CALL MPI_BCAST( remote, 1, MPI_REAL, 0, comm2d, ierr)
+       
        IF ( dt_coupling /= remote )  THEN
           WRITE( message_string, * ) 'coupling mode "', TRIM( coupling_mode ), &
@@ -200,7 +217,11 @@
        ENDIF
        IF ( dt_coupling <= 0.0 )  THEN
-          CALL MPI_SEND( dt_max, 1, MPI_REAL, target_id, 19, comm_inter, ierr )
-          CALL MPI_RECV( remote, 1, MPI_REAL, target_id, 19, comm_inter, &
-                         status, ierr )
+          IF ( myid == 0  ) THEN
+             CALL MPI_SEND( dt_max, 1, MPI_REAL, target_id, 19, comm_inter, ierr )
+             CALL MPI_RECV( remote, 1, MPI_REAL, target_id, 19, comm_inter, &
+                            status, ierr )
+          ENDIF   
+          CALL MPI_BCAST( remote, 1, MPI_REAL, 0, comm2d, ierr)
+          
           dt_coupling = MAX( dt_max, remote )
           WRITE( message_string, * ) 'coupling mode "', TRIM( coupling_mode ), &
@@ -209,9 +230,12 @@
           CALL message( 'check_parameters', 'PA0005', 0, 1, 0, 6, 0 )
        ENDIF
-
-       CALL MPI_SEND( restart_time, 1, MPI_REAL, target_id, 12, comm_inter, &
-                      ierr )
-       CALL MPI_RECV( remote, 1, MPI_REAL, target_id, 12, comm_inter, &
-                      status, ierr )
+       IF ( myid == 0 ) THEN
+          CALL MPI_SEND( restart_time, 1, MPI_REAL, target_id, 12, comm_inter, &
+                         ierr )
+          CALL MPI_RECV( remote, 1, MPI_REAL, target_id, 12, comm_inter, &
+                         status, ierr )
+       ENDIF 
+       CALL MPI_BCAST( remote, 1, MPI_REAL, 0, comm2d, ierr)
+      
        IF ( restart_time /= remote )  THEN
           WRITE( message_string, * ) 'coupling mode "', TRIM( coupling_mode ), &
@@ -220,9 +244,12 @@
           CALL message( 'check_parameters', 'PA0006', 1, 2, 0, 6, 0 )
        ENDIF
-
-       CALL MPI_SEND( dt_restart, 1, MPI_REAL, target_id, 13, comm_inter, &
-                      ierr )
-       CALL MPI_RECV( remote, 1, MPI_REAL, target_id, 13, comm_inter, &
-                      status, ierr )
+       IF ( myid == 0 ) THEN
+          CALL MPI_SEND( dt_restart, 1, MPI_REAL, target_id, 13, comm_inter, &
+                         ierr )
+          CALL MPI_RECV( remote, 1, MPI_REAL, target_id, 13, comm_inter, &
+                         status, ierr )
+       ENDIF   
+       CALL MPI_BCAST( remote, 1, MPI_REAL, 0, comm2d, ierr)
+      
        IF ( dt_restart /= remote )  THEN
           WRITE( message_string, * ) 'coupling mode "', TRIM( coupling_mode ), &
@@ -233,8 +260,12 @@
 
        simulation_time_since_reference = end_time - coupling_start_time
-       CALL MPI_SEND( simulation_time_since_reference, 1, MPI_REAL, target_id, &
-                      14, comm_inter, ierr )
-       CALL MPI_RECV( remote, 1, MPI_REAL, target_id, 14, comm_inter, &
-                      status, ierr )
+       IF  ( myid == 0 ) THEN
+          CALL MPI_SEND( simulation_time_since_reference, 1, MPI_REAL, target_id, &
+                         14, comm_inter, ierr )
+          CALL MPI_RECV( remote, 1, MPI_REAL, target_id, 14, comm_inter, &
+                         status, ierr )    
+       ENDIF 
+       CALL MPI_BCAST( remote, 1, MPI_REAL, 0, comm2d, ierr)
+      
        IF ( simulation_time_since_reference /= remote )  THEN
           WRITE( message_string, * ) 'coupling mode "', TRIM( coupling_mode ), &
@@ -245,38 +276,70 @@
        ENDIF
 
-       CALL MPI_SEND( dx, 1, MPI_REAL, target_id, 15, comm_inter, ierr )
-       CALL MPI_RECV( remote, 1, MPI_REAL, target_id, 15, comm_inter, &
-                      status, ierr )
-       IF ( dx /= remote )  THEN
-          WRITE( message_string, * ) 'coupling mode "', TRIM( coupling_mode ), &
-                 '":  dx = ', dx, '& is not equal to dx_remote = ', remote
-          CALL message( 'check_parameters', 'PA0009', 1, 2, 0, 6, 0 )
-       ENDIF
-
-       CALL MPI_SEND( dy, 1, MPI_REAL, target_id, 16, comm_inter, ierr )
-       CALL MPI_RECV( remote, 1, MPI_REAL, target_id, 16, comm_inter, &
-                      status, ierr )
-       IF ( dy /= remote )  THEN
-          WRITE( message_string, * ) 'coupling mode "', TRIM( coupling_mode ), &
-                 '":  dy = ', dy, '& is not equal to dy_remote = ', remote
-          CALL message( 'check_parameters', 'PA0010', 1, 2, 0, 6, 0 )
-       ENDIF
-
-       CALL MPI_SEND( nx, 1, MPI_INTEGER, target_id, 17, comm_inter, ierr )
-       CALL MPI_RECV( iremote, 1, MPI_INTEGER, target_id, 17, comm_inter, &
-                      status, ierr )
-       IF ( nx /= iremote )  THEN
-          WRITE( message_string, * ) 'coupling mode "', TRIM( coupling_mode ), &
-                 '": nx = ', nx, '& is not equal to nx_remote = ', iremote
-          CALL message( 'check_parameters', 'PA0011', 1, 2, 0, 6, 0 )
-       ENDIF
-
-       CALL MPI_SEND( ny, 1, MPI_INTEGER, target_id, 18, comm_inter, ierr )
-       CALL MPI_RECV( iremote, 1, MPI_INTEGER, target_id, 18, comm_inter, &
-                      status, ierr )
-       IF ( ny /= iremote )  THEN
-          WRITE( message_string, * ) 'coupling mode "', TRIM( coupling_mode ), &
-                 '": ny = ', ny, '& is not equal to ny_remote = ', iremote
-          CALL message( 'check_parameters', 'PA0012', 1, 2, 0, 6, 0 )
+  
+       IF ( myid == 0 ) THEN
+          CALL MPI_SEND( dx, 1, MPI_REAL, target_id, 15, comm_inter, ierr )
+          CALL MPI_RECV( remote, 1, MPI_REAL, target_id, 15, comm_inter, &
+                                                             status, ierr )
+       ENDIF
+       CALL MPI_BCAST( remote, 1, MPI_REAL, 0, comm2d, ierr)
+
+
+       IF ( coupling_mode == 'atmosphere_to_ocean') THEN
+
+          IF ( dx < remote ) THEN
+             WRITE( message_string, * ) 'coupling mode "', &
+                   TRIM( coupling_mode ),                  &
+           '": dx in Atmosphere is not equal to or not larger then dx in ocean'
+             CALL message( 'check_parameters', 'PA0009', 1, 2, 0, 6, 0 )
+          ENDIF
+
+          IF ( (nx_a+1)*dx /= (nx_o+1)*remote )  THEN
+             WRITE( message_string, * ) 'coupling mode "', &
+                    TRIM( coupling_mode ), &
+             '": Domain size in x-direction is not equal in ocean and atmosphere'
+             CALL message( 'check_parameters', 'PA0010', 1, 2, 0, 6, 0 )
+          ENDIF
+
+       ENDIF
+
+       IF ( myid == 0) THEN
+          CALL MPI_SEND( dy, 1, MPI_REAL, target_id, 16, comm_inter, ierr )
+          CALL MPI_RECV( remote, 1, MPI_REAL, target_id, 16, comm_inter, &
+                         status, ierr )
+       ENDIF
+       CALL MPI_BCAST( remote, 1, MPI_REAL, 0, comm2d, ierr)
+
+       IF ( coupling_mode == 'atmosphere_to_ocean') THEN
+
+          IF ( dy < remote )  THEN
+             WRITE( message_string, * ) 'coupling mode "', &
+                    TRIM( coupling_mode ), &
+                 '": dy in Atmosphere is not equal to or not larger then dy in ocean'
+             CALL message( 'check_parameters', 'PA0011', 1, 2, 0, 6, 0 )
+          ENDIF
+
+          IF ( (ny_a+1)*dy /= (ny_o+1)*remote )  THEN
+             WRITE( message_string, * ) 'coupling mode "', &
+                   TRIM( coupling_mode ), &
+             '": Domain size in y-direction is not equal in ocean and atmosphere'
+             CALL message( 'check_parameters', 'PA0012', 1, 2, 0, 6, 0 )
+          ENDIF
+
+          IF ( MOD(nx_o+1,nx_a+1) /= 0 )  THEN
+             WRITE( message_string, * ) 'coupling mode "', &
+                   TRIM( coupling_mode ), &
+             '": nx+1 in ocean is not divisible without remainder with nx+1 in', & 
+             ' atmosphere'
+             CALL message( 'check_parameters', 'PA0339', 1, 2, 0, 6, 0 )
+          ENDIF
+
+          IF ( MOD(ny_o+1,ny_a+1) /= 0 )  THEN
+             WRITE( message_string, * ) 'coupling mode "', &
+                   TRIM( coupling_mode ), &
+             '": ny+1 in ocean is not divisible without remainder with ny+1 in', & 
+             ' atmosphere'
+             CALL message( 'check_parameters', 'PA0340', 1, 2, 0, 6, 0 )
+          ENDIF
+
        ENDIF
 #else
@@ -290,11 +353,13 @@
 !
 !-- Exchange via intercommunicator
-    IF ( coupling_mode == 'atmosphere_to_ocean' )  THEN
+    IF ( coupling_mode == 'atmosphere_to_ocean' .AND. myid == 0 )  THEN
        CALL MPI_SEND( humidity, 1, MPI_LOGICAL, target_id, 19, comm_inter, &
                       ierr )
-    ELSEIF ( coupling_mode == 'ocean_to_atmosphere' )  THEN
+    ELSEIF ( coupling_mode == 'ocean_to_atmosphere' .AND. myid == 0)  THEN
        CALL MPI_RECV( humidity_remote, 1, MPI_LOGICAL, target_id, 19, &
                       comm_inter, status, ierr )
     ENDIF
+    CALL MPI_BCAST( humidity_remote, 1, MPI_LOGICAL, 0, comm2d, ierr)
+    
 #endif
 
@@ -372,4 +437,13 @@
           CALL message( 'check_parameters', 'PA0014', 1, 2, 0, 6, 0 )
        ENDIF
+       IF ( momentum_advec == 'ws-scheme' .OR. scalar_advec == 'ws-scheme' ) &
+       THEN
+          message_string = 'topography is still not allowed with ' // &
+                           'momentum_advec = "' // TRIM( momentum_advec ) //  &
+                           '"or scalar_advec = "' // TRIM( scalar_advec ) //'"'
+   ! message number still needs modification
+           CALL message( 'check_parameters', 'PA0341', 1, 2, 0, 6, 0 )
+       END IF
+          
 !
 !--    In case of non-flat topography, check whether the convention how to 
@@ -492,23 +566,31 @@
        CALL message( 'check_parameters', 'PA0021', 1, 2, 0, 6, 0 )
     ENDIF
-
+    
+    IF( momentum_advec == 'ws-scheme' .AND. & 
+        call_psolver_at_all_substeps == .FALSE. ) THEN
+        message_string = 'psolver must be called at each RK3 substep when "'//&
+                      TRIM(momentum_advec) // ' "is used for momentum_advec'
+        CALL message( 'check_parameters', 'PA0343', 1, 2, 0, 6, 0 )
+    END IF
 !
 !-- Advection schemes:
-    IF ( momentum_advec /= 'pw-scheme' .AND. momentum_advec /= 'ups-scheme' ) &
-    THEN
+    IF ( momentum_advec /= 'pw-scheme' .AND. momentum_advec /= 'ws-scheme' .AND. &
+         momentum_advec /= 'ups-scheme' ) THEN
        message_string = 'unknown advection scheme: momentum_advec = "' // &
                         TRIM( momentum_advec ) // '"'
        CALL message( 'check_parameters', 'PA0022', 1, 2, 0, 6, 0 )
     ENDIF
-    IF ( ( momentum_advec == 'ups-scheme'  .OR.  scalar_advec == 'ups-scheme' )&
-                                      .AND.  timestep_scheme /= 'euler' )  THEN
-       message_string = 'momentum_advec = "' // TRIM( momentum_advec ) // &
-                        '" is not allowed with timestep_scheme = "' //    &
-                        TRIM( timestep_scheme ) // '"'
+    IF ((( momentum_advec == 'ups-scheme'  .OR.  scalar_advec == 'ups-scheme' )&
+           .AND.  timestep_scheme /= 'euler' ) .OR. (( momentum_advec == 'ws-scheme'&
+           .OR.  scalar_advec == 'ws-scheme') .AND. (timestep_scheme == 'euler' .OR. &
+           timestep_scheme == 'leapfrog+euler' .OR. timestep_scheme == 'leapfrog'    &
+           .OR. timestep_scheme == 'runge-kutta-2'))) THEN
+       message_string = 'momentum_advec or scalar_advec = "' &
+         // TRIM( momentum_advec ) // '" is not allowed with timestep_scheme = "' // &
+         TRIM( timestep_scheme ) // '"'
        CALL message( 'check_parameters', 'PA0023', 1, 2, 0, 6, 0 )
     ENDIF
-
-    IF ( scalar_advec /= 'pw-scheme'  .AND.  scalar_advec /= 'bc-scheme'  .AND.&
-         scalar_advec /= 'ups-scheme' )  THEN
+    IF ( scalar_advec /= 'pw-scheme'  .AND.  scalar_advec /= 'ws-scheme' .AND. &
+        scalar_advec /= 'bc-scheme'  .AND.  scalar_advec /= 'ups-scheme' )  THEN
        message_string = 'unknown advection scheme: scalar_advec = "' // &
                         TRIM( scalar_advec ) // '"'
@@ -563,6 +645,6 @@
     ENDIF
 
-    IF ( momentum_advec /= 'pw-scheme' .AND. timestep_scheme(1:5) == 'runge' ) &
-    THEN
+    IF ( (momentum_advec /= 'pw-scheme' .AND. momentum_advec /= 'ws-scheme') &
+         .AND. timestep_scheme(1:5) == 'runge' ) THEN
        message_string = 'momentum advection scheme "' // &
                         TRIM( momentum_advec ) // '" & does not work with ' // &
@@ -712,5 +794,5 @@
           ug_vertical_gradient_level_ind(1) = nzt+1
           ug(nzt+1) = ug_surface
-          DO  k = nzt, 0, -1
+          DO  k = nzt, nzb, -1
              IF ( i < 11 ) THEN
                 IF ( ug_vertical_gradient_level(i) > zu(k)  .AND. &
@@ -778,5 +860,5 @@
           vg_vertical_gradient_level_ind(1) = nzt+1
           vg(nzt+1) = vg_surface
-          DO  k = nzt, 0, -1
+          DO  k = nzt, nzb, -1
              IF ( i < 11 ) THEN
                 IF ( vg_vertical_gradient_level(i) > zu(k)  .AND. &
@@ -1020,4 +1102,5 @@
  
              
+
 !
 !-- Compute Coriolis parameter
@@ -1159,6 +1242,6 @@
 !
 !-- Non-cyclic lateral boundaries require the multigrid method and Piascek-
-!-- Willimas advection scheme. Several schemes and tools do not work with
-!-- non-cyclic boundary conditions.
+!-- Willimas or Wicker - Skamarock advection scheme. Several schemes
+!-- and tools do not work with non-cyclic boundary conditions.
     IF ( bc_lr /= 'cyclic'  .OR.  bc_ns /= 'cyclic' )  THEN
        IF ( psolver /= 'multigrid' )  THEN
@@ -1167,14 +1250,24 @@
           CALL message( 'check_parameters', 'PA0051', 1, 2, 0, 6, 0 )
        ENDIF
-       IF ( momentum_advec /= 'pw-scheme' )  THEN
+       IF ( momentum_advec /= 'pw-scheme' .AND. &
+            momentum_advec /= 'ws-scheme')  THEN
           message_string = 'non-cyclic lateral boundaries do not allow ' // &
                            'momentum_advec = "' // TRIM( momentum_advec ) // '"'
           CALL message( 'check_parameters', 'PA0052', 1, 2, 0, 6, 0 )
        ENDIF
-       IF ( scalar_advec /= 'pw-scheme' )  THEN
+       IF ( scalar_advec /= 'pw-scheme' .AND. &
+            scalar_advec /= 'ws-scheme' )  THEN
           message_string = 'non-cyclic lateral boundaries do not allow ' // &
                            'scalar_advec = "' // TRIM( scalar_advec ) // '"'
           CALL message( 'check_parameters', 'PA0053', 1, 2, 0, 6, 0 )
        ENDIF
+       IF ( (scalar_advec == 'ws-scheme' .OR. momentum_advec == 'ws-scheme' ) &
+          .AND. loop_optimization == 'vector' ) THEN
+          message_string = 'non-cyclic lateral boundaries do not allow ' // &
+                           'loop_optimization = vector and ' //  &
+                           'scalar_advec = "' // TRIM( scalar_advec ) // '"' 
+  ! The error message number still needs modification. 
+          CALL message( 'check_parameters', 'PA0342', 1, 2, 0, 6, 0 )
+       END IF
        IF ( galilei_transformation )  THEN
           message_string = 'non-cyclic lateral boundaries do not allow ' // &
@@ -1407,4 +1500,10 @@
                         TRIM( bc_uv_b ) // '"'
        CALL message( 'check_parameters', 'PA0076', 1, 2, 0, 6, 0 )
+    ENDIF
+!
+!-- In case of coupled simulations u and v at the ground in atmosphere will be
+!-- assigned with the u and v values of the ocean surface
+    IF ( coupling_mode == 'atmosphere_to_ocean' )  THEN
+       ibc_uv_b = 2
     ENDIF
 
@@ -2109,4 +2208,5 @@
              hom(:,2,57,:) = SPREAD( zu, 2, statistic_regions+1 )
 
+
           CASE ( 'u"pt"' )
              dopr_index(i) = 58
@@ -2244,4 +2344,5 @@
 
        END SELECT
+
 !
 !--    Check to which of the predefined coordinate systems the profile belongs
@@ -2584,5 +2685,7 @@
 !-- Upper plot limit (grid point value) for 1D profiles
     IF ( z_max_do1d == -1.0 )  THEN
+
        nz_do1d = nzt+1
+
     ELSE
        DO  k = nzb+1, nzt+1
@@ -2737,4 +2840,5 @@
 
 !
+
 !-- Check netcdf precison
     ldum = .FALSE.
@@ -3070,9 +3174,7 @@
     IF ( conserve_volume_flow )  THEN
        IF ( TRIM( conserve_volume_flow_mode ) == 'default' )  THEN
-          IF ( bc_lr /= 'cyclic'  .OR.  bc_ns /= 'cyclic' )  THEN
-             conserve_volume_flow_mode = 'inflow_profile'
-          ELSE
-             conserve_volume_flow_mode = 'initial_profiles'
-          ENDIF
+
+          conserve_volume_flow_mode = 'initial_profiles'
+
        ELSEIF ( TRIM( conserve_volume_flow_mode ) /= 'initial_profiles' .AND.  &
             TRIM( conserve_volume_flow_mode ) /= 'inflow_profile' .AND.  &
@@ -3082,8 +3184,8 @@
           CALL message( 'check_parameters', 'PA0154', 1, 2, 0, 6, 0 )
        ENDIF
-       IF ( ( bc_lr /= 'cyclic'  .OR.  bc_ns /= 'cyclic' ) .AND. &
-            TRIM( conserve_volume_flow_mode ) /= 'inflow_profile' )  THEN
-          WRITE( message_string, * )  'noncyclic boundary conditions ', &
-               'require & conserve_volume_flow_mode = ''inflow_profile'''
+       IF ( (bc_lr /= 'cyclic'  .OR.  bc_ns /= 'cyclic')  .AND. &
+          TRIM( conserve_volume_flow_mode ) == 'bulk_velocity' )  THEN
+          WRITE( message_string, * )  'non-cyclic boundary conditions ', &
+               'require  conserve_volume_flow_mode = ''initial_profiles'''
           CALL message( 'check_parameters', 'PA0155', 1, 2, 0, 6, 0 )
        ENDIF
@@ -3091,5 +3193,5 @@
             TRIM( conserve_volume_flow_mode ) == 'inflow_profile' )  THEN
           WRITE( message_string, * )  'cyclic boundary conditions ', &
-               'require & conserve_volume_flow_mode = ''initial_profiles''', &
+               'require conserve_volume_flow_mode = ''initial_profiles''', &
                ' or ''bulk_velocity'''
           CALL message( 'check_parameters', 'PA0156', 1, 2, 0, 6, 0 )
@@ -3100,5 +3202,5 @@
          TRIM( conserve_volume_flow_mode ) /= 'bulk_velocity' ) )  THEN
        WRITE( message_string, * )  'nonzero bulk velocity requires ', &
-            'conserve_volume_flow = .T. and & ', &
+            'conserve_volume_flow = .T. and ', &
             'conserve_volume_flow_mode = ''bulk_velocity'''
        CALL message( 'check_parameters', 'PA0157', 1, 2, 0, 6, 0 )
@@ -3139,3 +3241,4 @@
 
 
+
  END SUBROUTINE check_parameters
Index: /palm/trunk/SOURCE/data_output_2d.f90
===================================================================
--- /palm/trunk/SOURCE/data_output_2d.f90	(revision 666)
+++ /palm/trunk/SOURCE/data_output_2d.f90	(revision 667)
@@ -4,5 +4,7 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng in loops and 
+! allocation of arrays local_2d and total_2d.
+! Calls of exchange_horiz are modiefied.
 !
 ! Former revisions:
@@ -112,7 +114,6 @@
 
        CASE ( 'xy' )
-
           s = 1
-          ALLOCATE( level_z(0:nzt+1), local_2d(nxl-1:nxr+1,nys-1:nyn+1) )
+          ALLOCATE( level_z(nzb:nzt+1), local_2d(nxlg:nxrg,nysg:nyng) )
 
 !
@@ -130,5 +131,5 @@
                 IF ( iso2d_output )  CALL check_open( 21 )
 #if defined( __parallel )
-                ALLOCATE( total_2d(-1:nx+1,-1:ny+1) )
+                ALLOCATE( total_2d(-nbgp:nx+nbgp,-nbgp:ny+nbgp) )
 #endif
              ENDIF
@@ -136,7 +137,6 @@
 
        CASE ( 'xz' )
-
           s = 2
-          ALLOCATE( local_2d(nxl-1:nxr+1,nzb:nzt+1) )
+          ALLOCATE( local_2d(nxlg:nxrg,nzb:nzt+1) )
 
 !
@@ -154,5 +154,5 @@
                 IF ( iso2d_output )  CALL check_open( 22 )
 #if defined( __parallel )
-                ALLOCATE( total_2d(-1:nx+1,nzb:nzt+1) )
+                ALLOCATE( total_2d(-nbgp:nx+nbgp,nzb:nzt+1) )
 #endif
              ENDIF
@@ -162,5 +162,5 @@
 
           s = 3
-          ALLOCATE( local_2d(nys-1:nyn+1,nzb:nzt+1) )
+          ALLOCATE( local_2d(nysg:nyng,nzb:nzt+1) )
 
 !
@@ -178,5 +178,5 @@
                 IF ( iso2d_output )  CALL check_open( 23 )
 #if defined( __parallel )
-                ALLOCATE( total_2d(-1:ny+1,nzb:nzt+1) )
+                ALLOCATE( total_2d(-nbgp:ny+nbgp,nzb:nzt+1) )
 #endif
              ENDIF
@@ -192,5 +192,5 @@
 !
 !-- Allocate a temporary array for resorting (kji -> ijk).
-    ALLOCATE( local_pf(nxl-1:nxr+1,nys-1:nyn+1,nzb:nzt+1) )
+    ALLOCATE( local_pf(nxlg:nxrg,nysg:nyng,nzb:nzt+1) )
 
 !
@@ -219,6 +219,6 @@
              CASE ( 'lwp*_xy' )        ! 2d-array
                 IF ( av == 0 )  THEN
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          local_pf(i,j,nzb+1) = SUM( ql(nzb:nzt,j,i) * &
                                                     dzw(1:nzt+1) )
@@ -226,6 +226,6 @@
                    ENDDO
                 ELSE
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          local_pf(i,j,nzb+1) = lwp_av(j,i)
                       ENDDO
@@ -248,10 +248,10 @@
                    IF ( simulated_time >= particle_advection_start )  THEN
                       tend = prt_count
-                      CALL exchange_horiz( tend )
+                      CALL exchange_horiz( tend, nbgp )
                    ELSE
                       tend = 0.0
                    ENDIF
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          DO  k = nzb, nzt+1
                             local_pf(i,j,k) = tend(k,j,i)
@@ -261,5 +261,5 @@
                    resorted = .TRUE.
                 ELSE
-                   CALL exchange_horiz( pc_av )
+                   CALL exchange_horiz( pc_av, nbgp )
                    to_be_resorted => pc_av
                 ENDIF
@@ -287,10 +287,10 @@
                          ENDDO
                       ENDDO
-                      CALL exchange_horiz( tend )
+                      CALL exchange_horiz( tend, nbgp )
                    ELSE
                       tend = 0.0
-                   ENDIF
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1
+                   END IF
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          DO  k = nzb, nzt+1
                             local_pf(i,j,k) = tend(k,j,i)
@@ -300,5 +300,5 @@
                    resorted = .TRUE.
                 ELSE
-                   CALL exchange_horiz( pr_av )
+                   CALL exchange_horiz( pr_av, nbgp )
                    to_be_resorted => pr_av
                 ENDIF
@@ -306,6 +306,6 @@
              CASE ( 'pra*_xy' )        ! 2d-array / integral quantity => no av
                 CALL exchange_horiz_2d( precipitation_amount )
-                DO  i = nxl-1, nxr+1
-                   DO  j = nys-1, nyn+1
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                       local_pf(i,j,nzb+1) =  precipitation_amount(j,i)
                    ENDDO
@@ -319,6 +319,6 @@
                 IF ( av == 0 )  THEN
                    CALL exchange_horiz_2d( precipitation_rate )
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1 
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          local_pf(i,j,nzb+1) =  precipitation_rate(j,i)
                       ENDDO
@@ -326,6 +326,6 @@
                 ELSE
                    CALL exchange_horiz_2d( precipitation_rate_av )
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1 
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          local_pf(i,j,nzb+1) =  precipitation_rate_av(j,i)
                       ENDDO
@@ -341,6 +341,6 @@
                       to_be_resorted => pt
                    ELSE
-                      DO  i = nxl-1, nxr+1
-                         DO  j = nys-1, nyn+1
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                             DO  k = nzb, nzt+1
                                local_pf(i,j,k) = pt(k,j,i) + l_d_cp *    &
@@ -399,12 +399,12 @@
              CASE ( 'qsws*_xy' )        ! 2d-array
                 IF ( av == 0 ) THEN
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1 
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          local_pf(i,j,nzb+1) =  qsws(j,i)
                       ENDDO
                    ENDDO
                 ELSE
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1 
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng 
                          local_pf(i,j,nzb+1) =  qsws_av(j,i)
                       ENDDO
@@ -417,6 +417,6 @@
              CASE ( 'qv_xy', 'qv_xz', 'qv_yz' )
                 IF ( av == 0 )  THEN
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          DO  k = nzb, nzt+1
                             local_pf(i,j,k) = q(k,j,i) - ql(k,j,i)
@@ -453,12 +453,12 @@
              CASE ( 'shf*_xy' )        ! 2d-array
                 IF ( av == 0 ) THEN
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1 
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          local_pf(i,j,nzb+1) =  shf(j,i)
                       ENDDO
                    ENDDO
                 ELSE
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1 
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          local_pf(i,j,nzb+1) =  shf_av(j,i)
                       ENDDO
@@ -471,12 +471,12 @@
              CASE ( 't*_xy' )        ! 2d-array
                 IF ( av == 0 )  THEN
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          local_pf(i,j,nzb+1) = ts(j,i)
                       ENDDO
                    ENDDO
                 ELSE
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          local_pf(i,j,nzb+1) = ts_av(j,i)
                       ENDDO
@@ -503,12 +503,12 @@
              CASE ( 'u*_xy' )        ! 2d-array
                 IF ( av == 0 )  THEN
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          local_pf(i,j,nzb+1) = us(j,i)
                       ENDDO
                    ENDDO
                 ELSE
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          local_pf(i,j,nzb+1) = us_av(j,i)
                       ENDDO
@@ -551,12 +551,12 @@
              CASE ( 'z0*_xy' )        ! 2d-array
                 IF ( av == 0 ) THEN
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1 
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          local_pf(i,j,nzb+1) =  z0(j,i)
                       ENDDO
                    ENDDO
                 ELSE
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1 
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          local_pf(i,j,nzb+1) =  z0_av(j,i)
                       ENDDO
@@ -593,6 +593,6 @@
 !--       Resort the array to be output, if not done above
           IF ( .NOT. resorted )  THEN
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       local_pf(i,j,k) = to_be_resorted(k,j,i)
@@ -647,6 +647,6 @@
 !--                   Carry out the averaging (all data are on the PE)
                       DO  k = nzb, nzt+1
-                         DO  j = nys-1, nyn+1
-                            DO  i = nxl-1, nxr+1
+                         DO  j = nysg, nyng
+                            DO  i = nxlg, nxrg
                                local_2d(i,j) = local_2d(i,j) + local_pf(i,j,k)
                             ENDDO
@@ -654,5 +654,5 @@
                       ENDDO
 
-                      local_2d = local_2d / ( nzt -nzb + 2.0 )
+                      local_2d = local_2d / ( nzt -nzb + 2.0)
 
                    ELSE
@@ -723,5 +723,5 @@
                          ENDIF
 #endif
-                         WRITE ( 21 )  nxl-1, nxr+1, nys-1, nyn+1
+                         WRITE ( 21 )  nxlg, nxrg, nysg, nyng
                          WRITE ( 21 )  local_2d
 
@@ -734,9 +734,9 @@
                          CALL MPI_BARRIER( comm2d, ierr )
 
-                         ngp = ( nxr-nxl+3 ) * ( nyn-nys+3 )
+                         ngp = ( nxrg-nxlg+1 ) * ( nyng-nysg+1 )
                          IF ( myid == 0 )  THEN
 !
 !--                         Local array can be relocated directly.
-                            total_2d(nxl-1:nxr+1,nys-1:nyn+1) = local_2d
+                            total_2d(nxlg:nxrg,nysg:nyng) = local_2d
 !
 !--                         Receive data from all other PEs.
@@ -760,10 +760,10 @@
 !--                         Output of the total cross-section.
                             IF ( iso2d_output )  THEN
-                               WRITE (21)  total_2d(0:nx+1,0:ny+1)
+                               WRITE (21)  total_2d(-nbgp:nx+nbgp,-nbgp:ny+nbgp)
                             ENDIF
 !
 !--                         Relocate the local array for the next loop increment
                             DEALLOCATE( local_2d )
-                            ALLOCATE( local_2d(nxl-1:nxr+1,nys-1:nyn+1) )
+                            ALLOCATE( local_2d(nxlg:nxrg,nysg:nyng) )
 
 #if defined( __netcdf )
@@ -789,11 +789,11 @@
 !
 !--                         First send the local index limits to PE0
-                            ind(1) = nxl-1; ind(2) = nxr+1
-                            ind(3) = nys-1; ind(4) = nyn+1
+                            ind(1) = nxlg; ind(2) = nxrg
+                            ind(3) = nysg; ind(4) = nyng
                             CALL MPI_SEND( ind(1), 4, MPI_INTEGER, 0, 0, &
                                            comm2d, ierr )
 !
 !--                         Send data to PE0
-                            CALL MPI_SEND( local_2d(nxl-1,nys-1), ngp, &
+                            CALL MPI_SEND( local_2d(nxlg,nysg), ngp, &
                                            MPI_REAL, 0, 1, comm2d, ierr )
                          ENDIF
@@ -882,16 +882,17 @@
 
                    ENDIF
+
 !
 !--                If required, carry out averaging along y
                    IF ( section(is,s) == -1 )  THEN
 
-                      ALLOCATE( local_2d_l(nxl-1:nxr+1,nzb:nzt+1) )
+                      ALLOCATE( local_2d_l(nxlg:nxrg,nzb:nzt+1) )
                       local_2d_l = 0.0
-                      ngp = ( nxr-nxl+3 ) * ( nzt-nzb+2 )
+                      ngp = ( nxrg-nxlg+1 ) * ( nzt-nzb+2 )
 !
 !--                   First local averaging on the PE
                       DO  k = nzb, nzt+1
                          DO  j = nys, nyn
-                            DO  i = nxl-1, nxr+1
+                            DO  i = nxlg, nxrg
                                local_2d_l(i,k) = local_2d_l(i,k) + &
                                                  local_pf(i,j,k)
@@ -903,6 +904,6 @@
 !--                   Now do the averaging over all PEs along y
                       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-                      CALL MPI_ALLREDUCE( local_2d_l(nxl-1,nzb),              &
-                                          local_2d(nxl-1,nzb), ngp, MPI_REAL, &
+                      CALL MPI_ALLREDUCE( local_2d_l(nxlg,nzb),              &
+                                          local_2d(nxlg,nzb), ngp, MPI_REAL, &
                                           MPI_SUM, comm1dy, ierr )
 #else
@@ -936,5 +937,5 @@
 !--                   BEGIN WORKAROUND---------------------------------------
                       IF ( npey /= 1  .AND.  section(is,s) /= -1)  THEN
-                         ALLOCATE( local_2d_l(nxl-1:nxr+1,nzb:nzt+1) )
+                         ALLOCATE( local_2d_l(nxlg:nxrg,nzb:nzt+1) )
                          local_2d_l = 0.0
                          IF ( section(is,s) >= nys .AND. section(is,s) <= nyn )&
@@ -945,8 +946,8 @@
 !
 !--                      Distribute data over all PEs along y
-                         ngp = ( nxr-nxl+3 ) * ( nzt-nzb+2 )
+                         ngp = ( nxrg-nxlg+1 ) * ( nzt-nzb+2 )
                          IF ( collective_wait ) CALL MPI_BARRIER( comm2d, ierr )
-                         CALL MPI_ALLREDUCE( local_2d_l(nxl-1,nzb),            &
-                                             local_2d(nxl-1,nzb), ngp,         &
+                         CALL MPI_ALLREDUCE( local_2d_l(nxlg,nzb),            &
+                                             local_2d(nxlg,nzb), ngp,         &
                                              MPI_REAL, MPI_SUM, comm1dy, ierr )
 #else
@@ -1022,5 +1023,5 @@
                               ( section(is,s) == -1  .AND.  nys-1 == -1 ) )  &
                          THEN
-                            WRITE (22)  nxl-1, nxr+1, nzb, nzt+1
+                            WRITE (22)  nxlg, nxrg, nzb, nzt+1
                             WRITE (22)  local_2d
                          ELSE
@@ -1036,5 +1037,5 @@
                          CALL MPI_BARRIER( comm2d, ierr )
 
-                         ngp = ( nxr-nxl+3 ) * ( nzt-nzb+2 )
+                         ngp = ( nxrg-nxlg+1 ) * ( nzt-nzb+2 )
                          IF ( myid == 0 )  THEN
 !
@@ -1044,5 +1045,5 @@
                                  ( section(is,s) == -1  .AND.  nys-1 == -1 ) ) &
                             THEN
-                               total_2d(nxl-1:nxr+1,nzb:nzt+1) = local_2d
+                               total_2d(nxlg:nxrg,nzb:nzt+1) = local_2d
                             ENDIF
 !
@@ -1073,10 +1074,10 @@
 !--                         Output of the total cross-section.
                             IF ( iso2d_output )  THEN
-                               WRITE (22)  total_2d(0:nx+1,nzb:nzt+1)
+                               WRITE (22)  total_2d(-nbgp:nx+nbgp,nzb:nzt+1)
                             ENDIF
 !
 !--                         Relocate the local array for the next loop increment
                             DEALLOCATE( local_2d )
-                            ALLOCATE( local_2d(nxl-1:nxr+1,nzb:nzt+1) )
+                            ALLOCATE( local_2d(nxlg:nxrg,nzb:nzt+1) )
 
 #if defined( __netcdf )
@@ -1099,5 +1100,5 @@
                                  ( section(is,s) == -1  .AND.  nys-1 == -1 ) ) &
                             THEN
-                               ind(1) = nxl-1; ind(2) = nxr+1
+                               ind(1) = nxlg; ind(2) = nxrg
                                ind(3) = nzb;   ind(4) = nzt+1
                             ELSE
@@ -1110,5 +1111,5 @@
 !--                         If applicable, send data to PE0.
                             IF ( ind(1) /= -9999 )  THEN
-                               CALL MPI_SEND( local_2d(nxl-1,nzb), ngp, &
+                               CALL MPI_SEND( local_2d(nxlg,nzb), ngp, &
                                               MPI_REAL, 0, 1, comm2d, ierr )
                             ENDIF
@@ -1187,11 +1188,11 @@
                    IF ( section(is,s) == -1 )  THEN
 
-                      ALLOCATE( local_2d_l(nys-1:nyn+1,nzb:nzt+1) )
+                      ALLOCATE( local_2d_l(nysg:nyng,nzb:nzt+1) )
                       local_2d_l = 0.0
-                      ngp = ( nyn-nys+3 ) * ( nzt-nzb+2 )
+                      ngp = ( nyng-nysg+1 ) * ( nzt-nzb+2 )
 !
 !--                   First local averaging on the PE
                       DO  k = nzb, nzt+1
-                         DO  j = nys-1, nyn+1
+                         DO  j = nysg, nyng
                             DO  i = nxl, nxr
                                local_2d_l(j,k) = local_2d_l(j,k) + &
@@ -1204,6 +1205,6 @@
 !--                   Now do the averaging over all PEs along x
                       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-                      CALL MPI_ALLREDUCE( local_2d_l(nys-1,nzb),              &
-                                          local_2d(nys-1,nzb), ngp, MPI_REAL, &
+                      CALL MPI_ALLREDUCE( local_2d_l(nysg,nzb),              &
+                                          local_2d(nysg,nzb), ngp, MPI_REAL, &
                                           MPI_SUM, comm1dx, ierr )
 #else
@@ -1237,5 +1238,5 @@
 !--                   BEGIN WORKAROUND---------------------------------------
                       IF ( npex /= 1  .AND.  section(is,s) /= -1)  THEN
-                         ALLOCATE( local_2d_l(nys-1:nyn+1,nzb:nzt+1) )
+                         ALLOCATE( local_2d_l(nysg:nyng,nzb:nzt+1) )
                          local_2d_l = 0.0
                          IF ( section(is,s) >= nxl .AND. section(is,s) <= nxr )&
@@ -1246,8 +1247,8 @@
 !
 !--                      Distribute data over all PEs along x
-                         ngp = ( nyn-nys+3 ) * ( nzt-nzb+2 )
+                         ngp = ( nyng-nysg+1 ) * ( nzt-nzb + 2 )
                          IF ( collective_wait ) CALL MPI_BARRIER( comm2d, ierr )
-                         CALL MPI_ALLREDUCE( local_2d_l(nys-1,nzb),            &
-                                             local_2d(nys-1,nzb), ngp,         &
+                         CALL MPI_ALLREDUCE( local_2d_l(nysg,nzb),            &
+                                             local_2d(nysg,nzb), ngp,         &
                                              MPI_REAL, MPI_SUM, comm1dx, ierr )
 #else
@@ -1323,5 +1324,5 @@
                               ( section(is,s) == -1  .AND.  nxl-1 == -1 ) )  &
                          THEN
-                            WRITE (23)  nys-1, nyn+1, nzb, nzt+1
+                            WRITE (23)  nysg, nyng, nzb, nzt+1
                             WRITE (23)  local_2d
                          ELSE
@@ -1337,5 +1338,5 @@
                          CALL MPI_BARRIER( comm2d, ierr )
 
-                         ngp = ( nyn-nys+3 ) * ( nzt-nzb+2 )
+                         ngp = ( nyng-nysg+1 ) * ( nzt-nzb+2 )
                          IF ( myid == 0 )  THEN
 !
@@ -1345,5 +1346,5 @@
                                  ( section(is,s) == -1  .AND.  nxl-1 == -1 ) ) &
                             THEN
-                               total_2d(nys-1:nyn+1,nzb:nzt+1) = local_2d
+                               total_2d(nysg:nyng,nzb:nzt+1) = local_2d
                             ENDIF
 !
@@ -1379,5 +1380,5 @@
 !--                         Relocate the local array for the next loop increment
                             DEALLOCATE( local_2d )
-                            ALLOCATE( local_2d(nys-1:nyn+1,nzb:nzt+1) )
+                            ALLOCATE( local_2d(nysg:nyng,nzb:nzt+1) )
 
 #if defined( __netcdf )
@@ -1400,5 +1401,5 @@
                                  ( section(is,s) == -1  .AND.  nxl-1 == -1 ) ) &
                             THEN
-                               ind(1) = nys-1; ind(2) = nyn+1
+                               ind(1) = nysg; ind(2) = nyng
                                ind(3) = nzb;   ind(4) = nzt+1
                             ELSE
@@ -1411,5 +1412,5 @@
 !--                         If applicable, send data to PE0.
                             IF ( ind(1) /= -9999 )  THEN
-                               CALL MPI_SEND( local_2d(nys-1,nzb), ngp, &
+                               CALL MPI_SEND( local_2d(nysg,nzb), ngp, &
                                               MPI_REAL, 0, 1, comm2d, ierr )
                             ENDIF
Index: /palm/trunk/SOURCE/data_output_3d.f90
===================================================================
--- /palm/trunk/SOURCE/data_output_3d.f90	(revision 666)
+++ /palm/trunk/SOURCE/data_output_3d.f90	(revision 667)
@@ -4,5 +4,7 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng in loops and 
+! allocation of arrays.  Calls of exchange_horiz are modified.
+! Skip-value skip_do_avs changed to a dynamic adaption of ghost points.
 !
 ! Former revisions:
@@ -102,5 +104,5 @@
 !
 !-- Allocate a temporary array with the desired output dimensions.
-    ALLOCATE( local_pf(nxl-1:nxr+1,nys-1:nyn+1,nzb:nz_do3d) )
+    ALLOCATE( local_pf(nxlg:nxrg,nysg:nyng,nzb:nz_do3d) )
 
 !
@@ -157,7 +159,7 @@
              IF ( av == 0 )  THEN
                 tend = prt_count
-                CALL exchange_horiz( tend )
-                DO  i = nxl-1, nxr+1
-                   DO  j = nys-1, nyn+1
+                CALL exchange_horiz( tend, nbgp )
+                DO  i = nxlg, nxrg
+                   DO  j = nysg, nyng
                       DO  k = nzb, nz_do3d
                          local_pf(i,j,k) = tend(k,j,i)
@@ -167,5 +169,5 @@
                 resorted = .TRUE.
              ELSE
-                CALL exchange_horiz( pc_av )
+                CALL exchange_horiz( pc_av, nbgp )
                 to_be_resorted => pc_av
              ENDIF
@@ -192,7 +194,7 @@
                    ENDDO
                 ENDDO
-                CALL exchange_horiz( tend )
-                DO  i = nxl-1, nxr+1
-                   DO  j = nys-1, nyn+1
+                CALL exchange_horiz( tend, nbgp )
+                DO  i = nxlg, nxrg
+                   DO  j = nysg, nyng
                       DO  k = nzb, nzt+1
                          local_pf(i,j,k) = tend(k,j,i)
@@ -202,5 +204,5 @@
                 resorted = .TRUE.
              ELSE
-                CALL exchange_horiz( pr_av )
+                CALL exchange_horiz( pr_av, nbgp )
                 to_be_resorted => pr_av
              ENDIF
@@ -211,6 +213,6 @@
                    to_be_resorted => pt
                 ELSE
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          DO  k = nzb, nz_do3d
                             local_pf(i,j,k) = pt(k,j,i) + l_d_cp *    &
@@ -263,6 +265,6 @@
           CASE ( 'qv' )
              IF ( av == 0 )  THEN
-                DO  i = nxl-1, nxr+1
-                   DO  j = nys-1, nyn+1
+                DO  i = nxlg, nxrg
+                   DO  j = nysg, nyng
                       DO  k = nzb, nz_do3d
                          local_pf(i,j,k) = q(k,j,i) - ql(k,j,i)
@@ -342,6 +344,6 @@
 !--    Resort the array to be output, if not done above
        IF ( .NOT. resorted )  THEN
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
                 DO  k = nzb, nz_do3d
                    local_pf(i,j,k) = to_be_resorted(k,j,i)
@@ -376,5 +378,5 @@
 !--       Determine the Skip-value for the next array. Record end and start 
 !--       require 4 byte each.
-          skip_do_avs = skip_do_avs + ( ((nx+2)*(ny+2)*(nz_do3d+1)) * 4 + 8 )
+          skip_do_avs = skip_do_avs + ( ((nx+2*nbgp)*(ny+2*nbgp)*(nz_do3d+1)) * 4 + 8 )
        ENDIF
 
@@ -386,5 +388,5 @@
 !--       of compressed data.
           CALL write_compressed( local_pf, 30, 33, myid, nxl, nxr, nyn, nys, &
-                                 nzb, nz_do3d, prec )
+                                 nzb, nz_do3d, prec, nbgp )
        ELSE
 !
@@ -400,5 +402,5 @@
                    WRITE ( 30 )  simulated_time, do3d_time_count(av), av
                 ENDIF
-                WRITE ( 30 )  nxl-1, nxr+1, nys-1, nyn+1, nzb, nz_do3d
+                WRITE ( 30 )  nxlg, nxrg, nysg, nyng, nzb, nz_do3d
                 WRITE ( 30 )  local_pf
 
@@ -411,17 +413,17 @@
                 IF ( nxr == nx  .AND.  nyn /= ny )  THEN
                    nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_do3d(av,if), &
-                                  local_pf(nxl:nxr+1,nys:nyn,nzb:nz_do3d),    &
+                                  local_pf(nxl:nxrg,nys:nyn,nzb:nz_do3d),    &
                       start = (/ nxl+1, nys+1, nzb+1, do3d_time_count(av) /), &
-                      count = (/ nxr-nxl+2, nyn-nys+1, nz_do3d-nzb+1, 1 /) )
+                      count = (/ nxr-nxl+1+nbgp, nyn-nys+1, nz_do3d-nzb+1, 1 /) )
                 ELSEIF ( nxr /= nx  .AND.  nyn == ny )  THEN
                    nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_do3d(av,if), &
-                                  local_pf(nxl:nxr,nys:nyn+1,nzb:nz_do3d),    &
+                                  local_pf(nxl:nxr,nys:nyng,nzb:nz_do3d),    &
                       start = (/ nxl+1, nys+1, nzb+1, do3d_time_count(av) /), &
-                      count = (/ nxr-nxl+1, nyn-nys+2, nz_do3d-nzb+1, 1 /) )
+                      count = (/ nxr-nxl+1, nyn-nys+1+nbgp, nz_do3d-nzb+1, 1 /) )
                 ELSEIF ( nxr == nx  .AND.  nyn == ny )  THEN
                    nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_do3d(av,if), &
-                                  local_pf(nxl:nxr+1,nys:nyn+1,nzb:nz_do3d),  &
+                                  local_pf(nxl:nxrg,nys:nyng,nzb:nz_do3d),  &
                       start = (/ nxl+1, nys+1, nzb+1, do3d_time_count(av) /), &
-                      count = (/ nxr-nxl+2, nyn-nys+2, nz_do3d-nzb+1, 1 /) )
+                      count = (/ nxr-nxl+1+nbgp, nyn-nys+1+nbgp, nz_do3d-nzb+1, 1 /) )
                 ELSE
                    nc_stat = NF90_PUT_VAR( id_set_3d(av), id_var_do3d(av,if), &
Index: /palm/trunk/SOURCE/data_output_mask.f90
===================================================================
--- /palm/trunk/SOURCE/data_output_mask.f90	(revision 666)
+++ /palm/trunk/SOURCE/data_output_mask.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! Calls of exchange_horiz are modified.
 !
 ! Former revisions:
@@ -123,5 +123,5 @@
              IF ( av == 0 )  THEN
                 tend = prt_count
-                CALL exchange_horiz( tend )
+                CALL exchange_horiz( tend, nbgp )
                 DO  i = 1, mask_size_l(mid,1)
                    DO  j = 1, mask_size_l(mid,2)
@@ -134,5 +134,5 @@
                 resorted = .TRUE.
              ELSE
-                CALL exchange_horiz( pc_av )
+                CALL exchange_horiz( pc_av, nbgp )
                 to_be_resorted => pc_av
              ENDIF
@@ -159,5 +159,5 @@
                    ENDDO
                 ENDDO
-                CALL exchange_horiz( tend )
+                CALL exchange_horiz( tend, nbgp )
                 DO  i = 1, mask_size_l(mid,1)
                    DO  j = 1, mask_size_l(mid,2)
@@ -170,5 +170,5 @@
                 resorted = .TRUE.
              ELSE
-                CALL exchange_horiz( pr_av )
+                CALL exchange_horiz( pr_av, nbgp )
                 to_be_resorted => pr_av
              ENDIF
@@ -439,4 +439,5 @@
 
        if = if + 1
+
     ENDDO
 
Index: /palm/trunk/SOURCE/diffusion_e.f90
===================================================================
--- /palm/trunk/SOURCE/diffusion_e.f90	(revision 666)
+++ /palm/trunk/SOURCE/diffusion_e.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng
 !
 ! Former revisions:
@@ -68,6 +68,6 @@
        REAL            ::  dvar_dz, l_stable, phi_m, var_reference
        REAL            ::  ddzu(1:nzt+1), dd2zu(1:nzt), ddzw(1:nzt+1), &
-                           l_grid(1:nzt), zu(0:nzt+1), zw(0:nzt+1)
-       REAL, DIMENSION(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) :: diss, tend
+                           l_grid(1:nzt), zu(nzb:nzt+1), zw(nzb:nzt+1)
+       REAL, DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) :: diss, tend
        REAL, DIMENSION(:,:), POINTER   ::  rif
        REAL, DIMENSION(:,:,:), POINTER ::  e, km, var
@@ -289,6 +289,6 @@
        REAL            ::  dvar_dz, l_stable, phi_m, var_reference
        REAL            ::  ddzu(1:nzt+1), dd2zu(1:nzt), ddzw(1:nzt+1), &
-                           l_grid(1:nzt), zu(0:nzt+1), zw(0:nzt+1)
-       REAL, DIMENSION(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) ::  diss, tend
+                           l_grid(1:nzt), zu(nzb:nzt+1), zw(nzb:nzt+1)
+       REAL, DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) :: diss, tend
        REAL, DIMENSION(:,:), POINTER   ::  rif
        REAL, DIMENSION(:,:,:), POINTER ::  e, km, var
Index: /palm/trunk/SOURCE/diffusion_s.f90
===================================================================
--- /palm/trunk/SOURCE/diffusion_s.f90	(revision 666)
+++ /palm/trunk/SOURCE/diffusion_s.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng
 !
 ! Former revisions:
@@ -64,5 +64,5 @@
        REAL    ::  vertical_gridspace
        REAL    ::  ddzu(1:nzt+1), ddzw(1:nzt+1)
-       REAL    ::  tend(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1)
+       REAL    ::  tend(nzb:nzt+1,nysg:nyng,nxlg:nxrg)
        REAL    ::  wall_s_flux(0:4)
        REAL, DIMENSION(:,:),   POINTER ::  s_flux_b, s_flux_t
@@ -176,5 +176,5 @@
        REAL    ::  vertical_gridspace
        REAL    ::  ddzu(1:nzt+1), ddzw(1:nzt+1)
-       REAL    ::  tend(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1)
+       REAL    ::  tend(nzb:nzt+1,nysg:nyng,nxlg:nxrg)
        REAL    ::  wall_s_flux(0:4)
        REAL, DIMENSION(:,:),   POINTER ::  s_flux_b, s_flux_t
Index: /palm/trunk/SOURCE/diffusion_u.f90
===================================================================
--- /palm/trunk/SOURCE/diffusion_u.f90	(revision 666)
+++ /palm/trunk/SOURCE/diffusion_u.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng
 !
 ! Former revisions:
@@ -72,6 +72,6 @@
        INTEGER ::  i, j, k
        REAL    ::  kmym_x, kmym_y, kmyp_x, kmyp_y, kmzm, kmzp
-       REAL    ::  ddzu(1:nzt+1), ddzw(1:nzt+1), km_damp_y(nys-1:nyn+1)
-       REAL    ::  tend(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1)
+       REAL    ::  ddzu(1:nzt+1), ddzw(1:nzt+1), km_damp_y(nysg:nyng)
+       REAL    ::  tend(nzb:nzt+1,nysg:nyng,nxlg:nxrg)
        REAL, DIMENSION(:,:),   POINTER ::  usws, uswst
        REAL, DIMENSION(:,:,:), POINTER ::  km, u, v, w
@@ -177,5 +177,5 @@
                       &   - kmzm * ( ( u(k,j,i)   - u(k-1,j,i)   ) * ddzu(k) &
                       &            + ( w(k-1,j,i) - w(k-1,j,i-1) ) * ddx     &
-                      &            )                                         &
+                      &            )                                          &
                       &   ) * ddzw(k)
              ENDDO
@@ -248,6 +248,6 @@
        INTEGER ::  i, j, k
        REAL    ::  kmym_x, kmym_y, kmyp_x, kmyp_y, kmzm, kmzp
-       REAL    ::  ddzu(1:nzt+1), ddzw(1:nzt+1), km_damp_y(nys-1:nyn+1)
-       REAL    ::  tend(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1)
+       REAL    ::  ddzu(1:nzt+1), ddzw(1:nzt+1), km_damp_y(nysg:nyng)
+       REAL    ::  tend(nzb:nzt+1,nysg:nyng,nxlg:nxrg)
        REAL, DIMENSION(nzb:nzt+1)      ::  usvs
        REAL, DIMENSION(:,:),   POINTER ::  usws, uswst
Index: /palm/trunk/SOURCE/diffusion_v.f90
===================================================================
--- /palm/trunk/SOURCE/diffusion_v.f90	(revision 666)
+++ /palm/trunk/SOURCE/diffusion_v.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng
 !
 ! Former revisions:
@@ -70,6 +70,6 @@
        INTEGER ::  i, j, k
        REAL    ::  kmxm_x, kmxm_y, kmxp_x, kmxp_y, kmzm, kmzp
-       REAL    ::  ddzu(1:nzt+1), ddzw(1:nzt+1), km_damp_x(nxl-1:nxr+1)
-       REAL    ::  tend(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1)
+       REAL    ::  ddzu(1:nzt+1), ddzw(1:nzt+1), km_damp_x(nxlg:nxrg)
+       REAL    ::  tend(nzb:nzt+1,nysg:nyng,nxlg:nxrg)
        REAL, DIMENSION(:,:),   POINTER ::  vsws, vswst
        REAL, DIMENSION(:,:,:), POINTER ::  km, u, v, w
@@ -246,6 +246,6 @@
        INTEGER ::  i, j, k
        REAL    ::  kmxm_x, kmxm_y, kmxp_x, kmxp_y, kmzm, kmzp
-       REAL    ::  ddzu(1:nzt+1), ddzw(1:nzt+1), km_damp_x(nxl-1:nxr+1)
-       REAL    ::  tend(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1)
+       REAL    ::  ddzu(1:nzt+1), ddzw(1:nzt+1), km_damp_x(nxlg:nxrg)
+       REAL    ::  tend(nzb:nzt+1,nysg:nyng,nxlg:nxrg)
        REAL, DIMENSION(nzb:nzt+1)      ::  vsus
        REAL, DIMENSION(:,:),   POINTER ::  vsws, vswst
Index: /palm/trunk/SOURCE/diffusion_w.f90
===================================================================
--- /palm/trunk/SOURCE/diffusion_w.f90	(revision 666)
+++ /palm/trunk/SOURCE/diffusion_w.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng
 !
 ! Former revisions:
@@ -64,7 +64,7 @@
        REAL    ::  kmxm_x, kmxm_z, kmxp_x, kmxp_z, kmym_y, kmym_z, kmyp_y, &
                    kmyp_z
-       REAL    ::  ddzu(1:nzt+1), ddzw(1:nzt+1), km_damp_x(nxl-1:nxr+1), &
-                   km_damp_y(nys-1:nyn+1)
-       REAL    ::  tend(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1)
+       REAL    ::  ddzu(1:nzt+1), ddzw(1:nzt+1), km_damp_x(nxlg:nxrg),        & 
+                   km_damp_y(nysg:nyng)
+       REAL    ::  tend(nzb:nzt+1,nysg:nyng,nxlg:nxrg)
        REAL, DIMENSION(:,:,:), POINTER ::  km, u, v, w
        REAL, DIMENSION(nzb:nzt+1,nys:nyn,nxl:nxr) ::  wsus, wsvs
@@ -211,7 +211,7 @@
        REAL    ::  kmxm_x, kmxm_z, kmxp_x, kmxp_z, kmym_y, kmym_z, kmyp_y, &
                    kmyp_z
-       REAL    ::  ddzu(1:nzt+1), ddzw(1:nzt+1), km_damp_x(nxl-1:nxr+1), &
-                   km_damp_y(nys-1:nyn+1)
-       REAL    ::  tend(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1)
+       REAL    ::  ddzu(1:nzt+1), ddzw(1:nzt+1), km_damp_x(nxlg:nxrg),        & 
+                   km_damp_y(nysg:nyng)
+       REAL    ::  tend(nzb:nzt+1,nysg:nyng,nxlg:nxrg)
        REAL, DIMENSION(nzb:nzt+1)      ::  wsus, wsvs
        REAL, DIMENSION(:,:,:), POINTER ::  km, u, v, w
Index: /palm/trunk/SOURCE/diffusivities.f90
===================================================================
--- /palm/trunk/SOURCE/diffusivities.f90	(revision 666)
+++ /palm/trunk/SOURCE/diffusivities.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng
 ! 
 ! Former revisions:
@@ -54,5 +54,5 @@
     REAL, SAVE ::  phi_m = 1.0
 
-    REAL    ::  var(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1)
+    REAL    ::  var(nzb:nzt+1,nysg:nyng,nxlg:nxrg)
 
     REAL, DIMENSION(1:nzt) ::  l, ll, sqrt_e
@@ -73,6 +73,6 @@
 
     !$OMP DO
-    DO  i = nxl-1, nxr+1
-       DO  j = nys-1, nyn+1
+    DO  i = nxlg, nxrg
+       DO  j = nysg, nyng
 
 !
@@ -157,5 +157,5 @@
 
     sums_l_l(nzt+1,:,tn) = sums_l_l(nzt,:,tn)   ! quasi boundary-condition for
-                                                ! data output
+                                                  ! data output
 
     !$OMP END PARALLEL
@@ -167,6 +167,6 @@
 !-- values of the diffusivities are not needed
     !$OMP PARALLEL DO
-    DO  i = nxl-1, nxr+1
-       DO  j = nys-1, nyn+1
+    DO  i = nxlg, nxrg
+       DO  j = nysg, nyng
           km(nzb_s_inner(j,i),j,i) = km(nzb_s_inner(j,i)+1,j,i)
           km(nzt+1,j,i)            = km(nzt,j,i)
Index: /palm/trunk/SOURCE/disturb_field.f90
===================================================================
--- /palm/trunk/SOURCE/disturb_field.f90	(revision 666)
+++ /palm/trunk/SOURCE/disturb_field.f90	(revision 667)
@@ -4,5 +4,6 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng
+! Calls of exchange_horiz are modified.
 !
 ! Former revisions:
@@ -44,9 +45,9 @@
 
     INTEGER ::  i, j, k
-    INTEGER ::  nzb_uv_inner(nys-1:nyn+1,nxl-1:nxr+1)
+    INTEGER ::  nzb_uv_inner(nysg:nyng,nxlg:nxrg)
 
     REAL    ::  randomnumber,                             &
-                dist1(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
-                field(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1)
+                dist1(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+                field(nzb:nzt+1,nysg:nyng,nxlg:nxrg)
     REAL, DIMENSION(:,:,:), ALLOCATABLE ::  dist2
 
@@ -57,5 +58,5 @@
 !-- Create an additional temporary array and initialize the arrays needed
 !-- to store the disturbance
-    ALLOCATE( dist2(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+    ALLOCATE( dist2(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
     dist1 = 0.0
     dist2 = 0.0
@@ -102,6 +103,5 @@
 !-- Exchange of ghost points for the random perturbation
 
-    CALL exchange_horiz( dist1 )
-
+    CALL exchange_horiz( dist1, nbgp )
 !
 !-- Applying the Shuman filter in order to smooth the perturbations.
@@ -128,5 +128,5 @@
 !-- Exchange of ghost points for the filtered perturbation.
 !-- Afterwards, filter operation and exchange of ghost points are repeated.
-    CALL exchange_horiz( dist2 )
+    CALL exchange_horiz( dist2, nbgp )
 
     DO  i = nxl, nxr
@@ -141,5 +141,5 @@
     ENDDO
 
-    CALL exchange_horiz( dist1 )
+    CALL exchange_horiz( dist1, nbgp )
 
 !
@@ -148,6 +148,6 @@
 !-- (diffusion criterion))
     IF ( TRIM( topography ) /= 'flat' )  THEN
-       DO  i = nxl-1, nxr+1
-          DO  j = nys-1, nyn+1
+       DO  i = nxlg, nxrg
+          DO  j = nysg, nyng
              dist1(nzb:nzb_uv_inner(j,i)+1,j,i) = 0.0
           ENDDO
@@ -157,6 +157,6 @@
 !
 !-- Random perturbation is added to the array to be disturbed.
-    DO  i = nxl-1, nxr+1
-       DO  j = nys-1, nyn+1
+    DO  i = nxlg, nxrg
+       DO  j = nysg, nyng
           DO  k = disturbance_level_ind_b-2, disturbance_level_ind_t+2
              field(k,j,i) = field(k,j,i) + dist1(k,j,i)
Index: /palm/trunk/SOURCE/exchange_horiz.f90
===================================================================
--- /palm/trunk/SOURCE/exchange_horiz.f90	(revision 666)
+++ /palm/trunk/SOURCE/exchange_horiz.f90	(revision 667)
@@ -1,8 +1,13 @@
- SUBROUTINE exchange_horiz( ar )
+ SUBROUTINE exchange_horiz( ar, nbgp_local)
 
 !------------------------------------------------------------------------------!
 ! Current revisions:
 ! -----------------
-! 
+! Dynamic exchange of ghost points with nbgp_local to ensure that no useless 
+! ghost points exchanged in case of multigrid. type_yz(0) and type_xz(0)
+! used for normal grid, the remaining types used for the several grid levels.
+! Exchange is done via MPI-Vectors with a dynamic value of ghost points which
+! depend on the advection scheme. Exchange of left and right PEs is 10% faster 
+! with MPI-Vectors than without. 
 !
 ! Former revisions:
@@ -41,10 +46,15 @@
     INTEGER, DIMENSION(MPI_STATUS_SIZE,4) ::  wait_stat
 #endif
-
-    REAL ::  ar(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1)
-
+    INTEGER :: i,nbgp_local
+    REAL, DIMENSION(nzb:nzt+1,nys-nbgp_local:nyn+nbgp_local, &
+                    nxl-nbgp_local:nxr+nbgp_local) ::  ar
 
     CALL cpu_log( log_point_s(2), 'exchange_horiz', 'start' )
 
+    IF ( exchange_mg == .TRUE. ) THEN
+      i = grid_level
+    ELSE
+      i = 0
+    END IF
 #if defined( __parallel )
 
@@ -56,6 +66,6 @@
 !--    within the PE memory
        IF ( bc_lr == 'cyclic' )  THEN
-          ar(:,nys:nyn,nxl-1) = ar(:,nys:nyn,nxr)
-          ar(:,nys:nyn,nxr+1) = ar(:,nys:nyn,nxl)
+          ar(:,:,nxl-nbgp_local:nxl-1) = ar(:,:,nxr-nbgp_local+1:nxr)
+          ar(:,:,nxr+1:nxr+nbgp_local) = ar(:,:,nxl:nxl+nbgp_local-1)
        ENDIF
 
@@ -65,18 +75,17 @@
 !
 !--    Send left boundary, receive right one
-       CALL MPI_ISEND(                                                     &
-               ar(nzb,nys-1,nxl), ngp_yz(grid_level), MPI_REAL, pleft,  0, &
-                          comm2d, req(1), ierr )
-       CALL MPI_IRECV(                                                       &
-               ar(nzb,nys-1,nxr+1), ngp_yz(grid_level), MPI_REAL, pright, 0, &
-                          comm2d, req(2), ierr )
+       CALL MPI_ISEND(ar(nzb,nys-nbgp_local,nxl),1,type_yz(i),pleft,0,comm2d,&
+                      req(1),ierr)
+       CALL MPI_IRECV(ar(nzb,nys-nbgp_local,nxr+1),1,type_yz(i),pright,0,&
+                     comm2d,req(2),ierr)
 !
 !--    Send right boundary, receive left one
-       CALL MPI_ISEND(                                                     &
-               ar(nzb,nys-1,nxr), ngp_yz(grid_level), MPI_REAL, pright, 1, &
-                          comm2d, req(3), ierr )
-       CALL MPI_IRECV(                                                       &
-               ar(nzb,nys-1,nxl-1), ngp_yz(grid_level), MPI_REAL, pleft,  1, &
-                          comm2d, req(4), ierr )
+
+
+       CALL MPI_ISEND(ar(nzb,nys-nbgp_local,nxr+1-nbgp_local),1,type_yz(i),pright, 1,  &
+                      comm2d, req(3), ierr )
+       CALL MPI_IRECV(ar(nzb,nys-nbgp_local,nxl-nbgp_local),1,type_yz(i),pleft,1,&
+                      comm2d,req(4), ierr)
+
        CALL MPI_WAITALL( 4, req, wait_stat, ierr )
 
@@ -89,6 +98,6 @@
 !--    within the PE memory
        IF ( bc_ns == 'cyclic' )  THEN
-          ar(:,nys-1,:) = ar(:,nyn,:)
-          ar(:,nyn+1,:) = ar(:,nys,:)
+          ar(:,nys-nbgp_local:nys-1,:) = ar(:,nyn-nbgp_local+1:nyn,:)
+          ar(:,nyn+1:nyn+nbgp_local,:) = ar(:,nys:nys+nbgp_local-1,:)
        ENDIF
 
@@ -98,18 +107,19 @@
 !
 !--    Send front boundary, receive rear one
-       CALL MPI_ISEND( ar(nzb,nys,nxl-1),   1, type_xz(grid_level), psouth, 0, &
+!--    MPI_ISEND initial send adress changed, type_xz() is sendet nbgp times
+
+       CALL MPI_ISEND( ar(nzb,nys,nxl-nbgp_local),1, type_xz(i), psouth, 0, &
                        comm2d, req(1), ierr )
-       CALL MPI_IRECV( ar(nzb,nyn+1,nxl-1), 1, type_xz(grid_level), pnorth, 0, &
+       CALL MPI_IRECV( ar(nzb,nyn+1,nxl-nbgp_local),1, type_xz(i), pnorth, 0, &
                        comm2d, req(2), ierr )
 !
 !--    Send rear boundary, receive front one
-       CALL MPI_ISEND( ar(nzb,nyn,nxl-1),   1, type_xz(grid_level), pnorth, 1, &
+       CALL MPI_ISEND( ar(nzb,nyn-nbgp_local+1,nxl-nbgp_local),1, type_xz(i), pnorth, 1, &
                        comm2d, req(3), ierr )
-       CALL MPI_IRECV( ar(nzb,nys-1,nxl-1), 1, type_xz(grid_level), psouth, 1, &
+       CALL MPI_IRECV( ar(nzb,nys-nbgp_local,nxl-nbgp_local),1, type_xz(i), psouth, 1, &
                        comm2d, req(4), ierr )
        call MPI_WAITALL( 4, req, wait_stat, ierr )
 
     ENDIF
-
 
 #else
@@ -118,16 +128,16 @@
 !-- Lateral boundary conditions in the non-parallel case
     IF ( bc_lr == 'cyclic' )  THEN
-       ar(:,nys:nyn,nxl-1) = ar(:,nys:nyn,nxr)
-       ar(:,nys:nyn,nxr+1) = ar(:,nys:nyn,nxl)
+        ar(:,:,nxl-nbgp_local:nxl-1) = ar(:,:,nxr-nbgp_local+1:nxr)
+        ar(:,:,nxr+1:nxr+nbgp_local) = ar(:,:,nxl:nxl+nbgp_local-1)
     ENDIF
 
     IF ( bc_ns == 'cyclic' )  THEN
-       ar(:,nys-1,:) = ar(:,nyn,:)
-       ar(:,nyn+1,:) = ar(:,nys,:)
+        ar(:,nys-nbgp_local:nys-1,:) = ar(:,nyn-nbgp_local+1:nyn,:)
+        ar(:,nyn+1:nyn+nbgp_local,:) = ar(:,nys:nys+nbgp_local-1,:)
     ENDIF
 
 #endif
-
     CALL cpu_log( log_point_s(2), 'exchange_horiz', 'stop' )
 
+
  END SUBROUTINE exchange_horiz
Index: /palm/trunk/SOURCE/exchange_horiz_2d.f90
===================================================================
--- /palm/trunk/SOURCE/exchange_horiz_2d.f90	(revision 666)
+++ /palm/trunk/SOURCE/exchange_horiz_2d.f90	(revision 667)
@@ -4,5 +4,6 @@
 ! Current revisions:
 ! -----------------
-! 
+! Dynamic exchange of ghost points with nbgp, which depends on the advection
+! scheme. Exchange between left and right PEs is now done with MPI-vectors.
 !
 ! Former revisions:
@@ -37,5 +38,6 @@
     IMPLICIT NONE
 
-    REAL ::  ar(nys-1:nyn+1,nxl-1:nxr+1)
+    REAL ::  ar(nysg:nyng,nxlg:nxrg)
+    INTEGER :: i
 
 
@@ -51,17 +53,18 @@
 !--    One-dimensional decomposition along y, boundary values can be exchanged
 !--    within the PE memory
-       ar(nys:nyn,nxl-1) = ar(nys:nyn,nxr)
-       ar(nys:nyn,nxr+1) = ar(nys:nyn,nxl)
+       ar(:,nxl-nbgp:nxl-1) = ar(:,nxr-nbgp+1:nxr)
+       ar(:,nxr+1:nxr+nbgp) = ar(:,nxl:nxl+nbgp-1)
 
     ELSE
 !
 !--    Send left boundary, receive right one
-       CALL MPI_SENDRECV( ar(nys,nxl),   ngp_y, MPI_REAL, pleft,  0, &
-                          ar(nys,nxr+1), ngp_y, MPI_REAL, pright, 0, &
+
+       CALL MPI_SENDRECV( ar(nysg,nxl), 1, type_y, pleft,  0, &
+                          ar(nysg,nxr+1), 1, type_y, pright, 0, &
                           comm2d, status, ierr )
 !
 !--    Send right boundary, receive left one
-       CALL MPI_SENDRECV( ar(nys,nxr),   ngp_y, MPI_REAL, pright,  1, &
-                          ar(nys,nxl-1), ngp_y, MPI_REAL, pleft,   1, &
+       CALL MPI_SENDRECV( ar(nysg,nxr+1-nbgp), 1, type_y, pright,  1, &
+                          ar(nysg,nxlg), 1, type_y, pleft,   1, &
                           comm2d, status, ierr )
     ENDIF
@@ -71,18 +74,20 @@
 !--    One-dimensional decomposition along x, boundary values can be exchanged
 !--    within the PE memory
-       ar(nys-1,:) = ar(nyn,:)
-       ar(nyn+1,:) = ar(nys,:)
+       ar(nys-nbgp:nys-1,:) = ar(nyn-nbgp+1:nyn,:)
+       ar(nyn+1:nyn+nbgp,:) = ar(nys:nys+nbgp-1,:)
 
     ELSE
 !
 !--    Send front boundary, receive rear one
-       CALL MPI_SENDRECV( ar(nys,nxl-1),   1, type_x, psouth, 0, &
-                          ar(nyn+1,nxl-1), 1, type_x, pnorth, 0, &
+
+       CALL MPI_SENDRECV( ar(nys,nxlg), 1, type_x, psouth, 0, &        !replace number of sended elements from 
+                          ar(nyn+1,nxlg), 1, type_x, pnorth, 0, &      ! nbgp to 1
                           comm2d, status, ierr )
 !
 !--    Send rear boundary, receive front one
-       CALL MPI_SENDRECV( ar(nyn,nxl-1),   1, type_x, pnorth, 1, &
-                          ar(nys-1,nxl-1), 1, type_x, psouth, 1, &
-                          comm2d, status, ierr )
+       CALL MPI_SENDRECV( ar(nyn+1-nbgp,nxlg), 1, type_x, pnorth, 1, &
+                          ar(nysg,nxlg), 1, type_x, psouth, 1, &
+                          comm2d, status, ierr )
+
     ENDIF
 
@@ -92,12 +97,13 @@
 !-- Lateral boundary conditions in the non-parallel case
     IF ( bc_lr == 'cyclic' )  THEN
-       ar(nys:nyn,nxl-1) = ar(nys:nyn,nxr)
-       ar(nys:nyn,nxr+1) = ar(nys:nyn,nxl)
+       ar(:,nxl-nbgp:nxl-1) = ar(:,nxr-nbgp+1:nxr)
+       ar(:,nxr+1:nxr+nbgp) = ar(:,nxl:nxl+nbgp-1)
     ENDIF
 
     IF ( bc_ns == 'cyclic' )  THEN
-       ar(nys-1,:) = ar(nyn,:)
-       ar(nyn+1,:) = ar(nys,:)
-    ENDIF
+       ar(nys-nbgp:nys-1,:) = ar(nyn-nbgp+1:nyn,:)
+       ar(nyn+1:nyn+nbgp,:) = ar(nys:nys+nbgp-1,:)
+    ENDIF
+
 
 #endif
@@ -106,9 +112,24 @@
 !-- Neumann-conditions at inflow/outflow in case of non-cyclic boundary
 !-- conditions
-    IF ( inflow_l .OR. outflow_l )  ar(:,nxl-1) = ar(:,nxl)
-    IF ( inflow_r .OR. outflow_r )  ar(:,nxr+1) = ar(:,nxr)
-    IF ( inflow_s .OR. outflow_s )  ar(nys-1,:) = ar(nys,:)
-    IF ( inflow_n .OR. outflow_n )  ar(nyn+1,:) = ar(nyn,:)
-
+    IF ( inflow_l .OR. outflow_l )  THEN
+       DO i=nbgp, 1, -1
+         ar(:,nxl-i) = ar(:,nxl)
+       END DO
+    END IF
+    IF ( inflow_r .OR. outflow_r )  THEN 
+       DO i=1, nbgp
+          ar(:,nxr+i) = ar(:,nxr)
+       END DO
+    END IF
+    IF ( inflow_s .OR. outflow_s )  THEN
+       DO i=nbgp, 1, -1
+         ar(nys-i,:) = ar(nys,:)
+       END DO
+    END IF
+    IF ( inflow_n .OR. outflow_n )  THEN
+       DO i=1, nbgp
+         ar(nyn+i,:) = ar(nyn,:)
+       END DO
+    END IF
     CALL cpu_log( log_point_s(13), 'exchange_horiz_2d', 'stop' )
 
@@ -134,5 +155,7 @@
     IMPLICIT NONE
 
-    INTEGER ::  ar(nys-1:nyn+1,nxl-1:nxr+1)
+    REAL ::  ar(nysg:nyng,nxlg:nxrg)
+    INTEGER :: i
+
 
 
@@ -154,12 +177,13 @@
 !
 !--    Send left boundary, receive right one
-       CALL MPI_SENDRECV( ar(nys,nxl),   ngp_y, MPI_INTEGER, pleft,  0, &
-                          ar(nys,nxr+1), ngp_y, MPI_INTEGER, pright, 0, &
+       CALL MPI_SENDRECV( ar(nysg,nxl), 1, type_y_int, pleft,  0, &
+                          ar(nysg,nxr+1), 1, type_y_int, pright, 0, &
                           comm2d, status, ierr )
 !
 !--    Send right boundary, receive left one
-       CALL MPI_SENDRECV( ar(nys,nxr),   ngp_y, MPI_INTEGER, pright,  1, &
-                          ar(nys,nxl-1), ngp_y, MPI_INTEGER, pleft,   1, &
-                          comm2d, status, ierr )
+       CALL MPI_SENDRECV( ar(nysg,nxr+1-nbgp), 1, type_y_int, pright,  1, &
+                          ar(nysg,nxlg), 1, type_y_int, pleft,   1, &
+                          comm2d, status, ierr )
+
     ENDIF
 
@@ -168,18 +192,21 @@
 !--    One-dimensional decomposition along x, boundary values can be exchanged
 !--    within the PE memory
-       ar(nys-1,:) = ar(nyn,:)
-       ar(nyn+1,:) = ar(nys,:)
+       ar(nysg:nys-1,:) = ar(nyn+1-nbgp:nyn,:)
+       ar(nyn+1:nyng,:) = ar(nys:nys-1+nbgp,:)
+
 
     ELSE
 !
 !--    Send front boundary, receive rear one
-       CALL MPI_SENDRECV( ar(nys,nxl-1),   1, type_x_int, psouth, 0, &
-                          ar(nyn+1,nxl-1), 1, type_x_int, pnorth, 0, &
-                          comm2d, status, ierr )
+       CALL MPI_SENDRECV( ar(nys,nxlg),1, type_x_int, psouth, 0, &
+                          ar(nyn+1,nxlg),1, type_x_int, pnorth, 0, &
+                          comm2d, status, ierr )
+
 !
 !--    Send rear boundary, receive front one
-       CALL MPI_SENDRECV( ar(nyn,nxl-1),   1, type_x_int, pnorth, 1, &
-                          ar(nys-1,nxl-1), 1, type_x_int, psouth, 1, &
-                          comm2d, status, ierr )
+       CALL MPI_SENDRECV( ar(nyn+1-nbgp,nxlg), nbgp, type_x_int, pnorth, 1, &
+                          ar(nysg,nxlg), nbgp, type_x_int, psouth, 1, &
+                          comm2d, status, ierr )
+
     ENDIF
 
@@ -194,6 +221,6 @@
 
     IF ( bc_ns == 'cyclic' )  THEN
-       ar(nys-1,:) = ar(nyn,:)
-       ar(nyn+1,:) = ar(nys,:)
+       ar(nysg:nys-1,:) = ar(nyn+1-nbgp:nyn,:)
+       ar(nyn+1:nyng,:) = ar(nys:nys-1+nbgp,:)
     ENDIF
 
Index: /palm/trunk/SOURCE/flow_statistics.f90
===================================================================
--- /palm/trunk/SOURCE/flow_statistics.f90	(revision 666)
+++ /palm/trunk/SOURCE/flow_statistics.f90	(revision 667)
@@ -4,5 +4,14 @@
 ! Current revisions:
 ! -----------------
-! 
+! When advection is computed with ws-scheme, turbulent fluxes are already
+! computed in the respective advection routines and buffered in arrays
+! sums_xx_ws_l(). This is due to a consistent treatment of statistics with the
+! numerics and to avoid unphysical kinks near the surface. 
+! So some if requests has to be done to dicern between fluxes from ws-scheme
+! other advection schemes. 
+! Furthermore the computation of z_i is only done if the heat flux exceeds a
+! minimum value. This affects only simulations of a neutral boundary layer and
+! is due to reasons of computations in the advection scheme.
+!
 !
 ! Former revisions:
@@ -102,5 +111,4 @@
     REAL    ::  sums_ll(nzb:nzt+1,2)
 
-
     CALL cpu_log( log_point(10), 'flow_statistics', 'start' )
 
@@ -133,4 +141,41 @@
        sums_l(nzb+10,pr_palm,0) = sums_divnew_l(sr)  ! new divergence from pres
 
+!
+!--    Copy the turbulent quantities, evaluated in the advection routines to 
+!--    the local array sums_l() for further computations
+       IF ( ws_scheme_mom )  THEN
+!       
+!--       Boundary condition for u'u' and v'v', because below the surface no
+!--       computation for these quantities is done.
+          DO  i = nxl, nxr
+             DO  j =  nys, nyn
+                sums_us2_ws_l(nzb_u_inner(j,i),sr) =                          & 
+                    sums_us2_ws_l(nzb_u_inner(j,i)+1,sr)
+                sums_vs2_ws_l(nzb_v_inner(j,i),sr) =                          &  
+                    sums_vs2_ws_l(nzb_v_inner(j,i)+1,sr)
+             ENDDO
+          ENDDO
+!          
+!--       Swap the turbulent quantities evaluated in advec_ws.
+          sums_l(:,13,0) = sums_wsus_ws_l(:,sr)       ! w*u* 
+          sums_l(:,15,0) = sums_wsvs_ws_l(:,sr)       ! w*v* 
+          sums_l(:,30,0) = sums_us2_ws_l(:,sr)        ! u*2 
+          sums_l(:,31,0) = sums_vs2_ws_l(:,sr)        ! v*2 
+          sums_l(:,32,0) = sums_ws2_ws_l(:,sr)        ! w*2 
+          sums_l(:,34,0) = sums_l(:,34,0) + 0.5 *                             & 
+                (sums_us2_ws_l(:,sr) + sums_vs2_ws_l(:,sr)                    &
+                + sums_ws2_ws_l(:,sr))                      ! e*
+          DO  k = nzb, nzt
+             sums_l(nzb+5,pr_palm,0) = sums_l(nzb+5,pr_palm,0) + 0.5 * (      &
+                sums_us2_ws_l(k,sr) + sums_vs2_ws_l(k,sr) +                   &
+                sums_ws2_ws_l(k,sr))
+          ENDDO
+       ENDIF
+       IF ( ws_scheme_sca )  THEN
+          sums_l(:,17,0) = sums_wspts_ws_l(:,sr)      ! w*pts* from advec_s_ws
+          IF ( ocean ) sums_l(:,66,0) = sums_wssas_ws_l(:,sr) ! w*sa*
+          IF ( humidity  .OR.  passive_scalar ) sums_l(:,49,0) =              &
+                                                   sums_wsqs_ws_l(:,sr) !w*q* 
+       ENDIF
 ! 
 !--    Horizontally averaged profiles of horizontal velocities and temperature.
@@ -138,4 +183,5 @@
 !--    for other horizontal averages.
        tn = 0
+
        !$OMP PARALLEL PRIVATE( i, j, k, tn )
 #if defined( __intel_openmp_bug )
@@ -215,5 +261,4 @@
        ENDIF
        !$OMP END PARALLEL
-
 !
 !--    Summation of thread sums
@@ -304,4 +349,5 @@
        hom(:,1,2,sr) = sums(:,2)             ! v
        hom(:,1,4,sr) = sums(:,4)             ! pt
+
 
 !
@@ -354,11 +400,5 @@
           DO  j =  nys, nyn
              sums_l_etot = 0.0
-             sums_l_eper = 0.0
              DO  k = nzb_s_inner(j,i), nzt+1
-                u2   = u(k,j,i)**2
-                v2   = v(k,j,i)**2
-                w2   = w(k,j,i)**2
-                ust2 = ( u(k,j,i) - hom(k,1,1,sr) )**2
-                vst2 = ( v(k,j,i) - hom(k,1,2,sr) )**2
 !
 !--             Prognostic and diagnostic variables
@@ -369,9 +409,4 @@
                 sums_l(k,40,tn) = sums_l(k,40,tn) + p(k,j,i)
 
-!
-!--             Variances
-                sums_l(k,30,tn) = sums_l(k,30,tn) + ust2 * rmask(j,i,sr)
-                sums_l(k,31,tn) = sums_l(k,31,tn) + vst2 * rmask(j,i,sr)
-                sums_l(k,32,tn) = sums_l(k,32,tn) + w2   * rmask(j,i,sr)
                 sums_l(k,33,tn) = sums_l(k,33,tn) + &
                                   ( pt(k,j,i)-hom(k,1,4,sr) )**2 * rmask(j,i,sr)
@@ -381,17 +416,8 @@
                                   ( q(k,j,i)-hom(k,1,41,sr) )**2 * rmask(j,i,sr)
                 ENDIF
-!
-!--             Higher moments
-!--             (Computation of the skewness of w further below)
-                sums_l(k,38,tn) = sums_l(k,38,tn) + w(k,j,i) * w2 * &
-                                                    rmask(j,i,sr)
-!
-!--             Perturbation energy
-                sums_l(k,34,tn) = sums_l(k,34,tn) + 0.5 * ( ust2 + vst2 + w2 ) &
-                                                    * rmask(j,i,sr)
+
                 sums_l_etot  = sums_l_etot + &
-                                        0.5 * ( u2 + v2 + w2 ) * rmask(j,i,sr)
-                sums_l_eper  = sums_l_eper + &
-                                        0.5 * ( ust2+vst2+w2 ) * rmask(j,i,sr)
+                                        0.5 * ( u(k,j,i)**2 + v(k,j,i)**2 +    &
+                                        w(k,j,i)**2 ) * rmask(j,i,sr)
              ENDDO
 !
@@ -401,5 +427,4 @@
 !--          allow vectorization of that loop.
              sums_l(nzb+4,pr_palm,tn) = sums_l(nzb+4,pr_palm,tn) + sums_l_etot
-             sums_l(nzb+5,pr_palm,tn) = sums_l(nzb+5,pr_palm,tn) + sums_l_eper
 !
 !--          2D-arrays (being collected in the last column of sums_l)
@@ -420,5 +445,58 @@
 
 !
+!--    Computation of statistics when ws-scheme is not used. Else these 
+!--    quantities are evaluated in the advection routines.
+       IF ( .NOT. ws_scheme_mom )  THEN
+          !$OMP DO
+          DO  i = nxl, nxr
+             DO  j =  nys, nyn
+                sums_l_eper = 0.0
+                DO  k = nzb_s_inner(j,i), nzt+1
+                   u2   = u(k,j,i)**2
+                   v2   = v(k,j,i)**2
+                   w2   = w(k,j,i)**2
+                   ust2 = ( u(k,j,i) - hom(k,1,1,sr) )**2
+                   vst2 = ( v(k,j,i) - hom(k,1,2,sr) )**2
+
+                   sums_l(k,30,tn) = sums_l(k,30,tn) + ust2 * rmask(j,i,sr)
+                   sums_l(k,31,tn) = sums_l(k,31,tn) + vst2 * rmask(j,i,sr)
+                   sums_l(k,32,tn) = sums_l(k,32,tn) + w2   * rmask(j,i,sr)
+!
+!--   Higher moments
+!--  (Computation of the skewness of w further below)
+                   sums_l(k,38,tn) = sums_l(k,38,tn) + w(k,j,i) * w2 * &
+                                                    rmask(j,i,sr)
+!
+!--             Perturbation energy
+
+                   sums_l(k,34,tn) = sums_l(k,34,tn) + 0.5 *       &
+                                  ( ust2 + vst2 + w2 ) * rmask(j,i,sr)
+                   sums_l_eper  = sums_l_eper + &
+                                        0.5 * ( ust2+vst2+w2 ) * rmask(j,i,sr)
+
+                ENDDO
+                sums_l(nzb+5,pr_palm,tn) = sums_l(nzb+5,pr_palm,tn)   &
+                     + sums_l_eper
+             ENDDO
+          ENDDO
+       ELSE
+          !$OMP DO
+          DO  i = nxl, nxr
+             DO  j =  nys, nyn
+                DO  k = nzb_s_inner(j,i), nzt + 1
+                   w2   = w(k,j,i)**2
+!
+!--                Higher moments
+!--                (Computation of the skewness of w further below)
+                   sums_l(k,38,tn) = sums_l(k,38,tn) + w(k,j,i) * w2 * &
+                                                    rmask(j,i,sr)
+                ENDDO
+             ENDDO
+          ENDDO
+       ENDIF
+
+!
 !--    Horizontally averaged profiles of the vertical fluxes
+
        !$OMP DO
        DO  i = nxl, nxr
@@ -587,24 +665,5 @@
                 pts = 0.5 * ( pt(k,j,i)   - hom(k,1,4,sr) + &
                               pt(k+1,j,i) - hom(k+1,1,4,sr) )
-!
-!--             Momentum flux w*u*
-                sums_l(k,13,tn) = sums_l(k,13,tn) + 0.5 *                     &
-                                                    ( w(k,j,i-1) + w(k,j,i) ) &
-                                                    * ust * rmask(j,i,sr)
-!
-!--             Momentum flux w*v*
-                sums_l(k,15,tn) = sums_l(k,15,tn) + 0.5 *                     &
-                                                    ( w(k,j-1,i) + w(k,j,i) ) &
-                                                    * vst * rmask(j,i,sr)
-!
-!--             Heat flux w*pt*
-!--             The following formula (comment line, not executed) does not
-!--             work if applied to subregions
-!                sums_l(k,17,tn) = sums_l(k,17,tn) + 0.5 *                     &
-!                                                    ( pt(k,j,i)+pt(k+1,j,i) ) &
-!                                                    * w(k,j,i) * rmask(j,i,sr)
-                sums_l(k,17,tn) = sums_l(k,17,tn) + pts * w(k,j,i) * &
-                                                    rmask(j,i,sr)
-!
+
 !--             Higher moments
                 sums_l(k,35,tn) = sums_l(k,35,tn) + pts * w(k,j,i)**2 * &
@@ -617,8 +676,10 @@
 !--             but so far there is no other suitable place to calculate)
                 IF ( ocean )  THEN
-                   pts = 0.5 * ( sa(k,j,i)   - hom(k,1,23,sr) + &
+                   IF( .NOT. ws_scheme_sca )  THEN
+                      pts = 0.5 * ( sa(k,j,i)   - hom(k,1,23,sr) + &
                                  sa(k+1,j,i) - hom(k+1,1,23,sr) )
-                   sums_l(k,66,tn) = sums_l(k,66,tn) + pts * w(k,j,i) * &
+                      sums_l(k,66,tn) = sums_l(k,66,tn) + pts * w(k,j,i) * &
                                                        rmask(j,i,sr)
+                   ENDIF
                    sums_l(k,64,tn) = sums_l(k,64,tn) + rho(k,j,i) * &
                                                        rmask(j,i,sr)
@@ -635,8 +696,5 @@
                    sums_l(k,46,tn) = sums_l(k,46,tn) + pts * w(k,j,i) * &
                                                        rmask(j,i,sr)
-                   pts = 0.5 * ( q(k,j,i)   - hom(k,1,41,sr) + &
-                                 q(k+1,j,i) - hom(k+1,1,41,sr) )
-                   sums_l(k,49,tn) = sums_l(k,49,tn) + pts * w(k,j,i) * &
-                                                       rmask(j,i,sr)
+
                    IF ( cloud_physics  .OR.  cloud_droplets )  THEN
                       pts = 0.5 *                                            &
@@ -652,5 +710,5 @@
 !
 !--             Passive scalar flux
-                IF ( passive_scalar )  THEN
+                IF ( passive_scalar .AND. ( .NOT. ws_scheme_sca ))  THEN
                    pts = 0.5 * ( q(k,j,i)   - hom(k,1,41,sr) + &
                                  q(k+1,j,i) - hom(k+1,1,41,sr) )
@@ -661,11 +719,62 @@
 !
 !--             Energy flux w*e*
-                sums_l(k,37,tn) = sums_l(k,37,tn) + w(k,j,i) * 0.5 *           &
-                                              ( ust**2 + vst**2 + w(k,j,i)**2 )&
-                                              * rmask(j,i,sr)
-          
+!--             has to be adjusted
+                sums_l(k,37,tn) = sums_l(k,37,tn) + w(k,j,i) * 0.5 *          &
+                                             ( ust**2 + vst**2 + w(k,j,i)**2 )&
+                                             * rmask(j,i,sr)
              ENDDO
           ENDDO
        ENDDO
+!-     for reasons of speed optimization the loop is splitted, to avoid if-else
+!-     statements inside the loop
+!-     Fluxes which have been computed in part directly inside the advection routines
+!-     treated seperatly.
+!-     First treat the momentum fluxes
+       IF ( .NOT. ws_scheme_mom )  THEN
+         !$OMP DO
+         DO  i = nxl, nxr
+            DO  j = nys, nyn
+               DO  k = nzb_diff_s_inner(j,i)-1, nzt_diff
+                  ust = 0.5 * ( u(k,j,i)   - hom(k,1,1,sr) + &
+                              u(k+1,j,i) - hom(k+1,1,1,sr) )
+                  vst = 0.5 * ( v(k,j,i)   - hom(k,1,2,sr) + &
+                              v(k+1,j,i) - hom(k+1,1,2,sr) )
+!                              
+!--               Momentum flux w*u*
+                  sums_l(k,13,tn) = sums_l(k,13,tn) + 0.5 *                   &
+                                                    ( w(k,j,i-1) + w(k,j,i) ) &
+                                                    * ust * rmask(j,i,sr)
+!
+!--               Momentum flux w*v*
+                  sums_l(k,15,tn) = sums_l(k,15,tn) + 0.5 *                   &
+                                                    ( w(k,j-1,i) + w(k,j,i) ) &
+                                                    * vst * rmask(j,i,sr)
+               ENDDO
+            ENDDO
+         ENDDO
+
+       ENDIF
+       IF ( .NOT. ws_scheme_sca )  THEN
+         !$OMP DO
+         DO  i = nxl, nxr
+            DO  j = nys, nyn
+               DO  k = nzb_diff_s_inner(j,i) - 1, nzt_diff
+!-                vertical heat flux
+                  sums_l(k,17,tn) = sums_l(k,17,tn) +  0.5 * &
+                           ( pt(k,j,i)   - hom(k,1,4,sr) + &
+                           pt(k+1,j,i) - hom(k+1,1,4,sr) ) &
+                           * w(k,j,i) * rmask(j,i,sr)
+                  IF ( humidity )  THEN
+                     pts = 0.5 * ( q(k,j,i)   - hom(k,1,41,sr) + &
+                                q(k+1,j,i) - hom(k+1,1,41,sr) )
+                     sums_l(k,49,tn) = sums_l(k,49,tn) + pts * w(k,j,i) * &
+                                                      rmask(j,i,sr)
+                  ENDIF
+               ENDDO
+            ENDDO
+         ENDDO
+
+       ENDIF
+
 
 !
@@ -814,4 +923,5 @@
 
 #if defined( __parallel )
+
 !
 !--    Compute total sum from local sums
@@ -843,4 +953,5 @@
           sums(k,70:pr_palm-2)    = sums(k,70:pr_palm-2)/ ngp_2dh_s_inner(k,sr)
        ENDDO
+
 !--    Upstream-parts
        sums(nzb:nzb+11,pr_palm-1) = sums(nzb:nzb+11,pr_palm-1) / ngp_3d(sr)
@@ -868,5 +979,4 @@
           ENDDO
        ENDIF
-
 !
 !--    Collect horizontal average in hom.
@@ -934,5 +1044,5 @@
                                        ! upstream-parts u_x, u_y, u_z, v_x,
                                        ! v_y, usw. (in last but one profile)
-       hom(:,1,pr_palm,sr) =   sums(:,pr_palm)  
+       hom(:,1,pr_palm,sr) =   sums(:,pr_palm)
                                        ! u*, w'u', w'v', t* (in last profile)
 
@@ -951,11 +1061,14 @@
        z_i(1) = 0.0
        first = .TRUE.
+
        IF ( ocean )  THEN
           DO  k = nzt, nzb+1, -1
-             IF ( first .AND. hom(k,1,18,sr) < 0.0 )  THEN
+             IF ( first .AND. hom(k,1,18,sr) < 0.0 &
+                .AND. abs(hom(k,1,18,sr)) > 1.0E-8)  THEN
                 first = .FALSE.
                 height = zw(k)
              ENDIF
              IF ( hom(k,1,18,sr) < 0.0  .AND. &
+                  abs(hom(k,1,18,sr)) > 1.0E-8 .AND. &
                   hom(k-1,1,18,sr) > hom(k,1,18,sr) )  THEN
                 IF ( zw(k) < 1.5 * height )  THEN
@@ -969,9 +1082,11 @@
        ELSE
           DO  k = nzb, nzt-1
-             IF ( first .AND. hom(k,1,18,sr) < 0.0 )  THEN
+             IF ( first .AND. hom(k,1,18,sr) < 0.0 &
+                .AND. abs(hom(k,1,18,sr)) > 1.0E-8 )  THEN
                 first = .FALSE.
                 height = zw(k)
              ENDIF
-             IF ( hom(k,1,18,sr) < 0.0  .AND. &
+             IF ( hom(k,1,18,sr) < 0.0  .AND. & 
+                  abs(hom(k,1,18,sr)) > 1.0E-8 .AND. &
                   hom(k+1,1,18,sr) > hom(k,1,18,sr) )  THEN
                 IF ( zw(k) < 1.5 * height )  THEN
@@ -1026,5 +1141,6 @@
 !--    the characteristic convective boundary layer temperature.
 !--    The horizontal average at nzb+1 is input for the average temperature.
-       IF ( hom(nzb,1,18,sr) > 0.0  .AND.  z_i(1) /= 0.0 )  THEN
+       IF ( hom(nzb,1,18,sr) > 0.0 .AND. abs(hom(nzb,1,18,sr)) > 1.0E-8 &
+           .AND.  z_i(1) /= 0.0 )  THEN
           hom(nzb+8,1,pr_palm,sr)  = ( g / hom(nzb+1,1,4,sr) * &
                                        hom(nzb,1,18,sr) *      &
Index: /palm/trunk/SOURCE/global_min_max.f90
===================================================================
--- /palm/trunk/SOURCE/global_min_max.f90	(revision 666)
+++ /palm/trunk/SOURCE/global_min_max.f90	(revision 667)
@@ -5,5 +5,6 @@
 ! Current revisions:
 ! -----------------
-! 
+! Adapting of the index arrays, because MINLOC assumes lowerbound at 1 and not 
+! at nbgp.
 !
 ! Former revisions:
@@ -59,6 +60,6 @@
 !--    Determine the local minimum
        fmin_ijk_l = MINLOC( ar )
-       fmin_ijk_l(1) = i1 + fmin_ijk_l(1) - 1    ! MINLOC assumes lowerbound = 1
-       fmin_ijk_l(2) = j1 + fmin_ijk_l(2) - 1
+       fmin_ijk_l(1) = i1 + fmin_ijk_l(1) - nbgp    ! MINLOC assumes lowerbound = 1
+       fmin_ijk_l(2) = j1 + fmin_ijk_l(2) - nbgp
        fmin_ijk_l(3) = k1 + fmin_ijk_l(3) - 1
        fmin_l(1)  = ar(fmin_ijk_l(1),fmin_ijk_l(2),fmin_ijk_l(3))
@@ -100,6 +101,6 @@
 !--    Determine the local maximum
        fmax_ijk_l = MAXLOC( ar )
-       fmax_ijk_l(1) = i1 + fmax_ijk_l(1) - 1    ! MAXLOC assumes lowerbound = 1
-       fmax_ijk_l(2) = j1 + fmax_ijk_l(2) - 1
+       fmax_ijk_l(1) = i1 + fmax_ijk_l(1) - nbgp    ! MAXLOC assumes lowerbound = 1
+       fmax_ijk_l(2) = j1 + fmax_ijk_l(2) - nbgp
        fmax_ijk_l(3) = k1 + fmax_ijk_l(3) - 1
        fmax_l(1) = ar(fmax_ijk_l(1),fmax_ijk_l(2),fmax_ijk_l(3))
@@ -221,5 +222,5 @@
           IF ( fmax_ijk(1) < 0 )  THEN
              value        = -value
-             value_ijk(1) = -value_ijk(1) - 10
+             value_ijk(1) = -value_ijk(1) - 10         !???
           ENDIF
 
@@ -228,8 +229,8 @@
 !
 !-- Limit index values to the range 0..nx, 0..ny
-    IF ( value_ijk(3) ==   -1 )  value_ijk(3) = nx
-    IF ( value_ijk(3) == nx+1 )  value_ijk(3) =  0
-    IF ( value_ijk(2) ==   -1 )  value_ijk(2) = ny
-    IF ( value_ijk(2) == ny+1 )  value_ijk(2) =  0
+    IF ( value_ijk(3) < 0  ) value_ijk(3) = nx +1 + value_ijk(3)
+    IF ( value_ijk(3) > nx ) value_ijk(3) = value_ijk(3) - (nx+1)
+    IF ( value_ijk(2) < 0  ) value_ijk(2) = ny +1 + value_ijk(2)
+    IF ( value_ijk(2) > ny ) value_ijk(2) = value_ijk(2) - (ny+1)
 
 
Index: /palm/trunk/SOURCE/header.f90
===================================================================
--- /palm/trunk/SOURCE/header.f90	(revision 666)
+++ /palm/trunk/SOURCE/header.f90	(revision 667)
@@ -4,5 +4,6 @@
 ! Current revisions:
 ! -----------------
-! 
+! Output of advection scheme.
+! Modified output of Prandtl-layer height.
 ! 
 ! Former revisions:
@@ -250,5 +251,7 @@
     IF ( momentum_advec == 'pw-scheme' )  THEN
        WRITE ( io, 113 )
-    ELSE
+    ELSEIF (momentum_advec == 'ws-scheme' ) THEN
+       WRITE ( io, 503 )
+    ELSEIF (momentum_advec == 'ups-scheme' ) THEN
        WRITE ( io, 114 )
        IF ( cut_spline_overshoot )  WRITE ( io, 124 )
@@ -267,4 +270,6 @@
     IF ( scalar_advec == 'pw-scheme' )  THEN
        WRITE ( io, 116 )
+    ELSEIF ( scalar_advec == 'ws-scheme' )  THEN
+       WRITE ( io, 504 )
     ELSEIF ( scalar_advec == 'ups-scheme' )  THEN
        WRITE ( io, 117 )
@@ -575,4 +580,5 @@
     ELSEIF( ibc_pt_t == 2 )  THEN
        roben  = TRIM( roben  ) // ' pt(nzt+1) = pt(nzt) + dpt/dz_ini'
+
     ENDIF
 
@@ -662,5 +668,5 @@
 
     IF ( prandtl_layer )  THEN
-       WRITE ( io, 305 )  0.5 * (zu(1)-zu(0)), roughness_length, kappa, &
+       WRITE ( io, 305 )  (zu(1)-zu(0)), roughness_length, kappa, &
                           rif_min, rif_max
        IF ( .NOT. constant_heatflux )  WRITE ( io, 308 )
@@ -1981,4 +1987,6 @@
             '    Dissipation calculation:           ',A/)
 502 FORMAT ('    Damping layer starts from ',F7.1,' m (GP ',I4,')'/)
+503 FORMAT (' --> Momentum advection via Wicker-Skamarock-Scheme 5th order')
+504 FORMAT (' --> Scalar advection via Wicker-Skamarock-Scheme 5th order')
 
 
Index: /palm/trunk/SOURCE/inflow_turbulence.f90
===================================================================
--- /palm/trunk/SOURCE/inflow_turbulence.f90	(revision 666)
+++ /palm/trunk/SOURCE/inflow_turbulence.f90	(revision 667)
@@ -4,5 +4,6 @@
 ! Current revisions:
 ! -----------------
-! 
+! Using nbgp recycling planes for a better resolution of the turbulent flow 
+! near the inflow.
 !
 ! Former revisions:
@@ -35,64 +36,68 @@
     IMPLICIT NONE
 
-    INTEGER ::  i, imax, j, k, ngp_ifd, ngp_pr
+    INTEGER ::  i, imax, j, k, l, ngp_ifd, ngp_pr
 
     REAL, DIMENSION(1:2) ::  volume_flow_l, volume_flow_offset
-    REAL, DIMENSION(nzb:nzt+1,5) ::  avpr, avpr_l
-    REAL, DIMENSION(nzb:nzt+1,nys-1:nyn+1,5) ::  inflow_dist
+    REAL, DIMENSION(nzb:nzt+1,5,nbgp) ::  avpr, avpr_l
+    REAL, DIMENSION(nzb:nzt+1,nysg:nyng,5,nbgp) ::  inflow_dist
 
     CALL cpu_log( log_point(40), 'inflow_turbulence', 'start' )
 
 !
-!-- Carry out horizontal averaging in the recycling plane
+!-- Carry out spanwise averaging in the recycling plane
     avpr_l = 0.0
-    ngp_pr = ( nzt - nzb + 2 ) * 5
-    ngp_ifd = ngp_pr * ( nyn - nys + 3 )
+    ngp_pr = ( nzt - nzb + 2 ) * 5 * nbgp
+    ngp_ifd = ngp_pr * ( nyn - nys + 1 + 2 * nbgp )
 
 !
 !-- First, local averaging within the recycling domain
-    IF ( recycling_plane >= nxl )  THEN
-
-       imax = MIN( nxr, recycling_plane )
-
-       DO  i = nxl, imax
+
+    i = recycling_plane
+
+#if defined( __parallel )
+    IF ( myidx == id_recycling )  THEN
+       
+       DO  l = 1, nbgp
           DO  j = nys, nyn
-             DO  k = nzb, nzt+1
-
-                avpr_l(k,1) = avpr_l(k,1) + u(k,j,i)
-                avpr_l(k,2) = avpr_l(k,2) + v(k,j,i)
-                avpr_l(k,3) = avpr_l(k,3) + w(k,j,i)
-                avpr_l(k,4) = avpr_l(k,4) + pt(k,j,i)
-                avpr_l(k,5) = avpr_l(k,5) + e(k,j,i)
+             DO  k = nzb, nzt + 1
+
+                avpr_l(k,1,l) = avpr_l(k,1,l) + u(k,j,i)
+                avpr_l(k,2,l) = avpr_l(k,2,l) + v(k,j,i)
+                avpr_l(k,3,l) = avpr_l(k,3,l) + w(k,j,i)
+                avpr_l(k,4,l) = avpr_l(k,4,l) + pt(k,j,i)
+                avpr_l(k,5,l) = avpr_l(k,5,l) + e(k,j,i)
 
              ENDDO
           ENDDO
-       ENDDO
-
-    ENDIF
-
-!    WRITE (9,*) '*** averaged profiles avpr_l'
-!    DO  k = nzb, nzt+1
-!       WRITE (9,'(F5.1,1X,F5.1,1X,F5.1,1X,F6.1,1X,F7.2)') avpr_l(k,1),avpr_l(k,2),avpr_l(k,3),avpr_l(k,4),avpr_l(k,5)
-!    ENDDO
-!    WRITE (9,*) ' '
-
-#if defined( __parallel )
+          i = i + 1
+       ENDDO
+
+    ENDIF
 !
 !-- Now, averaging over all PEs
     IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-    CALL MPI_ALLREDUCE( avpr_l(nzb,1), avpr(nzb,1), ngp_pr, MPI_REAL, MPI_SUM, &
-                        comm2d, ierr )
+    CALL MPI_ALLREDUCE( avpr_l(nzb,1,1), avpr(nzb,1,1), ngp_pr,  & 
+                    MPI_REAL, MPI_SUM, comm2d, ierr )
+
 #else
+    DO  l = 1, nbgp
+       DO  j = nys, nyn
+          DO  k = nzb, nzt + 1
+
+             avpr_l(k,1,l) = avpr_l(k,1,l) + u(k,j,i)
+             avpr_l(k,2,l) = avpr_l(k,2,l) + v(k,j,i)
+             avpr_l(k,3,l) = avpr_l(k,3,l) + w(k,j,i)
+             avpr_l(k,4,l) = avpr_l(k,4,l) + pt(k,j,i)
+             avpr_l(k,5,l) = avpr_l(k,5,l) + e(k,j,i)
+
+          ENDDO
+       ENDDO
+       i = i + 1 
+    ENDDO
+    
     avpr = avpr_l
 #endif
 
-    avpr = avpr / ( ( ny + 1 ) * ( recycling_plane + 1 ) )
-
-!    WRITE (9,*) '*** averaged profiles'
-!    DO  k = nzb, nzt+1
-!       WRITE (9,'(F5.1,1X,F5.1,1X,F5.1,1X,F6.1,1X,F7.2)') avpr(k,1),avpr(k,2),avpr(k,3),avpr(k,4),avpr(k,5)
-!    ENDDO
-!    WRITE (9,*) ' '
-
+    avpr = avpr / ( ny + 1 )
 !
 !-- Calculate the disturbances at the recycling plane
@@ -101,29 +106,34 @@
 #if defined( __parallel )
     IF ( myidx == id_recycling )  THEN
-
-       DO  j = nys-1, nyn+1
+       DO  l = 1, nbgp
+          DO  j = nysg, nyng
+             DO  k = nzb, nzt + 1
+
+                inflow_dist(k,j,1,l) = u(k,j,i+1) - avpr(k,1,l)
+                inflow_dist(k,j,2,l) = v(k,j,i)   - avpr(k,2,l)
+                inflow_dist(k,j,3,l) = w(k,j,i)   - avpr(k,3,l)
+                inflow_dist(k,j,4,l) = pt(k,j,i)  - avpr(k,4,l)
+                inflow_dist(k,j,5,l) = e(k,j,i)   - avpr(k,5,l)
+              
+            ENDDO
+          ENDDO
+          i = i + 1
+       ENDDO
+
+    ENDIF
+#else
+    DO  l = 1, nbgp
+       DO  j = nysg, nyng
           DO  k = nzb, nzt+1
 
-              inflow_dist(k,j,1) = u(k,j,i+1) - avpr(k,1)
-              inflow_dist(k,j,2) = v(k,j,i)   - avpr(k,2)
-              inflow_dist(k,j,3) = w(k,j,i)   - avpr(k,3)
-              inflow_dist(k,j,4) = pt(k,j,i)  - avpr(k,4)
-              inflow_dist(k,j,5) = e(k,j,i)   - avpr(k,5)
-
-          ENDDO
-       ENDDO
-
-    ENDIF
-#else
-    DO  j = nys-1, nyn+1
-       DO  k = nzb, nzt+1
-
-          inflow_dist(k,j,1) = u(k,j,i+1) - avpr(k,1)
-          inflow_dist(k,j,2) = v(k,j,i)   - avpr(k,2)
-          inflow_dist(k,j,3) = w(k,j,i)   - avpr(k,3)
-          inflow_dist(k,j,4) = pt(k,j,i)  - avpr(k,4)
-          inflow_dist(k,j,5) = e(k,j,i)   - avpr(k,5)
-
-       ENDDO
+             inflow_dist(k,j,1,l) = u(k,j,i+1) - avpr(k,1,l)
+             inflow_dist(k,j,2,l) = v(k,j,i)   - avpr(k,2,l)
+             inflow_dist(k,j,3,l) = w(k,j,i)   - avpr(k,3,l)
+             inflow_dist(k,j,4,l) = pt(k,j,i)  - avpr(k,4,l)
+             inflow_dist(k,j,5,l) = e(k,j,i)   - avpr(k,5,l)
+              
+          ENDDO
+       ENDDO
+       i = i + 1
     ENDDO
 #endif
@@ -134,5 +144,5 @@
     IF ( myidx == id_recycling  .AND.  myidx /= id_inflow )  THEN
 
-       CALL MPI_SEND( inflow_dist(nzb,nys-1,1), ngp_ifd, MPI_REAL, &
+       CALL MPI_SEND( inflow_dist(nzb,nysg,1,1), ngp_ifd, MPI_REAL, &
                       id_inflow, 1, comm1dx, ierr )
 
@@ -140,5 +150,5 @@
 
        inflow_dist = 0.0
-       CALL MPI_RECV( inflow_dist(nzb,nys-1,1), ngp_ifd, MPI_REAL, &
+       CALL MPI_RECV( inflow_dist(nzb,nysg,1,1), ngp_ifd, MPI_REAL, &
                       id_recycling, 1, comm1dx, status, ierr )
 
@@ -150,28 +160,17 @@
     IF ( nxl == 0 )  THEN
 
-       DO  j = nys-1, nyn+1
-          DO  k = nzb, nzt+1
-
-!              WRITE (9,*) 'j=',j,' k=',k
-!              WRITE (9,*) 'mean_u = ', mean_inflow_profiles(k,1), ' dist_u = ',&
-!                          inflow_dist(k,j,1)
-!              WRITE (9,*) 'mean_v = ', mean_inflow_profiles(k,2), ' dist_v = ',&
-!                          inflow_dist(k,j,2)
-!              WRITE (9,*) 'mean_w = 0.0', ' dist_w = ',&
-!                          inflow_dist(k,j,3)
-!              WRITE (9,*) 'mean_pt = ', mean_inflow_profiles(k,4), ' dist_pt = ',&
-!                          inflow_dist(k,j,4)
-!              WRITE (9,*) 'mean_e = ', mean_inflow_profiles(k,5), ' dist_e = ',&
-!                          inflow_dist(k,j,5)
-              u(k,j,0)   = mean_inflow_profiles(k,1) + &
-                           inflow_dist(k,j,1) * inflow_damping_factor(k)
-              v(k,j,-1)  = mean_inflow_profiles(k,2) + &
-                           inflow_dist(k,j,2) * inflow_damping_factor(k)
-              w(k,j,-1)  = inflow_dist(k,j,3) * inflow_damping_factor(k)
-              pt(k,j,-1) = mean_inflow_profiles(k,4) + &
-                           inflow_dist(k,j,4) * inflow_damping_factor(k)
-              e(k,j,-1)  = mean_inflow_profiles(k,5) + &
-                           inflow_dist(k,j,5) * inflow_damping_factor(k)
-              e(k,j,-1)  = MAX( e(k,j,-1), 0.0 )
+       DO  j = nysg, nyng
+          DO  k = nzb, nzt + 1
+
+              u(k,j,-nbgp+1:0)   = mean_inflow_profiles(k,1) + &
+                           inflow_dist(k,j,1,1:nbgp) * inflow_damping_factor(k)
+              v(k,j,-nbgp:-1)  = mean_inflow_profiles(k,2) + &
+                           inflow_dist(k,j,2,1:nbgp) * inflow_damping_factor(k)
+              w(k,j,-nbgp:-1)  = inflow_dist(k,j,3,1:nbgp) * inflow_damping_factor(k)
+              pt(k,j,-nbgp:-1) = mean_inflow_profiles(k,4) + &
+                           inflow_dist(k,j,4,1:nbgp) * inflow_damping_factor(k)
+              e(k,j,-nbgp:-1)  = mean_inflow_profiles(k,5) + &
+                           inflow_dist(k,j,5,1:nbgp) * inflow_damping_factor(k)
+              e(k,j,-nbgp:-1)  = MAX( e(k,j,-nbgp:-1), 0.0 )
 
           ENDDO
Index: /palm/trunk/SOURCE/init_1d_model.f90
===================================================================
--- /palm/trunk/SOURCE/init_1d_model.f90	(revision 666)
+++ /palm/trunk/SOURCE/init_1d_model.f90	(revision 667)
@@ -63,5 +63,5 @@
               l1d_m(nzb:nzt+1),  rif1d(nzb:nzt+1),   te_e(nzb:nzt+1),  &
               te_em(nzb:nzt+1),  te_u(nzb:nzt+1),    te_um(nzb:nzt+1), &
-              te_v(nzb:nzt+1),   te_vm(nzb:nzt+1),   u1d(nzb:nzt+1),   &
+              te_v(nzb:nzt+1),   te_vm(nzb:nzt+1),    u1d(nzb:nzt+1),   &
               u1d_m(nzb:nzt+1),  u1d_p(nzb:nzt+1),   v1d(nzb:nzt+1),   &
               v1d_m(nzb:nzt+1),  v1d_p(nzb:nzt+1) )
@@ -385,6 +385,9 @@
 !--       boundary condition applies to u and v.
 !--       The boundary condition for e is set further below ( (u*/cm)**2 ).
-          u1d_p(nzb) = -u1d_p(nzb+1)
-          v1d_p(nzb) = -v1d_p(nzb+1)
+         ! u1d_p(nzb) = -u1d_p(nzb+1)
+         ! v1d_p(nzb) = -v1d_p(nzb+1)
+
+          u1d_p(nzb) = 0.0
+          v1d_p(nzb) = 0.0
 
 !
Index: /palm/trunk/SOURCE/init_3d_model.f90
===================================================================
--- /palm/trunk/SOURCE/init_3d_model.f90	(revision 666)
+++ /palm/trunk/SOURCE/init_3d_model.f90	(revision 667)
@@ -7,5 +7,19 @@
 ! Current revisions:
 ! -----------------
-! Bugfix: type conversion of '1' to 64bit for the MAX function (ngp_3d_inner)
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng in loops and 
+! allocation of arrays. Calls of exchange_horiz are modified.
+! Call ws_init to initialize arrays needed for statistical evaluation and 
+! optimization when ws-scheme is used.
+!
+! 
+! Initial volume flow is now calculated by using the variable hom_sum.
+! Therefore the correction of initial volume flow for non-flat topography
+! removed (removed u_nzb_p1_for_vfc and v_nzb_p1_for_vfc) 
+!
+! Changed surface boundary conditions for u and v in case of ibc_uv_b == 0 from
+! mirror bc to dirichlet boundary conditions (u=v=0), so that k=nzb is 
+! representative for the height z0 
+!
+! Bugfix: type conversion of '1' to 64bit for the MAX function (ngp_3d_inner) 
 !
 ! Former revisions:
@@ -101,4 +115,5 @@
 !------------------------------------------------------------------------------!
 
+    USE advec_ws
     USE arrays_3d
     USE averaging
@@ -147,5 +162,5 @@
               ngp_2dh_s_inner(nzb:nzt+1,0:statistic_regions),               &
               ngp_2dh_s_inner_l(nzb:nzt+1,0:statistic_regions),             &
-              rmask(nys-1:nyn+1,nxl-1:nxr+1,0:statistic_regions),           &
+              rmask(nysg:nyng,nxlg:nxrg,0:statistic_regions),           &
               sums(nzb:nzt+1,pr_palm+max_pr_user),                          &
               sums_l(nzb:nzt+1,pr_palm+max_pr_user,0:threads_per_task-1),   &
@@ -154,49 +169,49 @@
               sums_wsts_bc_l(nzb:nzt+1,0:statistic_regions),                &
               ts_value(dots_max,0:statistic_regions) )
-    ALLOCATE( km_damp_x(nxl-1:nxr+1), km_damp_y(nys-1:nyn+1) )
-
-    ALLOCATE( rif_1(nys-1:nyn+1,nxl-1:nxr+1), shf_1(nys-1:nyn+1,nxl-1:nxr+1), &
-              ts(nys-1:nyn+1,nxl-1:nxr+1), tswst_1(nys-1:nyn+1,nxl-1:nxr+1),  &
-              us(nys-1:nyn+1,nxl-1:nxr+1), usws_1(nys-1:nyn+1,nxl-1:nxr+1),   &
-              uswst_1(nys-1:nyn+1,nxl-1:nxr+1),                               &
-              vsws_1(nys-1:nyn+1,nxl-1:nxr+1),                                &
-              vswst_1(nys-1:nyn+1,nxl-1:nxr+1), z0(nys-1:nyn+1,nxl-1:nxr+1) )
+    ALLOCATE( km_damp_x(nxlg:nxrg), km_damp_y(nysg:nyng) )
+
+    ALLOCATE( rif_1(nysg:nyng,nxlg:nxrg), shf_1(nysg:nyng,nxlg:nxrg), &
+              ts(nysg:nyng,nxlg:nxrg), tswst_1(nysg:nyng,nxlg:nxrg),  &
+              us(nysg:nyng,nxlg:nxrg), usws_1(nysg:nyng,nxlg:nxrg),   &
+              uswst_1(nysg:nyng,nxlg:nxrg),                               &
+              vsws_1(nysg:nyng,nxlg:nxrg),                                &
+              vswst_1(nysg:nyng,nxlg:nxrg), z0(nysg:nyng,nxlg:nxrg) )
 
     IF ( timestep_scheme(1:5) /= 'runge' )  THEN
 !
 !--    Leapfrog scheme needs two timelevels of diffusion quantities
-       ALLOCATE( rif_2(nys-1:nyn+1,nxl-1:nxr+1),   &
-                 shf_2(nys-1:nyn+1,nxl-1:nxr+1),   &
-                 tswst_2(nys-1:nyn+1,nxl-1:nxr+1), &
-                 usws_2(nys-1:nyn+1,nxl-1:nxr+1),  &
-                 uswst_2(nys-1:nyn+1,nxl-1:nxr+1), &
-                 vswst_2(nys-1:nyn+1,nxl-1:nxr+1), &
-                 vsws_2(nys-1:nyn+1,nxl-1:nxr+1) )
+       ALLOCATE( rif_2(nysg:nyng,nxlg:nxrg),   &
+                 shf_2(nysg:nyng,nxlg:nxrg),   &
+                 tswst_2(nysg:nyng,nxlg:nxrg), &
+                 usws_2(nysg:nyng,nxlg:nxrg),  &
+                 uswst_2(nysg:nyng,nxlg:nxrg), &
+                 vswst_2(nysg:nyng,nxlg:nxrg), &
+                 vsws_2(nysg:nyng,nxlg:nxrg) )
     ENDIF
 
     ALLOCATE( d(nzb+1:nzta,nys:nyna,nxl:nxra),         &
-              e_1(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),  &
-              e_2(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),  &
-              e_3(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),  &
-              kh_1(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
-              km_1(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
-              p(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),    &
-              pt_1(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
-              pt_2(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
-              pt_3(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
-              tend(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
-              u_1(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),  &
-              u_2(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),  &
-              u_3(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),  &
-              v_1(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
-              v_2(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
-              v_3(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
-              w_1(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),  &
-              w_2(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),  &
-              w_3(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+              e_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),  &
+              e_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),  &
+              e_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg),  &
+              kh_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+              km_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+              p(nzb:nzt+1,nysg:nyng,nxlg:nxrg),    &
+              pt_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+              pt_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+              pt_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+              tend(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+              u_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),  &
+              u_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),  &
+              u_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg),  &
+              v_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+              v_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+              v_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+              w_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),  &
+              w_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),  &
+              w_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
 
     IF ( timestep_scheme(1:5) /= 'runge' )  THEN
-       ALLOCATE( kh_2(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
-                 km_2(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+       ALLOCATE( kh_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+                 km_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
     ENDIF
 
@@ -204,25 +219,25 @@
 !
 !--    2D-humidity/scalar arrays
-       ALLOCATE ( qs(nys-1:nyn+1,nxl-1:nxr+1),     &
-                  qsws_1(nys-1:nyn+1,nxl-1:nxr+1), &
-                  qswst_1(nys-1:nyn+1,nxl-1:nxr+1) )
+       ALLOCATE ( qs(nysg:nyng,nxlg:nxrg),     &
+                  qsws_1(nysg:nyng,nxlg:nxrg), &
+                  qswst_1(nysg:nyng,nxlg:nxrg) )
 
        IF ( timestep_scheme(1:5) /= 'runge' )  THEN
-          ALLOCATE( qsws_2(nys-1:nyn+1,nxl-1:nxr+1), &
-                    qswst_2(nys-1:nyn+1,nxl-1:nxr+1) )
+          ALLOCATE( qsws_2(nysg:nyng,nxlg:nxrg), &
+                    qswst_2(nysg:nyng,nxlg:nxrg) )
        ENDIF
 !
 !--    3D-humidity/scalar arrays
-       ALLOCATE( q_1(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
-                 q_2(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
-                 q_3(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+       ALLOCATE( q_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+                 q_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+                 q_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
 
 !
 !--    3D-arrays needed for humidity only
        IF ( humidity )  THEN
-          ALLOCATE( vpt_1(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+          ALLOCATE( vpt_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
 
           IF ( timestep_scheme(1:5) /= 'runge' )  THEN
-             ALLOCATE( vpt_2(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+             ALLOCATE( vpt_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
           ENDIF
 
@@ -230,9 +245,9 @@
 !
 !--          Liquid water content
-             ALLOCATE ( ql_1(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+             ALLOCATE ( ql_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
 !
 !--          Precipitation amount and rate (only needed if output is switched)
-             ALLOCATE( precipitation_amount(nys-1:nyn+1,nxl-1:nxr+1), &
-                       precipitation_rate(nys-1:nyn+1,nxl-1:nxr+1) )
+             ALLOCATE( precipitation_amount(nysg:nyng,nxlg:nxrg), &
+                       precipitation_rate(nysg:nyng,nxlg:nxrg) )
           ENDIF
 
@@ -241,8 +256,8 @@
 !--          Liquid water content, change in liquid water content,
 !--          real volume of particles (with weighting), volume of particles
-             ALLOCATE ( ql_1(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
-                        ql_2(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
-                        ql_v(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
-                        ql_vp(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+             ALLOCATE ( ql_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+                        ql_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+                        ql_v(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+                        ql_vp(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
           ENDIF
 
@@ -252,16 +267,16 @@
 
     IF ( ocean )  THEN
-       ALLOCATE( saswsb_1(nys-1:nyn+1,nxl-1:nxr+1), &
-                 saswst_1(nys-1:nyn+1,nxl-1:nxr+1) )
-       ALLOCATE( prho_1(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
-                 rho_1(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),  &
-                 sa_1(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),   &
-                 sa_2(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),   &
-                 sa_3(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+       ALLOCATE( saswsb_1(nysg:nyng,nxlg:nxrg), &
+                 saswst_1(nysg:nyng,nxlg:nxrg) )
+       ALLOCATE( prho_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+                 rho_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),  &
+                 sa_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),   &
+                 sa_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),   &
+                 sa_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
        prho => prho_1
        rho  => rho_1  ! routines calc_mean_profile and diffusion_e require
                       ! density to be apointer
        IF ( humidity_remote )  THEN
-          ALLOCATE( qswst_remote(nys-1:nyn+1,nxl-1:nxr+1) )
+          ALLOCATE( qswst_remote(nysg:nyng,nxlg:nxrg))
           qswst_remote = 0.0
        ENDIF
@@ -272,5 +287,5 @@
 !-- particle velocities
     IF ( use_sgs_for_particles )  THEN
-       ALLOCATE ( diss(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+       ALLOCATE ( diss(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
     ELSE
        ALLOCATE ( diss(2,2,2) )  ! required because diss is used as a
@@ -288,18 +303,18 @@
 !-- 3D-arrays for the leaf area density and the canopy drag coefficient
     IF ( plant_canopy ) THEN
-       ALLOCATE ( lad_s(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),  &
-                  lad_u(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),  &
-                  lad_v(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),  &
-                  lad_w(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),  &
-                  cdc(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+       ALLOCATE ( lad_s(nzb:nzt+1,nysg:nyng,nxlg:nxrg),  &
+                  lad_u(nzb:nzt+1,nysg:nyng,nxlg:nxrg),  &
+                  lad_v(nzb:nzt+1,nysg:nyng,nxlg:nxrg),  &
+                  lad_w(nzb:nzt+1,nysg:nyng,nxlg:nxrg),  &
+                  cdc(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
 
        IF ( passive_scalar ) THEN
-          ALLOCATE ( sls(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),   &
-                     sec(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) ) 
+          ALLOCATE ( sls(nzb:nzt+1,nysg:nyng,nxlg:nxrg),   &
+                     sec(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ) 
        ENDIF
 
        IF ( cthf /= 0.0 ) THEN
-          ALLOCATE ( lai(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),   &
-                     canopy_heat_flux(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+          ALLOCATE ( lai(nzb:nzt+1,nysg:nyng,nxlg:nxrg),   &
+                     canopy_heat_flux(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
        ENDIF
 
@@ -309,14 +324,6 @@
 !-- 4D-array for storing the Rif-values at vertical walls
     IF ( topography /= 'flat' )  THEN
-       ALLOCATE( rif_wall(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1,1:4) )
+       ALLOCATE( rif_wall(nzb:nzt+1,nysg:nyng,nxlg:nxrg,1:4) )
        rif_wall = 0.0
-    ENDIF
-
-!
-!-- Velocities at nzb+1 needed for volume flow control
-    IF ( conserve_volume_flow )  THEN
-       ALLOCATE( u_nzb_p1_for_vfc(nys:nyn), v_nzb_p1_for_vfc(nxl:nxr) )
-       u_nzb_p1_for_vfc = 0.0
-       v_nzb_p1_for_vfc = 0.0
     ENDIF
 
@@ -325,30 +332,30 @@
 !-- are needed for radiation boundary conditions
     IF ( outflow_l )  THEN
-       ALLOCATE( u_m_l(nzb:nzt+1,nys-1:nyn+1,1:2), &
-                 v_m_l(nzb:nzt+1,nys-1:nyn+1,0:1), &
-                 w_m_l(nzb:nzt+1,nys-1:nyn+1,0:1) )
+       ALLOCATE( u_m_l(nzb:nzt+1,nysg:nyng,1:2), &
+                 v_m_l(nzb:nzt+1,nysg:nyng,0:1), &
+                 w_m_l(nzb:nzt+1,nysg:nyng,0:1) )
     ENDIF
     IF ( outflow_r )  THEN
-       ALLOCATE( u_m_r(nzb:nzt+1,nys-1:nyn+1,nx-1:nx), &
-                 v_m_r(nzb:nzt+1,nys-1:nyn+1,nx-1:nx), &
-                 w_m_r(nzb:nzt+1,nys-1:nyn+1,nx-1:nx) )
+       ALLOCATE( u_m_r(nzb:nzt+1,nysg:nyng,nx-1:nx), &
+                 v_m_r(nzb:nzt+1,nysg:nyng,nx-1:nx), &
+                 w_m_r(nzb:nzt+1,nysg:nyng,nx-1:nx) )
     ENDIF
     IF ( outflow_l  .OR.  outflow_r )  THEN
-       ALLOCATE( c_u(nzb:nzt+1,nys-1:nyn+1), c_v(nzb:nzt+1,nys-1:nyn+1), &
-                 c_w(nzb:nzt+1,nys-1:nyn+1) )
+       ALLOCATE( c_u(nzb:nzt+1,nysg:nyng), c_v(nzb:nzt+1,nysg:nyng), &
+                 c_w(nzb:nzt+1,nysg:nyng) )
     ENDIF
     IF ( outflow_s )  THEN
-       ALLOCATE( u_m_s(nzb:nzt+1,0:1,nxl-1:nxr+1), &
-                 v_m_s(nzb:nzt+1,1:2,nxl-1:nxr+1), &
-                 w_m_s(nzb:nzt+1,0:1,nxl-1:nxr+1) )
+       ALLOCATE( u_m_s(nzb:nzt+1,0:1,nxlg:nxrg), &
+                 v_m_s(nzb:nzt+1,1:2,nxlg:nxrg), &
+                 w_m_s(nzb:nzt+1,0:1,nxlg:nxrg) )
     ENDIF
     IF ( outflow_n )  THEN
-       ALLOCATE( u_m_n(nzb:nzt+1,ny-1:ny,nxl-1:nxr+1), &
-                 v_m_n(nzb:nzt+1,ny-1:ny,nxl-1:nxr+1), &
-                 w_m_n(nzb:nzt+1,ny-1:ny,nxl-1:nxr+1) )
+       ALLOCATE( u_m_n(nzb:nzt+1,ny-1:ny,nxlg:nxrg), &
+                 v_m_n(nzb:nzt+1,ny-1:ny,nxlg:nxrg), &
+                 w_m_n(nzb:nzt+1,ny-1:ny,nxlg:nxrg) )
     ENDIF
     IF ( outflow_s  .OR.  outflow_n )  THEN
-       ALLOCATE( c_u(nzb:nzt+1,nxl-1:nxr+1), c_v(nzb:nzt+1,nxl-1:nxr+1), &
-                 c_w(nzb:nzt+1,nxl-1:nxr+1) )
+       ALLOCATE( c_u(nzb:nzt+1,nxlg:nxrg), c_v(nzb:nzt+1,nxlg:nxrg), &
+                 c_w(nzb:nzt+1,nxlg:nxrg) )
     ENDIF
 
@@ -435,6 +442,6 @@
 !
 !--       Transfer initial profiles to the arrays of the 3D model
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
                 e(:,j,i)  = e1d
                 kh(:,j,i) = kh1d
@@ -447,6 +454,6 @@
 
           IF ( humidity  .OR.  passive_scalar )  THEN
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    q(:,j,i) = q_init
                 ENDDO
@@ -455,6 +462,6 @@
 
           IF ( .NOT. constant_diffusion )  THEN
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    e(:,j,i)  = e1d
                 ENDDO
@@ -505,22 +512,5 @@
                 ENDDO
              ENDDO
-             IF ( conserve_volume_flow )  THEN
-                IF ( nxr == nx )  THEN
-                   DO  j = nys, nyn
-                      DO  k = nzb + 1, nzb_u_inner(j,nx)
-                         u_nzb_p1_for_vfc(j) = u_nzb_p1_for_vfc(j) + &
-                                               u1d(k) * dzu(k)
-                      ENDDO
-                   ENDDO
-                ENDIF
-                IF ( nyn == ny )  THEN
-                   DO  i = nxl, nxr
-                      DO  k = nzb + 1, nzb_v_inner(ny,i)
-                         v_nzb_p1_for_vfc(i) = v_nzb_p1_for_vfc(i) + &
-                                               v1d(k) * dzu(k)
-                      ENDDO
-                   ENDDO
-                ENDIF
-             ENDIF
+             
 !
 !--          WARNING: The extra boundary conditions set after running the
@@ -530,16 +520,21 @@
 !--          ---------  advection scheme: keep u and v zero one layer below
 !--                     the topography.
-             IF ( ibc_uv_b == 0 )  THEN
-!
-!--             Satisfying the Dirichlet condition with an extra layer below
-!--             the surface where the u and v component change their sign.
-                DO  i = nxl-1, nxr+1
-                   DO  j = nys-1, nyn+1
-                      IF ( nzb_u_inner(j,i) == 0 ) u(0,j,i) = -u(1,j,i)
-                      IF ( nzb_v_inner(j,i) == 0 ) v(0,j,i) = -v(1,j,i)
-                   ENDDO
-                ENDDO
-
-             ELSE
+!
+!--           Following was removed, because mirror boundary condition are
+!--           replaced by dirichlet boundary conditions 
+!
+!             IF ( ibc_uv_b == 0 )  THEN
+!!
+!!--             Satisfying the Dirichlet condition with an extra layer below
+!!--             the surface where the u and v component change their sign.
+!                DO  i = nxl-1, nxr+1
+!                   DO  j = nys-1, nyn+1
+!                      IF ( nzb_u_inner(j,i) == 0 ) u(0,j,i) = -u(1,j,i)
+!                      IF ( nzb_v_inner(j,i) == 0 ) v(0,j,i) = -v(1,j,i)
+!                   ENDDO
+!                ENDDO
+!
+!             ELSE
+             IF ( ibc_uv_b == 1 )  THEN
 !
 !--             Neumann condition
@@ -560,6 +555,6 @@
 !--       Use constructed initial profiles (velocity constant with height,
 !--       temperature profile with constant gradient)
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
                 pt(:,j,i) = pt_init
                 u(:,j,i)  = u_init
@@ -574,32 +569,14 @@
 !--       in the limiting formula!). The original values are stored to be later
 !--       used for volume flow control.
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
                 u(nzb:nzb_u_inner(j,i)+1,j,i) = 0.0
                 v(nzb:nzb_v_inner(j,i)+1,j,i) = 0.0
              ENDDO
           ENDDO
-          IF ( conserve_volume_flow )  THEN
-             IF ( nxr == nx )  THEN
-                DO  j = nys, nyn
-                   DO  k = nzb + 1, nzb_u_inner(j,nx) + 1
-                      u_nzb_p1_for_vfc(j) = u_nzb_p1_for_vfc(j) + &
-                                            u_init(k) * dzu(k)
-                   ENDDO
-                ENDDO
-             ENDIF
-             IF ( nyn == ny )  THEN
-                DO  i = nxl, nxr
-                   DO  k = nzb + 1, nzb_v_inner(ny,i) + 1
-                      v_nzb_p1_for_vfc(i) = v_nzb_p1_for_vfc(i) + &
-                                            v_init(k) * dzu(k)
-                   ENDDO
-                ENDDO
-             ENDIF
-          ENDIF
 
           IF ( humidity  .OR.  passive_scalar )  THEN
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    q(:,j,i) = q_init
                 ENDDO
@@ -608,6 +585,6 @@
 
           IF ( ocean )  THEN
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    sa(:,j,i) = sa_init
                 ENDDO
@@ -660,4 +637,10 @@
 
        ENDIF
+!
+!--    Bottom boundary
+       IF ( ibc_uv_b == 0 .OR. ibc_uv_b == 2  )  THEN
+          u(nzb,:,:) = 0.0
+          v(nzb,:,:) = 0.0
+       ENDIF
 
 !
@@ -683,8 +666,7 @@
        hom(:,1,5,:) = SPREAD( u(:,nys,nxl), 2, statistic_regions+1 )
        hom(:,1,6,:) = SPREAD( v(:,nys,nxl), 2, statistic_regions+1 )
-       IF ( ibc_uv_b == 0 )  THEN
-          hom(nzb,1,5,:) = -hom(nzb+1,1,5,:)  ! due to satisfying the Dirichlet 
-          hom(nzb,1,6,:) = -hom(nzb+1,1,6,:)  ! condition with an extra layer 
-              ! below the surface where the u and v component change their sign
+       IF ( ibc_uv_b == 0 .OR. ibc_uv_b == 2)  THEN
+          hom(nzb,1,5,:) = 0.0   
+          hom(nzb,1,6,:) = 0.0  
        ENDIF
        hom(:,1,7,:)  = SPREAD( pt(:,nys,nxl), 2, statistic_regions+1 )
@@ -733,6 +715,6 @@
 !--             Over topography surface_heatflux is replaced by wall_heatflux(0)
                 IF ( TRIM( topography ) /= 'flat' )  THEN
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          IF ( nzb_s_inner(j,i) /= 0 )  THEN
                             shf(j,i) = wall_heatflux(0)
@@ -755,6 +737,6 @@
                 IF ( TRIM( topography ) /= 'flat' )  THEN
                    wall_qflux = wall_humidityflux
-                   DO  i = nxl-1, nxr+1
-                      DO  j = nys-1, nyn+1
+                   DO  i = nxlg, nxrg
+                      DO  j = nysg, nyng
                          IF ( nzb_s_inner(j,i) /= 0 )  THEN
                             qsws(j,i) = wall_qflux(0)
@@ -827,59 +809,4 @@
        ENDIF
 
-!
-!--    Calculate the initial volume flow at the right and north boundary
-       IF ( conserve_volume_flow )  THEN
-
-          volume_flow_initial_l = 0.0
-          volume_flow_area_l    = 0.0
-  
-          IF ( nxr == nx )  THEN
-             DO  j = nys, nyn
-                DO  k = nzb_2d(j,nx) + 1, nzt
-                   volume_flow_initial_l(1) = volume_flow_initial_l(1) + &
-                                              u(k,j,nx) * dzu(k)
-                   volume_flow_area_l(1)    = volume_flow_area_l(1) + dzu(k)
-                ENDDO
-!
-!--             Correction if velocity at nzb+1 has been set zero further above
-                volume_flow_initial_l(1) = volume_flow_initial_l(1) + &
-                                           u_nzb_p1_for_vfc(j)
-             ENDDO
-          ENDIF
-
-          IF ( nyn == ny )  THEN
-             DO  i = nxl, nxr
-                DO  k = nzb_2d(ny,i) + 1, nzt  
-                   volume_flow_initial_l(2) = volume_flow_initial_l(2) + &
-                                              v(k,ny,i) * dzu(k)
-                   volume_flow_area_l(2)    = volume_flow_area_l(2) + dzu(k)
-                ENDDO
-!
-!--             Correction if velocity at nzb+1 has been set zero further above
-                volume_flow_initial_l(2) = volume_flow_initial_l(2) + &
-                                           v_nzb_p1_for_vfc(i)
-             ENDDO
-          ENDIF
-
-#if defined( __parallel )
-          IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-          CALL MPI_ALLREDUCE( volume_flow_initial_l(1), volume_flow_initial(1),&
-                              2, MPI_REAL, MPI_SUM, comm2d, ierr )
-          IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-          CALL MPI_ALLREDUCE( volume_flow_area_l(1), volume_flow_area(1),      &
-                              2, MPI_REAL, MPI_SUM, comm2d, ierr )
-#else
-          volume_flow_initial = volume_flow_initial_l
-          volume_flow_area    = volume_flow_area_l
-#endif
-!
-!--       In case of 'bulk_velocity' mode, volume_flow_initial is overridden
-!--       and calculated from u|v_bulk instead.
-          IF ( TRIM( conserve_volume_flow_mode ) == 'bulk_velocity' )  THEN
-             volume_flow_initial(1) = u_bulk * volume_flow_area(1)
-             volume_flow_initial(2) = v_bulk * volume_flow_area(2)
-          ENDIF
-
-       ENDIF
 
 !
@@ -968,8 +895,8 @@
           sa_p  = sa
        ENDIF
-
+       
 
     ELSEIF ( TRIM( initializing_actions ) == 'read_restart_data'  .OR.    &
-             TRIM( initializing_actions ) == 'cyclic_fill' )  &
+         TRIM( initializing_actions ) == 'cyclic_fill' )  &
     THEN
 !
@@ -978,9 +905,5 @@
        IF ( TRIM( initializing_actions ) == 'cyclic_fill' )  THEN
 
-          WRITE (9,*) 'before read_parts_of_var_list'
-          CALL local_flush( 9 )
           CALL read_parts_of_var_list
-          WRITE (9,*) 'after read_parts_of_var_list'
-          CALL local_flush( 9 )
           CALL close_file( 13 )
 
@@ -1002,11 +925,11 @@
 !--          conditions are used)
              IF ( inflow_l )  THEN
-                DO  j = nys-1, nyn+1
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
-                      u(k,j,-1)  = mean_inflow_profiles(k,1)
-                      v(k,j,-1)  = mean_inflow_profiles(k,2)
-                      w(k,j,-1)  = 0.0
-                      pt(k,j,-1) = mean_inflow_profiles(k,4)
-                      e(k,j,-1)  = mean_inflow_profiles(k,5)
+                      u(k,j,nxlg:-1)  = mean_inflow_profiles(k,1)
+                      v(k,j,nxlg:-1)  = mean_inflow_profiles(k,2)
+                      w(k,j,nxlg:-1)  = 0.0
+                      pt(k,j,nxlg:-1) = mean_inflow_profiles(k,4)
+                      e(k,j,nxlg:-1)  = mean_inflow_profiles(k,5)
                    ENDDO
                 ENDDO
@@ -1064,9 +987,6 @@
 !
 !--    Read binary data from restart file
-          WRITE (9,*) 'before read_3d_binary'
-          CALL local_flush( 9 )
+
        CALL read_3d_binary
-          WRITE (9,*) 'after read_3d_binary'
-          CALL local_flush( 9 )
 
 !
@@ -1074,25 +994,4 @@
        IF ( TRIM( initializing_actions ) == 'cyclic_fill' .AND.  &
             topography /= 'flat' )  THEN
-!
-!--       Correction of initial volume flow
-          IF ( conserve_volume_flow )  THEN
-             IF ( nxr == nx )  THEN
-                DO  j = nys, nyn
-                   DO  k = nzb + 1, nzb_u_inner(j,nx)
-                      u_nzb_p1_for_vfc(j) = u_nzb_p1_for_vfc(j) + &
-                                            u(k,j,nx) * dzu(k)
-                   ENDDO
-                ENDDO
-             ENDIF
-             IF ( nyn == ny )  THEN
-                DO  i = nxl, nxr
-                   DO  k = nzb + 1, nzb_v_inner(ny,i)
-                      v_nzb_p1_for_vfc(i) = v_nzb_p1_for_vfc(i) + &
-                                            v(k,ny,i) * dzu(k)
-                   ENDDO
-                ENDDO
-             ENDIF
-          ENDIF
-
 !
 !--       Inside buildings set velocities and TKE back to zero.
@@ -1100,6 +999,6 @@
 !--       maybe revise later.
           IF ( timestep_scheme(1:5) == 'runge' )  THEN
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    u  (nzb:nzb_u_inner(j,i),j,i) = 0.0
                    v  (nzb:nzb_v_inner(j,i),j,i) = 0.0
@@ -1118,6 +1017,6 @@
              ENDDO
           ELSE
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    u  (nzb:nzb_u_inner(j,i),j,i) = 0.0
                    v  (nzb:nzb_v_inner(j,i),j,i) = 0.0
@@ -1139,56 +1038,4 @@
 
 !
-!--    Calculate the initial volume flow at the right and north boundary
-       IF ( conserve_volume_flow  .AND.  &
-            TRIM( initializing_actions ) == 'cyclic_fill' )  THEN
-
-          volume_flow_initial_l = 0.0
-          volume_flow_area_l    = 0.0
-  
-          IF ( nxr == nx )  THEN
-             DO  j = nys, nyn
-                DO  k = nzb_2d(j,nx) + 1, nzt
-                   volume_flow_initial_l(1) = volume_flow_initial_l(1) + &
-                                              u(k,j,nx) * dzu(k)
-                   volume_flow_area_l(1)    = volume_flow_area_l(1) + dzu(k)
-                ENDDO
-!
-!--             Correction if velocity inside buildings has been set to zero
-!--             further above
-                volume_flow_initial_l(1) = volume_flow_initial_l(1) + &
-                                           u_nzb_p1_for_vfc(j)
-             ENDDO
-          ENDIF
-
-          IF ( nyn == ny )  THEN
-             DO  i = nxl, nxr
-                DO  k = nzb_2d(ny,i) + 1, nzt  
-                   volume_flow_initial_l(2) = volume_flow_initial_l(2) + &
-                                              v(k,ny,i) * dzu(k)
-                   volume_flow_area_l(2)    = volume_flow_area_l(2) + dzu(k)
-                ENDDO
-!
-!--             Correction if velocity inside buildings has been set to zero
-!--             further above
-                volume_flow_initial_l(2) = volume_flow_initial_l(2) + &
-                                           v_nzb_p1_for_vfc(i)
-             ENDDO
-          ENDIF
-
-#if defined( __parallel )
-          IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-          CALL MPI_ALLREDUCE( volume_flow_initial_l(1), volume_flow_initial(1),&
-                              2, MPI_REAL, MPI_SUM, comm2d, ierr )
-          IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-          CALL MPI_ALLREDUCE( volume_flow_area_l(1), volume_flow_area(1),      &
-                              2, MPI_REAL, MPI_SUM, comm2d, ierr )
-#else
-          volume_flow_initial = volume_flow_initial_l
-          volume_flow_area    = volume_flow_area_l
-#endif  
-       ENDIF
-
-
-!
 !--    Calculate initial temperature field and other constants used in case
 !--    of a sloping surface
@@ -1243,7 +1090,84 @@
           w_m_n(:,:,:) = w(:,ny-1:ny,:)
        ENDIF
-
-    ENDIF
-
+       
+    ENDIF
+!
+!-- Calculate the initial volume flow at the right and north boundary
+    IF ( conserve_volume_flow ) THEN
+
+       volume_flow_initial_l = 0.0
+       volume_flow_area_l    = 0.0
+  
+       IF  ( TRIM( initializing_actions ) == 'cyclic_fill' )  THEN
+
+          IF ( nxr == nx )  THEN
+             DO  j = nys, nyn
+                DO  k = nzb_2d(j,nx) + 1, nzt
+                   volume_flow_initial_l(1) = volume_flow_initial_l(1) + &
+                                              hom_sum(k,1,0) * dzw(k)
+                   volume_flow_area_l(1)    = volume_flow_area_l(1) + dzw(k)
+                ENDDO
+             ENDDO
+          ENDIF
+          
+          IF ( nyn == ny )  THEN
+             DO  i = nxl, nxr
+                DO  k = nzb_2d(ny,i) + 1, nzt  
+                   volume_flow_initial_l(2) = volume_flow_initial_l(2) + &
+                                               hom_sum(k,2,0) * dzw(k)
+                   volume_flow_area_l(2)    = volume_flow_area_l(2) + dzw(k)
+                ENDDO
+             ENDDO
+          ENDIF
+
+       ELSEIF ( TRIM( initializing_actions ) /= 'read_restart_data' )  THEN
+
+          IF ( nxr == nx )  THEN
+             DO  j = nys, nyn
+                DO  k = nzb_2d(j,nx) + 1, nzt
+                   volume_flow_initial_l(1) = volume_flow_initial_l(1) + &
+                                               u(k,j,nx) * dzw(k)
+                   volume_flow_area_l(1)    = volume_flow_area_l(1) + dzw(k)
+                ENDDO
+             ENDDO
+          ENDIF
+          
+          IF ( nyn == ny )  THEN
+             DO  i = nxl, nxr
+                DO  k = nzb_2d(ny,i) + 1, nzt  
+                   volume_flow_initial_l(2) = volume_flow_initial_l(2) + &
+                                              v(k,ny,i) * dzw(k)
+                   volume_flow_area_l(2)    = volume_flow_area_l(2) + dzw(k)
+                ENDDO
+             ENDDO
+          ENDIF
+
+       ENDIF
+
+#if defined( __parallel )
+          CALL MPI_ALLREDUCE( volume_flow_initial_l(1), volume_flow_initial(1),&
+                              2, MPI_REAL, MPI_SUM, comm2d, ierr )
+          CALL MPI_ALLREDUCE( volume_flow_area_l(1), volume_flow_area(1),      &
+                              2, MPI_REAL, MPI_SUM, comm2d, ierr )
+
+          CALL MPI_ALLREDUCE( volume_flow_initial_l(2), volume_flow_initial(2),&
+                              2, MPI_REAL, MPI_SUM, comm2d, ierr )
+          CALL MPI_ALLREDUCE( volume_flow_area_l(2), volume_flow_area(2),      &
+                              2, MPI_REAL, MPI_SUM, comm2d, ierr )
+
+#else
+          volume_flow_initial = volume_flow_initial_l
+          volume_flow_area    = volume_flow_area_l
+#endif  
+
+!
+!--       In case of 'bulk_velocity' mode, volume_flow_initial is overridden
+!--       and calculated from u|v_bulk instead.
+          IF ( TRIM( conserve_volume_flow_mode ) == 'bulk_velocity' )  THEN
+             volume_flow_initial(1) = u_bulk * volume_flow_area(1)
+             volume_flow_initial(2) = v_bulk * volume_flow_area(2)
+          ENDIF
+
+       ENDIF
 !
 !-- Initialization of the leaf area density
@@ -1254,6 +1178,6 @@
           CASE( 'block' )
 
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    lad_s(:,j,i) = lad(:)
                    cdc(:,j,i)   = drag_coefficient
@@ -1277,10 +1201,10 @@
           END SELECT
 
-       CALL exchange_horiz( lad_s )
-       CALL exchange_horiz( cdc )
+       CALL exchange_horiz( lad_s, nbgp )
+       CALL exchange_horiz( cdc, nbgp )
 
        IF ( passive_scalar ) THEN
-          CALL exchange_horiz( sls )
-          CALL exchange_horiz( sec )
+          CALL exchange_horiz( sls, nbgp )
+          CALL exchange_horiz( sec, nbgp )
        ENDIF
 
@@ -1311,7 +1235,7 @@
        lad_w(nzt+1,:,:)     = lad_w(nzt,:,:)
 
-       CALL exchange_horiz( lad_u )
-       CALL exchange_horiz( lad_v )
-       CALL exchange_horiz( lad_w )
+       CALL exchange_horiz( lad_u, nbgp )
+       CALL exchange_horiz( lad_v, nbgp )
+       CALL exchange_horiz( lad_w, nbgp )
 
 !
@@ -1322,6 +1246,6 @@
 !--       integration of the leaf area density
           lai(:,:,:) = 0.0
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
                 DO  k = pch_index-1, 0, -1
                    lai(k,j,i) = lai(k+1,j,i) +                   &
@@ -1339,6 +1263,6 @@
 !--       Evaluation of the upward kinematic vertical heat flux within the 
 !--       canopy
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
                 DO  k = 0, pch_index
                    canopy_heat_flux(k,j,i) = cthf *                    &
@@ -1384,4 +1308,6 @@
 !-- Initialize quantities for special advections schemes
     CALL init_advec
+    IF ( momentum_advec == 'ws-scheme' .OR.  &
+         scalar_advec == 'ws-scheme' ) CALL ws_init
 
 !
@@ -1439,5 +1365,5 @@
     IF ( bc_lr == 'dirichlet/radiation' )  THEN
 
-       DO  i = nxl-1, nxr+1
+       DO  i = nxlg, nxrg
           IF ( i >= nx - outflow_damping_width )  THEN
              km_damp_x(i) = km_damp_max * MIN( 1.0,                    &
@@ -1452,5 +1378,5 @@
     ELSEIF ( bc_lr == 'radiation/dirichlet' )  THEN
 
-       DO  i = nxl-1, nxr+1
+       DO  i = nxlg, nxrg
           IF ( i <= outflow_damping_width )  THEN
              km_damp_x(i) = km_damp_max * MIN( 1.0,                    &
@@ -1467,5 +1393,5 @@
     IF ( bc_ns == 'dirichlet/radiation' )  THEN
 
-       DO  j = nys-1, nyn+1
+       DO  j = nysg, nyng
           IF ( j >= ny - outflow_damping_width )  THEN
              km_damp_y(j) = km_damp_max * MIN( 1.0,                    &
@@ -1480,5 +1406,5 @@
     ELSEIF ( bc_ns == 'radiation/dirichlet' )  THEN
 
-       DO  j = nys-1, nyn+1
+       DO  j = nysg, nyng
           IF ( j <= outflow_damping_width )  THEN
              km_damp_y(j) = km_damp_max * MIN( 1.0,                    &
@@ -1594,8 +1520,8 @@
 !-- buoyancy, etc. A zero value will occur for cases where all grid points of
 !-- the respective subdomain lie below the surface topography
-    ngp_2dh_outer   = MAX( 1, ngp_2dh_outer(:,:)   )
+    ngp_2dh_outer   = MAX( 1, ngp_2dh_outer(:,:)   ) 
     ngp_3d_inner    = MAX( INT(1, KIND = SELECTED_INT_KIND( 18 )),            &
                            ngp_3d_inner(:) )
-    ngp_2dh_s_inner = MAX( 1, ngp_2dh_s_inner(:,:) )
+    ngp_2dh_s_inner = MAX( 1, ngp_2dh_s_inner(:,:) ) 
 
     DEALLOCATE( ngp_2dh_l, ngp_2dh_outer_l, ngp_3d_inner_l, ngp_3d_inner_tmp )
Index: /palm/trunk/SOURCE/init_advec.f90
===================================================================
--- /palm/trunk/SOURCE/init_advec.f90	(revision 666)
+++ /palm/trunk/SOURCE/init_advec.f90	(revision 667)
@@ -8,5 +8,5 @@
 ! Former revisions:
 ! -----------------
-! $Id$
+! $Id: init_advec.f90 484 2010-02-05 07:36:54Z raasch 
 ! RCS Log replace by Id keyword, revision history cleaned up
 !
Index: /palm/trunk/SOURCE/init_coupling.f90
===================================================================
--- /palm/trunk/SOURCE/init_coupling.f90	(revision 666)
+++ /palm/trunk/SOURCE/init_coupling.f90	(revision 667)
@@ -4,5 +4,7 @@
 ! Current revisions:
 ! -----------------
-! 
+!
+! determination of target_id's moved to init_pegrid
+!
 !
 ! Former revisions:
@@ -24,4 +26,5 @@
     USE pegrid
     USE control_parameters
+    USE indices
 
     IMPLICIT NONE
@@ -48,4 +51,6 @@
 !-- ATTENTION: numprocs will be reset according to the new communicators
 #if defined ( __parallel )
+
+!myid_absolut = myid
     IF ( myid == 0 )  THEN
        READ (*,*,ERR=10,END=10)  coupling_mode, bc_data(1), bc_data(2)
@@ -94,19 +99,13 @@
 
        IF ( myid < bc_data(1) ) THEN
-          inter_color = 0
-          numprocs = bc_data(1)
+          inter_color     = 0
+          numprocs        = bc_data(1)
+          coupling_mode   = 'atmosphere_to_ocean'
        ELSE
-          inter_color = 1
-          numprocs = bc_data(2)
+          inter_color     = 1
+          numprocs        = bc_data(2)
+          coupling_mode   = 'ocean_to_atmosphere'
        ENDIF
-!
-!--    Calculate the target PE for coupling and set up the new communicators.
-!--    Currently only 1:1 topologies are supported.
-       target_id = myid - ISIGN( numprocs, inter_color - 1 )
-       IF ( inter_color == 0 ) THEN
-          coupling_mode = 'atmosphere_to_ocean'
-       ELSE
-          coupling_mode = 'ocean_to_atmosphere'
-       ENDIF
+
        CALL MPI_COMM_SPLIT( MPI_COMM_WORLD, inter_color, 0, comm_palm, ierr )
        comm2d = comm_palm
Index: /palm/trunk/SOURCE/init_grid.f90
===================================================================
--- /palm/trunk/SOURCE/init_grid.f90	(revision 666)
+++ /palm/trunk/SOURCE/init_grid.f90	(revision 667)
@@ -4,5 +4,10 @@
 ! Current revisions:
 ! -----------------
-! 
+! Definition of new array bounds nxlg, nxrg, nysg, nyng on each PE. 
+! Furthermore the allocation of arrays and steering of loops is done with these
+! parameters. Call of exchange_horiz are modified.
+! In case of dirichlet bounday condition at the bottom zu(0)=0.0
+! dzu_mg has to be set explicitly for a equally spaced grid near bottom.
+! ddzu_pres added to use a equally spaced grid near bottom.
 !
 ! Former revisions:
@@ -76,9 +81,15 @@
 
     REAL, DIMENSION(:,:,:), ALLOCATABLE ::  distance
-
+    
+!
+!   Computation of the array bounds.
+    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(0:nzt+1), zw(0:nzt+1) )
+              dzw(1:nzt+1), l_grid(1:nzt), zu(nzb:nzt+1), zw(nzb:nzt+1) )
 
 !
@@ -97,9 +108,13 @@
 !
 !--    Grid for atmosphere with surface at z=0 (k=0, w-grid).
-!--    Since the w-level lies on the surface, the first u-level (staggered!)
-!--    lies below the surface (used for "mirror" boundary condition).
 !--    The first u-level above the surface corresponds to the top of the 
 !--    Prandtl-layer.
-       zu(0) = - dz * 0.5
+
+       IF ( ibc_uv_b == 0 .OR. ibc_uv_b == 2 ) THEN
+          zu(0) = 0.0
+      !    zu(0) = - dz * 0.5
+       ELSE
+          zu(0) = - dz * 0.5
+       ENDIF
        zu(1) =   dz * 0.5
 
@@ -173,4 +188,16 @@
        dd2zu(k) = 1.0 / ( dzu(k) + dzu(k+1) )
     ENDDO
+    
+!    
+!-- In case of FFT method or SOR swap inverse grid lenght ddzu to ddzu_fft and 
+!-- modify the lowest entry. This is necessary for atmosphere runs, because
+!-- zu(0) and so ddzu(1) changed. Accompanied with this modified arrays 
+!-- pressure solver uses wrong values and this causes kinks in the profiles
+!-- of turbulent quantities.  
+    IF ( psolver /= 'multigrid' ) THEN
+       ALLOCATE( ddzu_pres(1:nzt+1) )
+       ddzu_pres = ddzu
+       IF( .NOT. ocean ) ddzu_pres(1) = ddzu_pres(2)
+    ENDIF   
 
 !
@@ -187,4 +214,9 @@
 
        dzu_mg(:,maximum_grid_level) = dzu
+!       
+!--    To ensure a equally spaced grid. For ocean runs this is not necessary, 
+!--    because zu(0) does not changed so far. Also this would cause errors 
+!--    if a vertical stretching for ocean runs is used.
+       IF ( .NOT. ocean ) dzu_mg(1,maximum_grid_level) = dzu(2)
        dzw_mg(:,maximum_grid_level) = dzw
        nzt_l = nzt
@@ -239,34 +271,38 @@
 !-- the flag arrays needed for the multigrid method
     gls = 2**( maximum_grid_level )
+
     ALLOCATE( corner_nl(nys:nyn,nxl:nxr), corner_nr(nys:nyn,nxl:nxr),       &
               corner_sl(nys:nyn,nxl:nxr), corner_sr(nys:nyn,nxl:nxr),       &
-              nzb_local(-gls:ny+gls,-gls:nx+gls), nzb_tmp(-1:ny+1,-1:nx+1), &
+              nzb_local(-gls:ny+gls,-gls:nx+gls),                                   &
+              nzb_tmp(-nbgp:ny+nbgp,-nbgp:nx+nbgp),                         &
               wall_l(nys:nyn,nxl:nxr), wall_n(nys:nyn,nxl:nxr),             &
               wall_r(nys:nyn,nxl:nxr), wall_s(nys:nyn,nxl:nxr) )
-    ALLOCATE( fwxm(nys-1:nyn+1,nxl-1:nxr+1), fwxp(nys-1:nyn+1,nxl-1:nxr+1), &
-              fwym(nys-1:nyn+1,nxl-1:nxr+1), fwyp(nys-1:nyn+1,nxl-1:nxr+1), &
-              fxm(nys-1:nyn+1,nxl-1:nxr+1), fxp(nys-1:nyn+1,nxl-1:nxr+1),   &
-              fym(nys-1:nyn+1,nxl-1:nxr+1), fyp(nys-1:nyn+1,nxl-1:nxr+1),   &
-              nzb_s_inner(nys-1:nyn+1,nxl-1:nxr+1),                         &
-              nzb_s_outer(nys-1:nyn+1,nxl-1:nxr+1),                         &
-              nzb_u_inner(nys-1:nyn+1,nxl-1:nxr+1),                         &
-              nzb_u_outer(nys-1:nyn+1,nxl-1:nxr+1),                         &
-              nzb_v_inner(nys-1:nyn+1,nxl-1:nxr+1),                         &
-              nzb_v_outer(nys-1:nyn+1,nxl-1:nxr+1),                         &
-              nzb_w_inner(nys-1:nyn+1,nxl-1:nxr+1),                         &
-              nzb_w_outer(nys-1:nyn+1,nxl-1:nxr+1),                         &
-              nzb_diff_s_inner(nys-1:nyn+1,nxl-1:nxr+1),                    &
-              nzb_diff_s_outer(nys-1:nyn+1,nxl-1:nxr+1),                    &
-              nzb_diff_u(nys-1:nyn+1,nxl-1:nxr+1),                          &
-              nzb_diff_v(nys-1:nyn+1,nxl-1:nxr+1),                          &
-              nzb_2d(nys-1:nyn+1,nxl-1:nxr+1),                              &
-              wall_e_x(nys-1:nyn+1,nxl-1:nxr+1),                            &
-              wall_e_y(nys-1:nyn+1,nxl-1:nxr+1),                            &
-              wall_u(nys-1:nyn+1,nxl-1:nxr+1),                              &
-              wall_v(nys-1:nyn+1,nxl-1:nxr+1),                              &
-              wall_w_x(nys-1:nyn+1,nxl-1:nxr+1),                            &
-              wall_w_y(nys-1:nyn+1,nxl-1:nxr+1) )
-
-    ALLOCATE( l_wall(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+    ALLOCATE( fwxm(nysg:nyng,nxlg:nxrg), fwxp(nysg:nyng,nxlg:nxrg),         &
+              fwym(nysg:nyng,nxlg:nxrg), fwyp(nysg:nyng,nxlg:nxrg),         &
+              fxm(nysg:nyng,nxlg:nxrg), fxp(nysg:nyng,nxlg:nxrg),           &
+              fym(nysg:nyng,nxlg:nxrg), fyp(nysg:nyng,nxlg:nxrg),           &
+              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_diff_u(nysg:nyng,nxlg:nxrg),                              &
+              nzb_diff_v(nysg:nyng,nxlg:nxrg),                              &
+              nzb_2d(nysg:nyng,nxlg:nxrg),                                  &
+              wall_e_x(nysg:nyng,nxlg:nxrg),                                &
+              wall_e_y(nysg:nyng,nxlg:nxrg),                                &
+              wall_u(nysg:nyng,nxlg:nxrg),                                  &
+              wall_v(nysg:nyng,nxlg:nxrg),                                  &
+              wall_w_x(nysg:nyng,nxlg:nxrg),                                &
+              wall_w_y(nysg:nyng,nxlg:nxrg) )
+
+
+
+    ALLOCATE( l_wall(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
 
     nzb_s_inner = nzb;  nzb_s_outer = nzb
@@ -327,6 +363,6 @@
     vertical_influence(0) = vertical_influence(1)
 
-    DO  i = nxl-1, nxr+1
-       DO  j = nys-1, nyn+1
+    DO  i = nxlg, nxrg
+       DO  j = nysg, nyng
           DO  k = nzb_s_inner(j,i) + 1, &
                   nzb_s_inner(j,i) + vertical_influence(nzb_s_inner(j,i))
@@ -477,4 +513,6 @@
           nzb_local(:,-gls:-1)        = nzb_local(:,nx-gls+1:nx)
           nzb_local(:,nx+1:nx+gls)    = nzb_local(:,0:gls-1)
+
+
       
           GOTO 12
@@ -588,5 +626,5 @@
 !--    nzb_s_outer: 
 !--    extend nzb_local east-/westwards first, then north-/southwards
-       nzb_tmp = nzb_local(-1:ny+1,-1:nx+1)
+       nzb_tmp = nzb_local(-nbgp:ny+nbgp,-nbgp:nx+nbgp)
        DO  j = -1, ny + 1
           DO  i = 0, nx
@@ -620,5 +658,5 @@
 !--    nzb_u_inner: 
 !--    extend nzb_local rightwards only
-       nzb_tmp = nzb_local(-1:ny+1,-1:nx+1)
+       nzb_tmp = nzb_local(-nbgp:ny+nbgp,-nbgp:nx+nbgp)
        DO  j = -1, ny + 1
           DO  i = 0, nx + 1
@@ -626,5 +664,5 @@
           ENDDO
        ENDDO
-       nzb_u_inner = nzb_tmp(nys-1:nyn+1,nxl-1:nxr+1)
+       nzb_u_inner = nzb_tmp(nysg:nyng,nxlg:nxrg)
 
 !
@@ -652,5 +690,5 @@
 !--    nzb_v_inner:
 !--    extend nzb_local northwards only
-       nzb_tmp = nzb_local(-1:ny+1,-1:nx+1)
+       nzb_tmp = nzb_local(-nbgp:ny+nbgp,-nbgp:nx+nbgp)
        DO  i = -1, nx + 1
           DO  j = 0, ny + 1
@@ -658,5 +696,5 @@
           ENDDO
        ENDDO
-       nzb_v_inner = nzb_tmp(nys-1:nyn+1,nxl-1:nxr+1)
+       nzb_v_inner = nzb_tmp(nys-nbgp:nyn+nbgp,nxl-nbgp:nxr+nbgp)
 
 !
@@ -1096,5 +1134,6 @@
 !
 !-- Need to set lateral boundary conditions for l_wall
-    CALL exchange_horiz( l_wall )
+
+    CALL exchange_horiz( l_wall, nbgp )
 
     DEALLOCATE( corner_nl, corner_nr, corner_sl, corner_sr, nzb_local, &
Index: /palm/trunk/SOURCE/init_particles.f90
===================================================================
--- /palm/trunk/SOURCE/init_particles.f90	(revision 666)
+++ /palm/trunk/SOURCE/init_particles.f90	(revision 667)
@@ -4,5 +4,6 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng for allocation 
+! of arrays.
 !
 ! Former revisions:
@@ -185,6 +186,6 @@
        ENDIF
 
-       ALLOCATE( prt_count(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),       &
-                 prt_start_index(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
+       ALLOCATE( prt_count(nzb:nzt+1,nysg:nyng,nxlg:nxrg),       &
+                 prt_start_index(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
                  particle_mask(maximum_number_of_particles),         &
                  particles(maximum_number_of_particles) )
@@ -209,6 +210,6 @@
 !--    particles, which can be also periodically released at later times.
 !--    Also allocate array for particle tail coordinates, if needed.
-       ALLOCATE( prt_count(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),       &
-                 prt_start_index(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
+       ALLOCATE( prt_count(nzb:nzt+1,nysg:nyng,nxlg:nxrg),       &
+                 prt_start_index(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
                  particle_mask(maximum_number_of_particles),         &
                  particles(maximum_number_of_particles) )
Index: /palm/trunk/SOURCE/init_pegrid.f90
===================================================================
--- /palm/trunk/SOURCE/init_pegrid.f90	(revision 666)
+++ /palm/trunk/SOURCE/init_pegrid.f90	(revision 667)
@@ -4,4 +4,19 @@
 ! Current revisions:
 ! -----------------
+!
+! Moved determination of target_id's from init_coupling
+!
+! Determination of parameters needed for coupling (coupling_topology, ngp_a, ngp_o)
+! with different grid/processor-topology in ocean and atmosphere
+!
+! 
+! Adaption of ngp_xy, ngp_y to a dynamic number of ghost points.
+! The maximum_grid_level changed from 1 to 0. 0 is the normal grid, 1 to 
+! maximum_grid_level the grids for multigrid, in which 0 and 1 are normal grids.
+! This distinction is due to reasons of data exchange and performance for the 
+! normal grid and grids in poismg.
+! The definition of MPI-Vectors adapted to a dynamic numer of ghost points.
+! New MPI-Vectors for data exchange between left and right boundaries added. 
+! This is due to reasons of performance (10% faster).
 ! 
 ! ATTENTION: nnz_x undefined problem still has to be solved!!!!!!!!
@@ -79,4 +94,5 @@
 
 
+
     IMPLICIT NONE
 
@@ -88,4 +104,6 @@
 
     INTEGER, DIMENSION(:), ALLOCATABLE ::  ind_all, nxlf, nxrf, nynf, nysf
+
+    INTEGER, DIMENSION(2) :: pdims_remote
 
     LOGICAL ::  found
@@ -103,4 +121,5 @@
 
 #if defined( __parallel )
+
 !
 !-- Determine the processor topology or check it, if prescribed by the user
@@ -624,9 +643,18 @@
 #endif
 
+! 
+!-- Determine the number of ghost points
+    IF (scalar_advec == 'ws-scheme' .OR. momentum_advec == 'ws-scheme') THEN
+       nbgp = 3
+    ELSE
+       nbgp = 1
+    END IF 
+
 !
 !-- In case of coupled runs, create a new MPI derived datatype for the
 !-- exchange of surface (xy) data .
 !-- Gridpoint number for the exchange of ghost points (xy-plane)
-    ngp_xy  = ( nxr - nxl + 3 ) * ( nyn - nys + 3 )
+
+    ngp_xy  = ( nxr - nxl + 1 + 2 * nbgp ) * ( nyn - nys + 1 + 2 * nbgp )
 
 !
@@ -635,4 +663,106 @@
     CALL MPI_TYPE_VECTOR( ngp_xy, 1, nzt-nzb+2, MPI_REAL, type_xy, ierr )
     CALL MPI_TYPE_COMMIT( type_xy, ierr )
+
+
+    IF ( TRIM( coupling_mode ) .NE. 'uncoupled' ) THEN
+   
+!
+!--    Pass the number of grid points of the atmosphere model to
+!--    the ocean model and vice versa
+       IF ( coupling_mode == 'atmosphere_to_ocean' )  THEN
+
+          nx_a = nx
+          ny_a = ny
+
+          IF ( myid == 0 ) THEN
+             CALL MPI_SEND( nx_a, 1, MPI_INTEGER, numprocs, 1, &
+                            comm_inter, ierr )
+             CALL MPI_SEND( ny_a, 1, MPI_INTEGER, numprocs, 2, &
+                            comm_inter, ierr )
+             CALL MPI_SEND( pdims, 2, MPI_INTEGER, numprocs, 3, &
+                            comm_inter, ierr )
+             CALL MPI_RECV( nx_o, 1, MPI_INTEGER, numprocs, 4, &
+                            comm_inter, status, ierr )
+             CALL MPI_RECV( ny_o, 1, MPI_INTEGER, numprocs, 5, &
+                            comm_inter, status, ierr )
+             CALL MPI_RECV( pdims_remote, 2, MPI_INTEGER, numprocs, 6, &
+                            comm_inter, status, ierr )
+          ENDIF
+
+          CALL MPI_BCAST( nx_o, 1, MPI_INTEGER, 0, comm2d, ierr)
+          CALL MPI_BCAST( ny_o, 1, MPI_INTEGER, 0, comm2d, ierr) 
+          CALL MPI_BCAST( pdims_remote, 2, MPI_INTEGER, 0, comm2d, ierr)
+       
+       ELSEIF ( coupling_mode == 'ocean_to_atmosphere' )  THEN
+
+          nx_o = nx
+          ny_o = ny 
+
+          IF ( myid == 0 ) THEN
+             CALL MPI_RECV( nx_a, 1, MPI_INTEGER, 0, 1, &
+                            comm_inter, status, ierr )
+             CALL MPI_RECV( ny_a, 1, MPI_INTEGER, 0, 2, &
+                            comm_inter, status, ierr )
+             CALL MPI_RECV( pdims_remote, 2, MPI_INTEGER, 0, 3, &
+                            comm_inter, status, ierr )
+             CALL MPI_SEND( nx_o, 1, MPI_INTEGER, 0, 4, &
+                            comm_inter, ierr )
+             CALL MPI_SEND( ny_o, 1, MPI_INTEGER, 0, 5, &
+                            comm_inter, ierr )
+             CALL MPI_SEND( pdims, 2, MPI_INTEGER, 0, 6, &
+                            comm_inter, ierr )
+          ENDIF
+
+          CALL MPI_BCAST( nx_a, 1, MPI_INTEGER, 0, comm2d, ierr)
+          CALL MPI_BCAST( ny_a, 1, MPI_INTEGER, 0, comm2d, ierr) 
+          CALL MPI_BCAST( pdims_remote, 2, MPI_INTEGER, 0, comm2d, ierr) 
+
+       ENDIF
+  
+       ngp_a = (nx_a+1+2*nbgp)*(ny_a+1+2*nbgp)
+       ngp_o = (nx_o+1+2*nbgp)*(ny_o+1+2*nbgp)
+
+!
+!--    determine if the horizontal grid and the number of PEs 
+!--    in ocean and atmosphere is same or not
+!--    (different number of PEs still not implemented)
+       IF ( nx_o == nx_a .AND. ny_o == ny_a .AND.  &
+            pdims(1) == pdims_remote(1) .AND. pdims(2) == pdims_remote(2) ) &
+       THEN
+          coupling_topology = 0
+       ELSE
+          coupling_topology = 1
+       ENDIF 
+
+!
+!--    Determine the target PEs for the exchange between ocean and
+!--    atmosphere (comm2d)
+       IF ( coupling_topology == 0) THEN
+          IF ( TRIM( coupling_mode ) .EQ. 'atmosphere_to_ocean' ) THEN
+             target_id = myid + numprocs
+          ELSE
+             target_id = myid 
+          ENDIF
+
+       ELSE
+
+!
+!--       In case of nonequivalent topology in ocean and atmosphere only for
+!--       PE0 in ocean and PE0 in atmosphere a target_id is needed, since
+!--       data echxchange between ocean and atmosphere will be done only by
+!--       those PEs.    
+          IF ( myid == 0 ) THEN
+             IF ( TRIM( coupling_mode ) .EQ. 'atmosphere_to_ocean' ) THEN
+                target_id = numprocs 
+             ELSE
+                target_id = 0
+             ENDIF
+ print*, coupling_mode, myid, " -> ", target_id, "numprocs: ", numprocs 
+          ENDIF
+       ENDIF
+
+    ENDIF
+
+
 #endif
 
@@ -854,5 +984,5 @@
     ELSE
 
-       maximum_grid_level = 1
+       maximum_grid_level = 0
 
     ENDIF
@@ -863,13 +993,19 @@
 !
 !-- Gridpoint number for the exchange of ghost points (y-line for 2D-arrays)
-    ngp_y  = nyn - nys + 1
+    ngp_y  = nyn - nys + 1 + 2 * nbgp
 
 !
 !-- Define a new MPI derived datatype for the exchange of ghost points in
 !-- y-direction for 2D-arrays (line)
-    CALL MPI_TYPE_VECTOR( nxr-nxl+3, 1, ngp_y+2, MPI_REAL, type_x, ierr )
+    CALL MPI_TYPE_VECTOR( nxr-nxl+1+2*nbgp, nbgp, ngp_y, MPI_REAL, type_x, ierr )
     CALL MPI_TYPE_COMMIT( type_x, ierr )
-    CALL MPI_TYPE_VECTOR( nxr-nxl+3, 1, ngp_y+2, MPI_INTEGER, type_x_int, ierr )
+    CALL MPI_TYPE_VECTOR( nxr-nxl+1+2*nbgp, nbgp, ngp_y, MPI_INTEGER, type_x_int, ierr )
     CALL MPI_TYPE_COMMIT( type_x_int, ierr )
+
+    CALL MPI_TYPE_VECTOR( nbgp, ngp_y, ngp_y, MPI_REAL, type_y, ierr )
+    CALL MPI_TYPE_COMMIT( type_y, ierr )
+    CALL MPI_TYPE_VECTOR( nbgp, ngp_y, ngp_y, MPI_INTEGER, type_y_int, ierr )
+    CALL MPI_TYPE_COMMIT( type_y_int, ierr )
+
 
 !
@@ -879,21 +1015,50 @@
 !-- Do these calculations for the model grid and (if necessary) also
 !-- for the coarser grid levels used in the multigrid method
-    ALLOCATE ( ngp_yz(maximum_grid_level), type_xz(maximum_grid_level) )
+    ALLOCATE ( ngp_yz(0:maximum_grid_level), type_xz(0:maximum_grid_level),&
+               type_yz(0:maximum_grid_level) )
 
     nxl_l = nxl; nxr_l = nxr; nys_l = nys; nyn_l = nyn; nzb_l = nzb; nzt_l = nzt
-         
-    DO i = maximum_grid_level, 1 , -1
-       ngp_yz(i) = (nzt_l - nzb_l + 2) * (nyn_l - nys_l + 3)
-
-       CALL MPI_TYPE_VECTOR( nxr_l-nxl_l+3, nzt_l-nzb_l+2, ngp_yz(i), &
+!
+!-- Discern between the model grid, which needs nbgp ghost points and
+!-- grid levels for the multigrid scheme. In the latter case only one
+!-- ghost point is necessary.
+!-- First definition of mpi-vectors for exchange of ghost layers on normal 
+!-- grid. The following loop is needed for data exchange in poismg.f90.
+!
+!-- Determine number of grid points of yz-layer for exchange
+    ngp_yz(0) = (nzt - nzb + 2) * (nyn - nys + 1 + 2 * nbgp)
+!
+!-- Define a new mpi datatype for the exchange of left - right boundaries.
+!-- Indeed the data are connected in the physical memory and no mpi-vector
+!-- is necessary, but the data exchange between left and right PE's using
+!-- mpi-vectors is 10% faster than without.
+    CALL MPI_TYPE_VECTOR( nxr-nxl+1+2*nbgp, nbgp*(nzt-nzb+2), ngp_yz(0), &
+                             MPI_REAL, type_xz(0), ierr )
+    CALL MPI_TYPE_COMMIT( type_xz(0), ierr )
+
+    CALL MPI_TYPE_VECTOR( nbgp, ngp_yz(0), ngp_yz(0), MPI_REAL, type_yz(0), ierr) 
+    CALL MPI_TYPE_COMMIT( type_yz(0), ierr )
+!
+!-- Definition of mpi-vectors for multigrid
+    IF ( psolver == 'multigrid' )  THEN
+!    
+!--   The definition of mpi-vectors as aforementioned, but only 1 ghost point is used.
+       DO i = maximum_grid_level, 1 , -1
+          ngp_yz(i) = (nzt_l - nzb_l + 2) * (nyn_l - nys_l + 3)
+
+          CALL MPI_TYPE_VECTOR( nxr_l-nxl_l+3, nzt_l-nzb_l+2, ngp_yz(i), &
                              MPI_REAL, type_xz(i), ierr )
-       CALL MPI_TYPE_COMMIT( type_xz(i), ierr )
-
-       nxl_l = nxl_l / 2 
-       nxr_l = nxr_l / 2
-       nys_l = nys_l / 2 
-       nyn_l = nyn_l / 2 
-       nzt_l = nzt_l / 2 
-    ENDDO
+          CALL MPI_TYPE_COMMIT( type_xz(i), ierr )
+
+          CALL MPI_TYPE_VECTOR( 1, ngp_yz(i), ngp_yz(i), MPI_REAL, type_yz(i), ierr)
+          CALL MPI_TYPE_COMMIT( type_yz(i), ierr )
+
+          nxl_l = nxl_l / 2
+          nxr_l = nxr_l / 2
+          nys_l = nys_l / 2
+          nyn_l = nyn_l / 2
+          nzt_l = nzt_l / 2
+       ENDDO
+    END IF
 #endif
 
Index: /palm/trunk/SOURCE/init_pt_anomaly.f90
===================================================================
--- /palm/trunk/SOURCE/init_pt_anomaly.f90	(revision 666)
+++ /palm/trunk/SOURCE/init_pt_anomaly.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! Call of exchange_horiz are modified.
 !
 ! Former revisions:
@@ -73,5 +73,5 @@
 !
 !-- Exchange of boundary values for temperature
-    CALL exchange_horiz( pt )
+    CALL exchange_horiz( pt, nbgp )
 
 
Index: /palm/trunk/SOURCE/init_rankine.f90
===================================================================
--- /palm/trunk/SOURCE/init_rankine.f90	(revision 666)
+++ /palm/trunk/SOURCE/init_rankine.f90	(revision 667)
@@ -4,5 +4,6 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng. 
+! Calls of exchange_horiz are modified.
 !
 ! Former revisions:
@@ -55,6 +56,6 @@
 !-- Reset initial profiles to constant profiles
     IF ( INDEX(initializing_actions, 'set_constant_profiles') /= 0 )  THEN
-       DO  i = nxl-1, nxr+1
-          DO  j = nys-1, nyn+1
+       DO  i = nxlg, nxrg
+          DO  j = nysg, nyng
              pt(:,j,i) = pt_init
              u(:,j,i)  = u_init
@@ -148,6 +149,6 @@
 !
 !-- Exchange of boundary values for the velocities.
-    CALL exchange_horiz( u )
-    CALL exchange_horiz( v )
+    CALL exchange_horiz( u, nbgp)
+    CALL exchange_horiz( v, nbgp )
 !
 !-- Make velocity field nondivergent.
Index: /palm/trunk/SOURCE/init_slope.f90
===================================================================
--- /palm/trunk/SOURCE/init_slope.f90	(revision 666)
+++ /palm/trunk/SOURCE/init_slope.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng.
 !
 ! Former revisions:
@@ -46,7 +46,7 @@
 !
 !-- Calculate reference temperature field needed for computing buoyancy
-    ALLOCATE( pt_slope_ref(nzb:nzt+1,nxl-1:nxr+1) )
+    ALLOCATE( pt_slope_ref(nzb:nzt+1,nxlg:nxrg) )
 
-    DO  i = nxl-1, nxr+1
+    DO  i = nxlg, nxrg
        DO  k = nzb, nzt+1
 
@@ -90,5 +90,5 @@
 !
 !--    Set initial temperature equal to the reference temperature field
-       DO  j = nys-1, nyn+1
+       DO  j = nysg, nyng
           pt(:,j,:) = pt_slope_ref
        ENDDO
Index: /palm/trunk/SOURCE/local_stop.f90
===================================================================
--- /palm/trunk/SOURCE/local_stop.f90	(revision 666)
+++ /palm/trunk/SOURCE/local_stop.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! Exchange of terminate_coupled between ocean and atmosphere via PE0
 !
 ! Former revisions:
@@ -63,8 +63,11 @@
              IF ( terminate_coupled == 0 )  THEN
                 terminate_coupled = 1
-                CALL MPI_SENDRECV( &
-                     terminate_coupled,        1, MPI_INTEGER, target_id,  0, &
-                     terminate_coupled_remote, 1, MPI_INTEGER, target_id,  0, &
-                     comm_inter, status, ierr )
+                IF ( myid == 0 ) THEN
+                   CALL MPI_SENDRECV( &
+                        terminate_coupled,        1, MPI_INTEGER, target_id,  0, &
+                        terminate_coupled_remote, 1, MPI_INTEGER, target_id,  0, &
+                        comm_inter, status, ierr )
+                ENDIF
+                CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_REAL, 0, comm2d, ierr)
              ENDIF
              CALL MPI_FINALIZE( ierr )
Index: /palm/trunk/SOURCE/modules.f90
===================================================================
--- /palm/trunk/SOURCE/modules.f90	(revision 666)
+++ /palm/trunk/SOURCE/modules.f90	(revision 667)
@@ -5,5 +5,36 @@
 ! Current revisions:
 ! -----------------
+!
+! Removed u_nzb_p1_for_vfc and v_nzb_p1_for_vfc
+!
+! For coupling with different resolution in ocean and atmophere:
+! +nx_a, +nx_o, ny_a, +ny_o, ngp_a, ngp_o, +total_2d_o, +total_2d_a,
+! +coupling_topology 
+!
+!
+! Buffer arrays for the left sided advective fluxes added in arrays_3d.
+! +flux_s_u, +flux_s_v, +flux_s_w, +diss_s_u, +diss_s_v, +diss_s_w,
+! +flux_s_pt, +diss_s_pt, +flux_s_e, +diss_s_e, +flux_s_q, +diss_s_q,
+! +flux_s_sa, +diss_s_sa
+! 3d arrays for dissipation control added. (only necessary for vector arch.)
+! +var_x, +var_y, +var_z, +gamma_x, +gamma_y, +gamma_z
+! Default of momentum_advec and scalar_advec changed to 'ws-scheme' .
+! +exchange_mg added in control_parameters to steer the data exchange.
+! Parameters +nbgp, +nxlg, +nxrg, +nysg, +nyng added in indices.
+! flag array +boundary_flags added in indices to steer the degradation of order 
+! of the advective fluxes when non-cyclic boundaries are used.
+! MPI-datatypes +type_y, +type_y_int and +type_yz for data_exchange added in 
+! pegrid.
+! +sums_wsus_ws_l, +sums_wsvs_ws_l, +sums_us2_ws_l, +sums_vs2_ws_l, 
+! +sums_ws2_ws_l, +sums_wspts_ws_l, +sums_wssas_ws_l, +sums_wsqs_ws_l 
+! and +weight_substep added in statistics to steer the statistical evaluation 
+! of turbulent fluxes in the advection routines.
+! LOGICALS +ws_scheme_sca and +ws_scheme_mom added to get a better performance
+! in prognostic_equations.
+! LOGICAL +dissipation_control control added to steer numerical dissipation
+! in ws-scheme.
 ! 
+!
+! Changed length of string run_description_header
 !
 ! Former revisions:
@@ -51,7 +82,16 @@
 ! ws_vertical_gradient_level_ind, w_subs
 !
-! 410 2009-12-04 17:05:40Z letzel
-! masked data output: + dt_domask, mask_01~20_x|y|z, mask_01~20_x|y|z_loop, 
+! Branch revisions:
+! -----------------
+! Masked data output: + dt_domask, mask_01~20_x|y|z, mask_01~20_x|y|z_loop, 
 ! mask_scale|_x|y|z, masks, skip_time_domask
+!
+! control_parameters: scalar and momentum advection set to 'ws-scheme' as
+! default scheme
+! indices: nbgp=3 set as default value
+!
+! Former revisions:
+! -----------------
+! $Id$
 !
 ! 388 2009-09-23 09:40:33Z raasch
@@ -190,11 +230,17 @@
 
     REAL, DIMENSION(:), ALLOCATABLE ::                                         &
-          ddzu, dd2zu, dzu, ddzw, dzw, hyp, inflow_damping_factor, km_damp_x,  &
-          km_damp_y, lad, l_grid, pt_init, q_init, rdf, sa_init, ug, u_init,   &
-          u_nzb_p1_for_vfc, vg, v_init, v_nzb_p1_for_vfc, w_subs, zu, zw
+          ddzu, ddzu_pres, dd2zu, dzu, ddzw, dzw, hyp, inflow_damping_factor,  &
+          km_damp_x, km_damp_y, lad, l_grid, pt_init, q_init, rdf, sa_init,    &
+          ug, u_init, u_nzb_p1_for_vfc, vg, v_init, v_nzb_p1_for_vfc, w_subs,  &
+          zu, zw, flux_s_u, flux_s_v, flux_s_w, diss_s_u, diss_s_v, diss_s_w,  &
+          flux_s_pt, diss_s_pt, flux_s_e, diss_s_e, flux_s_q, diss_s_q,        &
+          flux_s_sa, diss_s_sa
 
     REAL, DIMENSION(:,:), ALLOCATABLE ::                                       &
           c_u, c_v, c_w, dzu_mg, dzw_mg, f1_mg, f2_mg, f3_mg,                  &
-          mean_inflow_profiles, pt_slope_ref, qs, qswst_remote, ts, us, z0
+          mean_inflow_profiles, pt_slope_ref, qs, qswst_remote, ts, us, z0,    &
+          flux_l_u, flux_l_v, flux_l_w, diss_l_u, diss_l_v, diss_l_w,          &
+          flux_l_pt, diss_l_pt, flux_l_e, diss_l_e, flux_l_q, diss_l_q,        &
+          flux_l_sa, diss_l_sa, total_2d_o, total_2d_a
 
     REAL, DIMENSION(:,:), ALLOCATABLE, TARGET ::                               &
@@ -227,4 +273,7 @@
 
     REAL, DIMENSION(:,:,:,:), ALLOCATABLE ::  rif_wall
+
+    REAL, DIMENSION(:,:,:), ALLOCATABLE :: var_x, var_y, var_z, gamma_x,        &
+                                           gamma_y, gamma_z
 
     SAVE
@@ -290,4 +339,6 @@
 
     REAL    ::  pi = 3.141592654
+    REAL    ::  adv_sca_5, adv_sca_3, adv_mom_5, adv_mom_3
+    
 
     SAVE
@@ -336,7 +387,7 @@
     CHARACTER (LEN=16)   ::  conserve_volume_flow_mode = 'default', &
                              loop_optimization = 'default', &
-                             momentum_advec = 'pw-scheme', &
+                             momentum_advec = 'ws-scheme', &
                              psolver = 'poisfft', &
-                             scalar_advec = 'pw-scheme'
+                             scalar_advec = 'ws-scheme'
     CHARACTER (LEN=20)   ::  bc_e_b = 'neumann', bc_lr = 'cyclic', &
                              bc_ns = 'cyclic', bc_p_b = 'neumann', &
@@ -359,5 +410,6 @@
     CHARACTER (LEN=64)   ::  host = ' '
     CHARACTER (LEN=80)   ::  log_message, run_identifier
-    CHARACTER (LEN=100)  ::  initializing_actions = ' ', run_description_header
+    CHARACTER (LEN=100)  ::  initializing_actions = ' ' 
+    CHARACTER (LEN=110)  ::  run_description_header
     CHARACTER (LEN=1000) ::  message_string = ' '
 
@@ -378,4 +430,5 @@
     INTEGER ::  abort_mode = 1, average_count_pr = 0, average_count_sp = 0, &
                 average_count_3d = 0, current_timestep_number = 0, &
+                coupling_topology = 0, &
                 dist_range = 0, disturbance_level_ind_b, &
                 disturbance_level_ind_t, doav_n = 0, dopr_n = 0, &
@@ -432,9 +485,11 @@
                 constant_waterflux = .TRUE., create_disturbances = .TRUE., &
                 cut_spline_overshoot = .TRUE., &
-                data_output_2d_on_each_pe = .TRUE., do2d_at_begin = .FALSE., &
+                data_output_2d_on_each_pe = .TRUE., & 
+                dissipation_control = .FALSE., do2d_at_begin = .FALSE., &
                 do3d_at_begin = .FALSE., do3d_compress = .FALSE., &
                 do_sum = .FALSE., dp_external = .FALSE., dp_smooth = .FALSE., &
                 dt_changed = .FALSE., dt_fixed = .FALSE., &
                 disturbance_created = .FALSE., &
+                exchange_mg = .FALSE., &
                 first_call_advec_particles = .TRUE., &
                 force_print_header = .FALSE., galilei_transformation = .FALSE.,&
@@ -457,5 +512,6 @@
                 use_surface_fluxes = .FALSE., use_top_fluxes = .FALSE., &
                 use_ug_for_galilei_tr = .TRUE., use_upstream_for_tke = .FALSE.,&
-                wall_adjustment = .TRUE.
+                wall_adjustment = .TRUE., ws_scheme_sca = .FALSE.,             &
+                ws_scheme_mom = .FALSE.
 
     LOGICAL ::  data_output_xy(0:1) = .FALSE., data_output_xz(0:1) = .FALSE., &
@@ -761,7 +817,9 @@
 !------------------------------------------------------------------------------!
 
-    INTEGER ::  ngp_sums, nnx, nx = 0, nxa, nxl, nxlu, nxr, nxra, nx_on_file,  &
-                nny, ny = 0, nya, nyn, nyna, nys, nysv, ny_on_file, nnz,       &
-                nz = 0, nza, nzb, nzb_diff, nzt, nzta, nzt_diff
+    INTEGER ::  nbgp = 3, ngp_sums, nnx, nx = 0, nx_a, nx_o, nxa, nxl, nxlg,   &
+                nxlu, nxr, nxra, nxrg, nx_on_file, nny, ny = 0, ny_a, ny_o,    &
+                nya, nyn, nyna, nyng, nys, nysg, nysv, ny_on_file, nnz, nz = 0,&
+                nza, nzb, nzb_diff, nzt, nzta, nzt_diff
+
 
     INTEGER( KIND = SELECTED_INT_KIND(18) ), DIMENSION(:), ALLOCATABLE ::      &
@@ -771,5 +829,6 @@
                 ngp_2dh, nnx_pe, nny_pe, nxl_mg, nxr_mg, nyn_mg, nys_mg, nzt_mg
 
-    INTEGER, DIMENSION(:,:), ALLOCATABLE ::                                    &
+
+    INTEGER, DIMENSION(:,:), ALLOCATABLE :: boundary_flags,                    &
                 ngp_2dh_outer, ngp_2dh_s_inner, mg_loc_ind, nzb_diff_s_inner,  &
                 nzb_diff_s_outer, nzb_diff_u, nzb_diff_v, nzb_inner, nzb_outer,&
@@ -1157,12 +1216,13 @@
 
     INTEGER ::  comm1dx, comm1dy, comm2d, comm_inter, comm_palm, ierr, myidx,  &
-                myidy, ndim = 2, ngp_xy, ngp_y, pleft, pnorth, pright, psouth, &
+                myidy, ndim = 2, ngp_a, ngp_o, ngp_xy, ngp_y,                  &
+                pleft, pnorth, pright, psouth,                                 &
                 sendrecvcount_xy, sendrecvcount_yz, sendrecvcount_zx,          &
                 sendrecvcount_zyd, sendrecvcount_yxd,                          &
-                type_x, type_x_int, type_xy
+                type_x, type_x_int, type_xy, type_y, type_y_int
 
     INTEGER ::  ibuf(12), pcoord(2), pdims(2), status(MPI_STATUS_SIZE)
 
-    INTEGER, DIMENSION(:), ALLOCATABLE ::  ngp_yz, type_xz
+    INTEGER, DIMENSION(:), ALLOCATABLE ::  ngp_yz, type_xz, type_yz
 
     LOGICAL ::  collective_wait = .FALSE., reorder = .TRUE.
@@ -1325,6 +1385,12 @@
     LOGICAL ::  flow_statistics_called = .FALSE.
     REAL ::     u_max, v_max, w_max
-    REAL, DIMENSION(:), ALLOCATABLE       ::  sums_divnew_l, sums_divold_l
-    REAL, DIMENSION(:,:), ALLOCATABLE     ::  sums, sums_wsts_bc_l, ts_value
+    REAL, DIMENSION(:), ALLOCATABLE       ::  sums_divnew_l, sums_divold_l, &
+                                              weight_substep
+    REAL, DIMENSION(:,:), ALLOCATABLE     ::  sums, sums_wsts_bc_l,        &
+                                              sums_wsus_ws_l, sums_wsvs_ws_l,&
+                                              sums_us2_ws_l, sums_vs2_ws_l, &
+                                              sums_ws2_ws_l,                 &
+                                              sums_wspts_ws_l, sums_wssas_ws_l, &
+                                              sums_wsqs_ws_l, ts_value
     REAL, DIMENSION(:,:,:), ALLOCATABLE   ::  hom_sum, rmask, spectrum_x, &
                                               spectrum_y, sums_l, sums_l_l, &
Index: /palm/trunk/SOURCE/palm.f90
===================================================================
--- /palm/trunk/SOURCE/palm.f90	(revision 666)
+++ /palm/trunk/SOURCE/palm.f90	(revision 667)
@@ -142,4 +142,5 @@
     CALL check_parameters
 
+
 !
 !-- Initialize all necessary variables
@@ -185,5 +186,4 @@
 !-- If required, repeat output of header including the required CPU-time
     IF ( myid == 0 )  CALL header
-
 !
 !-- If required, final user-defined actions, and
Index: /palm/trunk/SOURCE/parin.f90
===================================================================
--- /palm/trunk/SOURCE/parin.f90	(revision 666)
+++ /palm/trunk/SOURCE/parin.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! Steering parameter dissipation_control added in inipar. (commented out)
 !
 ! Former revisions:
@@ -124,9 +124,9 @@
              collective_wait, conserve_volume_flow, conserve_volume_flow_mode, &
              coupling_start_time, cthf, cut_spline_overshoot, &
-             cycle_mg, damp_level_1d, dissipation_1d, dp_external, dp_level_b, &
-             dp_smooth, dpdxy, drag_coefficient, dt, dt_pr_1d, &
-             dt_run_control_1d, dx, dy, dz, dz_max, dz_stretch_factor, & 
-             dz_stretch_level, e_init, e_min, end_time_1d, fft_method, &
-             galilei_transformation, grid_matching, humidity, &
+             cycle_mg, damp_level_1d, dissipation_1d, & !dissipation_control, &
+             dp_external, dp_level_b, dp_smooth, dpdxy, drag_coefficient, &
+             dt, dt_pr_1d, dt_run_control_1d, dx, dy, dz, dz_max, & 
+             dz_stretch_factor, dz_stretch_level, e_init, e_min, end_time_1d, &
+             fft_method, galilei_transformation, grid_matching, humidity, &
              inflow_damping_height, inflow_damping_width, &
              inflow_disturbance_begin, inflow_disturbance_end, &
@@ -190,5 +190,5 @@
     NAMELIST /envpar/  host, local_dvrserver_running, maximum_cpu_time_allowed,  &
                        revision, return_addres, return_username, run_identifier, &
-                       tasks_per_node, write_binary
+                       tasks_per_node, write_binary                     
 
 !
@@ -278,4 +278,5 @@
                  hom(0:nz+1,2,pr_palm+max_pr_user,0:statistic_regions),        &
                  hom_sum(0:nz+1,pr_palm+max_pr_user,0:statistic_regions) )
+
        hom = 0.0
 
Index: /palm/trunk/SOURCE/poisfft.f90
===================================================================
--- /palm/trunk/SOURCE/poisfft.f90	(revision 666)
+++ /palm/trunk/SOURCE/poisfft.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! ddzu replaced by ddzu_pres due to changes in zu(0).
 !
 ! Former revisions:
@@ -287,6 +287,6 @@
        DO  k = 0, nz-1
           DO  i = nxl_z, nxr_z
-             tri(2,i,k) = ddzu(k+1) * ddzw(k+1)
-             tri(3,i,k) = ddzu(k+2) * ddzw(k+1)
+             tri(2,i,k) = ddzu_pres(k+1) * ddzw(k+1)
+             tri(3,i,k) = ddzu_pres(k+2) * ddzw(k+1)
           ENDDO
        ENDDO
@@ -367,6 +367,6 @@
              ENDIF
              DO  k = 0,nz-1
-                a = -1.0 * ddzu(k+2) * ddzw(k+1)
-                c = -1.0 * ddzu(k+1) * ddzw(k+1)
+                a = -1.0 * ddzu_pres(k+2) * ddzw(k+1)
+                c = -1.0 * ddzu_pres(k+1) * ddzw(k+1)
                 tri(1,i,k) = a + c - ll(i)
              ENDDO
@@ -379,6 +379,6 @@
                            ( dy * dy )
              DO  k = 0, nz-1
-                a = -1.0 * ddzu(k+2) * ddzw(k+1)
-                c = -1.0 * ddzu(k+1) * ddzw(k+1)
+                a = -1.0 * ddzu_pres(k+2) * ddzw(k+1)
+                c = -1.0 * ddzu_pres(k+1) * ddzw(k+1)
                 tri(1,i,k) = a + c - ll(i)
                 IF ( i >= 1 .and. i < nnxh )  THEN
@@ -1363,6 +1363,6 @@
        DO  k = 0, nz-1
           DO  i = 0,nx
-             tri(2,i,k) = ddzu(k+1) * ddzw(k+1)
-             tri(3,i,k) = ddzu(k+2) * ddzw(k+1)
+             tri(2,i,k) = ddzu_pres(k+1) * ddzw(k+1)
+             tri(3,i,k) = ddzu_pres(k+2) * ddzw(k+1)
           ENDDO
        ENDDO
@@ -1439,6 +1439,6 @@
           DO  k = 0, nz-1
              DO  i = 0, nx
-                a = -1.0 * ddzu(k+2) * ddzw(k+1)
-                c = -1.0 * ddzu(k+1) * ddzw(k+1)
+                a = -1.0 * ddzu_pres(k+2) * ddzw(k+1)
+                c = -1.0 * ddzu_pres(k+1) * ddzw(k+1)
                 tri(1,i,k) = a + c - l(i)
              ENDDO
Index: /palm/trunk/SOURCE/poisfft_hybrid.f90
===================================================================
--- /palm/trunk/SOURCE/poisfft_hybrid.f90	(revision 666)
+++ /palm/trunk/SOURCE/poisfft_hybrid.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions: 
 ! ----------------- 
-! 
+! ddzu replaced by ddzu_pres due to changes in zu(0).
 !
 ! Former revisions: 
@@ -848,6 +848,6 @@
        DO  k = 0, nz-1
           DO  i = 0,nx
-             tri(2,i,k) = ddzu(k+1) * ddzw(k+1)
-             tri(3,i,k) = ddzu(k+2) * ddzw(k+1)
+             tri(2,i,k) = ddzu_pres(k+1) * ddzw(k+1)
+             tri(3,i,k) = ddzu_pres(k+2) * ddzw(k+1)
           ENDDO
        ENDDO
@@ -908,6 +908,6 @@
           DO  k = 0,nz-1
              DO  i = 0, nx
-                a = -1.0 * ddzu(k+2) * ddzw(k+1)
-                c = -1.0 * ddzu(k+1) * ddzw(k+1)
+                a = -1.0 * ddzu_pres(k+2) * ddzw(k+1)
+                c = -1.0 * ddzu_pres(k+1) * ddzw(k+1)
                 tri(1,i,k) = a + c - l(i)
              ENDDO
Index: /palm/trunk/SOURCE/poismg.f90
===================================================================
--- /palm/trunk/SOURCE/poismg.f90	(revision 666)
+++ /palm/trunk/SOURCE/poismg.f90	(revision 667)
@@ -8,5 +8,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! Calls of exchange_horiz are modified.
 !
 ! Former revisions:
@@ -70,5 +70,4 @@
     CALL cpu_log( log_point_s(29), 'poismg', 'start' )
 
-
 !
 !-- Initialize arrays and variables used in this subroutine
@@ -78,5 +77,5 @@
 !
 !-- Some boundaries have to be added to divergence array
-    CALL exchange_horiz( d )
+    CALL exchange_horiz( d, 1)
     d(nzb,:,:) = d(nzb+1,:,:)
 
@@ -220,5 +219,5 @@
 !
 !-- Horizontal boundary conditions
-    CALL exchange_horiz( r )
+    CALL exchange_horiz( r, 1)
 
     IF ( bc_lr /= 'cyclic' )  THEN
@@ -393,5 +392,5 @@
 !
 !-- Horizontal boundary conditions
-    CALL exchange_horiz( f_mg )
+    CALL exchange_horiz( f_mg, 1)
 
     IF ( bc_lr /= 'cyclic' )  THEN
@@ -491,5 +490,5 @@
 !
 !-- Horizontal boundary conditions
-    CALL exchange_horiz( temp )
+    CALL exchange_horiz( temp, 1)
 
     IF ( bc_lr /= 'cyclic' )  THEN
@@ -861,5 +860,5 @@
 !
 !--       Horizontal boundary conditions
-          CALL exchange_horiz( p_mg )
+          CALL exchange_horiz( p_mg, 1 )
 
           IF ( bc_lr /= 'cyclic' )  THEN
@@ -936,5 +935,5 @@
 !
 !-- One more time horizontal boundary conditions
-    CALL exchange_horiz( p_mg )
+    CALL exchange_horiz( p_mg, 1)
 
  END SUBROUTINE redblack
Index: /palm/trunk/SOURCE/prandtl_fluxes.f90
===================================================================
--- /palm/trunk/SOURCE/prandtl_fluxes.f90	(revision 666)
+++ /palm/trunk/SOURCE/prandtl_fluxes.f90	(revision 667)
@@ -4,5 +4,9 @@
 ! Current revisions:
 ! -----------------
-! 
+!
+! Changed surface boundary conditions for u and v from mirror bc to dirichelt bc,
+! therefore u(uzb,:,:) and v(nzb,:,:) is now representative for the height z0
+!
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng
 !
 ! Former revisions:
@@ -53,5 +57,5 @@
     REAL    ::  a, b, e_q, rifm, uv_total, z_p
 
-!
+! 
 !-- Compute theta*
     IF ( constant_heatflux )  THEN
@@ -60,6 +64,6 @@
 !--    for u* use the value from the previous time step
        !$OMP PARALLEL DO
-       DO  i = nxl-1, nxr+1
-          DO  j = nys-1, nyn+1
+       DO  i = nxlg, nxrg
+          DO  j = nysg, nyng
              ts(j,i) = -shf(j,i) / ( us(j,i) + 1E-30 )
 !
@@ -76,6 +80,6 @@
 !--    (the Richardson number is still the one from the previous time step)
        !$OMP PARALLEL DO PRIVATE( a, b, k, z_p )
-       DO  i = nxl-1, nxr+1
-          DO  j = nys-1, nyn+1
+       DO  i = nxlg, nxrg
+          DO  j = nysg, nyng
 
              k   = nzb_s_inner(j,i)
@@ -108,6 +112,6 @@
     IF ( .NOT. humidity )  THEN
        !$OMP PARALLEL DO PRIVATE( k, z_p )
-       DO  i = nxl-1, nxr+1
-          DO  j = nys-1, nyn+1
+       DO  i = nxlg, nxrg
+          DO  j = nysg, nyng
              k   = nzb_s_inner(j,i)
              z_p = zu(k+1) - zw(k)
@@ -126,6 +130,6 @@
     ELSE
        !$OMP PARALLEL DO PRIVATE( k, z_p )
-       DO  i = nxl-1, nxr+1
-          DO  j = nys-1, nyn+1
+       DO  i = nxlg, nxrg
+          DO  j = nysg, nyng
              k   = nzb_s_inner(j,i)
              z_p = zu(k+1) - zw(k)
@@ -155,8 +159,11 @@
 
 !
-!--       Compute the absolute value of the horizontal velocity
-          uv_total = SQRT( ( 0.5 * ( u(k+1,j,i) + u(k+1,j,i+1) ) )**2 + &
-                           ( 0.5 * ( v(k+1,j,i) + v(k+1,j+1,i) ) )**2   &
-                         )
+!--       Compute the absolute value of the horizontal velocity 
+!--       (relative to the surface)
+          uv_total = SQRT( ( 0.5 * ( u(k+1,j,i) + u(k+1,j,i+1)        &
+                                   - u(k,j,i)   - u(k,j,i+1) ) )**2 + &
+                           ( 0.5 * ( v(k+1,j,i) + v(k+1,j+1,i)        &
+                                   - v(k,j,i)   - v(k,j+1,i) ) )**2 )    
+
 
           IF ( rif(j,i) >= 0.0 )  THEN
@@ -203,5 +210,5 @@
 !
 !--          Stable stratification
-             usws(j,i) = kappa * u(k+1,j,i) / (                           &
+             usws(j,i) = kappa * ( u(k+1,j,i) - u(k,j,i) )/ (              &
                                      LOG( z_p / z0(j,i) ) +               &
                                      5.0 * rifm * ( z_p - z0(j,i) ) / z_p &
@@ -213,5 +220,5 @@
              b = SQRT( SQRT( 1.0 - 16.0 * rifm / z_p * z0(j,i) ) )
 
-             usws(j,i) = kappa * u(k+1,j,i) / (                           &
+             usws(j,i) = kappa * ( u(k+1,j,i) - u(k,j,i) ) / (            &
                          LOG( z_p / z0(j,i) ) -                           &
                          LOG( (1.0 + a )**2 * ( 1.0 + a**2 ) / (          &
@@ -240,5 +247,5 @@
 !
 !--          Stable stratification
-             vsws(j,i) = kappa * v(k+1,j,i) / (                           &
+             vsws(j,i) = kappa * ( v(k+1,j,i) -  v(k,j,i) ) / (           &
                                      LOG( z_p / z0(j,i) ) +               &
                                      5.0 * rifm * ( z_p - z0(j,i) ) / z_p &
@@ -250,5 +257,5 @@
              b = SQRT( SQRT( 1.0 - 16.0 * rifm / z_p * z0(j,i) ) )
 
-             vsws(j,i) = kappa * v(k+1,j,i) / (                           &
+             vsws(j,i) = kappa * ( v(k+1,j,i) - v(k,j,i) ) / (            &
                          LOG( z_p / z0(j,i) ) -                           &
                          LOG( (1.0 + a )**2 * ( 1.0 + a**2 ) / (          &
@@ -268,6 +275,6 @@
 !--       For a given water flux in the Prandtl layer:
           !$OMP PARALLEL DO
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
                 qs(j,i) = -qsws(j,i) / ( us(j,i) + 1E-30 )
              ENDDO
@@ -276,6 +283,6 @@
        ELSE          
           !$OMP PARALLEL DO PRIVATE( a, b, k, z_p )
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
 
                 k   = nzb_s_inner(j,i)
@@ -325,6 +332,6 @@
     IF ( .NOT. constant_heatflux )  THEN
        !$OMP PARALLEL DO
-       DO  i = nxl-1, nxr+1
-          DO  j = nys-1, nyn+1
+       DO  i = nxlg, nxrg
+          DO  j = nysg, nyng
              shf(j,i) = -ts(j,i) * us(j,i)
           ENDDO
@@ -336,6 +343,6 @@
     IF ( .NOT. constant_waterflux .AND. ( humidity .OR. passive_scalar ) ) THEN
        !$OMP PARALLEL DO
-       DO  i = nxl-1, nxr+1
-          DO  j = nys-1, nyn+1
+       DO  i = nxlg, nxrg
+          DO  j = nysg, nyng
              qsws(j,i) = -qs(j,i) * us(j,i)
           ENDDO
@@ -347,6 +354,6 @@
     IF ( ibc_e_b == 2 )  THEN
        !$OMP PARALLEL DO
-       DO  i = nxl-1, nxr+1
-          DO  j = nys-1, nyn+1
+       DO  i = nxlg, nxrg
+          DO  j = nysg, nyng
              e(nzb_s_inner(j,i)+1,j,i) = ( us(j,i) / 0.1 )**2
 !
Index: /palm/trunk/SOURCE/pres.f90
===================================================================
--- /palm/trunk/SOURCE/pres.f90	(revision 666)
+++ /palm/trunk/SOURCE/pres.f90	(revision 667)
@@ -4,5 +4,15 @@
 ! Current revisions:
 ! -----------------
-! 
+! New allocation of tend when ws-scheme and multigrid is used. This is due to 
+! reasons of perforance of the data_exchange. The same is done with p after
+! poismg is called.
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng when no 
+! multigrid is used. Calls of exchange_horiz are modified.
+!
+! bugfix: After pressure correction no volume flow correction in case of 
+! non-cyclic boundary conditions 
+! (has to be done only before pressure correction)
+!
+! Call of SOR routine is referenced with ddzu_pres.
 !
 ! Former revisions:
@@ -74,8 +84,20 @@
 
 !
-!-- Multigrid method needs additional grid points for the divergence array
+!-- Multigrid method expects 1 additional grid point for the arrays 
+!-- d, tend and p
     IF ( psolver == 'multigrid' )  THEN
+     
        DEALLOCATE( d )
-       ALLOCATE( d(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+       ALLOCATE( d(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) ) 
+       
+       IF ( ws_scheme_mom  .OR. ws_scheme_sca )  THEN
+       
+          DEALLOCATE( tend )
+          ALLOCATE( tend(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+          DEALLOCATE( p )
+          ALLOCATE( p(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+          
+       ENDIF
+       
     ENDIF
 
@@ -103,5 +125,5 @@
 !--       Sum up the volume flow through the south/north boundary
           DO  k = nzb_2d(j,i) + 1, nzt
-             volume_flow_l(1) = volume_flow_l(1) + u(k,j,i) * dzu(k)
+             volume_flow_l(1) = volume_flow_l(1) + u(k,j,i) * dzw(k)
           ENDDO
        ENDDO
@@ -117,6 +139,6 @@
                                / volume_flow_area(1)
 
-       DO  j = nys-1, nyn+1
-          DO  k = nzb_v_inner(j,i) + 1, nzt
+       DO  j = nysg, nyng
+          DO  k = nzb_2d(j,i) + 1, nzt
              u(k,j,i) = u(k,j,i) + volume_flow_offset(1)
           ENDDO
@@ -142,5 +164,5 @@
 !--       Sum up the volume flow through the south/north boundary
           DO  k = nzb_2d(j,i) + 1, nzt
-             volume_flow_l(2) = volume_flow_l(2) + v(k,j,i) * dzu(k)
+             volume_flow_l(2) = volume_flow_l(2) + v(k,j,i) * dzw(k)
           ENDDO
        ENDDO
@@ -156,5 +178,5 @@
                                / volume_flow_area(2)
 
-       DO  i = nxl-1, nxr+1
+       DO  i = nxlg, nxrg
           DO  k = nzb_v_inner(j,i) + 1, nzt
              v(k,j,i) = v(k,j,i) + volume_flow_offset(2)
@@ -186,6 +208,6 @@
              w_l(k) = w_l(k) / ngp_2dh_outer(k,0)
           ENDDO
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
                 DO  k = nzb_w_inner(j,i)+1, nzt
                    w(k,j,i) = w(k,j,i) - w_l(k)
@@ -267,7 +289,7 @@
           DO  j = nys, nyn
              DO  k = nzb_s_inner(j,i)+1, nzt
-                d(k,j,i) = ( ( u(k,j,i+1) - u(k,j,i) ) * ddx + &
-                             ( v(k,j+1,i) - v(k,j,i) ) * ddy + &
-                             ( w(k,j,i) - w(k-1,j,i) ) * ddzw(k) ) * ddt_3d
+             d(k,j,i) = ( ( u(k,j,i+1) - u(k,j,i) ) * ddx + &
+                          ( v(k,j+1,i) - v(k,j,i) ) * ddy + &
+                          ( w(k,j,i) - w(k-1,j,i) ) * ddzw(k) ) * ddt_3d
              ENDDO
           ENDDO
@@ -298,6 +320,6 @@
              DO  k = nzb_s_inner(j,i)+1, nzt
                 d(k,j,i) = ( ( u(k,j,i+1) - u(k,j,i) ) * ddx + &
-                             ( v(k,j+1,i) - v(k,j,i) ) * ddy + &
-                             ( w(k,j,i) - w(k-1,j,i) ) * ddzw(k) ) * ddt_3d
+                          ( v(k,j+1,i) - v(k,j,i) ) * ddy + &
+                          ( w(k,j,i) - w(k-1,j,i) ) * ddzw(k) ) * ddt_3d
              ENDDO
           ENDDO
@@ -331,5 +353,5 @@
 !-- comment line)
 !    CALL global_min_max( nzb+1, nzt, nys, nyn, nxl, nxr, d, 'abs', divmax, &
-!                         divmax_ijk )
+!                        divmax_ijk )
 
     CALL cpu_log( log_point_s(1), 'divergence', 'stop' )
@@ -364,5 +386,5 @@
        IF ( nxra > nxr  .OR.  nyna > nyn  .OR.  nza > nz )  THEN
           DEALLOCATE( tend )
-          ALLOCATE( tend(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+          ALLOCATE( tend(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
        ENDIF
 
@@ -387,6 +409,6 @@
 !--       Neumann (dp/dz = 0)
           !$OMP PARALLEL DO
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
                 tend(nzb_s_inner(j,i),j,i) = tend(nzb_s_inner(j,i)+1,j,i)
              ENDDO
@@ -400,6 +422,6 @@
 !--       the computation (cf. above: computation of divergences).
           !$OMP PARALLEL DO
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
                 tend(nzb_s_inner(j,i),j,i) = tend(nzb_s_inner(j,i)+1,j,i)
              ENDDO
@@ -410,6 +432,6 @@
 !--       Dirichlet
           !$OMP PARALLEL DO
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
                 tend(nzb_s_inner(j,i),j,i) = 0.0
              ENDDO
@@ -424,6 +446,6 @@
 !--       Neumann
           !$OMP PARALLEL DO
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
                 tend(nzt+1,j,i) = tend(nzt,j,i)
              ENDDO
@@ -434,6 +456,6 @@
 !--       Dirichlet
           !$OMP PARALLEL DO
-          DO  i = nxl-1, nxr+1
-             DO  j = nys-1, nyn+1
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
                 tend(nzt+1,j,i) = 0.0
              ENDDO
@@ -444,5 +466,5 @@
 !
 !--    Exchange boundaries for p
-       CALL exchange_horiz( tend )
+       CALL exchange_horiz( tend, nbgp )
       
     ELSEIF ( psolver == 'sor' )  THEN
@@ -451,5 +473,5 @@
 !--    Solve Poisson equation for perturbation pressure using SOR-Red/Black
 !--    scheme
-       CALL sor( d, ddzu, ddzw, p )
+       CALL sor( d, ddzu_pres, ddzw, p )
        tend = p
 
@@ -458,11 +480,28 @@
 !
 !--    Solve Poisson equation for perturbation pressure using Multigrid scheme,
-!--    array tend is used to store the residuals
+!--    array tend is used to store the residuals, logical exchange_mg is used
+!--    to discern data exchange in multigrid ( 1 ghostpoint ) and normal grid
+!--    ( nbgp ghost points ).
+       exchange_mg = .TRUE.
        CALL poismg( tend )
- 
+       exchange_mg = .FALSE.
 !
 !--    Restore perturbation pressure on tend because this array is used
 !--    further below to correct the velocity fields
+
        tend = p
+       IF( ws_scheme_mom .OR. ws_scheme_sca )  THEN
+!       
+!--       Allocate p to its normal size and restore pressure.         
+          DEALLOCATE( p )
+          ALLOCATE( p(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+          DO  i = nxl, nxr
+             DO  j = nys, nyn
+                DO  k = nzb_s_inner(j,i), nzt
+                   p(k,j,i) = tend(k,j,i)
+                ENDDO
+             ENDDO
+          ENDDO
+       ENDIF
 
     ENDIF
@@ -476,8 +515,8 @@
 !--    optimization
        !$OMP PARALLEL DO
-       DO  j = nys-1, nyn+1
+       DO  j = nysg, nyng
           DO  k = nzb, nzt+1
-             p(k,j,nxl-1) = tend(k,j,nxl-1)
-             p(k,j,nxr+1) = tend(k,j,nxr+1)
+             p(k,j,nxlg:nxl-1) = tend(k,j,nxlg:nxl-1)
+             p(k,j,nxr+1:nxrg) = tend(k,j,nxr+1:nxrg)
           ENDDO
        ENDDO
@@ -496,5 +535,5 @@
     DO  i = nxl, nxr
        IF ( psolver(1:7) == 'poisfft' )  THEN
-          DO  j = nys-1, nyn+1
+          DO  j = nysg, nyng
              DO  k = nzb, nzt+1
                 p(k,j,i) = tend(k,j,i)
@@ -517,16 +556,16 @@
 !--       Sum up the volume flow through the right and north boundary
           IF ( conserve_volume_flow  .AND.  bc_lr == 'cyclic'  .AND. &
-               i == nx )  THEN
+               bc_ns == 'cyclic' .AND. i == nx )  THEN
              !$OMP CRITICAL
              DO  k = nzb_2d(j,i) + 1, nzt
-                volume_flow_l(1) = volume_flow_l(1) + u(k,j,i) * dzu(k)
+                volume_flow_l(1) = volume_flow_l(1) + u(k,j,i) * dzw(k)
              ENDDO
              !$OMP END CRITICAL
           ENDIF
           IF ( conserve_volume_flow  .AND.  bc_ns == 'cyclic'  .AND. &
-               j == ny )  THEN
+               bc_lr == 'cyclic' .AND. j == ny )  THEN
              !$OMP CRITICAL
              DO  k = nzb_2d(j,i) + 1, nzt
-                volume_flow_l(2) = volume_flow_l(2) + v(k,j,i) * dzu(k)
+                volume_flow_l(2) = volume_flow_l(2) + v(k,j,i) * dzw(k)
              ENDDO
              !$OMP END CRITICAL
@@ -538,7 +577,15 @@
 
 !
+!-- Resize tend to its normal size in case of multigrid and ws-scheme.
+    IF ( psolver == 'multigrid' .AND. ( ws_scheme_mom        &
+                                   .OR. ws_scheme_sca ) )  THEN
+       DEALLOCATE( tend )
+       ALLOCATE( tend(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+    ENDIF
+
+!
 !-- Conserve the volume flow
     IF ( conserve_volume_flow  .AND. &
-         ( bc_lr == 'cyclic'  .OR.  bc_ns == 'cyclic' ) )  THEN
+         ( bc_lr == 'cyclic'  .AND.  bc_ns == 'cyclic' ) )  THEN
 
 #if defined( __parallel )   
@@ -557,16 +604,11 @@
        DO  i = nxl, nxr
           DO  j = nys, nyn
-             IF ( bc_lr == 'cyclic' )  THEN
-                DO  k = nzb_u_inner(j,i) + 1, nzt
-                   u(k,j,i) = u(k,j,i) + volume_flow_offset(1)
-                ENDDO
-             ENDIF
-             IF ( bc_ns == 'cyclic' )  THEN
-                DO  k = nzb_v_inner(j,i) + 1, nzt
-                   v(k,j,i) = v(k,j,i) + volume_flow_offset(2)
-                ENDDO
-             ENDIF
-          ENDDO
-       ENDDO
+             DO  k = nzb_u_inner(j,i) + 1, nzt
+                u(k,j,i) = u(k,j,i) + volume_flow_offset(1)
+                v(k,j,i) = v(k,j,i) + volume_flow_offset(2)
+             ENDDO
+          ENDDO
+       ENDDO
+
        !$OMP END PARALLEL
 
@@ -575,7 +617,7 @@
 !
 !-- Exchange of boundaries for the velocities
-    CALL exchange_horiz( u )
-    CALL exchange_horiz( v )
-    CALL exchange_horiz( w )
+    CALL exchange_horiz( u, nbgp )
+    CALL exchange_horiz( v, nbgp )
+    CALL exchange_horiz( w, nbgp )
 
 !
@@ -620,4 +662,5 @@
     ENDDO
 #endif
+
     localsum = localsum + threadsum
     !$OMP END PARALLEL
@@ -631,4 +674,5 @@
 
     CALL cpu_log( log_point(8), 'pres', 'stop' )
+    
 
 
Index: /palm/trunk/SOURCE/production_e.f90
===================================================================
--- /palm/trunk/SOURCE/production_e.f90	(revision 666)
+++ /palm/trunk/SOURCE/production_e.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng
 !
 ! Former revisions:
@@ -1109,6 +1109,6 @@
 
           IF ( first_call )  THEN
-             ALLOCATE( u_0(nys-1:nyn+1,nxl-1:nxr+1), &
-                       v_0(nys-1:nyn+1,nxl-1:nxr+1) )
+             ALLOCATE( u_0(nysg:nyng,nxlg:nxrg), &
+                       v_0(nysg:nyng,nxlg:nxrg) )
              first_call = .FALSE.
           ENDIF
Index: /palm/trunk/SOURCE/prognostic_equations.f90
===================================================================
--- /palm/trunk/SOURCE/prognostic_equations.f90	(revision 666)
+++ /palm/trunk/SOURCE/prognostic_equations.f90	(revision 667)
@@ -4,5 +4,7 @@
 ! Current revisions:
 ! -----------------
-!
+! Calls of the advection routines with WS5 added.
+! Calls of ws_statistics added to set the statistical arrays to zero after each 
+! time step.
 !
 ! Former revisions:
@@ -79,5 +81,5 @@
     USE pointer_interfaces
     USE statistics
-
+    USE advec_ws
     USE advec_s_pw_mod
     USE advec_s_up_mod
@@ -144,4 +146,6 @@
     IF ( ocean    )  CALL calc_mean_profile( rho, 64 )
     IF ( humidity )  CALL calc_mean_profile( vpt, 44 )
+    IF ( ( ws_scheme_mom .OR. ws_scheme_sca ) .AND. &
+       intermediate_timestep_count == 1 )  CALL ws_statistics
 
 !
@@ -164,5 +168,10 @@
           IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' )  THEN
              tend(:,j,i) = 0.0
-             CALL advec_u_pw( i, j )
+             IF ( ws_scheme_mom )  THEN
+                 CALL advec_u_ws( i, j )
+             ELSE
+                 CALL advec_u_pw( i, j )
+             ENDIF
+
           ELSE
              IF ( momentum_advec /= 'ups-scheme' )  THEN
@@ -245,5 +254,10 @@
           IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' )  THEN
              tend(:,j,i) = 0.0
-             CALL advec_v_pw( i, j )
+             IF ( ws_scheme_mom )  THEN
+                 CALL advec_v_ws( i, j )
+             ELSE 
+                 CALL advec_v_pw( i, j )
+             ENDIF
+
           ELSE
              IF ( momentum_advec /= 'ups-scheme' )  THEN
@@ -325,5 +339,10 @@
           IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' )  THEN
              tend(:,j,i) = 0.0
-             CALL advec_w_pw( i, j )
+             IF ( ws_scheme_mom )  THEN
+                 CALL advec_w_ws( i, j )
+             ELSE  
+                 CALL advec_w_pw( i, j )
+             ENDIF
+
           ELSE
              IF ( momentum_advec /= 'ups-scheme' )  THEN
@@ -425,5 +444,10 @@
              IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' )  THEN
                 tend(:,j,i) = 0.0
-                CALL advec_s_pw( i, j, pt )
+                IF ( ws_scheme_sca )  THEN
+                   CALL advec_s_ws( i, j, pt, 'pt', flux_s_pt, &
+                                 diss_s_pt, flux_l_pt, diss_l_pt )
+                ELSE
+                    CALL advec_s_pw( i, j, pt )
+                ENDIF
              ELSE
                 IF ( scalar_advec /= 'ups-scheme' )  THEN
@@ -541,5 +565,11 @@
                 IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' ) THEN
                    tend(:,j,i) = 0.0
-                   CALL advec_s_pw( i, j, sa )
+                   IF ( ws_scheme_sca )  THEN
+                       CALL advec_s_ws( i, j, sa, 'sa', flux_s_sa,  &
+                                    diss_s_sa, flux_l_sa, diss_l_sa )
+                   ELSE
+                       CALL advec_s_pw( i, j, sa )
+                   ENDIF
+
                 ELSE
                    IF ( scalar_advec /= 'ups-scheme' )  THEN
@@ -634,5 +664,10 @@
                 IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' ) THEN
                    tend(:,j,i) = 0.0
-                   CALL advec_s_pw( i, j, q )
+                   IF ( ws_scheme_sca )  THEN
+                       CALL advec_s_ws( i, j, q, 'q', flux_s_q, &
+                                   diss_s_q, flux_l_q, diss_l_q )
+                   ELSE
+                       CALL advec_s_pw( i, j, q )
+                   ENDIF
                 ELSE
                    IF ( scalar_advec /= 'ups-scheme' )  THEN
@@ -661,5 +696,5 @@
 !--          Sink or source of scalar concentration due to canopy elements
              IF ( plant_canopy ) CALL plant_canopy_model( i, j, 5 )
-
+             
 !
 !--          If required compute influence of large-scale subsidence/ascent
@@ -769,5 +804,10 @@
                    THEN
                       tend(:,j,i) = 0.0
-                      CALL advec_s_pw( i, j, e )
+                      IF ( ws_scheme_sca )  THEN
+                          CALL advec_s_ws( i, j, e, 'e', flux_s_e, & 
+                                      diss_s_e, flux_l_e, diss_l_e )
+                      ELSE
+                          CALL advec_s_pw( i, j, e )
+                      ENDIF
                    ELSE
                       IF ( scalar_advec /= 'ups-scheme' )  THEN
@@ -884,4 +924,6 @@
     IF ( humidity )  CALL calc_mean_profile( vpt, 44 )
     IF ( .NOT. constant_diffusion )  CALL production_e_init
+    IF ( ( ws_scheme_mom .OR. ws_scheme_sca ) .AND. &
+       intermediate_timestep_count == 1 )  CALL ws_statistics
 
 
@@ -898,6 +940,11 @@
              tend(:,j,i) = 0.0
              IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' )  THEN
-                CALL advec_u_pw( i, j )
-             ELSE
+                IF ( ws_scheme_mom )  THEN
+    !               CALL local_diss( i, j, u)    ! dissipation control
+                   CALL advec_u_ws( i, j )
+                ELSE 
+                   CALL advec_u_pw( i, j )
+                ENDIF 
+            ELSE
                 CALL advec_u_up( i, j )
              ENDIF
@@ -962,5 +1009,10 @@
              tend(:,j,i) = 0.0
              IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' )  THEN
-                CALL advec_v_pw( i, j )
+                IF ( ws_scheme_mom )  THEN
+                 !   CALL local_diss( i, j, v)
+                    CALL advec_v_ws( i, j )
+                ELSE 
+                    CALL advec_v_pw( i, j )
+                ENDIF
              ELSE
                 CALL advec_v_up( i, j )
@@ -1022,5 +1074,10 @@
           tend(:,j,i) = 0.0
           IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' )  THEN
-             CALL advec_w_pw( i, j )
+             IF ( ws_scheme_mom )  THEN
+             !   CALL local_diss( i, j, w)
+                CALL advec_w_ws( i, j )
+             ELSE 
+                CALL advec_w_pw( i, j )
+             END IF
           ELSE
              CALL advec_w_up( i, j )
@@ -1081,5 +1138,11 @@
           tend(:,j,i) = 0.0
           IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' )  THEN
-             CALL advec_s_pw( i, j, pt )
+                IF ( ws_scheme_sca )  THEN
+       !            CALL local_diss( i, j, pt )
+                   CALL advec_s_ws( i, j, pt, 'pt', flux_s_pt, &
+                             diss_s_pt, flux_l_pt, diss_l_pt )
+                ELSE
+                   CALL advec_s_pw( i, j, pt )
+                ENDIF
           ELSE
              CALL advec_s_up( i, j, pt )
@@ -1119,4 +1182,5 @@
           ENDIF
 
+
           CALL user_actions( i, j, 'pt-tendency' )
 
@@ -1156,5 +1220,11 @@
              IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' ) &
              THEN
-                CALL advec_s_pw( i, j, sa )
+                IF ( ws_scheme_sca )  THEN
+            !        CALL local_diss( i, j, sa )
+                    CALL advec_s_ws( i, j, sa, 'sa', flux_s_sa,  &
+                                diss_s_sa, flux_l_sa, diss_l_sa  )
+                ELSE 
+                    CALL advec_s_pw( i, j, sa )
+                ENDIF
              ELSE
                 CALL advec_s_up( i, j, sa )
@@ -1208,5 +1278,11 @@
              IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' ) &
              THEN
-                CALL advec_s_pw( i, j, q )
+                IF ( ws_scheme_sca )  THEN
+          !         CALL local_diss( i, j, q )
+                   CALL advec_s_ws( i, j, q, 'q', flux_s_q, & 
+                                diss_s_q, flux_l_q, diss_l_q )
+                ELSE 
+                   CALL advec_s_pw( i, j, q )
+                ENDIF
              ELSE
                 CALL advec_s_up( i, j, q )
@@ -1232,10 +1308,8 @@
              IF ( plant_canopy ) CALL plant_canopy_model( i, j, 5 )
 
-
 !--          If required compute influence of large-scale subsidence/ascent
              IF ( large_scale_subsidence ) THEN
                 CALL subsidence ( i, j, tend, q, q_init )
              ENDIF
-
 
              CALL user_actions( i, j, 'q-tendency' )
@@ -1279,6 +1353,12 @@
              tend(:,j,i) = 0.0
              IF ( ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' )  &
-                  .AND.  .NOT. use_upstream_for_tke )  THEN
-                CALL advec_s_pw( i, j, e )
+                 .AND.  .NOT. use_upstream_for_tke )  THEN
+                 IF ( ws_scheme_sca )  THEN
+                 !    CALL local_diss( i, j, e )
+                     CALL advec_s_ws( i, j, e, 'e', flux_s_e, &
+                                diss_s_e, flux_l_e, diss_l_e  )
+                 ELSE
+                     CALL advec_s_pw( i, j, e )
+                 ENDIF
              ELSE
                 CALL advec_s_up( i, j, e )
@@ -1379,4 +1459,6 @@
     IF ( ocean    )  CALL calc_mean_profile( rho, 64 )
     IF ( humidity )  CALL calc_mean_profile( vpt, 44 )
+    IF ( ( ws_scheme_mom .OR. ws_scheme_sca ) .AND. &
+       intermediate_timestep_count == 1 )  CALL ws_statistics
 
 !
@@ -1395,5 +1477,9 @@
     IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' )  THEN
        tend = 0.0
-       CALL advec_u_pw
+       IF ( ws_scheme_mom )  THEN
+          CALL advec_u_ws
+       ELSE
+          CALL advec_u_pw
+       ENDIF
     ELSE
        IF ( momentum_advec /= 'ups-scheme' )  THEN
@@ -1484,5 +1570,9 @@
     IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' )  THEN
        tend = 0.0
-       CALL advec_v_pw
+       IF ( ws_scheme_mom )  THEN
+          CALL advec_v_ws
+       ELSE 
+          CALL advec_v_pw
+       END IF
     ELSE
        IF ( momentum_advec /= 'ups-scheme' )  THEN
@@ -1572,5 +1662,9 @@
     IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' )  THEN
        tend = 0.0
-       CALL advec_w_pw
+       IF ( ws_scheme_mom )  THEN
+          CALL advec_w_ws
+       ELSE
+          CALL advec_w_pw
+       ENDIF
     ELSE
        IF ( momentum_advec /= 'ups-scheme' )  THEN
@@ -1676,5 +1770,9 @@
        IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' )  THEN
           tend = 0.0
-          CALL advec_s_pw( pt )
+          IF ( ws_scheme_sca )  THEN
+             CALL advec_s_ws( pt, 'pt' )
+          ELSE
+             CALL advec_s_pw( pt )
+          ENDIF
        ELSE
           IF ( scalar_advec /= 'ups-scheme' )  THEN
@@ -1795,5 +1893,9 @@
           IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' )  THEN
              tend = 0.0
-             CALL advec_s_pw( sa )
+             IF ( ws_scheme_sca )  THEN
+                 CALL advec_s_ws( sa, 'sa' )
+             ELSE
+                 CALL advec_s_pw( sa )
+             ENDIF
           ELSE
              IF ( scalar_advec /= 'ups-scheme' )  THEN
@@ -1894,5 +1996,9 @@
           IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' )  THEN
              tend = 0.0
-             CALL advec_s_pw( q )
+             IF ( ws_scheme_sca )  THEN
+                CALL advec_s_ws( q, 'q' )
+             ELSE
+                CALL advec_s_pw( q )
+             ENDIF
           ELSE
              IF ( scalar_advec /= 'ups-scheme' )  THEN
@@ -1920,5 +2026,5 @@
 !--    Sink or source of scalar concentration due to canopy elements
        IF ( plant_canopy ) CALL plant_canopy_model( 5 )
-
+       
 !
 !--    If required compute influence of large-scale subsidence/ascent
@@ -2029,5 +2135,9 @@
              IF ( tsc(2) == 2.0  .OR.  timestep_scheme(1:5) == 'runge' )  THEN
                 tend = 0.0
-                CALL advec_s_pw( e )
+                IF ( ws_scheme_sca )  THEN
+                   CALL advec_s_ws( e, 'e' )
+                ELSE 
+                   CALL advec_s_pw( e )
+                ENDIF
              ELSE
                 IF ( scalar_advec /= 'ups-scheme' )  THEN
Index: /palm/trunk/SOURCE/read_3d_binary.f90
===================================================================
--- /palm/trunk/SOURCE/read_3d_binary.f90	(revision 666)
+++ /palm/trunk/SOURCE/read_3d_binary.f90	(revision 667)
@@ -4,5 +4,7 @@
 ! Current revisions:
 ! -----------------
-! 
+! +/- 1 replaced with +/- nbgp when swapping and allocating variables.
+! Bugfix: When using initializing_actions = 'cyclic_fill' in some cases
+! not the whole model domain was filled with data of the prerun.
 !
 ! Former revisions:
@@ -128,50 +130,49 @@
 !--    matches another time(s) in the current subdomain by shifting it
 !--    for nx_on_file+1, ny_on_file+1 respectively
-
+    
        shift_y = 0
-       j       = 0  ! counter for the number of files to be opened
-
-       DO WHILE ( nyspr+shift_y <= nyn-offset_y  .AND.  &
-                  nynpr+shift_y >= nys-offset_y )
-
-          shift_x = 0
-
-          DO WHILE ( nxlpr+shift_x <= nxr-offset_x  .AND. &
-                     nxrpr+shift_x >= nxl-offset_x )
-
-             j = j +1
-
-             IF ( j > 1000 )  THEN
-!
-!--             Array bound exceeded
-                message_string = 'data from subdomain of previous' // &
-                                 ' run mapped more than 1000 times'
-                CALL message( 'read_3d_binary', 'PA0284', 2, 2, -1, 6, 1 )
- 
-             ENDIF
-
-             IF ( j == 1 )  THEN
-                files_to_be_opened = files_to_be_opened + 1
-                file_list(files_to_be_opened) = i-1
-             ENDIF
-
-             offset_xa(files_to_be_opened,j) = offset_x + shift_x
-             offset_ya(files_to_be_opened,j) = offset_y + shift_y
-
-!
-!--          Index bounds of overlapping data
-             nxlfa(files_to_be_opened,j) = MAX( nxl-offset_x-shift_x, nxlpr )
-             nxrfa(files_to_be_opened,j) = MIN( nxr-offset_x-shift_x, nxrpr )
-             nysfa(files_to_be_opened,j) = MAX( nys-offset_y, nyspr )
-             nynfa(files_to_be_opened,j) = MIN( nyn-offset_y, nynpr )
-
-             shift_x = shift_x + ( nx_on_file + 1 )
-
-          ENDDO
-
-          shift_y = shift_y + ( ny_on_file + 1 )
-
+       j       = 0
+       DO WHILE (  nyspr+shift_y <= nyn-offset_y )
+          
+          IF ( nynpr+shift_y >= nys-offset_y ) THEN 
+
+             shift_x = 0
+             DO WHILE ( nxlpr+shift_x <= nxr-offset_x )
+                
+                IF ( nxrpr+shift_x >= nxl-offset_x ) THEN
+                   j = j +1
+                   IF ( j > 1000 )  THEN
+!
+!--                   Array bound exceeded
+                      message_string = 'data from subdomain of previous' // &
+                                       ' run mapped more than 1000 times'
+                      CALL message( 'read_3d_binary', 'PA0284', 2, 2, -1,   &
+                                       6, 1 )
+                   ENDIF
+
+                   IF ( j == 1 )  THEN
+                      files_to_be_opened = files_to_be_opened + 1
+                      file_list(files_to_be_opened) = i-1
+                   ENDIF
+                      
+                   offset_xa(files_to_be_opened,j) = offset_x + shift_x
+                   offset_ya(files_to_be_opened,j) = offset_y + shift_y
+!
+!--                Index bounds of overlapping data
+                   nxlfa(files_to_be_opened,j) = MAX( nxl-offset_x-shift_x, nxlpr )
+                   nxrfa(files_to_be_opened,j) = MIN( nxr-offset_x-shift_x, nxrpr )
+                   nysfa(files_to_be_opened,j) = MAX( nys-offset_y-shift_y, nyspr )
+                   nynfa(files_to_be_opened,j) = MIN( nyn-offset_y-shift_y, nynpr )
+
+                ENDIF
+
+                shift_x = shift_x + ( nx_on_file + 1 )
+             ENDDO
+       
+          ENDIF
+             
+          shift_y = shift_y + ( ny_on_file + 1 )             
        ENDDO
-
+          
        IF ( j > 0 )  overlap_count(files_to_be_opened) = j
 
@@ -180,27 +181,30 @@
        IF ( j > 0 )  THEN
           WRITE (9,*) '*** reading from file: ', i, j, ' times'
-          WRITE (9,*) '    nxl = ', nxl, ' nxr = ', nxr, ' nys = ', nys, ' nyn = ', nyn
+          WRITE (9,*) '    nxl = ', nxl, ' nxr = ', nxr, ' nys = ', & 
+                                    nys, ' nyn = ', nyn
           WRITE (9,*) ' '
           DO  k = 1, j
              WRITE (9,*) 'k = ', k
-             WRITE (9,'(6(A,I4))')  'nxlfa = ', nxlfa(files_to_be_opened,k), &
-                         ' nxrfa = ', nxrfa(files_to_be_opened,k), &
-                         ' offset_xa = ', offset_xa(files_to_be_opened,k), &
-                         ' nysfa = ', nysfa(files_to_be_opened,k), &
-                         ' nynfa = ', nynfa(files_to_be_opened,k), &
-                         ' offset_ya = ', offset_ya(files_to_be_opened,k)
+             WRITE (9,'(6(A,I4))') 'nxlfa = ', nxlfa(files_to_be_opened,k),&
+                     ' nxrfa = ', nxrfa(files_to_be_opened,k), &
+                     ' offset_xa = ', offset_xa(files_to_be_opened,k), &
+                     ' nysfa = ', nysfa(files_to_be_opened,k), &
+                     ' nynfa = ', nynfa(files_to_be_opened,k), &
+                     ' offset_ya = ', offset_ya(files_to_be_opened,k)
           ENDDO
           CALL local_flush( 9 )
        ENDIF
 
+          
     ENDDO
-
+   
 !
 !-- Save the id-string of the current process, since myid_char may now be used
 !-- to open files created by PEs with other id.
-    myid_char_save = myid_char
+          myid_char_save = myid_char
 
 !
 !-- Test output (remove later)
+         
     DO i = 1, numprocs_previous_run
        WRITE (9,*) 'i=',i-1, ' ibs= ',hor_index_bounds_previous_run(1:4,i-1)
@@ -311,8 +315,8 @@
 !
 !--    Allocate temporary arrays sized as the arrays on the restart file
-       ALLOCATE( tmp_2d(nys_on_file-1:nyn_on_file+1,           &
-                        nxl_on_file-1:nxr_on_file+1),          &
-                 tmp_3d(nzb:nzt+1,nys_on_file-1:nyn_on_file+1, &
-                        nxl_on_file-1:nxr_on_file+1) )
+       ALLOCATE( tmp_2d(nys_on_file-nbgp:nyn_on_file+nbgp,           &
+                        nxl_on_file-nbgp:nxr_on_file+nbgp),          &
+                 tmp_3d(nzb:nzt+1,nys_on_file-nbgp:nyn_on_file+nbgp, &
+                        nxl_on_file-nbgp:nxr_on_file+nbgp) )
 
 !
@@ -349,19 +353,19 @@
                 CASE ( 'e' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   e(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   e(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                           tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'e_av' )
                    IF ( .NOT. ALLOCATED( e_av ) )  THEN
-                      ALLOCATE( e_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   e_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( e_av(nzb:nzt+1,nys-nbgp:nyn+nbgp,nxl-nbgp:nxr+nbgp) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   e_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                            tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'e_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   e_m(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   e_m(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                             tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'iran' ) ! matching random numbers is still unresolved
@@ -371,185 +375,185 @@
                 CASE ( 'kh' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   kh(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   kh(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                             tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'kh_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   kh_m(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   kh_m(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                              tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'km' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   km(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   km(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                               tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'km_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   km_m(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   km_m(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'lwp_av' )
                    IF ( .NOT. ALLOCATED( lwp_av ) )  THEN
-                      ALLOCATE( lwp_av(nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   lwp_av(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( lwp_av(nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_2d
+                   lwp_av(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                  tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'p' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   p(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   p(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                 tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'p_av' )
                    IF ( .NOT. ALLOCATED( p_av ) )  THEN
-                      ALLOCATE( p_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   p_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( p_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   p_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                  tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'pc_av' )
                    IF ( .NOT. ALLOCATED( pc_av ) )  THEN
-                      ALLOCATE( pc_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   pc_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( pc_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   pc_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                  tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'pr_av' )
                    IF ( .NOT. ALLOCATED( pr_av ) )  THEN
-                      ALLOCATE( pr_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   pr_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( pr_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   pr_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                   tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'precipitation_amount' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   precipitation_amount(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   precipitation_amount(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                         tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'precipitation_rate_a' )
                    IF ( .NOT. ALLOCATED( precipitation_rate_av ) )  THEN
-                      ALLOCATE( precipitation_rate_av(nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   precipitation_rate_av(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( precipitation_rate_av(nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_2d
+                   precipitation_rate_av(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                         tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'pt' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   pt(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   pt(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                   tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'pt_av' )
                    IF ( .NOT. ALLOCATED( pt_av ) )  THEN
-                      ALLOCATE( pt_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   pt_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( pt_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   pt_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                    tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'pt_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   pt_m(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   pt_m(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                    tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'q' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   q(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   q(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                    tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'q_av' )
                    IF ( .NOT. ALLOCATED( q_av ) )  THEN
-                      ALLOCATE( q_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   q_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( q_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg ))
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   q_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                     tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'q_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   q_m(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   q_m(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                      tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'ql' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   ql(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   ql(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                       tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'ql_av' )
                    IF ( .NOT. ALLOCATED( ql_av ) )  THEN
-                      ALLOCATE( ql_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   ql_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( ql_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   ql_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                       tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'ql_c_av' )
                    IF ( .NOT. ALLOCATED( ql_c_av ) )  THEN
-                      ALLOCATE( ql_c_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   ql_c_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( ql_c_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   ql_c_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                        tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'ql_v_av' )
                    IF ( .NOT. ALLOCATED( ql_v_av ) )  THEN
-                      ALLOCATE( ql_v_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   ql_v_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( ql_v_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   ql_v_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                        tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'ql_vp_av' )
                    IF ( .NOT. ALLOCATED( ql_vp_av ) )  THEN
-                      ALLOCATE( ql_vp_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   ql_vp_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( ql_vp_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   ql_vp_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                        tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'qs' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   qs(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   qs(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                                          tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'qsws' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   qsws(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   qsws(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                                          tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'qsws_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   qsws_m(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   qsws_m(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                                          tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'qsws_av' )
                    IF ( .NOT. ALLOCATED( qsws_av ) )  THEN
-                      ALLOCATE( qsws_av(nys-1:nyn+1,nxl-1:nxr+1) )
+                      ALLOCATE( qsws_av(nysg:nyng,nxlg:nxrg) )
                    ENDIF  
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   qsws_av(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   qsws_av(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                                          tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'qswst' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   qswst(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   qswst(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                                          tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'qswst_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   qswst_m(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   qswst_m(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                                          tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'qv_av' )
                    IF ( .NOT. ALLOCATED( qv_av ) )  THEN
-                      ALLOCATE( qv_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   qv_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( qv_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   qv_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                             tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'random_iv' )  ! still unresolved issue
@@ -559,76 +563,76 @@
                 CASE ( 'rho_av' )
                    IF ( .NOT. ALLOCATED( rho_av ) )  THEN
-                      ALLOCATE( rho_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   rho_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( rho_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   rho_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'rif' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   rif(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   rif(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                                         tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'rif_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   rif_m(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   rif_m(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                        tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'rif_wall' )
                    IF ( k == 1 )  THEN
-                      ALLOCATE( tmp_4d(nzb:nzt+1,nys_on_file-1:nyn_on_file+1, &
-                                       nxl_on_file-1:nxr_on_file+1,1:4) )
+                      ALLOCATE( tmp_4d(nzb:nzt+1,nys_on_file-nbgp:nyn_on_file+nbgp, &
+                                       nxl_on_file-nbgp:nxr_on_file+nbgp,1:4) )
                       READ ( 13 )  tmp_4d
                    ENDIF
-                   rif_wall(:,nysc-1:nync+1,nxlc-1:nxrc+1,:) = &
-                                         tmp_4d(:,nysf-1:nynf+1,nxlf-1:nxrf+1,:)
+                   rif_wall(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp,:) = &
+                            tmp_4d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp,:)
 
                 CASE ( 's_av' )
                    IF ( .NOT. ALLOCATED( s_av ) )  THEN
-                      ALLOCATE( s_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   s_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( s_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg))
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   s_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                 tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'sa' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   sa(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   sa(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                  tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'sa_av' )
                    IF ( .NOT. ALLOCATED( sa_av ) )  THEN
-                      ALLOCATE( sa_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   sa_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( sa_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   sa_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                    tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'saswsb' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   saswsb(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   saswsb(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                         tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'saswst' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   saswst(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   saswst(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                         tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'shf' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   shf(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   shf(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                      tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'shf_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   shf_m(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   shf_m(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                        tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
                 CASE ( 'shf_av' )
                    IF ( .NOT. ALLOCATED( shf_av ) )  THEN
-                      ALLOCATE( shf_av(nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   shf_av(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( shf_av(nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_2d
+                   shf_av(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                         tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
                 CASE ( 'spectrum_x' )
                    IF ( k == 1 )  THEN
@@ -663,51 +667,51 @@
                 CASE ( 'ts' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   ts(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   ts(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                     tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'ts_av' )
                    IF ( .NOT. ALLOCATED( ts_av ) )  THEN
-                      ALLOCATE( ts_av(nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   ts_av(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( ts_av(nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_2d
+                   ts_av(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                        tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'tswst' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   tswst(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   tswst(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                         tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'tswst_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   tswst_m(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   tswst_m(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                          tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'u' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   u(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   u(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                             tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'u_av' )
                    IF ( .NOT. ALLOCATED( u_av ) )  THEN
-                      ALLOCATE( u_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   u_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( u_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   u_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                               tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'u_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   u_m(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   u_m(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                 tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'u_m_l' )
                    IF ( k == 1 )  THEN
                       ALLOCATE( tmp_3dwul(nzb:nzt+1, &
-                                          nys_on_file-1:nyn_on_file+1,1:2) )
+                                          nys_on_file-nbgp:nyn_on_file+nbgp,1:2) )
                       READ ( 13 )  tmp_3dwul
                    ENDIF
                    IF ( outflow_l )  THEN
-                      u_m_l(:,nysc-1:nync+1,:) = tmp_3dwul(:,nysf-1:nynf+1,:)
+                      u_m_l(:,nysc-nbgp:nync+nbgp,:) = tmp_3dwul(:,nysf-nbgp:nynf+nbgp,:)
                    ENDIF
 
@@ -715,9 +719,9 @@
                    IF ( k == 1 )  THEN
                       ALLOCATE( tmp_3dwun(nzb:nzt+1,ny-1:ny, &
-                                          nxl_on_file-1:nxr_on_file+1) )
+                                          nxl_on_file-nbgp:nxr_on_file+nbgp) )
                       READ ( 13 )  tmp_3dwun
                    ENDIF
                    IF ( outflow_n )  THEN
-                      u_m_n(:,:,nxlc-1:nxrc+1) = tmp_3dwun(:,:,nxlf-1:nxrf+1)
+                      u_m_n(:,:,nxlc-nbgp:nxrc+nbgp) = tmp_3dwun(:,:,nxlf-nbgp:nxrf+nbgp)
                    ENDIF
 
@@ -725,9 +729,9 @@
                    IF ( k == 1 )  THEN
                       ALLOCATE( tmp_3dwur(nzb:nzt+1,&
-                                          nys_on_file-1:nyn_on_file+1,nx-1:nx) )
+                                          nys_on_file-nbgp:nyn_on_file+nbgp,nx-1:nx) )
                       READ ( 13 )  tmp_3dwur
                    ENDIF
                    IF ( outflow_r )  THEN
-                      u_m_r(:,nysc-1:nync+1,:) = tmp_3dwur(:,nysf-1:nynf+1,:)
+                      u_m_r(:,nysc-nbgp:nync+nbgp,:) = tmp_3dwur(:,nysf-nbgp:nynf+nbgp,:)
                    ENDIF
 
@@ -735,70 +739,70 @@
                    IF ( k == 1 )  THEN
                       ALLOCATE( tmp_3dwus(nzb:nzt+1,0:1, &
-                                          nxl_on_file-1:nxr_on_file+1) )
+                                          nxl_on_file-nbgp:nxr_on_file+nbgp) )
                       READ ( 13 )  tmp_3dwus
                    ENDIF
                    IF ( outflow_s )  THEN
-                      u_m_s(:,:,nxlc-1:nxrc+1) = tmp_3dwus(:,:,nxlf-1:nxrf+1)
+                      u_m_s(:,:,nxlc-nbgp:nxrc+nbgp) = tmp_3dwus(:,:,nxlf-nbgp:nxrf+nbgp)
                    ENDIF
 
                 CASE ( 'us' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   us(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   us(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                     tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'usws' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   usws(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   usws(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                       tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'uswst' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   uswst(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   uswst(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                        tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'usws_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   usws_m(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   usws_m(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                         tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'uswst_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   uswst_m(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   uswst_m(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                          tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'us_av' )
                    IF ( .NOT. ALLOCATED( us_av ) )  THEN
-                      ALLOCATE( us_av(nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   us_av(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( us_av(nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_2d
+                   us_av(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                        tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'v' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   v(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   v(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                              tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'v_av' )
                    IF ( .NOT. ALLOCATED( v_av ) )  THEN
-                      ALLOCATE( v_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   v_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( v_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   v_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                               tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'v_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   v_m(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   v_m(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'v_m_l' )
                    IF ( k == 1 )  THEN
                       ALLOCATE( tmp_3dwvl(nzb:nzt+1,&
-                                          nys_on_file-1:nyn_on_file+1,0:1) )
+                                          nys_on_file-nbgp:nyn_on_file+nbgp,0:1) )
                       READ ( 13 )  tmp_3dwvl
                    ENDIF
                    IF ( outflow_l )  THEN
-                      v_m_l(:,nysc-1:nync+1,:) = tmp_3dwvl(:,nysf-1:nynf+1,:)
+                      v_m_l(:,nysc-nbgp:nync+nbgp,:) = tmp_3dwvl(:,nysf-nbgp:nynf+nbgp,:)
                    ENDIF
 
@@ -806,9 +810,9 @@
                    IF ( k == 1 )  THEN
                       ALLOCATE( tmp_3dwvn(nzb:nzt+1,ny-1:ny, &
-                                          nxl_on_file-1:nxr_on_file+1) )
+                                          nxl_on_file-nbgp:nxr_on_file+nbgp) )
                       READ ( 13 )  tmp_3dwvn
                    ENDIF
                    IF ( outflow_n )  THEN
-                      v_m_n(:,:,nxlc-1:nxrc+1) = tmp_3dwvn(:,:,nxlf-1:nxrf+1)
+                      v_m_n(:,:,nxlc-nbgp:nxrc+nbgp) = tmp_3dwvn(:,:,nxlf-nbgp:nxrf+nbgp)
                    ENDIF
 
@@ -816,9 +820,9 @@
                    IF ( k == 1 )  THEN
                       ALLOCATE( tmp_3dwvr(nzb:nzt+1,&
-                                          nys_on_file-1:nyn_on_file+1,nx-1:nx) )
+                                          nys_on_file-nbgp:nyn_on_file+nbgp,nx-1:nx) )
                       READ ( 13 )  tmp_3dwvr
                    ENDIF
                    IF ( outflow_r )  THEN
-                      v_m_r(:,nysc-1:nync+1,:) = tmp_3dwvr(:,nysf-1:nynf+1,:)
+                      v_m_r(:,nysc-nbgp:nync+nbgp,:) = tmp_3dwvr(:,nysf-nbgp:nynf+nbgp,:)
                    ENDIF
 
@@ -826,75 +830,75 @@
                    IF ( k == 1 )  THEN
                       ALLOCATE( tmp_3dwvs(nzb:nzt+1,1:2, &
-                                          nxl_on_file-1:nxr_on_file+1) )
+                                          nxl_on_file-nbgp:nxr_on_file+nbgp) )
                       READ ( 13 )  tmp_3dwvs
                    ENDIF
                    IF ( outflow_s )  THEN
-                      v_m_s(:,:,nxlc-1:nxrc+1) = tmp_3dwvs(:,:,nxlf-1:nxrf+1)
+                      v_m_s(:,:,nxlc-nbgp:nxrc+nbgp) = tmp_3dwvs(:,:,nxlf-nbgp:nxrf+nbgp)
                    ENDIF
 
                 CASE ( 'vpt' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   vpt(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   vpt(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                               tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'vpt_av' )
                    IF ( .NOT. ALLOCATED( vpt_av ) )  THEN
-                      ALLOCATE( vpt_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   vpt_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( vpt_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   vpt_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                               tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'vpt_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   vpt_m(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   vpt_m(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'vsws' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   vsws(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   vsws(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                       tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'vswst' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   vswst(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   vswst(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                        tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'vsws_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   vsws_m(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   vsws_m(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                         tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'vswst_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   vswst_m(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   vswst_m(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                          tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'w' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   w(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   w(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                             tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'w_av' )
                    IF ( .NOT. ALLOCATED( w_av ) )  THEN
-                      ALLOCATE( w_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   w_av(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( w_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_3d
+                   w_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                               tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'w_m' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   w_m(:,nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_3d(:,nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   w_m(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) = &
+                                tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'w_m_l' )
                    IF ( k == 1 )  THEN
                       ALLOCATE( tmp_3dwwl(nzb:nzt+1,&
-                                          nys_on_file-1:nyn_on_file+1,0:1) )
+                                          nys_on_file-nbgp:nyn_on_file+nbgp,0:1) )
                       READ ( 13 )  tmp_3dwwl
                    ENDIF
                    IF ( outflow_l )  THEN
-                      w_m_l(:,nysc-1:nync+1,:) = tmp_3dwwl(:,nysf-1:nynf+1,:)
+                      w_m_l(:,nysc-nbgp:nync+nbgp,:) = tmp_3dwwl(:,nysf-nbgp:nynf+nbgp,:)
                    ENDIF
 
@@ -902,9 +906,9 @@
                    IF ( k == 1 )  THEN
                       ALLOCATE( tmp_3dwwn(nzb:nzt+1,ny-1:ny, &
-                                          nxl_on_file-1:nxr_on_file+1) )
+                                          nxl_on_file-nbgp:nxr_on_file+nbgp) )
                       READ ( 13 )  tmp_3dwwn
                    ENDIF
                    IF ( outflow_n )  THEN
-                      w_m_n(:,:,nxlc-1:nxrc+1) = tmp_3dwwn(:,:,nxlf-1:nxrf+1)
+                      w_m_n(:,:,nxlc-nbgp:nxrc+nbgp) = tmp_3dwwn(:,:,nxlf-nbgp:nxrf+nbgp)
                    ENDIF
 
@@ -912,9 +916,9 @@
                    IF ( k == 1 )  THEN
                       ALLOCATE( tmp_3dwwr(nzb:nzt+1,&
-                                          nys_on_file-1:nyn_on_file+1,nx-1:nx) )
+                                          nys_on_file-nbgp:nyn_on_file+nbgp,nx-1:nx) )
                       READ ( 13 )  tmp_3dwwr
                    ENDIF
                    IF ( outflow_r )  THEN
-                      w_m_r(:,nysc-1:nync+1,:) = tmp_3dwwr(:,nysf-1:nynf+1,:)
+                      w_m_r(:,nysc-nbgp:nync+nbgp,:) = tmp_3dwwr(:,nysf-nbgp:nynf+nbgp,:)
                    ENDIF
 
@@ -922,9 +926,9 @@
                    IF ( k == 1 )  THEN
                       ALLOCATE( tmp_3dwws(nzb:nzt+1,0:1, &
-                                          nxl_on_file-1:nxr_on_file+1) )
+                                          nxl_on_file-nbgp:nxr_on_file+nbgp) )
                       READ ( 13 )  tmp_3dwws
                    ENDIF
                    IF ( outflow_s )  THEN
-                      w_m_s(:,:,nxlc-1:nxrc+1) = tmp_3dwws(:,:,nxlf-1:nxrf+1)
+                      w_m_s(:,:,nxlc-nbgp:nxrc+nbgp) = tmp_3dwws(:,:,nxlf-nbgp:nxrf+nbgp)
                    ENDIF
                    DEALLOCATE( tmp_3dwws )
@@ -932,14 +936,14 @@
                 CASE ( 'z0' )
                    IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   z0(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                   z0(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                     tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE ( 'z0_av' )
                    IF ( .NOT. ALLOCATED( z0_av ) )  THEN
-                      ALLOCATE( z0_av(nys-1:nyn+1,nxl-1:nxr+1) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_2d
-                   z0_av(nysc-1:nync+1,nxlc-1:nxrc+1) = &
-                                          tmp_2d(nysf-1:nynf+1,nxlf-1:nxrf+1)
+                      ALLOCATE( z0_av(nysg:nyng,nxlg:nxrg) )
+                   ENDIF
+                   IF ( k == 1 )  READ ( 13 )  tmp_2d
+                   z0_av(nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp)  = &
+                       tmp_2d(nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
                 CASE DEFAULT
Index: /palm/trunk/SOURCE/sor.f90
===================================================================
--- /palm/trunk/SOURCE/sor.f90	(revision 666)
+++ /palm/trunk/SOURCE/sor.f90	(revision 667)
@@ -4,5 +4,7 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng.
+! Call of exchange_horiz are modified.
+! bug removed in declaration of ddzw(), nz replaced by nzt+1
 !
 ! Former revisions:
@@ -36,7 +38,7 @@
 
     INTEGER ::  i, j, k, n, nxl1, nxl2, nys1, nys2
-    REAL    ::  ddzu(1:nz+1), ddzw(1:nz)
+    REAL    ::  ddzu(1:nz+1), ddzw(1:nzt+1)
     REAL    ::  d(nzb+1:nzt,nys:nyn,nxl:nxr),         &
-                p(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1)
+                p(nzb:nzt+1,nysg:nyng,nxlg:nxrg)
     REAL, DIMENSION(:), ALLOCATABLE ::  f1, f2, f3
 
@@ -102,5 +104,5 @@
 !
 !--    Exchange of boundary values for p.
-       CALL exchange_horiz( p )
+       CALL exchange_horiz( p, nbgp )
 
 !
@@ -147,16 +149,12 @@
 !
 !--    Exchange of boundary values for p.
-       CALL exchange_horiz( p )
+       CALL exchange_horiz( p, nbgp )
 
 !
 !--    Boundary conditions top/bottom.
 !--    Bottom boundary
-       IF ( ibc_p_b == 1 )  THEN
-!
-!--       Neumann
+       IF ( ibc_p_b == 1 )  THEN       !       Neumann
           p(nzb,:,:) = p(nzb+1,:,:)
-       ELSE
-!
-!--       Dirichlet
+       ELSE                            !       Dirichlet
           p(nzb,:,:) = 0.0
        ENDIF
@@ -164,11 +162,7 @@
 !
 !--    Top boundary
-       IF ( ibc_p_t == 1 )  THEN
-!
-!--       Neumann
+       IF ( ibc_p_t == 1 )  THEN                 !  Neumann
           p(nzt+1,:,:) = p(nzt,:,:)
-       ELSE
-!
-!--       Dirichlet
+       ELSE                      !  Dirichlet
           p(nzt+1,:,:) = 0.0
        ENDIF
@@ -185,4 +179,5 @@
        ENDIF
 
+
     ENDDO
 
Index: /palm/trunk/SOURCE/subsidence.f90
===================================================================
--- /palm/trunk/SOURCE/subsidence.f90	(revision 666)
+++ /palm/trunk/SOURCE/subsidence.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng.
 !
 ! Former revisions:
@@ -121,5 +121,5 @@
        REAL :: tmp_grad
     
-       REAL, DIMENSION(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) :: var, tendency
+       REAL, DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) :: var, tendency
        REAL, DIMENSION(nzb:nzt+1) :: var_init, var_mod
 
@@ -197,5 +197,5 @@
        REAL :: tmp_grad
     
-       REAL, DIMENSION(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) :: var, tendency
+       REAL, DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) :: var, tendency
        REAL, DIMENSION(nzb:nzt+1) :: var_init, var_mod
 
Index: /palm/trunk/SOURCE/sum_up_3d_data.f90
===================================================================
--- /palm/trunk/SOURCE/sum_up_3d_data.f90	(revision 666)
+++ /palm/trunk/SOURCE/sum_up_3d_data.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-!  
+!  nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng.
 !
 ! Former revisions:
@@ -65,5 +65,5 @@
              CASE ( 'e' )
                 IF ( .NOT. ALLOCATED( e_av ) )  THEN
-                   ALLOCATE( e_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( e_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 e_av = 0.0
@@ -71,5 +71,5 @@
              CASE ( 'lwp*' )
                 IF ( .NOT. ALLOCATED( lwp_av ) )  THEN
-                   ALLOCATE( lwp_av(nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( lwp_av(nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 lwp_av = 0.0
@@ -77,5 +77,5 @@
              CASE ( 'p' )
                 IF ( .NOT. ALLOCATED( p_av ) )  THEN
-                   ALLOCATE( p_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( p_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 p_av = 0.0
@@ -83,5 +83,5 @@
              CASE ( 'pc' )
                 IF ( .NOT. ALLOCATED( pc_av ) )  THEN
-                   ALLOCATE( pc_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( pc_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 pc_av = 0.0
@@ -89,5 +89,5 @@
              CASE ( 'pr' )
                 IF ( .NOT. ALLOCATED( pr_av ) )  THEN
-                   ALLOCATE( pr_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( pr_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 pr_av = 0.0
@@ -95,5 +95,5 @@
              CASE ( 'prr*' )
                 IF ( .NOT. ALLOCATED( precipitation_rate_av ) )  THEN
-                   ALLOCATE( precipitation_rate_av(nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( precipitation_rate_av(nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 precipitation_rate_av = 0.0
@@ -101,5 +101,5 @@
              CASE ( 'pt' )
                 IF ( .NOT. ALLOCATED( pt_av ) )  THEN
-                   ALLOCATE( pt_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( pt_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 pt_av = 0.0
@@ -107,5 +107,5 @@
              CASE ( 'q' )
                 IF ( .NOT. ALLOCATED( q_av ) )  THEN
-                   ALLOCATE( q_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( q_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 q_av = 0.0
@@ -113,5 +113,5 @@
              CASE ( 'ql' )
                 IF ( .NOT. ALLOCATED( ql_av ) )  THEN
-                   ALLOCATE( ql_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( ql_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 ql_av = 0.0
@@ -119,5 +119,5 @@
              CASE ( 'ql_c' )
                 IF ( .NOT. ALLOCATED( ql_c_av ) )  THEN
-                   ALLOCATE( ql_c_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( ql_c_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 ql_c_av = 0.0
@@ -125,5 +125,5 @@
              CASE ( 'ql_v' )
                 IF ( .NOT. ALLOCATED( ql_v_av ) )  THEN
-                   ALLOCATE( ql_v_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( ql_v_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 ql_v_av = 0.0
@@ -131,5 +131,5 @@
              CASE ( 'ql_vp' )
                 IF ( .NOT. ALLOCATED( ql_vp_av ) )  THEN
-                   ALLOCATE( ql_vp_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( ql_vp_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 ql_vp_av = 0.0
@@ -137,5 +137,5 @@
              CASE ( 'qsws*' )
                 IF ( .NOT. ALLOCATED( qsws_av ) )  THEN
-                   ALLOCATE( qsws_av(nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( qsws_av(nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 qsws_av = 0.0
@@ -143,5 +143,5 @@
              CASE ( 'qv' )
                 IF ( .NOT. ALLOCATED( qv_av ) )  THEN
-                   ALLOCATE( qv_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( qv_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 qv_av = 0.0
@@ -149,5 +149,5 @@
              CASE ( 'rho' )
                 IF ( .NOT. ALLOCATED( rho_av ) )  THEN
-                   ALLOCATE( rho_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( rho_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 rho_av = 0.0
@@ -155,5 +155,5 @@
              CASE ( 's' )
                 IF ( .NOT. ALLOCATED( s_av ) )  THEN
-                   ALLOCATE( s_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( s_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 s_av = 0.0
@@ -161,5 +161,5 @@
              CASE ( 'sa' )
                 IF ( .NOT. ALLOCATED( sa_av ) )  THEN
-                   ALLOCATE( sa_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( sa_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 sa_av = 0.0
@@ -167,5 +167,5 @@
              CASE ( 'shf*' )
                 IF ( .NOT. ALLOCATED( shf_av ) )  THEN
-                   ALLOCATE( shf_av(nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( shf_av(nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 shf_av = 0.0
@@ -173,5 +173,5 @@
              CASE ( 't*' )
                 IF ( .NOT. ALLOCATED( ts_av ) )  THEN
-                   ALLOCATE( ts_av(nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( ts_av(nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 ts_av = 0.0
@@ -179,5 +179,5 @@
              CASE ( 'u' )
                 IF ( .NOT. ALLOCATED( u_av ) )  THEN
-                   ALLOCATE( u_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( u_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 u_av = 0.0
@@ -185,5 +185,5 @@
              CASE ( 'u*' )
                 IF ( .NOT. ALLOCATED( us_av ) )  THEN
-                   ALLOCATE( us_av(nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( us_av(nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 us_av = 0.0
@@ -191,5 +191,5 @@
              CASE ( 'v' )
                 IF ( .NOT. ALLOCATED( v_av ) )  THEN
-                   ALLOCATE( v_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( v_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 v_av = 0.0
@@ -197,5 +197,5 @@
              CASE ( 'vpt' )
                 IF ( .NOT. ALLOCATED( vpt_av ) )  THEN
-                   ALLOCATE( vpt_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( vpt_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 vpt_av = 0.0
@@ -203,5 +203,5 @@
              CASE ( 'w' )
                 IF ( .NOT. ALLOCATED( w_av ) )  THEN
-                   ALLOCATE( w_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( w_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 w_av = 0.0
@@ -209,5 +209,5 @@
              CASE ( 'z0*' )
                 IF ( .NOT. ALLOCATED( z0_av ) )  THEN
-                   ALLOCATE( z0_av(nys-1:nyn+1,nxl-1:nxr+1) )
+                   ALLOCATE( z0_av(nysg:nyng,nxlg:nxrg) )
                 ENDIF
                 z0_av = 0.0
@@ -233,6 +233,6 @@
 
           CASE ( 'e' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       e_av(k,j,i) = e_av(k,j,i) + e(k,j,i)
@@ -242,6 +242,6 @@
 
           CASE ( 'lwp*' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    lwp_av(j,i) = lwp_av(j,i) + SUM( ql(nzb:nzt,j,i) * &
                                                     dzw(1:nzt+1) )
@@ -250,6 +250,6 @@
 
           CASE ( 'p' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       p_av(k,j,i) = p_av(k,j,i) + p(k,j,i)
@@ -289,6 +289,6 @@
 
           CASE ( 'pr*' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    precipitation_rate_av(j,i) = precipitation_rate_av(j,i) + &
                                                 precipitation_rate(j,i)
@@ -298,7 +298,7 @@
           CASE ( 'pt' )
              IF ( .NOT. cloud_physics ) THEN
-                DO  i = nxl-1, nxr+1
-                   DO  j = nys-1, nyn+1
-                      DO  k = nzb, nzt+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
+                   DO  k = nzb, nzt+1
                          pt_av(k,j,i) = pt_av(k,j,i) + pt(k,j,i)
                       ENDDO
@@ -306,7 +306,7 @@
                 ENDDO
              ELSE
-                DO  i = nxl-1, nxr+1
-                   DO  j = nys-1, nyn+1
-                      DO  k = nzb, nzt+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
+                   DO  k = nzb, nzt+1
                          pt_av(k,j,i) = pt_av(k,j,i) + pt(k,j,i) + l_d_cp * &
                                                        pt_d_t(k) * ql(k,j,i)
@@ -317,6 +317,6 @@
 
           CASE ( 'q' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       q_av(k,j,i) = q_av(k,j,i) + q(k,j,i)
@@ -326,6 +326,6 @@
 
           CASE ( 'ql' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       ql_av(k,j,i) = ql_av(k,j,i) + ql(k,j,i)
@@ -335,6 +335,6 @@
 
           CASE ( 'ql_c' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       ql_c_av(k,j,i) = ql_c_av(k,j,i) + ql_c(k,j,i)
@@ -344,6 +344,6 @@
 
           CASE ( 'ql_v' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       ql_v_av(k,j,i) = ql_v_av(k,j,i) + ql_v(k,j,i)
@@ -353,6 +353,6 @@
 
           CASE ( 'ql_vp' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       ql_vp_av(k,j,i) = ql_vp_av(k,j,i) + ql_vp(k,j,i)
@@ -362,6 +362,6 @@
 
           CASE ( 'qsws*' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    qsws_av(j,i) = qsws_av(j,i) + qsws(j,i)
                 ENDDO
@@ -369,6 +369,6 @@
 
           CASE ( 'qv' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       qv_av(k,j,i) = qv_av(k,j,i) + q(k,j,i) - ql(k,j,i)
@@ -378,6 +378,6 @@
 
           CASE ( 'rho' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       rho_av(k,j,i) = rho_av(k,j,i) + rho(k,j,i)
@@ -387,6 +387,6 @@
 
           CASE ( 's' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       s_av(k,j,i) = s_av(k,j,i) + q(k,j,i)
@@ -396,6 +396,6 @@
 
           CASE ( 'sa' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       sa_av(k,j,i) = sa_av(k,j,i) + sa(k,j,i)
@@ -405,6 +405,6 @@
 
           CASE ( 'shf*' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    shf_av(j,i) = shf_av(j,i) + shf(j,i)
                 ENDDO
@@ -412,6 +412,6 @@
 
           CASE ( 't*' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    ts_av(j,i) = ts_av(j,i) + ts(j,i)
                 ENDDO
@@ -419,6 +419,6 @@
 
           CASE ( 'u' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       u_av(k,j,i) = u_av(k,j,i) + u(k,j,i)
@@ -428,6 +428,6 @@
 
           CASE ( 'u*' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    us_av(j,i) = us_av(j,i) + us(j,i)
                 ENDDO
@@ -435,6 +435,6 @@
 
           CASE ( 'v' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       v_av(k,j,i) = v_av(k,j,i) + v(k,j,i)
@@ -444,6 +444,6 @@
 
           CASE ( 'vpt' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       vpt_av(k,j,i) = vpt_av(k,j,i) + vpt(k,j,i)
@@ -453,6 +453,6 @@
 
           CASE ( 'w' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    DO  k = nzb, nzt+1
                       w_av(k,j,i) = w_av(k,j,i) + w(k,j,i)
@@ -462,6 +462,6 @@
 
           CASE ( 'z0*' )
-             DO  i = nxl-1, nxr+1
-                DO  j = nys-1, nyn+1
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
                    z0_av(j,i) = z0_av(j,i) + z0(j,i)
                 ENDDO
Index: /palm/trunk/SOURCE/surface_coupler.f90
===================================================================
--- /palm/trunk/SOURCE/surface_coupler.f90	(revision 666)
+++ /palm/trunk/SOURCE/surface_coupler.f90	(revision 667)
@@ -5,4 +5,10 @@
 ! -----------------
 ! 
+! additional case for nonequivalent processor and grid topopolgy in ocean and 
+! atmosphere added (coupling_topology = 1)
+!
+! 
+! Added exchange of u and v from Ocean to Atmosphere
+!
 !
 ! Former revisions:
@@ -39,8 +45,11 @@
 
     REAL    ::  time_since_reference_point_rem
+    REAL    ::  total_2d(-nbgp:ny+nbgp,-nbgp:nx+nbgp)
 
 #if defined( __parallel )
 
-       CALL cpu_log( log_point(39), 'surface_coupler', 'start' )
+    CALL cpu_log( log_point(39), 'surface_coupler', 'start' )
+
+
 
 !
@@ -51,8 +60,25 @@
 !-- If necessary, the coupler will be called at the beginning of the next 
 !-- restart run.
-    CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER, target_id,  &
-                       0, &
-                       terminate_coupled_remote, 1, MPI_INTEGER, target_id,  &
-                       0, comm_inter, status, ierr )
+
+    IF ( coupling_topology == 0 ) THEN
+       CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER, target_id,  &
+                          0,                                                    &
+                          terminate_coupled_remote, 1, MPI_INTEGER, target_id,  &
+                          0, comm_inter, status, ierr )
+    ELSE
+       IF ( myid == 0) THEN
+          CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER, &
+                             target_id, 0,                             &
+                             terminate_coupled_remote, 1, MPI_INTEGER, & 
+                             target_id, 0,                             &
+                             comm_inter, status, ierr )
+       ENDIF
+       CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_INTEGER, 0, comm2d, ierr)
+
+       ALLOCATE( total_2d_a(-nbgp:ny_a+nbgp,-nbgp:nx_a+nbgp),       &
+                 total_2d_o(-nbgp:ny_o+nbgp,-nbgp:nx_o+nbgp) )
+
+    ENDIF
+
     IF ( terminate_coupled_remote > 0 )  THEN
        WRITE( message_string, * ) 'remote model "',                         &
@@ -64,81 +90,298 @@
                                   '" has',                                  &
                                   '&terminate_coupled = ',                  &
-                                  terminate_coupled
+                                   terminate_coupled
        CALL message( 'surface_coupler', 'PA0310', 1, 2, 0, 6, 0 )
        RETURN
     ENDIF
+ 
 
 !
 !-- Exchange the current simulated time between the models,
-!-- currently just for testing
-    CALL MPI_SEND( time_since_reference_point, 1, MPI_REAL, target_id, 11, &
-                   comm_inter, ierr )
-    CALL MPI_RECV( time_since_reference_point_rem, 1, MPI_REAL, target_id, 11, &
-                   comm_inter, status, ierr )
+!-- currently just for total_2ding
+    IF ( coupling_topology == 0 ) THEN   
+       CALL MPI_SEND( time_since_reference_point, 1, MPI_REAL, &
+                      target_id, 11, comm_inter, ierr )
+       CALL MPI_RECV( time_since_reference_point_rem, 1, MPI_REAL, &
+                      target_id, 11, comm_inter, status, ierr )
+    ELSE
+       IF ( myid == 0 ) THEN
+          CALL MPI_SEND( time_since_reference_point, 1, MPI_REAL, &
+                         target_id, 11, comm_inter, ierr )
+          CALL MPI_RECV( time_since_reference_point_rem, 1, MPI_REAL, &
+                         target_id, 11, comm_inter, status, ierr )
+       ENDIF
+       CALL MPI_BCAST( time_since_reference_point_rem, 1, MPI_REAL, &
+                       0, comm2d, ierr )
+    ENDIF
     WRITE ( 9, * ) 'simulated time: ', simulated_time
     WRITE ( 9, * ) 'time since start of coupling: ', &
-                   time_since_reference_point, ' remote: ', &
-                   time_since_reference_point_rem
-    CALL local_flush( 9 )
+                  time_since_reference_point, ' remote: ', &
+                  time_since_reference_point_rem
+   CALL local_flush( 9 )
+  
 
 !
 !-- Exchange the interface data
     IF ( coupling_mode == 'atmosphere_to_ocean' )  THEN
-
-!
-!--    Send heat flux at bottom surface to the ocean model
-       WRITE ( 9, * )  '*** send shf to ocean'
-       CALL local_flush( 9 )
-       CALL MPI_SEND( shf(nys-1,nxl-1), ngp_xy, MPI_REAL, target_id, 12, &
-                      comm_inter, ierr )
-
-!
-!--    Send humidity flux at bottom surface to the ocean model
-       IF ( humidity )  THEN
-          WRITE ( 9, * )  '*** send qsws to ocean'
+    
+!
+!--    Horizontal grid size and number of processors is equal
+!--    in ocean and atmosphere
+       IF ( coupling_topology == 0 ) THEN
+
+!
+!--       Send heat flux at bottom surface to the ocean model
+          CALL MPI_SEND( shf(nysg,nxlg), ngp_xy, MPI_REAL, &
+                         target_id, 12, comm_inter, ierr )
+
+!
+!--       Send humidity flux at bottom surface to the ocean model
+          IF ( humidity )  THEN
+             CALL MPI_SEND( qsws(nysg,nxlg), ngp_xy, MPI_REAL, &
+                            target_id, 13, comm_inter, ierr )
+          ENDIF
+
+!
+!--       Receive temperature at the bottom surface from the ocean model
+          WRITE ( 9, * )  '*** receive pt from ocean'
           CALL local_flush( 9 )
-          CALL MPI_SEND( qsws(nys-1,nxl-1), ngp_xy, MPI_REAL, target_id, 13, &
-               comm_inter, ierr )
+          CALL MPI_RECV( pt(0,nysg,nxlg), 1, type_xy, &
+                         target_id, 14, comm_inter, status, ierr )
+
+!
+!--       Send the momentum flux (u) at bottom surface to the ocean model
+          CALL MPI_SEND( usws(nysg,nxlg), ngp_xy, MPI_REAL, &
+                         target_id, 15, comm_inter, ierr )
+
+!
+!--       Send the momentum flux (v) at bottom surface to the ocean model
+          CALL MPI_SEND( vsws(nysg,nxlg), ngp_xy, MPI_REAL, &
+                         target_id, 16, comm_inter, ierr )
+
+!
+!--       Receive u at the bottom surface from the ocean model
+          CALL MPI_RECV( u(0,nysg,nxlg), 1, type_xy, &
+                         target_id, 17, comm_inter, status, ierr )
+
+!
+!--       Receive v at the bottom surface from the ocean model
+          CALL MPI_RECV( v(0,nysg,nxlg), 1, type_xy, &
+                         target_id, 18,  comm_inter, status, ierr )
+
+!
+!--    Horizontal grid size or number of processors differs between 
+!--    ocean and atmosphere
+       ELSE
+      
+!
+!--       Send heat flux at bottom surface to the ocean model
+          total_2d_a = 0.0
+          total_2d = 0.0
+          total_2d(nys:nyn,nxl:nxr) = shf(nys:nyn,nxl:nxr)
+          CALL MPI_REDUCE( total_2d, total_2d_a, ngp_a, MPI_REAL, &
+                           MPI_SUM, 0, comm2d, ierr )
+          CALL interpolate_to_ocean(12)
+   
+!
+!--       Send humidity flux at bottom surface to the ocean model
+          IF ( humidity ) THEN
+             total_2d_a = 0.0
+             total_2d = 0.0
+             total_2d(nys:nyn,nxl:nxr) = qsws(nys:nyn,nxl:nxr)
+             CALL MPI_REDUCE( total_2d, total_2d_a, ngp_a, MPI_REAL, &
+                              MPI_SUM, 0, comm2d, ierr )
+             CALL interpolate_to_ocean(13)
+          ENDIF
+
+!
+!--       Receive temperature at the bottom surface from the ocean model
+          IF ( myid == 0 ) THEN
+             CALL MPI_RECV( total_2d_a(-nbgp,-nbgp), ngp_a, MPI_REAL, &
+                            target_id, 14, comm_inter, status, ierr )   
+          ENDIF
+          CALL MPI_BARRIER( comm2d, ierr )
+          CALL MPI_BCAST( total_2d_a(-nbgp,-nbgp), ngp_a, MPI_REAL, &
+                          0, comm2d, ierr )
+          pt(0,nysg:nyng,nxlg:nxrg) = total_2d_a(nysg:nyng,nxlg:nxrg)
+
+!
+!--       Send momentum flux (u) at bottom surface to the ocean model
+          total_2d_a = 0.0 
+          total_2d = 0.0
+          total_2d(nys:nyn,nxl:nxr) = usws(nys:nyn,nxl:nxr)
+          CALL MPI_REDUCE( total_2d, total_2d_a, ngp_a, MPI_REAL, &
+                           MPI_SUM, 0, comm2d, ierr )
+          CALL interpolate_to_ocean(15)
+
+!
+!--       Send momentum flux (v) at bottom surface to the ocean model
+          total_2d_a = 0.0
+          total_2d = 0.0
+          total_2d(nys:nyn,nxl:nxr) = vsws(nys:nyn,nxl:nxr)
+          CALL MPI_REDUCE( total_2d, total_2d_a, ngp_a, MPI_REAL, &
+                           MPI_SUM, 0, comm2d, ierr )
+          CALL interpolate_to_ocean(16)
+
+!
+!--       Receive u at the bottom surface from the ocean model
+          IF ( myid == 0 ) THEN
+             CALL MPI_RECV( total_2d_a(-nbgp,-nbgp), ngp_a, MPI_REAL, &
+                            target_id, 17, comm_inter, status, ierr )            
+          ENDIF
+          CALL MPI_BARRIER( comm2d, ierr )
+          CALL MPI_BCAST( total_2d_a(-nbgp,-nbgp), ngp_a, MPI_REAL, &
+                          0, comm2d, ierr )
+          u(0,nysg:nyng,nxlg:nxrg) = total_2d_a(nysg:nyng,nxlg:nxrg)
+   
+!
+!--       Receive v at the bottom surface from the ocean model
+          IF ( myid == 0 ) THEN
+             CALL MPI_RECV( total_2d_a(-nbgp,-nbgp), ngp_a, MPI_REAL, &
+                            target_id, 18, comm_inter, status, ierr )            
+          ENDIF
+          CALL MPI_BARRIER( comm2d, ierr )
+          CALL MPI_BCAST( total_2d_a(-nbgp,-nbgp), ngp_a, MPI_REAL, &
+                          0, comm2d, ierr )
+          v(0,nysg:nyng,nxlg:nxrg) = total_2d_a(nysg:nyng,nxlg:nxrg)
+
        ENDIF
 
-!
-!--    Receive temperature at the bottom surface from the ocean model
-       WRITE ( 9, * )  '*** receive pt from ocean'
-       CALL local_flush( 9 )
-       CALL MPI_RECV( pt(0,nys-1,nxl-1), 1, type_xy, target_id, 14, &
-                      comm_inter, status, ierr )
-
-!
-!--    Send the momentum flux (u) at bottom surface to the ocean model
-       WRITE ( 9, * )  '*** send usws to ocean'
-       CALL local_flush( 9 )
-       CALL MPI_SEND( usws(nys-1,nxl-1), ngp_xy, MPI_REAL, target_id, 15, &
-                      comm_inter, ierr )
-
-!
-!--    Send the momentum flux (v) at bottom surface to the ocean model
-       WRITE ( 9, * )  '*** send vsws to ocean'
-       CALL local_flush( 9 )
-       CALL MPI_SEND( vsws(nys-1,nxl-1), ngp_xy, MPI_REAL, target_id, 16, &
-                      comm_inter, ierr )
-
     ELSEIF ( coupling_mode == 'ocean_to_atmosphere' )  THEN
 
 !
-!--    Receive heat flux at the sea surface (top) from the atmosphere model
-       WRITE ( 9, * )  '*** receive tswst from atmosphere'
-       CALL local_flush( 9 )
-       CALL MPI_RECV( tswst(nys-1,nxl-1), ngp_xy, MPI_REAL, target_id, 12, &
-                      comm_inter, status, ierr )
-
-!
-!--    Receive humidity flux from the atmosphere model (bottom)
-!--    and add it to the heat flux at the sea surface (top)...
+!--    Horizontal grid size and number of processors is equal
+!--    in ocean and atmosphere
+       IF ( coupling_topology == 0 ) THEN
+!
+!--       Receive heat flux at the sea surface (top) from the atmosphere model
+          CALL MPI_RECV( tswst(nysg,nxlg), ngp_xy, MPI_REAL, &
+                         target_id, 12, comm_inter, status, ierr )
+
+
+!
+!--       Receive humidity flux from the atmosphere model (bottom)
+!--       and add it to the heat flux at the sea surface (top)...
+          IF ( humidity_remote )  THEN
+             CALL MPI_RECV( qswst_remote(nysg,nxlg), ngp_xy, MPI_REAL, &
+                            target_id, 13, comm_inter, status, ierr )
+
+          ENDIF
+
+!
+!--       Send sea surface temperature to the atmosphere model
+          CALL MPI_SEND( pt(nzt,nysg,nxlg), 1, type_xy, &
+                         target_id, 14, comm_inter, ierr )
+
+!
+!--       Receive momentum flux (u) at the sea surface (top) from the atmosphere
+!--       model
+          WRITE ( 9, * )  '*** receive uswst from atmosphere'
+          CALL local_flush( 9 )
+          CALL MPI_RECV( uswst(nysg,nxlg), ngp_xy, MPI_REAL, &
+                         target_id, 15, comm_inter, status, ierr )
+
+!
+!--       Receive momentum flux (v) at the sea surface (top) from the atmosphere
+!--       model
+          CALL MPI_RECV( vswst(nysg,nxlg), ngp_xy, MPI_REAL, &
+                         target_id, 16, comm_inter, status, ierr )
+
+!--       Send u to the atmosphere model
+          CALL MPI_SEND( u(nzt,nysg,nxlg), 1, type_xy, &
+                         target_id, 17, comm_inter, ierr )
+
+!
+!--       Send v to the atmosphere model
+          CALL MPI_SEND( v(nzt,nysg,nxlg), 1, type_xy, &
+                         target_id, 18, comm_inter, ierr )
+
+!
+!--    Horizontal gridsize or number of processors differs between 
+!--    ocean and atmosphere
+       ELSE
+
+!
+!--       Receive heat flux at the sea surface (top) from the atmosphere model
+          IF ( myid == 0 ) THEN
+             CALL MPI_RECV( total_2d_o(-nbgp,-nbgp), ngp_o, MPI_REAL, &
+                            target_id, 12, comm_inter, status, ierr )            
+          ENDIF
+          CALL MPI_BARRIER( comm2d, ierr )
+          CALL MPI_BCAST( total_2d_o(-nbgp,-nbgp), ngp_o, MPI_REAL, &
+                          0, comm2d, ierr)
+          tswst(nysg:nyng,nxlg:nxrg) = total_2d_o(nysg:nyng,nxlg:nxrg)
+
+!
+!--       Receive humidity flux at the sea surface (top) from the 
+!--       atmosphere model
+          IF ( humidity_remote ) THEN
+             IF ( myid == 0 ) THEN
+                CALL MPI_RECV( total_2d_o(-nbgp,-nbgp), ngp_o, MPI_REAL, &
+                               target_id, 13, comm_inter, status, ierr )            
+             ENDIF
+             CALL MPI_BARRIER( comm2d, ierr )
+             CALL MPI_BCAST( total_2d_o(-nbgp,-nbgp), ngp_o, MPI_REAL, &
+                             0, comm2d, ierr)
+             qswst_remote(nysg:nyng,nxlg:nxrg) = total_2d_o(nysg:nyng,nxlg:nxrg)
+          ENDIF
+
+!
+!--       Send surface temperature to atmosphere
+          total_2d_o = 0.0
+          total_2d = 0.0
+          total_2d(nys:nyn,nxl:nxr) = pt(nzt,nys:nyn,nxl:nxr)
+
+          CALL MPI_REDUCE(total_2d, total_2d_o, ngp_o, &
+                          MPI_REAL, MPI_SUM, 0, comm2d, ierr) 
+
+          CALL interpolate_to_atmos(14)
+
+!
+!--       Receive momentum flux (u) at the sea surface (top) from the 
+!--       atmosphere model
+          IF ( myid == 0 ) THEN
+             CALL MPI_RECV( total_2d_o(-nbgp,-nbgp), ngp_o, MPI_REAL, &
+                            target_id, 15, comm_inter, status, ierr )            
+          ENDIF
+          CALL MPI_BARRIER( comm2d, ierr )
+          CALL MPI_BCAST( total_2d_o(-nbgp,-nbgp), ngp_o, MPI_REAL, &
+                          0, comm2d, ierr)
+          uswst(nysg:nyng,nxlg:nxrg) = total_2d_o(nysg:nyng,nxlg:nxrg)
+
+!
+!--       Receive momentum flux (v) at the sea surface (top) from the 
+!--       atmosphere model
+          IF ( myid == 0 ) THEN
+             CALL MPI_RECV( total_2d_o(-nbgp,-nbgp), ngp_o, MPI_REAL, &
+                            target_id, 16, comm_inter, status, ierr )            
+          ENDIF
+          CALL MPI_BARRIER( comm2d, ierr )
+          CALL MPI_BCAST( total_2d_o(-nbgp,-nbgp), ngp_o, MPI_REAL, &
+                          0, comm2d, ierr)
+          vswst(nysg:nyng,nxlg:nxrg) = total_2d_o(nysg:nyng,nxlg:nxrg)
+
+!
+!--       Send u to atmosphere
+          total_2d_o = 0.0  
+          total_2d = 0.0
+          total_2d(nys:nyn,nxl:nxr) = u(nzt,nys:nyn,nxl:nxr)
+          CALL MPI_REDUCE(total_2d, total_2d_o, ngp_o, MPI_REAL, &
+                          MPI_SUM, 0, comm2d, ierr)  
+          CALL interpolate_to_atmos(17)
+
+!
+!--       Send v to atmosphere
+          total_2d_o = 0.0
+          total_2d = 0.0
+          total_2d(nys:nyn,nxl:nxr) = v(nzt,nys:nyn,nxl:nxr)
+          CALL MPI_REDUCE(total_2d, total_2d_o, ngp_o, MPI_REAL, &
+                          MPI_SUM, 0, comm2d, ierr)  
+          CALL interpolate_to_atmos(18)
+        
+       ENDIF
+
+!
+!--    Conversions of fluxes received from atmosphere
        IF ( humidity_remote )  THEN
-          WRITE ( 9, * )  '*** receive qswst_remote from atmosphere'
-          CALL local_flush( 9 )
-          CALL MPI_RECV( qswst_remote(nys-1,nxl-1), ngp_xy, MPI_REAL, &
-                         target_id, 13, comm_inter, status, ierr )
-
           !here tswst is still the sum of atmospheric bottom heat fluxes
           tswst = tswst + qswst_remote * 2.2626108e6 / 1005.0
@@ -146,9 +389,9 @@
           !/(rho_atm(=1.0)*c_p)
 !
-!--    ...and convert it to a salinity flux at the sea surface (top)
+!--        ...and convert it to a salinity flux at the sea surface (top)
 !--       following Steinhorn (1991), JPO 21, pp. 1681-1683:
 !--       S'w' = -S * evaporation / ( rho_water * ( 1 - S ) )
           saswst = -1.0 * sa(nzt,:,:) * qswst_remote /  &
-               ( rho(nzt,:,:) * ( 1.0 - sa(nzt,:,:) ) )
+                    ( rho(nzt,:,:) * ( 1.0 - sa(nzt,:,:) ) )
        ENDIF
 
@@ -156,29 +399,6 @@
 !--    Adjust the kinematic heat flux with respect to ocean density
 !--    (constants are the specific heat capacities for air and water)
-       !now tswst is the ocean top heat flux
+!--    now tswst is the ocean top heat flux
        tswst = tswst / rho(nzt,:,:) * 1005.0 / 4218.0
-
-!
-!--    Send sea surface temperature to the atmosphere model
-       WRITE ( 9, * )  '*** send pt to atmosphere'
-       CALL local_flush( 9 )
-       CALL MPI_SEND( pt(nzt,nys-1,nxl-1), 1, type_xy, target_id, 14, &
-                      comm_inter, ierr )
-
-!
-!--    Receive momentum flux (u) at the sea surface (top) from the atmosphere
-!--    model
-       WRITE ( 9, * )  '*** receive uswst from atmosphere'
-       CALL local_flush( 9 )
-       CALL MPI_RECV( uswst(nys-1,nxl-1), ngp_xy, MPI_REAL, target_id, 15, &
-                      comm_inter, status, ierr )
-
-!
-!--    Receive momentum flux (v) at the sea surface (top) from the atmosphere
-!--    model
-       WRITE ( 9, * )  '*** receive vswst from atmosphere'
-       CALL local_flush( 9 )
-       CALL MPI_RECV( vswst(nys-1,nxl-1), ngp_xy, MPI_REAL, target_id, 16, &
-                      comm_inter, status, ierr )
 
 !
@@ -187,4 +407,9 @@
        vswst = vswst / rho(nzt,:,:)
 
+
+    ENDIF
+
+    IF ( coupling_topology == 1 ) THEN
+       DEALLOCATE( total_2d_o, total_2d_a )
     ENDIF
 
@@ -193,3 +418,138 @@
 #endif
 
- END SUBROUTINE surface_coupler
+  END SUBROUTINE surface_coupler
+
+
+
+  SUBROUTINE interpolate_to_atmos(tag)
+
+    USE arrays_3d
+    USE control_parameters
+    USE grid_variables
+    USE indices
+    USE pegrid
+
+    IMPLICIT NONE
+
+  
+    INTEGER             ::  dnx, dnx2, dny, dny2, i, ii, j, jj
+    INTEGER, intent(in) ::  tag
+
+    CALL MPI_BARRIER( comm2d, ierr )
+
+    IF ( myid == 0 ) THEN
+
+! 
+!--    cyclic boundary conditions for the total 2D-grid
+       total_2d_o(-nbgp:-1,:) = total_2d_o(ny+1-nbgp:ny,:)
+       total_2d_o(:,-nbgp:-1) = total_2d_o(:,nx+1-nbgp:nx)
+
+       total_2d_o(ny+1:ny+nbgp,:) = total_2d_o(0:nbgp-1,:)
+       total_2d_o(:,nx+1:nx+nbgp) = total_2d_o(:,0:nbgp-1)
+
+!
+!--    Number of gridpoints of the fine grid within one mesh of the coarse grid
+       dnx = (nx_o+1) / (nx_a+1) 
+       dny = (ny_o+1) / (ny_a+1) 
+
+!
+!--    Distance for interpolation around coarse grid points within the fine grid 
+!--    (note: 2*dnx2 must not be equal with dnx)  
+       dnx2 = 2 * ( dnx / 2 )
+       dny2 = 2 * ( dny / 2 )
+
+       total_2d_a = 0.0
+!
+!--    Interpolation from ocean-grid-layer to atmosphere-grid-layer
+       DO  j = 0, ny_a
+          DO  i = 0, nx_a 
+             DO  jj = 0, dny2
+                DO  ii = 0, dnx2
+                   total_2d_a(j,i) = total_2d_a(j,i) &
+                                     + total_2d_o(j*dny+jj,i*dnx+ii)
+                ENDDO
+             ENDDO
+             total_2d_a(j,i) = total_2d_a(j,i) / ( ( dnx2 + 1 ) * ( dny2 + 1 ) )
+          ENDDO
+       ENDDO
+! 
+!--    cyclic boundary conditions for atmosphere grid
+       total_2d_a(-nbgp:-1,:) = total_2d_a(ny_a+1-nbgp:ny_a,:)
+       total_2d_a(:,-nbgp:-1) = total_2d_a(:,nx_a+1-nbgp:nx_a)
+       
+       total_2d_a(ny_a+1:ny_a+nbgp,:) = total_2d_a(0:nbgp-1,:)
+       total_2d_a(:,nx_a+1:nx_a+nbgp) = total_2d_a(:,0:nbgp-1)
+!
+!--    Transfer of the atmosphere-grid-layer to the atmosphere
+       CALL MPI_SEND( total_2d_a(-nbgp,-nbgp), ngp_a, MPI_REAL, &
+                      target_id, tag, comm_inter, ierr )
+
+    ENDIF
+
+    CALL MPI_BARRIER( comm2d, ierr )
+
+  END SUBROUTINE interpolate_to_atmos
+
+
+  SUBROUTINE interpolate_to_ocean(tag)
+
+    USE arrays_3d
+    USE control_parameters
+    USE grid_variables
+    USE indices
+    USE pegrid
+
+    IMPLICIT NONE
+
+    REAL                ::  fl, fr, myl, myr
+    INTEGER             ::  dnx, dny, i, ii, j, jj
+    INTEGER, intent(in) ::  tag
+
+    CALL MPI_BARRIER( comm2d, ierr )
+
+    IF ( myid == 0 ) THEN   
+
+!
+!      Number of gridpoints of the fine grid within one mesh of the coarse grid
+       dnx = ( nx_o + 1 ) / ( nx_a + 1 ) 
+       dny = ( ny_o + 1 ) / ( ny_a + 1 ) 
+
+! 
+!--    cyclic boundary conditions for atmosphere grid
+       total_2d_a(-nbgp:-1,:) = total_2d_a(ny+1-nbgp:ny,:)
+       total_2d_a(:,-nbgp:-1) = total_2d_a(:,nx+1-nbgp:nx)
+       
+       total_2d_a(ny+1:ny+nbgp,:) = total_2d_a(0:nbgp-1,:)
+       total_2d_a(:,nx+1:nx+nbgp) = total_2d_a(:,0:nbgp-1)
+!
+!--    Bilinear Interpolation from atmosphere-grid-layer to ocean-grid-layer
+       DO  j = 0, ny
+          DO  i = 0, nx
+             myl = ( total_2d_a(j+1,i)   - total_2d_a(j,i)   ) / dny
+             myr = ( total_2d_a(j+1,i+1) - total_2d_a(j,i+1) ) / dny
+             DO  jj = 0, dny-1
+                fl = myl*jj  + total_2d_a(j,i)  
+                fr = myr*jj  + total_2d_a(j,i+1)  
+                DO  ii = 0, dnx-1
+                   total_2d_o(j*dny+jj,i*dnx+ii) = ( fr - fl ) / dnx * ii + fl
+                ENDDO
+             ENDDO
+          ENDDO
+       ENDDO
+! 
+!--    cyclic boundary conditions for ocean grid
+       total_2d_o(-nbgp:-1,:) = total_2d_o(ny_o+1-nbgp:ny_o,:)
+       total_2d_o(:,-nbgp:-1) = total_2d_o(:,nx_o+1-nbgp:nx_o)
+
+       total_2d_o(ny_o+1:ny_o+nbgp,:) = total_2d_o(0:nbgp-1,:)
+       total_2d_o(:,nx_o+1:nx_o+nbgp) = total_2d_o(:,0:nbgp-1)
+       
+
+       CALL MPI_SEND( total_2d_o(-nbgp,-nbgp), ngp_o, MPI_REAL, &
+                      target_id, tag, comm_inter, ierr )
+
+    ENDIF
+
+    CALL MPI_BARRIER( comm2d, ierr )  
+
+  END SUBROUTINE interpolate_to_ocean
Index: /palm/trunk/SOURCE/time_integration.f90
===================================================================
--- /palm/trunk/SOURCE/time_integration.f90	(revision 666)
+++ /palm/trunk/SOURCE/time_integration.f90	(revision 667)
@@ -4,5 +4,6 @@
 ! Current revisions:
 ! -----------------
-! 
+! Calls of exchange_horiz are modified.
+! Adaption to slooping surface.
 !
 ! Former revisions:
@@ -94,5 +95,7 @@
 !-- determine and print out the run control parameters
     IF ( simulated_time == 0.0 )  CALL timestep
+
     CALL run_control
+
 
 !
@@ -126,5 +129,4 @@
 
        CALL cpu_log( log_point_s(10), 'timesteps', 'start' )
-
 !
 !--    Determine size of next time step
@@ -189,20 +191,20 @@
 !--       Exchange of ghost points (lateral boundary conditions)
           CALL cpu_log( log_point(26), 'exchange-horiz-progn', 'start' )
-          CALL exchange_horiz( u_p )
-          CALL exchange_horiz( v_p )
-          CALL exchange_horiz( w_p )
-          CALL exchange_horiz( pt_p )
-          IF ( .NOT. constant_diffusion       )  CALL exchange_horiz( e_p )
+          CALL exchange_horiz( u_p, nbgp )
+          CALL exchange_horiz( v_p, nbgp )
+          CALL exchange_horiz( w_p, nbgp )
+          CALL exchange_horiz( pt_p, nbgp )
+          IF ( .NOT. constant_diffusion )  CALL exchange_horiz( e_p, nbgp )
           IF ( ocean )  THEN
-             CALL exchange_horiz( sa_p )
-             CALL exchange_horiz( rho )
-             CALL exchange_horiz( prho )
-          ENDIF
-          IF ( humidity  .OR.  passive_scalar )  CALL exchange_horiz( q_p )
+             CALL exchange_horiz( sa_p, nbgp )
+             CALL exchange_horiz( rho, nbgp )
+             CALL exchange_horiz( prho, nbgp )
+          ENDIF
+          IF (humidity  .OR.  passive_scalar) CALL exchange_horiz( q_p, nbgp )
           IF ( cloud_droplets )  THEN
-             CALL exchange_horiz( ql )
-             CALL exchange_horiz( ql_c )
-             CALL exchange_horiz( ql_v )
-             CALL exchange_horiz( ql_vp )
+             CALL exchange_horiz( ql, nbgp )
+             CALL exchange_horiz( ql_c, nbgp )
+             CALL exchange_horiz( ql_v, nbgp )
+             CALL exchange_horiz( ql_vp, nbgp )
           ENDIF
 
@@ -228,6 +230,6 @@
 !--       when a sloping surface is used
           IF ( sloping_surface )  THEN
-             IF ( nxl ==  0 )  pt(:,:,nxl-1) = pt(:,:,nxl-1) - pt_slope_offset
-             IF ( nxr == nx )  pt(:,:,nxr+1) = pt(:,:,nxr+1) + pt_slope_offset
+             IF ( nxl ==  0 ) pt(:,:,nxlg:nxl-1) = pt(:,:,nxlg:nxl-1) - pt_slope_offset
+             IF ( nxr == nx )  pt(:,:,nxr+1:nxrg) = pt(:,:,nxr+1:nxrg) + pt_slope_offset
           ENDIF
 
@@ -563,4 +565,5 @@
        CALL cpu_log( log_point_s(10), 'timesteps', 'stop' )
 
+
     ENDDO   ! time loop
 
Index: /palm/trunk/SOURCE/timestep.f90
===================================================================
--- /palm/trunk/SOURCE/timestep.f90	(revision 666)
+++ /palm/trunk/SOURCE/timestep.f90	(revision 667)
@@ -4,5 +4,10 @@
 ! Current revisions:
 ! -----------------
-! 
+!
+!
+! Exchange of terminate_coupled between ocean and atmosphere via PE0
+!
+! Minimum grid spacing dxyz2_min(k) is now calculated using dzw instead of dzu
+!
 !
 ! Former revisions:
@@ -60,13 +65,15 @@
     REAL, DIMENSION(nzb+1:nzt) ::  dxyz2_min
 
+
+
     CALL cpu_log( log_point(12), 'calculate_timestep', 'start' )
 
 !
 !-- Determine the maxima of the velocity components.
-    CALL global_min_max( nzb, nzt+1, nys-1, nyn+1, nxl-1, nxr+1, u, 'abs', &
+    CALL global_min_max( nzb, nzt+1, nysg, nyng, nxlg, nxrg, u, 'abs', &
                          u_max, u_max_ijk )
-    CALL global_min_max( nzb, nzt+1, nys-1, nyn+1, nxl-1, nxr+1, v, 'abs', &
+    CALL global_min_max( nzb, nzt+1, nysg, nyng, nxlg, nxrg, v, 'abs', &
                          v_max, v_max_ijk )
-    CALL global_min_max( nzb, nzt+1, nys-1, nyn+1, nxl-1, nxr+1, w, 'abs', &
+    CALL global_min_max( nzb, nzt+1, nysg, nyng, nxlg, nxrg, w, 'abs', &
                          w_max, w_max_ijk )
 
@@ -152,5 +159,5 @@
 
        DO  k = nzb+1, nzt
-           dxyz2_min(k) = MIN( dx2, dy2, dzu(k)*dzu(k) ) * 0.125
+           dxyz2_min(k) = MIN( dx2, dy2, dzw(k)*dzw(k) ) * 0.125
        ENDDO
 
@@ -323,8 +330,11 @@
           IF ( coupling_mode /= 'uncoupled' .AND. terminate_coupled == 0 )  THEN
              terminate_coupled = 2
-             CALL MPI_SENDRECV( &
-                  terminate_coupled,        1, MPI_INTEGER, target_id,  0, &
-                  terminate_coupled_remote, 1, MPI_INTEGER, target_id,  0, &
-                  comm_inter, status, ierr )
+             IF ( myid == 0 ) THEN
+                CALL MPI_SENDRECV( &
+                     terminate_coupled,        1, MPI_INTEGER, target_id,  0, &
+                     terminate_coupled_remote, 1, MPI_INTEGER, target_id,  0, &
+                     comm_inter, status, ierr )
+             ENDIF
+             CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_INTEGER, 0, comm2d, ierr)
           ENDIF
 #endif
Index: /palm/trunk/SOURCE/user_3d_data_averaging.f90
===================================================================
--- /palm/trunk/SOURCE/user_3d_data_averaging.f90	(revision 666)
+++ /palm/trunk/SOURCE/user_3d_data_averaging.f90	(revision 667)
@@ -41,5 +41,5 @@
 !          CASE ( 'u2' )
 !             IF ( .NOT. ALLOCATED( u2_av ) )  THEN
-!                ALLOCATE( u2_av(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+!                ALLOCATE( u2_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
 !             ENDIF
 !             u2_av = 0.0
@@ -60,6 +60,6 @@
 !--       Sample for user-defined output:
 !          CASE ( 'u2' )
-!             DO  i = nxl-1, nxr+1
-!                DO  j = nys-1, nyn+1
+!             DO  i = nxlg, nxrg
+!                DO  j = nysg, nyng
 !                   DO  k = nzb, nzt+1
 !                      u2_av(k,j,i) = u2_av(k,j,i) + u2(k,j,i)
@@ -83,6 +83,6 @@
 !--       Sample for user-defined output:
 !          CASE ( 'u2' )
-!             DO  i = nxl-1, nxr+1
-!                DO  j = nys-1, nyn+1
+!             DO  i = nxlg, nxrg
+!                DO  j = nysg, nyng
 !                   DO  k = nzb, nzt+1
 !                      u2_av(k,j,i) = u2_av(k,j,i) / REAL( average_count_3d )
Index: /palm/trunk/SOURCE/user_actions.f90
===================================================================
--- /palm/trunk/SOURCE/user_actions.f90	(revision 666)
+++ /palm/trunk/SOURCE/user_actions.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng.
 !
 ! Former revisions:
@@ -69,13 +69,13 @@
 !--          data output)
 !--          Sample for user-defined output:
-!             DO  i = nxl-1, nxr+1
-!                DO  j = nys-1, nyn+1
-!                   DO  k = nzb, nzt+1
+!             DO  i = nxlg, nxrg
+!                DO  j = nysg, nyng
+!                   DO  k = nzb, nzt
 !                      u2(k,j,i) = u(k,j,i)**2
 !                   ENDDO
 !                ENDDO
 !             ENDDO
-!             DO  i = nxl-1, nxr
-!                DO  j = nys-1, nyn
+!             DO  i = nxlg, nxr
+!                DO  j = nysg, nyn
 !                   DO  k = nzb, nzt+1
 !                      ustvst(k,j,i) =  &
Index: /palm/trunk/SOURCE/user_data_output_2d.f90
===================================================================
--- /palm/trunk/SOURCE/user_data_output_2d.f90	(revision 666)
+++ /palm/trunk/SOURCE/user_data_output_2d.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng.
 !
 ! Former revisions:
@@ -34,5 +34,5 @@
     LOGICAL ::  found, two_d
 
-    REAL, DIMENSION(nxl-1:nxr+1,nys-1:nyn+1,nzb:nzt+1) ::  local_pf
+    REAL, DIMENSION(nxlg:nxrg,nysg:nyng,nzb:nzt+1) ::  local_pf
 
 
@@ -48,6 +48,6 @@
 !       CASE ( 'u2_xy', 'u2_xz', 'u2_yz' )
 !          IF ( av == 0 )  THEN
-!             DO  i = nxl-1, nxr+1
-!                DO  j = nys-1, nyn+1
+!             DO  i = nxlg, nxrg
+!                DO  j = nysg, nyng
 !                   DO  k = nzb, nzt+1
 !                      local_pf(i,j,k) = u2(k,j,i)
@@ -56,6 +56,6 @@
 !             ENDDO
 !          ELSE
-!             DO  i = nxl-1, nxr+1
-!                DO  j = nys-1, nyn+1
+!             DO  i = nxlg, nxrg
+!                DO  j = nysg, nyng
 !                   DO  k = nzb, nzt+1
 !                      local_pf(i,j,k) = u2_av(k,j,i)
Index: /palm/trunk/SOURCE/user_data_output_3d.f90
===================================================================
--- /palm/trunk/SOURCE/user_data_output_3d.f90	(revision 666)
+++ /palm/trunk/SOURCE/user_data_output_3d.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng.
 !
 ! Former revisions:
@@ -31,5 +31,5 @@
     LOGICAL ::  found
 
-    REAL(spk), DIMENSION(nxl-1:nxr+1,nys-1:nyn+1,nzb:nz_do) ::  local_pf
+   REAL(spk), DIMENSION(nxlg:nxrg,nysg:nyng,nzb:nz_do) ::  local_pf
 
 
@@ -45,6 +45,6 @@
 !       CASE ( 'u2' )
 !          IF ( av == 0 )  THEN
-!             DO  i = nxl-1, nxr+1
-!                DO  j = nys-1, nyn+1
+!             DO  i = nxlg, nxrg
+!                DO  j = nysg, nyng
 !                   DO  k = nzb, nz_do
 !                      local_pf(i,j,k) = u2(k,j,i)
@@ -53,6 +53,6 @@
 !             ENDDO
 !          ELSE
-!             DO  i = nxl-1, nxr+1
-!                DO  j = nys-1, nyn+1
+!             DO  i = nxlg, nxrg
+!                DO  j = nysg, nyng
 !                   DO  k = nzb, nz_do
 !                      local_pf(i,j,k) = u2_av(k,j,i)
Index: /palm/trunk/SOURCE/user_init.f90
===================================================================
--- /palm/trunk/SOURCE/user_init.f90	(revision 666)
+++ /palm/trunk/SOURCE/user_init.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng.
 !
 ! Former revisions:
@@ -30,6 +30,6 @@
 !-- Here the user-defined initializing actions follow:
 !-- Sample for user-defined output
-!    ALLOCATE( u2(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-!    ALLOCATE( ustvst(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) );  ustvst = 0.0
+!    ALLOCATE( u2(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+!    ALLOCATE( ustvst(nzb:nzt+1,nysg:nyng,nxlg:nxrg) );  ustvst = 0.0
 
 !-- Sample for user-defined time series
Index: /palm/trunk/SOURCE/user_init_plant_canopy.f90
===================================================================
--- /palm/trunk/SOURCE/user_init_plant_canopy.f90	(revision 666)
+++ /palm/trunk/SOURCE/user_init_plant_canopy.f90	(revision 667)
@@ -4,5 +4,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng.
 !
 ! Former revisions:
@@ -49,7 +49,7 @@
 !--       plant canopy extends only over the second half of the 
 !--       model domain along x
-!          DO  i = nxl-1, nxr+1
+!          DO  i = nxlg, nxrg
 !             IF ( i >= INT(nx+1/2) ) THEN
-!                DO  j = nys-1, nyn+1
+!                DO  j = nysg, nyng
 !                   lad_s(:,j,i) = lad(:)
 !                   cdc(:,j,i)   = drag_coefficient
Index: /palm/trunk/SOURCE/user_read_restart_data.f90
===================================================================
--- /palm/trunk/SOURCE/user_read_restart_data.f90	(revision 666)
+++ /palm/trunk/SOURCE/user_read_restart_data.f90	(revision 667)
@@ -7,5 +7,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! Allocation with nbgp.
 !
 ! Former revisions:
@@ -50,9 +50,8 @@
                                                        offset_ya
 
-    REAL, DIMENSION(nys_on_file-1:nyn_on_file+1,nxl_on_file-1:nxr_on_file+1) ::&
+    REAL, DIMENSION(nys_on_file-nbgp:nyn_on_file+nbgp,nxl_on_file-nbgp:nxr_on_file+nbgp) :: &
           tmp_2d
 
-    REAL, DIMENSION(nzb:nzt+1,nys_on_file-1:nyn_on_file+1, &
-                    nxl_on_file-1:nxr_on_file+1) ::        &
+    REAL, DIMENSION(nzb:nzt+1,nys_on_file-nbgp:nyn_on_file+nbgp,nxl_on_file-nbgp:nxr_on_file+nbgp) :: &
           tmp_3d
 
Index: /palm/trunk/SOURCE/wall_fluxes.f90
===================================================================
--- /palm/trunk/SOURCE/wall_fluxes.f90	(revision 666)
+++ /palm/trunk/SOURCE/wall_fluxes.f90	(revision 667)
@@ -62,10 +62,10 @@
        INTEGER ::  i, j, k, wall_index
 
-       INTEGER, DIMENSION(nys-1:nyn+1,nxl-1:nxr+1) ::  nzb_uvw_inner, &
+       INTEGER, DIMENSION(nysg:nyng,nxlg:nxrg) ::  nzb_uvw_inner, &
                                                        nzb_uvw_outer
        REAL ::  a, b, c1, c2, h1, h2, zp
        REAL ::  pts, pt_i, rifs, u_i, v_i, us_wall, vel_total, ws, wspts
 
-       REAL, DIMENSION(nys-1:nyn+1,nxl-1:nxr+1)   ::  wall
+       REAL, DIMENSION(nysg:nyng,nxlg:nxrg)   ::  wall
        REAL, DIMENSION(nzb:nzt+1,nys:nyn,nxl:nxr) ::  wall_flux
 
@@ -348,5 +348,5 @@
        REAL ::  rifs
 
-       REAL, DIMENSION(nys-1:nyn+1,nxl-1:nxr+1)   ::  wall
+       REAL, DIMENSION(nysg:nyng,nxlg:nxrg)   ::  wall
        REAL, DIMENSION(nzb:nzt+1,nys:nyn,nxl:nxr) ::  wall_flux
 
Index: /palm/trunk/SOURCE/write_compressed.f90
===================================================================
--- /palm/trunk/SOURCE/write_compressed.f90	(revision 666)
+++ /palm/trunk/SOURCE/write_compressed.f90	(revision 667)
@@ -1,9 +1,9 @@
  SUBROUTINE write_compressed( field, fid_avs, fid_fld, my_id, nxl, nxr, nyn, &
-                              nys, nzb, nz_do3d, prec )
+                              nys, nzb, nz_do3d, prec, nbgp )
 
 !------------------------------------------------------------------------------!
 ! Current revisions:
 ! -----------------
-! 
+! Array bounds and nx, ny adapted with nbgp.
 !
 ! Former revisions:
@@ -58,7 +58,7 @@
 
     INTEGER, INTENT(IN) ::  fid_avs, fid_fld, my_id, nxl, nxr, nyn, nys, nzb, &
-                            nz_do3d, prec
-
-    REAL(spk), INTENT(IN) :: field(1:((nxr-nxl+3)*(nyn-nys+3)*(nz_do3d-nzb+1)))
+                            nz_do3d, prec, nbgp
+
+    REAL(spk), INTENT(IN) :: field(1:((nxr-nxl+1+2*nbgp)*(nyn-nys+1+2*nbgp)*(nz_do3d-nzb+1)))
 
 !
@@ -73,6 +73,6 @@
     ENDDO
 
-    nx     = nxr - nxl + 2
-    ny     = nyn - nys + 2
+    nx     = nxr - nxl + 2*nbgp
+    ny     = nyn - nys + 2*nbgp
     nz     = nz_do3d - nzb
     length = (nx+1) * (ny+1) * (nz+1)
