Index: /palm/trunk/SOURCE/module_interface.f90
===================================================================
--- /palm/trunk/SOURCE/module_interface.f90	(revision 4752)
+++ /palm/trunk/SOURCE/module_interface.f90	(revision 4753)
@@ -1,20 +1,19 @@
 !> @file module_interface.f90
-!------------------------------------------------------------------------------!
-! This file is part of PALM.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
+!--------------------------------------------------------------------------------------------------!
+! This file is part of the PALM model system.
+!
+! PALM is free software: you can redistribute it and/or modify it under the terms of the GNU General
+! Public License as published by the Free Software Foundation, either version 3 of the License, or
+! (at your option) any later version.
+!
+! PALM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
+! implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+! Public License for more details.
+!
+! You should have received a copy of the GNU General Public License along with PALM. If not, see
+! <http://www.gnu.org/licenses/>.
 !
 ! Copyright 1997-2020 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 !
 ! Current revisions:
@@ -25,4 +24,7 @@
 ! -----------------
 ! $Id$
+! file re-formatted to follow the PALM coding standard
+!
+! 4731 2020-10-07 13:25:11Z schwenkel
 ! Move exchange_horiz from time_integration to modules
 !
@@ -32,72 +34,70 @@
 ! 4590 2020-07-06 14:34:59Z suehring
 ! Enable mpi-io for biomet
-! 
+!
 ! 4525 2020-05-10 17:05:07Z raasch
 ! added restart I/O for global salsa data,
 ! added restart with MPI-IO for salsa
-! 
+!
 ! 4518 2020-05-04 15:44:28Z suehring
 ! Call of doq_rrd_local enabled
-! 
+!
 ! 4517 2020-05-03 14:29:30Z raasch
 ! added restart with MPI-IO for reading local arrays
-! 
+!
 ! 4514 2020-04-30 16:29:59Z suehring
 ! Added global restart routines for plant-canopy model
-! 
+!
 ! 4495 2020-04-13 20:11:20Z raasch
 ! restart data handling with MPI-IO added
-! 
+!
 ! 4414 2020-02-19 20:16:04Z suehring
 ! Add module interface for basic initialization of numerics.
-! 
+!
 ! 4411 2020-02-18 14:28:02Z maronga
 ! Added output routines for WTM
-! 
+!
 ! 4407 2020-02-13 20:31:44Z knoop
 ! Changed program_debug_output_unit to 9 in dom_init call
-! 
+!
 ! 4400 2020-02-10 20:32:41Z suehring
 ! - Use data-output module for virtual measurement output
 ! - Remove deprecated routines for virtual measurement module
-! 
+!
 ! 4361 2020-01-07 12:22:38Z suehring
 ! Remove unused arrays in pmc_rrd_local
-! 
+!
 ! 4360 2020-01-07 11:25:50Z suehring
 ! Add pcm_rrd_local and pcm_wrd_local
-! 
+!
 ! 4331 2019-12-10 18:25:02Z suehring
-! Change interface for doq_check_data_output, in order to perform further 
-! output checks.
-! 
+! Change interface for doq_check_data_output, in order to perform further output checks.
+!
 ! 4281 2019-10-29 15:15:39Z schwenkel
 ! Added dynamics boundary conditions
-! 
+!
 ! 4272 2019-10-23 15:18:57Z schwenkel
-! Further modularization of boundary conditions: moved boundary conditions to
-! respective modules
+! Further modularization of boundary conditions: moved boundary conditions to respective modules
 !
 ! 4268 2019-10-17 11:29:38Z schwenkel
 ! Introduction of module_interface_boundary_conditions
-! 
+!
 ! 4182 2019-08-22 15:20:23Z scharf
 ! Corrected "Former revisions" section
-! 
+!
 ! 4173 2019-08-20 12:04:06Z gronemeier
 ! add vdi_internal_controls
-! 
+!
 ! 4157 2019-08-14 09:19:12Z suehring
 ! Call doq_init from module interface
-! 
+!
 ! 4132 2019-08-02 12:34:17Z suehring
 ! Bugfix in masked data output for diagnostic quantities
-! 
+!
 ! 4131 2019-08-02 11:06:18Z monakurppa
 ! Add output of 3D plant-canopy outputs (merge from branch resler)
-! 
+!
 ! 4048 2019-06-21 21:00:21Z knoop
 ! Moved turbulence_closure_mod calls into this module_interface
-! 
+!
 ! 4047 2019-06-21 18:58:09Z knoop
 ! Introduction of the dynamics module
@@ -110,8 +110,7 @@
 !
 ! 4017 2019-06-06 12:16:46Z schwenkel
-! local_pf need INTENT(INOUT) attribute rather than INTENT(OUT). This is
-! because INTENT(OUT) sets the array to not-defined. Especially for outputs that
-! are not defined everywhere, e.g. land-surface outputs, this will be
-! problematic as NaN will be output.
+! local_pf need INTENT(INOUT) attribute rather than INTENT(OUT). This is because INTENT(OUT) sets
+! the array to not-defined. Especially for outputs that are not defined everywhere, e.g.
+! land-surface outputs, this will be problematic as NaN will be output.
 !
 ! 3987 2019-05-22 09:52:13Z kanani
@@ -119,10 +118,8 @@
 !
 ! 3956 2019-05-07 12:32:52Z monakurppa
-! - Added calls for salsa_non_advective_processes and
-!   salsa_exchange_horiz_bounds
-! - Moved the call for salsa_data_output_2d/3d before that of
-!   radiation_data_output_2d/3d. radiation_data_output_2d/3d tries to read a
-!   salsa output variable and encounters a segmentation fault for "Ntot" due
-!   to the shortoutput name
+! - Added calls for salsa_non_advective_processes and salsa_exchange_horiz_bounds
+! - Moved the call for salsa_data_output_2d/3d before that of radiation_data_output_2d/3d.
+!   radiation_data_output_2d/3d tries to read a salsa output variable and encounters a segmentation
+!   fault for "Ntot" due to the shortoutput name
 !
 ! 3931 2019-04-24 16:34:28Z schwenkel
@@ -133,6 +130,6 @@
 !
 ! 3887 2019 -04-12 08:47:41Z schwenkel
-! Changes related to global restructuring of location messages and introduction
-! of additional debug messages
+! Changes related to global restructuring of location messages and introduction of additional debug
+! messages
 !
 ! 3880 2019 -04-08 21:43:02Z knoop
@@ -149,7 +146,6 @@
 !
 ! 3766 2019-02-26 16:23:41Z raasch
-! first argument removed from module_interface_rrd_*, statement added to avoid
-! compiler warning about unused variable, file reformatted with respect to coding
-! standards
+! first argument removed from module_interface_rrd_*, statement added to avoid compiler warning
+! about unused variable, file reformatted with respect to coding standards
 !
 ! 3762 2019-02-25 16:54:16Z suehring
@@ -166,6 +162,5 @@
 !
 ! 3735 2019-02-12 09:52:40Z dom_dwd_user
-! Accepting variable j from check_parameters and passing it to
-! bio_check_data_output
+! Accepting variable j from check_parameters and passing it to bio_check_data_output
 ! Add required restart data for surface output module
 !
@@ -189,6 +184,6 @@
 !
 ! 3649 2019-01-02 16:52:21Z suehring
-! Initialize strings, in order to avoid compiler warnings for non-initialized
-! characters with intent(out) attribute
+! Initialize strings, in order to avoid compiler warnings for non-initialized characters with
+! intent(out) attribute
 !
 ! 3648 2019-01-02 16:35:46Z suehring
@@ -202,13 +197,13 @@
 !>
 !> @todo Re-format module to be consistent with coding standard
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  MODULE module_interface
 
-    USE indices,                                                               &
+    USE indices,                                                                                   &
         ONLY:  nbgp, nxl, nxlg, nxr, nxrg, nys, nysg, nyn, nyng, nzb, nzt
 
     USE kinds
 
-    USE pegrid,                                                                &
+    USE pegrid,                                                                                    &
         ONLY:  comm2d
 
@@ -216,372 +211,372 @@
 !-- load module-specific control parameters.
 !-- ToDo: move all of them to respective module or a dedicated central module
-    USE data_output_module,                                                    &
-        ONLY:  dom_def_end,                                                    &
-               dom_finalize_output,                                            &
+    USE data_output_module,                                                                        &
+        ONLY:  dom_def_end,                                                                        &
+               dom_finalize_output,                                                                &
                dom_init
 
-    USE dynamics_mod, &
-        ONLY:  dynamics_parin, &
-               dynamics_check_parameters, &
-               dynamics_check_data_output_ts, &
-               dynamics_check_data_output_pr, &
-               dynamics_check_data_output, &
-               dynamics_init_masks, &
-               dynamics_define_netcdf_grid, &
-               dynamics_init_arrays, &
-               dynamics_init, &
-               dynamics_init_checks, &
-               dynamics_header, &
-               dynamics_actions, &
-               dynamics_non_advective_processes, &
-               dynamics_exchange_horiz, &
-               dynamics_prognostic_equations, &
-               dynamics_boundary_conditions, &
-               dynamics_swap_timelevel, &
-               dynamics_3d_data_averaging, &
-               dynamics_data_output_2d, &
-               dynamics_data_output_3d, &
-               dynamics_statistics, &
-               dynamics_rrd_global, &
-               dynamics_rrd_local, &
-               dynamics_wrd_global, &
-               dynamics_wrd_local, &
-               dynamics_last_actions
-
-    USE turbulence_closure_mod, &
-        ONLY:  tcm_check_parameters, &
-               tcm_check_data_output, &
-               tcm_init_arrays, &
-               tcm_init, &
-               tcm_actions, &
-               tcm_prognostic_equations, &
-               tcm_boundary_conds, &
-               tcm_swap_timelevel, &
-               tcm_3d_data_averaging, &
-               tcm_data_output_2d, &
-               tcm_data_output_3d
-
-    USE control_parameters,                                                    &
-        ONLY:  air_chemistry,                                                  &
-               biometeorology,                                                 &
-               coupling_char,                                                  &
-               debug_output,                                                   &
-               debug_output_timestep,                                          &
-               indoor_model,                                                   &
-               land_surface,                                                   &
-               large_scale_forcing,                                            &
-               nesting_offline,                                                &
-               nudging,                                                        &
-               ocean_mode,                                                     &
-               plant_canopy,                                                   &
-               salsa,                                                          &
-               surface_output,                                                 &
-               syn_turb_gen,                                                   &
-               urban_surface,                                                  &
-               vdi_checks,                                                     &
-               virtual_flight,                                                 &
-               virtual_measurement,                                            &
+    USE dynamics_mod,                                                                              &
+        ONLY:  dynamics_3d_data_averaging,                                                         &
+               dynamics_actions,                                                                   &
+               dynamics_boundary_conditions,                                                       &
+               dynamics_check_data_output,                                                         &
+               dynamics_check_data_output_pr,                                                      &
+               dynamics_check_data_output_ts,                                                      &
+               dynamics_check_parameters,                                                          &
+               dynamics_data_output_2d,                                                            &
+               dynamics_data_output_3d,                                                            &
+               dynamics_define_netcdf_grid,                                                        &
+               dynamics_exchange_horiz,                                                            &
+               dynamics_header,                                                                    &
+               dynamics_init,                                                                      &
+               dynamics_init_arrays,                                                               &
+               dynamics_init_checks,                                                               &
+               dynamics_init_masks,                                                                &
+               dynamics_last_actions,                                                              &
+               dynamics_non_advective_processes,                                                   &
+               dynamics_parin,                                                                     &
+               dynamics_prognostic_equations,                                                      &
+               dynamics_rrd_global,                                                                &
+               dynamics_rrd_local,                                                                 &
+               dynamics_statistics,                                                                &
+               dynamics_swap_timelevel,                                                            &
+               dynamics_wrd_global,                                                                &
+               dynamics_wrd_local
+
+    USE turbulence_closure_mod,                                                                    &
+        ONLY:  tcm_3d_data_averaging,                                                              &
+               tcm_actions,                                                                        &
+               tcm_boundary_conds,                                                                 &
+               tcm_check_data_output,                                                              &
+               tcm_check_parameters,                                                               &
+               tcm_data_output_2d,                                                                 &
+               tcm_data_output_3d,                                                                 &
+               tcm_init,                                                                           &
+               tcm_init_arrays,                                                                    &
+               tcm_prognostic_equations,                                                           &
+               tcm_swap_timelevel
+
+    USE control_parameters,                                                                        &
+        ONLY:  air_chemistry,                                                                      &
+               biometeorology,                                                                     &
+               coupling_char,                                                                      &
+               debug_output,                                                                       &
+               debug_output_timestep,                                                              &
+               indoor_model,                                                                       &
+               land_surface,                                                                       &
+               large_scale_forcing,                                                                &
+               nesting_offline,                                                                    &
+               nudging,                                                                            &
+               ocean_mode,                                                                         &
+               plant_canopy,                                                                       &
+               salsa,                                                                              &
+               surface_output,                                                                     &
+               syn_turb_gen,                                                                       &
+               urban_surface,                                                                      &
+               vdi_checks,                                                                         &
+               virtual_flight,                                                                     &
+               virtual_measurement,                                                                &
                wind_turbine
 
 !
-!-- load interface routines of all PALM modules
-    USE biometeorology_mod,                                                    &
-        ONLY:  bio_parin,                                                      &
-               bio_check_data_output,                                          &
-               bio_init,                                                       &
-               bio_init_checks,                                                &
-               bio_header,                                                     &
-               bio_3d_data_averaging,                                          &
-               bio_data_output_2d,                                             &
-               bio_data_output_3d,                                             &
-               bio_rrd_global,                                                 &
-               bio_rrd_local,                                                  &
-               bio_wrd_global,                                                 &
+!-- Load interface routines of all PALM modules
+    USE biometeorology_mod,                                                                        &
+        ONLY:  bio_3d_data_averaging,                                                              &
+               bio_check_data_output,                                                              &
+               bio_data_output_2d,                                                                 &
+               bio_data_output_3d,                                                                 &
+               bio_init,                                                                           &
+               bio_init_checks,                                                                    &
+               bio_header,                                                                         &
+               bio_parin,                                                                          &
+               bio_rrd_global,                                                                     &
+               bio_rrd_local,                                                                      &
+               bio_wrd_global,                                                                     &
                bio_wrd_local
 
-    USE bulk_cloud_model_mod,                                                  &
-        ONLY:  bulk_cloud_model,                                               &
-               bcm_parin,                                                      &
-               bcm_check_parameters,                                           &
-               bcm_check_data_output_pr,                                       &
-               bcm_check_data_output,                                          &
-               bcm_init_arrays,                                                &
-               bcm_init,                                                       &
-               bcm_header,                                                     &
-               bcm_actions,                                                    &
-               bcm_non_advective_processes,                                    &
-               bcm_exchange_horiz,                                             &
-               bcm_prognostic_equations,                                       &
-               bcm_boundary_conditions,                                        &
-               bcm_swap_timelevel,                                             &
-               bcm_3d_data_averaging,                                          &
-               bcm_data_output_2d,                                             &
-               bcm_data_output_3d,                                             &
-               bcm_rrd_global,                                                 &
-               bcm_wrd_global,                                                 &
-               bcm_rrd_local,                                                  &
-               bcm_wrd_local
-
-   USE chemistry_model_mod,                                                    &
-       ONLY:  chem_parin,                                                      &
-              chem_check_parameters,                                           &
-              chem_check_data_output_pr,                                       &
-              chem_check_data_output,                                          &
-              chem_exchange_horiz_bounds,                                      &
-              chem_init_arrays,                                                &
-              chem_init,                                                       &
-              chem_header,                                                     &
-              chem_actions,                                                    &
-              chem_non_advective_processes,                                    &
-              chem_prognostic_equations,                                       &
-              chem_boundary_conditions,                                        &
-              chem_swap_timelevel,                                             &
-              chem_3d_data_averaging,                                          &
-              chem_data_output_2d,                                             &
-              chem_data_output_3d,                                             &
-              chem_statistics,                                                 &
-              chem_rrd_local,                                                  &
+    USE bulk_cloud_model_mod,                                                                      &
+        ONLY:  bcm_3d_data_averaging,                                                              &
+               bcm_actions,                                                                        &
+               bcm_boundary_conditions,                                                            &
+               bcm_check_data_output_pr,                                                           &
+               bcm_check_data_output,                                                              &
+               bcm_check_parameters,                                                               &
+               bcm_data_output_2d,                                                                 &
+               bcm_data_output_3d,                                                                 &
+               bcm_exchange_horiz,                                                                 &
+               bcm_header,                                                                         &
+               bcm_init_arrays,                                                                    &
+               bcm_init,                                                                           &
+               bcm_non_advective_processes,                                                        &
+               bcm_parin,                                                                          &
+               bcm_prognostic_equations,                                                           &
+               bcm_rrd_global,                                                                     &
+               bcm_rrd_local,                                                                      &
+               bcm_swap_timelevel,                                                                 &
+               bcm_wrd_global,                                                                     &
+               bcm_wrd_local,                                                                      &
+               bulk_cloud_model
+
+   USE chemistry_model_mod,                                                                        &
+       ONLY:  chem_3d_data_averaging,                                                              &
+              chem_actions,                                                                        &
+              chem_boundary_conditions,                                                            &
+              chem_check_data_output,                                                              &
+              chem_check_data_output_pr,                                                           &
+              chem_check_parameters,                                                               &
+              chem_data_output_2d,                                                                 &
+              chem_data_output_3d,                                                                 &
+              chem_exchange_horiz_bounds,                                                          &
+              chem_header,                                                                         &
+              chem_init,                                                                           &
+              chem_init_arrays,                                                                    &
+              chem_non_advective_processes,                                                        &
+              chem_parin,                                                                          &
+              chem_prognostic_equations,                                                           &
+              chem_rrd_local,                                                                      &
+              chem_statistics,                                                                     &
+              chem_swap_timelevel,                                                                 &
               chem_wrd_local
 
-    USE diagnostic_output_quantities_mod,                                      &
-        ONLY:  doq_3d_data_averaging,                                          &
-               doq_check_data_output,                                          &
-               doq_define_netcdf_grid,                                         &
-               doq_init,                                                       &
-               doq_output_2d,                                                  &
-               doq_output_3d,                                                  &
-               doq_rrd_local,                                                  &
+    USE diagnostic_output_quantities_mod,                                                          &
+        ONLY:  doq_3d_data_averaging,                                                              &
+               doq_check_data_output,                                                              &
+               doq_define_netcdf_grid,                                                             &
+               doq_init,                                                                           &
+               doq_output_2d,                                                                      &
+               doq_output_3d,                                                                      &
+               doq_rrd_local,                                                                      &
                doq_wrd_local
 
-    USE flight_mod,                                                            &
-        ONLY:  flight_parin,                                                   &
-               flight_header,                                                  &
-               flight_init,                                                    &
-               flight_rrd_global,                                              &
+    USE flight_mod,                                                                                &
+        ONLY:  flight_header,                                                                      &
+               flight_init,                                                                        &
+               flight_parin,                                                                       &
+               flight_rrd_global,                                                                  &
                flight_wrd_global
 
-    USE gust_mod,                                                              &
-        ONLY:  gust_module_enabled,                                            &
-               gust_parin,                                                     &
-               gust_check_parameters,                                          &
-               gust_check_data_output_pr,                                      &
-               gust_check_data_output,                                         &
-               gust_init_arrays,                                               &
-               gust_init,                                                      &
-               gust_header,                                                    &
-               gust_actions,                                                   &
-               gust_prognostic_equations,                                      &
-               gust_swap_timelevel,                                            &
-               gust_3d_data_averaging,                                         &
-               gust_data_output_2d,                                            &
-               gust_data_output_3d,                                            &
-               gust_statistics,                                                &
-               gust_rrd_global,                                                &
-               gust_wrd_global,                                                &
-               gust_rrd_local,                                                 &
+    USE gust_mod,                                                                                  &
+        ONLY:  gust_3d_data_averaging,                                                             &
+               gust_actions,                                                                       &
+               gust_check_parameters,                                                              &
+               gust_check_data_output,                                                             &
+               gust_check_data_output_pr,                                                          &
+               gust_data_output_2d,                                                                &
+               gust_data_output_3d,                                                                &
+               gust_header,                                                                        &
+               gust_init,                                                                          &
+               gust_init_arrays,                                                                   &
+               gust_module_enabled,                                                                &
+               gust_parin,                                                                         &
+               gust_prognostic_equations,                                                          &
+               gust_rrd_global,                                                                    &
+               gust_rrd_local,                                                                     &
+               gust_statistics,                                                                    &
+               gust_swap_timelevel,                                                                &
+               gust_wrd_global,                                                                    &
                gust_wrd_local
 
-    USE indoor_model_mod,                                                      &
-        ONLY:  im_parin,                                                       &
-               im_check_data_output,                                           &
-               im_check_parameters,                                            &
-               im_data_output_3d,                                              &
-               im_init
-
-    USE lagrangian_particle_model_mod,                                         &
-        ONLY:  lpm_parin,                                                      &
-               lpm_header,                                                     &
-               lpm_check_parameters,                                           &
-               lpm_exchange_horiz_bounds,                                      &
-               lpm_init_arrays,                                                &
-               lpm_init,                                                       &
-               lpm_actions,                                                    &
-               lpm_rrd_global,                                                 &
-               lpm_rrd_local,                                                  &
-               lpm_wrd_local,                                                  &
+    USE indoor_model_mod,                                                                          &
+        ONLY:  im_check_data_output,                                                               &
+               im_check_parameters,                                                                &
+               im_data_output_3d,                                                                  &
+               im_init,                                                                            &
+               im_parin
+
+    USE lagrangian_particle_model_mod,                                                             &
+        ONLY:  lpm_actions,                                                                        &
+               lpm_check_parameters,                                                               &
+               lpm_exchange_horiz_bounds,                                                          &
+               lpm_header,                                                                         &
+               lpm_init,                                                                           &
+               lpm_init_arrays,                                                                    &
+               lpm_parin,                                                                          &
+               lpm_rrd_global,                                                                     &
+               lpm_rrd_local,                                                                      &
+               lpm_wrd_local,                                                                      &
                lpm_wrd_global
 
-    USE land_surface_model_mod,                                                &
-        ONLY:  lsm_parin,                                                      &
-               lsm_check_parameters,                                           &
-               lsm_check_data_output_pr,                                       &
-               lsm_check_data_output,                                          &
-               lsm_init_arrays,                                                &
-               lsm_init,                                                       &
-               lsm_header,                                                     &
-               lsm_swap_timelevel,                                             &
-               lsm_3d_data_averaging,                                          &
-               lsm_data_output_2d,                                             &
-               lsm_rrd_local,                                                  &
+    USE land_surface_model_mod,                                                                    &
+        ONLY:  lsm_3d_data_averaging,                                                              &
+               lsm_check_parameters,                                                               &
+               lsm_check_data_output,                                                              &
+               lsm_check_data_output_pr,                                                           &
+               lsm_data_output_2d,                                                                 &
+               lsm_header,                                                                         &
+               lsm_init,                                                                           &
+               lsm_init_arrays,                                                                    &
+               lsm_parin,                                                                          &
+               lsm_rrd_local,                                                                      &
+               lsm_swap_timelevel,                                                                 &
                lsm_wrd_local
 
-    USE lsf_nudging_mod,                                                       &
-        ONLY:  lsf_nudging_check_parameters,                                   &
-               lsf_nudging_check_data_output_pr,                               &
-               lsf_init,                                                       &
-               nudge_init,                                                     &
-               lsf_nudging_header
-
-    USE multi_agent_system_mod,                                                &
+    USE lsf_nudging_mod,                                                                           &
+        ONLY:  lsf_nudging_check_parameters,                                                       &
+               lsf_nudging_check_data_output_pr,                                                   &
+               lsf_nudging_header,                                                                 &
+               lsf_init,                                                                           &
+               nudge_init
+
+    USE multi_agent_system_mod,                                                                    &
         ONLY:  mas_parin
 
-    USE nesting_offl_mod,                                                      &
-        ONLY:  nesting_offl_parin,                                             &
-               nesting_offl_check_parameters,                                  &
-               nesting_offl_header
-
-    USE ocean_mod,                                                             &
-        ONLY:  ocean_parin,                                                    &
-               ocean_check_parameters,                                         &
-               ocean_check_data_output_pr,                                     &
-               ocean_check_data_output,                                        &
-               ocean_exchange_horiz,                                           &
-               ocean_init_arrays,                                              &
-               ocean_init,                                                     &
-               ocean_header,                                                   &
-               ocean_actions,                                                  &
-               ocean_prognostic_equations,                                     &
-               ocean_boundary_conditions,                                      &
-               ocean_swap_timelevel,                                           &
-               ocean_3d_data_averaging,                                        &
-               ocean_data_output_2d,                                           &
-               ocean_data_output_3d,                                           &
-               ocean_rrd_global,                                               &
-               ocean_wrd_global,                                               &
-               ocean_rrd_local,                                                &
+    USE nesting_offl_mod,                                                                          &
+        ONLY:  nesting_offl_check_parameters,                                                      &
+               nesting_offl_header,                                                                &
+               nesting_offl_parin
+
+    USE ocean_mod,                                                                                 &
+        ONLY:  ocean_3d_data_averaging,                                                            &
+               ocean_actions,                                                                      &
+               ocean_boundary_conditions,                                                          &
+               ocean_check_parameters,                                                             &
+               ocean_check_data_output,                                                            &
+               ocean_check_data_output_pr,                                                         &
+               ocean_data_output_2d,                                                               &
+               ocean_data_output_3d,                                                               &
+               ocean_exchange_horiz,                                                               &
+               ocean_init_arrays,                                                                  &
+               ocean_init,                                                                         &
+               ocean_header,                                                                       &
+               ocean_parin,                                                                        &
+               ocean_prognostic_equations,                                                         &
+               ocean_rrd_global,                                                                   &
+               ocean_rrd_local,                                                                    &
+               ocean_swap_timelevel,                                                               &
+               ocean_wrd_global,                                                                   &
                ocean_wrd_local
 
-    USE particle_attributes,                                                   &
+    USE particle_attributes,                                                                       &
         ONLY:  particle_advection
 
-    USE poismg_noopt_mod,                                                      &
+    USE poismg_noopt_mod,                                                                          &
         ONLY:  poismg_noopt_init
 
-    USE plant_canopy_model_mod,                                                &
-         ONLY: pcm_parin,                                                      &
-               pcm_check_parameters,                                           &
-               pcm_check_data_output,                                          &
-               pcm_init,                                                       &
-               pcm_header,                                                     &
-               pcm_3d_data_averaging,                                          &
-               pcm_data_output_3d,                                             &
-               pcm_rrd_global,                                                 &
-               pcm_rrd_local,                                                  &
-               pcm_wrd_global,                                                 &
+    USE plant_canopy_model_mod,                                                                    &
+         ONLY: pcm_3d_data_averaging,                                                              &
+               pcm_check_parameters,                                                               &
+               pcm_check_data_output,                                                              &
+               pcm_data_output_3d,                                                                 &
+               pcm_header,                                                                         &
+               pcm_init,                                                                           &
+               pcm_parin,                                                                          &
+               pcm_rrd_global,                                                                     &
+               pcm_rrd_local,                                                                      &
+               pcm_wrd_global,                                                                     &
                pcm_wrd_local
 
-    USE radiation_model_mod,                                                   &
-        ONLY:  radiation,                                                      &
-               radiation_parin,                                                &
-               radiation_check_parameters,                                     &
-               radiation_check_data_output_ts,                                 &
-               radiation_check_data_output_pr,                                 &
-               radiation_check_data_output,                                    &
-               radiation_init,                                                 &
-               radiation_header,                                               &
-               radiation_3d_data_averaging,                                    &
-               radiation_data_output_2d,                                       &
-               radiation_data_output_3d,                                       &
-               radiation_rrd_local,                                            &
+    USE radiation_model_mod,                                                                       &
+        ONLY:  radiation,                                                                          &
+               radiation_3d_data_averaging,                                                        &
+               radiation_check_parameters,                                                         &
+               radiation_check_data_output,                                                        &
+               radiation_check_data_output_pr,                                                     &
+               radiation_check_data_output_ts,                                                     &
+               radiation_data_output_2d,                                                           &
+               radiation_data_output_3d,                                                           &
+               radiation_header,                                                                   &
+               radiation_init,                                                                     &
+               radiation_parin,                                                                    &
+               radiation_rrd_local,                                                                &
                radiation_wrd_local
 
-    USE salsa_mod,                                                             &
-        ONLY:  salsa_parin,                                                    &
-               salsa_check_parameters,                                         &
-               salsa_check_data_output_pr,                                     &
-               salsa_check_data_output,                                        &
-               salsa_init_arrays,                                              &
-               salsa_init,                                                     &
-               salsa_header,                                                   &
-               salsa_actions,                                                  &
-               salsa_non_advective_processes,                                  &
-               salsa_exchange_horiz_bounds,                                    &
-               salsa_prognostic_equations,                                     &
-               salsa_boundary_conditions,                                      &
-               salsa_swap_timelevel,                                           &
-               salsa_3d_data_averaging,                                        &
-               salsa_data_output_2d,                                           &
-               salsa_data_output_3d,                                           &
-               salsa_statistics,                                               &
-               salsa_rrd_global,                                               &
-               salsa_rrd_local,                                                &
-               salsa_wrd_global,                                               &
+    USE salsa_mod,                                                                                 &
+        ONLY:  salsa_3d_data_averaging,                                                            &
+               salsa_actions,                                                                      &
+               salsa_boundary_conditions,                                                          &
+               salsa_check_data_output,                                                            &
+               salsa_check_data_output_pr,                                                         &
+               salsa_check_parameters,                                                             &
+               salsa_data_output_2d,                                                               &
+               salsa_data_output_3d,                                                               &
+               salsa_exchange_horiz_bounds,                                                        &
+               salsa_header,                                                                       &
+               salsa_init,                                                                         &
+               salsa_init_arrays,                                                                  &
+               salsa_non_advective_processes,                                                      &
+               salsa_parin,                                                                        &
+               salsa_prognostic_equations,                                                         &
+               salsa_rrd_global,                                                                   &
+               salsa_rrd_local,                                                                    &
+               salsa_statistics,                                                                   &
+               salsa_swap_timelevel,                                                               &
+               salsa_wrd_global,                                                                   &
                salsa_wrd_local
 
-    USE spectra_mod,                                                           &
-        ONLY:  calculate_spectra,                                              &
-               spectra_parin,                                                  &
-               spectra_check_parameters,                                       &
-               spectra_header
-
-    USE surface_data_output_mod,                                               &
-        ONLY:  surface_data_output_parin,                                      &
-               surface_data_output_check_parameters,                           &
-               surface_data_output_init_arrays,                                &
-               surface_data_output_rrd_local,                                  &
-               surface_data_output_rrd_global,                                 &
-               surface_data_output_wrd_local,                                  &
-               surface_data_output_wrd_global
-
-    USE surface_mod,                                                           &
+    USE spectra_mod,                                                                               &
+        ONLY:  calculate_spectra,                                                                  &
+               spectra_check_parameters,                                                           &
+               spectra_header,                                                                     &
+               spectra_parin
+
+    USE surface_data_output_mod,                                                                   &
+        ONLY:  surface_data_output_check_parameters,                                               &
+               surface_data_output_init_arrays,                                                    &
+               surface_data_output_parin,                                                          &
+               surface_data_output_rrd_global,                                                     &
+               surface_data_output_rrd_local,                                                      &
+               surface_data_output_wrd_global,                                                     &
+               surface_data_output_wrd_local
+
+    USE surface_mod,                                                                               &
         ONLY:  init_bc
 
-    USE synthetic_turbulence_generator_mod,                                    &
-        ONLY:  stg_parin,                                                      &
-               stg_check_parameters,                                           &
-               stg_header,                                                     &
-               stg_rrd_global,                                                 &
+    USE synthetic_turbulence_generator_mod,                                                        &
+        ONLY:  stg_check_parameters,                                                               &
+               stg_header,                                                                         &
+               stg_parin,                                                                          &
+               stg_rrd_global,                                                                     &
                stg_wrd_global
 
-    USE urban_surface_mod,                                                     &
-        ONLY:  usm_parin,                                                      &
-               usm_check_parameters,                                           &
-               usm_check_data_output,                                          &
-               usm_init_arrays,                                                &
-               usm_init,                                                       &
-               usm_swap_timelevel,                                             &
-               usm_3d_data_averaging,                                          &
-               usm_rrd_local,                                                  &
+    USE urban_surface_mod,                                                                         &
+        ONLY:  usm_3d_data_averaging,                                                              &
+               usm_check_data_output,                                                              &
+               usm_check_parameters,                                                               &
+               usm_init,                                                                           &
+               usm_init_arrays,                                                                    &
+               usm_parin,                                                                          &
+               usm_swap_timelevel,                                                                 &
+               usm_rrd_local,                                                                      &
                usm_wrd_local
 
-    USE vdi_internal_controls,                                                 &
+    USE vdi_internal_controls,                                                                     &
         ONLY:  vdi_actions
-               
-    USE virtual_measurement_mod,                                               &
-        ONLY:  vm_check_parameters,                                            &
-               vm_init,                                                        &
-               vm_init_output,                                                 &
+
+    USE virtual_measurement_mod,                                                                   &
+        ONLY:  vm_check_parameters,                                                                &
+               vm_init,                                                                            &
+               vm_init_output,                                                                     &
                vm_parin
 
-    USE wind_turbine_model_mod,                                                &
-        ONLY:  wtm_parin,                                                      &
-               wtm_check_parameters,                                           &
-               wtm_init,                                                       &
-               wtm_init_arrays,                                                &
-               wtm_init_output,                                                &
-               wtm_actions,                                                    &
-               wtm_rrd_global,                                                 &
+    USE wind_turbine_model_mod,                                                                    &
+        ONLY:  wtm_actions,                                                                        &
+               wtm_check_parameters,                                                               &
+               wtm_init,                                                                           &
+               wtm_init_arrays,                                                                    &
+               wtm_init_output,                                                                    &
+               wtm_parin,                                                                          &
+               wtm_rrd_global,                                                                     &
                wtm_wrd_global
 
-    USE user,                                                                  &
-        ONLY:  user_module_enabled,                                            &
-               user_parin,                                                     &
-               user_check_parameters,                                          &
-               user_check_data_output_ts,                                      &
-               user_check_data_output_pr,                                      &
-               user_check_data_output,                                         &
-               user_init,                                                      &
-               user_init_arrays,                                               &
-               user_header,                                                    &
-               user_actions,                                                   &
-               user_3d_data_averaging,                                         &
-               user_data_output_2d,                                            &
-               user_data_output_3d,                                            &
-               user_statistics,                                                &
-               user_rrd_global,                                                &
-               user_rrd_local,                                                 &
-               user_wrd_global,                                                &
-               user_wrd_local,                                                 &
-               user_last_actions
+    USE user,                                                                                      &
+        ONLY:  user_3d_data_averaging,                                                             &
+               user_actions,                                                                       &
+               user_check_data_output,                                                             &
+               user_check_data_output_pr,                                                          &
+               user_check_data_output_ts,                                                          &
+               user_check_parameters,                                                              &
+               user_data_output_2d,                                                                &
+               user_data_output_3d,                                                                &
+               user_header,                                                                        &
+               user_init,                                                                          &
+               user_init_arrays,                                                                   &
+               user_last_actions,                                                                  &
+               user_module_enabled,                                                                &
+               user_parin,                                                                         &
+               user_statistics,                                                                    &
+               user_rrd_global,                                                                    &
+               user_rrd_local,                                                                     &
+               user_wrd_global,                                                                    &
+               user_wrd_local
 
     IMPLICIT NONE
@@ -591,32 +586,32 @@
 !
 !-- Public functions
-    PUBLIC                                                                     &
-       module_interface_parin,                                                 &
-       module_interface_check_parameters,                                      &
-       module_interface_check_data_output_ts,                                  &
-       module_interface_check_data_output_pr,                                  &
-       module_interface_check_data_output,                                     &
-       module_interface_init_masks,                                            &
-       module_interface_define_netcdf_grid,                                    &
-       module_interface_init_arrays,                                           &
-       module_interface_init,                                                  &
-       module_interface_init_checks,                                           &
-       module_interface_init_numerics,                                         &
-       module_interface_init_output,                                           &
-       module_interface_header,                                                &
-       module_interface_actions,                                               &
-       module_interface_non_advective_processes,                               &
-       module_interface_exchange_horiz,                                        &
-       module_interface_prognostic_equations,                                  &
-       module_interface_boundary_conditions,                                   &
-       module_interface_swap_timelevel,                                        &
-       module_interface_3d_data_averaging,                                     &
-       module_interface_data_output_2d,                                        &
-       module_interface_data_output_3d,                                        &
-       module_interface_statistics,                                            &
-       module_interface_rrd_global,                                            &
-       module_interface_wrd_global,                                            &
-       module_interface_rrd_local,                                             &
-       module_interface_wrd_local,                                             &
+    PUBLIC                                                                                         &
+       module_interface_parin,                                                                     &
+       module_interface_check_parameters,                                                          &
+       module_interface_check_data_output_ts,                                                      &
+       module_interface_check_data_output_pr,                                                      &
+       module_interface_check_data_output,                                                         &
+       module_interface_init_masks,                                                                &
+       module_interface_define_netcdf_grid,                                                        &
+       module_interface_init_arrays,                                                               &
+       module_interface_init,                                                                      &
+       module_interface_init_checks,                                                               &
+       module_interface_init_numerics,                                                             &
+       module_interface_init_output,                                                               &
+       module_interface_header,                                                                    &
+       module_interface_actions,                                                                   &
+       module_interface_non_advective_processes,                                                   &
+       module_interface_exchange_horiz,                                                            &
+       module_interface_prognostic_equations,                                                      &
+       module_interface_boundary_conditions,                                                       &
+       module_interface_swap_timelevel,                                                            &
+       module_interface_3d_data_averaging,                                                         &
+       module_interface_data_output_2d,                                                            &
+       module_interface_data_output_3d,                                                            &
+       module_interface_statistics,                                                                &
+       module_interface_rrd_global,                                                                &
+       module_interface_wrd_global,                                                                &
+       module_interface_rrd_local,                                                                 &
+       module_interface_wrd_local,                                                                 &
        module_interface_last_actions
 
@@ -743,9 +738,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Read module-specific parameter namelists
-!------------------------------------------------------------------------------!
+!------------------------------------------------------------------------------                    !
  SUBROUTINE module_interface_parin
 
@@ -785,9 +780,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Perform module-specific initialization checks
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_check_parameters
 
@@ -825,14 +820,14 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Check module-specific data output of timeseries
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_check_data_output_ts( dots_max, dots_num, dots_label, dots_unit )
 
-
-    INTEGER(iwp),      INTENT(IN)    ::  dots_max !< variable output array index
+    INTEGER(iwp),      INTENT(IN)       ::  dots_max !< variable output array index
     INTEGER(iwp),      INTENT(INOUT)    ::  dots_num !< variable output array index
+
     CHARACTER (LEN=*), DIMENSION(dots_max), INTENT(INOUT) :: dots_label
     CHARACTER (LEN=*), DIMENSION(dots_max), INTENT(INOUT) :: dots_unit
@@ -857,18 +852,18 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Check module-specific data output of profiles
-!------------------------------------------------------------------------------!
- SUBROUTINE module_interface_check_data_output_pr( variable, var_count, unit,  &
-                                                   dopr_unit )
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE module_interface_check_data_output_pr( variable, var_count, unit, dopr_unit )
 
 
     CHARACTER (LEN=*), INTENT(IN)    ::  variable  !< variable name
-    INTEGER(iwp),      INTENT(IN)    ::  var_count !< variable output array index
     CHARACTER (LEN=*), INTENT(INOUT) ::  unit      !< physical unit of variable
     CHARACTER (LEN=*), INTENT(OUT)   ::  dopr_unit !< local value of dopr_unit
 
+    INTEGER(iwp),      INTENT(IN)    ::  var_count !< variable output array index
+
 
     IF ( debug_output )  CALL debug_message( 'checking module-specific data output pr', 'start' )
@@ -876,9 +871,9 @@
     CALL dynamics_check_data_output_pr( variable, var_count, unit, dopr_unit )
 
-    IF ( unit == 'illegal' .AND.  bulk_cloud_model )  THEN
+    IF ( unit == 'illegal'  .AND.  bulk_cloud_model )  THEN
        CALL bcm_check_data_output_pr( variable, var_count, unit, dopr_unit )
     ENDIF
 
-    IF ( unit == 'illegal' .AND.  air_chemistry )  THEN
+    IF ( unit == 'illegal'  .AND.  air_chemistry )  THEN
        CALL chem_check_data_output_pr( variable, var_count, unit, dopr_unit )
     ENDIF
@@ -918,9 +913,9 @@
  END SUBROUTINE module_interface_check_data_output_pr
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Check module-specific 2D and 3D data output
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_check_data_output( variable, unit, i, j, ilen, k )
 
@@ -949,6 +944,6 @@
     ENDIF
 
-    IF ( unit == 'illegal'  .AND.  air_chemistry                               &
-         .AND.  (variable(1:3) == 'kc_' .OR. variable(1:3) == 'em_') )  THEN  ! ToDo: remove aditional conditions
+    IF ( unit == 'illegal'  .AND.  air_chemistry  .AND.  (variable(1:3) == 'kc_'  .OR.             &
+         variable(1:3) == 'em_') )  THEN  ! ToDo: remove aditional conditions
        CALL chem_check_data_output( variable, unit, i, ilen, k )
     ENDIF
@@ -970,6 +965,5 @@
     ENDIF
 
-    IF ( unit == 'illegal'  .AND.  plant_canopy                                &
-         .AND.  variable(1:4) == 'pcm_' )  THEN  ! ToDo: remove aditional conditions
+    IF ( unit == 'illegal'  .AND.  plant_canopy  .AND.  variable(1:4) == 'pcm_' )  THEN  ! ToDo: remove aditional conditions
        CALL pcm_check_data_output( variable, unit )
     ENDIF
@@ -987,6 +981,5 @@
     ENDIF
 
-    IF ( unit == 'illegal'  .AND.  urban_surface                      &
-        .AND.  variable(1:4) == 'usm_' )  THEN  ! ToDo: remove aditional conditions
+    IF ( unit == 'illegal'  .AND.  urban_surface  .AND.  variable(1:4) == 'usm_' )  THEN  ! ToDo: remove aditional conditions
        CALL usm_check_data_output( variable, unit )
     ENDIF
@@ -1003,10 +996,10 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 !
 ! Description:
 ! ------------
 !> Interface for init_masks. ToDo: get rid of these redundant calls!
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_init_masks( variable, unit )
 
@@ -1020,9 +1013,9 @@
     CALL dynamics_init_masks( variable, unit )
 
-    IF ( unit == 'illegal'  .AND.  air_chemistry                               &
-         .AND.  (variable(1:3) == 'kc_' .OR. variable(1:3) == 'em_') )  THEN  ! ToDo: remove aditional conditions
+    IF ( unit == 'illegal'  .AND.  air_chemistry  .AND.  (variable(1:3) == 'kc_'  .OR.             &
+         variable(1:3) == 'em_') )  THEN  ! ToDo: remove aditional conditions
        CALL chem_check_data_output( variable, unit, 0, 0, 0 )
     ENDIF
-    
+
     IF ( unit == 'illegal' )  THEN
        CALL doq_check_data_output( variable, unit )
@@ -1048,25 +1041,25 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 !
 ! Description:
 ! ------------
 !> Define appropriate grid for module-specific netcdf output variables.
-!------------------------------------------------------------------------------!
- SUBROUTINE module_interface_define_netcdf_grid( var, found,                   &
-                                                 grid_x, grid_y, grid_z )
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE module_interface_define_netcdf_grid( var, found, grid_x, grid_y, grid_z )
 
 
     CHARACTER (LEN=*), INTENT(IN)  ::  var    !< variable name
-    LOGICAL,           INTENT(OUT) ::  found  !< indicates if variable was found
     CHARACTER (LEN=*), INTENT(OUT) ::  grid_x !< netcdf dimension in x-direction
     CHARACTER (LEN=*), INTENT(OUT) ::  grid_y !< netcdf dimension in y-direction
     CHARACTER (LEN=*), INTENT(OUT) ::  grid_z !< netcdf dimension in z-direction
 
+    LOGICAL,           INTENT(OUT) ::  found  !< indicates if variable was found
+
 
     IF ( debug_output )  CALL debug_message( 'defining module-specific netcdf grids', 'start' )
 !
-!-- As long as no action is done in this subroutine, initialize strings with
-!-- intent(out) attribute, in order to avoid compiler warnings.
+!-- As long as no action is done in this subroutine, initialize strings with intent(out) attribute,
+!-- in order to avoid compiler warnings.
     found  = .FALSE.
     grid_x = 'none'
@@ -1083,9 +1076,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Allocate module-specific arrays and pointers
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_init_arrays
 
@@ -1115,9 +1108,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Perform module-specific initialization
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_init
 
@@ -1154,9 +1147,9 @@
  END SUBROUTINE module_interface_init
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Initialize boundary conditions and numerical schemes.
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_init_numerics
 
@@ -1168,13 +1161,13 @@
 !-- Please note, wall flags are only applied in the non-optimized version.
     CALL poismg_noopt_init
- 
+
  END SUBROUTINE module_interface_init_numerics
 
- 
-!------------------------------------------------------------------------------!
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Initialize data output
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_init_output
 
@@ -1183,7 +1176,7 @@
 !
 !-- Initialize data-output module
-    CALL dom_init( file_suffix_of_output_group=coupling_char,                  &
-                   mpi_comm_of_output_group=comm2d,                            &
-                   program_debug_output_unit=9,                                &
+    CALL dom_init( file_suffix_of_output_group=coupling_char,                                      &
+                   mpi_comm_of_output_group=comm2d,                                                &
+                   program_debug_output_unit=9,                                                    &
                    debug_output=debug_output )
 !
@@ -1197,9 +1190,9 @@
  END SUBROUTINE module_interface_init_output
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Perform module-specific post-initialization checks
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_init_checks
 
@@ -1209,5 +1202,5 @@
     CALL dynamics_init_checks
 
-    IF ( biometeorology      )  CALL bio_init_checks
+    IF ( biometeorology )  CALL bio_init_checks
 
     IF ( debug_output )  CALL debug_message( 'module-specific post-initialization checks', 'end' )
@@ -1217,9 +1210,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Gather module-specific header output
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_header( io )
 
@@ -1256,9 +1249,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Perform module-specific actions while in time-integration (vector-optimized)
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_actions( location )
 
@@ -1284,15 +1277,15 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Perform module-specific actions while in time-integration (cache-optimized)
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_actions_ij( i, j, location )
 
+    CHARACTER (LEN=*), INTENT(IN) ::  location  !< call location string
 
     INTEGER(iwp),      INTENT(IN) ::  i         !< grid index in x-direction
     INTEGER(iwp),      INTENT(IN) ::  j         !< grid index in y-direction
-    CHARACTER (LEN=*), INTENT(IN) ::  location  !< call location string
 
     CALL dynamics_actions( i, j, location )
@@ -1312,9 +1305,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Compute module-specific non_advective_processes (vector-optimized)
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_non_advective_processes
 
@@ -1330,9 +1323,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Compute module-specific non_advective_processes (cache-optimized)
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_non_advective_processes_ij( i, j )
 
@@ -1350,12 +1343,13 @@
  END SUBROUTINE module_interface_non_advective_processes_ij
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Exchange horiz for module-specific quantities
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_exchange_horiz( location )
 
     CHARACTER (LEN=*), INTENT(IN) ::  location !< call location string
+
 
     IF ( debug_output_timestep )  CALL debug_message( 'module-specific exchange_horiz', 'start' )
@@ -1366,5 +1360,5 @@
     IF ( air_chemistry       )  CALL chem_exchange_horiz_bounds( location )
     IF ( ocean_mode          )  CALL ocean_exchange_horiz( location )
-    IF ( particle_advection  )  CALL lpm_exchange_horiz_bounds ( location )
+    IF ( particle_advection  )  CALL lpm_exchange_horiz_bounds( location )
     IF ( salsa               )  CALL salsa_exchange_horiz_bounds( location )
 
@@ -1375,9 +1369,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Compute module-specific prognostic_equations (vector-optimized)
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_prognostic_equations
 
@@ -1396,9 +1390,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Compute module-specific prognostic_equations (cache-optimized)
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_prognostic_equations_ij( i, j, i_omp_start, tn )
 
@@ -1421,9 +1415,9 @@
  END SUBROUTINE module_interface_prognostic_equations_ij
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Compute module-specific boundary conditions
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_boundary_conditions
 
@@ -1444,9 +1438,9 @@
  END SUBROUTINE module_interface_boundary_conditions
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Swap the timelevel pointers for module-specific arrays
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_swap_timelevel ( swap_mode )
 
@@ -1474,10 +1468,10 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 !
 ! Description:
 ! ------------
 !> Perform module-specific averaging of 3D data
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_3d_data_averaging( mode, variable )
 
@@ -1511,22 +1505,24 @@
  END SUBROUTINE module_interface_3d_data_averaging
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 !
 ! Description:
 ! ------------
 !> Define module-specific 2D output variables
-!------------------------------------------------------------------------------!
- SUBROUTINE module_interface_data_output_2d( av, variable, found, grid, mode,  &
-                                             local_pf, two_d, nzb_do, nzt_do,  &
-                                             fill_value )
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE module_interface_data_output_2d( av, variable, found, grid, mode, local_pf, two_d,     &
+                                             nzb_do, nzt_do, fill_value )
+
+    CHARACTER (LEN=*), INTENT(IN)    ::  mode       !< either 'xy', 'xz' or 'yz'
+    CHARACTER (LEN=*), INTENT(IN)    ::  variable   !< variable name
+    CHARACTER (LEN=*), INTENT(INOUT) ::  grid       !< name of vertical grid
 
     INTEGER(iwp),      INTENT(IN)    ::  av         !< flag for (non-)average output
-    CHARACTER (LEN=*), INTENT(IN)    ::  variable   !< variable name
-    LOGICAL,           INTENT(INOUT) ::  found      !< flag if output variable is found
-    CHARACTER (LEN=*), INTENT(INOUT) ::  grid       !< name of vertical grid
-    CHARACTER (LEN=*), INTENT(IN)    ::  mode       !< either 'xy', 'xz' or 'yz'
-    LOGICAL,           INTENT(OUT)   ::  two_d      !< flag for 2D variables
     INTEGER(iwp),      INTENT(IN)    ::  nzb_do     !< vertical output index (bottom) (usually 0)
     INTEGER(iwp),      INTENT(IN)    ::  nzt_do     !< vertical output index (top) (usually nz_do3d)
+
+    LOGICAL,           INTENT(INOUT) ::  found      !< flag if output variable is found
+    LOGICAL,           INTENT(OUT)   ::  two_d      !< flag for 2D variables
+
     REAL(wp),          INTENT(IN)    ::  fill_value !< to be removed
 
@@ -1536,72 +1532,53 @@
     IF ( debug_output_timestep )  CALL debug_message( 'module-specific 2d data output', 'start' )
 
-    CALL dynamics_data_output_2d(                                                  &
-               av, variable, found, grid, mode, local_pf, two_d, nzb_do, nzt_do, fill_value &
-            )
+    CALL dynamics_data_output_2d( av, variable, found, grid, mode, local_pf, two_d, nzb_do, nzt_do,&
+                                  fill_value )
 
     IF ( .NOT. found )  THEN
-       CALL tcm_data_output_2d(                                                  &
-               av, variable, found, grid, mode, local_pf, nzb_do, nzt_do &
-            )
+       CALL tcm_data_output_2d( av, variable, found, grid, mode, local_pf, nzb_do, nzt_do )
     ENDIF
 
     IF ( .NOT. found  .AND.  biometeorology )  THEN
-       CALL bio_data_output_2d(                                                &
-               av, variable, found, grid, local_pf, two_d, nzb_do, nzt_do      &
-            )
+       CALL bio_data_output_2d( av, variable, found, grid, local_pf, two_d, nzb_do, nzt_do )
     ENDIF
 
     IF ( .NOT. found  .AND.  bulk_cloud_model )  THEN
-       CALL bcm_data_output_2d(                                                &
-               av, variable, found, grid, mode, local_pf, two_d, nzb_do, nzt_do&
-            )
+       CALL bcm_data_output_2d( av, variable, found, grid, mode, local_pf, two_d, nzb_do, nzt_do )
     ENDIF
 
     IF ( .NOT. found  .AND.  air_chemistry )  THEN
-       CALL chem_data_output_2d(                                               &
-               av, variable, found, grid, mode, local_pf, two_d, nzb_do, nzt_do, fill_value &
-            )
+       CALL chem_data_output_2d( av, variable, found, grid, mode, local_pf, two_d, nzb_do, nzt_do, &
+                                 fill_value )
     ENDIF
 
     IF ( .NOT. found )  THEN
-       CALL doq_output_2d(                                                     &
-               av, variable, found, grid, mode, local_pf, two_d,               &
-               nzb_do, nzt_do, fill_value )
+       CALL doq_output_2d( av, variable, found, grid, mode, local_pf, two_d, nzb_do, nzt_do,       &
+                           fill_value )
     ENDIF
 
     IF ( .NOT. found  .AND.  gust_module_enabled )  THEN
-       CALL gust_data_output_2d(                                               &
-               av, variable, found, grid, mode, local_pf, two_d, nzb_do, nzt_do, fill_value &
-            )
+       CALL gust_data_output_2d( av, variable, found, grid, mode, local_pf, two_d, nzb_do, nzt_do, &
+                                 fill_value )
     ENDIF
 
     IF ( .NOT. found  .AND.  land_surface )  THEN
-       CALL lsm_data_output_2d(                                                &
-               av, variable, found, grid, mode, local_pf, two_d, nzb_do, nzt_do&
-            )
+       CALL lsm_data_output_2d( av, variable, found, grid, mode, local_pf, two_d, nzb_do, nzt_do )
     ENDIF
 
     IF ( .NOT. found  .AND.  ocean_mode )  THEN
-       CALL ocean_data_output_2d(                                              &
-               av, variable, found, grid, mode, local_pf, nzb_do, nzt_do       &
-            )
+       CALL ocean_data_output_2d( av, variable, found, grid, mode, local_pf, nzb_do, nzt_do )
     ENDIF
 
     IF ( .NOT. found  .AND.  radiation )  THEN
-       CALL radiation_data_output_2d(                                                       &
-               av, variable, found, grid, mode, local_pf, two_d, nzb_do, nzt_do, fill_value &
-            )
+       CALL radiation_data_output_2d( av, variable, found, grid, mode, local_pf, two_d, nzb_do,    &
+                                      nzt_do, fill_value )
     ENDIF
 
     IF ( .NOT. found  .AND.  salsa )  THEN
-       CALL salsa_data_output_2d(                                              &
-               av, variable, found, grid, mode, local_pf, two_d, nzb_do, nzt_do&
-            )
+       CALL salsa_data_output_2d( av, variable, found, grid, mode, local_pf, two_d, nzb_do, nzt_do )
     ENDIF
 
     IF ( .NOT. found  .AND.  user_module_enabled )  THEN
-       CALL user_data_output_2d(                                               &
-               av, variable, found, grid, local_pf, two_d, nzb_do, nzt_do      &
-            )
+       CALL user_data_output_2d( av, variable, found, grid, local_pf, two_d, nzb_do, nzt_do )
     ENDIF
 
@@ -1612,21 +1589,23 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 !
 ! Description:
 ! ------------
 !> Define module-specific 3D output variables
-!------------------------------------------------------------------------------!
- SUBROUTINE module_interface_data_output_3d( av, variable, found, local_pf,    &
-                                             fill_value, resorted, nzb_do, nzt_do )
-
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE module_interface_data_output_3d( av, variable, found, local_pf, fill_value, resorted,  &
+                                             nzb_do, nzt_do )
+
+    CHARACTER (LEN=*), INTENT(IN)    ::  variable   !< variable name
 
     INTEGER(iwp),      INTENT(IN)    ::  av         !< flag for (non-)average output
-    CHARACTER (LEN=*), INTENT(IN)    ::  variable   !< variable name
-    LOGICAL,           INTENT(INOUT) ::  found      !< flag if output variable is found
-    REAL(wp),          INTENT(IN)    ::  fill_value !< ToDo: refactor
-    LOGICAL,           INTENT(OUT)   ::  resorted   !< flag if output has been resorted
     INTEGER(iwp),      INTENT(IN)    ::  nzb_do     !< vertical output index (bottom) (usually 0)
     INTEGER(iwp),      INTENT(IN)    ::  nzt_do     !< vertical output index (top) (usually nz_do3d)
+
+    LOGICAL,           INTENT(INOUT) ::  found      !< flag if output variable is found
+    LOGICAL,           INTENT(OUT)   ::  resorted   !< flag if output has been resorted
+
+    REAL(wp),          INTENT(IN)    ::  fill_value !< ToDo: refactor
 
     REAL(sp), DIMENSION(nxl:nxr,nys:nyn,nzb_do:nzt_do), INTENT(INOUT) ::  local_pf
@@ -1704,16 +1683,17 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Compute module-specific profile and timeseries data
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_statistics( mode, sr, tn, dots_max )
 
 
     CHARACTER (LEN=*), INTENT(IN) ::  mode     !< statistical analysis mode
+
+    INTEGER(iwp),      INTENT(IN) ::  dots_max !< maximum number of timeseries
     INTEGER(iwp),      INTENT(IN) ::  sr       !<
     INTEGER(iwp),      INTENT(IN) ::  tn       !<
-    INTEGER(iwp),      INTENT(IN) ::  dots_max !< maximum number of timeseries
 
 
@@ -1801,9 +1781,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Write module-specific restart data globaly shared by all MPI ranks
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_wrd_global
 
@@ -1833,15 +1813,15 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Read module-specific local restart data arrays (Fortran binary format).
-!------------------------------------------------------------------------------!
- SUBROUTINE module_interface_rrd_local_ftn( map_index,                             &
-                                           nxlf, nxlc, nxl_on_file,               &
-                                           nxrf, nxrc, nxr_on_file,               &
-                                           nynf, nync, nyn_on_file,               &
-                                           nysf, nysc, nys_on_file,               &
-                                           tmp_2d, tmp_3d, found )
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE module_interface_rrd_local_ftn( map_index,                                             &
+                                            nxlf, nxlc, nxl_on_file,                               &
+                                            nxrf, nxrc, nxr_on_file,                               &
+                                            nynf, nync, nyn_on_file,                               &
+                                            nysf, nysc, nys_on_file,                               &
+                                            tmp_2d, tmp_3d, found )
 
 
@@ -1859,11 +1839,12 @@
     INTEGER(iwp), INTENT(IN)  ::  nysf         !<
     INTEGER(iwp), INTENT(IN)  ::  nys_on_file  !<
+
     LOGICAL,      INTENT(INOUT) ::  found        !< flag if variable was found
 
-    REAL(wp), &
-       DIMENSION(nys_on_file-nbgp:nyn_on_file+nbgp,nxl_on_file-nbgp:nxr_on_file+nbgp), &
+    REAL(wp),                                                                                      &
+       DIMENSION(nys_on_file-nbgp:nyn_on_file+nbgp,nxl_on_file-nbgp:nxr_on_file+nbgp),             &
        INTENT(OUT) :: tmp_2d   !<
-    REAL(wp), &
-       DIMENSION(nzb:nzt+1,nys_on_file-nbgp:nyn_on_file+nbgp,nxl_on_file-nbgp:nxr_on_file+nbgp), &
+    REAL(wp),                                                                                      &
+       DIMENSION(nzb:nzt+1,nys_on_file-nbgp:nyn_on_file+nbgp,nxl_on_file-nbgp:nxr_on_file+nbgp),   &
        INTENT(OUT) :: tmp_3d   !<
 
@@ -1873,127 +1854,112 @@
     ENDIF
 
-    CALL dynamics_rrd_local(                                                   &
-           map_index,                                                          &
-           nxlf, nxlc, nxl_on_file,                                            &
-           nxrf, nxrc, nxr_on_file,                                            &
-           nynf, nync, nyn_on_file,                                            &
-           nysf, nysc, nys_on_file,                                            &
-           tmp_2d, tmp_3d, found                                               &
-        ) ! ToDo: change interface to pass variable
-
-    IF ( .NOT. found )  CALL bio_rrd_local(                                    &
-                               found                                           &
-                            )
-
-    IF ( .NOT. found )  CALL bcm_rrd_local(                                    &
-                               map_index,                                      &
-                               nxlf, nxlc, nxl_on_file,                        &
-                               nxrf, nxrc, nxr_on_file,                        &
-                               nynf, nync, nyn_on_file,                        &
-                               nysf, nysc, nys_on_file,                        &
-                               tmp_2d, tmp_3d, found                           &
-                            ) ! ToDo: change interface to pass variable
-
-    IF ( .NOT. found )  CALL chem_rrd_local(                                   &
-                               map_index,                                      &
-                               nxlf, nxlc, nxl_on_file,                        &
-                               nxrf, nxrc, nxr_on_file,                        &
-                               nynf, nync, nyn_on_file,                        &
-                               nysf, nysc, nys_on_file,                        &
-                               tmp_3d, found                                   &
-                            ) ! ToDo: change interface to pass variable
-
-    IF ( .NOT. found )  CALL doq_rrd_local(                                    &
-                               map_index,                                      &
-                               nxlf, nxlc, nxl_on_file,                        &
-                               nxrf, nxrc, nxr_on_file,                        &
-                               nynf, nync, nyn_on_file,                        &
-                               nysf, nysc, nys_on_file,                        &
-                               tmp_2d, tmp_3d, found                           &
-                            ) ! ToDo: change interface to pass variable
-
-    IF ( .NOT. found )  CALL gust_rrd_local(                                   &
-                               map_index,                                      &
-                               nxlf, nxlc, nxl_on_file,                        &
-                               nxrf, nxrc, nxr_on_file,                        &
-                               nynf, nync, nyn_on_file,                        &
-                               nysf, nysc, nys_on_file,                        &
-                               tmp_2d, tmp_3d, found                           &
-                            ) ! ToDo: change interface to pass variable
-
-    IF ( .NOT. found )  CALL lpm_rrd_local(                                    &
-                               map_index,                                      &
-                               nxlf, nxlc, nxl_on_file,                        &
-                               nxrf, nxrc, nxr_on_file,                        &
-                               nynf, nync, nyn_on_file,                        &
-                               nysf, nysc, nys_on_file,                        &
-                               tmp_3d, found                                   &
-                            ) ! ToDo: change interface to pass variable
-
-    IF ( .NOT. found )  CALL lsm_rrd_local(                                    &
-                               map_index,                                      &
-                               nxlf, nxlc, nxl_on_file,                        &
-                               nxrf, nxrc, nxr_on_file,                        &
-                               nynf, nync, nyn_on_file,                        &
-                               nysf, nysc, nys_on_file,                        &
-                               tmp_2d, found                                   &
-                            ) ! ToDo: change interface to pass variable
-
-     IF ( .NOT. found )  CALL pcm_rrd_local(                                   &
-                               map_index,                                      &
-                               nxlf, nxlc, nxl_on_file,                        &
-                               nxrf, nxrc, nxr_on_file,                        &
-                               nynf, nync, nyn_on_file,                        &
-                               nysf, nysc, nys_on_file,                        &
-                               found                                           &
+    CALL dynamics_rrd_local( map_index,                                                            &
+                             nxlf, nxlc, nxl_on_file,                                              &
+                             nxrf, nxrc, nxr_on_file,                                              &
+                             nynf, nync, nyn_on_file,                                              &
+                             nysf, nysc, nys_on_file,                                              &
+                             tmp_2d, tmp_3d, found                                                 &
+                           ) ! ToDo: change interface to pass variable
+
+    IF ( .NOT. found )  CALL bio_rrd_local( found )
+
+    IF ( .NOT. found )  CALL bcm_rrd_local( map_index,                                             &
+                                            nxlf, nxlc, nxl_on_file,                               &
+                                            nxrf, nxrc, nxr_on_file,                               &
+                                            nynf, nync, nyn_on_file,                               &
+                                            nysf, nysc, nys_on_file,                               &
+                                            tmp_2d, tmp_3d, found                                  &
+                                          ) ! ToDo: change interface to pass variable
+
+    IF ( .NOT. found )  CALL chem_rrd_local( map_index,                                            &
+                                             nxlf, nxlc, nxl_on_file,                              &
+                                             nxrf, nxrc, nxr_on_file,                              &
+                                             nynf, nync, nyn_on_file,                              &
+                                             nysf, nysc, nys_on_file,                              &
+                                             tmp_3d, found                                         &
+                                           ) ! ToDo: change interface to pass variable
+
+    IF ( .NOT. found )  CALL doq_rrd_local( map_index,                                             &
+                                            nxlf, nxlc, nxl_on_file,                               &
+                                            nxrf, nxrc, nxr_on_file,                               &
+                                            nynf, nync, nyn_on_file,                               &
+                                            nysf, nysc, nys_on_file,                               &
+                                            tmp_2d, tmp_3d, found                                  &
+                                          ) ! ToDo: change interface to pass variable
+
+    IF ( .NOT. found )  CALL gust_rrd_local( map_index,                                            &
+                                             nxlf, nxlc, nxl_on_file,                              &
+                                             nxrf, nxrc, nxr_on_file,                              &
+                                             nynf, nync, nyn_on_file,                              &
+                                             nysf, nysc, nys_on_file,                              &
+                                             tmp_2d, tmp_3d, found                                 &
+                                           ) ! ToDo: change interface to pass variable
+
+    IF ( .NOT. found )  CALL lpm_rrd_local( map_index,                                             &
+                                            nxlf, nxlc, nxl_on_file,                               &
+                                            nxrf, nxrc, nxr_on_file,                               &
+                                            nynf, nync, nyn_on_file,                               &
+                                            nysf, nysc, nys_on_file,                               &
+                                            tmp_3d, found                                          &
+                                          ) ! ToDo: change interface to pass variable
+
+    IF ( .NOT. found )  CALL lsm_rrd_local( map_index,                                             &
+                                            nxlf, nxlc, nxl_on_file,                               &
+                                            nxrf, nxrc, nxr_on_file,                               &
+                                            nynf, nync, nyn_on_file,                               &
+                                            nysf, nysc, nys_on_file,                               &
+                                            tmp_2d, found                                          &
+                                          ) ! ToDo: change interface to pass variable
+
+     IF ( .NOT. found )  CALL pcm_rrd_local( map_index,                                            &
+                                             nxlf, nxlc, nxl_on_file,                              &
+                                             nxrf, nxrc, nxr_on_file,                              &
+                                             nynf, nync, nyn_on_file,                              &
+                                             nysf, nysc, nys_on_file,                              &
+                                             found                                                 &
                                            )
 
-    IF ( .NOT. found )  CALL ocean_rrd_local(                                  &
-                               map_index,                                      &
-                               nxlf, nxlc, nxl_on_file,                        &
-                               nxrf, nxrc, nxr_on_file,                        &
-                               nynf, nync, nyn_on_file,                        &
-                               nysf, nysc, nys_on_file,                        &
-                               tmp_3d, found                                   &
-                            ) ! ToDo: change interface to pass variable
-
-    IF ( .NOT. found )  CALL radiation_rrd_local(                              &
-                               map_index,                                      &
-                               nxlf, nxlc, nxl_on_file,                        &
-                               nxrf, nxrc, nxr_on_file,                        &
-                               nynf, nync, nyn_on_file,                        &
-                               nysf, nysc, nys_on_file,                        &
-                               tmp_2d, tmp_3d, found                           &
-                            ) ! ToDo: change interface to pass variable
-
-    IF ( .NOT. found )  CALL salsa_rrd_local(                                  &
-                               map_index,                                      &
-                               nxlf, nxlc, nxl_on_file,                        &
-                               nxrf, nxrc, nxr_on_file,                        &
-                               nynf, nync, nyn_on_file,                        &
-                               nysf, nysc, nys_on_file,                        &
-                               tmp_3d, found                                   &
-                            ) ! ToDo: change interface to pass variable
-
-    IF ( .NOT. found )  CALL usm_rrd_local(                                    &
-                               map_index,                                      &
-                               nxlf, nxlc, nxl_on_file,                        &
-                               nxrf, nxr_on_file,                              &
-                               nynf, nyn_on_file,                              &
-                               nysf, nysc, nys_on_file,                        &
-                               found                                           &
-                            ) ! ToDo: change interface to pass variable
+    IF ( .NOT. found )  CALL ocean_rrd_local( map_index,                                           &
+                                              nxlf, nxlc, nxl_on_file,                             &
+                                              nxrf, nxrc, nxr_on_file,                             &
+                                              nynf, nync, nyn_on_file,                             &
+                                              nysf, nysc, nys_on_file,                             &
+                                              tmp_3d, found                                        &
+                                            ) ! ToDo: change interface to pass variable
+
+    IF ( .NOT. found )  CALL radiation_rrd_local( map_index,                                       &
+                                                  nxlf, nxlc, nxl_on_file,                         &
+                                                  nxrf, nxrc, nxr_on_file,                         &
+                                                  nynf, nync, nyn_on_file,                         &
+                                                  nysf, nysc, nys_on_file,                         &
+                                                  tmp_2d, tmp_3d, found                            &
+                                                ) ! ToDo: change interface to pass variable
+
+    IF ( .NOT. found )  CALL salsa_rrd_local( map_index,                                           &
+                                              nxlf, nxlc, nxl_on_file,                             &
+                                              nxrf, nxrc, nxr_on_file,                             &
+                                              nynf, nync, nyn_on_file,                             &
+                                              nysf, nysc, nys_on_file,                             &
+                                              tmp_3d, found                                        &
+                                            ) ! ToDo: change interface to pass variable
+
+    IF ( .NOT. found )  CALL usm_rrd_local( map_index,                                             &
+                                            nxlf, nxlc, nxl_on_file,                               &
+                                            nxrf, nxr_on_file,                                     &
+                                            nynf, nyn_on_file,                                     &
+                                            nysf, nysc, nys_on_file,                               &
+                                            found                                                  &
+                                          ) ! ToDo: change interface to pass variable
 !
 !-- Surface data do not need overlap data, so do not pass these information.
     IF ( .NOT. found )  CALL surface_data_output_rrd_local( found )
 
-    IF ( .NOT. found )  CALL user_rrd_local(                                   &
-                               map_index,                                      &
-                               nxlf, nxlc, nxl_on_file,                        &
-                               nxrf, nxrc, nxr_on_file,                        &
-                               nynf, nync, nyn_on_file,                        &
-                               nysf, nysc, nys_on_file,                        &
-                               tmp_3d, found                                   &
-                            ) ! ToDo: change interface to pass variable
+    IF ( .NOT. found )  CALL user_rrd_local( map_index,                                            &
+                                             nxlf, nxlc, nxl_on_file,                              &
+                                             nxrf, nxrc, nxr_on_file,                              &
+                                             nynf, nync, nyn_on_file,                              &
+                                             nysf, nysc, nys_on_file,                              &
+                                             tmp_3d, found                                         &
+                                           ) ! ToDo: change interface to pass variable
 
     IF ( debug_output )  THEN
@@ -2004,9 +1970,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Read module-specific local restart data arrays (MPI-IO).
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_rrd_local_mpi
 
@@ -2040,9 +2006,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Write module-specific restart data specific to local MPI ranks
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_wrd_local
 
@@ -2074,9 +2040,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Perform module-specific last actions before the program terminates
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE module_interface_last_actions
 
Index: /palm/trunk/SOURCE/modules.f90
===================================================================
--- /palm/trunk/SOURCE/modules.f90	(revision 4752)
+++ /palm/trunk/SOURCE/modules.f90	(revision 4753)
@@ -1,20 +1,19 @@
 !> @file modules.f90
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! This file is part of the PALM model system.
 !
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
+! PALM is free software: you can redistribute it and/or modify it under the terms of the GNU General
+! Public License as published by the Free Software Foundation, either version 3 of the License, or
+! (at your option) any later version.
+!
+! PALM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
+! implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+! Public License for more details.
+!
+! You should have received a copy of the GNU General Public License along with PALM. If not, see
+! <http://www.gnu.org/licenses/>.
 !
 ! Copyright 1997-2020 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 !
 ! Current revisions:
@@ -25,4 +24,7 @@
 ! -----------------
 ! $Id$
+! file re-formatted to follow the PALM coding standard
+!
+! 4742 2020-10-14 15:11:02Z schwenkel
 ! Implement snow and graupel (bulk microphysics)
 !
@@ -69,6 +71,6 @@
 !
 ! 4472 2020-03-24 12:21:00Z Giersch
-! Additional switch added to activate calculations in flow_statistics for the
-! kolmogorov length scale
+! Additional switch added to activate calculations in flow_statistics for the kolmogorov length
+! scale
 !
 ! 4461 2020-03-12 16:51:59Z raasch
@@ -76,12 +78,11 @@
 !
 ! 4414 2020-02-19 20:16:04Z suehring
-! - nzb_diff_s_inner, nzb_diff_s_outer, nzb_inner,nzb_outer, nzb_s_inner,
-!   nzb_s_outer, nzb_u_inner, nzb_u_outer, nzb_v_inner, nzb_v_outer,
-!   nzb_w_inner, nzb_w_outer
+! - nzb_diff_s_inner, nzb_diff_s_outer, nzb_inner,nzb_outer, nzb_s_inner, nzb_s_outer, nzb_u_inner,
+!   nzb_u_outer, nzb_v_inner, nzb_v_outer, nzb_w_inner, nzb_w_outer
 !
 !
 ! 4360 2020-01-07 11:25:50Z suehring
-! Introduction of wall_flags_total_0, which currently sets bits based on static
-! topography information used in wall_flags_static_0
+! Introduction of wall_flags_total_0, which currently sets bits based on static topography
+! information used in wall_flags_static_0
 !
 ! 4340 2019-12-16 08:17:03Z Giersch
@@ -107,6 +108,5 @@
 !
 ! 4184 2019-08-23 08:07:40Z oliver.maas
-! changed allocated length of recycling_method_for_thermodynamic_quantities
-! from 20 to 80 characters
+! changed allocated length of recycling_method_for_thermodynamic_quantities from 20 to 80 characters
 !
 ! 4183 2019-08-23 07:33:16Z oliver.maas
@@ -127,6 +127,5 @@
 !
 ! 4131 2019-08-02 11:06:18Z monakurppa
-! Add max_pr_salsa to control_parameters. Used in creating profile output for
-! salsa.
+! Add max_pr_salsa to control_parameters. Used in creating profile output for salsa.
 !
 ! 4110 2019-07-22 17:05:21Z suehring
@@ -141,6 +140,6 @@
 !
 ! 4069 2019-07-01 14:05:51Z Giersch
-! Masked output running index mid has been introduced as a local variable to
-! avoid runtime error (Loop variable has been modified) in time_integration
+! Masked output running index mid has been introduced as a local variable to avoid runtime error
+! (Loop variable has been modified) in time_integration
 !
 ! 4017 2019-06-06 12:16:46Z schwenkel
@@ -151,6 +150,6 @@
 !
 ! 3885 2019-04-11 11:29:34Z kanani
-! Changes related to global restructuring of location messages and introduction
-! of additional debug messages
+! Changes related to global restructuring of location messages and introduction of additional debug
+! messages
 !
 ! 3871 2019-04-08 14:38:39Z knoop
@@ -163,16 +162,16 @@
 ! -surface_data_output +surface_output
 !
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Definition of global variables
-!------------------------------------------------------------------------------!
-
-
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
+
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Definition of variables for special advection schemes.
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  MODULE advection
 
@@ -190,11 +189,11 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> The variable in this module is used by multi_agent_system_mod AND
-!> netcdf_interface_mod. It must be here to avoid circular dependency.
+!> The variable in this module is used by multi_agent_system_mod AND netcdf_interface_mod. It must
+!> be here to avoid circular dependency.
 !> This is a workaround.
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  MODULE mas_global_attributes
 
@@ -208,106 +207,175 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Definition of all arrays defined on the computational grid.
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  MODULE arrays_3d
 
     USE kinds
 
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  c_u_m                  !< mean phase velocity at outflow for u-component used in radiation boundary condition
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  c_u_m_l                !< mean phase velocity at outflow for u-component used in radiation boundary condition (local subdomain value)
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  c_v_m                  !< mean phase velocity at outflow for v-component used in radiation boundary condition
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  c_v_m_l                !< mean phase velocity at outflow for v-component used in radiation boundary condition (local subdomain value)
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  c_w_m                  !< mean phase velocity at outflow for w-component used in radiation boundary condition
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  c_w_m_l                !< mean phase velocity at outflow for w-component used in radiation boundary condition (local subdomain value)
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ddzu                   !< 1/dzu
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ddzu_pres              !< modified ddzu for pressure solver
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  dd2zu                  !< 1/(dzu(k)+dzu(k+1))
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  dzu                    !< vertical grid size (u-grid)
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ddzw                   !< 1/dzw
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  dzw                    !< vertical grid size (w-grid)
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  hyp                    !< hydrostatic pressure
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  inflow_damping_factor  !< used for turbulent inflow (non-cyclic boundary conditions)
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ptdf_x                 !< damping factor for potential temperature in x-direction
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ptdf_y                 !< damping factor for potential temperature in y-direction
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  pt_init                !< initial profile of potential temperature
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  q_init                 !< initial profile of total water mixing ratio
-                                                                   !< (or total water content with active cloud physics)
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  rdf                    !< rayleigh damping factor for velocity components
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  rdf_sc                 !< rayleigh damping factor for scalar quantities
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ref_state              !< reference state of potential temperature
-                                                                   !< (and density in case of ocean simulation)
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  s_init                 !< initial profile of passive scalar concentration
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  sa_init                !< initial profile of salinity (ocean)
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ug                     !< geostrophic wind component in x-direction
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  u_init                 !< initial profile of horizontal velocity component u
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  u_stokes_zu            !< u-component of Stokes drift velocity at zu levels
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  u_stokes_zw            !< u-component of Stokes drift velocity at zw levels
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  vg                     !< geostrophic wind component in y-direction
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  v_init                 !< initial profile of horizontal velocity component v
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  v_stokes_zu            !< v-component of Stokes drift velocity at zu levels
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  v_stokes_zw            !< v-component of Stokes drift velocity at zw levels
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  w_subs                 !< subsidence/ascent velocity
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  x                      !< horizontal grid coordinate of v-grid (in m)
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  xu                     !< horizontal grid coordinate of u-grid (in m)
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  y                      !< horizontal grid coordinate of u-grid (in m)
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  yv                     !< horizontal grid coordinate of v-grid (in m)
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  zu                     !< vertical grid coordinate of u-grid (in m)
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  zw                     !< vertical grid coordinate of w-grid (in m)
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  c_u_m                           !< mean phase velocity at outflow for u-component used
+                                                                            !< in radiation boundary condition
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  c_u_m_l                         !< mean phase velocity at outflow for u-component used
+                                                                            !< in radiation boundary condition (local subdomain value)
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  c_v_m                           !< mean phase velocity at outflow for v-component used
+                                                                            !< in radiation boundary condition
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  c_v_m_l                         !< mean phase velocity at outflow for v-component used
+                                                                            !< in radiation boundary condition (local subdomain value)
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  c_w_m                           !< mean phase velocity at outflow for w-component used
+                                                                            !< in radiation boundary condition
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  c_w_m_l                         !< mean phase velocity at outflow for w-component used
+                                                                            !< in radiation boundary condition (local subdomain value)
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  d_exner                         !< ratio of potential and actual temperature
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ddzu                            !< 1/dzu
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ddzu_pres                       !< modified ddzu for pressure solver
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  dd2zu                           !< 1/(dzu(k)+dzu(k+1))
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  drho_air                        !< inverse air density profile on the uv grid
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  drho_air_zw                     !< inverse air density profile on the w grid
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  dzu                             !< vertical grid size (u-grid)
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ddzw                            !< 1/dzw
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  dzw                             !< vertical grid size (w-grid)
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  exner                           !< ratio of actual and potential temperature
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  heatflux_input_conversion       !< conversion factor array for heatflux input
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  heatflux_output_conversion      !< conversion factor array for heatflux output
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  hyp                             !< hydrostatic pressure
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  hyrho                           !< density of air calculated with hydrostatic pressure
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  inflow_damping_factor           !< used for turbulent inflow
+                                                                            !< (non-cyclic boundary conditions)
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  momentumflux_input_conversion   !< conversion factor array for momentumflux input
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  momentumflux_output_conversion  !< conversion factor array for momentumflux output
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ptdf_x                          !< damping factor for potential temperature in
+                                                                            !< x-direction
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ptdf_y                          !< damping factor for potential temperature in
+                                                                            !< y-direction
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  pt_init                         !< initial profile of potential temperature
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  q_init                          !< initial profile of total water mixing ratio
+                                                                            !< (or total water content with active cloud physics)
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  rdf                             !< rayleigh damping factor for velocity components
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  rdf_sc                          !< rayleigh damping factor for scalar quantities
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ref_state                       !< reference state of potential temperature
+                                                                            !< (and density in case of ocean simulation)
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  rho_air                         !< air density profile on the uv grid
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  rho_air_zw                      !< air density profile on the w grid
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  s_init                          !< initial profile of passive scalar concentration
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  sa_init                         !< initial profile of salinity (ocean)
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ug                              !< geostrophic wind component in x-direction
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  u_init                          !< initial profile of horizontal velocity component u
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  u_stokes_zu                     !< u-component of Stokes drift velocity at zu levels
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  u_stokes_zw                     !< u-component of Stokes drift velocity at zw levels
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  vg                              !< geostrophic wind component in y-direction
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  v_init                          !< initial profile of horizontal velocity component v
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  v_stokes_zu                     !< v-component of Stokes drift velocity at zu levels
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  v_stokes_zw                     !< v-component of Stokes drift velocity at zw levels
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  waterflux_input_conversion      !< conversion factor array for waterflux input
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  waterflux_output_conversion     !< conversion factor array for waterflux output
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  w_subs                          !< subsidence/ascent velocity
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  x                               !< horizontal grid coordinate of v-grid (in m)
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  xu                              !< horizontal grid coordinate of u-grid (in m)
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  y                               !< horizontal grid coordinate of u-grid (in m)
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  yv                              !< horizontal grid coordinate of v-grid (in m)
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  zu                              !< vertical grid coordinate of u-grid (in m)
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  zw                              !< vertical grid coordinate of w-grid (in m)
+
 
     REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  c_u                   !< phase speed of u-velocity component
     REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  c_v                   !< phase speed of v-velocity component
     REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  c_w                   !< phase speed of w-velocity component
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_diss           !< artificial numerical dissipation flux at south face of grid box - TKE dissipation
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_e              !< artificial numerical dissipation flux at south face of grid box - subgrid-scale TKE
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_nc             !< artificial numerical dissipation flux at south face of grid box - clouddrop-number concentration
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_ng             !< artificial numerical dissipation flux at south face of grid box - graupel number concentration
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_ni             !< artificial numerical dissipation flux at south face of grid box - ice crystal-number concentration
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_nr             !< artificial numerical dissipation flux at south face of grid box - raindrop-number concentration
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_ns             !< artificial numerical dissipation flux at south face of grid box - snow-number concentration
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_pt             !< artificial numerical dissipation flux at south face of grid box - potential temperature
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_q              !< artificial numerical dissipation flux at south face of grid box - total water mixing ratio
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_qc             !< artificial numerical dissipation flux at south face of grid box - cloudwater mixing ratio
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_qg             !< artificial numerical dissipation flux at south face of grid box - graupel mixing ratio
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_qi             !< artificial numerical dissipation flux at south face of grid box - ice crystal mixing ratio
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_qr             !< artificial numerical dissipation flux at south face of grid box - rainwater mixing ratio
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_qs             !< artificial numerical dissipation flux at south face of grid box - snow mixing ratio
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_s              !< artificial numerical dissipation flux at south face of grid box - passive scalar
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_sa             !< artificial numerical dissipation flux at south face of grid box - salinity
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_u              !< artificial numerical dissipation flux at south face of grid box - u-component
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_v              !< artificial numerical dissipation flux at south face of grid box - v-component
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_w              !< artificial numerical dissipation flux at south face of grid box - w-component
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_diss           !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - TKE dissipation
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_e              !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - subgrid-scale TKE
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_nc             !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - clouddrop-number concentration
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_ng             !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - graupel number concentration
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_ni             !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - ice crystal-number concentration
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_nr             !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - raindrop-number concentration
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_ns             !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - snow-number concentration
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_pt             !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - potential temperature
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_q              !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - total water mixing ratio
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_qc             !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - cloudwater mixing ratio
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_qg             !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - graupel mixing ratio
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_qi             !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - ice crystal mixing ratio
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_qr             !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - rainwater mixing ratio
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_qs             !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - snow mixing ratio
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_s              !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - passive scalar
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_sa             !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - salinity
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_u              !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - u-component
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_v              !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - v-component
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  diss_s_w              !< artificial numerical dissipation flux at south face of grid
+                                                                    !< box - w-component
     REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  dzu_mg                !< vertical grid size (u-grid) for multigrid pressure solver
     REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  dzw_mg                !< vertical grid size (w-grid) for multigrid pressure solver
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_diss           !< 6th-order advective flux at south face of grid box - TKE dissipation
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_e              !< 6th-order advective flux at south face of grid box - subgrid-scale TKE
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_nc             !< 6th-order advective flux at south face of grid box - clouddrop-number concentration
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_ng             !< 6th-order advective flux at south face of grid box - graupel-number concentration
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_ni             !< 6th-order advective flux at south face of grid box - icecrystal-number concentration
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_nr             !< 6th-order advective flux at south face of grid box - raindrop-number concentration
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_ns             !< 6th-order advective flux at south face of grid box - graupel-number concentration
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_pt             !< 6th-order advective flux at south face of grid box - potential temperature
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_q              !< 6th-order advective flux at south face of grid box - total water mixing ratio
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_qc             !< 6th-order advective flux at south face of grid box - cloudwater mixing ratio
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_qg             !< 6th-order advective flux at south face of grid box - graupel mixing ratio
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_qi             !< 6th-order advective flux at south face of grid box - ice crystal mixing ratio
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_qr             !< 6th-order advective flux at south face of grid box - rainwater mixing ratio
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_qs             !< 6th-order advective flux at south face of grid box - snow mixing ratio
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_s              !< 6th-order advective flux at south face of grid box - passive scalar
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_sa             !< 6th-order advective flux at south face of grid box - salinity
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_u              !< 6th-order advective flux at south face of grid box - u-component
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_v              !< 6th-order advective flux at south face of grid box - v-component
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_w              !< 6th-order advective flux at south face of grid box - w-component
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  f1_mg                 !< grid factor used in right hand side of Gauss-Seidel equation (multigrid)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  f2_mg                 !< grid factor used in right hand side of Gauss-Seidel equation (multigrid)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  f3_mg                 !< grid factor used in right hand side of Gauss-Seidel equation (multigrid)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_diss           !< 6th-order advective flux at south face of grid box -
+                                                                    !< TKE dissipation
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_e              !< 6th-order advective flux at south face of grid box -
+                                                                    !< subgrid-scale TKE
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_nc             !< 6th-order advective flux at south face of grid box -
+                                                                    !< clouddrop-number concentration
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_ng             !< 6th-order advective flux at south face of grid box -
+                                                                    !< graupel-number concentration
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_ni             !< 6th-order advective flux at south face of grid box -
+                                                                    !< icecrystal-number concentration
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_nr             !< 6th-order advective flux at south face of grid box -
+                                                                    !< raindrop-number concentration
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_ns             !< 6th-order advective flux at south face of grid box -
+                                                                    !< graupel-number concentration
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_pt             !< 6th-order advective flux at south face of grid box -
+                                                                    !< potential temperature
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_q              !< 6th-order advective flux at south face of grid box -
+                                                                    !< total water mixing ratio
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_qc             !< 6th-order advective flux at south face of grid box -
+                                                                    !< cloudwater mixing ratio
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_qg             !< 6th-order advective flux at south face of grid box -
+                                                                    !< graupel mixing ratio
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_qi             !< 6th-order advective flux at south face of grid box -
+                                                                    !< ice crystal mixing ratio
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_qr             !< 6th-order advective flux at south face of grid box -
+                                                                    !< rainwater mixing ratio
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_qs             !< 6th-order advective flux at south face of grid box -
+                                                                    !< snow mixing ratio
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_s              !< 6th-order advective flux at south face of grid box -
+                                                                    !< passive scalar
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_sa             !< 6th-order advective flux at south face of grid box -
+                                                                    !< salinity
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_u              !< 6th-order advective flux at south face of grid box -
+                                                                    !< u-component
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_v              !< 6th-order advective flux at south face of grid box -
+                                                                    !< v-component
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  flux_s_w              !< 6th-order advective flux at south face of grid box -
+                                                                    !< w-component
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  f1_mg                 !< grid factor used in right hand side of Gauss-Seidel equation
+                                                                    !< (multigrid)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  f2_mg                 !< grid factor used in right hand side of Gauss-Seidel equation
+                                                                    !< (multigrid)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  f3_mg                 !< grid factor used in right hand side of Gauss-Seidel equation
+                                                                    !< (multigrid)
     REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  mean_inflow_profiles  !< used for turbulent inflow (non-cyclic boundary conditions)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  precipitation_amount  !< precipitation amount due to gravitational settling (bulk microphysics)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  precipitation_amount  !< precipitation amount due to gravitational settling
+                                                                    !< (bulk microphysics)
     REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  pt_slope_ref          !< potential temperature in rotated coordinate system
                                                                     !< (in case of sloped surface)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  total_2d_a            !< horizontal array to store the total domain data, used for atmosphere-ocean coupling (atmosphere data)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  total_2d_o            !< horizontal array to store the total domain data, used for atmosphere-ocean coupling (ocean data)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  rho_air_mg            !< air density profiles on the uv grid for multigrid
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  rho_air_zw_mg         !< air density profiles on the w grid for multigrid
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  total_2d_a            !< horizontal array to store the total domain data, used for
+                                                                    !< atmosphere-ocean coupling (atmosphere data)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  total_2d_o            !< horizontal array to store the total domain data, used for
+                                                                    !< atmosphere-ocean coupling (ocean data)
 
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  d           !< divergence
@@ -315,31 +383,56 @@
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  de_dy       !< gradient of sgs tke in y-direction (lpm)
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  de_dz       !< gradient of sgs tke in z-direction (lpm)
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_diss !< artificial numerical dissipation flux at left face of grid box - TKE dissipation
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_e    !< artificial numerical dissipation flux at left face of grid box - subgrid-scale TKE
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_nc   !< artificial numerical dissipation flux at left face of grid box - clouddrop-number concentration
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_ng   !< artificial numerical dissipation flux at left face of grid box - graupel-number concentration
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_ni   !< artificial numerical dissipation flux at left face of grid box - ice crystal-number concentration
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_nr   !< artificial numerical dissipation flux at left face of grid box - raindrop-number concentration
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_ns   !< artificial numerical dissipation flux at left face of grid box - snow-number concentration
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_pt   !< artificial numerical dissipation flux at left face of grid box - potential temperature
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_q    !< artificial numerical dissipation flux at left face of grid box - total water mixing ratio
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_qc   !< artificial numerical dissipation flux at left face of grid box - cloudwater
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_qg   !< artificial numerical dissipation flux at left face of grid box - graupel
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_qi   !< artificial numerical dissipation flux at left face of grid box - ice crystal
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_qr   !< artificial numerical dissipation flux at left face of grid box - rainwater
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_qs   !< artificial numerical dissipation flux at left face of grid box - snow
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_s    !< artificial numerical dissipation flux at left face of grid box - passive scalar
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_sa   !< artificial numerical dissipation flux at left face of grid box - salinity
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_u    !< artificial numerical dissipation flux at left face of grid box - u-component
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_v    !< artificial numerical dissipation flux at left face of grid box - v-component
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_w    !< artificial numerical dissipation flux at left face of grid box - w-component
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_diss !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< TKE dissipation
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_e    !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< subgrid-scale TKE
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_nc   !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< clouddrop-number concentration
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_ng   !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< graupel-number concentration
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_ni   !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< ice crystal-number concentration
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_nr   !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< raindrop-number concentration
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_ns   !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< snow-number concentration
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_pt   !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< potential temperature
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_q    !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< total water mixing ratio
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_qc   !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< cloudwater
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_qg   !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< graupel
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_qi   !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< ice crystal
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_qr   !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< rainwater
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_qs   !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< snow
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_s    !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< passive scalar
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_sa   !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< salinity
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_u    !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< u-component
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_v    !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< v-component
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  diss_l_w    !< artificial numerical dissipation flux at left face of grid box -
+                                                            !< w-component
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  flux_l_diss !< 6th-order advective flux at south face of grid box - TKE dissipation
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  flux_l_e    !< 6th-order advective flux at south face of grid box - subgrid-scale TKE
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  flux_l_nc   !< 6th-order advective flux at south face of grid box - clouddrop-number concentration
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  flux_l_ng   !< 6th-order advective flux at south face of grid box - graupel-number concentration
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  flux_l_ni   !< 6th-order advective flux at south face of grid box - ice crystal-number concentration
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  flux_l_nr   !< 6th-order advective flux at south face of grid box - raindrop-number concentration
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  flux_l_e    !< 6th-order advective flux at south face of grid box - subgrid-scale
+                                                            !< TKE
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  flux_l_nc   !< 6th-order advective flux at south face of grid box - clouddrop-number
+                                                            !< concentration
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  flux_l_ng   !< 6th-order advective flux at south face of grid box -
+                                                            !< graupel-number concentration
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  flux_l_ni   !< 6th-order advective flux at south face of grid box -
+                                                            !< ice crystal-number concentration
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  flux_l_nr   !< 6th-order advective flux at south face of grid box - raindrop-number
+                                                            !< concentration
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  flux_l_ns   !< 6th-order advective flux at south face of grid box - snow-number concentration
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  flux_l_pt   !< 6th-order advective flux at south face of grid box - potential temperature
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  flux_l_pt   !< 6th-order advective flux at south face of grid box - potential
+                                                            !< temperature
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  flux_l_q    !< 6th-order advective flux at south face of grid box - mixing ratio
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  flux_l_qc   !< 6th-order advective flux at south face of grid box - cloudwater
@@ -356,19 +449,33 @@
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  km  !< eddy diffusivity for momentum
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  prr         !< rain rate
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  p_loc       !< local array in multigrid/sor solver containing the pressure which is iteratively advanced in each iteration step
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  p_loc       !< local array in multigrid/sor solver containing the pressure which is
+                                                            !< iteratively advanced in each iteration step
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  tend        !< tendency field (time integration)
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  tric        !< coefficients of the tridiagonal matrix for solution of the Poisson equation in Fourier space
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_m_l       !< velocity data (u at left boundary) from time level t-dt required for radiation boundary condition
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_m_n       !< velocity data (u at north boundary) from time level t-dt required for radiation boundary condition
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_m_r       !< velocity data (u at right boundary) from time level t-dt required for radiation boundary condition
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_m_s       !< velocity data (u at south boundary) from time level t-dt required for radiation boundary condition
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_m_l       !< velocity data (v at left boundary) from time level t-dt required for radiation boundary condition
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_m_n       !< velocity data (v at north boundary) from time level t-dt required for radiation boundary condition
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_m_r       !< velocity data (v at right boundary) from time level t-dt required for radiation boundary condition
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_m_s       !< velocity data (v at south boundary) from time level t-dt required for radiation boundary condition
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_m_l       !< velocity data (w at left boundary) from time level t-dt required for radiation boundary condition
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_m_n       !< velocity data (w at north boundary) from time level t-dt required for radiation boundary condition
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_m_r       !< velocity data (w at right boundary) from time level t-dt required for radiation boundary condition
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_m_s       !< velocity data (w at south boundary) from time level t-dt required for radiation boundary condition
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  tric        !< coefficients of the tridiagonal matrix for solution of the Poisson
+                                                            !< equation in Fourier space
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_m_l       !< velocity data (u at left boundary) from time level t-dt required for
+                                                            !< radiation boundary condition
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_m_n       !< velocity data (u at north boundary) from time level t-dt required for
+                                                            !< radiation boundary condition
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_m_r       !< velocity data (u at right boundary) from time level t-dt required for
+                                                            !< radiation boundary condition
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  u_m_s       !< velocity data (u at south boundary) from time level t-dt required for
+                                                            !< radiation boundary condition
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_m_l       !< velocity data (v at left boundary) from time level t-dt required for
+                                                            !< radiation boundary condition
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_m_n       !< velocity data (v at north boundary) from time level t-dt required for
+                                                            !< radiation boundary condition
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_m_r       !< velocity data (v at right boundary) from time level t-dt required for
+                                                            !< radiation boundary condition
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  v_m_s       !< velocity data (v at south boundary) from time level t-dt required for
+                                                            !< radiation boundary condition
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_m_l       !< velocity data (w at left boundary) from time level t-dt required for
+                                                            !< radiation boundary condition
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_m_n       !< velocity data (w at north boundary) from time level t-dt required for
+                                                            !< radiation boundary condition
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_m_r       !< velocity data (w at right boundary) from time level t-dt required for
+                                                            !< radiation boundary condition
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  w_m_s       !< velocity data (w at south boundary) from time level t-dt required for
+                                                            !< radiation boundary condition
 
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  diss_1  !< pointer for swapping of timelevels for respective quantity
@@ -404,4 +511,5 @@
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  qc_2    !< pointer for swapping of timelevels for respective quantity
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  qc_3    !< pointer for swapping of timelevels for respective quantity
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  qf_1    !< pointer for swapping of timelevels for respective quantity
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  qg_1    !< pointer for swapping of timelevels for respective quantity
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  qg_2    !< pointer for swapping of timelevels for respective quantity
@@ -412,5 +520,4 @@
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  ql_v    !< pointer: volume of liquid water
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  ql_vp   !< pointer: liquid water weighting factor
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  qf_1    !< pointer for swapping of timelevels for respective quantity
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  ql_1    !< pointer for swapping of timelevels for respective quantity
     REAL(wp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  ql_2    !< pointer for swapping of timelevels for respective quantity
@@ -460,9 +567,9 @@
     REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  qc         !< pointer: cloud water content
     REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  qc_p       !< pointer: prognostic value cloud water content
+    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  qf         !< pointer: frozen water content
     REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  qg         !< pointer: graupel water content
     REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  qg_p       !< pointer: prognostic value graupel water content
     REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  qi         !< pointer: ice crystal content
     REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  qi_p       !< pointer: prognostic value ice crystal content
-    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  qf         !< pointer: frozen water content
     REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  ql         !< pointer: liquid water content
     REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  ql_c       !< pointer: change in liquid water content due to
@@ -477,23 +584,38 @@
     REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  sa         !< pointer: ocean salinity
     REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  sa_p       !< pointer: prognostic value of ocean salinity
-    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tdiss_m    !< pointer: weighted tendency of diss for previous sub-timestep (Runge-Kutta)
-    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  te_m       !< pointer: weighted tendency of e for previous sub-timestep (Runge-Kutta)
-    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tnc_m      !< pointer: weighted tendency of nc for previous sub-timestep (Runge-Kutta)
+    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tdiss_m    !< pointer: weighted tendency of diss for previous sub-timestep
+                                                                   !< (Runge-Kutta)
+    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  te_m       !< pointer: weighted tendency of e for previous sub-timestep
+                                                                   !< (Runge-Kutta)
+    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tnc_m      !< pointer: weighted tendency of nc for previous sub-timestep
+                                                                   !< (Runge-Kutta)
     REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tng_m      !< pointer: weighted tendency of ng for previous sub-timestep (Runge-Kutta)
-    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tni_m      !< pointer: weighted tendency of ni for previous sub-timestep (Runge-Kutta)
-    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tnr_m      !< pointer: weighted tendency of nr for previous sub-timestep (Runge-Kutta)
+    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tni_m      !< pointer: weighted tendency of ni for previous sub-timestep
+                                                                   !< (Runge-Kutta)
+    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tnr_m      !< pointer: weighted tendency of nr for previous sub-timestep
+                                                                   !< (Runge-Kutta)
     REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tns_m      !< pointer: weighted tendency of ns for previous sub-timestep (Runge-Kutta)
-    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tpt_m      !< pointer: weighted tendency of pt for previous sub-timestep (Runge-Kutta)
-    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tq_m       !< pointer: weighted tendency of q for previous sub-timestep (Runge-Kutta)
-    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tqc_m      !< pointer: weighted tendency of qc for previous sub-timestep (Runge-Kutta)
+    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tpt_m      !< pointer: weighted tendency of pt for previous sub-timestep
+                                                                   !< (Runge-Kutta)
+    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tq_m       !< pointer: weighted tendency of q for previous sub-timestep
+                                                                   !< (Runge-Kutta)
+    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tqc_m      !< pointer: weighted tendency of qc for previous sub-timestep
+                                                                   !< (Runge-Kutta)
     REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tqg_m      !< pointer: weighted tendency of qg for previous sub-timestep (Runge-Kutta)
-    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tqi_m      !< pointer: weighted tendency of qi for previous sub-timestep (Runge-Kutta)
-    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tqr_m      !< pointer: weighted tendency of qr for previous sub-timestep (Runge-Kutta)
+    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tqi_m      !< pointer: weighted tendency of qi for previous sub-timestep
+                                                                   !< (Runge-Kutta)
+    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tqr_m      !< pointer: weighted tendency of qr for previous sub-timestep
+                                                                   !< (Runge-Kutta)
     REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tqs_m      !< pointer: weighted tendency of qs for previous sub-timestep (Runge-Kutta)
-    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  ts_m       !< pointer: weighted tendency of s for previous sub-timestep (Runge-Kutta)
-    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tsa_m      !< pointer: weighted tendency of sa for previous sub-timestep (Runge-Kutta)
-    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tu_m       !< pointer: weighted tendency of u for previous sub-timestep (Runge-Kutta)
-    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tv_m       !< pointer: weighted tendency of v for previous sub-timestep (Runge-Kutta)
-    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tw_m       !< pointer: weighted tendency of w for previous sub-timestep (Runge-Kutta)
+    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  ts_m       !< pointer: weighted tendency of s for previous sub-timestep
+                                                                   !< (Runge-Kutta)
+    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tsa_m      !< pointer: weighted tendency of sa for previous sub-timestep
+                                                                   !< (Runge-Kutta)
+    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tu_m       !< pointer: weighted tendency of u for previous sub-timestep
+                                                                   !< (Runge-Kutta)
+    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tv_m       !< pointer: weighted tendency of v for previous sub-timestep
+                                                                   !< (Runge-Kutta)
+    REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  tw_m       !< pointer: weighted tendency of w for previous sub-timestep
+                                                                   !< (Runge-Kutta)
     REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  u          !< pointer: horizontal velocity component u (x-direction)
     REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  u_p        !< pointer: prognostic value of u
@@ -504,24 +626,6 @@
     REAL(wp), DIMENSION(:,:,:), POINTER, CONTIGUOUS ::  w_p        !< pointer: prognostic value of w
 
-    REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  tri    !<  array to hold the tridiagonal matrix for solution of the Poisson equation in Fourier space (4th dimension for threads)
-
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  rho_air      !< air density profile on the uv grid
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  rho_air_zw   !< air density profile on the w grid
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  drho_air     !< inverse air density profile on the uv grid
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  drho_air_zw  !< inverse air density profile on the w grid
-
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  rho_air_mg     !< air density profiles on the uv grid for multigrid
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  rho_air_zw_mg  !< air density profiles on the w grid for multigrid
-
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  heatflux_input_conversion       !< conversion factor array for heatflux input
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  waterflux_input_conversion      !< conversion factor array for waterflux input
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  momentumflux_input_conversion   !< conversion factor array for momentumflux input
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  heatflux_output_conversion      !< conversion factor array for heatflux output
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  waterflux_output_conversion     !< conversion factor array for waterflux output
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  momentumflux_output_conversion  !< conversion factor array for momentumflux output
-
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  hyrho   !< density of air calculated with hydrostatic pressure
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  exner   !< ratio of actual and potential temperature
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  d_exner !< ratio of potential and actual temperature
+    REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::  tri    !< array to hold the tridiagonal matrix for solution of the Poisson
+                                                         !< equation in Fourier space (4th dimension for threads)
 
     SAVE
@@ -530,9 +634,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Definition of variables needed for time-averaging of 2d/3d data.
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  MODULE averaging
 
@@ -594,9 +698,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Definition of parameters for program control
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  MODULE control_parameters
 
@@ -608,6 +712,9 @@
     END TYPE file_status
 
-    INTEGER, PARAMETER      ::  mask_xyz_dimension = 100  !< limit of mask dimensions (100 points in each direction)
-    INTEGER, PARAMETER      ::  max_masks = 50            !< maximum number of masks
+    INTEGER(iwp), PARAMETER ::  fl_max = 500              !< maximum number of virtual-flight measurements
+    INTEGER,      PARAMETER ::  mask_xyz_dimension = 100  !< limit of mask dimensions (100 points in each direction)
+    INTEGER,      PARAMETER ::  max_masks = 50            !< maximum number of masks
+    INTEGER(iwp), PARAMETER ::  var_fl_max = 20           !< maximum number of different sampling variables in virtual flight
+                                                          !< measurements
     INTEGER(iwp), PARAMETER ::  varnamelength = 30        !< length of output variable names
 
@@ -616,16 +723,19 @@
 
     CHARACTER (LEN=1)    ::  cycle_mg = 'w'                               !< namelist parameter (see documentation)
-    CHARACTER (LEN=1)    ::  timestep_reason = ' '                        !< 'A'dvection or 'D'iffusion criterion, written to RUN_CONTROL file
-    CHARACTER (LEN=8)    ::  coupling_char = ''                           !< appended to filenames in coupled or nested runs ('_O': ocean PE,
-                                                                          !< '_NV': vertically nested atmosphere PE, '_N##': PE of nested domain ##
-    CHARACTER (LEN=23)   ::  origin_date_time = '2019-06-21 12:00:00 +00' !< date and time to be simulated
+    CHARACTER (LEN=1)    ::  timestep_reason = ' '                        !< 'A'dvection or 'D'iffusion criterion, written to
+                                                                          !< RUN_CONTROL file
+    CHARACTER (LEN=5)    ::  run_zone = ' '                               !< time zone of simulation run
+    CHARACTER (LEN=8)    ::  coupling_char = ''                           !< appended to filenames in coupled or nested runs
+                                                                          !< ('_O': ocean PE,
+                                                                          !< '_NV': vertically nested atmosphere PE, '_N##': PE of
+                                                                          !< nested domain ##
+    CHARACTER (LEN=8)    ::  run_time = ' '                               !< time of simulation run
+    CHARACTER (LEN=9)    ::  simulated_time_chr                           !< simulated time, printed to RUN_CONTROL file
     CHARACTER (LEN=10)   ::  run_date = ' '                               !< date of simulation run
-    CHARACTER (LEN=8)    ::  run_time = ' '                               !< time of simulation run
-    CHARACTER (LEN=5)    ::  run_zone = ' '                               !< time zone of simulation run
-    CHARACTER (LEN=9)    ::  simulated_time_chr                           !< simulated time, printed to RUN_CONTROL file
     CHARACTER (LEN=11)   ::  topography_grid_convention = ' '             !< namelist parameter
+    CHARACTER (LEN=12)   ::  revision = ' '                               !< PALM revision number
     CHARACTER (LEN=12)   ::  version = ' '                                !< PALM version number
-    CHARACTER (LEN=12)   ::  revision = ' '                               !< PALM revision number
-    CHARACTER (LEN=12)   ::  user_interface_current_revision = ' '        !< revision number of the currently used user-interface (must match user_interface_required_revision)
+    CHARACTER (LEN=12)   ::  user_interface_current_revision = ' '        !< revision number of the currently used user-interface
+                                                                          !< (must match user_interface_required_revision)
     CHARACTER (LEN=12)   ::  user_interface_required_revision = ' '       !< required user-interface revision number
     CHARACTER (LEN=16)   ::  conserve_volume_flow_mode = 'default'        !< namelist parameter
@@ -635,6 +745,4 @@
     CHARACTER (LEN=16)   ::  scalar_advec = 'ws-scheme'                   !< namelist parameter
     CHARACTER (LEN=20)   ::  approximation = 'boussinesq'                 !< namelist parameter
-    CHARACTER (LEN=40)   ::  flux_input_mode = 'approximation-specific'   !< type of flux input: dynamic or kinematic
-    CHARACTER (LEN=40)   ::  flux_output_mode = 'approximation-specific'  !< type of flux output: dynamic or kinematic
     CHARACTER (LEN=20)   ::  bc_e_b = 'neumann'                           !< namelist parameter
     CHARACTER (LEN=20)   ::  bc_lr = 'cyclic'                             !< namelist parameter
@@ -651,10 +759,10 @@
     CHARACTER (LEN=20)   ::  bc_uv_t = 'dirichlet'                        !< namelist parameter
     CHARACTER (LEN=20)   ::  coupling_mode = 'uncoupled'                  !< coupling mode for atmosphere-ocean coupling
-    CHARACTER (LEN=20)   ::  coupling_mode_remote = 'uncoupled'           !< coupling mode of the remote process in case of coupled atmosphere-ocean runs
+    CHARACTER (LEN=20)   ::  coupling_mode_remote = 'uncoupled'           !< coupling mode of the remote process in case of coupled
+                                                                          !< atmosphere-ocean runs
     CHARACTER (LEN=20)   ::  dissipation_1d = 'detering'                  !< namelist parameter
     CHARACTER (LEN=20)   ::  fft_method = 'temperton-algorithm'           !< namelist parameter
     CHARACTER (LEN=20)   ::  mixing_length_1d = 'blackadar'               !< namelist parameter
     CHARACTER (LEN=20)   ::  random_generator = 'random-parallel'         !< namelist parameter
-    CHARACTER (LEN=80)   ::  recycling_method_for_thermodynamic_quantities = 'turbulent_fluctuation'        !< namelist parameter
     CHARACTER (LEN=20)   ::  reference_state = 'initial_profile'          !< namelist parameter
     CHARACTER (LEN=20)   ::  restart_data_format = 'fortran_binary'       !< namelist parameter
@@ -663,34 +771,39 @@
     CHARACTER (LEN=20)   ::  timestep_scheme = 'runge-kutta-3'            !< namelist parameter
     CHARACTER (LEN=20)   ::  turbulence_closure = '1.5-order'             !< namelist parameter
+    CHARACTER (LEN=23)   ::  origin_date_time = '2019-06-21 12:00:00 +00' !< date and time to be simulated
+    CHARACTER (LEN=40)   ::  flux_input_mode = 'approximation-specific'   !< type of flux input: dynamic or kinematic
+    CHARACTER (LEN=40)   ::  flux_output_mode = 'approximation-specific'  !< type of flux output: dynamic or kinematic
     CHARACTER (LEN=40)   ::  topography = 'flat'                          !< namelist parameter
-    CHARACTER (LEN=64)   ::  host = '????'                                !< configuration identifier as given by palmrun option -c, ENVPAR namelist parameter provided by palmrun
+    CHARACTER (LEN=64)   ::  host = '????'                                !< configuration identifier as given by palmrun option -c,
+                                                                          !< ENVPAR namelist parameter provided by palmrun
     CHARACTER (LEN=80)   ::  log_message                                  !< user-defined message for debugging (sse data_log.f90)
-    CHARACTER (LEN=80)   ::  run_identifier                               !< run identifier as given by palmrun option -r, ENVPAR namelist parameter provided by palmrun
+    CHARACTER (LEN=80)   ::  recycling_method_for_thermodynamic_quantities = 'turbulent_fluctuation'        !< namelist parameter
+    CHARACTER (LEN=80)   ::  run_identifier                               !< run identifier as given by palmrun option -r, ENVPAR
+                                                                          !< namelist parameter provided by palmrun
     CHARACTER (LEN=100)  ::  initializing_actions = ' '                   !< namelist parameter
-    CHARACTER (LEN=100)  ::  restart_string = ' '                         !< for storing strings in case of writing/reading restart data
-    CHARACTER (LEN=210)  ::  run_description_header                       !< string containing diverse run informations as run identifier, coupling mode, host, ensemble number, run date and time
+    CHARACTER (LEN=100)  ::  restart_string = ' '                         !< for storing strings in case of writing/reading restart
+                                                                          !< data
+    CHARACTER (LEN=210)  ::  run_description_header                       !< string containing diverse run informations as run
+                                                                          !< identifier, coupling mode, host, ensemble number, run
+                                                                          !< date and time
     CHARACTER (LEN=1000) ::  debug_string = ' '                           !<.....
     CHARACTER (LEN=1000) ::  message_string = ' '                         !< dynamic string for error message output
 
-    CHARACTER (LEN=varnamelength), DIMENSION(500) ::  data_output = ' '       !< namelist parameter
-    CHARACTER (LEN=varnamelength), DIMENSION(500) ::  data_output_user = ' '  !< namelist parameter
-    CHARACTER (LEN=varnamelength), DIMENSION(500) ::  doav = ' '              !< label array for multi-dimensional,
-                                                                              !< averaged output quantities
+    CHARACTER (LEN=varnamelength), DIMENSION(200) ::  data_output_pr_user = ' '  !< namelist parameter
+    CHARACTER (LEN=varnamelength), DIMENSION(300) ::  data_output_pr = ' '       !< namelist parameter
+    CHARACTER (LEN=varnamelength), DIMENSION(500) ::  data_output = ' '          !< namelist parameter
+    CHARACTER (LEN=varnamelength), DIMENSION(500) ::  data_output_user = ' '     !< namelist parameter
+    CHARACTER (LEN=varnamelength), DIMENSION(500) ::  doav = ' '                 !< label array for multi-dimensional,
+                                                                                 !< averaged output quantities
 
     CHARACTER (LEN=varnamelength), DIMENSION(max_masks,100) ::  data_output_masks = ' '       !< namelist parameter
     CHARACTER (LEN=varnamelength), DIMENSION(max_masks,100) ::  data_output_masks_user = ' '  !< namelist parameter
-
-    CHARACTER (LEN=varnamelength), DIMENSION(300) ::  data_output_pr = ' '  !< namelist parameter
-
-    CHARACTER (LEN=varnamelength), DIMENSION(200) ::  data_output_pr_user = ' '  !< namelist parameter
+    CHARACTER (LEN=varnamelength), DIMENSION(0:1,500)       ::  do2d = ' '                    !< label array for 2d output
+                                                                                              !< quantities
+    CHARACTER (LEN=varnamelength), DIMENSION(0:1,500)       ::  do3d = ' '                    !< label array for 3d output
+                                                                                              !< quantities
 
     CHARACTER (LEN=varnamelength), DIMENSION(max_masks,0:1,100) ::  domask = ' ' !< label array for multi-dimensional,
                                                                                  !< masked output quantities
-
-    CHARACTER (LEN=varnamelength), DIMENSION(0:1,500) ::  do2d = ' '  !< label array for 2d output quantities
-    CHARACTER (LEN=varnamelength), DIMENSION(0:1,500) ::  do3d = ' '  !< label array for 3d output quantities
-
-    INTEGER(iwp), PARAMETER ::  fl_max = 500     !< maximum number of virtual-flight measurements
-    INTEGER(iwp), PARAMETER ::  var_fl_max = 20  !< maximum number of different sampling variables in virtual flight measurements
 
     INTEGER(iwp) ::  abort_mode = 1                    !< abort condition (nested runs)
@@ -699,6 +812,8 @@
     INTEGER(iwp) ::  average_count_3d = 0              !< number of samples in 3d output
     INTEGER(iwp) ::  current_timestep_number = 0       !< current timestep number, printed to RUN_CONTROL file
-    INTEGER(iwp) ::  coupling_topology = 0             !< switch for atmosphere-ocean-coupling: 0: same number of grid points and PEs along x and y in atmosphere and ocean, otherwise 1
-    INTEGER(iwp) ::  dist_range = 0                    !< switch for steering the horizontal disturbance range, 1: inflow disturbances in case of non-cyclic horizontal BC, 0: otherwise
+    INTEGER(iwp) ::  coupling_topology = 0             !< switch for atmosphere-ocean-coupling: 0: same number of grid points and
+                                                       !< PEs along x and y in atmosphere and ocean, otherwise 1
+    INTEGER(iwp) ::  dist_range = 0                    !< switch for steering the horizontal disturbance range, 1: inflow
+                                                       !< disturbances in case of non-cyclic horizontal BC, 0: otherwise
     INTEGER(iwp) ::  disturbance_level_ind_b           !< lowest grid index where flow disturbance is applied
     INTEGER(iwp) ::  disturbance_level_ind_t           !< highest grid index where flow disturbance is applied
@@ -711,5 +826,6 @@
     INTEGER(iwp) ::  ensemble_member_nr = 0            !< namelist parameter
     INTEGER(iwp) ::  gamma_mg                          !< switch for steering the multigrid cycle: 1: v-cycle, 2: w-cycle
-    INTEGER(iwp) ::  gathered_size                     !< number of total domain grid points of the grid level which is gathered on PE0 (multigrid solver)
+    INTEGER(iwp) ::  gathered_size                     !< number of total domain grid points of the grid level which is gathered on
+                                                       !< PE0 (multigrid solver)
     INTEGER(iwp) ::  grid_level                        !< current grid level handled in the multigrid solver
     INTEGER(iwp) ::  ibc_e_b                           !< integer flag for bc_e_b
@@ -729,14 +845,19 @@
     INTEGER(iwp) ::  intermediate_timestep_count_max   !< maximum number of Runge-Kutta substeps
     INTEGER(iwp) ::  io_group = 0                      !< I/O group to which the PE belongs (= #PE / io_blocks)
-    INTEGER(iwp) ::  io_blocks = 1                     !< number of blocks for which I/O is done in sequence (total number of PEs / maximum_parallel_io_streams)
+    INTEGER(iwp) ::  io_blocks = 1                     !< number of blocks for which I/O is done in sequence (total number of PEs /
+                                                       !< maximum_parallel_io_streams)
     INTEGER(iwp) ::  iran = -1234567                   !< integer random number used for flow disturbances
-    INTEGER(iwp) ::  length = 0                        !< integer that specifies the length of a string in case of writing/reading restart data
+    INTEGER(iwp) ::  length = 0                        !< integer that specifies the length of a string in case of writing/reading
+                                                       !< restart data
     INTEGER(iwp) ::  masks = 0                         !< counter for number of masked output quantities
     INTEGER(iwp) ::  maximum_grid_level                !< number of grid levels that the multigrid solver is using
-    INTEGER(iwp) ::  maximum_parallel_io_streams = -1  !< maximum number of parallel io streams that the underlying parallel file system allows, set with palmrun option -w, ENVPAR namelist parameter, provided by palmrun
+    INTEGER(iwp) ::  maximum_parallel_io_streams = -1  !< maximum number of parallel io streams that the underlying parallel file
+                                                       !< system allows, set with palmrun option -w, ENVPAR namelist parameter, provided by palmrun
     INTEGER(iwp) ::  max_pr_salsa = 0                  !< number of salsa profiles (must not change within a job chain)
     INTEGER(iwp) ::  max_pr_user = 0                   !< number of user-defined profiles (must not change within a job chain)
-    INTEGER(iwp) ::  max_pr_user_tmp = 0               !< number of user-defined profiles that is temporary stored to check it against max_pr_user in case of restarts
-    INTEGER(iwp) ::  mgcycles = 0                      !< number of multigrid cycles that the multigrid solver has actually carried out
+    INTEGER(iwp) ::  max_pr_user_tmp = 0               !< number of user-defined profiles that is temporary stored to check it
+                                                       !< against max_pr_user in case of restarts
+    INTEGER(iwp) ::  mgcycles = 0                      !< number of multigrid cycles that the multigrid solver has actually carried
+                                                       !< out
     INTEGER(iwp) ::  mg_cycles = 4                     !< namelist parameter
     INTEGER(iwp) ::  mg_switch_to_pe0_level = -1       !< namelist parameter
@@ -750,15 +871,18 @@
     INTEGER(iwp) ::  num_leg=0                         !< number of different legs in virtual flight measurements
     INTEGER(iwp) ::  num_var_fl                        !< number of sampling/output variables in virtual flight measurements
-    INTEGER(iwp) ::  num_var_fl_user=0                 !< number of user-defined sampling/output variables in virtual flight measurements
+    INTEGER(iwp) ::  num_var_fl_user=0                 !< number of user-defined sampling/output variables in virtual flight
+                                                       !< measurements
     INTEGER(iwp) ::  number_stretch_level_start        !< number of user-specified start levels for stretching
     INTEGER(iwp) ::  number_stretch_level_end          !< number of user-specified end levels for stretching
     INTEGER(iwp) ::  nz_do3d = -9999                   !< namelist parameter
     INTEGER(iwp) ::  prt_time_count = 0                !< number of output intervals for particle data output
-    INTEGER(iwp) ::  recycling_plane                   !< position of recycling plane along x (in grid points) in case of turbulence recycling
+    INTEGER(iwp) ::  recycling_plane                   !< position of recycling plane along x (in grid points) in case of turbulence
+                                                       !< recycling
     INTEGER(iwp) ::  runnr = 0                         !< number of run in job chain
     INTEGER(iwp) ::  subdomain_size                    !< number of grid points in (3d) subdomain including ghost points
     INTEGER(iwp) ::  symmetry_flag = 0                 !< flag for sterring the symmetric behavior of the bottom and top boundary
     INTEGER(iwp) ::  terminate_coupled = 0             !< switch for steering termination in case of coupled runs
-    INTEGER(iwp) ::  terminate_coupled_remote = 0      !< switch for steering termination in case of coupled runs (condition of the remote model)
+    INTEGER(iwp) ::  terminate_coupled_remote = 0      !< switch for steering termination in case of coupled runs (condition of the
+                                                       !< remote model)
     INTEGER(iwp) ::  timestep_count = 0                !< number of timesteps carried out since the beginning of the initial run
     INTEGER(iwp) ::  y_shift = 0                       !< namelist parameter
@@ -776,8 +900,11 @@
     INTEGER(iwp) ::  domask_no(max_masks,0:1) = 0                !< number of masked output quantities
     INTEGER(iwp) ::  domask_time_count(max_masks,0:1)            !< number of output intervals for masked data
-    INTEGER(iwp) ::  dz_stretch_level_end_index(9)               !< vertical grid level index until which the vertical grid spacing is stretched
-    INTEGER(iwp) ::  dz_stretch_level_start_index(9)             !< vertical grid level index above which the vertical grid spacing is stretched
+    INTEGER(iwp) ::  dz_stretch_level_end_index(9)               !< vertical grid level index until which the vertical grid spacing
+                                                                 !< is stretched
+    INTEGER(iwp) ::  dz_stretch_level_start_index(9)             !< vertical grid level index above which the vertical grid spacing
+                                                                 !< is stretched
     INTEGER(iwp) ::  mask_size(max_masks,3) = -1                 !< size of mask array per mask and dimension (for netcdf output)
-    INTEGER(iwp) ::  mask_size_l(max_masks,3) = -1               !< subdomain size of mask array per mask and dimension (for netcdf output)
+    INTEGER(iwp) ::  mask_size_l(max_masks,3) = -1               !< subdomain size of mask array per mask and dimension
+                                                                 !< (for netcdf output)
     INTEGER(iwp) ::  mask_start_l(max_masks,3) = -1              !< subdomain start index of mask array (for netcdf output)
     INTEGER(iwp) ::  pt_vertical_gradient_level_ind(10) = -9999  !< grid index values of pt_vertical_gradient_level(s)
@@ -796,4 +923,7 @@
     INTEGER(iwp), DIMENSION(0:1) ::  ntdim_3d     !< number of output intervals for 3d data
 
+    INTEGER(iwp), DIMENSION(max_masks,mask_xyz_dimension) ::  mask_k_over_surface = -1  !< namelist parameter, k index of height
+                                                                                        !<over surface
+
     INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  grid_level_count  !< internal switch for steering the multigrid v- and w-cycles
 
@@ -805,12 +935,15 @@
     INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  mask_k_global  !< global grid index of masked output point on z-dimension
 
-    INTEGER(iwp), DIMENSION(max_masks,mask_xyz_dimension) ::  mask_k_over_surface = -1  !< namelist parameter, k index of height over surface
 
     LOGICAL ::  agent_time_unlimited = .FALSE.                   !< namelist parameter
     LOGICAL ::  air_chemistry = .FALSE.                          !< chemistry model switch
-    LOGICAL ::  bc_dirichlet_l = .FALSE.                         !< flag indicating dirichlet boundary condition on left model boundary
-    LOGICAL ::  bc_dirichlet_n = .FALSE.                         !< flag indicating dirichlet boundary condition on north model boundary
-    LOGICAL ::  bc_dirichlet_r = .FALSE.                         !< flag indicating dirichlet boundary condition on right model boundary
-    LOGICAL ::  bc_dirichlet_s = .FALSE.                         !< flag indicating dirichlet boundary condition on south model boundary
+    LOGICAL ::  bc_dirichlet_l = .FALSE.                         !< flag indicating dirichlet boundary condition on left model
+                                                                 !< boundary
+    LOGICAL ::  bc_dirichlet_n = .FALSE.                         !< flag indicating dirichlet boundary condition on north model
+                                                                 !< boundary
+    LOGICAL ::  bc_dirichlet_r = .FALSE.                         !< flag indicating dirichlet boundary condition on right model
+                                                                 !< boundary
+    LOGICAL ::  bc_dirichlet_s = .FALSE.                         !< flag indicating dirichlet boundary condition on south model
+                                                                 !< boundary
     LOGICAL ::  bc_lr_cyc =.TRUE.                                !< left-right boundary condition cyclic?
     LOGICAL ::  bc_lr_dirrad = .FALSE.                           !< left-right boundary condition dirichlet/radiation?
@@ -820,7 +953,10 @@
     LOGICAL ::  bc_ns_raddir = .FALSE.                           !< north-south boundary condition radiation/dirichlet?
     LOGICAL ::  bc_radiation_l = .FALSE.                         !< radiation boundary condition for outflow at left domain boundary
-    LOGICAL ::  bc_radiation_n = .FALSE.                         !< radiation boundary condition for outflow at north domain boundary
-    LOGICAL ::  bc_radiation_r = .FALSE.                         !< radiation boundary condition for outflow at right domain boundary
-    LOGICAL ::  bc_radiation_s = .FALSE.                         !< radiation boundary condition for outflow at south domain boundary
+    LOGICAL ::  bc_radiation_n = .FALSE.                         !< radiation boundary condition for outflow at north domain
+                                                                 !< boundary
+    LOGICAL ::  bc_radiation_r = .FALSE.                         !< radiation boundary condition for outflow at right domain
+                                                                 !< boundary
+    LOGICAL ::  bc_radiation_s = .FALSE.                         !< radiation boundary condition for outflow at south domain
+                                                                 !< boundary
     LOGICAL ::  biometeorology = .FALSE.                         !< biometeorology module switch
     LOGICAL ::  calc_soil_moisture_during_spinup = .FALSE.       !< namelist parameter
@@ -859,12 +995,15 @@
     LOGICAL ::  galilei_transformation = .FALSE.                 !< namelist parameter
     LOGICAL ::  humidity = .FALSE.                               !< namelist parameter
-    LOGICAL ::  humidity_remote = .FALSE.                        !< switch for receiving near-surface humidity flux (atmosphere-ocean coupling)
+    LOGICAL ::  humidity_remote = .FALSE.                        !< switch for receiving near-surface humidity flux
+                                                                 !< (atmosphere-ocean coupling)
     LOGICAL ::  include_total_domain_boundaries = .FALSE.        !< store outer boundaries in restart file (MPI-IO)
     LOGICAL ::  indoor_model = .FALSE.                           !< switch for indoor-climate and energy-demand model
-    LOGICAL ::  kolmogorov_length_scale = .FALSE.                !< switch to activate calculations in flow_statistics for the kolmogorov length scale
+    LOGICAL ::  kolmogorov_length_scale = .FALSE.                !< switch to activate calculations in flow_statistics for the
+                                                                 !< kolmogorov length scale
     LOGICAL ::  large_scale_forcing = .FALSE.                    !< namelist parameter
     LOGICAL ::  large_scale_subsidence = .FALSE.                 !< namelist parameter
     LOGICAL ::  land_surface = .FALSE.                           !< use land surface model?
-    LOGICAL ::  les_dai = .FALSE.                                !< use Dai et al. turbulence closure (modified 1.5-order closure) for LES mode. Shall replace the default 1.5-order closure
+    LOGICAL ::  les_dai = .FALSE.                                !< use Dai et al. turbulence closure (modified 1.5-order closure)
+                                                                 !< for LES mode. Shall replace the default 1.5-order closure
     LOGICAL ::  les_dynamic = .FALSE.                            !< use dynamic subgrid model as turbulence closure for LES mode
     LOGICAL ::  les_default = .FALSE.                            !< use 1.5-order default turbulence closure for LES mode
@@ -873,5 +1012,6 @@
     LOGICAL ::  lsf_vert = .TRUE.                                !< use atmospheric forcing (large scale forcing)?
     LOGICAL ::  masking_method = .FALSE.                         !< namelist parameter
-    LOGICAL ::  mg_switch_to_pe0 = .FALSE.                       !< internal multigrid switch for steering the ghost point exchange in case that data has been collected on PE0
+    LOGICAL ::  mg_switch_to_pe0 = .FALSE.                       !< internal multigrid switch for steering the ghost point exchange
+                                                                 !< in case that data has been collected on PE0
     LOGICAL ::  monotonic_limiter_z = .FALSE.                    !< use monotonic flux limiter for vertical scalar advection
     LOGICAL ::  nesting_offline = .FALSE.                        !< flag controlling offline nesting in COSMO model
@@ -885,7 +1025,9 @@
     LOGICAL ::  rans_tke_e = .FALSE.                             !< use TKE-e turbulence closure for RANS mode
     LOGICAL ::  rans_tke_l = .FALSE.                             !< use TKE-l turbulence closure for RANS mode
-    LOGICAL ::  read_svf = .FALSE.                               !< ENVPAR namelist parameter to steer input of svf (ENVPAR is provided by palmrun)
+    LOGICAL ::  read_svf = .FALSE.                               !< ENVPAR namelist parameter to steer input of svf
+                                                                 !< (ENVPAR is provided by palmrun)
     LOGICAL ::  run_control_header = .FALSE.                     !< onetime output of RUN_CONTROL header
-    LOGICAL ::  run_coupled = .TRUE.                             !< internal switch telling PALM to run in coupled mode (i.e. to exchange surface data) in case of atmosphere-ocean coupling
+    LOGICAL ::  run_coupled = .TRUE.                             !< internal switch telling PALM to run in coupled mode
+                                                                 !< (i.e. to exchange surface data) in case of atmosphere-ocean coupling
     LOGICAL ::  salsa = .FALSE.                                  !< switch for the sectional aerosol module salsa
     LOGICAL ::  scalar_rayleigh_damping = .TRUE.                 !< namelist parameter
@@ -905,5 +1047,6 @@
     LOGICAL ::  use_fixed_date = .FALSE.                         !< date of simulation does not change (namelist parameter)
     LOGICAL ::  use_fixed_time = .FALSE.                         !< time of simulation does not change (namelist parameter)
-    LOGICAL ::  use_free_convection_scaling = .FALSE.            !< namelist parameter to switch on free convection velocity scale in calculation of horizontal wind speed (surface_layer_fluxes)
+    LOGICAL ::  use_free_convection_scaling = .FALSE.            !< namelist parameter to switch on free convection velocity scale
+                                                                 !< in calculation of horizontal wind speed (surface_layer_fluxes)
     LOGICAL ::  use_initial_profile_as_reference = .FALSE.       !< use of initial profiles as reference state?
     LOGICAL ::  use_prescribed_profile_data = .FALSE.            !< use of prescribed wind profiles?
@@ -920,6 +1063,8 @@
     LOGICAL ::  wall_adjustment = .TRUE.                         !< namelist parameter
     LOGICAL ::  wind_turbine = .FALSE.                           !< flag for use of wind turbine model
-    LOGICAL ::  write_binary = .FALSE.                           !< ENVPAR namelist parameter to steer restart I/O (ENVPAR is provided by palmrun)
-    LOGICAL ::  write_svf = .FALSE.                              !< ENVPAR namelist parameter to steer output of svf (ENVPAR is provided by palmrun)
+    LOGICAL ::  write_binary = .FALSE.                           !< ENVPAR namelist parameter to steer restart I/O
+                                                                 !< (ENVPAR is provided by palmrun)
+    LOGICAL ::  write_svf = .FALSE.                              !< ENVPAR namelist parameter to steer output of svf
+                                                                 !< (ENVPAR is provided by palmrun)
     LOGICAL ::  ws_scheme_sca = .FALSE.                          !< use Wicker-Skamarock scheme (scalar advection)?
     LOGICAL ::  ws_scheme_mom = .FALSE.                          !< use Wicker-Skamarock scheme (momentum advection)?
@@ -1067,5 +1212,6 @@
     REAL(wp) ::  time_restart = 9999999.9_wp                   !< time at which run shall be terminated and restarted
     REAL(wp) ::  time_run_control = 0.0_wp                     !< time since last RUN_CONTROL output
-    REAL(wp) ::  time_since_reference_point = 0.0_wp           !< time after atmosphere-ocean coupling has been activated, or time after spinup phase of LSM has been finished
+    REAL(wp) ::  time_since_reference_point = 0.0_wp           !< time after atmosphere-ocean coupling has been activated, or time
+                                                               !< after spinup phase of LSM has been finished
     REAL(wp) ::  top_heatflux = 9999999.9_wp                   !< namelist parameter
     REAL(wp) ::  top_momentumflux_u = 9999999.9_wp             !< namelist parameter
@@ -1100,9 +1246,9 @@
     REAL(wp) ::  mask_scale(3)                                     !< collective array for mask_scale_x/y/z
     REAL(wp) ::  pt_vertical_gradient(10) = 0.0_wp                 !< namelist parameter
-    REAL(wp) ::  pt_vertical_gradient_level(10) = -999999.9_wp   !< namelist parameter
+    REAL(wp) ::  pt_vertical_gradient_level(10) = -999999.9_wp     !< namelist parameter
     REAL(wp) ::  q_vertical_gradient(10) = 0.0_wp                  !< namelist parameter
-    REAL(wp) ::  q_vertical_gradient_level(10) = -999999.9_wp    !< namelist parameter
+    REAL(wp) ::  q_vertical_gradient_level(10) = -999999.9_wp      !< namelist parameter
     REAL(wp) ::  s_vertical_gradient(10) = 0.0_wp                  !< namelist parameter
-    REAL(wp) ::  s_vertical_gradient_level(10) = -999999.9_wp    !< namelist parameter
+    REAL(wp) ::  s_vertical_gradient_level(10) = -999999.9_wp      !< namelist parameter
     REAL(wp) ::  skip_time_domask(max_masks) = 9999999.9_wp        !< namelist parameter
     REAL(wp) ::  threshold(20) = 0.0_wp                            !< namelist parameter
@@ -1117,7 +1263,9 @@
     REAL(wp) ::  vg_vertical_gradient(10) = 0.0_wp                 !< namelist parameter
     REAL(wp) ::  vg_vertical_gradient_level(10) = -9999999.9_wp    !< namelist parameter
-    REAL(wp) ::  volume_flow(1:3) = 0.0_wp                         !< volume flow through 1:yz-plane, 2: xz-plane, 3: xy-plane (nest childs only)
+    REAL(wp) ::  volume_flow(1:3) = 0.0_wp                         !< volume flow through 1:yz-plane, 2: xz-plane, 3: xy-plane
+                                                                   !< (nest childs only)
     REAL(wp) ::  volume_flow_area(1:3) = 0.0_wp                    !< area of the respective volume flow planes
-    REAL(wp) ::  volume_flow_initial(1:3) = 0.0_wp                 !< initial volume flow (t=0) through the respective volume flow planes
+    REAL(wp) ::  volume_flow_initial(1:3) = 0.0_wp                 !< initial volume flow (t=0) through the respective volume flow
+                                                                   !< planes
     REAL(wp) ::  wall_heatflux(0:5) = 0.0_wp                       !< namelist parameter
     REAL(wp) ::  wall_humidityflux(0:5) = 0.0_wp                   !< namelist parameter
@@ -1138,5 +1286,5 @@
 
 !
-!--    internal mask arrays ("mask,dimension,selection")
+!--    Internal mask arrays ("mask,dimension,selection")
        REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  mask       !< collective array for mask_x/y/z
        REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  mask_loop  !< collective array for mask_x/y/z_loop
@@ -1147,9 +1295,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Definition of grid spacings.
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  MODULE grid_variables
 
@@ -1176,9 +1324,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Definition of array bounds, number of gridpoints, and wall flag arrays.
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  MODULE indices
 
@@ -1186,6 +1334,8 @@
 
     INTEGER(iwp) ::  nbgp = 3       !< number of boundary ghost points
-    INTEGER(iwp) ::  ngp_sums       !< number of vertical profile grid points time number of output profiles - used for allreduce statements in MPI calls
-    INTEGER(iwp) ::  ngp_sums_ls    !< number of vertical profile grid points time number of large-scale forcing profiles - used for allreduce statements in MPI calls
+    INTEGER(iwp) ::  ngp_sums       !< number of vertical profile grid points time number of output profiles - used for allreduce
+                                    !< statements in MPI calls
+    INTEGER(iwp) ::  ngp_sums_ls    !< number of vertical profile grid points time number of large-scale forcing profiles - used for
+                                    !< allreduce statements in MPI calls
     INTEGER(iwp) ::  nnx            !< number of subdomain grid points in x-direction
     INTEGER(iwp) ::  nx = 0         !< nx+1 = total number of grid points in x-direction
@@ -1194,5 +1344,6 @@
     INTEGER(iwp) ::  nxl            !< left-most grid index of subdomain (excluding ghost points)
     INTEGER(iwp) ::  nxlg           !< left-most grid index of subdomain (including ghost points)
-    INTEGER(iwp) ::  nxlu           !< =nxl+1 (at left domain boundary with inflow from left), else =nxl (used for u-velocity component)
+    INTEGER(iwp) ::  nxlu           !< =nxl+1 (at left domain boundary with inflow from left), else =nxl
+                                    !< (used for u-velocity component)
     INTEGER(iwp) ::  nxr            !< right-most grid index of subdomain (excluding ghost points)
     INTEGER(iwp) ::  nxrg           !< right-most grid index of subdomain (including ghost points)
@@ -1206,5 +1357,6 @@
     INTEGER(iwp) ::  nys            !< south-most grid index of subdomain (excluding ghost points)
     INTEGER(iwp) ::  nysg           !< south-most grid index of subdomain (including ghost points)
-    INTEGER(iwp) ::  nysv           !< =nys+1 (at south domain boundary with inflow from south), else =nys (used for v-velocity component)
+    INTEGER(iwp) ::  nysv           !< =nys+1 (at south domain boundary with inflow from south), else =nys
+                                    !< (used for v-velocity component)
     INTEGER(iwp) ::  ny_on_file     !< ny of previous run in job chain
     INTEGER(iwp) ::  nnz            !< number of subdomain grid points in z-direction
@@ -1216,20 +1368,20 @@
     INTEGER(iwp) ::  topo_min_level !< minimum topography-top index (usually equal to nzb)
 
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  ngp_2dh       !< number of grid points of a horizontal cross section through the
+                                                              !< total domain
     INTEGER(idp), DIMENSION(:), ALLOCATABLE ::  ngp_3d        !< number of grid points of the total domain
     INTEGER(idp), DIMENSION(:), ALLOCATABLE ::  ngp_3d_inner  !< ! need to have 64 bit for grids > 2E9
-
-    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  ngp_2dh  !< number of grid points of a horizontal cross section through the total domain
-    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nxl_mg   !< left-most grid index of subdomain on different multigrid level
-    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nxr_mg   !< right-most grid index of subdomain on different multigrid level
-    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nyn_mg   !< north-most grid index of subdomain on different multigrid level
-    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nys_mg   !< south-most grid index of subdomain on different multigrid level
-    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nzt_mg   !< top-most grid index of subdomain on different multigrid level
-
-
-    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  ngp_2dh_outer     !< number of horizontal grid points which are non-topography and non-surface-bounded
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nxl_mg        !< left-most grid index of subdomain on different multigrid level
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nxr_mg        !< right-most grid index of subdomain on different multigrid level
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nyn_mg        !< north-most grid index of subdomain on different multigrid level
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nys_mg        !< south-most grid index of subdomain on different multigrid level
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nzt_mg        !< top-most grid index of subdomain on different multigrid level
+
+
+    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  mg_loc_ind        !< internal array to store index bounds of all PEs of that
+                                                                    !< multigrid level where data is collected to PE0
+    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  ngp_2dh_outer     !< number of horizontal grid points which are non-topography and
+                                                                    !< non-surface-bounded
     INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  ngp_2dh_s_inner   !< number of horizontal grid points which are non-topography
-    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  mg_loc_ind        !< internal array to store index bounds of all PEs of that multigrid level where data is collected to PE0
-
-    INTEGER(iwp), DIMENSION(:,:,:), POINTER ::  flags  !< pointer to wall_flags_1-10
 
     INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE,  TARGET ::  wall_flags_1   !< topograpyh masking flag on multigrid level 1
@@ -1244,9 +1396,15 @@
     INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE,  TARGET ::  wall_flags_10  !< topograpyh masking flag on multigrid level 10
 
-    INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE ::  advc_flags_m            !< flags used to degrade order of advection scheme for momentum
-    INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE ::  advc_flags_s            !< flags used to degrade order of advection scheme for scalar quantities
+    INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE ::  advc_flags_m            !< flags used to degrade order of advection scheme for
+                                                                            !< momentum
+    INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE ::  advc_flags_s            !< flags used to degrade order of advection scheme for
+                                                                            !< scalar quantities
     INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE ::  topo_top_ind            !< precalculated topography top indices
-    INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE ::  wall_flags_static_0     !< flags to mask topography and surface-bounded grid points
-    INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE ::  wall_flags_total_0      !< merged array, which contains the static and dynamic flags
+    INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE ::  wall_flags_static_0     !< flags to mask topography and surface-bounded grid
+                                                                            !< points
+    INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE ::  wall_flags_total_0      !< merged array, which contains the static and dynamic
+                                                                            !< flags
+
+    INTEGER(iwp), DIMENSION(:,:,:), POINTER ::  flags  !< pointer to wall_flags_1-10
 
     SAVE
@@ -1255,24 +1413,26 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Interfaces for special subroutines which use optional parameters.
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  MODULE interfaces
 
     INTERFACE
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> @todo Missing subroutine description.
-!------------------------------------------------------------------------------!
-       SUBROUTINE global_min_max ( i1, i2, j1, j2, k1, k2, array, mode, offset, &
-                                   result, result_ijk, result1, result1_ijk )
+!--------------------------------------------------------------------------------------------------!
+       SUBROUTINE global_min_max ( i1, i2, j1, j2, k1, k2, array, mode, offset, result, result_ijk,&
+                                   result1, result1_ijk )
 
           USE kinds
 
-          CHARACTER (LEN=*), INTENT(IN) ::  mode                      !< mode of global min/max function: can be 'min', 'max', 'minmax', 'abs', or 'absoff'
+          CHARACTER (LEN=*), INTENT(IN) ::  mode                      !< mode of global min/max function: can be 'min', 'max',
+                                                                      !< 'minmax', 'abs', or 'absoff'
+
           INTEGER(iwp), INTENT(IN)      ::  i1                        !< internal index of min/max function
           INTEGER(iwp), INTENT(IN)      ::  i2                        !< internal index of min/max function
@@ -1281,9 +1441,13 @@
           INTEGER(iwp), INTENT(IN)      ::  k1                        !< internal index of min/max function
           INTEGER(iwp), INTENT(IN)      ::  k2                        !< internal index of min/max function
+
           INTEGER(iwp)                  ::  result_ijk(3)             !< grid index result of min/max function
           INTEGER(iwp), OPTIONAL        ::  result1_ijk(3)            !< optional grid index result of min/max function
-          REAL(wp)                      ::  offset                    !< min/max function calculates absolute value with respect to an offset
+
+          REAL(wp)                      ::  offset                    !< min/max function calculates absolute value with respect to
+                                                                      !< an offset
           REAL(wp)                      ::  result                    !< result of min/max function
           REAL(wp), OPTIONAL            ::  result1                   !< optional result of min/max function
+
           REAL(wp), INTENT(IN)          ::  array(i1:i2,j1:j2,k1:k2)  !< input array of min/max function
 
@@ -1297,19 +1461,18 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> Interfaces for subroutines with pointer arguments called in
-!> prognostic_equations.
-!------------------------------------------------------------------------------!
+!> Interfaces for subroutines with pointer arguments called in prognostic_equations.
+!--------------------------------------------------------------------------------------------------!
  MODULE pointer_interfaces
 
     INTERFACE
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> @todo Missing subroutine description.
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
        SUBROUTINE advec_s_bc( sk, sk_char )
 
@@ -1329,11 +1492,10 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> Definition of variables which define processor topology and the exchange of
-!> ghost point layers. This module must be placed in all routines containing
-!> MPI-calls.
-!------------------------------------------------------------------------------!
+!> Definition of variables which define processor topology and the exchange of ghost point layers.
+!> This module must be placed in all routines containing MPI-calls.
+!--------------------------------------------------------------------------------------------------!
  MODULE pegrid
 
@@ -1349,5 +1511,6 @@
     INTEGER(iwp) ::  comm1dx                     !< communicator for domain decomposition along x
     INTEGER(iwp) ::  comm1dy                     !< communicator for domain decomposition along y
-    INTEGER(iwp) ::  comm2d                      !< standard 2d (xy) communicator used in PALM for the process group the PE belongs to
+    INTEGER(iwp) ::  comm2d                      !< standard 2d (xy) communicator used in PALM for the process group the PE belongs
+                                                 !< to
     INTEGER(iwp) ::  comm_inter                  !< intercommunicator that connects atmosphere/ocean process groups
     INTEGER(iwp) ::  comm_palm                   !< internal communicator used during the MPI setup at the beginning of a run
@@ -1361,6 +1524,8 @@
     INTEGER(iwp) ::  myidy = 0                   !< id number of processor elements with same position along y-direction
     INTEGER(iwp) ::  ndim = 2                    !< dimension of the virtual PE grid
-    INTEGER(iwp) ::  ngp_a                       !< used in atmosphere/ocean coupling: total number of horizontal grid points (atmosphere)
-    INTEGER(iwp) ::  ngp_o                       !< used in atmosphere/ocean coupling: total number of horizontal grid points (ocean)
+    INTEGER(iwp) ::  ngp_a                       !< used in atmosphere/ocean coupling: total number of horizontal grid points
+                                                 !< (atmosphere)
+    INTEGER(iwp) ::  ngp_o                       !< used in atmosphere/ocean coupling: total number of horizontal grid points
+                                                 !< (ocean)
     INTEGER(iwp) ::  ngp_xy                      !< used in atmosphere/ocean coupling: number of grid points of the subdomain
     INTEGER(iwp) ::  ngp_y                       !< number of subdomain grid points along y including ghost points
@@ -1374,9 +1539,13 @@
     INTEGER(iwp) ::  psouth                      !< MPI id of north neigbour pe
     INTEGER(iwp) ::  req_count = 0               !< MPI return variable - checks if Send-Receive operation is already finished
-    INTEGER(iwp) ::  sendrecvcount_xy            !< number of subdomain gridpoints to be exchanged in direct transpositions (y --> x, or x --> y) or second (2d) transposition x --> y
-    INTEGER(iwp) ::  sendrecvcount_yz            !< number of subdomain gridpoints to be exchanged in third (2d) transposition y --> z
-    INTEGER(iwp) ::  sendrecvcount_zx            !< number of subdomain gridpoints to be exchanged in first (2d) transposition z --> x
+    INTEGER(iwp) ::  sendrecvcount_xy            !< number of subdomain gridpoints to be exchanged in direct transpositions
+                                                 !< (y --> x, or x --> y) or second (2d) transposition x --> y
+    INTEGER(iwp) ::  sendrecvcount_yz            !< number of subdomain gridpoints to be exchanged in third (2d) transposition
+                                                 !< y --> z
+    INTEGER(iwp) ::  sendrecvcount_zx            !< number of subdomain gridpoints to be exchanged in first (2d) transposition
+                                                 !< z --> x
     INTEGER(iwp) ::  sendrecvcount_zyd           !< number of subdomain gridpoints to be exchanged in direct transpositions z --> y (used for calculating spectra)
-    INTEGER(iwp) ::  target_id                   !< in atmosphere/ocean coupling: id of the ocean/atmosphere counterpart PE with whom the atmosphere/ocean PE exchanges data
+    INTEGER(iwp) ::  target_id                   !< in atmosphere/ocean coupling: id of the ocean/atmosphere counterpart PE with
+                                                 !< whom the atmosphere/ocean PE exchanges data
     INTEGER(iwp) ::  tasks_per_node = -9999      !< MPI tasks per compute node
     INTEGER(iwp) ::  threads_per_task = 1        !< number of OPENMP threads per MPI task
@@ -1408,9 +1577,8 @@
     INTEGER(iwp) ::  pcoord(2)                !< PE coordinates along x and y
     INTEGER(iwp) ::  status(MPI_STATUS_SIZE)  !< MPI status variable used in various MPI calls
+    INTEGER(iwp) ::  type_x_byte              !< derived MPI datatype for 2-D 8-bit integer ghost-point exchange - north / south
+    INTEGER(iwp) ::  type_y_byte              !< derived MPI datatype for 2-D integer ghost-point exchange - left / right
 
     INTEGER(iwp), DIMENSION(MPI_STATUS_SIZE,100) ::  wait_stat  !< MPI status variable used in various MPI calls
-
-    INTEGER(iwp) ::  type_x_byte !< derived MPI datatype for 2-D 8-bit integer ghost-point exchange - north / south
-    INTEGER(iwp) ::  type_y_byte !< derived MPI datatype for 2-D integer ghost-point exchange - left / right
 
     INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  ngp_xz      !< number of ghost points in xz-plane on different multigrid level
@@ -1418,10 +1586,16 @@
     INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  ngp_yz      !< number of ghost points in yz-plane on different multigrid level
     INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  ngp_yz_int  !< number of ghost points in yz-plane on different multigrid level
-    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  type_x_int  !< derived MPI datatype for 2-D integer ghost-point exchange - north / south
-    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  type_xz     !< derived MPI datatype for 3-D integer ghost-point exchange - north / south
-    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  type_xz_int !< derived MPI datatype for 3-D integer ghost-point exchange - north / south
-    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  type_y_int  !< derived MPI datatype for 2-D integer ghost-point exchange - left / right
-    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  type_yz     !< derived MPI datatype for 3-D integer ghost-point exchange - left / right
-    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  type_yz_int !< derived MPI datatype for 3-D integer ghost-point exchange - left / right
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  type_x_int  !< derived MPI datatype for 2-D integer ghost-point exchange - north /
+                                                            !< south
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  type_xz     !< derived MPI datatype for 3-D integer ghost-point exchange - north /
+                                                            !< south
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  type_xz_int !< derived MPI datatype for 3-D integer ghost-point exchange - north /
+                                                            !< south
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  type_y_int  !< derived MPI datatype for 2-D integer ghost-point exchange - left /
+                                                            !< right
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  type_yz     !< derived MPI datatype for 3-D integer ghost-point exchange - left /
+                                                            !< right
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  type_yz_int !< derived MPI datatype for 3-D integer ghost-point exchange - left /
+                                                            !< right
 
     LOGICAL ::  left_border_pe  = .FALSE.  !< = .TRUE. if PE is on left border of computational domain
@@ -1440,9 +1614,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Definition of variables which control PROFIL-output.
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  MODULE profil_parameter
 
@@ -1452,25 +1626,25 @@
 
     CHARACTER (LEN=27), DIMENSION(20) ::  cross_ts_profiles = &  !< time series to be plotted into one coordinate system, respectively
-                           (/ ' E E*                      ', &
-                              ' dt                        ', &
-                              ' u* w*                     ', &
-                              ' th*                       ', &
-                              ' umax vmax wmax            ', &
-                              ' div_old div_new           ', &
-                              ' zi_wtheta zi_theta        ', &
-                              ' w"theta"0 w"theta" wtheta ', &
-                              ' theta(0) theta(zp)        ', &
-                              ' splux spluy spluz         ', &
-                              ' L                         ', &
-                            ( '                           ', i9 = 1, 9 ) /)
+                                          (/ ' E E*                      ',                        &
+                                             ' dt                        ',                        &
+                                             ' u* w*                     ',                        &
+                                             ' th*                       ',                        &
+                                             ' umax vmax wmax            ',                        &
+                                             ' div_old div_new           ',                        &
+                                             ' zi_wtheta zi_theta        ',                        &
+                                             ' w"theta"0 w"theta" wtheta ',                        &
+                                             ' theta(0) theta(zp)        ',                        &
+                                             ' splux spluy spluz         ',                        &
+                                             ' L                         ',                        &
+                                           ( '                           ', i9 = 1, 9 ) /)
 
     CHARACTER (LEN=100), DIMENSION(crmax) ::  cross_profiles = &  !< quantities to be plotted into one coordinate system, respectively
-                          (/ ' u v                                          ', &
-                             ' pt                                           ', &
-                             ' w"theta" w*theta* w*theta*BC wtheta wthetaBC ', &
-                             ' w"u" w*u* wu w"v" w*v* wv                    ', &
-                             ' km kh                                        ', &
-                             ' l                                            ', &
-             ( '                                              ', i9 = 1, 94 ) /)
+                                              (/ ' u v                                          ', &
+                                                 ' pt                                           ', &
+                                                 ' w"theta" w*theta* w*theta*BC wtheta wthetaBC ', &
+                                                 ' w"u" w*u* wu w"v" w*v* wv                    ', &
+                                                 ' km kh                                        ', &
+                                                 ' l                                            ', &
+                                 ( '                                              ', i9 = 1, 94 ) /)
 
     INTEGER(iwp) ::  profile_columns = 2  !< number of coordinate systems on a profile plot per column
@@ -1484,15 +1658,14 @@
  END MODULE profil_parameter
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Definition of statistical quantities, e.g. global sums.
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  MODULE statistics
 
     USE kinds
 
-    CHARACTER (LEN=40) ::  region(0:9) =  &  !< label for statistic region
-                           'total domain                            '
+    CHARACTER (LEN=40) ::  region(0:9) = 'total domain                            '  !< label for statistic region
 
     INTEGER(iwp) ::  pr_palm = 200          !< maximum number of output profiles
@@ -1512,36 +1685,53 @@
     REAL(wp), DIMENSION(2) ::  z_i  !< inversion height
 
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  mean_surface_level_height  !< mean surface level height for the different statistic regions
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  mean_surface_level_height  !< mean surface level height for the different statistic
+                                                                       !< regions
     REAL(wp), DIMENSION(:), ALLOCATABLE ::  sums_divnew_l              !< subdomain sum (_l) of divergence after pressure
                                                                        !< solver call (new)
     REAL(wp), DIMENSION(:), ALLOCATABLE ::  sums_divold_l              !< subdomain sum (_l) of divergence before pressure
                                                                        !< solver call (old)
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  weight_pres                !< substep weighting factor for pressure solver
     REAL(wp), DIMENSION(:), ALLOCATABLE ::  weight_substep             !< weighting factor for substeps in timestepping
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  weight_pres                !< substep weighting factor for pressure solver
 
     REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums             !< global sum array for the various output quantities
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_salsa_ws_l  !< subdomain sum of vertical salsa flux w's' (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_ls_l        !< subdomain sum of large scale forcing and nudging tendencies
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_salsa_ws_l  !< subdomain sum of vertical salsa flux w's'
+                                                               !< (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_us2_ws_l    !< subdomain sum of horizontal momentum flux u'u'
+                                                               !< (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_vs2_ws_l    !< subdomain sum of horizontal momentum flux v'v'
+                                                               !< (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_ws2_ws_l    !< subdomain sum of vertical momentum flux w'w'
+                                                               !< (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsncs_ws_l  !< subdomain sum of vertical clouddrop-number concentration flux
+                                                               !< w'nc' (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsngs_ws_l  !< subdomain sum of vertical graupel-number concentration flux w'nc' (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsnis_ws_l  !< subdomain sum of vertical ice crystal concentration flux w'ni'
+                                                               !< (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsnrs_ws_l  !< subdomain sum of vertical raindrop-number concentration flux w'nr'
+                                                               !< (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsnss_ws_l  !< subdomain sum of vertical snow-number concentration flux w'ns' (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wssas_ws_l  !< subdomain sum of vertical salinity flux w'sa'
+                                                               !< (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsss_ws_l   !< subdomain sum of vertical passive scalar flux w's'
+                                                               !< (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wspts_ws_l  !< subdomain sum of vertical sensible heat flux w'pt'
+                                                               !< (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsqcs_ws_l  !< subdomain sum of vertical cloudwater flux w'qc'
+                                                               !< (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsqgs_ws_l  !< subdomain sum of vertical graupel flux w'qg' (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsqis_ws_l  !< subdomain sum of vertical ice crystal flux w'qi'
+                                                               !< (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsqrs_ws_l  !< subdomain sum of vertical rainwater flux w'qr'
+                                                               !< (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsqss_ws_l  !< subdomain sum of vertical snow flux w'qs' (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsqs_ws_l   !< subdomain sum of vertical latent heat flux w'q'
+                                                               !< (5th-order advection scheme only)
     REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsts_bc_l   !< subdomain sum of sensible heat flux in Bott-Chlond scheme
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsus_ws_l   !< subdomain sum of vertical momentum flux w'u'
+                                                               !< (5th-order advection scheme only)
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsvs_ws_l   !< subdomain sum of vertical momentum flux w'v'
+                                                               !< (5th-order advection scheme only)
     REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  ts_value         !< timeseries output array for the various output quantities
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsus_ws_l   !< subdomain sum of vertical momentum flux w'u' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsvs_ws_l   !< subdomain sum of vertical momentum flux w'v' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_us2_ws_l    !< subdomain sum of horizontal momentum flux u'u' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_vs2_ws_l    !< subdomain sum of horizontal momentum flux v'v' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_ws2_ws_l    !< subdomain sum of vertical momentum flux w'w' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsncs_ws_l  !< subdomain sum of vertical clouddrop-number concentration flux w'nc' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsngs_ws_l  !< subdomain sum of vertical graupel-number concentration flux w'nc' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsnis_ws_l  !< subdomain sum of vertical ice crystal concentration flux w'ni' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsnrs_ws_l  !< subdomain sum of vertical raindrop-number concentration flux w'nr' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsnss_ws_l  !< subdomain sum of vertical snow-number concentration flux w'ns' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wspts_ws_l  !< subdomain sum of vertical sensible heat flux w'pt' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsqs_ws_l   !< subdomain sum of vertical latent heat flux w'q' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsqcs_ws_l  !< subdomain sum of vertical cloudwater flux w'qc' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsqgs_ws_l  !< subdomain sum of vertical graupel flux w'qg' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsqis_ws_l  !< subdomain sum of vertical ice crystal flux w'qi' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsqrs_ws_l  !< subdomain sum of vertical rainwater flux w'qr' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsqss_ws_l  !< subdomain sum of vertical snow flux w'qs' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wssas_ws_l  !< subdomain sum of vertical salinity flux w'sa' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_wsss_ws_l   !< subdomain sum of vertical passive scalar flux w's' (5th-order advection scheme only)
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  sums_ls_l        !< subdomain sum of large scale forcing and nudging tendencies
 
     REAL(wp), DIMENSION(:,:), POINTER     ::  sums_wschs_ws_l  !< subdomain sum of vertical chemistry flux w'ch'
@@ -1560,9 +1750,9 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Definition of indices for transposed arrays.
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
  MODULE transpose_indices
 
Index: /palm/trunk/SOURCE/multi_agent_system_mod.f90
===================================================================
--- /palm/trunk/SOURCE/multi_agent_system_mod.f90	(revision 4752)
+++ /palm/trunk/SOURCE/multi_agent_system_mod.f90	(revision 4753)
@@ -1,20 +1,19 @@
 !> @file multi_agent_system_mod.f90
-!--------------------------------------------------------------------------------!
-! This file is part of PALM-4U.
-!
-! PALM-4U is free software: you can redistribute it and/or modify it under the 
-! terms of the GNU General Public License as published by the Free Software 
-! Foundation, either version 3 of the License, or (at your option) any later 
-! version.
-!
-! PALM-4U is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
+!--------------------------------------------------------------------------------------------------!
+! This file is part of the PALM model system.
+!
+! PALM is free software: you can redistribute it and/or modify it under the terms of the GNU General
+! Public License as published by the Free Software Foundation, either version 3 of the License, or
+! (at your option) any later version.
+!
+! PALM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
+! implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+! Public License for more details.
+!
+! You should have received a copy of the GNU General Public License along with PALM. If not, see
+! <http://www.gnu.org/licenses/>.
 !
 ! Copyright 2016-2020 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 !
 ! Current revisions:
@@ -25,45 +24,47 @@
 ! -----------------
 ! $Id$
+! file re-formatted to follow the PALM coding standard
+!
+! 4623 2020-07-24 08:42:02Z raasch
 ! some switches calculated explicitly to avoid compiler warnings
-! 
+!
 ! 4481 2020-03-31 18:55:54Z maronga
 ! bugfix: cpp-directives for serial mode added
-! 
+!
 ! 4346 2019-12-18 11:55:56Z motisi
-! Removed wall_flags_static_0 from USE statements as it's not used within
-! the module
-! 
+! Removed wall_flags_static_0 from USE statements as it's not used within the module
+!
 ! 4329 2019-12-10 15:46:36Z motisi
 ! Renamed wall_flags_0 to wall_flags_static_0
-! 
+!
 ! 4307 2019-11-26 14:12:36Z maronga
 ! Activated output of iPT
-! 
+!
 ! 4182 2019-08-22 15:20:23Z scharf
 ! Corrected "Former revisions" section
-! 
+!
 ! 4168 2019-08-16 13:50:17Z suehring
 ! Replace function get_topography_top_index by topo_top_ind
-! 
+!
 ! 3987 2019-05-22 09:52:13Z kanani
 ! Introduce alternative switch for debug output during timestepping
-! 
+!
 ! 3885 2019-04-11 11:29:34Z kanani
-! Changes related to global restructuring of location messages and introduction 
-! of additional debug messages
-! 
+! Changes related to global restructuring of location messages and introduction  of additional debug
+! messages
+!
 ! 3876 2019-04-08 18:41:49Z knoop
-! replaced nspec by nvar: only variable species should bconsidered, fixed species are not relevant 
-! 
+! replaced nspec by nvar: only variable species should bconsidered, fixed species are not relevant
+!
 ! 3766 2019-02-26 16:23:41Z raasch
 ! save attribute added to local targets to avoid outlive pointer target warning
-! 
+!
 ! 3665 2019-01-10 08:28:24Z raasch
 ! unused variables removed
-! 
+!
 ! 3159 2018-07-20 11:20:01Z sward
 ! Initial revision
 !
-! 
+!
 !
 ! Authors:
@@ -74,33 +75,31 @@
 ! Description:
 ! ------------
-!> Multi Agent System for the simulation of pedestrian movement in urban
-!> environments
-!------------------------------------------------------------------------------!
+!> Multi Agent System for the simulation of pedestrian movement in urban environments
+!--------------------------------------------------------------------------------------------------!
  MODULE multi_agent_system_mod
 
     USE, INTRINSIC ::  ISO_C_BINDING
 
-    USE basic_constants_and_equations_mod,                                     &
+    USE basic_constants_and_equations_mod,                                                         &
         ONLY:  pi
 
-    USE control_parameters,                                                    &
-        ONLY:  biometeorology,                                                 &
-               debug_output_timestep,                                          &
-               dt_3d,                                                          &
-               dt_write_agent_data,                                            &
-               message_string,                                                 &
+    USE control_parameters,                                                                        &
+        ONLY:  biometeorology,                                                                     &
+               debug_output_timestep,                                                              &
+               dt_3d,                                                                              &
+               dt_write_agent_data,                                                                &
+               message_string,                                                                     &
                time_since_reference_point
 
-    USE cpulog,                                                                &
+    USE cpulog,                                                                                    &
         ONLY:  cpu_log, log_point, log_point_s
 
-    USE grid_variables,                                                        &
+    USE grid_variables,                                                                            &
         ONLY:  ddx, ddy, dx, dy
 
-    USE indices,                                                               &
-        ONLY:  nx, nxl, nxlg, nxr, nxrg, ny, nyn, nyng, nys, nysg, nzb,        &
-               topo_top_ind
-               
-    USE random_function_mod,                                                   &
+    USE indices,                                                                                   &
+        ONLY:  nx, nxl, nxlg, nxr, nxrg, ny, nyn, nyng, nys, nysg, nzb, topo_top_ind
+
+    USE random_function_mod,                                                                       &
         ONLY:  random_function
 
@@ -108,8 +107,14 @@
 
     USE pegrid
+
+    INTEGER(iwp), PARAMETER ::  max_number_of_agent_groups = 100  !< maximum allowed number of agent groups
+    INTEGER(iwp), PARAMETER ::  nr_2_direction_move = 10000       !< parameter for agent exchange
+    INTEGER(iwp), PARAMETER ::  phase_init    = 1                 !< phase parameter
+    INTEGER(iwp), PARAMETER ::  phase_release = 2                 !< phase parameter
 
     CHARACTER(LEN=15) ::  bc_mas_lr = 'absorb'  !< left/right boundary condition
     CHARACTER(LEN=15) ::  bc_mas_ns = 'absorb'  !< north/south boundary condition
 
+    INTEGER(iwp) ::  agt_path_size = 15                !< size of agent path array
     INTEGER(iwp) ::  deleted_agents = 0                !< number of deleted agents per time step
     INTEGER(iwp) ::  dim_size_agtnum_manual = 9999999  !< namelist parameter (see documentation)
@@ -133,13 +138,6 @@
     INTEGER(iwp) ::  number_of_agent_groups = 1        !< namelist parameter (see documentation)
     INTEGER(iwp) ::  sort_count_mas = 0                !< counter for sorting agents
-    INTEGER(iwp) ::  agt_path_size = 15                !< size of agent path array
     INTEGER(iwp) ::  step_dealloc_mas = 100            !< namelist parameter (see documentation)
     INTEGER(iwp) ::  total_number_of_agents            !< total number of agents in the whole model domain
-
-    INTEGER(iwp), PARAMETER ::  NR_2_direction_move = 10000 !< parameter for agent exchange
-    INTEGER(iwp), PARAMETER ::  PHASE_INIT    = 1           !< phase parameter
-    INTEGER(iwp), PARAMETER ::  PHASE_RELEASE = 2           !< phase parameter
-
-    INTEGER(iwp), PARAMETER ::  max_number_of_agent_groups = 100 !< maximum allowed number of agent groups
 
     INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  agt_count         !< 3d array of number of agents of every grid box
@@ -147,5 +145,6 @@
     INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  top_top_s         !< k-index of first s-gridpoint above topography
     INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  top_top_w         !< k-index of first v-gridpoint above topography
-    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  obstacle_flags    !< flags to identify corners and edges of topography that cannot be crossed by agents
+    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  obstacle_flags    !< flags to identify corners and edges of topography that cannot
+                                                                    !< be crossed by agents
 
     LOGICAL ::  deallocate_memory_mas = .TRUE.          !< namelist parameter (see documentation)
@@ -245,13 +244,14 @@
     END TYPE agent_type
 
+    TYPE(agent_type)                        ::  zero_agent           !< zero agent to avoid weird thing
+
     TYPE(agent_type), DIMENSION(:), POINTER ::  agents               !< Agent array for this grid cell
-    TYPE(agent_type)                        ::  zero_agent           !< zero agent to avoid weird thing
 #if defined( __parallel )
-    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  move_also_north  !< for agent exchange between PEs
-    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  move_also_south  !< for agent exchange between PEs
     TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  agt_gh_l         !< ghost layer left of pe domain
     TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  agt_gh_n         !< ghost layer north of pe domain
     TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  agt_gh_r         !< ghost layer right of pe domain
     TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  agt_gh_s         !< ghost layer south of pe domain
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  move_also_north  !< for agent exchange between PEs
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  move_also_south  !< for agent exchange between PEs
 #endif
 !
@@ -338,16 +338,16 @@
 
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Multi Agent System:
 !> executes a number of agents sub-timesteps until the model timestep is reached.
-!> The agent timestep is usually smaller than the model timestep
-!------------------------------------------------------------------------------!
+!> The agent timestep is usually smaller than the model timestep.
+!--------------------------------------------------------------------------------------------------!
  SUBROUTINE multi_agent_system
 
-    USE biometeorology_mod,                                                    &
-        ONLY:  bio_calc_ipt,                                                   &
-               bio_calculate_mrt_grid,                                         &
+    USE biometeorology_mod,                                                                        &
+        ONLY:  bio_calc_ipt,                                                                       &
+               bio_calculate_mrt_grid,                                                             &
                bio_get_thermal_index_input_ij
 
@@ -362,11 +362,12 @@
     INTEGER(iwp)       ::  js                 !< counter
     INTEGER(iwp), SAVE ::  mas_count = 0      !< counts the mas-calls
-    INTEGER(iwp)                :: a     !< agent iterator
-    !-- local meteorological conditions
-    REAL(wp)                    :: tmrt  !< mean radiant temperature        (degree_C)
-    REAL(wp)                    :: ta    !< air temperature                 (degree_C)
-    REAL(wp)                    :: vp    !< vapour pressure                 (hPa)
-    REAL(wp)                    :: v     !< wind speed    (local level)     (m/s)
-    REAL(wp)                    :: pair  !< air pressure                    (hPa)
+    INTEGER(iwp)       ::  a                  !< agent iterator
+!
+!-- local meteorological conditions
+    REAL(wp) ::  pair  !< air pressure                    (hPa)
+    REAL(wp) ::  ta    !< air temperature                 (degree_C)
+    REAL(wp) ::  tmrt  !< mean radiant temperature        (degree_C)
+    REAL(wp) ::  v     !< wind speed    (local level)     (m/s)
+    REAL(wp) ::  vp    !< vapour pressure                 (hPa)
 
 
@@ -379,6 +380,6 @@
     CALL cpu_log( log_point(9), 'mas', 'start' )
 !
-!-- Initialize variables for the next (sub-) timestep, i.e., for marking
-!-- those agents to be deleted after the timestep
+!-- Initialize variables for the next (sub-) timestep, i.e., for marking those agents to be deleted
+!-- after the timestep
     deleted_agents = 0
     agent_substep_time = 0.0_wp
@@ -387,8 +388,7 @@
     IF ( time_arel >= dt_arel  .AND.  end_time_arel > time_since_reference_point )  THEN
 
-       CALL mas_create_agent(PHASE_RELEASE)
-!
-!--    The MOD function allows for changes in the output interval with
-!--    restart runs.
+       CALL mas_create_agent( phase_release )
+!
+!--    The MOD function allows for changes in the output interval with restart runs.
        time_arel = MOD( time_arel, MAX( dt_arel, dt_3d ) )
 
@@ -402,7 +402,7 @@
 !
 !-- Timestep loop for agent transport.
-!-- This loop has to be repeated until the transport time of every agent
-!-- (within the total domain!) has reached the LES timestep (dt_3d).
-!-- Timestep scheme is Euler-forward
+!-- This loop has to be repeated until the transport time of every agent (within the total domain!)
+!-- has reached the LES timestep (dt_3d).
+!-- Timestep scheme is Euler-forward.
     DO
 !
@@ -415,5 +415,5 @@
           CALL mas_data_output_agents ( first_call )
 #else
-          WRITE( message_string, * ) 'NetCDF is needed for agent output. ',    &
+          WRITE( message_string, * ) 'NetCDF is needed for agent output. ',                        &
                                      'Set __netcdf in compiler options'
           CALL message( 'multi_agent_system', 'PA0071', 1, 2, 0, 6, 0 )
@@ -423,6 +423,6 @@
        ENDIF
 !
-!--    Flag is true by default, will be set to false if an agent has not yet
-!--    reached the model timestep
+!--    Flag is true by default, will be set to false if an agent has not yet reached the model
+!--    timestep.
        grid_agents(:,:)%time_loop_done = .TRUE.
 
@@ -466,14 +466,13 @@
              agents(1:number_of_agents)%agent_mask = .TRUE.
 !
-!--          Initialize the variable storing the total time that an agent
-!--          has advanced within the timestep procedure
+!--          Initialize the variable storing the total time that an agent has advanced within the
+!--          timestep procedure.
              IF ( first_loop_stride )  THEN
                 agents(1:number_of_agents)%dt_sum = 0.0_wp
              ENDIF
 !
-!--          Initialize the switch used for the loop exit condition checked
-!--          at the end of this loop. If at least one agent has failed to
-!--          reach the LES timestep, this switch will be set false in 
-!--          mas_transport.
+!--          Initialize the switch used for the loop exit condition checked at the end of this loop.
+!--          If at least one agent has failed to reach the LES timestep, this switch will be set
+!--          false in mas_transport.
              dt_3d_reached_l_mas = .TRUE.
 !
@@ -481,14 +480,13 @@
              CALL mas_timestep
 !
-!--          Delete agents that have been simulated longer than allowed
+!--          Delete agents that have been simulated longer than allowed.
              CALL mas_boundary_conds( 'max_sim_time' )
 !
-!--          Delete agents that have reached target area
+!--          Delete agents that have reached target area.
              CALL mas_boundary_conds( 'target_area' )
 !
-!---         If not all agents of the actual grid cell have reached the 
-!--          LES timestep, this cell has to to another loop iteration. Due to
-!--          the fact that agents can move into neighboring grid cell, 
-!--          these neighbor cells also have to perform another loop iteration
+!--          If not all agents of the actual grid cell have reached the LES timestep, this cell has
+!--          to do another loop iteration. Due to the fact that agents can move into neighboring
+!--          grid cell, these neighbor cells also have to perform another loop iteration.
              IF ( .NOT. dt_3d_reached_l_mas )  THEN
                 js = MAX(nys,j-1)
@@ -504,11 +502,11 @@
 
 !
-!--    Find out, if all agents on every PE have completed the LES timestep
-!--    and set the switch corespondingly
+!--    Find out, if all agents on each PE have completed the LES timestep and set the switch
+!--    corespondingly.
        dt_3d_reached_l_mas = ALL(grid_agents(:,:)%time_loop_done)
 #if defined( __parallel )
        IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-       CALL MPI_ALLREDUCE( dt_3d_reached_l_mas, dt_3d_reached_mas, 1, MPI_LOGICAL, &
-                           MPI_LAND, comm2d, ierr )
+       CALL MPI_ALLREDUCE( dt_3d_reached_l_mas, dt_3d_reached_mas, 1, MPI_LOGICAL,  MPI_LAND,      &
+                           comm2d, ierr )
 #else
        dt_3d_reached_mas = dt_3d_reached_l_mas
@@ -527,11 +525,10 @@
        CALL mas_eh_exchange_horiz
 !
-!--    Pack agents (eliminate those marked for deletion),
-!--    determine new number of agents
+!--    Pack agents (eliminate those marked for deletion), determine new number of agents
        CALL mas_ps_sort_in_subboxes
        CALL cpu_log( log_point_s(18), 'mas_move_exch_sort', 'stop' )
 !
-!--    Initialize variables for the next (sub-) timestep, i.e., for marking
-!--    those agents to be deleted after the timestep
+!--    Initialize variables for the next (sub-) timestep, i.e., for marking those agents to be
+!--    deleted after the timestep
        deleted_agents = 0
 
@@ -556,6 +553,5 @@
 !
 !--             Determine local meteorological conditions
-                CALL bio_get_thermal_index_input_ij ( .FALSE., i, j, ta, vp,  &
-                                                      v, pair, tmrt )
+                CALL bio_get_thermal_index_input_ij ( .FALSE., i, j, ta, vp, v, pair, tmrt )
 
                 DO  a = 1, number_of_agents
@@ -563,15 +559,15 @@
 !--                Calculate instationary thermal indices based on local tmrt
 
-                   CALL bio_calc_ipt ( ta, vp, v, pair, tmrt,                 &
-                                       agents(a)%dt_sum,                      &
-                                       agents(a)%energy_storage,              &
-                                       agents(a)%clothing_temp,               &
-                                       agents(a)%clo,                         &
-                                       agents(a)%actlev,                      &
-                                       agents(a)%age_years,                   &
-                                       agents(a)%weight,                      &
-                                       agents(a)%height,                      &
-                                       agents(a)%work,                        &
-                                       agents(a)%sex,                         &
+                   CALL bio_calc_ipt ( ta, vp, v, pair, tmrt,                                      &
+                                       agents(a)%dt_sum,                                           &
+                                       agents(a)%energy_storage,                                   &
+                                       agents(a)%clothing_temp,                                    &
+                                       agents(a)%clo,                                              &
+                                       agents(a)%actlev,                                           &
+                                       agents(a)%age_years,                                        &
+                                       agents(a)%weight,                                           &
+                                       agents(a)%height,                                           &
+                                       agents(a)%work,                                             &
+                                       agents(a)%sex,                                              &
                                        agents(a)%ipt )
                 END DO
@@ -602,96 +598,93 @@
  END SUBROUTINE multi_agent_system
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> Calculation of the direction vector from each agent to its current
-!> intermittent target
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_agent_direction
-
-       IMPLICIT NONE
-
-       LOGICAL ::  path_flag !< true if new path must be calculated
-
-       INTEGER(iwp) ::  n  !< loop variable over all agents in a grid box
-       INTEGER(iwp) ::  pc !< agent path counter
-
-       REAL(wp) ::  abs_dir         !< length of direction vector (for normalization)
-!       REAL(wp) ::  d_curr_target   !< rounding influence expressed as x speed component
-!       REAL(wp) ::  d_prev_target   !< rounding influence expressed as x speed component
-       REAL(wp) ::  dir_x           !< direction of agent (x)
-       REAL(wp) ::  dir_y           !< direction of agent (y)
-!       REAL(wp) ::  dist_round = 3. !< distance at which agents start rounding a corner
-       REAL(wp) ::  dtit            !< distance to intermittent target
-!       REAL(wp) ::  round_fac  = 0.2 !< factor for rounding influence
-!       REAL(wp) ::  speed_round_x   !< rounding influence expressed as x speed component
-!       REAL(wp) ::  speed_round_y   !< rounding influence expressed as x speed component
-
-!
-!--    loop over all agents in the current grid box
-       DO n = 1, number_of_agents
-          path_flag = .FALSE.
+!> Calculation of the direction vector from each agent to its current intermittent target.
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_agent_direction
+
+    IMPLICIT NONE
+
+    LOGICAL ::  path_flag  !< true if new path must be calculated
+
+    INTEGER(iwp) ::  n   !< loop variable over all agents in a grid box
+    INTEGER(iwp) ::  pc  !< agent path counter
+
+    REAL(wp) ::  abs_dir         !< length of direction vector (for normalization)
+!    REAL(wp) ::  d_curr_target   !< rounding influence expressed as x speed component
+!    REAL(wp) ::  d_prev_target   !< rounding influence expressed as x speed component
+    REAL(wp) ::  dir_x           !< direction of agent (x)
+    REAL(wp) ::  dir_y           !< direction of agent (y)
+!    REAL(wp) ::  dist_round = 3. !< distance at which agents start rounding a corner
+    REAL(wp) ::  dtit            !< distance to intermittent target
+!    REAL(wp) ::  round_fac  = 0.2 !< factor for rounding influence
+!    REAL(wp) ::  speed_round_x   !< rounding influence expressed as x speed component
+!    REAL(wp) ::  speed_round_y   !< rounding influence expressed as x speed component
+
+!
+!-- Loop over all agents in the current grid box
+    DO  n = 1, number_of_agents
+       path_flag = .FALSE.
+       pc = agents(n)%path_counter
+!
+!--    If no path was calculated for agent yet, do it.
+       IF ( pc >= 999 )  THEN
+          CALL mas_nav_find_path(n)
           pc = agents(n)%path_counter
 !
-!--       If no path was calculated for agent yet, do it
-          IF ( pc >= 999 ) THEN
+!--    Check if new path must be calculated and if so, do it.
+       ELSE
+!
+!--       Case one: Agent has come close enough to intermittent target.
+!--                 -> chose new int target and calculate rest of path if no
+!--                    new intermittent targets are left
+          dtit = SQRT(   ( agents(n)%x - agents(n)%path_x(pc) )**2                                 &
+                       + ( agents(n)%y - agents(n)%path_y(pc) )**2 )
+          IF ( dtit < dist_to_int_target )  THEN
+             agents(n)%path_counter = agents(n)%path_counter + 1
+             pc = agents(n)%path_counter
+!
+!--          Path counter out of scope (each agent can store a maximum of 15 intermittent targets on
+!--          the way to its final target); new path must be calculated.
+             IF ( pc >= SIZE( agents(n)%path_x) )  THEN
+                path_flag = .TRUE.
+             ENDIF
+!
+!--          Case two: Agent too far from path
+!--                    -> set flag for new path to be calculated
+          ELSEIF ( dist_point_to_edge(agents(n)%path_x(pc-1),                                      &
+                                      agents(n)%path_y(pc-1),                                      &
+                                      agents(n)%path_x(pc),                                        &
+                                      agents(n)%path_y(pc),                                        &
+                                      agents(n)%x, agents(n)%y)                                    &
+                   > max_dist_from_path )                                                          &
+          THEN
+             path_flag = .TRUE.
+          ENDIF
+!
+!--       If one of the above two cases was true, calculate new path and reset 0th path point.
+!--       This point (the last target the agent had) is needed for the agent's rounding of corners
+!--       and the calculation of its deviation from its current path.
+          IF ( path_flag )  THEN
              CALL mas_nav_find_path(n)
              pc = agents(n)%path_counter
-!
-!--       Check if new path must be calculated and if so, do it
-          ELSE
-!
-!--          Case one: Agent has come close enough to intermittent target.
-!--                    -> chose new int target and calculate rest of path if no
-!--                       new intermittent targets are left
-             dtit = SQRT((agents(n)%x - agents(n)%path_x(pc))**2               &
-                       + (agents(n)%y - agents(n)%path_y(pc))**2)
-             IF ( dtit < dist_to_int_target ) THEN
-                agents(n)%path_counter = agents(n)%path_counter + 1
-                pc = agents(n)%path_counter
-!
-!--             Path counter out of scope (each agent can store a maximum of 15
-!--             intermittent targets on the way to her final target); new path
-!--             must be calculated
-                IF ( pc >= SIZE(agents(n)%path_x) ) THEN
-                   path_flag = .TRUE.
-                ENDIF
-!
-!--          Case two: Agent too far from path 
-!--                    -> set flag for new path to be calculated
-             ELSEIF ( dist_point_to_edge(agents(n)%path_x(pc-1),               &
-                                         agents(n)%path_y(pc-1),               &
-                                         agents(n)%path_x(pc),                 &
-                                         agents(n)%path_y(pc),                 &
-                                         agents(n)%x, agents(n)%y)             &
-                      > max_dist_from_path )                                   &
-             THEN
-                path_flag = .TRUE.
-             ENDIF
-!
-!--          If either of the above two cases was true, calculate new path and 
-!--          reset 0th path point. This point (the last target the agent had)
-!--          is needed for the agents rounding of corners and the calculation
-!--          of her deviation from her current path
-             IF ( path_flag ) THEN
-                CALL mas_nav_find_path(n)
-                pc = agents(n)%path_counter
-             ENDIF
           ENDIF
-!
-!--       Normalize direction vector
-          abs_dir             = 1.0d-12
-          dir_x               = agents(n)%path_x(pc) - agents(n)%x
-          dir_y               = agents(n)%path_y(pc) - agents(n)%y
-          abs_dir             = SQRT(dir_x**2 + dir_y**2)+1.0d-12
-!--         needed later for corner rounding
-!           dir_x               = dir_x/abs_dir
-!           dir_y               = dir_y/abs_dir
-!           dir_x               = dir_x + speed_round_x
-!           dir_y               = dir_y + speed_round_y
-!           abs_dir             = SQRT(dir_x**2 + dir_y**2)+1.0d-12
-          agents(n)%speed_e_x = dir_x/abs_dir
-          agents(n)%speed_e_y = dir_y/abs_dir
-       ENDDO
+       ENDIF
+!
+!--    Normalize direction vector
+       abs_dir             = 1.0d-12
+       dir_x               = agents(n)%path_x(pc) - agents(n)%x
+       dir_y               = agents(n)%path_y(pc) - agents(n)%y
+       abs_dir             = SQRT( dir_x**2 + dir_y**2 )+1.0d-12
+!--      needed later for corner rounding
+!        dir_x               = dir_x/abs_dir
+!        dir_y               = dir_y/abs_dir
+!        dir_x               = dir_x + speed_round_x
+!        dir_y               = dir_y + speed_round_y
+!        abs_dir             = SQRT(dir_x**2 + dir_y**2)+1.0d-12
+       agents(n)%speed_e_x = dir_x/abs_dir
+       agents(n)%speed_e_y = dir_y/abs_dir
+    ENDDO
 
 !
@@ -702,5 +695,5 @@
 !           speed_round_x = 0.
 !           speed_round_y = 0.
-!           
+!
 !           d_curr_target = SQRT( (agents(n)%path_x(pc) - agents(n)%x)**2 +      &
 !                                 (agents(n)%path_y(pc) - agents(n)%y)**2 )
@@ -722,7 +715,7 @@
 !                              SIN( pi/dist_round*d_curr_target )
 !           ENDIF
-! 
-!           IF ( d_prev_target < dist_round ) THEN
-!              IF ( agents(n)%path_x(pc) /= agents(n)%path_x(pc+1) ) THEN
+!
+!           IF ( d_prev_target < dist_round )  THEN
+!              IF ( agents(n)%path_x(pc) /= agents(n)%path_x(pc+1) )  THEN
 !                 speed_round_x = speed_round_x +                                   &
 !                                 (agents(n)%path_x(pc) - agents(n)%path_x(pc+1)) / &
@@ -731,6 +724,6 @@
 !                                 SIN( pi/dist_round*d_prev_target )
 !              ENDIF
-!              
-!              IF ( agents(n)%path_y(pc) /= agents(n)%path_y(pc+1) ) THEN
+!
+!              IF ( agents(n)%path_y(pc) /= agents(n)%path_y(pc+1) )  THEN
 !                 speed_round_y = speed_round_y +                                   &
 !                              (agents(n)%path_y(pc) - agents(n)%path_y(pc+1)) / &
@@ -739,252 +732,260 @@
 !                              SIN( pi/dist_round*d_prev_target )
 !              ENDIF
-             
+
 !           ENDIF
 
 
-    END SUBROUTINE mas_agent_direction
-
-!------------------------------------------------------------------------------!
+ END SUBROUTINE mas_agent_direction
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Boundary conditions for maximum time, target reached and out of domain
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_boundary_conds( location )
-
-       IMPLICIT NONE
-
-       CHARACTER (LEN=*) ::  location !< Identifier
-
-       INTEGER(iwp) ::  n   !< agent number
-       INTEGER(iwp) ::  grp !< agent group
-
-       REAL(wp) ::  dist_to_target !< distance to target
-
-       IF ( location == 'max_sim_time' )  THEN
-
-!
-!--       Delete agents that have been simulated longer than allowed
-          DO  n = 1, number_of_agents
-
-             IF ( agents(n)%age > agent_maximum_age  .AND.                     &
-                  agents(n)%agent_mask )                                       &
-             THEN
-                agents(n)%agent_mask  = .FALSE.
-                deleted_agents = deleted_agents + 1
-             ENDIF
-
-          ENDDO
-       ENDIF
-
-       IF ( location == 'target_area' )  THEN
-
-!
-!--       Delete agents that entered target region
-          DO  n = 1, number_of_agents
-             grp = agents(n)%group
-             dist_to_target = SQRT((agents(n)%x-at_x(grp))**2                  &
-                                 + (agents(n)%y-at_y(grp))**2)
-             IF ( dist_to_target < dist_target_reached ) THEN
-                agents(n)%agent_mask  = .FALSE.
-                deleted_agents = deleted_agents + 1
-             ENDIF
-
-          ENDDO
-       ENDIF
-
-    END SUBROUTINE mas_boundary_conds
-
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_boundary_conds( location )
+
+    IMPLICIT NONE
+
+    CHARACTER (LEN=*) ::  location  !< Identifier
+
+    INTEGER(iwp) ::  grp  !< agent group
+    INTEGER(iwp) ::  n    !< agent number
+
+    REAL(wp) ::  dist_to_target !< distance to target
+
+    IF ( location == 'max_sim_time' )  THEN
+
+!
+!--    Delete agents that have been simulated longer than allowed
+       DO  n = 1, number_of_agents
+
+          IF ( agents(n)%age > agent_maximum_age  .AND.  agents(n)%agent_mask )  THEN
+             agents(n)%agent_mask  = .FALSE.
+             deleted_agents = deleted_agents + 1
+          ENDIF
+
+       ENDDO
+    ENDIF
+
+    IF ( location == 'target_area' )  THEN
+
+!
+!--    Delete agents that entered target region
+       DO  n = 1, number_of_agents
+          grp = agents(n)%group
+          dist_to_target = SQRT( ( agents(n)%x - at_x(grp) )**2 + ( agents(n)%y - at_y(grp) )**2 )
+          IF ( dist_to_target < dist_target_reached )  THEN
+             agents(n)%agent_mask  = .FALSE.
+             deleted_agents = deleted_agents + 1
+          ENDIF
+
+       ENDDO
+    ENDIF
+
+ END SUBROUTINE mas_boundary_conds
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Release new agents at their respective sources
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_create_agent (phase)
-
-       IMPLICIT  NONE
-
-       INTEGER(iwp) ::  alloc_size  !< relative increase of allocated memory for agents
-       INTEGER(iwp) ::  i           !< loop variable ( agent groups )
-       INTEGER(iwp) ::  ip          !< index variable along x
-       INTEGER(iwp) ::  jp          !< index variable along y
-       INTEGER(iwp) ::  loop_stride !< loop variable for initialization
-       INTEGER(iwp) ::  n           !< loop variable ( number of agents )
-       INTEGER(iwp) ::  new_size    !< new size of allocated memory for agents
-       INTEGER(iwp) ::  rn_side     !< index of agent path
-
-       INTEGER(iwp), INTENT(IN) ::  phase       !< mode of inititialization
-
-       INTEGER(iwp), DIMENSION(nysg:nyng,nxlg:nxrg) ::  local_count !< start address of new agent
-       INTEGER(iwp), DIMENSION(nysg:nyng,nxlg:nxrg) ::  local_start !< start address of new agent
-
-       LOGICAL ::  first_stride !< flag for initialization
-
-       REAL(wp) ::  pos_x       !< increment for agent position in x
-       REAL(wp) ::  pos_y       !< increment for agent position in y
-       REAL(wp) ::  rand_contr  !< dummy argument for random position
-       REAL(wp) ::  rn_side_dum !< index of agent path
-
-       TYPE(agent_type),TARGET ::  tmp_agent !< temporary agent used for initialization
-
-!
-!--    Calculate agent positions and store agent attributes, if
-!--    agent is situated on this PE
-       DO  loop_stride = 1, 2
-          first_stride = (loop_stride == 1)
-          IF ( first_stride )   THEN
-             local_count = 0           ! count number of agents
-          ELSE
-             local_count = agt_count   ! Start address of new agents
-          ENDIF
-
-          DO  i = 1, number_of_agent_groups
-
-             pos_y = ass(i)
-
-             DO WHILE ( pos_y <= asn(i) )
-
-                IF ( pos_y >= nys * dy  .AND.                                  &
-                           pos_y <  ( nyn + 1 ) * dy  )                        &
-                THEN
-
-                   pos_x = asl(i)
-
-            xloop: DO WHILE ( pos_x <= asr(i) )
-
-                      IF ( pos_x >= nxl * dx  .AND.                            &
-                                 pos_x <  ( nxr + 1) * dx )                    &
-                      THEN
-
-                         tmp_agent%agent_mask = .TRUE.
-                         tmp_agent%group         = i
-                         tmp_agent%id            = 0_idp
-                         tmp_agent%block_nr      = -1
-                         tmp_agent%path_counter  = 999 !SIZE(tmp_agent%path_x)
-                         tmp_agent%age           = 0.0_wp
-                         tmp_agent%age_m         = 0.0_wp
-                         tmp_agent%dt_sum        = 0.0_wp
-                         tmp_agent%clo           = -999.0_wp
-                         tmp_agent%energy_storage= 0.0_wp
-                         tmp_agent%ipt           = 99999.0_wp
-                         tmp_agent%clothing_temp = -999._wp      !< energy stored by agent (W)
-                         tmp_agent%actlev        = 134.6862_wp   !< metabolic + work energy of the person
-                         tmp_agent%age_years     = 35._wp        !< physical age of the person
-                         tmp_agent%weight        = 75._wp        !< total weight of the person (kg)
-                         tmp_agent%height        = 1.75_wp       !< height of the person (m)
-                         tmp_agent%work          = 134.6862_wp   !< workload of the agent (W)
-                         tmp_agent%sex           = 1             !< agents gender: 1 = male, 2 = female
-                         tmp_agent%force_x       = 0.0_wp
-                         tmp_agent%force_y       = 0.0_wp
-                         tmp_agent%origin_x      = pos_x
-                         tmp_agent%origin_y      = pos_y
-                         tmp_agent%speed_abs     = 0.0_wp
-                         tmp_agent%speed_e_x     = 0.0_wp
-                         tmp_agent%speed_e_y     = 0.0_wp
-                         tmp_agent%speed_des     = random_normal(desired_speed,&
-                                                                 des_sp_sig)
-                         tmp_agent%speed_x       = 0.0_wp
-                         tmp_agent%speed_y       = 0.0_wp
-                         tmp_agent%x             = pos_x
-                         tmp_agent%y             = pos_y
-                         tmp_agent%path_x        = -1.0_wp
-                         tmp_agent%path_y        = -1.0_wp
-                         tmp_agent%t_x           = - pi
-                         tmp_agent%t_y           = - pi
-!
-!--                      Determine the grid indices of the agent position
-                         ip = tmp_agent%x * ddx
-                         jp = tmp_agent%y * ddy
-!
-!--                      Give each agent its target
-                         IF ( a_rand_target(i) ) THEN
-!
-!--                         Agent shall receive random target just outside 
-!--                         simulated area
-                            rn_side_dum = random_function(iran_agent)
-                            rn_side     = FLOOR(4.*rn_side_dum)
-                            IF ( rn_side < 2 ) THEN
-                               IF ( rn_side == 0 ) THEN
-                                  tmp_agent%t_y = -2*dy
-                               ELSE
-                                  tmp_agent%t_y = (ny+3)*dy
-                               ENDIF
-                               tmp_agent%t_x = random_function(iran_agent) *   &
-                                               (nx+1)*dx
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_create_agent ( phase )
+
+    IMPLICIT  NONE
+
+    INTEGER(iwp) ::  alloc_size  !< relative increase of allocated memory for agents
+    INTEGER(iwp) ::  i           !< loop variable ( agent groups )
+    INTEGER(iwp) ::  ip          !< index variable along x
+    INTEGER(iwp) ::  jp          !< index variable along y
+    INTEGER(iwp) ::  loop_stride !< loop variable for initialization
+    INTEGER(iwp) ::  n           !< loop variable ( number of agents )
+    INTEGER(iwp) ::  new_size    !< new size of allocated memory for agents
+    INTEGER(iwp) ::  rn_side     !< index of agent path
+
+    INTEGER(iwp), INTENT(IN) ::  phase       !< mode of inititialization
+
+    INTEGER(iwp), DIMENSION(nysg:nyng,nxlg:nxrg) ::  local_count !< start address of new agent
+    INTEGER(iwp), DIMENSION(nysg:nyng,nxlg:nxrg) ::  local_start !< start address of new agent
+
+    LOGICAL ::  first_stride !< flag for initialization
+
+    REAL(wp) ::  pos_x       !< increment for agent position in x
+    REAL(wp) ::  pos_y       !< increment for agent position in y
+    REAL(wp) ::  rand_contr  !< dummy argument for random position
+    REAL(wp) ::  rn_side_dum !< index of agent path
+
+    TYPE(agent_type),TARGET ::  tmp_agent !< temporary agent used for initialization
+
+!
+!-- Calculate agent positions and store agent attributes, if agent is situated on this PE.
+    DO  loop_stride = 1, 2
+       first_stride = (loop_stride == 1)
+       IF ( first_stride )  THEN
+          local_count = 0           ! count number of agents
+       ELSE
+          local_count = agt_count   ! Start address of new agents
+       ENDIF
+
+       DO  i = 1, number_of_agent_groups
+
+          pos_y = ass(i)
+
+          DO WHILE ( pos_y <= asn(i) )
+
+             IF ( pos_y >= nys * dy  .AND.  pos_y <  ( nyn + 1 ) * dy  )  THEN
+
+                pos_x = asl(i)
+
+         xloop: DO WHILE ( pos_x <= asr(i) )
+
+                   IF ( pos_x >= nxl * dx  .AND.  pos_x <  ( nxr + 1) * dx )  THEN
+
+                      tmp_agent%agent_mask = .TRUE.
+                      tmp_agent%group         = i
+                      tmp_agent%id            = 0_idp
+                      tmp_agent%block_nr      = -1
+                      tmp_agent%path_counter  = 999 !SIZE(tmp_agent%path_x)
+                      tmp_agent%age           = 0.0_wp
+                      tmp_agent%age_m         = 0.0_wp
+                      tmp_agent%dt_sum        = 0.0_wp
+                      tmp_agent%clo           = -999.0_wp
+                      tmp_agent%energy_storage= 0.0_wp
+                      tmp_agent%ipt           = 99999.0_wp
+                      tmp_agent%clothing_temp = -999._wp      !< energy stored by agent (W)
+                      tmp_agent%actlev        = 134.6862_wp   !< metabolic + work energy of the person
+                      tmp_agent%age_years     = 35._wp        !< physical age of the person
+                      tmp_agent%weight        = 75._wp        !< total weight of the person (kg)
+                      tmp_agent%height        = 1.75_wp       !< height of the person (m)
+                      tmp_agent%work          = 134.6862_wp   !< workload of the agent (W)
+                      tmp_agent%sex           = 1             !< agents gender: 1 = male, 2 = female
+                      tmp_agent%force_x       = 0.0_wp
+                      tmp_agent%force_y       = 0.0_wp
+                      tmp_agent%origin_x      = pos_x
+                      tmp_agent%origin_y      = pos_y
+                      tmp_agent%speed_abs     = 0.0_wp
+                      tmp_agent%speed_e_x     = 0.0_wp
+                      tmp_agent%speed_e_y     = 0.0_wp
+                      tmp_agent%speed_des     = random_normal(desired_speed,des_sp_sig)
+                      tmp_agent%speed_x       = 0.0_wp
+                      tmp_agent%speed_y       = 0.0_wp
+                      tmp_agent%x             = pos_x
+                      tmp_agent%y             = pos_y
+                      tmp_agent%path_x        = -1.0_wp
+                      tmp_agent%path_y        = -1.0_wp
+                      tmp_agent%t_x           = - pi
+                      tmp_agent%t_y           = - pi
+!
+!--                   Determine the grid indices of the agent position
+                      ip = tmp_agent%x * ddx
+                      jp = tmp_agent%y * ddy
+!
+!--                   Give each agent its target
+                      IF ( a_rand_target(i) )  THEN
+!
+!--                      Agent shall receive random target just outside simulated area
+                         rn_side_dum = random_function(iran_agent)
+                         rn_side     = FLOOR( 4.*rn_side_dum )
+                         IF ( rn_side < 2 )  THEN
+                            IF ( rn_side == 0 )  THEN
+                               tmp_agent%t_y = -2 * dy
                             ELSE
-                               IF ( rn_side == 2 ) THEN
-                                  tmp_agent%t_x = -2*dx
-                               ELSE
-                                  tmp_agent%t_x = (nx+3)*dx
-                               ENDIF
-                               tmp_agent%t_y = random_function(iran_agent) *   &
-                                               (ny+1)*dy
+                               tmp_agent%t_y = ( ny + 3 ) * dy
                             ENDIF
-!
-!--                      Agent gets target of her group
+                            tmp_agent%t_x = random_function(iran_agent) * ( nx + 1 ) * dx
                          ELSE
-                            tmp_agent%t_x = at_x(i)
-                            tmp_agent%t_y = at_y(i)
+                            IF ( rn_side == 2 )  THEN
+                               tmp_agent%t_x = -2 * dx
+                            ELSE
+                               tmp_agent%t_x = ( nx + 3 ) * dx
+                            ENDIF
+                            tmp_agent%t_y = random_function(iran_agent) * ( ny + 1 ) * dy
                          ENDIF
-
-                         local_count(jp,ip) = local_count(jp,ip) + 1
-
-                         IF ( .NOT. first_stride )  THEN
-                            grid_agents(jp,ip)%agents(local_count(jp,ip))      &
-                                              = tmp_agent
-                         ENDIF
-
+!
+!--                   Agent gets target of her group
+                      ELSE
+                         tmp_agent%t_x = at_x(i)
+                         tmp_agent%t_y = at_y(i)
                       ENDIF
 
-                      pos_x = pos_x + adx(i)
-
-                   ENDDO xloop
-
-                ENDIF
-
-                pos_y = pos_y + ady(i)
-
-             ENDDO
+                      local_count(jp,ip) = local_count(jp,ip) + 1
+
+                      IF ( .NOT. first_stride )  THEN
+                         grid_agents(jp,ip)%agents(local_count(jp,ip)) = tmp_agent
+                      ENDIF
+
+                   ENDIF
+
+                   pos_x = pos_x + adx(i)
+
+                ENDDO xloop
+
+             ENDIF
+
+             pos_y = pos_y + ady(i)
 
           ENDDO
 
-!
-!--       Allocate or reallocate agents array to new size
-          IF ( first_stride )  THEN
-             DO  ip = nxlg, nxrg
-                DO  jp = nysg, nyng
-                   IF ( phase == PHASE_INIT )  THEN
-                      IF ( local_count(jp,ip) > 0 )  THEN
-                         alloc_size = MAX( INT( local_count(jp,ip) *           &
-                            ( 1.0_wp + alloc_factor_mas / 100.0_wp ) ),        &
-                            min_nr_agent )
-                      ELSE
-                         alloc_size = min_nr_agent
-                      ENDIF
-                      ALLOCATE(grid_agents(jp,ip)%agents(1:alloc_size))
-                      DO  n = 1, alloc_size
-                         grid_agents(jp,ip)%agents(n) = zero_agent
-                      ENDDO
-                   ELSEIF ( phase == PHASE_RELEASE )  THEN
-                      IF ( local_count(jp,ip) > 0 )  THEN
-                         new_size   = local_count(jp,ip) + agt_count(jp,ip)
-                         alloc_size = MAX( INT( new_size * ( 1.0_wp +          &
-                            alloc_factor_mas / 100.0_wp ) ), min_nr_agent )
-                         IF( alloc_size > SIZE( grid_agents(jp,ip)%agents) )   &
-                         THEN
-                            CALL mas_eh_realloc_agents_array(ip,jp,alloc_size)
-                         ENDIF
+       ENDDO
+
+!
+!--    Allocate or reallocate agents array to new size
+       IF ( first_stride )  THEN
+          DO  ip = nxlg, nxrg
+             DO  jp = nysg, nyng
+                IF ( phase == phase_init )  THEN
+                   IF ( local_count(jp,ip) > 0 )  THEN
+                      alloc_size = MAX( INT( local_count(jp,ip) *                                  &
+                                             ( 1.0_wp + alloc_factor_mas / 100.0_wp ) ),           &
+                                        min_nr_agent )
+                   ELSE
+                      alloc_size = min_nr_agent
+                   ENDIF
+                   ALLOCATE( grid_agents(jp,ip)%agents(1:alloc_size) )
+                   DO  n = 1, alloc_size
+                      grid_agents(jp,ip)%agents(n) = zero_agent
+                   ENDDO
+                ELSEIF ( phase == phase_release )  THEN
+                   IF ( local_count(jp,ip) > 0 )  THEN
+                      new_size   = local_count(jp,ip) + agt_count(jp,ip)
+                      alloc_size = MAX( INT( new_size *                                            &
+                                             ( 1.0_wp + alloc_factor_mas / 100.0_wp ) ),           &
+                                        min_nr_agent )
+                      IF( alloc_size > SIZE( grid_agents(jp,ip)%agents) )  THEN
+                         CALL mas_eh_realloc_agents_array(ip,jp,alloc_size)
                       ENDIF
                    ENDIF
-                ENDDO
+                ENDIF
              ENDDO
-          ENDIF
+          ENDDO
+       ENDIF
+
+    ENDDO
+
+    local_start = agt_count+1
+    agt_count   = local_count
+
+!
+!-- Calculate agent IDs
+    DO  ip = nxl, nxr
+       DO  jp = nys, nyn
+          number_of_agents = agt_count(jp,ip)
+          IF ( number_of_agents <= 0 )  CYCLE
+          agents => grid_agents(jp,ip)%agents(1:number_of_agents)
+
+          DO  n = local_start(jp,ip), number_of_agents  !only new agents
+
+             agents(n)%id = 10000_idp**2 * grid_agents(jp,ip)%id_counter + 10000_idp * jp + ip
+!
+!--          Count the number of agents that have been released before
+             grid_agents(jp,ip)%id_counter = grid_agents(jp,ip)%id_counter + 1
+
+          ENDDO
 
        ENDDO
-
-       local_start = agt_count+1
-       agt_count   = local_count
-
-!
-!--    Calculate agent IDs
+    ENDDO
+
+!
+!-- Add random fluctuation to agent positions.
+    IF ( random_start_position_agents )  THEN
        DO  ip = nxl, nxr
           DO  jp = nys, nyn
@@ -992,1201 +993,990 @@
              IF ( number_of_agents <= 0 )  CYCLE
              agents => grid_agents(jp,ip)%agents(1:number_of_agents)
-
-             DO  n = local_start(jp,ip), number_of_agents  !only new agents 
-
-                agents(n)%id = 10000_idp**2 * grid_agents(jp,ip)%id_counter +  &
-                               10000_idp * jp + ip
-!
-!--             Count the number of agents that have been released before
-                grid_agents(jp,ip)%id_counter = grid_agents(jp,ip)%id_counter  &
-                                                + 1
-
+!
+!--          Move only new agents. Moreover, limit random fluctuation in order to prevent that
+!--          agents move more than one grid box, which would lead to problems concerning agent
+!--          exchange  between processors in case adx/ady are larger than dx/dy, respectively.
+             DO  n = local_start(jp,ip), number_of_agents
+                IF ( asl(agents(n)%group) /= asr(agents(n)%group) )  THEN
+                   rand_contr = ( random_function( iran_agent ) - 0.5_wp ) * adx(agents(n)%group)
+                   agents(n)%x = agents(n)%x + MERGE( rand_contr, SIGN( dx, rand_contr ),          &
+                                                      ABS( rand_contr ) < dx                       &
+                                                    )
+                ENDIF
+                IF ( ass(agents(n)%group) /= asn(agents(n)%group) )  THEN
+                   rand_contr = ( random_function( iran_agent ) - 0.5_wp ) * ady(agents(n)%group)
+                   agents(n)%y = agents(n)%y +                                                     &
+                                 MERGE( rand_contr, SIGN( dy, rand_contr ), ABS( rand_contr ) < dy )
+                ENDIF
              ENDDO
+!
+!--          Delete agents that have been simulated longer than allowed
+             CALL mas_boundary_conds( 'max_sim_time' )
+!
+!--          Delete agents that have reached target area
+             CALL mas_boundary_conds( 'target_area' )
 
           ENDDO
        ENDDO
-
-!
-!--    Add random fluctuation to agent positions.
-       IF ( random_start_position_agents )  THEN
-          DO  ip = nxl, nxr
-             DO  jp = nys, nyn
-                number_of_agents = agt_count(jp,ip)
-                IF ( number_of_agents <= 0 )  CYCLE
-                agents => grid_agents(jp,ip)%agents(1:number_of_agents)
-!
-!--             Move only new agents. Moreover, limit random fluctuation 
-!--             in order to prevent that agents move more than one grid box, 
-!--             which would lead to problems concerning agent exchange 
-!--             between processors in case adx/ady are larger than dx/dy, 
-!--             respectively.  
-                DO  n = local_start(jp,ip), number_of_agents
-                   IF ( asl(agents(n)%group) /= asr(agents(n)%group) )  THEN
-                      rand_contr = ( random_function( iran_agent ) - 0.5_wp ) *&
-                                     adx(agents(n)%group)
-                      agents(n)%x = agents(n)%x +                        &
-                              MERGE( rand_contr, SIGN( dx, rand_contr ),       &
-                                     ABS( rand_contr ) < dx                    &
-                                   ) 
-                   ENDIF
-                   IF ( ass(agents(n)%group) /= asn(agents(n)%group) )  THEN
-                      rand_contr = ( random_function( iran_agent ) - 0.5_wp ) *&
-                                     ady(agents(n)%group)
-                      agents(n)%y = agents(n)%y +                        &
-                              MERGE( rand_contr, SIGN( dy, rand_contr ),       &
-                                     ABS( rand_contr ) < dy )
-                   ENDIF
-                ENDDO
-!
-!--             Delete agents that have been simulated longer than allowed
-                CALL mas_boundary_conds( 'max_sim_time' )
-!
-!--             Delete agents that have reached target area
-                CALL mas_boundary_conds( 'target_area' )
-
-             ENDDO
-          ENDDO
-!
-!--       Exchange agents between grid cells and processors
-          CALL mas_eh_move_agent
-          CALL mas_eh_exchange_horiz
-
-       ENDIF
-!
-!--    In case of random_start_position_agents, delete agents identified by 
-!--    mas_eh_exchange_horiz and mas_boundary_conds. Then sort agents into 
-!--    blocks, which is needed for a fast interpolation of the LES fields 
-!--    on the agent position.
-       CALL mas_ps_sort_in_subboxes
-
-!
-!--    Determine the current number of agents
-       number_of_agents = 0
+!
+!--    Exchange agents between grid cells and processors
+       CALL mas_eh_move_agent
+       CALL mas_eh_exchange_horiz
+
+    ENDIF
+!
+!-- In case of random_start_position_agents, delete agents identified by mas_eh_exchange_horiz and
+!-- mas_boundary_conds. Then sort agents into blocks, which is needed for a fast interpolation of
+!-- the LES fields on the agent position.
+    CALL mas_ps_sort_in_subboxes
+
+!
+!-- Determine the current number of agents
+    number_of_agents = 0
+    DO  ip = nxl, nxr
+       DO  jp = nys, nyn
+          number_of_agents = number_of_agents + agt_count(jp,ip)
+       ENDDO
+    ENDDO
+!
+!-- Calculate the number of agents of the total domain
+#if defined( __parallel )
+    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+    CALL MPI_ALLREDUCE( number_of_agents, total_number_of_agents, 1, MPI_INTEGER, MPI_SUM, comm2d, &
+                        ierr )
+#else
+    total_number_of_agents = number_of_agents
+#endif
+
+    RETURN
+
+ END SUBROUTINE mas_create_agent
+
+!--------------------------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Creates flags that indicate if a gridbox contains edges or corners. These flags are used for
+!> agents to check if obstacles are close to them.
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_create_obstacle_flags
+
+    USE arrays_3d,                                                                                 &
+        ONLY:  zw
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  il
+    INTEGER(iwp) ::  jl
+
+    ALLOCATE( obstacle_flags(nysg:nyng,nxlg:nxrg) )
+
+    obstacle_flags = 0
+
+    DO  il = nxlg, nxrg
+       DO  jl = nysg, nyng
+!
+!--       Exclude cyclic topography boundary
+          IF ( il < 0 .OR. il > nx .OR. jl < 0 .OR. jl > ny ) CYCLE
+!
+!--       North edge
+          IF ( jl < nyng )  THEN
+             IF ( ( top_top_s(jl,il) - top_top_s(jl+1,il) ) > 1  .AND.                             &
+                  ( zw( top_top_w(jl,il) ) - zw( top_top_w(jl+1,il) ) ) > 0.51_wp )                &
+             THEN
+                obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 0 )
+             ENDIF
+          ENDIF
+!
+!--       North right corner
+          IF ( jl < nyng  .AND.  il < nxrg )  THEN
+             IF ( ( top_top_s(jl,il) - top_top_s(jl+1,il)   ) > 1  .AND.                           &
+                  ( top_top_s(jl,il) - top_top_s(jl+1,il+1) ) > 1  .AND.                           &
+                  ( top_top_s(jl,il) - top_top_s(jl,il+1)   ) > 1  .AND.                           &
+                  ( zw( top_top_w(jl,il) ) - zw( top_top_w(jl+1,il+1) ) ) > 0.51_wp )              &
+             THEN
+                obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 1 )
+             ENDIF
+          ENDIF
+!
+!--       Right edge
+          IF ( il < nxrg )  THEN
+             IF ( ( top_top_s(jl,il) - top_top_s(jl,il+1) ) > 1  .AND.                             &
+                  ( zw( top_top_w(jl,il) ) - zw( top_top_w(jl,il+1) ) ) > 0.51_wp )                &
+             THEN
+                obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 2 )
+             ENDIF
+          ENDIF
+!
+!--       South right corner
+          IF ( jl > nysg  .AND.  il < nxrg )  THEN
+             IF ( ( top_top_s(jl,il) - top_top_s(jl,il+1)   ) > 1  .AND.                           &
+                  ( top_top_s(jl,il) - top_top_s(jl-1,il+1) ) > 1  .AND.                           &
+                  ( top_top_s(jl,il) - top_top_s(jl-1,il)   ) > 1  .AND.                           &
+                  ( zw(top_top_w(jl,il) ) - zw( top_top_w(jl-1,il+1) ) ) > 0.51_wp )               &
+             THEN
+                obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 3 )
+             ENDIF
+          ENDIF
+!
+!--       South edge
+          IF ( jl > nysg )  THEN
+             IF ( ( top_top_s(jl,il) - top_top_s(jl-1,il) ) > 1  .AND.                             &
+                  ( zw( top_top_w(jl,il) ) - zw( top_top_w(jl-1,il) ) ) > 0.51_wp )                &
+             THEN
+                obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 4 )
+             ENDIF
+          ENDIF
+!
+!--       South left corner
+          IF ( jl > nysg  .AND.  il > nxlg )  THEN
+             IF ( ( top_top_s(jl,il) - top_top_s(jl-1,il)   ) > 1  .AND.                           &
+                  ( top_top_s(jl,il) - top_top_s(jl-1,il-1) ) > 1  .AND.                           &
+                  ( top_top_s(jl,il) - top_top_s(jl,il-1)   ) > 1  .AND.                           &
+                  ( zw( top_top_w(jl,il) ) - zw(top_top_w(jl-1,il-1) ) ) > 0.51_wp )               &
+             THEN
+                obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 5 )
+             ENDIF
+          ENDIF
+!
+!--       Left edge
+          IF ( il > nxlg )  THEN
+             IF ( ( top_top_s(jl,il) - top_top_s(jl,il-1) ) > 1  .AND.                             &
+                  ( zw(top_top_w(jl,il) ) - zw(top_top_w(jl,il-1) ) ) > 0.51_wp )                  &
+             THEN
+                obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 6 )
+             ENDIF
+          ENDIF
+!
+!--       North left corner
+          IF ( jl < nyng  .AND.  il > nxlg )  THEN
+             IF ( ( top_top_s(jl,il) - top_top_s(jl,il-1)   ) > 1  .AND.                           &
+                  ( top_top_s(jl,il) - top_top_s(jl+1,il-1) ) > 1  .AND.                           &
+                  ( top_top_s(jl,il) - top_top_s(jl+1,il)   ) > 1  .AND.                           &
+                  ( zw( top_top_w(jl,il) ) - zw( top_top_w(jl+1,il-1) ) ) > 0.51_wp )              &
+             THEN
+                obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 7 )
+             ENDIF
+          ENDIF
+
+       ENDDO
+    ENDDO
+
+ END SUBROUTINE mas_create_obstacle_flags
+
+!--------------------------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Write agent data in netCDF format
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_data_output_agents( ftest )
+
+    USE control_parameters,                                                                        &
+        ONLY:  agt_time_count, biometeorology, end_time, message_string, multi_agent_system_end,   &
+               multi_agent_system_start
+
+    USE netcdf_interface,                                                                          &
+        ONLY:  nc_stat, id_set_agt, id_var_time_agt, id_var_agt, netcdf_handle_error
+
+    USE pegrid
+
+#if defined( __netcdf )
+    USE NETCDF
+#endif
+    USE mas_global_attributes,                                                                     &
+        ONLY:  dim_size_agtnum
+
+    IMPLICIT NONE
+
+#if defined( __parallel )
+    INTEGER(iwp) ::  agt_size !< Agent size in bytes
+    INTEGER(iwp) ::  n        !< counter (number of PEs)
+    INTEGER(iwp) ::  noa_rcv  !< received number of agents
+#endif
+    INTEGER(iwp) ::  dummy    !< dummy
+    INTEGER(iwp) ::  ii       !< counter (x)
+    INTEGER(iwp) ::  ip       !< counter (x)
+    INTEGER(iwp) ::  jp       !< counter (y)
+    INTEGER(iwp) ::  noa      !< number of agents
+    INTEGER(iwp) ::  out_noa  !< number of agents for output
+
+#if defined( __parallel )
+    INTEGER(iwp), DIMENSION(0:numprocs-1) ::  noa_arr !< number of agents on each PE
+#endif
+!
+!-- SAVE attribute required to avoid compiler warning about pointer outlive the pointer target
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE, TARGET, SAVE ::  trf_agents !< all agents on current PE
+#if defined( __parallel )
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE, TARGET, SAVE ::  out_agents !< all agents in entire domain
+#endif
+
+    LOGICAL, INTENT (INOUT) :: ftest
+
+    LOGICAL, SAVE :: agt_dimension_exceeded = .FALSE.
+
+
+    CALL cpu_log( log_point_s(17), 'mas_data_output', 'start' )
+!
+!-- Get total number of agents and put all agents on one PE in one array
+    noa = 0
+    DO  ip = nxl, nxr
+       DO  jp = nys, nyn
+          noa  = noa  + agt_count(jp,ip)
+       ENDDO
+    ENDDO
+    IF ( noa > 0 )  THEN
+       ALLOCATE( trf_agents(1:noa) )
+       dummy = 1
        DO  ip = nxl, nxr
           DO  jp = nys, nyn
-             number_of_agents = number_of_agents + agt_count(jp,ip)
+             IF ( agt_count(jp,ip) == 0 )  CYCLE
+             agents => grid_agents(jp,ip)%agents(1:agt_count(jp,ip))
+             trf_agents(dummy:(dummy-1+agt_count(jp,ip))) = agents
+             dummy = dummy + agt_count(jp,ip)
           ENDDO
        ENDDO
-!
-!--    Calculate the number of agents of the total domain
+    ENDIF
 #if defined( __parallel )
-       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-       CALL MPI_ALLREDUCE( number_of_agents, total_number_of_agents, 1, &
-       MPI_INTEGER, MPI_SUM, comm2d, ierr )
+!
+!-- Gather all agents on PE0 for output
+    IF ( myid == 0 )  THEN
+       noa_arr(0) = noa
+!
+!--    Receive data from all other PEs.
+       DO  n = 1, numprocs-1
+           CALL MPI_RECV( noa_arr(n), 1, MPI_INTEGER, n, 0, comm2d, status, ierr )
+       ENDDO
+    ELSE
+       CALL MPI_SEND( noa, 1, MPI_INTEGER, 0, 0, comm2d, ierr )
+    ENDIF
+    CALL MPI_BARRIER( comm2d, ierr )
+    agt_size = STORAGE_SIZE( zero_agent ) / 8
+    IF ( myid == 0 )  THEN
+!
+!--    Receive data from all other PEs.
+       out_noa = SUM( noa_arr )
+       IF ( out_noa > 0 )  THEN
+          ALLOCATE( out_agents(1:out_noa) )
+          IF ( noa > 0 )  THEN
+             out_agents(1:noa) = trf_agents
+          ENDIF
+          noa_rcv = noa
+          DO  n = 1, numprocs-1
+             IF ( noa_arr(n) > 0 )  THEN
+                CALL MPI_RECV( out_agents(noa_rcv+1), noa_arr(n) * agt_size, MPI_BYTE, n, 0,       &
+                               comm2d, status, ierr )
+                noa_rcv = noa_rcv + noa_arr(n)
+             ENDIF
+          ENDDO
+       ELSE
+          ALLOCATE( out_agents(1:2) )
+          out_agents = zero_agent
+          out_noa    = 2
+       ENDIF
+    ELSE
+       IF ( noa > 0 )  THEN
+          CALL MPI_SEND( trf_agents(1), noa*agt_size, MPI_BYTE, 0, 0, comm2d, ierr )
+       ENDIF
+    ENDIF
+!
+!-- A barrier has to be set, because otherwise some PEs may proceed too fast so that PE0 may receive
+!-- wrong data on tag 0.
+    CALL MPI_BARRIER( comm2d, ierr )
+#endif
+    IF ( myid == 0 )  THEN
+#if defined( __parallel )
+       agents => out_agents
 #else
-       total_number_of_agents = number_of_agents
+       agents => trf_agents
 #endif
 
-       RETURN
-
-    END SUBROUTINE mas_create_agent
-
-!------------------------------------------------------------------------------!
+#if defined( __netcdf )
+!
+!--    Update maximum number of agents
+       maximum_number_of_agents = MAX(maximum_number_of_agents, out_noa)
+!
+!--       Output in netCDF format
+       IF ( ftest )  THEN
+!
+!--          First, define size of agent number dimension from amount of agents released, release
+!--          interval, time of agent simulation and max age of agents.
+          dim_size_agtnum = MIN( MIN( multi_agent_system_end, end_time )                           &
+                                    - multi_agent_system_start,                                    &
+                                 agent_maximum_age)
+
+          DO ii = 1, number_of_agent_groups
+             dim_size_agtnum = dim_size_agtnum                                                     &
+                                + ( FLOOR( ( asr(ii)-asl(ii) ) / adx(ii) ) + 1 )                   &
+                                * ( FLOOR( ( asn(ii)-ass(ii) ) / ady(ii) ) + 1 )                   &
+                                * ( FLOOR( dim_size_agtnum / dt_arel )     + 1 )                   &
+                                * dim_size_factor_agtnum
+             dim_size_agtnum = MIN( dim_size_agtnum, dim_size_agtnum_manual )
+          ENDDO
+          CALL check_open( 118 )
+       ENDIF
+
+!
+!--    Update the NetCDF time axis
+       agt_time_count = agt_time_count + 1
+
+       IF ( .NOT. agt_dimension_exceeded )  THEN
+!
+!--       If number of agents to be output exceeds dimension, set flag and print warning.
+          IF ( out_noa > dim_size_agtnum )  THEN
+
+             agt_dimension_exceeded = .TRUE.
+             WRITE( message_string, '(A,F11.1,2(A,I8))' )                                          &
+                                                  'Number of agents exceeds agent dimension.' //   &
+                                                  '&Starting at time_since_reference_point = ',    &
+                                                  time_since_reference_point,                      &
+                                                  ' s, &data may be missing.'//                    &
+                                                  '&Number of agents:     ', out_noa,              &
+                                                  '&Agent dimension size: ', dim_size_agtnum
+
+             CALL message( 'mas_data_output_agents', 'PA0420', 0, 1, 0, 6, 0 )
+
+          ENDIF
+       ENDIF
+
+!
+!--    Reduce number of output agents to dimension size, if necessary.
+       IF ( agt_dimension_exceeded )  THEN
+
+          out_noa = MIN( out_noa, dim_size_agtnum )
+
+       ENDIF
+
+       nc_stat = NF90_PUT_VAR( id_set_agt, id_var_time_agt,                                        &
+                               (/ time_since_reference_point + agent_substep_time /),              &
+                               start = (/ agt_time_count /),                                       &
+                               count = (/ 1 /) )
+       CALL netcdf_handle_error( 'mas_data_output_agents', 1 )
+
+!
+!--       Output agent attributes
+
+       nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(1), agents%id,                               &
+                               start = (/ 1, agt_time_count /),                                    &
+                               count = (/ out_noa /) )
+       CALL netcdf_handle_error( 'mas_data_output_agents', 2 )
+
+       nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(2), agents%x,                                &
+                               start = (/ 1, agt_time_count /),                                    &
+                               count = (/ out_noa /) )
+       CALL netcdf_handle_error( 'mas_data_output_agents', 3 )
+
+       nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(3), agents%y,                                &
+                               start = (/ 1, agt_time_count /),                                    &
+                               count = (/ out_noa /) )
+       CALL netcdf_handle_error( 'mas_data_output_agents', 4 )
+
+       nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(4), agents%windspeed,                        &
+                               start = (/ 1, agt_time_count /),                                    &
+                               count = (/ out_noa /) )
+       CALL netcdf_handle_error( 'mas_data_output_agents', 5 )
+
+       nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(5), agents%t,                                &
+                               start = (/ 1, agt_time_count /),                                    &
+                               count = (/ out_noa /) )
+       CALL netcdf_handle_error( 'mas_data_output_agents', 6 )
+
+       nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(6), agents%group,                            &
+                               start = (/ 1, agt_time_count /),                                    &
+                               count = (/ out_noa /) )
+       CALL netcdf_handle_error( 'mas_data_output_agents', 7 )
+
+
+       IF ( biometeorology )  THEN
+          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(7), agents%ipt,                           &
+                                  start = (/ 1, agt_time_count /),                                 &
+                                  count = (/ out_noa /) )
+          CALL netcdf_handle_error( 'mas_data_output_agents', 8 )
+       ENDIF
+
+
+
+!           nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(8), agents%pm10,                         &
+!                                   start = (/ 1, agt_time_count /),                                &
+!                                   count = (/ out_noa /) )
+!           CALL netcdf_handle_error( 'mas_data_output_agents', 9 )
+!
+!           nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(9), agents%pm25,                         &
+!                                   start = (/ 1, agt_time_count /),                                &
+!                                   count = (/ out_noa /) )
+!           CALL netcdf_handle_error( 'mas_data_output_agents', 10 )
+!
+!
+!           nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(9), agents%uv,                           &
+!                                   start = (/ 1, agt_time_count /),                                &
+!                                   count = (/ out_noa /) )
+!           CALL netcdf_handle_error( 'mas_data_output_agents', 10 )
+
+       CALL cpu_log( log_point_s(17), 'mas_data_output', 'stop' )
+
+
+#endif
+
+#if defined( __parallel )
+       IF ( ALLOCATED( out_agents ) ) DEALLOCATE( out_agents )
+#endif
+    ELSE
+       CALL cpu_log( log_point_s(17), 'mas_data_output', 'stop' )
+    ENDIF
+
+    IF ( ALLOCATED( trf_agents ) ) DEALLOCATE( trf_agents )
+
+ END SUBROUTINE mas_data_output_agents
+
+#if defined( __parallel )
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> Creates flags that indicate if a gridbox contains edges or corners. These
-!> flags are used for agents to check if obstacles are close to them.
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_create_obstacle_flags
-
-       USE arrays_3d,                                                          &
-           ONLY:  zw
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  il
-       INTEGER(iwp) ::  jl
-
-       ALLOCATE(obstacle_flags(nysg:nyng,nxlg:nxrg))
-
-       obstacle_flags = 0
-
-       DO il = nxlg, nxrg
-          DO jl = nysg, nyng
-!
-!--          Exclude cyclic topography boundary
-             IF ( il < 0 .OR. il > nx .OR. jl < 0 .OR. jl > ny ) CYCLE
-!
-!--          North edge
-             IF ( jl < nyng ) THEN
-                IF ( ( top_top_s(jl,il) - top_top_s(jl+1,il) ) > 1 .AND.       &
-                     ( zw( top_top_w(jl,il) ) -                                &
-                       zw( top_top_w(jl+1,il) ) ) > .51_wp )                   &
-                THEN
-                   obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 0 )
+!> If an agent moves from one processor to another, this subroutine moves the corresponding elements
+!> from the agent arrays of the old grid cells to the agent arrays of the new grid cells.
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_eh_add_agents_to_gridcell (agent_array)
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  aindex  !< dummy argument for new number of agents per grid box
+    INTEGER(iwp) ::  ip      !< grid index (x) of agent
+    INTEGER(iwp) ::  jp      !< grid index (x) of agent
+    INTEGER(iwp) ::  n       !< index variable of agent
+
+    LOGICAL ::  pack_done  !< flag to indicate that packing is done
+
+    TYPE(agent_type), DIMENSION(:), INTENT(IN)  ::  agent_array  !< new agents in a grid box
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  temp_ns      !< temporary agent array for reallocation
+
+    pack_done     = .FALSE.
+
+    DO  n = 1, SIZE(agent_array)
+
+       IF ( .NOT. agent_array(n)%agent_mask )  CYCLE
+
+       ip = agent_array(n)%x * ddx
+       jp = agent_array(n)%y * ddy
+
+       IF ( ip >= nxl  .AND.  ip <= nxr  .AND.  jp >= nys  .AND.  jp <= nyn )  THEN ! agent stays on processor
+          number_of_agents = agt_count(jp,ip)
+          agents => grid_agents(jp,ip)%agents(1:number_of_agents)
+
+          aindex = agt_count(jp,ip)+1
+          IF( aindex > SIZE( grid_agents(jp,ip)%agents ) )  THEN
+             IF ( pack_done )  THEN
+                CALL mas_eh_realloc_agents_array (ip,jp)
+             ELSE
+                CALL mas_ps_pack
+                agt_count(jp,ip) = number_of_agents
+                aindex = agt_count(jp,ip)+1
+                IF ( aindex > SIZE( grid_agents(jp,ip)%agents ) )  THEN
+                   CALL mas_eh_realloc_agents_array (ip,jp)
+                ENDIF
+                pack_done = .TRUE.
+             ENDIF
+          ENDIF
+          grid_agents(jp,ip)%agents(aindex) = agent_array(n)
+          agt_count(jp,ip) = aindex
+       ELSE
+          IF ( jp <= nys - 1 )  THEN
+             nr_move_south = nr_move_south+1
+!
+!--          Before agent information is swapped to exchange-array, check if enough memory is
+!--          allocated. If required, reallocate exchange array.
+             IF ( nr_move_south > SIZE( move_also_south ) )  THEN
+!
+!--             At first, allocate further temporary array to swap agent information.
+                ALLOCATE( temp_ns( SIZE( move_also_south ) + nr_2_direction_move ) )
+                temp_ns(1:nr_move_south-1) = move_also_south(1:nr_move_south-1)
+                DEALLOCATE( move_also_south )
+                ALLOCATE( move_also_south( SIZE(temp_ns) ) )
+                move_also_south(1:nr_move_south-1) = temp_ns(1:nr_move_south-1)
+                DEALLOCATE( temp_ns )
+
+             ENDIF
+
+             move_also_south(nr_move_south) = agent_array(n)
+
+             IF ( jp == -1 )  THEN
+!
+!--             Apply boundary condition along y
+                IF ( ibc_mas_ns == 0 )  THEN
+                   move_also_south(nr_move_south)%y = move_also_south(nr_move_south)%y             &
+                                                      + ( ny + 1 ) * dy
+                   move_also_south(nr_move_south)%origin_y =                                       &
+                                                         move_also_south(nr_move_south)%origin_y   &
+                                                         + ( ny + 1 ) * dy
+                ELSEIF ( ibc_mas_ns == 1 )  THEN
+!
+!--                Agent absorption
+                   move_also_south(nr_move_south)%agent_mask = .FALSE.
+                   deleted_agents = deleted_agents + 1
+
                 ENDIF
              ENDIF
-!
-!--          North right corner
-             IF ( jl < nyng .AND. il < nxrg ) THEN
-                IF ( ( top_top_s(jl,il) - top_top_s(jl+1,il) )   > 1 .AND.     &
-                     ( top_top_s(jl,il) - top_top_s(jl+1,il+1) ) > 1 .AND.     &
-                     ( top_top_s(jl,il) - top_top_s(jl,il+1) )   > 1 .AND.     &
-                     ( zw( top_top_w(jl,il) ) -                                &
-                       zw( top_top_w(jl+1,il+1) ) ) > .51_wp )                 &
-                THEN
-                   obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 1 )
-                ENDIF
+          ELSEIF ( jp >= nyn+1 )  THEN
+             nr_move_north = nr_move_north+1
+!
+!--          Before agent information is swapped to exchange-array, check  if enough memory is
+!--          allocated. If required, reallocate exchange array.
+             IF ( nr_move_north > SIZE( move_also_north ) )  THEN
+!
+!--             At first, allocate further temporary array to swap agent information.
+                ALLOCATE( temp_ns( SIZE( move_also_north ) + nr_2_direction_move ) )
+                temp_ns(1:nr_move_north-1) = move_also_south(1:nr_move_north-1)
+                DEALLOCATE( move_also_north )
+                ALLOCATE( move_also_north(SIZE(temp_ns)) )
+                move_also_north(1:nr_move_north-1) = temp_ns(1:nr_move_north-1)
+                DEALLOCATE( temp_ns )
+
              ENDIF
-!
-!--          Right edge
-             IF ( il < nxrg ) THEN
-                IF ( ( top_top_s(jl,il) - top_top_s(jl,il+1) ) > 1 .AND.       &
-                     ( zw( top_top_w(jl,il) ) -                                &
-                       zw( top_top_w(jl,il+1) ) ) > .51_wp )                   &
-                THEN
-                   obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 2 )
-                ENDIF
-             ENDIF
-!
-!--          South right corner
-             IF ( jl > nysg .AND. il < nxrg ) THEN
-                IF ( ( top_top_s(jl,il) - top_top_s(jl,il+1) )   > 1 .AND.     &
-                     ( top_top_s(jl,il) - top_top_s(jl-1,il+1) ) > 1 .AND.     &
-                     ( top_top_s(jl,il) - top_top_s(jl-1,il) )   > 1 .AND.     &
-                     ( zw(top_top_w(jl,il)) -                                  &
-                       zw( top_top_w(jl-1,il+1) ) ) > .51_wp )                 &
-                THEN
-                   obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 3 )
-                ENDIF
-             ENDIF
-!
-!--          South edge
-             IF ( jl > nysg ) THEN
-                IF ( ( top_top_s(jl,il) - top_top_s(jl-1,il) ) > 1 .AND.       &
-                     ( zw( top_top_w(jl,il) ) -                                &
-                       zw( top_top_w(jl-1,il) ) ) > .51_wp )                   &
-                THEN
-                   obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 4 )
-                ENDIF
-             ENDIF
-!
-!--          South left corner
-             IF ( jl > nysg .AND. il > nxlg ) THEN
-                IF ( ( top_top_s(jl,il) - top_top_s(jl-1,il) )   > 1 .AND.     &
-                     ( top_top_s(jl,il) - top_top_s(jl-1,il-1) ) > 1 .AND.     &
-                     ( top_top_s(jl,il) - top_top_s(jl,il-1) )   > 1 .AND.     &
-                     ( zw( top_top_w(jl,il) ) -                                &
-                       zw(top_top_w(jl-1,il-1) ) ) > .51_wp )                  &
-                THEN
-                   obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 5 )
-                ENDIF
-             ENDIF
-!
-!--          Left edge
-             IF ( il > nxlg ) THEN
-                IF ( ( top_top_s(jl,il) - top_top_s(jl,il-1) ) > 1 .AND.       &
-                     ( zw(top_top_w(jl,il) ) -                                 &
-                       zw(top_top_w(jl,il-1) ) ) > .51_wp )                 &
-                THEN
-                   obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 6 )
-                ENDIF
-             ENDIF
-!
-!--          North left corner
-             IF ( jl < nyng .AND. il > nxlg ) THEN
-                IF ( ( top_top_s(jl,il) - top_top_s(jl,il-1) )   > 1 .AND.     &
-                     ( top_top_s(jl,il) - top_top_s(jl+1,il-1) ) > 1 .AND.     &
-                     ( top_top_s(jl,il) - top_top_s(jl+1,il) )   > 1 .AND.     &
-                     ( zw( top_top_w(jl,il) ) -                                &
-                       zw( top_top_w(jl+1,il-1) ) ) > .51_wp )                 &
-                THEN
-                   obstacle_flags(jl,il) = IBSET( obstacle_flags(jl,il), 7 )
-                ENDIF
-             ENDIF
-
-          ENDDO
-       ENDDO
-
-    END SUBROUTINE mas_create_obstacle_flags
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Write agent data in netCDF format 
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_data_output_agents( ftest )
-
-       USE control_parameters,                                                 &
-           ONLY:  agt_time_count, biometeorology, end_time, message_string,    &
-                  multi_agent_system_end, multi_agent_system_start
-
-       USE netcdf_interface,                                                   &
-           ONLY:  nc_stat, id_set_agt, id_var_time_agt,        &
-                  id_var_agt, netcdf_handle_error
-
-       USE pegrid
-
-#if defined( __netcdf )
-       USE NETCDF
-#endif
-       USE mas_global_attributes,                                              &
-           ONLY:  dim_size_agtnum
-
-       IMPLICIT NONE
-
-#if defined( __parallel )
-       INTEGER(iwp) ::  agt_size !< Agent size in bytes
-       INTEGER(iwp) ::  n        !< counter (number of PEs)
-       INTEGER(iwp) ::  noa_rcv  !< received number of agents
-#endif
-       INTEGER(iwp) ::  dummy    !< dummy
-       INTEGER(iwp) ::  ii       !< counter (x)
-       INTEGER(iwp) ::  ip       !< counter (x)
-       INTEGER(iwp) ::  jp       !< counter (y)
-       INTEGER(iwp) ::  noa      !< number of agents
-       INTEGER(iwp) ::  out_noa  !< number of agents for output
-
-#if defined( __parallel )
-       INTEGER(iwp), DIMENSION(0:numprocs-1) ::  noa_arr !< number of agents on each PE
-#endif
-!
-!--    SAVE attribute required to avoid compiler warning about pointer outlive the pointer target
-       TYPE(agent_type), DIMENSION(:), ALLOCATABLE, TARGET, SAVE ::  trf_agents !< all agents on current PE
-#if defined( __parallel )
-       TYPE(agent_type), DIMENSION(:), ALLOCATABLE, TARGET, SAVE ::  out_agents !< all agents in entire domain
-#endif
-
-       LOGICAL, INTENT (INOUT) :: ftest
-
-       LOGICAL, SAVE :: agt_dimension_exceeded = .FALSE.
-
-       CALL cpu_log( log_point_s(17), 'mas_data_output', 'start' )
-!
-!--    Get total number of agents and put all agents on one PE in one array
-       noa = 0
-       DO  ip = nxl, nxr
-          DO  jp = nys, nyn
-             noa  = noa  + agt_count(jp,ip)
-          ENDDO
-       ENDDO
-       IF(noa > 0) THEN
-          ALLOCATE(trf_agents(1:noa))
-          dummy = 1
-          DO  ip = nxl, nxr
-             DO  jp = nys, nyn
-                IF ( agt_count(jp,ip) == 0 ) CYCLE
-                agents => grid_agents(jp,ip)%agents(1:agt_count(jp,ip))
-                trf_agents(dummy:(dummy-1+agt_count(jp,ip))) = agents
-                dummy = dummy + agt_count(jp,ip)
-             ENDDO
-          ENDDO
-       ENDIF
-#if defined( __parallel )
-!
-!--    Gather all agents on PE0 for output
-       IF ( myid == 0 )  THEN
-          noa_arr(0) = noa
-!
-!--       Receive data from all other PEs.
-          DO  n = 1, numprocs-1
-              CALL MPI_RECV( noa_arr(n), 1, MPI_INTEGER,                       &
-                              n, 0, comm2d, status, ierr )
-          ENDDO
-       ELSE
-          CALL MPI_SEND( noa, 1, MPI_INTEGER, 0, 0, comm2d, ierr )
-       ENDIF
-       CALL MPI_BARRIER( comm2d, ierr )
-       agt_size = STORAGE_SIZE(zero_agent)/8
-       IF ( myid == 0 )  THEN
-!
-!--       Receive data from all other PEs.
-          out_noa = SUM(noa_arr)
-          IF ( out_noa > 0 ) THEN
-             ALLOCATE( out_agents(1:out_noa) )
-             IF ( noa > 0 ) THEN
-                out_agents(1:noa) = trf_agents
-             ENDIF
-             noa_rcv = noa
-             DO n = 1, numprocs-1
-                IF ( noa_arr(n) > 0 ) THEN
-                   CALL MPI_RECV( out_agents(noa_rcv+1), noa_arr(n)*agt_size,  &
-                                  MPI_BYTE, n, 0, comm2d, status, ierr )
-                   noa_rcv = noa_rcv + noa_arr(n)
-                ENDIF
-             ENDDO
-          ELSE
-             ALLOCATE( out_agents(1:2) )
-             out_agents = zero_agent
-             out_noa    = 2
-          ENDIF
-       ELSE
-          IF ( noa > 0 ) THEN
-             CALL MPI_SEND( trf_agents(1), noa*agt_size, MPI_BYTE, 0, 0,       &
-                                        comm2d, ierr )
-          ENDIF
-       ENDIF
-!
-!--    A barrier has to be set, because otherwise some PEs may
-!--    proceed too fast so that PE0 may receive wrong data on
-!--    tag 0
-       CALL MPI_BARRIER( comm2d, ierr )
-#endif
-       IF ( myid == 0 ) THEN
-#if defined( __parallel )
-          agents => out_agents
-#else
-          agents => trf_agents
-#endif
-
-#if defined( __netcdf )
-!
-!--       Update maximum number of agents
-          maximum_number_of_agents = MAX(maximum_number_of_agents, out_noa)
-!
-!--       Output in netCDF format
-          IF ( ftest ) THEN
-!
-!--          First, define size of agent number dimension from amount of agents
-!--          released, release interval, time of agent simulation and max
-!--          age of agents
-             dim_size_agtnum = MIN( MIN( multi_agent_system_end, end_time )    &
-                                       - multi_agent_system_start,             &
-                                    agent_maximum_age)
-
-             DO ii = 1, number_of_agent_groups
-                dim_size_agtnum = dim_size_agtnum                              &
-                                + (FLOOR( ( asr(ii)-asl(ii) ) / adx(ii) ) + 1) &
-                                * (FLOOR( ( asn(ii)-ass(ii) ) / ady(ii) ) + 1) &
-                                * (FLOOR( dim_size_agtnum / dt_arel )     + 1) &
-                                * dim_size_factor_agtnum
-                dim_size_agtnum = MIN( dim_size_agtnum, dim_size_agtnum_manual )
-             ENDDO
-             CALL check_open( 118 )
-          ENDIF
-
-!
-!--       Update the NetCDF time axis
-          agt_time_count = agt_time_count + 1
-
-          IF ( .NOT. agt_dimension_exceeded ) THEN
-!
-!--          if number of agents to be output exceeds dimension, set flag and
-!--          print warning
-             IF ( out_noa > dim_size_agtnum ) THEN
-
-                agt_dimension_exceeded = .TRUE.
-                WRITE(message_string,'(A,F11.1,2(A,I8))')                      &
-                                'Number of agents exceeds agent dimension.' // &
-                                '&Starting at time_since_reference_point = ',  &
-                                time_since_reference_point,                    &
-                                ' s, &data may be missing.'//                  &
-                                '&Number of agents:     ', out_noa,            &
-                                '&Agent dimension size: ', dim_size_agtnum
-
-                CALL message( 'mas_data_output_agents',                        &
-                              'PA0420', 0, 1, 0, 6, 0 )
-
-             ENDIF
-          ENDIF
-
-!
-!--       reduce number of output agents to dimension size, if necessary
-          IF ( agt_dimension_exceeded ) THEN
-
-             out_noa = MIN( out_noa, dim_size_agtnum )
-
-          ENDIF
-
-          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_time_agt,                 &
-                                  (/ time_since_reference_point + agent_substep_time /),            &
-                                  start = (/ agt_time_count /),                &
-                                  count = (/ 1 /) )
-          CALL netcdf_handle_error( 'mas_data_output_agents', 1 )
-
-!
-!--       Output agent attributes
-
-          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(1), agents%id,        &
-                                  start = (/ 1, agt_time_count /),             &
-                                  count = (/ out_noa /) )
-          CALL netcdf_handle_error( 'mas_data_output_agents', 2 )
- 
-          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(2), agents%x,         &
-                                  start = (/ 1, agt_time_count /),             &
-                                  count = (/ out_noa /) )
-          CALL netcdf_handle_error( 'mas_data_output_agents', 3 )
-
-          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(3), agents%y,         &
-                                  start = (/ 1, agt_time_count /),             &
-                                  count = (/ out_noa /) )
-          CALL netcdf_handle_error( 'mas_data_output_agents', 4 )
-
-          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(4), agents%windspeed, &
-                                  start = (/ 1, agt_time_count /),             &
-                                  count = (/ out_noa /) )
-          CALL netcdf_handle_error( 'mas_data_output_agents', 5 )
-
-          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(5), agents%t,         &
-                                  start = (/ 1, agt_time_count /),             &
-                                  count = (/ out_noa /) )
-          CALL netcdf_handle_error( 'mas_data_output_agents', 6 )
-
-          nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(6), agents%group,     &
-                                  start = (/ 1, agt_time_count /),             &
-                                  count = (/ out_noa /) )
-          CALL netcdf_handle_error( 'mas_data_output_agents', 7 )
-
-
-          IF ( biometeorology )  THEN
-             nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(7), agents%ipt,    &
-                                     start = (/ 1, agt_time_count /),          &
-                                     count = (/ out_noa /) )
-             CALL netcdf_handle_error( 'mas_data_output_agents', 8 )  
-          ENDIF
-          
-
-          
-!           nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(8), agents%pm10,      &
-!                                   start = (/ 1, agt_time_count /),             &
-!                                   count = (/ out_noa /) )
-!           CALL netcdf_handle_error( 'mas_data_output_agents', 9 )
-! 
-!           nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(9), agents%pm25,      &
-!                                   start = (/ 1, agt_time_count /),             &
-!                                   count = (/ out_noa /) )
-!           CALL netcdf_handle_error( 'mas_data_output_agents', 10 )
-! 
-! 
-!           nc_stat = NF90_PUT_VAR( id_set_agt, id_var_agt(9), agents%uv,        &
-!                                   start = (/ 1, agt_time_count /),             &
-!                                   count = (/ out_noa /) )
-!           CALL netcdf_handle_error( 'mas_data_output_agents', 10 )
-
-          CALL cpu_log( log_point_s(17), 'mas_data_output', 'stop' )
-
-
-#endif
-
-#if defined( __parallel )
-          IF ( ALLOCATED( out_agents ) ) DEALLOCATE( out_agents )
-#endif
-       ELSE
-          CALL cpu_log( log_point_s(17), 'mas_data_output', 'stop' )
-       ENDIF
-
-       IF ( ALLOCATED( trf_agents ) ) DEALLOCATE( trf_agents )
-
-    END SUBROUTINE mas_data_output_agents
-
-#if defined( __parallel )
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> If an agent moves from one processor to another, this subroutine moves 
-!> the corresponding elements from the agent arrays of the old grid cells 
-!> to the agent arrays of the new grid cells.
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_eh_add_agents_to_gridcell (agent_array)
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  aindex !< dummy argument for new number of agents per grid box
-       INTEGER(iwp) ::  ip     !< grid index (x) of agent
-       INTEGER(iwp) ::  jp     !< grid index (x) of agent
-       INTEGER(iwp) ::  n      !< index variable of agent
-
-       LOGICAL ::  pack_done !< flag to indicate that packing is done
-
-       TYPE(agent_type), DIMENSION(:), INTENT(IN)  ::  agent_array !< new agents in a grid box
-       TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  temp_ns     !< temporary agent array for reallocation
-
-       pack_done     = .FALSE.
-
-       DO n = 1, SIZE(agent_array)
-
-          IF ( .NOT. agent_array(n)%agent_mask )  CYCLE
-
-          ip = agent_array(n)%x * ddx
-          jp = agent_array(n)%y * ddy
-
-          IF ( ip >= nxl  .AND.  ip <= nxr  .AND.                              &
-               jp >= nys  .AND.  jp <= nyn )  &
-          THEN ! agent stays on processor
-             number_of_agents = agt_count(jp,ip)
-             agents => grid_agents(jp,ip)%agents(1:number_of_agents)
-
-             aindex = agt_count(jp,ip)+1
-             IF( aindex > SIZE(grid_agents(jp,ip)%agents) )  THEN
-                IF ( pack_done )  THEN
-                   CALL mas_eh_realloc_agents_array (ip,jp)
-                ELSE
-                   CALL mas_ps_pack
-                   agt_count(jp,ip) = number_of_agents
-                   aindex = agt_count(jp,ip)+1
-                   IF ( aindex > SIZE(grid_agents(jp,ip)%agents) )  THEN
-                      CALL mas_eh_realloc_agents_array (ip,jp)
-                   ENDIF
-                   pack_done = .TRUE.
-                ENDIF
-             ENDIF
-             grid_agents(jp,ip)%agents(aindex) = agent_array(n)
-             agt_count(jp,ip) = aindex
-          ELSE
-             IF ( jp <= nys - 1 )  THEN
-                nr_move_south = nr_move_south+1
-!
-!--             Before agent information is swapped to exchange-array, check 
-!--             if enough memory is allocated. If required, reallocate exchange
-!--             array.
-                IF ( nr_move_south > SIZE(move_also_south) )  THEN
-!
-!--                At first, allocate further temporary array to swap agent 
-!--                information.
-                   ALLOCATE( temp_ns(SIZE(move_also_south)+NR_2_direction_move))
-                   temp_ns(1:nr_move_south-1) = move_also_south                &
-                                                (1:nr_move_south-1)
-                   DEALLOCATE( move_also_south )
-                   ALLOCATE( move_also_south(SIZE(temp_ns)) )
-                   move_also_south(1:nr_move_south-1) = temp_ns                &
-                                                        (1:nr_move_south-1)
-                   DEALLOCATE( temp_ns )
-
-                ENDIF
-
-                move_also_south(nr_move_south) = agent_array(n)
-
-                IF ( jp == -1 )  THEN
-!
-!--                Apply boundary condition along y
-                   IF ( ibc_mas_ns == 0 )  THEN
-                      move_also_south(nr_move_south)%y =                       &
-                                         move_also_south(nr_move_south)%y      &
-                                       + ( ny + 1 ) * dy
-                      move_also_south(nr_move_south)%origin_y =                &
-                                       move_also_south(nr_move_south)%origin_y &
-                                       + ( ny + 1 ) * dy
-                   ELSEIF ( ibc_mas_ns == 1 )  THEN
-!
-!--                   Agent absorption
-                      move_also_south(nr_move_south)%agent_mask = .FALSE.
-                      deleted_agents = deleted_agents + 1
-
-                   ENDIF
-                ENDIF
-             ELSEIF ( jp >= nyn+1 )  THEN
-                nr_move_north = nr_move_north+1
-!
-!--             Before agent information is swapped to exchange-array, check 
-!--             if enough memory is allocated. If required, reallocate exchange
-!--             array.
-                IF ( nr_move_north > SIZE(move_also_north) )  THEN
-!
-!--                At first, allocate further temporary array to swap agent 
-!--                information.
-                   ALLOCATE( temp_ns(SIZE(move_also_north)+NR_2_direction_move))
-                   temp_ns(1:nr_move_north-1) =                                &
-                                move_also_south(1:nr_move_north-1)
-                   DEALLOCATE( move_also_north )
-                   ALLOCATE( move_also_north(SIZE(temp_ns)) )
-                   move_also_north(1:nr_move_north-1) =                        &
-                               temp_ns(1:nr_move_north-1)
-                   DEALLOCATE( temp_ns )
-
-                ENDIF
-
-                move_also_north(nr_move_north) = agent_array(n)
-                IF ( jp == ny+1 )  THEN
-!
-!--                Apply boundary condition along y
-                   IF ( ibc_mas_ns == 0 )  THEN
-
-                      move_also_north(nr_move_north)%y =                       &
-                         move_also_north(nr_move_north)%y                      &
-                       - ( ny + 1 ) * dy
-                      move_also_north(nr_move_north)%origin_y =                &
-                         move_also_north(nr_move_north)%origin_y               &
-                       - ( ny + 1 ) * dy
-                   ELSEIF ( ibc_mas_ns == 1 )  THEN
-!
-!--                   Agent absorption
-                      move_also_north(nr_move_north)%agent_mask = .FALSE.
-                      deleted_agents = deleted_agents + 1
-
-                   ENDIF
+
+             move_also_north(nr_move_north) = agent_array(n)
+             IF ( jp == ny+1 )  THEN
+!
+!--             Apply boundary condition along y
+                IF ( ibc_mas_ns == 0 )  THEN
+
+                   move_also_north(nr_move_north)%y = move_also_north(nr_move_north)%y             &
+                                                      - ( ny + 1 ) * dy
+                   move_also_north(nr_move_north)%origin_y =                                       &
+                                                        move_also_north(nr_move_north)%origin_y    &
+                                                        - ( ny + 1 ) * dy
+                ELSEIF ( ibc_mas_ns == 1 )  THEN
+!
+!--                Agent absorption
+                   move_also_north(nr_move_north)%agent_mask = .FALSE.
+                   deleted_agents = deleted_agents + 1
+
                 ENDIF
              ENDIF
           ENDIF
-       ENDDO
-
-       RETURN
-
-    END SUBROUTINE mas_eh_add_agents_to_gridcell
+       ENDIF
+    ENDDO
+
+    RETURN
+
+ END SUBROUTINE mas_eh_add_agents_to_gridcell
 #endif
 
 
 #if defined( __parallel )
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> After ghost layer agents have been received from neighboring PEs, this
-!> subroutine sorts them into the corresponding grid cells
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_eh_add_ghost_agents_to_gridcell (agent_array)
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  ip     !< grid index (x) of agent
-       INTEGER(iwp) ::  jp     !< grid index (x) of agent
-       INTEGER(iwp) ::  n      !< index variable of agent
-       INTEGER(iwp) ::  aindex !< dummy argument for new number of agents per grid box
-
-       LOGICAL ::  pack_done !< flag to indicate that packing is done
-
-       TYPE(agent_type), DIMENSION(:), INTENT(IN)  ::  agent_array !< new agents in a grid box
-
-       pack_done     = .FALSE.
-
-       DO n = 1, SIZE(agent_array)
-
-          IF ( .NOT. agent_array(n)%agent_mask )  CYCLE
-
-          ip = agent_array(n)%x * ddx
-          jp = agent_array(n)%y * ddy
-
-          IF ( ip < nxl  .OR.  ip > nxr  .OR.  jp < nys  .OR.  jp > nyn ) THEN
-             number_of_agents = agt_count(jp,ip)
-             agents => grid_agents(jp,ip)%agents(1:number_of_agents)
-
-             aindex = agt_count(jp,ip)+1
-             IF( aindex > SIZE(grid_agents(jp,ip)%agents) )  THEN
-                IF ( pack_done )  THEN
+!> After ghost layer agents have been received from neighboring PEs, this subroutine sorts them into
+!> the corresponding grid cells
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_eh_add_ghost_agents_to_gridcell (agent_array)
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  aindex  !< dummy argument for new number of agents per grid box
+    INTEGER(iwp) ::  ip      !< grid index (x) of agent
+    INTEGER(iwp) ::  jp      !< grid index (x) of agent
+    INTEGER(iwp) ::  n       !< index variable of agent
+
+    LOGICAL ::  pack_done  !< flag to indicate that packing is done
+
+    TYPE(agent_type), DIMENSION(:), INTENT(IN)  ::  agent_array  !< new agents in a grid box
+
+    pack_done     = .FALSE.
+
+    DO  n = 1, SIZE(agent_array)
+
+       IF ( .NOT. agent_array(n)%agent_mask )  CYCLE
+
+       ip = agent_array(n)%x * ddx
+       jp = agent_array(n)%y * ddy
+
+       IF ( ip < nxl  .OR.  ip > nxr  .OR.  jp < nys  .OR.  jp > nyn )  THEN
+          number_of_agents = agt_count(jp,ip)
+          agents => grid_agents(jp,ip)%agents(1:number_of_agents)
+
+          aindex = agt_count(jp,ip)+1
+          IF( aindex > SIZE( grid_agents(jp,ip)%agents ) )  THEN
+             IF ( pack_done )  THEN
+                CALL mas_eh_realloc_agents_array (ip,jp)
+             ELSE
+                CALL mas_ps_pack
+                agt_count(jp,ip) = number_of_agents
+                aindex = agt_count(jp,ip)+1
+                IF ( aindex > SIZE( grid_agents(jp,ip)%agents ) )  THEN
                    CALL mas_eh_realloc_agents_array (ip,jp)
-                ELSE
-                   CALL mas_ps_pack
-                   agt_count(jp,ip) = number_of_agents
-                   aindex = agt_count(jp,ip)+1
-                   IF ( aindex > SIZE(grid_agents(jp,ip)%agents) )  THEN
-                      CALL mas_eh_realloc_agents_array (ip,jp)
-                   ENDIF
-                   pack_done = .TRUE.
                 ENDIF
+                pack_done = .TRUE.
              ENDIF
-             grid_agents(jp,ip)%agents(aindex) = agent_array(n)
-             agt_count(jp,ip) = aindex
           ENDIF
-       ENDDO
-    END SUBROUTINE mas_eh_add_ghost_agents_to_gridcell
+          grid_agents(jp,ip)%agents(aindex) = agent_array(n)
+          agt_count(jp,ip) = aindex
+       ENDIF
+    ENDDO
+ END SUBROUTINE mas_eh_add_ghost_agents_to_gridcell
 #endif
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Resizing of agent arrays
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_eh_dealloc_agents_array
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  i         !< grid index (x) of agent
-       INTEGER(iwp) ::  j         !< grid index (y) of agent
-       INTEGER(iwp) ::  old_size  !< old array size
-       INTEGER(iwp) ::  new_size  !< new array size
-       INTEGER(iwp) ::  noa       !< number of agents
-
-       LOGICAL ::  dealloc  !< flag that indicates if reallocation is necessary
-
-       TYPE(agent_type), DIMENSION(10) ::  tmp_agents_s !< temporary static agent array
-
-       TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  tmp_agents_d !< temporary dynamic agent array
-
-       DO  i = nxlg, nxrg
-          DO  j = nysg, nyng
-!
-!--          Determine number of active agents
-             noa = agt_count(j,i)
-!
-!--          Determine allocated memory size
-             old_size = SIZE( grid_agents(j,i)%agents )
-!
-!--          Check for large unused memory
-             dealloc = ( ( noa < min_nr_agent .AND. old_size  > min_nr_agent ) &
-                    .OR. ( noa > min_nr_agent .AND. old_size - noa *           &
-                         ( 1.0_wp + 0.01_wp * alloc_factor_mas ) > 0.0_wp ) )
-!
-!--          If large unused memory was found, resize the corresponding array
-             IF ( dealloc )  THEN
-                IF ( noa < min_nr_agent )  THEN
-                   new_size = min_nr_agent
-                ELSE 
-                   new_size = INT( noa * ( 1.0_wp +                            &
-                                            0.01_wp * alloc_factor_mas ) )
-                ENDIF
-
-                IF ( noa <= 10 )  THEN
-
-                   tmp_agents_s(1:noa) = grid_agents(j,i)%agents(1:noa)
-
-                   DEALLOCATE(grid_agents(j,i)%agents)
-                   ALLOCATE(grid_agents(j,i)%agents(1:new_size))
-
-                   grid_agents(j,i)%agents(1:noa)          = tmp_agents_s(1:noa)
-                   grid_agents(j,i)%agents(noa+1:new_size) = zero_agent
-
-                ELSE
-
-                   ALLOCATE(tmp_agents_d(noa))
-                   tmp_agents_d(1:noa) = grid_agents(j,i)%agents(1:noa)
-
-                   DEALLOCATE(grid_agents(j,i)%agents)
-                   ALLOCATE(grid_agents(j,i)%agents(new_size))
-
-                   grid_agents(j,i)%agents(1:noa)          = tmp_agents_d(1:noa)
-                   grid_agents(j,i)%agents(noa+1:new_size) = zero_agent
-
-                   DEALLOCATE(tmp_agents_d)
-
-                ENDIF
-
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_eh_dealloc_agents_array
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  i         !< grid index (x) of agent
+    INTEGER(iwp) ::  j         !< grid index (y) of agent
+    INTEGER(iwp) ::  new_size  !< new array size
+    INTEGER(iwp) ::  noa       !< number of agents
+    INTEGER(iwp) ::  old_size  !< old array size
+
+    LOGICAL ::  dealloc  !< flag that indicates if reallocation is necessary
+
+    TYPE(agent_type), DIMENSION(10) ::  tmp_agents_s  !< temporary static agent array
+
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  tmp_agents_d  !< temporary dynamic agent array
+
+    DO  i = nxlg, nxrg
+       DO  j = nysg, nyng
+!
+!--       Determine number of active agents
+          noa = agt_count(j,i)
+!
+!--       Determine allocated memory size
+          old_size = SIZE( grid_agents(j,i)%agents )
+!
+!--       Check for large unused memory
+          dealloc = ( ( noa < min_nr_agent .AND. old_size  > min_nr_agent )  .OR.                  &
+                      ( noa > min_nr_agent .AND.                                                   &
+                        old_size - noa * ( 1.0_wp + 0.01_wp * alloc_factor_mas ) > 0.0_wp )        &
+                    )
+!
+!--       If large unused memory was found, resize the corresponding array
+          IF ( dealloc )  THEN
+             IF ( noa < min_nr_agent )  THEN
+                new_size = min_nr_agent
+             ELSE
+                new_size = INT( noa * ( 1.0_wp + 0.01_wp * alloc_factor_mas ) )
              ENDIF
-          ENDDO
+
+             IF ( noa <= 10 )  THEN
+
+                tmp_agents_s(1:noa) = grid_agents(j,i)%agents(1:noa)
+
+                DEALLOCATE(grid_agents(j,i)%agents)
+                ALLOCATE(grid_agents(j,i)%agents(1:new_size))
+
+                grid_agents(j,i)%agents(1:noa)          = tmp_agents_s(1:noa)
+                grid_agents(j,i)%agents(noa+1:new_size) = zero_agent
+
+             ELSE
+
+                ALLOCATE(tmp_agents_d(noa))
+                tmp_agents_d(1:noa) = grid_agents(j,i)%agents(1:noa)
+
+                DEALLOCATE(grid_agents(j,i)%agents)
+                ALLOCATE(grid_agents(j,i)%agents(new_size))
+
+                grid_agents(j,i)%agents(1:noa)          = tmp_agents_d(1:noa)
+                grid_agents(j,i)%agents(noa+1:new_size) = zero_agent
+
+                DEALLOCATE(tmp_agents_d)
+
+             ENDIF
+
+          ENDIF
        ENDDO
-
-    END SUBROUTINE mas_eh_dealloc_agents_array
-
-!------------------------------------------------------------------------------!
+    ENDDO
+
+ END SUBROUTINE mas_eh_dealloc_agents_array
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Exchange between subdomains.
-!> As soon as one agent has moved beyond the boundary of the domain, it
-!> is included in the relevant transfer arrays and marked for subsequent
-!> deletion on this PE.
-!> First sweep for crossings in x direction. Find out first the number of
-!> agents to be transferred and allocate temporary arrays needed to store
-!> them.
-!> For a one-dimensional decomposition along y, no transfer is necessary,
-!> because the agent remains on the PE, but the agent coordinate has to
-!> be adjusted.
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_eh_exchange_horiz
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  ip               !< index variable along x
-       INTEGER(iwp) ::  jp               !< index variable along y
-       INTEGER(iwp) ::  n                !< agent index variable
+!> As soon as one agent has moved beyond the boundary of the domain, it is included in the relevant
+!> transfer arrays and marked for subsequent deletion on this PE.
+!> First sweep for crossings in x direction. Find out first the number of agents to be transferred
+!> and allocate temporary arrays needed to store them.
+!> For a one-dimensional decomposition along y, no transfer is necessary, because the agent remains
+!> on the PE, but the agent coordinate has to be adjusted.
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_eh_exchange_horiz
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  ip               !< index variable along x
+    INTEGER(iwp) ::  jp               !< index variable along y
+    INTEGER(iwp) ::  n                !< agent index variable
 
 #if defined( __parallel )
 
-       INTEGER(iwp) ::  i                !< grid index (x) of agent positition
-       INTEGER(iwp) ::  j                !< grid index (y) of agent positition
-       INTEGER(iwp) ::  par_size         !< Agent size in bytes
-
-       INTEGER(iwp) ::  trla_count       !< number of agents send to left PE
-       INTEGER(iwp) ::  trla_count_recv  !< number of agents receive from right PE
-       INTEGER(iwp) ::  trna_count       !< number of agents send to north PE
-       INTEGER(iwp) ::  trna_count_recv  !< number of agents receive from south PE
-       INTEGER(iwp) ::  trra_count       !< number of agents send to right PE
-       INTEGER(iwp) ::  trra_count_recv  !< number of agents receive from left PE
-       INTEGER(iwp) ::  trsa_count       !< number of agents send to south PE
-       INTEGER(iwp) ::  trsa_count_recv  !< number of agents receive from north PE
-
-       TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  rvla  !< agents received from right PE
-       TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  rvna  !< agents received from south PE
-       TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  rvra  !< agents received from left PE
-       TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  rvsa  !< agents received from north PE
-       TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  trla  !< agents send to left PE
-       TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  trna  !< agents send to north PE
-       TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  trra  !< agents send to right PE
-       TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  trsa  !< agents send to south PE
-
-!
-!--    Exchange between subdomains.
-!--    As soon as one agent has moved beyond the boundary of the domain, it
-!--    is included in the relevant transfer arrays and marked for subsequent
-!--    deletion on this PE.
-!--    First sweep for crossings in x direction. Find out first the number of
-!--    agents to be transferred and allocate temporary arrays needed to store
-!--    them.
-!--    For a one-dimensional decomposition along y, no transfer is necessary,
-!--    because the agent remains on the PE, but the agent coordinate has to
-!--    be adjusted.
-       trla_count  = 0
-       trra_count  = 0
-
-       trla_count_recv   = 0
-       trra_count_recv   = 0
-
-       IF ( pdims(1) /= 1 )  THEN
-!
-!--       First calculate the storage necessary for sending and receiving the data.
-!--       Compute only first (nxl) and last (nxr) loop iterration.
-          DO  ip = nxl, nxr, nxr - nxl
-             DO  jp = nys, nyn
-
-                number_of_agents = agt_count(jp,ip)
-                IF ( number_of_agents <= 0 )  CYCLE
-                agents => grid_agents(jp,ip)%agents(1:number_of_agents)
-                DO  n = 1, number_of_agents
-                   IF ( agents(n)%agent_mask )  THEN
-                      i = agents(n)%x * ddx
-!
-!--                   Above calculation does not work for indices less than zero
-                      IF ( agents(n)%x < 0.0_wp )  i = -1
-
-                      IF ( i < nxl )  THEN
-                         trla_count = trla_count + 1
-                      ELSEIF ( i > nxr )  THEN
-                         trra_count = trra_count + 1
-                      ENDIF
-                   ENDIF
-                ENDDO
-
-             ENDDO
-          ENDDO
-
-          IF ( trla_count  == 0 )  trla_count  = 1
-          IF ( trra_count  == 0 )  trra_count  = 1
-
-          ALLOCATE( trla(trla_count), trra(trra_count) )
-
-          trla = zero_agent
-          trra = zero_agent
-
-          trla_count  = 0
-          trra_count  = 0
-
-       ENDIF
-!
-!--    Compute only first (nxl) and last (nxr) loop iterration
-       DO  ip = nxl, nxr, nxr-nxl
+    INTEGER(iwp) ::  i                !< grid index (x) of agent positition
+    INTEGER(iwp) ::  j                !< grid index (y) of agent positition
+    INTEGER(iwp) ::  par_size         !< Agent size in bytes
+
+    INTEGER(iwp) ::  trla_count       !< number of agents send to left PE
+    INTEGER(iwp) ::  trla_count_recv  !< number of agents receive from right PE
+    INTEGER(iwp) ::  trna_count       !< number of agents send to north PE
+    INTEGER(iwp) ::  trna_count_recv  !< number of agents receive from south PE
+    INTEGER(iwp) ::  trra_count       !< number of agents send to right PE
+    INTEGER(iwp) ::  trra_count_recv  !< number of agents receive from left PE
+    INTEGER(iwp) ::  trsa_count       !< number of agents send to south PE
+    INTEGER(iwp) ::  trsa_count_recv  !< number of agents receive from north PE
+
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  rvla  !< agents received from right PE
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  rvna  !< agents received from south PE
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  rvra  !< agents received from left PE
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  rvsa  !< agents received from north PE
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  trla  !< agents send to left PE
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  trna  !< agents send to north PE
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  trra  !< agents send to right PE
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  trsa  !< agents send to south PE
+
+!
+!-- Exchange between subdomains.
+!-- As soon as one agent has moved beyond the boundary of the domain, it is included in the relevant
+!-- transfer arrays and marked for subsequent deletion on this PE.
+!-- First sweep for crossings in x direction. Find out first the number of agents to be transferred
+!-- and allocate temporary arrays needed to store them.
+!-- For a one-dimensional decomposition along y, no transfer is necessary, because the agent remains
+!-- on the PE, but the agent coordinate has to be adjusted.
+    trla_count  = 0
+    trra_count  = 0
+
+    trla_count_recv   = 0
+    trra_count_recv   = 0
+
+    IF ( pdims(1) /= 1 )  THEN
+!
+!--    First calculate the storage necessary for sending and receiving the data.
+!--    Compute only first (nxl) and last (nxr) loop iterration.
+       DO  ip = nxl, nxr, nxr - nxl
           DO  jp = nys, nyn
-             number_of_agents = agt_count(jp,ip)
-             IF ( number_of_agents <= 0 ) CYCLE
-             agents => grid_agents(jp,ip)%agents(1:number_of_agents)
-             DO  n = 1, number_of_agents
-!
-!--             Only those agents that have not been marked as 'deleted' may
-!--             be moved.
-                IF ( agents(n)%agent_mask )  THEN
-
-                   i = agents(n)%x * ddx
-!
-!--                Above calculation does not work for indices less than zero
-                   IF ( agents(n)%x < 0.0_wp )  i = -1
-
-                   IF ( i <  nxl )  THEN
-                      IF ( i < 0 )  THEN
-!
-!--                      Apply boundary condition along x
-                         IF ( ibc_mas_lr == 0 )  THEN
-!
-!--                         Cyclic condition
-                            IF ( pdims(1) == 1 )  THEN
-                               agents(n)%x        = ( nx + 1 ) * dx +          &
-                                                    agents(n)%x
-                               agents(n)%origin_x = ( nx + 1 ) * dx +          &
-                                                    agents(n)%origin_x
-                            ELSE
-                               trla_count         = trla_count + 1
-                               trla(trla_count)   = agents(n)
-                               trla(trla_count)%x = ( nx + 1 ) * dx +          &
-                                                    trla(trla_count)%x
-                               trla(trla_count)%origin_x =                     &
-                                                trla(trla_count)%origin_x +    &
-                                                ( nx + 1 ) * dx
-                               agents(n)%agent_mask  = .FALSE.
-                               deleted_agents = deleted_agents + 1
-
-                               IF ( trla(trla_count)%x >=                      &
-                                        (nx + 1)* dx - 1.0E-12_wp )            &
-                               THEN
-                                  trla(trla_count)%x = trla(trla_count)%x -    &
-                                                   1.0E-10_wp
-                                  trla(trla_count)%origin_x =                  &
-                                                   trla(trla_count)%origin_x - 1
-                               ENDIF
-
-                            ENDIF
-
-                         ELSEIF ( ibc_mas_lr == 1 )  THEN
-!
-!--                         Agent absorption
-                            agents(n)%agent_mask = .FALSE.
-                            deleted_agents = deleted_agents + 1
-
-                         ENDIF
-                      ELSE
-!
-!--                      Store agent data in the transfer array, which will be 
-!--                      send to the neighbouring PE
-                         trla_count = trla_count + 1
-                         trla(trla_count) = agents(n)
-                         agents(n)%agent_mask = .FALSE.
-                         deleted_agents = deleted_agents + 1
-
-                      ENDIF
-
-                   ELSEIF ( i > nxr )  THEN
-                      IF ( i > nx )  THEN
-!
-!--                      Apply boundary condition along x
-                         IF ( ibc_mas_lr == 0 )  THEN
-!
-!--                         Cyclic condition
-                            IF ( pdims(1) == 1 )  THEN
-                               agents(n)%x = agents(n)%x - ( nx + 1 ) * dx
-                               agents(n)%origin_x = agents(n)%origin_x - &
-                               ( nx + 1 ) * dx
-                            ELSE
-                               trra_count = trra_count + 1
-                               trra(trra_count) = agents(n)
-                               trra(trra_count)%x = trra(trra_count)%x -       &
-                                                    ( nx + 1 ) * dx
-                               trra(trra_count)%origin_x =                     &
-                                                   trra(trra_count)%origin_x - &
-                                                   ( nx + 1 ) * dx
-                               agents(n)%agent_mask = .FALSE.
-                               deleted_agents = deleted_agents + 1
-
-                            ENDIF
-
-                         ELSEIF ( ibc_mas_lr == 1 )  THEN
-!
-!--                         Agent absorption
-                            agents(n)%agent_mask = .FALSE.
-                            deleted_agents = deleted_agents + 1
-
-                         ENDIF
-                      ELSE
-!
-!--                      Store agent data in the transfer array, which will be send
-!--                      to the neighbouring PE
-                         trra_count = trra_count + 1
-                         trra(trra_count) = agents(n)
-                         agents(n)%agent_mask = .FALSE.
-                         deleted_agents = deleted_agents + 1
-
-                      ENDIF
-
-                   ENDIF
-                ENDIF
-
-             ENDDO
-          ENDDO
-       ENDDO
-
-!
-!--    Allocate arrays required for north-south exchange, as these
-!--    are used directly after agents are exchange along x-direction.
-       ALLOCATE( move_also_north(1:NR_2_direction_move) )
-       ALLOCATE( move_also_south(1:NR_2_direction_move) )
-
-       nr_move_north = 0
-       nr_move_south = 0
-!
-!--    Send left boundary, receive right boundary (but first exchange how many
-!--    and chec if agent storage must be extended)
-       IF ( pdims(1) /= 1 )  THEN
-
-          CALL MPI_SENDRECV( trla_count,      1, MPI_INTEGER, pleft,  0, &
-                             trra_count_recv, 1, MPI_INTEGER, pright, 0, &
-                             comm2d, status, ierr )
-
-          ALLOCATE(rvra(MAX(1,trra_count_recv)))
-!
-!--       This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit 
-!--       variables in structure agent_type (due to the calculation of par_size)
-          par_size = STORAGE_SIZE(trla(1))/8
-          CALL MPI_SENDRECV( trla, max(1,trla_count)*par_size, MPI_BYTE, pleft,&
-                    1, rvra, max(1,trra_count_recv)*par_size, MPI_BYTE, pright,&
-                             1, comm2d, status, ierr )
-
-          IF ( trra_count_recv > 0 ) THEN
-             CALL mas_eh_add_agents_to_gridcell(rvra(1:trra_count_recv))
-          ENDIF
-
-          DEALLOCATE(rvra)
-
-!
-!--       Send right boundary, receive left boundary
-          CALL MPI_SENDRECV( trra_count,      1, MPI_INTEGER, pright, 0,       &
-                             trla_count_recv, 1, MPI_INTEGER, pleft,  0,       &
-                             comm2d, status, ierr )
-
-          ALLOCATE(rvla(MAX(1,trla_count_recv)))
-!
-!--       This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit 
-!--       variables in structure agent_type (due to the calculation of par_size)
-          par_size = STORAGE_SIZE(trra(1))/8
-          CALL MPI_SENDRECV( trra, max(1,trra_count)*par_size, MPI_BYTE,       &
-                             pright, 1, rvla,                                  &
-                             max(1,trla_count_recv)*par_size, MPI_BYTE,        &
-                             pleft, 1, comm2d, status, ierr )
-
-          IF ( trla_count_recv > 0 ) THEN
-             CALL mas_eh_add_agents_to_gridcell(rvla(1:trla_count_recv))
-          ENDIF
-
-          DEALLOCATE( rvla )
-          DEALLOCATE( trla, trra )
-
-       ENDIF
-
-!
-!--    Check whether agents have crossed the boundaries in y direction. Note 
-!--    that this case can also apply to agents that have just been received
-!--    from the adjacent right or left PE.
-!--    Find out first the number of agents to be transferred and allocate
-!--    temporary arrays needed to store them.
-!--    For a one-dimensional decomposition along y, no transfer is necessary,
-!--    because the agent remains on the PE.
-       trsa_count  = nr_move_south
-       trna_count  = nr_move_north
-
-       trsa_count_recv   = 0
-       trna_count_recv   = 0
-
-       IF ( pdims(2) /= 1 )  THEN
-!
-!--       First calculate the storage necessary for sending and receiving the
-!--       data
-          DO  ip = nxl, nxr
-             DO  jp = nys, nyn, nyn-nys    !compute only first (nys) and last (nyn) loop iterration
-                number_of_agents = agt_count(jp,ip)
-                IF ( number_of_agents <= 0 )  CYCLE
-                agents => grid_agents(jp,ip)%agents(1:number_of_agents)
-                DO  n = 1, number_of_agents
-                   IF ( agents(n)%agent_mask )  THEN
-                      j = agents(n)%y * ddy
-!
-!--                   Above calculation does not work for indices less than zero
-                      IF ( agents(n)%y < 0.0_wp )  j = -1
-
-                      IF ( j < nys )  THEN
-                         trsa_count = trsa_count + 1
-                      ELSEIF ( j > nyn )  THEN
-                         trna_count = trna_count + 1
-                      ENDIF
-                   ENDIF
-                ENDDO
-             ENDDO
-          ENDDO
-
-          IF ( trsa_count  == 0 )  trsa_count  = 1
-          IF ( trna_count  == 0 )  trna_count  = 1
-
-          ALLOCATE( trsa(trsa_count), trna(trna_count) )
-
-          trsa = zero_agent
-          trna = zero_agent
-
-          trsa_count  = nr_move_south
-          trna_count  = nr_move_north
-
-          trsa(1:nr_move_south) = move_also_south(1:nr_move_south)
-          trna(1:nr_move_north) = move_also_north(1:nr_move_north)
-
-       ENDIF
-
-       DO  ip = nxl, nxr
-          DO  jp = nys, nyn, nyn-nys ! compute only first (nys) and last (nyn) loop iterration
+
              number_of_agents = agt_count(jp,ip)
              IF ( number_of_agents <= 0 )  CYCLE
              agents => grid_agents(jp,ip)%agents(1:number_of_agents)
              DO  n = 1, number_of_agents
-!
-!--             Only those agents that have not been marked as 'deleted' may
-!--             be moved.
                 IF ( agents(n)%agent_mask )  THEN
-
-                   j = agents(n)%y * ddy
+                   i = agents(n)%x * ddx
 !
 !--                Above calculation does not work for indices less than zero
-                   IF ( agents(n)%y < 0.0_wp * dy )  j = -1
-
-                   IF ( j < nys )  THEN
-                      IF ( j < 0 )  THEN
-!
-!--                      Apply boundary condition along y
-                         IF ( ibc_mas_ns == 0 )  THEN
-!
-!--                         Cyclic condition
-                            IF ( pdims(2) == 1 )  THEN
-                               agents(n)%y = ( ny + 1 ) * dy + agents(n)%y
-                               agents(n)%origin_y = ( ny + 1 ) * dy +          &
-                                                     agents(n)%origin_y
-                            ELSE
-                               trsa_count         = trsa_count + 1
-                               trsa(trsa_count)   = agents(n)
-                               trsa(trsa_count)%y = ( ny + 1 ) * dy +          &
-                                                 trsa(trsa_count)%y
-                               trsa(trsa_count)%origin_y =                     &
-                                                  trsa(trsa_count)%origin_y    &
-                                                + ( ny + 1 ) * dy
-                               agents(n)%agent_mask = .FALSE.
-                               deleted_agents = deleted_agents + 1
-
-                               IF ( trsa(trsa_count)%y >=                      &
-                                                    (ny+1)* dy - 1.0E-12_wp )  &
-                               THEN
-                                  trsa(trsa_count)%y = trsa(trsa_count)%y -    &
-                                                       1.0E-10_wp
-                                  trsa(trsa_count)%origin_y =                  &
-                                                  trsa(trsa_count)%origin_y - 1
-                               ENDIF
-
+                   IF ( agents(n)%x < 0.0_wp )  i = -1
+
+                   IF ( i < nxl )  THEN
+                      trla_count = trla_count + 1
+                   ELSEIF ( i > nxr )  THEN
+                      trra_count = trra_count + 1
+                   ENDIF
+                ENDIF
+             ENDDO
+
+          ENDDO
+       ENDDO
+
+       IF ( trla_count  == 0 )  trla_count  = 1
+       IF ( trra_count  == 0 )  trra_count  = 1
+
+       ALLOCATE( trla(trla_count), trra(trra_count) )
+
+       trla = zero_agent
+       trra = zero_agent
+
+       trla_count  = 0
+       trra_count  = 0
+
+    ENDIF
+!
+!-- Compute only first (nxl) and last (nxr) loop iterration
+    DO  ip = nxl, nxr, nxr-nxl
+       DO  jp = nys, nyn
+          number_of_agents = agt_count(jp,ip)
+          IF ( number_of_agents <= 0 ) CYCLE
+          agents => grid_agents(jp,ip)%agents(1:number_of_agents)
+          DO  n = 1, number_of_agents
+!
+!--          Only those agents that have not been marked as 'deleted' may be moved.
+             IF ( agents(n)%agent_mask )  THEN
+
+                i = agents(n)%x * ddx
+!
+!--             Above calculation does not work for indices less than zero
+                IF ( agents(n)%x < 0.0_wp )  i = -1
+
+                IF ( i <  nxl )  THEN
+                   IF ( i < 0 )  THEN
+!
+!--                   Apply boundary condition along x
+                      IF ( ibc_mas_lr == 0 )  THEN
+!
+!--                      Cyclic condition
+                         IF ( pdims(1) == 1 )  THEN
+                            agents(n)%x        = ( nx + 1 ) * dx + agents(n)%x
+                            agents(n)%origin_x = ( nx + 1 ) * dx + agents(n)%origin_x
+                         ELSE
+                            trla_count         = trla_count + 1
+                            trla(trla_count)   = agents(n)
+                            trla(trla_count)%x = ( nx + 1 ) * dx + trla(trla_count)%x
+                            trla(trla_count)%origin_x = trla(trla_count)%origin_x + ( nx + 1 ) * dx
+                            agents(n)%agent_mask  = .FALSE.
+                            deleted_agents = deleted_agents + 1
+
+                            IF ( trla(trla_count)%x >= (nx + 1)* dx - 1.0E-12_wp )  THEN
+                               trla(trla_count)%x = trla(trla_count)%x - 1.0E-10_wp
+                               trla(trla_count)%origin_x = trla(trla_count)%origin_x - 1
                             ENDIF
 
-                         ELSEIF ( ibc_mas_ns == 1 )  THEN
-!
-!--                         Agent absorption
-                            agents(n)%agent_mask = .FALSE.
-                            deleted_agents          = deleted_agents + 1
-
                          ENDIF
-                      ELSE
-!
-!--                      Store agent data in the transfer array, which will 
-!--                      be send to the neighbouring PE
-                         trsa_count = trsa_count + 1
-                         trsa(trsa_count) = agents(n)
+
+                      ELSEIF ( ibc_mas_lr == 1 )  THEN
+!
+!--                      Agent absorption
                          agents(n)%agent_mask = .FALSE.
                          deleted_agents = deleted_agents + 1
 
                       ENDIF
-
-                   ELSEIF ( j > nyn )  THEN
-                      IF ( j > ny )  THEN
-!
-!--                      Apply boundary condition along y
-                         IF ( ibc_mas_ns == 0 )  THEN
-!
-!--                         Cyclic condition
-                            IF ( pdims(2) == 1 )  THEN
-                               agents(n)%y        = agents(n)%y -              &
-                                                    ( ny + 1 ) * dy
-                               agents(n)%origin_y = agents(n)%origin_y -       &
-                                                    ( ny + 1 ) * dy
-                            ELSE
-                               trna_count         = trna_count + 1
-                               trna(trna_count)   = agents(n)
-                               trna(trna_count)%y =                            &
-                                          trna(trna_count)%y - ( ny + 1 ) * dy
-                               trna(trna_count)%origin_y =                     &
-                                         trna(trna_count)%origin_y -           &
-                                         ( ny + 1 ) * dy
-                               agents(n)%agent_mask = .FALSE.
-                               deleted_agents          = deleted_agents + 1
-                            ENDIF
-
-                         ELSEIF ( ibc_mas_ns == 1 )  THEN
-!
-!--                         Agent absorption
+                   ELSE
+!
+!--                   Store agent data in the transfer array, which will be send to the neighbouring
+!--                   PE.
+                      trla_count = trla_count + 1
+                      trla(trla_count) = agents(n)
+                      agents(n)%agent_mask = .FALSE.
+                      deleted_agents = deleted_agents + 1
+
+                   ENDIF
+
+                ELSEIF ( i > nxr )  THEN
+                   IF ( i > nx )  THEN
+!
+!--                   Apply boundary condition along x
+                      IF ( ibc_mas_lr == 0 )  THEN
+!
+!--                      Cyclic condition
+                         IF ( pdims(1) == 1 )  THEN
+                            agents(n)%x = agents(n)%x - ( nx + 1 ) * dx
+                            agents(n)%origin_x = agents(n)%origin_x - ( nx + 1 ) * dx
+                         ELSE
+                            trra_count = trra_count + 1
+                            trra(trra_count) = agents(n)
+                            trra(trra_count)%x = trra(trra_count)%x - ( nx + 1 ) * dx
+                            trra(trra_count)%origin_x = trra(trra_count)%origin_x - ( nx + 1 ) * dx
                             agents(n)%agent_mask = .FALSE.
                             deleted_agents = deleted_agents + 1
 
                          ENDIF
-                      ELSE
-!
-!--                      Store agent data in the transfer array, which will 
-!--                      be send to the neighbouring PE
-                         trna_count = trna_count + 1
-                         trna(trna_count) = agents(n)
+
+                      ELSEIF ( ibc_mas_lr == 1 )  THEN
+!
+!--                      Agent absorption
                          agents(n)%agent_mask = .FALSE.
                          deleted_agents = deleted_agents + 1
 
                       ENDIF
-
+                   ELSE
+!
+!--                   Store agent data in the transfer array, which will be send to the neighbouring
+!--                   PE.
+                      trra_count = trra_count + 1
+                      trra(trra_count) = agents(n)
+                      agents(n)%agent_mask = .FALSE.
+                      deleted_agents = deleted_agents + 1
+
+                   ENDIF
+
+                ENDIF
+             ENDIF
+
+          ENDDO
+       ENDDO
+    ENDDO
+
+!
+!-- Allocate arrays required for north-south exchange, as these are used directly after agents are
+!-- exchange along x-direction.
+    ALLOCATE( move_also_north(1:nr_2_direction_move) )
+    ALLOCATE( move_also_south(1:nr_2_direction_move) )
+
+    nr_move_north = 0
+    nr_move_south = 0
+!
+!-- Send left boundary, receive right boundary (but first exchange how many and check if agent
+!-- storage must be extended).
+    IF ( pdims(1) /= 1 )  THEN
+
+       CALL MPI_SENDRECV( trla_count,      1, MPI_INTEGER, pleft,  0,                              &
+                          trra_count_recv, 1, MPI_INTEGER, pright, 0,                              &
+                          comm2d, status, ierr )
+
+       ALLOCATE( rvra(MAX( 1,trra_count_recv )) )
+!
+!--    This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit variables in structure
+!--    agent_type (due to the calculation of par_size)
+       par_size = STORAGE_SIZE( trla(1) ) / 8
+       CALL MPI_SENDRECV( trla, MAX( 1, trla_count ) * par_size,     MPI_BYTE, pleft,  1,          &
+                          rvra, MAX( 1, trra_count_recv )* par_size, MPI_BYTE, pright, 1,          &
+                          comm2d, status, ierr )
+
+       IF ( trra_count_recv > 0 )  THEN
+          CALL mas_eh_add_agents_to_gridcell(rvra(1:trra_count_recv))
+       ENDIF
+
+       DEALLOCATE(rvra)
+
+!
+!--    Send right boundary, receive left boundary
+       CALL MPI_SENDRECV( trra_count,      1, MPI_INTEGER, pright, 0,                              &
+                          trla_count_recv, 1, MPI_INTEGER, pleft,  0,                              &
+                          comm2d, status, ierr )
+
+       ALLOCATE(rvla(MAX(1,trla_count_recv)))
+!
+!--    This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit variables in structure
+!--    agent_type (due to the calculation of par_size)
+       par_size = STORAGE_SIZE(trra(1))/8
+       CALL MPI_SENDRECV( trra, MAX( 1, trra_count ) * par_size,   MPI_BYTE, pright, 1,            &
+                          rvla, MAX(1,trla_count_recv) * par_size, MPI_BYTE,  pleft, 1,            &
+                          comm2d, status, ierr )
+
+       IF ( trla_count_recv > 0 )  THEN
+          CALL mas_eh_add_agents_to_gridcell(rvla(1:trla_count_recv))
+       ENDIF
+
+       DEALLOCATE( rvla )
+       DEALLOCATE( trla, trra )
+
+    ENDIF
+
+!
+!-- Check whether agents have crossed the boundaries in y direction. Note that this case can also
+!-- apply to agents that have just been received from the adjacent right or left PE.
+!-- Find out first the number of agents to be transferred and allocate temporary arrays needed to
+!-- store them.
+!-- For a one-dimensional decomposition along y, no transfer is necessary, because the agent remains
+!-- on the PE.
+    trsa_count  = nr_move_south
+    trna_count  = nr_move_north
+
+    trsa_count_recv   = 0
+    trna_count_recv   = 0
+
+    IF ( pdims(2) /= 1 )  THEN
+!
+!--    First calculate the storage necessary for sending and receiving the data.
+       DO  ip = nxl, nxr
+          DO  jp = nys, nyn, nyn-nys    ! compute only first (nys) and last (nyn) loop iterration
+             number_of_agents = agt_count(jp,ip)
+             IF ( number_of_agents <= 0 )  CYCLE
+             agents => grid_agents(jp,ip)%agents(1:number_of_agents)
+             DO  n = 1, number_of_agents
+                IF ( agents(n)%agent_mask )  THEN
+                   j = agents(n)%y * ddy
+!
+!--                Above calculation does not work for indices less than zero
+                   IF ( agents(n)%y < 0.0_wp )  j = -1
+
+                   IF ( j < nys )  THEN
+                      trsa_count = trsa_count + 1
+                   ELSEIF ( j > nyn )  THEN
+                      trna_count = trna_count + 1
                    ENDIF
                 ENDIF
@@ -2195,536 +1985,631 @@
        ENDDO
 
-!
-!--    Send front boundary, receive back boundary (but first exchange how many
-!--    and chec if agent storage must be extended)
-       IF ( pdims(2) /= 1 )  THEN
-
-          CALL MPI_SENDRECV( trsa_count,      1, MPI_INTEGER, psouth, 0,       &
-                             trna_count_recv, 1, MPI_INTEGER, pnorth, 0,       &
-                             comm2d, status, ierr )
-
-          ALLOCATE(rvna(MAX(1,trna_count_recv)))
-!
-!--       This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit 
-!--       variables in structure agent_type (due to the calculation of par_size)
-          par_size = STORAGE_SIZE(trsa(1))/8
-          CALL MPI_SENDRECV( trsa, trsa_count*par_size, MPI_BYTE,              &
-                             psouth, 1, rvna,                                  &
-                             trna_count_recv*par_size, MPI_BYTE, pnorth, 1,    &
-                             comm2d, status, ierr )
-
-          IF ( trna_count_recv  > 0 ) THEN
-             CALL mas_eh_add_agents_to_gridcell(rvna(1:trna_count_recv))
-          ENDIF
-
-          DEALLOCATE(rvna)
-
-!
-!--       Send back boundary, receive front boundary
-          CALL MPI_SENDRECV( trna_count,      1, MPI_INTEGER, pnorth, 0,       &
-                             trsa_count_recv, 1, MPI_INTEGER, psouth, 0,       &
-                             comm2d, status, ierr )
-
-          ALLOCATE(rvsa(MAX(1,trsa_count_recv)))
-!
-!--       This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit 
-!--       variables in structure agent_type (due to the calculation of par_size)
-          par_size = STORAGE_SIZE(trna(1))/8
-          CALL MPI_SENDRECV( trna, trna_count*par_size, MPI_BYTE,              &
-                             pnorth, 1, rvsa,                                  &
-                             trsa_count_recv*par_size, MPI_BYTE, psouth, 1,    &
-                             comm2d, status, ierr )
-
-          IF ( trsa_count_recv > 0 ) THEN
-             CALL mas_eh_add_agents_to_gridcell(rvsa(1:trsa_count_recv))
-          ENDIF
-
-          DEALLOCATE(rvsa)
-
-          number_of_agents = number_of_agents + trsa_count_recv
-
-          DEALLOCATE( trsa, trna )
-
-       ENDIF
-
-       DEALLOCATE( move_also_north )
-       DEALLOCATE( move_also_south )
-
-!
-!--    Accumulate the number of agents transferred between the subdomains)
-       CALL mas_eh_ghost_exchange
-
-#else
-
-       DO  ip = nxl, nxr, nxr-nxl
-          DO  jp = nys, nyn
-             number_of_agents = agt_count(jp,ip)
-             IF ( number_of_agents <= 0 )  CYCLE
-             agents => grid_agents(jp,ip)%agents(1:number_of_agents)
-             DO  n = 1, number_of_agents
-!
-!--             Apply boundary conditions
-                IF ( agents(n)%x < 0.0_wp )  THEN
-
-                   IF ( ibc_mas_lr == 0 )  THEN
-!
-!--                   Cyclic boundary. Relevant coordinate has to be changed.
-                      agents(n)%x = ( nx + 1 ) * dx + agents(n)%x
-                      agents(n)%origin_x = ( nx + 1 ) * dx + &
-                                  agents(n)%origin_x
-                   ELSEIF ( ibc_mas_lr == 1 )  THEN
-!
-!--                   Agent absorption
+       IF ( trsa_count  == 0 )  trsa_count  = 1
+       IF ( trna_count  == 0 )  trna_count  = 1
+
+       ALLOCATE( trsa(trsa_count), trna(trna_count) )
+
+       trsa = zero_agent
+       trna = zero_agent
+
+       trsa_count  = nr_move_south
+       trna_count  = nr_move_north
+
+       trsa(1:nr_move_south) = move_also_south(1:nr_move_south)
+       trna(1:nr_move_north) = move_also_north(1:nr_move_north)
+
+    ENDIF
+
+    DO  ip = nxl, nxr
+       DO  jp = nys, nyn, nyn-nys ! compute only first (nys) and last (nyn) loop iterration
+          number_of_agents = agt_count(jp,ip)
+          IF ( number_of_agents <= 0 )  CYCLE
+          agents => grid_agents(jp,ip)%agents(1:number_of_agents)
+          DO  n = 1, number_of_agents
+!
+!--          Only those agents that have not been marked as 'deleted' may be moved.
+             IF ( agents(n)%agent_mask )  THEN
+
+                j = agents(n)%y * ddy
+!
+!--             Above calculation does not work for indices less than zero
+                IF ( agents(n)%y < 0.0_wp * dy )  j = -1
+
+                IF ( j < nys )  THEN
+                   IF ( j < 0 )  THEN
+!
+!--                   Apply boundary condition along y
+                      IF ( ibc_mas_ns == 0 )  THEN
+!
+!--                      Cyclic condition
+                         IF ( pdims(2) == 1 )  THEN
+                            agents(n)%y = ( ny + 1 ) * dy + agents(n)%y
+                            agents(n)%origin_y = ( ny + 1 ) * dy + agents(n)%origin_y
+                         ELSE
+                            trsa_count         = trsa_count + 1
+                            trsa(trsa_count)   = agents(n)
+                            trsa(trsa_count)%y = ( ny + 1 ) * dy + trsa(trsa_count)%y
+                            trsa(trsa_count)%origin_y = trsa(trsa_count)%origin_y + ( ny + 1 ) * dy
+                            agents(n)%agent_mask = .FALSE.
+                            deleted_agents = deleted_agents + 1
+
+                            IF ( trsa(trsa_count)%y >= (ny+1)* dy - 1.0E-12_wp )  THEN
+                               trsa(trsa_count)%y = trsa(trsa_count)%y - 1.0E-10_wp
+                               trsa(trsa_count)%origin_y = trsa(trsa_count)%origin_y - 1
+                            ENDIF
+
+                         ENDIF
+
+                      ELSEIF ( ibc_mas_ns == 1 )  THEN
+!
+!--                      Agent absorption
+                         agents(n)%agent_mask = .FALSE.
+                         deleted_agents          = deleted_agents + 1
+
+                      ENDIF
+                   ELSE
+!
+!--                   Store agent data in the transfer array, which will be send to the neighbouring
+!--                   PE.
+                      trsa_count = trsa_count + 1
+                      trsa(trsa_count) = agents(n)
                       agents(n)%agent_mask = .FALSE.
                       deleted_agents = deleted_agents + 1
+
                    ENDIF
 
-                ELSEIF ( agents(n)%x >= ( nx + 1 ) * dx )  THEN
-
-                   IF ( ibc_mas_lr == 0 )  THEN
-!
-!--                   Cyclic boundary. Relevant coordinate has to be changed.
-                      agents(n)%x = agents(n)%x - ( nx + 1 ) * dx
-
-                   ELSEIF ( ibc_mas_lr == 1 )  THEN
-!
-!--                   Agent absorption
+                ELSEIF ( j > nyn )  THEN
+                   IF ( j > ny )  THEN
+!
+!--                   Apply boundary condition along y
+                      IF ( ibc_mas_ns == 0 )  THEN
+!
+!--                      Cyclic condition
+                         IF ( pdims(2) == 1 )  THEN
+                            agents(n)%y        = agents(n)%y        - ( ny + 1 ) * dy
+                            agents(n)%origin_y = agents(n)%origin_y - ( ny + 1 ) * dy
+                         ELSE
+                            trna_count         = trna_count + 1
+                            trna(trna_count)   = agents(n)
+                            trna(trna_count)%y = trna(trna_count)%y - ( ny + 1 ) * dy
+                            trna(trna_count)%origin_y = trna(trna_count)%origin_y - ( ny + 1 ) * dy
+                            agents(n)%agent_mask = .FALSE.
+                            deleted_agents          = deleted_agents + 1
+                         ENDIF
+
+                      ELSEIF ( ibc_mas_ns == 1 )  THEN
+!
+!--                      Agent absorption
+                         agents(n)%agent_mask = .FALSE.
+                         deleted_agents = deleted_agents + 1
+
+                      ENDIF
+                   ELSE
+!
+!--                   Store agent data in the transfer array, which will be send to the neighbouring
+!--                   PE
+                      trna_count = trna_count + 1
+                      trna(trna_count) = agents(n)
                       agents(n)%agent_mask = .FALSE.
                       deleted_agents = deleted_agents + 1
+
                    ENDIF
 
+                ENDIF
+             ENDIF
+          ENDDO
+       ENDDO
+    ENDDO
+
+!
+!-- Send front boundary, receive back boundary (but first exchange how many and check if agent
+!-- storage must be extended).
+    IF ( pdims(2) /= 1 )  THEN
+
+       CALL MPI_SENDRECV( trsa_count,      1, MPI_INTEGER, psouth, 0,                              &
+                          trna_count_recv, 1, MPI_INTEGER, pnorth, 0,                              &
+                          comm2d, status, ierr )
+
+       ALLOCATE( rvna( MAX( 1, trna_count_recv )) )
+!
+!--    This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit variables in structure
+!--    agent_type (due to the calculation of par_size)
+       par_size = STORAGE_SIZE(trsa(1))/8
+       CALL MPI_SENDRECV( trsa, trsa_count * par_size,      MPI_BYTE, psouth, 1,                   &
+                          rvna, trna_count_recv * par_size, MPI_BYTE, pnorth, 1,                   &
+                          comm2d, status, ierr )
+
+       IF ( trna_count_recv  > 0 )  THEN
+          CALL mas_eh_add_agents_to_gridcell(rvna(1:trna_count_recv))
+       ENDIF
+
+       DEALLOCATE( rvna )
+
+!
+!--    Send back boundary, receive front boundary
+       CALL MPI_SENDRECV( trna_count,      1, MPI_INTEGER, pnorth, 0,                              &
+                          trsa_count_recv, 1, MPI_INTEGER, psouth, 0,                              &
+                          comm2d, status, ierr )
+
+       ALLOCATE( rvsa( MAX( 1, trsa_count_recv )) )
+!
+!--    This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit variables in structure
+!--    agent_type (due to the calculation of par_size)
+       par_size = STORAGE_SIZE( trna(1) ) / 8
+       CALL MPI_SENDRECV( trna, trna_count * par_size,      MPI_BYTE, pnorth, 1,                   &
+                          rvsa, trsa_count_recv * par_size, MPI_BYTE, psouth, 1,                   &
+                          comm2d, status, ierr )
+
+       IF ( trsa_count_recv > 0 )  THEN
+          CALL mas_eh_add_agents_to_gridcell(rvsa(1:trsa_count_recv))
+       ENDIF
+
+       DEALLOCATE( rvsa )
+
+       number_of_agents = number_of_agents + trsa_count_recv
+
+       DEALLOCATE( trsa, trna )
+
+    ENDIF
+
+    DEALLOCATE( move_also_north )
+    DEALLOCATE( move_also_south )
+
+!
+!-- Accumulate the number of agents transferred between the subdomains)
+    CALL mas_eh_ghost_exchange
+
+#else
+
+    DO  ip = nxl, nxr, nxr-nxl
+       DO  jp = nys, nyn
+          number_of_agents = agt_count(jp,ip)
+          IF ( number_of_agents <= 0 )  CYCLE
+          agents => grid_agents(jp,ip)%agents(1:number_of_agents)
+          DO  n = 1, number_of_agents
+!
+!--          Apply boundary conditions
+             IF ( agents(n)%x < 0.0_wp )  THEN
+
+                IF ( ibc_mas_lr == 0 )  THEN
+!
+!--                Cyclic boundary. Relevant coordinate has to be changed.
+                   agents(n)%x = ( nx + 1 ) * dx + agents(n)%x
+                   agents(n)%origin_x = ( nx + 1 ) * dx + agents(n)%origin_x
+                ELSEIF ( ibc_mas_lr == 1 )  THEN
+!
+!--                Agent absorption
+                   agents(n)%agent_mask = .FALSE.
+                   deleted_agents = deleted_agents + 1
+                ENDIF
+
+             ELSEIF ( agents(n)%x >= ( nx + 1 ) * dx )  THEN
+
+                IF ( ibc_mas_lr == 0 )  THEN
+!
+!--                Cyclic boundary. Relevant coordinate has to be changed.
+                   agents(n)%x = agents(n)%x - ( nx + 1 ) * dx
+
+                ELSEIF ( ibc_mas_lr == 1 )  THEN
+!
+!--                Agent absorption
+                   agents(n)%agent_mask = .FALSE.
+                   deleted_agents = deleted_agents + 1
+                ENDIF
+
+             ENDIF
+          ENDDO
+       ENDDO
+    ENDDO
+
+    DO  ip = nxl, nxr
+       DO  jp = nys, nyn, nyn-nys
+          number_of_agents = agt_count(jp,ip)
+          IF ( number_of_agents <= 0 )  CYCLE
+          agents => grid_agents(jp,ip)%agents(1:number_of_agents)
+          DO  n = 1, number_of_agents
+
+             IF ( agents(n)%y < 0.0_wp )  THEN
+
+                IF ( ibc_mas_ns == 0 )  THEN
+!
+!--                Cyclic boundary. Relevant coordinate has to be changed.
+                   agents(n)%y = ( ny + 1 ) * dy + agents(n)%y
+                   agents(n)%origin_y = ( ny + 1 ) * dy + &
+                        agents(n)%origin_y
+
+                ELSEIF ( ibc_mas_ns == 1 )  THEN
+!
+!--                Agent absorption
+                   agents(n)%agent_mask = .FALSE.
+                   deleted_agents = deleted_agents + 1
+                ENDIF
+
+             ELSEIF ( agents(n)%y >= ( ny + 0.5_wp ) * dy )  THEN
+
+                IF ( ibc_mas_ns == 0 )  THEN
+!
+!--                Cyclic boundary. Relevant coordinate has to be changed.
+                   agents(n)%y = agents(n)%y - ( ny + 1 ) * dy
+
+                ELSEIF ( ibc_mas_ns == 1 )  THEN
+!
+!--                Agent absorption
+                   agents(n)%agent_mask = .FALSE.
+                   deleted_agents = deleted_agents + 1
+                ENDIF
+
+             ENDIF
+
+          ENDDO
+       ENDDO
+    ENDDO
+#endif
+
+ END SUBROUTINE mas_eh_exchange_horiz
+
+
+#if defined( __parallel )
+!--------------------------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Sends the agents from the three gridcells closest to the north/south/left/right border of a PE to
+!> the corresponding neighbors ghost layer (which is three grid boxes deep)
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_eh_ghost_exchange
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  agt_size    !< Bit size of agent datatype
+    INTEGER(iwp) ::  ghla_count  !< ghost points left agent
+    INTEGER(iwp) ::  ghna_count  !< ghost points north agent
+    INTEGER(iwp) ::  ghra_count  !< ghost points right agent
+    INTEGER(iwp) ::  ghsa_count  !< ghost points south agent
+    INTEGER(iwp) ::  ip          !< index variable along x
+    INTEGER(iwp) ::  jp          !< index variable along y
+
+    LOGICAL ::  ghla_empty      !< ghost points left agent
+    LOGICAL ::  ghla_empty_rcv  !< ghost points left agent
+    LOGICAL ::  ghna_empty      !< ghost points north agent
+    LOGICAL ::  ghna_empty_rcv  !< ghost points north agent
+    LOGICAL ::  ghra_empty      !< ghost points right agent
+    LOGICAL ::  ghra_empty_rcv  !< ghost points right agent
+    LOGICAL ::  ghsa_empty      !< ghost points south agent
+    LOGICAL ::  ghsa_empty_rcv  !< ghost points south agent
+
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  ghla  !< agents received from right PE
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  ghna  !< agents received from south PE
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  ghra  !< agents received from left PE
+    TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  ghsa  !< agents received from north PE
+
+    ghla_empty = .TRUE.
+    ghna_empty = .TRUE.
+    ghra_empty = .TRUE.
+    ghsa_empty = .TRUE.
+!
+!-- Reset ghost layer
+    DO  ip = nxlg, nxl-1
+       DO  jp = nysg, nyng
+          agt_count(jp,ip) = 0
+       ENDDO
+    ENDDO
+    DO  ip = nxr+1, nxrg
+       DO  jp = nysg, nyng
+          agt_count(jp,ip) = 0
+       ENDDO
+    ENDDO
+    DO  ip = nxl, nxr
+       DO  jp = nysg, nys-1
+          agt_count(jp,ip) = 0
+       ENDDO
+    ENDDO
+    DO  ip = nxl, nxr
+       DO  jp = nyn+1, nyng
+          agt_count(jp,ip) = 0
+       ENDDO
+    ENDDO
+!
+!-- Transfer of agents from left to right and vice versa
+    IF ( pdims(1) /= 1 )  THEN
+!
+!--    Reset left and right ghost layers
+       ghla_count  = 0
+       ghra_count  = 0
+!
+!--    First calculate the storage necessary for sending and receiving the data.
+       ghla_count = SUM(agt_count(nys:nyn,nxl:nxl+2))
+       ghra_count = SUM(agt_count(nys:nyn,nxr-2:nxr))
+!
+!--    No cyclic boundaries for agents
+       IF ( nxl == 0  .OR.  ghla_count == 0 )  THEN
+          ghla_count = 1
+       ELSE
+          ghla_empty = .FALSE.
+       ENDIF
+       IF ( nxr == nx  .OR.  ghra_count == 0 )  THEN
+          ghra_count = 1
+       ELSE
+          ghra_empty = .FALSE.
+       ENDIF
+       ALLOCATE( ghla(1:ghla_count), ghra(1:ghra_count) )
+       ghla = zero_agent
+       ghra = zero_agent
+!
+!--    Get all agents that will be sent left into one array
+       ghla_count = 0
+       IF ( nxl /= 0 )  THEN
+          DO  ip = nxl, nxl+2
+             DO jp = nys, nyn
+
+                number_of_agents = agt_count(jp,ip)
+                IF ( number_of_agents <= 0 )  CYCLE
+                ghla(ghla_count+1:ghla_count+number_of_agents)                                     &
+                                                     = grid_agents(jp,ip)%agents(1:number_of_agents)
+                ghla_count = ghla_count + number_of_agents
+
+             ENDDO
+          ENDDO
+       ENDIF
+       IF ( ghla_count == 0 )  ghla_count = 1
+!
+!--    Get all agents that will be sent right into one array
+       ghra_count = 0
+       IF ( nxr /= nx )  THEN
+          DO  ip = nxr-2, nxr
+             DO  jp = nys, nyn
+
+                number_of_agents = agt_count(jp,ip)
+                IF ( number_of_agents <= 0 )  CYCLE
+                ghra(ghra_count+1:ghra_count+number_of_agents)                                     &
+                                                   = grid_agents(jp,ip)%agents(1:number_of_agents)
+                ghra_count = ghra_count + number_of_agents
+
+             ENDDO
+          ENDDO
+       ENDIF
+       IF ( ghra_count == 0 ) ghra_count = 1
+!
+!--    Send/receive number of agents that will be transferred to/from left/right neighbor.
+       CALL MPI_SENDRECV( ghla_count,      1, MPI_INTEGER, pleft,  0,                              &
+                          ghra_count_recv, 1, MPI_INTEGER, pright, 0,                              &
+                          comm2d, status, ierr )
+       ALLOCATE ( agt_gh_r(1:ghra_count_recv) )
+!
+!--    Send/receive number of agents that will be transferred to/from right/left neighbor
+       CALL MPI_SENDRECV( ghra_count,      1, MPI_INTEGER, pright,  0,                             &
+                          ghla_count_recv, 1, MPI_INTEGER, pleft,   0,                             &
+                          comm2d, status, ierr )
+!
+!--    Send/receive flag that indicates if there are actually any agents in ghost layer
+       CALL MPI_SENDRECV( ghla_empty,     1, MPI_LOGICAL, pleft, 1,                                &
+                          ghra_empty_rcv, 1, MPI_LOGICAL, pright,1,                                &
+                          comm2d, status, ierr )
+       CALL MPI_SENDRECV( ghra_empty,     1, MPI_LOGICAL, pright,1,                                &
+                          ghla_empty_rcv, 1, MPI_LOGICAL, pleft, 1,                                &
+                          comm2d, status, ierr )
+
+
+       ALLOCATE ( agt_gh_l(1:ghla_count_recv) )
+!
+!--    Get bit size of one agent
+       agt_size = STORAGE_SIZE(zero_agent)/8
+!
+!--    Send/receive agents to/from left/right neighbor
+       CALL MPI_SENDRECV( ghla,     ghla_count      * agt_size, MPI_BYTE, pleft, 1,                &
+                          agt_gh_r, ghra_count_recv * agt_size, MPI_BYTE, pright,1,                &
+                          comm2d, status, ierr )
+!
+!--    Send/receive agents to/from left/right neighbor
+       CALL MPI_SENDRECV( ghra,     ghra_count      * agt_size, MPI_BYTE, pright,1,                &
+                          agt_gh_l, ghla_count_recv * agt_size, MPI_BYTE, pleft, 1,                &
+                          comm2d, status, ierr )
+!
+!--    If agents were received, add them to the respective ghost layer cells
+       IF ( .NOT. ghra_empty_rcv )  THEN
+          CALL mas_eh_add_ghost_agents_to_gridcell(agt_gh_r)
+       ENDIF
+
+       IF ( .NOT. ghla_empty_rcv )  THEN
+          CALL mas_eh_add_ghost_agents_to_gridcell(agt_gh_l)
+       ENDIF
+
+       DEALLOCATE( ghla, ghra, agt_gh_l, agt_gh_r )
+
+    ENDIF
+
+!
+!-- Transfer of agents from south to north and vice versa
+    IF ( pdims(2) /= 1 )  THEN
+!
+!--    Reset south and north ghost layers
+       ghsa_count  = 0
+       ghna_count  = 0
+!
+!--    First calculate the storage necessary for sending and receiving the data.
+       ghsa_count = SUM( agt_count(nys:nys+2,nxlg:nxrg) )
+       ghna_count = SUM( agt_count(nyn-2:nyn,nxlg:nxrg) )
+!
+!--    No cyclic boundaries for agents
+       IF ( nys == 0  .OR.  ghsa_count == 0 )  THEN
+          ghsa_count = 1
+       ELSE
+          ghsa_empty = .FALSE.
+       ENDIF
+       IF ( nyn == ny  .OR.  ghna_count == 0 )  THEN
+          ghna_count = 1
+       ELSE
+          ghna_empty = .FALSE.
+       ENDIF
+       ALLOCATE( ghsa(1:ghsa_count), ghna(1:ghna_count) )
+       ghsa = zero_agent
+       ghna = zero_agent
+!
+!--    Get all agents that will be sent south into one array
+       ghsa_count = 0
+       IF ( nys /= 0 )  THEN
+          DO  ip = nxlg, nxrg
+             DO  jp = nys, nys+2
+
+                number_of_agents = agt_count(jp,ip)
+                IF ( number_of_agents <= 0 )  CYCLE
+                ghsa(ghsa_count+1:ghsa_count+number_of_agents)                                     &
+                                                    = grid_agents(jp,ip)%agents(1:number_of_agents)
+                ghsa_count = ghsa_count + number_of_agents
+
+             ENDDO
+          ENDDO
+       ENDIF
+       IF ( ghsa_count == 0 )  ghsa_count = 1
+!
+!--    Get all agents that will be sent north into one array
+       ghna_count = 0
+       IF ( nyn /= ny )  THEN
+          DO  ip = nxlg, nxrg
+             DO  jp = nyn-2, nyn
+
+                number_of_agents = agt_count(jp,ip)
+                IF ( number_of_agents <= 0 )  CYCLE
+                ghna(ghna_count+1:ghna_count+number_of_agents)                                     &
+                                                     = grid_agents(jp,ip)%agents(1:number_of_agents)
+                ghna_count = ghna_count + number_of_agents
+
+             ENDDO
+          ENDDO
+       ENDIF
+       IF ( ghna_count == 0 ) ghna_count = 1
+!
+!--    Send/receive number of agents that will be transferred to/from south/north neighbor
+       CALL MPI_SENDRECV( ghsa_count,        1, MPI_INTEGER, psouth, 0,                            &
+                          ghna_count_recv,   1, MPI_INTEGER, pnorth, 0,                            &
+                          comm2d, status, ierr )
+       ALLOCATE ( agt_gh_n(1:ghna_count_recv) )
+!
+!--    Send/receive number of agents that will be transferred to/from north/south neighbor
+       CALL MPI_SENDRECV( ghna_count,        1, MPI_INTEGER, pnorth, 0,                            &
+                          ghsa_count_recv,   1, MPI_INTEGER, psouth, 0,                            &
+                          comm2d, status, ierr )
+!
+!--    Send/receive flag that indicates if there are actually any agents in ghost layer
+       CALL MPI_SENDRECV( ghsa_empty,     1, MPI_LOGICAL, psouth, 1,                               &
+                          ghna_empty_rcv, 1, MPI_LOGICAL, pnorth, 1,                               &
+                          comm2d, status, ierr )
+       CALL MPI_SENDRECV( ghna_empty,     1, MPI_LOGICAL, pnorth, 1,                               &
+                          ghsa_empty_rcv, 1, MPI_LOGICAL, psouth, 1,                               &
+                          comm2d, status, ierr )
+
+
+       ALLOCATE ( agt_gh_s(1:ghsa_count_recv) )
+!
+!--    Get bit size of one agent
+       agt_size = STORAGE_SIZE(zero_agent)/8
+!
+!--    Send/receive agents to/from south/north neighbor
+       CALL MPI_SENDRECV( ghsa,     ghsa_count      * agt_size, MPI_BYTE, psouth,1,                &
+                          agt_gh_n, ghna_count_recv * agt_size, MPI_BYTE, pnorth,1,                &
+                          comm2d, status, ierr )
+!
+!--       Send/receive agents to/from south/north neighbor
+       CALL MPI_SENDRECV( ghna,     ghna_count      * agt_size, MPI_BYTE, pnorth,1,                &
+                          agt_gh_s, ghsa_count_recv * agt_size, MPI_BYTE, psouth,1,                &
+                          comm2d, status, ierr )
+!
+!--    If agents were received, add them to the respective ghost layer cells
+       IF ( .NOT. ghna_empty_rcv )  THEN
+          CALL mas_eh_add_ghost_agents_to_gridcell(agt_gh_n)
+       ENDIF
+
+       IF ( .NOT. ghsa_empty_rcv )  THEN
+          CALL mas_eh_add_ghost_agents_to_gridcell(agt_gh_s)
+       ENDIF
+
+       DEALLOCATE( ghna, ghsa, agt_gh_n, agt_gh_s )
+
+    ENDIF
+
+ END SUBROUTINE mas_eh_ghost_exchange
+#endif
+
+!--------------------------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> If an agent moves from one grid cell to another (on the current processor!), this subroutine
+!> moves the corresponding element from the agent array of the old grid cell to the agent array of
+!> the new grid cell.
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_eh_move_agent
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  aindex         !< dummy argument for number of new agent per grid box
+    INTEGER(iwp) ::  i              !< grid index (x) of agent position
+    INTEGER(iwp) ::  ip             !< index variable along x
+    INTEGER(iwp) ::  j              !< grid index (y) of agent position
+    INTEGER(iwp) ::  jp             !< index variable along y
+    INTEGER(iwp) ::  n              !< index variable for agent array
+    INTEGER(iwp) ::  na_before_move !< number of agents per grid box before moving
+
+    TYPE(agent_type), DIMENSION(:), POINTER ::  agents_before_move !< agents before moving
+
+    DO  ip = nxl, nxr
+       DO  jp = nys, nyn
+
+             na_before_move = agt_count(jp,ip)
+             IF ( na_before_move <= 0 )  CYCLE
+             agents_before_move => grid_agents(jp,ip)%agents(1:na_before_move)
+
+             DO  n = 1, na_before_move
+                i = agents_before_move(n)%x * ddx
+                j = agents_before_move(n)%y * ddy
+
+!--             For mas_eh_exchange_horiz to work properly agents need to be moved to the outermost
+!--             gridboxes of the respective processor.
+!--             If the agent index is inside the processor the following lines will not change the
+!--             index.
+                i = MIN ( i , nxr )
+                i = MAX ( i , nxl )
+                j = MIN ( j , nyn )
+                j = MAX ( j , nys )
+
+!
+!--             Check if agent has moved to another grid cell.
+                IF ( i /= ip  .OR.  j /= jp )  THEN
+!
+!--                If the agent stays on the same processor, the agent will be added to the agent
+!--                array of the new processor.
+                   number_of_agents = agt_count(j,i)
+                   agents => grid_agents(j,i)%agents(1:number_of_agents)
+
+                   aindex = number_of_agents+1
+                   IF (  aindex > SIZE(grid_agents(j,i)%agents)  )  THEN
+                      CALL mas_eh_realloc_agents_array(i,j)
+                   ENDIF
+
+                   grid_agents(j,i)%agents(aindex) = agents_before_move(n)
+                   agt_count(j,i) = aindex
+
+                   agents_before_move(n)%agent_mask = .FALSE.
                 ENDIF
              ENDDO
-          ENDDO
+
        ENDDO
-
-       DO  ip = nxl, nxr
-          DO  jp = nys, nyn, nyn-nys
-             number_of_agents = agt_count(jp,ip)
-             IF ( number_of_agents <= 0 )  CYCLE
-             agents => grid_agents(jp,ip)%agents(1:number_of_agents)
-             DO  n = 1, number_of_agents
-
-                IF ( agents(n)%y < 0.0_wp )  THEN
-
-                   IF ( ibc_mas_ns == 0 )  THEN
-!
-!--                   Cyclic boundary. Relevant coordinate has to be changed.
-                      agents(n)%y = ( ny + 1 ) * dy + agents(n)%y
-                      agents(n)%origin_y = ( ny + 1 ) * dy + &
-                           agents(n)%origin_y
-
-                   ELSEIF ( ibc_mas_ns == 1 )  THEN
-!
-!--                   Agent absorption
-                      agents(n)%agent_mask = .FALSE.
-                      deleted_agents = deleted_agents + 1
-                   ENDIF
-
-                ELSEIF ( agents(n)%y >= ( ny + 0.5_wp ) * dy )  THEN
-
-                   IF ( ibc_mas_ns == 0 )  THEN
-!
-!--                   Cyclic boundary. Relevant coordinate has to be changed.
-                      agents(n)%y = agents(n)%y - ( ny + 1 ) * dy
-
-                   ELSEIF ( ibc_mas_ns == 1 )  THEN
-!
-!--                   Agent absorption
-                      agents(n)%agent_mask = .FALSE.
-                      deleted_agents = deleted_agents + 1
-                   ENDIF
-
-                ENDIF
-
-             ENDDO
-          ENDDO
-       ENDDO
-#endif
-
-    END SUBROUTINE mas_eh_exchange_horiz
-
-
-#if defined( __parallel )
-!------------------------------------------------------------------------------!
+    ENDDO
+
+    RETURN
+
+ END SUBROUTINE mas_eh_move_agent
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> Sends the agents from the three gridcells closest to the 
-!> north/south/left/right border of a PE to the corresponding neighbors ghost
-!> layer (which is three grid boxes deep)
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_eh_ghost_exchange
+!> If the allocated memory for the agent array does not suffice to add arriving agents from
+!> neighbour grid cells, this subrouting reallocates the agent array to assure enough memory is
+!> available.
+!--------------------------------------------------------------------------------------------------!
+    SUBROUTINE mas_eh_realloc_agents_array (i,j,size_in)
 
        IMPLICIT NONE
 
-       INTEGER(iwp) ::  ip          !< index variable along x
-       INTEGER(iwp) ::  jp          !< index variable along y
-       INTEGER(iwp) ::  agt_size    !< Bit size of agent datatype
-       INTEGER(iwp) ::  ghla_count  !< ghost points left agent
-       INTEGER(iwp) ::  ghna_count  !< ghost points north agent
-       INTEGER(iwp) ::  ghra_count  !< ghost points right agent
-       INTEGER(iwp) ::  ghsa_count  !< ghost points south agent
-
-       LOGICAL ::  ghla_empty      !< ghost points left agent
-       LOGICAL ::  ghla_empty_rcv  !< ghost points left agent
-       LOGICAL ::  ghna_empty      !< ghost points north agent
-       LOGICAL ::  ghna_empty_rcv  !< ghost points north agent
-       LOGICAL ::  ghra_empty      !< ghost points right agent
-       LOGICAL ::  ghra_empty_rcv  !< ghost points right agent
-       LOGICAL ::  ghsa_empty      !< ghost points south agent
-       LOGICAL ::  ghsa_empty_rcv  !< ghost points south agent
-
-       TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  ghla  !< agents received from right PE
-       TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  ghna  !< agents received from south PE
-       TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  ghra  !< agents received from left PE
-       TYPE(agent_type), DIMENSION(:), ALLOCATABLE ::  ghsa  !< agents received from north PE
-
-       ghla_empty = .TRUE.
-       ghna_empty = .TRUE.
-       ghra_empty = .TRUE.
-       ghsa_empty = .TRUE.
-!
-!--    reset ghost layer
-       DO ip = nxlg, nxl-1
-          DO jp = nysg, nyng
-             agt_count(jp,ip) = 0
-          ENDDO
-       ENDDO
-       DO ip = nxr+1, nxrg
-          DO jp = nysg, nyng
-             agt_count(jp,ip) = 0
-          ENDDO
-       ENDDO
-       DO ip = nxl, nxr
-          DO jp = nysg, nys-1
-             agt_count(jp,ip) = 0
-          ENDDO
-       ENDDO
-       DO ip = nxl, nxr
-          DO jp = nyn+1, nyng
-             agt_count(jp,ip) = 0
-          ENDDO
-       ENDDO
-!
-!--    Transfer of agents from left to right and vice versa
-       IF ( pdims(1) /= 1 )  THEN
-!
-!--       Reset left and right ghost layers
-          ghla_count  = 0
-          ghra_count  = 0
-!
-!--       First calculate the storage necessary for sending 
-!--       and receiving the data.
-          ghla_count = SUM(agt_count(nys:nyn,nxl:nxl+2))
-          ghra_count = SUM(agt_count(nys:nyn,nxr-2:nxr))
-!
-!--       No cyclic boundaries for agents
-          IF ( nxl == 0 .OR. ghla_count == 0 ) THEN
-             ghla_count = 1
-          ELSE
-             ghla_empty = .FALSE.
-          ENDIF
-          IF ( nxr == nx .OR. ghra_count == 0 ) THEN
-             ghra_count = 1
-          ELSE
-             ghra_empty = .FALSE.
-          ENDIF
-          ALLOCATE( ghla(1:ghla_count), ghra(1:ghra_count) )
-          ghla = zero_agent
-          ghra = zero_agent
-!
-!--       Get all agents that will be sent left into one array
-          ghla_count = 0
-          IF ( nxl /= 0 ) THEN
-             DO ip = nxl, nxl+2
-                DO jp = nys, nyn
-
-                   number_of_agents = agt_count(jp,ip)
-                   IF ( number_of_agents <= 0 )  CYCLE
-                   ghla(ghla_count+1:ghla_count+number_of_agents)              &
-                       = grid_agents(jp,ip)%agents(1:number_of_agents)
-                   ghla_count = ghla_count + number_of_agents
-
-                ENDDO
-             ENDDO
-          ENDIF
-          IF ( ghla_count == 0 )  ghla_count = 1
-!
-!--       Get all agents that will be sent right into one array
-          ghra_count = 0
-          IF ( nxr /= nx ) THEN
-             DO ip = nxr-2, nxr
-                DO jp = nys, nyn
-
-                   number_of_agents = agt_count(jp,ip)
-                   IF ( number_of_agents <= 0 )  CYCLE
-                   ghra(ghra_count+1:ghra_count+number_of_agents)              &
-                       = grid_agents(jp,ip)%agents(1:number_of_agents)
-                   ghra_count = ghra_count + number_of_agents
-
-                ENDDO
-             ENDDO
-          ENDIF
-          IF ( ghra_count == 0 ) ghra_count = 1
-!
-!--       Send/receive number of agents that 
-!--       will be transferred to/from left/right neighbor
-          CALL MPI_SENDRECV( ghla_count,      1, MPI_INTEGER, pleft,  0,       &
-                             ghra_count_recv, 1, MPI_INTEGER, pright, 0,       &
-                             comm2d, status, ierr )
-          ALLOCATE ( agt_gh_r(1:ghra_count_recv) )
-!
-!--       Send/receive number of agents that 
-!--       will be transferred to/from right/left neighbor
-          CALL MPI_SENDRECV( ghra_count,      1, MPI_INTEGER, pright,  0,      &
-                             ghla_count_recv, 1, MPI_INTEGER, pleft,   0,      &
-                             comm2d, status, ierr )
-!
-!--       Send/receive flag that indicates if there are actually any agents 
-!--       in ghost  layer
-          CALL MPI_SENDRECV( ghla_empty,     1, MPI_LOGICAL, pleft, 1,         &
-                             ghra_empty_rcv, 1, MPI_LOGICAL, pright,1,         &
-                             comm2d, status, ierr )
-          CALL MPI_SENDRECV( ghra_empty,     1, MPI_LOGICAL, pright,1,         &
-                             ghla_empty_rcv, 1, MPI_LOGICAL, pleft, 1,         &
-                             comm2d, status, ierr )
-
-
-          ALLOCATE ( agt_gh_l(1:ghla_count_recv) )
-!
-!--       Get bit size of one agent
-          agt_size = STORAGE_SIZE(zero_agent)/8
-!
-!--       Send/receive agents to/from left/right neighbor
-          CALL MPI_SENDRECV( ghla,     ghla_count      * agt_size, MPI_BYTE,   &
-                                pleft, 1,                                      &
-                             agt_gh_r, ghra_count_recv * agt_size, MPI_BYTE,   &
-                                pright,1,                                      &
-                             comm2d, status, ierr )
-!
-!--       Send/receive agents to/from left/right neighbor
-          CALL MPI_SENDRECV( ghra,     ghra_count      * agt_size, MPI_BYTE,   &
-                                pright,1,                                      &
-                             agt_gh_l, ghla_count_recv * agt_size, MPI_BYTE,   &
-                                pleft, 1,                                      &
-                             comm2d, status, ierr )
-!
-!--       If agents were received, add them to the respective ghost layer cells
-          IF ( .NOT. ghra_empty_rcv ) THEN
-             CALL mas_eh_add_ghost_agents_to_gridcell(agt_gh_r)
-          ENDIF
-
-          IF ( .NOT. ghla_empty_rcv ) THEN
-             CALL mas_eh_add_ghost_agents_to_gridcell(agt_gh_l)
-          ENDIF
-
-          DEALLOCATE( ghla, ghra, agt_gh_l, agt_gh_r )
-
-       ENDIF
-
-!
-!--    Transfer of agents from south to north and vice versa
-       IF ( pdims(2) /= 1 )  THEN
-!
-!--       Reset south and north ghost layers
-          ghsa_count  = 0
-          ghna_count  = 0
-!
-!--       First calculate the storage necessary for sending 
-!--       and receiving the data.
-          ghsa_count = SUM(agt_count(nys:nys+2,nxlg:nxrg))
-          ghna_count = SUM(agt_count(nyn-2:nyn,nxlg:nxrg))
-!
-!--       No cyclic boundaries for agents
-          IF ( nys == 0 .OR. ghsa_count == 0 ) THEN
-             ghsa_count = 1
-          ELSE
-             ghsa_empty = .FALSE.
-          ENDIF
-          IF ( nyn == ny .OR. ghna_count == 0 ) THEN
-             ghna_count = 1
-          ELSE
-             ghna_empty = .FALSE.
-          ENDIF
-          ALLOCATE( ghsa(1:ghsa_count), ghna(1:ghna_count) )
-          ghsa = zero_agent
-          ghna = zero_agent
-!
-!--       Get all agents that will be sent south into one array
-          ghsa_count = 0
-          IF ( nys /= 0 ) THEN
-             DO ip = nxlg, nxrg
-                DO jp = nys, nys+2
-
-                   number_of_agents = agt_count(jp,ip)
-                   IF ( number_of_agents <= 0 )  CYCLE
-                   ghsa(ghsa_count+1:ghsa_count+number_of_agents)              &
-                       = grid_agents(jp,ip)%agents(1:number_of_agents)
-                   ghsa_count = ghsa_count + number_of_agents
-
-                ENDDO
-             ENDDO
-          ENDIF
-          IF ( ghsa_count == 0 )  ghsa_count = 1
-!
-!--       Get all agents that will be sent north into one array
-          ghna_count = 0
-          IF ( nyn /= ny ) THEN
-             DO ip = nxlg, nxrg
-                DO jp = nyn-2, nyn
-
-                   number_of_agents = agt_count(jp,ip)
-                   IF ( number_of_agents <= 0 )  CYCLE
-                   ghna(ghna_count+1:ghna_count+number_of_agents)              &
-                       = grid_agents(jp,ip)%agents(1:number_of_agents)
-                   ghna_count = ghna_count + number_of_agents
-
-                ENDDO
-             ENDDO
-          ENDIF
-          IF ( ghna_count == 0 ) ghna_count = 1
-!
-!--       Send/receive number of agents that 
-!--       will be transferred to/from south/north neighbor
-          CALL MPI_SENDRECV( ghsa_count, 1, MPI_INTEGER, psouth, 0,            &
-                             ghna_count_recv,   1, MPI_INTEGER, pnorth, 0,     &
-                             comm2d, status, ierr )
-          ALLOCATE ( agt_gh_n(1:ghna_count_recv) )
-!
-!--       Send/receive number of agents that 
-!--       will be transferred to/from north/south neighbor
-          CALL MPI_SENDRECV( ghna_count, 1, MPI_INTEGER, pnorth, 0,            &
-                             ghsa_count_recv,   1, MPI_INTEGER, psouth, 0,     &
-                             comm2d, status, ierr )
-!
-!--       Send/receive flag that indicates if there are actually any agents 
-!--       in ghost  layer
-          CALL MPI_SENDRECV( ghsa_empty,     1, MPI_LOGICAL, psouth, 1,        &
-                             ghna_empty_rcv, 1, MPI_LOGICAL, pnorth, 1,        &
-                             comm2d, status, ierr )
-          CALL MPI_SENDRECV( ghna_empty,     1, MPI_LOGICAL, pnorth, 1,        &
-                             ghsa_empty_rcv, 1, MPI_LOGICAL, psouth, 1,        &
-                             comm2d, status, ierr )
-
-
-          ALLOCATE ( agt_gh_s(1:ghsa_count_recv) )
-!
-!--       Get bit size of one agent
-          agt_size = STORAGE_SIZE(zero_agent)/8
-!
-!--       Send/receive agents to/from south/north neighbor
-          CALL MPI_SENDRECV( ghsa,     ghsa_count      * agt_size, MPI_BYTE,   &
-                                psouth,1,                                      &
-                             agt_gh_n, ghna_count_recv * agt_size, MPI_BYTE,   &
-                                pnorth,1,                                      &
-                             comm2d, status, ierr )
-!
-!--       Send/receive agents to/from south/north neighbor
-          CALL MPI_SENDRECV( ghna,     ghna_count      * agt_size, MPI_BYTE,   &
-                                pnorth,1,                                      &
-                             agt_gh_s, ghsa_count_recv * agt_size, MPI_BYTE,   &
-                                psouth,1,                                      &
-                             comm2d, status, ierr )
-!
-!--       If agents were received, add them to the respective ghost layer cells
-          IF ( .NOT. ghna_empty_rcv ) THEN
-             CALL mas_eh_add_ghost_agents_to_gridcell(agt_gh_n)
-          ENDIF
-
-          IF ( .NOT. ghsa_empty_rcv ) THEN
-             CALL mas_eh_add_ghost_agents_to_gridcell(agt_gh_s)
-          ENDIF
-
-          DEALLOCATE( ghna, ghsa, agt_gh_n, agt_gh_s )
-
-       ENDIF
-
-    END SUBROUTINE mas_eh_ghost_exchange
-#endif
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> If an agent moves from one grid cell to another (on the current 
-!> processor!), this subroutine moves the corresponding element from the
-!> agent array of the old grid cell to the agent array of the new grid 
-!> cell.
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_eh_move_agent
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  i              !< grid index (x) of agent position
-       INTEGER(iwp) ::  ip             !< index variable along x
-       INTEGER(iwp) ::  j              !< grid index (y) of agent position
-       INTEGER(iwp) ::  jp             !< index variable along y
-       INTEGER(iwp) ::  n              !< index variable for agent array
-       INTEGER(iwp) ::  na_before_move !< number of agents per grid box before moving
-       INTEGER(iwp) ::  aindex         !< dummy argument for number of new agent per grid box
-
-       TYPE(agent_type), DIMENSION(:), POINTER ::  agents_before_move !< agents before moving
-
-       DO  ip = nxl, nxr
-          DO  jp = nys, nyn
-
-                na_before_move = agt_count(jp,ip)
-                IF ( na_before_move <= 0 )  CYCLE
-                agents_before_move => grid_agents(jp,ip)%agents(1:na_before_move)
-
-                DO  n = 1, na_before_move
-                   i = agents_before_move(n)%x * ddx
-                   j = agents_before_move(n)%y * ddy
-
-!--                For mas_eh_exchange_horiz to work properly agents need to be
-!--                moved to the outermost gridboxes of the respective processor.
-!--                If the agent index is inside the processor the following
-!--                lines will not change the index
-                   i = MIN ( i , nxr )
-                   i = MAX ( i , nxl )
-                   j = MIN ( j , nyn )
-                   j = MAX ( j , nys )
-
-!
-!--                Check if agent has moved to another grid cell.
-                   IF ( i /= ip  .OR.  j /= jp )  THEN
-!
-!--                   If the agent stays on the same processor, the agent
-!--                   will be added to the agent array of the new processor.
-                      number_of_agents = agt_count(j,i)
-                      agents => grid_agents(j,i)%agents(1:number_of_agents)
-
-                      aindex = number_of_agents+1
-                      IF (  aindex > SIZE(grid_agents(j,i)%agents)  )     &
-                      THEN
-                         CALL mas_eh_realloc_agents_array(i,j)
-                      ENDIF
-
-                      grid_agents(j,i)%agents(aindex) = agents_before_move(n)
-                      agt_count(j,i) = aindex
-
-                      agents_before_move(n)%agent_mask = .FALSE.
-                   ENDIF
-                ENDDO
-
-          ENDDO
-       ENDDO
-
-       RETURN
-
-    END SUBROUTINE mas_eh_move_agent
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> If the allocated memory for the agent array do not suffice to add arriving
-!> agents from neighbour grid cells, this subrouting reallocates the 
-!> agent array to assure enough memory is available. 
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_eh_realloc_agents_array (i,j,size_in)
-
-       IMPLICIT NONE
-
+       INTEGER(iwp) :: new_size  !< new array size
        INTEGER(iwp) :: old_size  !< old array size
-       INTEGER(iwp) :: new_size  !< new array size
 
        INTEGER(iwp), INTENT(in) ::  i  !< grid index (y)
@@ -2733,11 +2618,11 @@
        INTEGER(iwp), INTENT(in), OPTIONAL ::  size_in  !< size of input array
 
-       TYPE(agent_type), DIMENSION(10) :: tmp_agents_s !< temporary static agent array
-
-       TYPE(agent_type), DIMENSION(:), ALLOCATABLE :: tmp_agents_d !< temporary dynamic agent array
+       TYPE(agent_type), DIMENSION(10) :: tmp_agents_s  !< temporary static agent array
+
+       TYPE(agent_type), DIMENSION(:), ALLOCATABLE :: tmp_agents_d  !< temporary dynamic agent array
 
        old_size = SIZE(grid_agents(j,i)%agents)
 
-       IF ( PRESENT(size_in) )   THEN
+       IF ( PRESENT( size_in ) )  THEN
           new_size = size_in
        ELSE
@@ -2751,6 +2636,6 @@
           tmp_agents_s(1:old_size) = grid_agents(j,i)%agents(1:old_size)
 
-          DEALLOCATE(grid_agents(j,i)%agents)
-          ALLOCATE(grid_agents(j,i)%agents(new_size))
+          DEALLOCATE( grid_agents(j,i)%agents )
+          ALLOCATE( grid_agents(j,i)%agents(new_size) )
 
           grid_agents(j,i)%agents(1:old_size)         = tmp_agents_s(1:old_size)
@@ -2759,14 +2644,14 @@
        ELSE
 
-          ALLOCATE(tmp_agents_d(new_size))
+          ALLOCATE( tmp_agents_d(new_size) )
           tmp_agents_d(1:old_size) = grid_agents(j,i)%agents
 
-          DEALLOCATE(grid_agents(j,i)%agents)
-          ALLOCATE(grid_agents(j,i)%agents(new_size))
+          DEALLOCATE( grid_agents(j,i)%agents )
+          ALLOCATE( grid_agents(j,i)%agents(new_size) )
 
           grid_agents(j,i)%agents(1:old_size)         = tmp_agents_d(1:old_size)
           grid_agents(j,i)%agents(old_size+1:new_size) = zero_agent
 
-          DEALLOCATE(tmp_agents_d)
+          DEALLOCATE( tmp_agents_d )
 
        ENDIF
@@ -2776,1236 +2661,1207 @@
     END SUBROUTINE mas_eh_realloc_agents_array
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> Inquires prognostic model quantities at the position of each agent and
-!> stores them in that agent for later output
-!------------------------------------------------------------------------------! 
-    SUBROUTINE mas_get_prognostic_quantities
-
-       USE arrays_3d,                                                          &
-           ONLY:  u, v, pt, exner
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  i_offset  !<  index offset for windspeed measurement
-       INTEGER(iwp) ::  il        !<  x-index
-       INTEGER(iwp) ::  is        !<  subgrid box counter
-       INTEGER(iwp) ::  j_offset  !<  index offset for windspeed measurement
-       INTEGER(iwp) ::  jl        !<  y-index
-       INTEGER(iwp) ::  kl        !<  z-index
-       INTEGER(iwp) ::  nl        !<  agent counter
-       INTEGER(iwp) ::  se        !<  subgrid box end index
-       INTEGER(iwp) ::  si        !<  subgrid box start index
-
-       REAL(wp) ::  u_a  !< windspeed at agent position (x)
-       REAL(wp) ::  v_a  !< windspeed at agent position (y)
-
-       DO  il = nxl, nxr
-          DO  jl = nys, nyn
-
-             number_of_agents = agt_count(jl,il)
-!
-!--          If grid cell is empty, cycle
-             IF ( number_of_agents <= 0 ) CYCLE
-             kl = s_measure_height(jl,il)
-
-             agents => grid_agents(jl,il)%agents(1:number_of_agents)
-!
-!--          loop over the four subgrid boxes
-             DO is = 0,3
-!
-!--             Set indices
-                si = grid_agents(jl,il)%start_index(is)
-                se = grid_agents(jl,il)%end_index(is)
-                DO nl = si, se
-!
-!--                Calculate index offset in x-direction:
-!--                Left value if wall right of grid box
-!--                Right value if wall left of grid box
-!--                Else the one that is closer to the agent
-                   IF ( BTEST( obstacle_flags( jl, il+1 ), 6 ) ) THEN
-                      i_offset = 0
-                   ELSEIF ( BTEST( obstacle_flags( jl, il-1 ), 2 ) ) THEN
-                      i_offset = 1
-                   ELSE
-                      i_offset = MERGE( 0, 1, BTEST(is,1) )
-                   ENDIF
-                   u_a = u( kl, jl, il + i_offset )
-!
-!--                Calculate index offset in y-direction:
-!--                South value if wall north of grid box
-!--                North value if wall south of grid box
-!--                Else the one that is closer to the agent
-                   IF ( BTEST( obstacle_flags( jl+1, il ), 4 ) ) THEN
-                      j_offset = 0
-                   ELSEIF ( BTEST( obstacle_flags( jl-1, il ), 0 ) ) THEN
-                      j_offset = 1
-                   ELSE
-                      j_offset = MERGE( 0, 1, BTEST(is,0) )
-                   ENDIF
-                   v_a = v( kl, jl + j_offset, il )
-!
-!--                Calculate windspeed at agent postion
-                   agents(nl)%windspeed = SQRT(u_a**2 + v_a**2)
-!
-!--                Calculate temperature at agent position
-                   agents(nl)%t = pt(kl,jl,il) * exner(kl)
-
-                ENDDO
+!> Inquires prognostic model quantities at the position of each agent and stores them in that agent
+!> for later output
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_get_prognostic_quantities
+
+    USE arrays_3d,                                                                                 &
+        ONLY:  exner, pt, u, v
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  i_offset  !<  index offset for windspeed measurement
+    INTEGER(iwp) ::  il        !<  x-index
+    INTEGER(iwp) ::  is        !<  subgrid box counter
+    INTEGER(iwp) ::  j_offset  !<  index offset for windspeed measurement
+    INTEGER(iwp) ::  jl        !<  y-index
+    INTEGER(iwp) ::  kl        !<  z-index
+    INTEGER(iwp) ::  nl        !<  agent counter
+    INTEGER(iwp) ::  se        !<  subgrid box end index
+    INTEGER(iwp) ::  si        !<  subgrid box start index
+
+    REAL(wp) ::  u_a  !< windspeed at agent position (x)
+    REAL(wp) ::  v_a  !< windspeed at agent position (y)
+
+    DO  il = nxl, nxr
+       DO  jl = nys, nyn
+
+          number_of_agents = agt_count(jl,il)
+!
+!--       If grid cell is empty, cycle
+          IF ( number_of_agents <= 0 )  CYCLE
+          kl = s_measure_height(jl,il)
+
+          agents => grid_agents(jl,il)%agents(1:number_of_agents)
+!
+!--       Loop over the four subgrid boxes
+          DO  is = 0,3
+!
+!--          Set indices
+             si = grid_agents(jl,il)%start_index(is)
+             se = grid_agents(jl,il)%end_index(is)
+             DO  nl = si, se
+!
+!--             Calculate index offset in x-direction:
+!--             Left value if wall right of grid box
+!--             Right value if wall left of grid box
+!--             Else the one that is closer to the agent
+                IF ( BTEST( obstacle_flags( jl, il+1 ), 6 ) )  THEN
+                   i_offset = 0
+                ELSEIF ( BTEST( obstacle_flags( jl, il-1 ), 2 ) )  THEN
+                   i_offset = 1
+                ELSE
+                   i_offset = MERGE( 0, 1, BTEST(is,1) )
+                ENDIF
+                u_a = u( kl, jl, il + i_offset )
+!
+!--             Calculate index offset in y-direction:
+!--             South value if wall north of grid box
+!--             North value if wall south of grid box
+!--             Else the one that is closer to the agent
+                IF ( BTEST( obstacle_flags( jl+1, il ), 4 ) )  THEN
+                   j_offset = 0
+                ELSEIF ( BTEST( obstacle_flags( jl-1, il ), 0 ) )  THEN
+                   j_offset = 1
+                ELSE
+                   j_offset = MERGE( 0, 1, BTEST(is,0) )
+                ENDIF
+                v_a = v( kl, jl + j_offset, il )
+!
+!--             Calculate windspeed at agent postion
+                agents(nl)%windspeed = SQRT(u_a**2 + v_a**2)
+!
+!--             Calculate temperature at agent position
+                agents(nl)%t = pt(kl,jl,il) * exner(kl)
 
              ENDDO
 
           ENDDO
+
        ENDDO
-
-    END SUBROUTINE mas_get_prognostic_quantities
-
-!------------------------------------------------------------------------------!
+    ENDDO
+
+ END SUBROUTINE mas_get_prognostic_quantities
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Adds an item to the priority queue (binary heap) at the correct position
-!------------------------------------------------------------------------------! 
-    SUBROUTINE mas_heap_insert_item( id, priority )
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  cur_pos !< current position
-       INTEGER(iwp) ::  id      !< mesh ID of item
-
-       REAL(wp) ::  priority !< item priority
-
-       TYPE(heap_item) ::  item !< heap item
-
-       item%mesh_id  = id
-       item%priority = priority
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_heap_insert_item( id, priority )
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  cur_pos  !< current position
+    INTEGER(iwp) ::  id       !< mesh ID of item
+
+    REAL(wp) ::  priority  !< item priority
+
+    TYPE(heap_item) ::  item  !< heap item
+
+    item%mesh_id  = id
+    item%priority = priority
 !
 !--    Extend heap, if necessary
-       IF ( heap_count + 1 > SIZE(queue) ) THEN
-          CALL mas_heap_extend
+    IF ( heap_count + 1 > SIZE( queue ) )  THEN
+       CALL mas_heap_extend
+    ENDIF
+!
+!-- Insert item at first unoccupied postion (highest index) of heap
+    cur_pos = heap_count
+    queue(cur_pos) = item
+!
+!-- Sort while inserted item is not at top of heap
+    DO WHILE ( cur_pos /= 0 )
+!
+!--    If priority < its parent's priority, swap them.
+!--    Else, sorting is done.
+       IF ( queue(cur_pos)%priority < queue(FLOOR( (cur_pos) / 2.0_wp ))%priority )  THEN
+          item = queue(cur_pos)
+          queue(cur_pos) = queue(FLOOR( ( cur_pos ) / 2.0_wp ))
+          queue(FLOOR( ( cur_pos ) / 2.0_wp )) = item
+          cur_pos = FLOOR( ( cur_pos ) / 2.0_wp )
+       ELSE
+          EXIT
        ENDIF
-!
-!--    Insert item at first unoccupied postion (highest index) of heap
-       cur_pos = heap_count
-       queue(cur_pos) = item
-!
-!--    Sort while inserted item is not at top of heap
-       DO WHILE ( cur_pos /= 0 )
-!
-!--       If priority < its parent's priority, swap them. 
-!--       Else, sorting is done.
-          IF ( queue(cur_pos)%priority                                         &
-              < queue(FLOOR((cur_pos)/2.))%priority )                          &
-          THEN
-             item = queue(cur_pos)
-             queue(cur_pos) = queue(FLOOR((cur_pos)/2.))
-             queue(FLOOR((cur_pos)/2.)) = item
-             cur_pos = FLOOR((cur_pos)/2.)
+    ENDDO
+!
+!-- Item was added to heap, so the heap count increases
+    heap_count = heap_count + 1
+
+ END SUBROUTINE mas_heap_insert_item
+
+!--------------------------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Extends the size of the priority queue (binary heap)
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_heap_extend
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  soh  !< size of heap
+
+    TYPE(heap_item), DIMENSION(:), ALLOCATABLE ::  dummy_heap  !< dummy heap
+
+    soh = SIZE( queue ) - 1
+    ALLOCATE( dummy_heap(0:soh) )
+    dummy_heap = queue
+    DEALLOCATE( queue )
+    ALLOCATE( queue(0:2*soh+1) )
+    queue(0:soh) = dummy_heap(0:soh)
+
+ END SUBROUTINE mas_heap_extend
+
+!--------------------------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Removes first (smallest) element from the priority queue, reorders the rest and returns the ID of
+!> the removed mesh point
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_heap_extract_item ( id )
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  child    !< child of item in heap
+    INTEGER(iwp) ::  cur_pos  !< current position of item in heap
+    INTEGER(iwp) ::  id       !< ID of item extracted item
+
+    TYPE(heap_item) ::  dummy
+!
+!-- Get ID of mesh point with lowest priority (extracted item: top of heap)
+    id = queue(0)%mesh_id
+!
+!-- Put last item in heap at first position
+    queue(0) = queue(heap_count-1)
+    cur_pos = 0
+    DO
+!
+!--    If current item has no children, sorting is done
+       IF( 2*cur_pos+1 > heap_count - 1 )  THEN
+          EXIT
+!
+!--    If current item has only one child, check if item and its child are ordered correctly. Else,
+!--    swap them.
+       ELSEIF ( 2*cur_pos+2 > heap_count - 1 )  THEN
+          IF ( queue(cur_pos)%priority > queue(2*cur_pos+1)%priority )  THEN
+             dummy = queue(cur_pos)
+             queue(cur_pos) = queue(2*cur_pos+1)
+             queue(2*cur_pos+1) = dummy
+             cur_pos = 2*cur_pos+1
           ELSE
              EXIT
           ENDIF
-       ENDDO
-!
-!--    Item was added to heap, so the heap count increases
-       heap_count = heap_count + 1
-
-    END SUBROUTINE mas_heap_insert_item
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Extends the size of the priority queue (binary heap)
-!------------------------------------------------------------------------------! 
-    SUBROUTINE mas_heap_extend
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  soh !< size of heap
-
-       TYPE(heap_item), DIMENSION(:), ALLOCATABLE ::  dummy_heap !< dummy heap
-
-       soh = SIZE(queue)-1
-       ALLOCATE(dummy_heap(0:soh))
-       dummy_heap = queue
-       DEALLOCATE(queue)
-       ALLOCATE(queue(0:2*soh+1))
-       queue(0:soh) = dummy_heap(0:soh)
-
-    END SUBROUTINE mas_heap_extend
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Removes first (smallest) element from the priority queue, reorders the rest
-!> and returns the ID of the removed mesh point
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_heap_extract_item ( id )
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  id      !< ID of item extracted item
-       INTEGER(iwp) ::  child   !< child of item in heap
-       INTEGER(iwp) ::  cur_pos !< current position of item in heap
-
-       TYPE(heap_item) ::  dummy
-!
-!--    Get ID of mesh point with lowest priority (extracted item: top of heap)
-       id = queue(0)%mesh_id
-!
-!--    Put last item in heap at first position
-       queue(0) = queue(heap_count-1)
-       cur_pos = 0
-       DO
-!
-!--       If current item has no children, sorting is done
-          IF( 2*cur_pos+1 > heap_count - 1 ) THEN
+       ELSE
+!
+!--       Determine the smaller child
+          IF ( queue(2*cur_pos+1)%priority >= queue(2*cur_pos+2)%priority )  THEN
+             child = 2
+          ELSE
+             child = 1
+          ENDIF
+!
+!--       Check if item and its smaller child are ordered falsely. If so, swap them. Else, sorting
+!--       is done.
+          IF ( queue(cur_pos)%priority > queue(2*cur_pos+child )%priority )  THEN
+             dummy = queue(cur_pos)
+             queue(cur_pos) = queue(2*cur_pos+child)
+             queue(2*cur_pos+child) = dummy
+             cur_pos = 2*cur_pos+child
+          ELSE
              EXIT
-!
-!--       If current item has only one child, check if item and its child are
-!--       ordered correctly. Else, swap them.
-          ELSEIF ( 2*cur_pos+2 > heap_count - 1 ) THEN
-             IF ( queue(cur_pos)%priority > queue(2*cur_pos+1)%priority ) THEN
-                dummy = queue(cur_pos)
-                queue(cur_pos) = queue(2*cur_pos+1)
-                queue(2*cur_pos+1) = dummy
-                cur_pos = 2*cur_pos+1
-             ELSE
-                EXIT
-             ENDIF
-          ELSE
-!
-!--          determine the smaller child
-             IF ( queue(2*cur_pos+1)%priority                                  &
-                 >= queue(2*cur_pos+2)%priority )                              &
-             THEN
-                child = 2
-             ELSE
-                child = 1
-             ENDIF
-!
-!--          Check if item and its smaller child are ordered falsely. If so, 
-!--          swap them. Else, sorting is done.
-             IF ( queue(cur_pos)%priority > queue(2*cur_pos+child )%priority ) &
-             THEN
-                dummy = queue(cur_pos)
-                queue(cur_pos) = queue(2*cur_pos+child)
-                queue(2*cur_pos+child) = dummy
-                cur_pos = 2*cur_pos+child
-             ELSE
-                EXIT
-             ENDIF
           ENDIF
-       ENDDO
-!
-!--    Top item was removed from heap, thus, heap_cout decreases by one
-       heap_count = heap_count-1
-
-    END SUBROUTINE mas_heap_extract_item
-
-!------------------------------------------------------------------------------!
+       ENDIF
+    ENDDO
+!
+!-- Top item was removed from heap, thus, heap_cout decreases by one
+    heap_count = heap_count-1
+
+ END SUBROUTINE mas_heap_extract_item
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Initialization of Multi Agent System
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_init
-
-       USE control_parameters,                                                 &
-           ONLY:  coupling_char, initializing_actions, io_blocks, io_group
-
-       USE arrays_3d,                                                          &
-           ONLY:  zu, zw
-
-       USE indices,                                                            &
-           ONLY:  nzt 
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  i             !< grid cell (x)
-       INTEGER(iwp) ::  ii            !< io-block counter
-       INTEGER(iwp) ::  il            !< io-block counter
-       INTEGER(iwp) ::  jl            !< io-block counter
-       INTEGER(iwp) ::  kl            !< io-block counter
-       INTEGER(iwp) ::  kdum            !< io-block counter
-       INTEGER(iwp) ::  locdum            !< io-block counter
-       INTEGER(iwp) ::  j             !< grid cell (y)
-       INTEGER(iwp) ::  size_of_mesh  !< temporary value for read
-       INTEGER(iwp) ::  size_of_pols  !< temporary value for read
-       INTEGER(iwp) ::  ioerr         !< IOSTAT flag for IO-commands ( 0 = no error )
-
-       LOGICAL ::  navigation_data_present  !< Flag: check for input file
-
-       REAL(wp) ::  zdum  !< dummy for measurement height
-       REAL(wp) ::  avg_agt_height = 1.8_wp
-
-
-!
-!--    Check the number of agent groups.
-       IF ( number_of_agent_groups > max_number_of_agent_groups )  THEN
-          WRITE( message_string, * ) 'max_number_of_agent_groups =',      &
-                                     max_number_of_agent_groups ,         &
-                                     '&number_of_agent_groups reset to ', &
-                                     max_number_of_agent_groups
-          CALL message( 'mas_init', 'PA0072', 0, 1, 0, 6, 0 )
-          number_of_agent_groups = max_number_of_agent_groups
-       ENDIF
-
-!
-!--    Set some parameters
-       d_sigma_rep_agent = 1.0_wp/sigma_rep_agent
-       d_sigma_rep_wall  = 1.0_wp/sigma_rep_wall
-       d_tau_accel_agent = 1.0_wp/tau_accel_agent
-       IF ( dt_agent /= 999.0_wp ) THEN
-          agent_own_timestep = .TRUE.
-       ENDIF
-
-!
-!--    Get index of first grid box above topography
-       ALLOCATE( top_top_s(nysg:nyng,nxlg:nxrg),                               &
-                 top_top_w(nysg:nyng,nxlg:nxrg),                               &
-                 s_measure_height(nys:nyn,nxl:nxr) )
-!
-!--    Get first index above topography for scalar grid and last index in
-!--    topography for z-component of wind
-       DO il = nxlg, nxrg
-          DO jl = nysg, nyng
-             top_top_s(jl,il) = topo_top_ind(jl,il,0) + 1
-             top_top_w(jl,il) = topo_top_ind(jl,il,3)
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_init
+
+    USE control_parameters,                                                                        &
+        ONLY:  coupling_char, initializing_actions, io_blocks, io_group
+
+    USE arrays_3d,                                                                                 &
+        ONLY:  zu, zw
+
+    USE indices,                                                                                   &
+        ONLY:  nzt
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  i             !< grid cell (x)
+    INTEGER(iwp) ::  ii            !< io-block counter
+    INTEGER(iwp) ::  il            !< io-block counter
+    INTEGER(iwp) ::  ioerr         !< IOSTAT flag for IO-commands ( 0 = no error )
+    INTEGER(iwp) ::  j             !< grid cell (y)
+    INTEGER(iwp) ::  jl            !< io-block counter
+    INTEGER(iwp) ::  kl            !< io-block counter
+    INTEGER(iwp) ::  kdum          !< io-block counter
+    INTEGER(iwp) ::  locdum        !< io-block counter
+    INTEGER(iwp) ::  size_of_mesh  !< temporary value for read
+    INTEGER(iwp) ::  size_of_pols  !< temporary value for read
+
+    LOGICAL ::  navigation_data_present  !< Flag: check for input file
+
+    REAL(wp) ::  zdum  !< dummy for measurement height
+    REAL(wp) ::  avg_agt_height = 1.8_wp
+
+
+!
+!-- Check the number of agent groups.
+    IF ( number_of_agent_groups > max_number_of_agent_groups )  THEN
+       WRITE( message_string, * ) 'max_number_of_agent_groups =', max_number_of_agent_groups ,     &
+                                  '&number_of_agent_groups reset to ', max_number_of_agent_groups
+       CALL message( 'mas_init', 'PA0072', 0, 1, 0, 6, 0 )
+       number_of_agent_groups = max_number_of_agent_groups
+    ENDIF
+
+!
+!-- Set some parameters
+    d_sigma_rep_agent = 1.0_wp/sigma_rep_agent
+    d_sigma_rep_wall  = 1.0_wp/sigma_rep_wall
+    d_tau_accel_agent = 1.0_wp/tau_accel_agent
+    IF ( dt_agent /= 999.0_wp )  THEN
+       agent_own_timestep = .TRUE.
+    ENDIF
+
+!
+!-- Get index of first grid box above topography
+    ALLOCATE( top_top_s(nysg:nyng,nxlg:nxrg),                                                      &
+              top_top_w(nysg:nyng,nxlg:nxrg),                                                      &
+              s_measure_height(nys:nyn,nxl:nxr) )
+!
+!-- Get first index above topography for scalar grid and last index in topography for z-component of
+!-- wind.
+    DO  il = nxlg, nxrg
+       DO  jl = nysg, nyng
+          top_top_s(jl,il) = topo_top_ind(jl,il,0) + 1
+          top_top_w(jl,il) = topo_top_ind(jl,il,3)
+       ENDDO
+    ENDDO
+!
+!-- Create 2D array containing the index at which measurements are done by agents. The height of
+!-- this measurement is given by avg_agt_height.
+    DO  il = nxl, nxr
+       DO  jl = nys, nyn
+
+          kdum = top_top_w(jl,il)
+          zdum = zw(kdum)
+          zdum = zdum + avg_agt_height
+          locdum = 0
+!
+!--       Locate minimum distance from u-grid to measurement height (zdum)
+          DO  kl = 1, nzt
+             IF ( ABS(zu(kl)-zdum) < ABS(zu(locdum)-zdum) ) locdum = kl
           ENDDO
+          s_measure_height(jl,il) = locdum
+
        ENDDO
-!
-!--    Create 2D array containing the index at which measurements are done by
-!--    agents. The height of this measurement is given by avg_agt_height.
-       DO il = nxl, nxr
-          DO jl = nys, nyn
-
-             kdum = top_top_w(jl,il)
-             zdum = zw(kdum)
-             zdum = zdum + avg_agt_height
-             locdum = 0
-!
-!--          Locate minimum distance from u-grid to measurement height (zdum)
-             DO kl = 1, nzt
-                IF ( ABS(zu(kl)-zdum) < ABS(zu(locdum)-zdum) ) locdum = kl
+    ENDDO
+
+    CALL mas_create_obstacle_flags
+
+!
+!-- Set default start positions, if necessary
+    IF ( asl(1) == 9999999.9_wp )  asl(1) = 0.0_wp
+    IF ( asr(1) == 9999999.9_wp )  asr(1) = ( nx + 1 ) * dx
+    IF ( ass(1) == 9999999.9_wp )  ass(1) = 0.0_wp
+    IF ( asn(1) == 9999999.9_wp )  asn(1) = ( ny + 1 ) * dy
+    IF ( adx(1) == 9999999.9_wp  .OR.  adx(1) == 0.0_wp ) adx(1) = dx
+    IF ( ady(1) == 9999999.9_wp  .OR.  ady(1) == 0.0_wp ) ady(1) = dy
+
+    DO  j = 2, number_of_agent_groups
+       IF ( asl(j) == 9999999.9_wp )  asl(j) = asl(j-1)
+       IF ( asr(j) == 9999999.9_wp )  asr(j) = asr(j-1)
+       IF ( ass(j) == 9999999.9_wp )  ass(j) = ass(j-1)
+       IF ( asn(j) == 9999999.9_wp )  asn(j) = asn(j-1)
+       IF ( adx(j) == 9999999.9_wp  .OR.  adx(j) == 0.0_wp ) adx(j) = adx(j-1)
+       IF ( ady(j) == 9999999.9_wp  .OR.  ady(j) == 0.0_wp ) ady(j) = ady(j-1)
+    ENDDO
+
+!
+!-- Check boundary condition and set internal variables
+    SELECT CASE ( bc_mas_lr )
+
+       CASE ( 'cyclic' )
+          ibc_mas_lr = 0
+
+       CASE ( 'absorb' )
+          ibc_mas_lr = 1
+
+       CASE DEFAULT
+          WRITE( message_string, * ) 'unknown boundary condition ',                                &
+                                     'bc_mas_lr = "', TRIM( bc_mas_lr ), '"'
+          CALL message( 'mas_init', 'PA0073', 1, 2, 0, 6, 0 )
+
+    END SELECT
+    SELECT CASE ( bc_mas_ns )
+
+       CASE ( 'cyclic' )
+          ibc_mas_ns = 0
+
+       CASE ( 'absorb' )
+          ibc_mas_ns = 1
+
+       CASE DEFAULT
+          WRITE( message_string, * ) 'unknown boundary condition ',                                &
+                                     'bc_mas_ns = "', TRIM( bc_mas_ns ), '"'
+          CALL message( 'mas_init', 'PA0074', 1, 2, 0, 6, 0 )
+
+    END SELECT
+
+!
+!-- For the first model run of a possible job chain initialize the agents, otherwise read the agent
+!-- data from restart file.
+    IF ( TRIM( initializing_actions ) == 'read_restart_data'  .AND.  read_agents_from_restartfile )&
+    THEN
+
+!           CALL mas_read_restart_file
+
+    ELSE
+!
+!--    Read preprocessed data of navigation mesh and building polygons for agent pathfinding
+       DO  ii = 0, io_blocks-1
+          IF ( ii == io_group )  THEN
+!
+!--          Check for naviation input file and open it
+             INQUIRE( FILE='NAVIGATION_DATA' // TRIM( coupling_char ),                             &
+                      EXIST=navigation_data_present )
+             IF ( .NOT. navigation_data_present )  THEN
+                message_string = 'Input file NAVIGATION_DATA' //                                   &
+                                  TRIM( coupling_char ) // ' for MAS missing. ' //                 &
+                                  '&Please run agent_preprocessing before the job to create it.'
+                CALL message( 'mas_init', 'PA0525', 1, 2, 0, 6, 0 )
+             ENDIF
+             OPEN ( 119, FILE='NAVIGATION_DATA'//TRIM( coupling_char ), FORM='UNFORMATTED',        &
+                         IOSTAT=ioerr )
+!
+!--             Read mesh data
+             READ( 119 ) size_of_mesh
+             ALLOCATE( mesh(1:size_of_mesh) )
+             DO  i = 1, size_of_mesh
+                READ( 119 ) mesh(i)%polygon_id, mesh(i)%vertex_id,                                 &
+                            mesh(i)%noc, mesh(i)%origin_id,                                        &
+                            mesh(i)%cost_so_far, mesh(i)%x,                                        &
+                            mesh(i)%y, mesh(i)%x_s, mesh(i)%y_s
+                ALLOCATE( mesh(i)%connected_vertices(1:mesh(i)%noc),                               &
+                          mesh(i)%distance_to_vertex(1:mesh(i)%noc) )
+                DO  j = 1, mesh(i)%noc
+                   READ( 119 ) mesh(i)%connected_vertices(j),                                      &
+                               mesh(i)%distance_to_vertex(j)
+                ENDDO
              ENDDO
-             s_measure_height(jl,il) = locdum
-
-          ENDDO
+!
+!--             Read polygon data
+             READ( 119 ) size_of_pols
+             ALLOCATE( polygons(1:size_of_pols) )
+             DO  i = 1, size_of_pols
+                READ( 119 ) polygons(i)%nov
+                ALLOCATE( polygons(i)%vertices(0:polygons(i)%nov+1) )
+                DO  j = 0, polygons(i)%nov+1
+                   READ( 119 ) polygons(i)%vertices(j)%delete,                                     &
+                               polygons(i)%vertices(j)%x,                                          &
+                               polygons(i)%vertices(j)%y
+                ENDDO
+             ENDDO
+             CLOSE(119)
+
+          ENDIF
+#if defined( __parallel ) && ! defined ( __check )
+          CALL MPI_BARRIER( comm2d, ierr )
+#endif
        ENDDO
 
-       CALL mas_create_obstacle_flags
-
-!
-!--    Set default start positions, if necessary
-       IF ( asl(1) == 9999999.9_wp )  asl(1) = 0.0_wp
-       IF ( asr(1) == 9999999.9_wp )  asr(1) = ( nx + 1 ) * dx
-       IF ( ass(1) == 9999999.9_wp )  ass(1) = 0.0_wp
-       IF ( asn(1) == 9999999.9_wp )  asn(1) = ( ny + 1 ) * dy
-       IF ( adx(1) == 9999999.9_wp .OR. adx(1) == 0.0_wp ) adx(1) = dx
-       IF ( ady(1) == 9999999.9_wp .OR. ady(1) == 0.0_wp ) ady(1) = dy
-
-       DO  j = 2, number_of_agent_groups
-          IF ( asl(j) == 9999999.9_wp )  asl(j) = asl(j-1)
-          IF ( asr(j) == 9999999.9_wp )  asr(j) = asr(j-1)
-          IF ( ass(j) == 9999999.9_wp )  ass(j) = ass(j-1)
-          IF ( asn(j) == 9999999.9_wp )  asn(j) = asn(j-1)
-          IF ( adx(j) == 9999999.9_wp .OR. adx(j) == 0.0_wp ) adx(j) = adx(j-1)
-          IF ( ady(j) == 9999999.9_wp .OR. ady(j) == 0.0_wp ) ady(j) = ady(j-1)
-       ENDDO
-
-!
-!--    Check boundary condition and set internal variables
-       SELECT CASE ( bc_mas_lr )
-
-          CASE ( 'cyclic' )
-             ibc_mas_lr = 0
-
-          CASE ( 'absorb' )
-             ibc_mas_lr = 1
-
-          CASE DEFAULT
-             WRITE( message_string, * ) 'unknown boundary condition ',         &
-                                        'bc_mas_lr = "', TRIM( bc_mas_lr ), '"'
-             CALL message( 'mas_init', 'PA0073', 1, 2, 0, 6, 0 )
-
-       END SELECT
-       SELECT CASE ( bc_mas_ns )
-
-          CASE ( 'cyclic' )
-             ibc_mas_ns = 0
-
-          CASE ( 'absorb' )
-             ibc_mas_ns = 1
-
-          CASE DEFAULT
-             WRITE( message_string, * ) 'unknown boundary condition ',         &
-                                        'bc_mas_ns = "', TRIM( bc_mas_ns ), '"'
-             CALL message( 'mas_init', 'PA0074', 1, 2, 0, 6, 0 )
-
-       END SELECT
-
-!
-!--    For the first model run of a possible job chain initialize the
-!--    agents, otherwise read the agent data from restart file.
-       IF ( TRIM( initializing_actions ) == 'read_restart_data'                &
-            .AND.  read_agents_from_restartfile )  THEN
-
-!           CALL mas_read_restart_file
-
-       ELSE
-!
-!--       Read preprocessed data of navigation mesh and building polygons
-!--       for agent pathfinding
-          DO ii = 0, io_blocks-1
-             IF ( ii == io_group )  THEN
-!
-!--             Check for naviation input file and open it
-                INQUIRE( FILE='NAVIGATION_DATA' // TRIM( coupling_char ), EXIST=navigation_data_present )
-                IF ( .NOT. navigation_data_present )  THEN
-                   message_string = 'Input file NAVIGATION_DATA' //                &
-                                     TRIM( coupling_char ) // ' for MAS missing. ' // &
-                                     '&Please run agent_preprocessing before the job to create it.'
-                   CALL message( 'mas_init', 'PA0525', 1, 2, 0, 6, 0 )
-                ENDIF
-                OPEN ( 119, FILE='NAVIGATION_DATA'//TRIM( coupling_char ),     &
-                            FORM='UNFORMATTED', IOSTAT=ioerr )
-!
-!--             Read mesh data
-                READ(119) size_of_mesh
-                ALLOCATE( mesh(1:size_of_mesh))
-                DO i = 1, size_of_mesh
-                   READ(119) mesh(i)%polygon_id, mesh(i)%vertex_id,            &
-                             mesh(i)%noc, mesh(i)%origin_id,                   &
-                             mesh(i)%cost_so_far, mesh(i)%x,                   &
-                             mesh(i)%y, mesh(i)%x_s, mesh(i)%y_s
-                   ALLOCATE( mesh(i)%connected_vertices(1:mesh(i)%noc),        &
-                             mesh(i)%distance_to_vertex(1:mesh(i)%noc) )
-                   DO j = 1, mesh(i)%noc
-                      READ(119) mesh(i)%connected_vertices(j),                 &
-                                mesh(i)%distance_to_vertex(j)
-                   ENDDO
-                ENDDO
-!
-!--             Read polygon data
-                READ(119) size_of_pols
-                ALLOCATE( polygons(1:size_of_pols) )
-                DO i = 1, size_of_pols
-                   READ(119) polygons(i)%nov
-                   ALLOCATE( polygons(i)%vertices(0:polygons(i)%nov+1) )
-                   DO j = 0, polygons(i)%nov+1
-                      READ(119) polygons(i)%vertices(j)%delete,                &
-                                polygons(i)%vertices(j)%x,                     &
-                                polygons(i)%vertices(j)%y
-                   ENDDO
-                ENDDO
-                CLOSE(119)
-
-             ENDIF
-#if defined( __parallel ) && ! defined ( __check )
-             CALL MPI_BARRIER( comm2d, ierr )
-#endif
-          ENDDO
-
-!
-!--       Allocate agent arrays and set attributes of the initial set of
-!--       agents, which can be also periodically released at later times.
-          ALLOCATE( agt_count  (nysg:nyng,nxlg:nxrg),                          &
-                    grid_agents(nysg:nyng,nxlg:nxrg) )
-!
-!--       Allocate dummy arrays for pathfinding
-          ALLOCATE( dummy_path_x(0:agt_path_size),                 &
-                    dummy_path_y(0:agt_path_size) )
-
-          number_of_agents = 0
-          sort_count_mas   = 0
-          agt_count        = 0
-
-!
-!--       initialize counter for agent IDs 
-          grid_agents%id_counter = 1
-
-!
-!--       Initialize all agents with dummy values (otherwise errors may
-!--       occur within restart runs). The reason for this is still not clear
-!--       and may be presumably caused by errors in the respective user-interface.
-          zero_agent%agent_mask = .FALSE.
-          zero_agent%block_nr      = -1
-          zero_agent%group         = 0
-          zero_agent%id            = 0_idp
-          zero_agent%path_counter  = agt_path_size
-          zero_agent%age           = 0.0_wp
-          zero_agent%age_m         = 0.0_wp
-          zero_agent%dt_sum        = 0.0_wp
-          zero_agent%clo           = 0.0_wp
-          zero_agent%energy_storage= 0.0_wp
-          zero_agent%force_x       = 0.0_wp
-          zero_agent%force_y       = 0.0_wp
-          zero_agent%origin_x      = 0.0_wp
-          zero_agent%origin_y      = 0.0_wp
-          zero_agent%speed_abs     = 0.0_wp
-          zero_agent%speed_e_x     = 0.0_wp
-          zero_agent%speed_e_y     = 0.0_wp
-          zero_agent%speed_des     = random_normal(desired_speed, des_sp_sig)
-          zero_agent%speed_x       = 0.0_wp
-          zero_agent%speed_y       = 0.0_wp
-          zero_agent%ipt           = 0.0_wp
-          zero_agent%x             = 0.0_wp
-          zero_agent%y             = 0.0_wp
-          zero_agent%path_x        = 0.0_wp
-          zero_agent%path_y        = 0.0_wp
-          zero_agent%t_x           = 0.0_wp
-          zero_agent%t_y           = 0.0_wp
-
-!
-!--    Set a seed value for the random number generator to be exclusively
-!--    used for the agent code. The generated random numbers should be
-!--    different on the different PEs.
-          iran_agent = iran_agent + myid
-
-          CALL mas_create_agent (PHASE_INIT)
-
-       ENDIF
-
-!
-!--    To avoid programm abort, assign agents array to the local version of 
-!--    first grid cell
-       number_of_agents = agt_count(nys,nxl)
-       agents => grid_agents(nys,nxl)%agents(1:number_of_agents)
-
-    END SUBROUTINE mas_init
-
-!------------------------------------------------------------------------------!
+!
+!--    Allocate agent arrays and set attributes of the initial set of agents, which can be also
+!--    periodically released at later times.
+       ALLOCATE( agt_count  (nysg:nyng,nxlg:nxrg),                                                 &
+                 grid_agents(nysg:nyng,nxlg:nxrg) )
+!
+!--    Allocate dummy arrays for pathfinding
+       ALLOCATE( dummy_path_x(0:agt_path_size),                                                    &
+                 dummy_path_y(0:agt_path_size) )
+
+       number_of_agents = 0
+       sort_count_mas   = 0
+       agt_count        = 0
+
+!
+!--    Initialize counter for agent IDs
+       grid_agents%id_counter = 1
+
+!
+!--    Initialize all agents with dummy values (otherwise errors may occur within restart runs).
+!--    The reason for this is still not clear and may be presumably caused by errors in the
+!--    respective user-interface.
+       zero_agent%agent_mask = .FALSE.
+       zero_agent%block_nr      = -1
+       zero_agent%group         = 0
+       zero_agent%id            = 0_idp
+       zero_agent%path_counter  = agt_path_size
+       zero_agent%age           = 0.0_wp
+       zero_agent%age_m         = 0.0_wp
+       zero_agent%dt_sum        = 0.0_wp
+       zero_agent%clo           = 0.0_wp
+       zero_agent%energy_storage= 0.0_wp
+       zero_agent%force_x       = 0.0_wp
+       zero_agent%force_y       = 0.0_wp
+       zero_agent%origin_x      = 0.0_wp
+       zero_agent%origin_y      = 0.0_wp
+       zero_agent%speed_abs     = 0.0_wp
+       zero_agent%speed_e_x     = 0.0_wp
+       zero_agent%speed_e_y     = 0.0_wp
+       zero_agent%speed_des     = random_normal(desired_speed, des_sp_sig)
+       zero_agent%speed_x       = 0.0_wp
+       zero_agent%speed_y       = 0.0_wp
+       zero_agent%ipt           = 0.0_wp
+       zero_agent%x             = 0.0_wp
+       zero_agent%y             = 0.0_wp
+       zero_agent%path_x        = 0.0_wp
+       zero_agent%path_y        = 0.0_wp
+       zero_agent%t_x           = 0.0_wp
+       zero_agent%t_y           = 0.0_wp
+
+!
+!--    Set a seed value for the random number generator to be exclusively used for the agent code.
+!--    The generated random numbers should be different on the different PEs.
+       iran_agent = iran_agent + myid
+
+       CALL mas_create_agent( phase_init )
+
+    ENDIF
+
+!
+!-- To avoid programm abort, assign agents array to the local version of first grid cell.
+    number_of_agents = agt_count(nys,nxl)
+    agents => grid_agents(nys,nxl)%agents(1:number_of_agents)
+
+ END SUBROUTINE mas_init
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Output of informative message about maximum agent number
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_last_actions
-
-       USE control_parameters,                                                 &
-           ONLY:  message_string
-
-       IMPLICIT NONE
-
-       WRITE(message_string,'(A,I8,A)')                                        &
-                         'The maximumn number of agents during this run was',  &
-                         maximum_number_of_agents,                             &
-                         '&Consider adjusting the INPUT parameter'//           &
-                         '&dim_size_agtnum_manual accordingly for the next run.'
-
-       CALL message( 'mas_data_output_agents', 'PA0457', 0, 0, 0, 6, 0 )
-
-    END SUBROUTINE mas_last_actions
-
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_last_actions
+
+    USE control_parameters,                                                                        &
+        ONLY:  message_string
+
+    IMPLICIT NONE
+
+    WRITE(message_string,'(A,I8,A)') 'The maximumn number of agents during this run was',          &
+                                     maximum_number_of_agents,                                     &
+                                     '&Consider adjusting the INPUT parameter'//                   &
+                                     '&dim_size_agtnum_manual accordingly for the next run.'
+
+    CALL message( 'mas_data_output_agents', 'PA0457', 0, 0, 0, 6, 0 )
+
+ END SUBROUTINE mas_last_actions
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> Finds the shortest path from a start position to a target position using the
-!> A*-algorithm
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_nav_a_star( start_x, start_y, target_x, target_y, nsteps )
-
-       IMPLICIT NONE
-
-       LOGICAL ::  target_reached !< flag
-
-       INTEGER(iwp) ::  cur_node     !< current node of binary heap
-       INTEGER(iwp) ::  il           !< counter (x)
-       INTEGER(iwp) ::  neigh_node   !< neighbor node
-       INTEGER(iwp) ::  node_counter !< binary heap node counter
-       INTEGER(iwp) ::  path_ag      !< index of agent path
-       INTEGER(iwp) ::  som          !< size of mesh
-       INTEGER(iwp) ::  steps        !< steps along the path
-       INTEGER(iwp) ::  nsteps        !< number of steps
-
-       REAL(wp) ::  start_x      !< x-coordinate agent
-       REAL(wp) ::  start_y      !< y-coordinate agent
-       REAL(wp) ::  new_cost     !< updated cost to reach node
-       REAL(wp) ::  new_priority !< priority of node to be added to queue
-       REAL(wp) ::  rn_gate      !< random number for corner gate
-       REAL(wp) ::  target_x     !< x-coordinate target
-       REAL(wp) ::  target_y     !< y-coordinate target
-!
-!--    Coordinate Type
-       TYPE coord
-          REAL(wp) ::  x   !< x-coordinate
-          REAL(wp) ::  x_s !< x-coordinate (shifted)
-          REAL(wp) ::  y   !< y-coordinate
-          REAL(wp) ::  y_s !< y-coordinate (shifted)
-       END TYPE coord
-
-       TYPE(coord), DIMENSION(:), ALLOCATABLE, TARGET ::  path     !< path array
-       TYPE(coord), DIMENSION(:), ALLOCATABLE, TARGET ::  tmp_path !< temporary path for resizing
-
-       node_counter = 0
-!
-!--    Create temporary navigation mesh including agent and target positions
-       CALL mas_nav_create_tmp_mesh( start_x, start_y, target_x, target_y, som )
-       tmp_mesh(som)%cost_so_far = 0.0_wp
-!
-!--    Initialize priority queue
-       heap_count = 0_iwp
-       ALLOCATE(queue(0:100))
-       target_reached = .FALSE.
-!
-!--    Add starting point (agent position) to frontier (the frontier consists
-!--    of all the nodes that are to be visited. The node with the smallest
-!--    priority will be visited first. The priority consists of the distance
-!--    from the start node to this node plus a minimal guess (direct distance)
-!--    from this node to the goal). For the starting node, the priority is set
-!--    to 0, as it's the only node thus far
-       CALL mas_heap_insert_item(som,0.0_wp)
-       cur_node = som
-       DO WHILE ( heap_count > 0 )
-!
-!--       Step one: Pick lowest priority item from queue
-          node_counter = node_counter + 1
-          CALL mas_heap_extract_item(cur_node)
-!
-!--       Node 0 is the goal node
-          IF ( cur_node == 0 ) THEN
-             EXIT
+!> Finds the shortest path from a start position to a target position using the A*-algorithm
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_nav_a_star( start_x, start_y, target_x, target_y, nsteps )
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  cur_node      !< current node of binary heap
+    INTEGER(iwp) ::  il            !< counter (x)
+    INTEGER(iwp) ::  neigh_node    !< neighbor node
+    INTEGER(iwp) ::  node_counter  !< binary heap node counter
+    INTEGER(iwp) ::  nsteps        !< number of steps
+    INTEGER(iwp) ::  path_ag       !< index of agent path
+    INTEGER(iwp) ::  som           !< size of mesh
+    INTEGER(iwp) ::  steps         !< steps along the path
+
+    LOGICAL ::  target_reached  !< flag
+
+    REAL(wp) ::  new_cost      !< updated cost to reach node
+    REAL(wp) ::  new_priority  !< priority of node to be added to queue
+    REAL(wp) ::  start_x       !< x-coordinate agent
+    REAL(wp) ::  start_y       !< y-coordinate agent
+    REAL(wp) ::  rn_gate       !< random number for corner gate
+    REAL(wp) ::  target_x      !< x-coordinate target
+    REAL(wp) ::  target_y      !< y-coordinate target
+!
+!-- Coordinate Type
+    TYPE coord
+       REAL(wp) ::  x    !< x-coordinate
+       REAL(wp) ::  x_s  !< x-coordinate (shifted)
+       REAL(wp) ::  y    !< y-coordinate
+       REAL(wp) ::  y_s  !< y-coordinate (shifted)
+    END TYPE coord
+
+    TYPE(coord), DIMENSION(:), ALLOCATABLE, TARGET ::  path      !< path array
+    TYPE(coord), DIMENSION(:), ALLOCATABLE, TARGET ::  tmp_path  !< temporary path for resizing
+
+    node_counter = 0
+!
+!-- Create temporary navigation mesh including agent and target positions
+    CALL mas_nav_create_tmp_mesh( start_x, start_y, target_x, target_y, som )
+    tmp_mesh(som)%cost_so_far = 0.0_wp
+!
+!-- Initialize priority queue
+    heap_count = 0_iwp
+    ALLOCATE( queue(0:100) )
+    target_reached = .FALSE.
+!
+!-- Add starting point (agent position) to frontier (the frontier consists of all the nodes that
+!-- are to be visited. The node with the smallest priority will be visited first. The priority
+!-- consists of the distance from the start node to this node plus a minimal guess (direct
+!-- distance) from this node to the goal). For the starting node, the priority is set to 0, as it's
+!-- the only node thus far.
+    CALL mas_heap_insert_item( som, 0.0_wp )
+    cur_node = som
+    DO WHILE ( heap_count > 0 )
+!
+!--    Step one: Pick lowest priority item from queue
+       node_counter = node_counter + 1
+       CALL mas_heap_extract_item(cur_node)
+!
+!--    Node 0 is the goal node
+       IF ( cur_node == 0 )  THEN
+          EXIT
+       ENDIF
+!
+!--    Loop over all of cur_node's neighbors
+       DO  il = 1, tmp_mesh(cur_node)%noc
+          neigh_node = tmp_mesh(cur_node)%connected_vertices(il)
+!
+!--       Check, if the way from the start node to this neigh_node via cur_node is shorter than the
+!--       previously found shortest path to it.
+!--       If so, replace said cost and add neigh_node to the frontier.
+!--       cost_so_far is initialized as 1.d12 so that all found distances should be smaller.
+          new_cost   = tmp_mesh(cur_node)%cost_so_far + tmp_mesh(cur_node)%distance_to_vertex(il)
+          IF ( new_cost < tmp_mesh(neigh_node)%cost_so_far )  THEN
+             tmp_mesh(neigh_node)%cost_so_far = new_cost
+             tmp_mesh(neigh_node)%origin_id   = cur_node
+!
+!--             Priority in the queue is cost_so_far + heuristic to goal
+             new_priority = new_cost                                                               &
+                            + heuristic(tmp_mesh(neigh_node)%x,                                    &
+                              tmp_mesh(neigh_node)%y, tmp_mesh(0)%x,                               &
+                              tmp_mesh(0)%y)
+             CALL mas_heap_insert_item(neigh_node,new_priority)
           ENDIF
-!
-!--       Loop over all of cur_node's neighbors
-          DO il = 1, tmp_mesh(cur_node)%noc
-             neigh_node = tmp_mesh(cur_node)%connected_vertices(il)
-!
-!--          Check, if the way from the start node to this neigh_node via
-!--          cur_node is shorter than the previously found shortest path to it.
-!--          If so, replace said cost and add neigh_node to the frontier.
-!--          cost_so_far is initialized as 1.d12 so that all found distances
-!--          should be smaller.
-             new_cost   = tmp_mesh(cur_node)%cost_so_far                       &
-                         + tmp_mesh(cur_node)%distance_to_vertex(il)
-             IF ( new_cost < tmp_mesh(neigh_node)%cost_so_far ) THEN
-                tmp_mesh(neigh_node)%cost_so_far = new_cost
-                tmp_mesh(neigh_node)%origin_id   = cur_node
-!
-!--             Priority in the queue is cost_so_far + heuristic to goal
-                new_priority = new_cost                                        &
-                              + heuristic(tmp_mesh(neigh_node)%x,              &
-                                tmp_mesh(neigh_node)%y, tmp_mesh(0)%x,         &
-                                tmp_mesh(0)%y)
-                CALL mas_heap_insert_item(neigh_node,new_priority)
-             ENDIF
-          ENDDO
        ENDDO
-!
-!--    Add nodes to a path array. To do this, we must backtrack from the target
-!--    node to its origin to its origin and so on until an node is reached that
-!--    has no origin (%origin_id == -1). This is the starting node.
-       DEALLOCATE(queue)
-       cur_node = 0
-       steps = 0
-       ALLOCATE(path(1:100))
-       DO WHILE ( cur_node /= -1 )
-          steps = steps + 1
-!
-!--       Resize path array if necessary
-          IF ( steps > SIZE(path) ) THEN
-             ALLOCATE(tmp_path(1:steps-1))
-             tmp_path(1:steps-1) = path(1:steps-1)
-             DEALLOCATE(path)
-             ALLOCATE(path(1:2*(steps-1)))
-             path(1:steps-1) = tmp_path(1:steps-1)
-             DEALLOCATE(tmp_path)
-          ENDIF
-          path(steps)%x = tmp_mesh(cur_node)%x
-          path(steps)%y = tmp_mesh(cur_node)%y
-          path(steps)%x_s = tmp_mesh(cur_node)%x_s
-          path(steps)%y_s = tmp_mesh(cur_node)%y_s
-          cur_node = tmp_mesh(cur_node)%origin_id
-       ENDDO
-!
-!--    Add calculated intermittent targets to the path until either the
-!--    target or the maximum number of intermittent targets is reached.
-!--    Ignore starting point (reduce index by one), it is agent position.
-       dummy_path_x = -1
-       dummy_path_y = -1
-       path_ag = 1
+    ENDDO
+!
+!-- Add nodes to a path array. To do this, we must backtrack from the target node to its origin and
+!-- so on until a node is reached that has no origin (%origin_id == -1). This is the starting node.
+    DEALLOCATE( queue )
+    cur_node = 0
+    steps = 0
+    ALLOCATE( path(1:100) )
+    DO WHILE ( cur_node /= -1 )
+       steps = steps + 1
+!
+!--    Resize path array if necessary
+       IF ( steps > SIZE(path) )  THEN
+          ALLOCATE( tmp_path(1:steps-1) )
+          tmp_path(1:steps-1) = path(1:steps-1)
+          DEALLOCATE( path )
+          ALLOCATE( path(1:2*(steps-1)) )
+          path(1:steps-1) = tmp_path(1:steps-1)
+          DEALLOCATE( tmp_path )
+       ENDIF
+       path(steps)%x = tmp_mesh(cur_node)%x
+       path(steps)%y = tmp_mesh(cur_node)%y
+       path(steps)%x_s = tmp_mesh(cur_node)%x_s
+       path(steps)%y_s = tmp_mesh(cur_node)%y_s
+       cur_node = tmp_mesh(cur_node)%origin_id
+    ENDDO
+!
+!-- Add calculated intermittent targets to the path until either the target or the maximum number of
+!-- intermittent targets is reached.
+!-- Ignore starting point (reduce index by one), it is agent position.
+    dummy_path_x = -1
+    dummy_path_y = -1
+    path_ag = 1
+    steps = steps - 1
+    nsteps = 0
+    DO WHILE( steps > 0 .AND. path_ag <= agt_path_size )
+!
+!--    Each target point is randomly chosen along a line target along the bisector of the building
+!--    corner that starts at corner_gate_start and has a width of corner_gate_width. This is to
+!--    avoid clustering when opposing agent groups try to reach the same corner target.
+       rn_gate = random_function(iran_agent) * corner_gate_width + corner_gate_start
+       dummy_path_x(path_ag) = path(steps)%x + rn_gate * (path(steps)%x_s - path(steps)%x)
+       dummy_path_y(path_ag) = path(steps)%y + rn_gate * (path(steps)%y_s - path(steps)%y)
        steps = steps - 1
-       nsteps = 0
-       DO WHILE( steps > 0 .AND. path_ag <= agt_path_size )
-!
-!--       Each target point is randomly chosen along a line target along the
-!--       bisector of the building corner that starts at corner_gate_start
-!--       and has a width of corner_gate_width. This is to avoid clustering
-!--       when opposing agent groups try to reach the same corner target.
-          rn_gate = random_function(iran_agent) * corner_gate_width            &
-                                                + corner_gate_start
-          dummy_path_x(path_ag) = path(steps)%x + rn_gate                      &
-                         * (path(steps)%x_s - path(steps)%x)
-          dummy_path_y(path_ag) = path(steps)%y + rn_gate                      &
-                         * (path(steps)%y_s - path(steps)%y)
-          steps = steps - 1
-          path_ag = path_ag + 1
-          nsteps = nsteps + 1
-       ENDDO
-!
-!--    Set current intermittent target of this agent
-       DEALLOCATE(tmp_mesh, path)
-
-    END SUBROUTINE mas_nav_a_star
-
-!------------------------------------------------------------------------------! 
+       path_ag = path_ag + 1
+       nsteps = nsteps + 1
+    ENDDO
+!
+!-- Set current intermittent target of this agent
+    DEALLOCATE( tmp_mesh, path )
+
+ END SUBROUTINE mas_nav_a_star
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> Adds a connection between two points of the navigation mesh 
-!> (one-way: in_mp1 to in_mp2)
-!------------------------------------------------------------------------------! 
-    SUBROUTINE mas_nav_add_connection ( in_mp1, id2, in_mp2 )
-
-       IMPLICIT NONE
-
-       LOGICAL ::  connection_established  !< Flag to indicate if connection has already been established
-
-       INTEGER(iwp) ::  id2  !< ID of in_mp2
-       INTEGER(iwp) ::  il   !< local counter
-       INTEGER(iwp) ::  noc1 !< number of connections in in_mp1
-
-       INTEGER, DIMENSION(:), ALLOCATABLE ::  dum_cv !< dummy array for connected_vertices
-
-       REAL(wp) ::  dist  !< Distance between the two points
-
-       REAL(wp), DIMENSION(:), ALLOCATABLE ::  dum_dtv
-
-       TYPE(mesh_point) ::  in_mp1  !< mesh point that gets a new connection
-       TYPE(mesh_point) ::  in_mp2  !< mesh point in_mp1 will be connected to
-
-       connection_established = .FALSE.
-!
-!--    Check if connection has already been established
-       noc1 = SIZE(in_mp1%connected_vertices)
-       DO il = 1, in_mp1%noc
-          IF ( in_mp1%connected_vertices(il) == id2 ) THEN
-             connection_established = .TRUE.
-             EXIT
-          ENDIF
-       ENDDO
-
-       IF ( .NOT. connection_established ) THEN
-!
-!--       Resize arrays, if necessary
-          IF ( in_mp1%noc >= noc1 ) THEN
-             ALLOCATE( dum_cv(1:noc1),dum_dtv(1:noc1) )
-             dum_cv  = in_mp1%connected_vertices
-             dum_dtv = in_mp1%distance_to_vertex
-             DEALLOCATE( in_mp1%connected_vertices, in_mp1%distance_to_vertex )
-             ALLOCATE( in_mp1%connected_vertices(1:2*noc1),                    &
-                       in_mp1%distance_to_vertex(1:2*noc1) )
-             in_mp1%connected_vertices         = -999
-             in_mp1%distance_to_vertex         = -999.
-             in_mp1%connected_vertices(1:noc1) = dum_cv
-             in_mp1%distance_to_vertex(1:noc1) = dum_dtv
-          ENDIF
-
-!
-!--       Add connection
-          in_mp1%noc = in_mp1%noc+1
-          dist = SQRT( (in_mp1%x - in_mp2%x)**2 + (in_mp1%y - in_mp2%y)**2 )
-          in_mp1%connected_vertices(in_mp1%noc) = id2
-          in_mp1%distance_to_vertex(in_mp1%noc) = dist
+!> Adds a connection between two points of the navigation mesh (one-way: in_mp1 to in_mp2)
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_nav_add_connection ( in_mp1, id2, in_mp2 )
+
+    IMPLICIT NONE
+
+    LOGICAL ::  connection_established  !< Flag to indicate if connection has already been established
+
+    INTEGER(iwp) ::  id2   !< ID of in_mp2
+    INTEGER(iwp) ::  il    !< local counter
+    INTEGER(iwp) ::  noc1  !< number of connections in in_mp1
+
+    INTEGER, DIMENSION(:), ALLOCATABLE ::  dum_cv  !< dummy array for connected_vertices
+
+    REAL(wp) ::  dist  !< Distance between the two points
+
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  dum_dtv
+
+    TYPE(mesh_point) ::  in_mp1  !< mesh point that gets a new connection
+    TYPE(mesh_point) ::  in_mp2  !< mesh point in_mp1 will be connected to
+
+    connection_established = .FALSE.
+!
+!-- Check if connection has already been established
+    noc1 = SIZE( in_mp1%connected_vertices )
+    DO  il = 1, in_mp1%noc
+       IF ( in_mp1%connected_vertices(il) == id2 )  THEN
+          connection_established = .TRUE.
+          EXIT
        ENDIF
-
-    END SUBROUTINE mas_nav_add_connection
-
-!------------------------------------------------------------------------------!
+    ENDDO
+
+    IF ( .NOT. connection_established )  THEN
+!
+!--    Resize arrays, if necessary
+       IF ( in_mp1%noc >= noc1 )  THEN
+          ALLOCATE( dum_cv(1:noc1),dum_dtv(1:noc1) )
+          dum_cv  = in_mp1%connected_vertices
+          dum_dtv = in_mp1%distance_to_vertex
+          DEALLOCATE( in_mp1%connected_vertices, in_mp1%distance_to_vertex )
+          ALLOCATE( in_mp1%connected_vertices(1:2*noc1),                                           &
+                    in_mp1%distance_to_vertex(1:2*noc1) )
+          in_mp1%connected_vertices         = -999
+          in_mp1%distance_to_vertex         = -999.
+          in_mp1%connected_vertices(1:noc1) = dum_cv
+          in_mp1%distance_to_vertex(1:noc1) = dum_dtv
+       ENDIF
+
+!
+!--    Add connection
+       in_mp1%noc = in_mp1%noc+1
+       dist = SQRT( (in_mp1%x - in_mp2%x)**2 + (in_mp1%y - in_mp2%y)**2 )
+       in_mp1%connected_vertices(in_mp1%noc) = id2
+       in_mp1%distance_to_vertex(in_mp1%noc) = dist
+    ENDIF
+
+ END SUBROUTINE mas_nav_add_connection
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Adds a vertex (curren position of agent or target) to the existing tmp_mesh
-!------------------------------------------------------------------------------! 
-    SUBROUTINE mas_nav_add_vertex_to_mesh ( in_mp, in_id )
-
-       IMPLICIT NONE
-
-       LOGICAL ::  intersection_found !< flag
-
-       INTEGER(iwp) ::  jl    !< mesh point counter
-       INTEGER(iwp) ::  pl    !< polygon counter
-       INTEGER(iwp) ::  vl    !< vertex counter
-       INTEGER(iwp) ::  pid_t !< polygon id of tested mesh point
-       INTEGER(iwp) ::  vid_t !< vertex id of tested mesh point
-       INTEGER(iwp) ::  in_id !< vertex id of tested mesh point
-
-       LOGICAL ::  is_left_n  !< local switch
-       LOGICAL ::  is_left_p  !< local switch
-       LOGICAL ::  is_right_n !< local switch
-       LOGICAL ::  is_right_p !< local switch
-
-       REAL(wp) ::  v1x !< x-coordinate of test vertex 1 for intersection test
-       REAL(wp) ::  v1y !< y-coordinate of test vertex 1 for intersection test
-       REAL(wp) ::  v2x !< x-coordinate of test vertex 2 for intersection test
-       REAL(wp) ::  v2y !< y-coordinate of test vertex 2 for intersection test
-       REAL(wp) ::  x   !< x-coordinate of current mesh point
-       REAL(wp) ::  x_t !< x-coordinate of tested mesh point
-       REAL(wp) ::  y   !< y-coordinate of current mesh point
-       REAL(wp) ::  y_t !< y-coordinate of tested mesh point
-
-       TYPE(mesh_point) ::  in_mp !< Input mesh point
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_nav_add_vertex_to_mesh ( in_mp, in_id )
+
+    IMPLICIT NONE
+
+
+    INTEGER(iwp) ::  in_id  !< vertex id of tested mesh point
+    INTEGER(iwp) ::  jl     !< mesh point counter
+    INTEGER(iwp) ::  pl     !< polygon counter
+    INTEGER(iwp) ::  vl     !< vertex counter
+    INTEGER(iwp) ::  pid_t  !< polygon id of tested mesh point
+    INTEGER(iwp) ::  vid_t  !< vertex id of tested mesh point
+
+    LOGICAL ::  intersection_found  !< flag
+    LOGICAL ::  is_left_n           !< local switch
+    LOGICAL ::  is_left_p           !< local switch
+    LOGICAL ::  is_right_n          !< local switch
+    LOGICAL ::  is_right_p          !< local switch
+
+    REAL(wp) ::  v1x  !< x-coordinate of test vertex 1 for intersection test
+    REAL(wp) ::  v1y  !< y-coordinate of test vertex 1 for intersection test
+    REAL(wp) ::  v2x  !< x-coordinate of test vertex 2 for intersection test
+    REAL(wp) ::  v2y  !< y-coordinate of test vertex 2 for intersection test
+    REAL(wp) ::  x    !< x-coordinate of current mesh point
+    REAL(wp) ::  x_t  !< x-coordinate of tested mesh point
+    REAL(wp) ::  y    !< y-coordinate of current mesh point
+    REAL(wp) ::  y_t  !< y-coordinate of tested mesh point
+
+    TYPE(mesh_point) ::  in_mp  !< Input mesh point
 !
 !--
-       x = in_mp%x
-       y = in_mp%y
-       DO jl = 0, SIZE(tmp_mesh)-2
-          IF ( in_id == jl ) CYCLE
-!
-!--       Ignore mesh points with 0 connections
-          IF ( tmp_mesh(jl)%polygon_id /= -1 ) THEN
-             IF ( tmp_mesh(jl)%noc == 0 ) CYCLE
-          ENDIF
-          x_t = tmp_mesh(jl)%x
-          y_t = tmp_mesh(jl)%y
-          pid_t = tmp_mesh(jl)%polygon_id
-          vid_t = tmp_mesh(jl)%vertex_id
-!
-!--       If the connecting line between the target and a mesh point points 
-!--       into the mesh point's polygon, no connection will be 
-!--       established between the two points. This is the case if the 
-!--       previous (next, n) vertex of the polygon is right of the connecting
-!--       line and the next (previous, p) vertex of the polygon is left of the
-!--       connecting line.
-          IF ( pid_t > 0  .AND.  pid_t <= SIZE( polygons ) )  THEN
-             is_left_p  = is_left(  x, y, x_t, y_t, polygons(pid_t)%vertices(vid_t-1)%x,           &
-                                    polygons(pid_t)%vertices(vid_t-1)%y )
-             is_left_n  = is_left(  x, y, x_t, y_t, polygons(pid_t)%vertices(vid_t+1)%x,           &
-                                    polygons(pid_t)%vertices(vid_t+1)%y )
-             is_right_p = is_right( x, y, x_t, y_t, polygons(pid_t)%vertices(vid_t-1)%x,           &
-                                    polygons(pid_t)%vertices(vid_t-1)%y )
-             is_right_n = is_right( x, y, x_t, y_t, polygons(pid_t)%vertices(vid_t+1)%x,           &
-                                    polygons(pid_t)%vertices(vid_t+1)%y )
-             IF ( ( is_left_p .AND. is_right_n )  .OR.  ( is_right_p .AND. is_left_n ) )  CYCLE
-          ENDIF
-!
-!--       For each edge of each polygon, check if it intersects with the 
-!--       potential connection. If at least one intersection is found,
-!--       no connection can be made
-          intersection_found = .FALSE.
-          DO pl = 1, SIZE(polygons)
-             DO vl = 1, polygons(pl)%nov
-                v1x = polygons(pl)%vertices(vl)%x
-                v1y = polygons(pl)%vertices(vl)%y
-                v2x = polygons(pl)%vertices(vl+1)%x
-                v2y = polygons(pl)%vertices(vl+1)%y
-                intersection_found = intersect(x,y,x_t,y_t,v1x,v1y,v2x,v2y)
-                IF ( intersection_found ) THEN
-                   EXIT
-                ENDIF
-             ENDDO
-             IF ( intersection_found ) EXIT
+    x = in_mp%x
+    y = in_mp%y
+    DO  jl = 0, SIZE( tmp_mesh )-2
+       IF ( in_id == jl )  CYCLE
+!
+!--    Ignore mesh points with 0 connections
+       IF ( tmp_mesh(jl)%polygon_id /= -1 )  THEN
+          IF ( tmp_mesh(jl)%noc == 0 )  CYCLE
+       ENDIF
+       x_t = tmp_mesh(jl)%x
+       y_t = tmp_mesh(jl)%y
+       pid_t = tmp_mesh(jl)%polygon_id
+       vid_t = tmp_mesh(jl)%vertex_id
+!
+!--    If the connecting line between the target and a mesh point points into the mesh point's
+!--    polygon, no connection will be established between the two points. This is the case if the
+!--    previous (next, n) vertex of the polygon is right of the connecting line and the next
+!--    (previous, p) vertex of the polygon is left of the connecting line.
+       IF ( pid_t > 0  .AND.  pid_t <= SIZE( polygons ) )  THEN
+          is_left_p  = is_left(  x, y, x_t, y_t, polygons(pid_t)%vertices(vid_t-1)%x,              &
+                                 polygons(pid_t)%vertices(vid_t-1)%y )
+          is_left_n  = is_left(  x, y, x_t, y_t, polygons(pid_t)%vertices(vid_t+1)%x,              &
+                                 polygons(pid_t)%vertices(vid_t+1)%y )
+          is_right_p = is_right( x, y, x_t, y_t, polygons(pid_t)%vertices(vid_t-1)%x,              &
+                                 polygons(pid_t)%vertices(vid_t-1)%y )
+          is_right_n = is_right( x, y, x_t, y_t, polygons(pid_t)%vertices(vid_t+1)%x,              &
+                                 polygons(pid_t)%vertices(vid_t+1)%y )
+          IF ( ( is_left_p .AND. is_right_n )  .OR.  ( is_right_p .AND. is_left_n ) )  CYCLE
+       ENDIF
+!
+!--    For each edge of each polygon, check if it intersects with the potential connection. If at
+!--    least one intersection is found, no connection can be made.
+       intersection_found = .FALSE.
+       DO  pl = 1, SIZE( polygons )
+          DO  vl = 1, polygons(pl)%nov
+             v1x = polygons(pl)%vertices(vl)%x
+             v1y = polygons(pl)%vertices(vl)%y
+             v2x = polygons(pl)%vertices(vl+1)%x
+             v2y = polygons(pl)%vertices(vl+1)%y
+             intersection_found = intersect(x,y,x_t,y_t,v1x,v1y,v2x,v2y)
+             IF ( intersection_found )  THEN
+                EXIT
+             ENDIF
           ENDDO
-          IF ( intersection_found ) CYCLE
-!
-!--       If neither of the above two test was true, a connection will be
-!--       established between the two mesh points.
-          CALL mas_nav_add_connection(in_mp,jl, tmp_mesh(jl))
-          CALL mas_nav_add_connection(tmp_mesh(jl),in_id, in_mp)
+          IF ( intersection_found )  EXIT
        ENDDO
-       CALL mas_nav_reduce_connections(in_mp)
-
-    END SUBROUTINE mas_nav_add_vertex_to_mesh
-
-!------------------------------------------------------------------------------!
+       IF ( intersection_found )  CYCLE
+!
+!--    If neither of the above two tests was true, a connection will be established between the two
+!--    mesh points.
+       CALL mas_nav_add_connection(in_mp,jl, tmp_mesh(jl))
+       CALL mas_nav_add_connection(tmp_mesh(jl),in_id, in_mp)
+    ENDDO
+    CALL mas_nav_reduce_connections(in_mp)
+
+ END SUBROUTINE mas_nav_add_vertex_to_mesh
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Creates a temporary copy of the navigation mesh to be used for pathfinding
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_nav_create_tmp_mesh( a_x, a_y, t_x, t_y, som )
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  som !< size of mesh
-       INTEGER(iwp) ::  noc !< number of connetions
-       INTEGER(iwp) ::  im  !< local mesh point counter
-
-       REAL(wp) ::  a_x !< x-coordinate agent
-       REAL(wp) ::  a_y !< y-coordinate agent
-       REAL(wp) ::  t_x !< x-coordinate target
-       REAL(wp) ::  t_y !< y-coordinate target
-!
-!--    give tmp_mesh the size of mesh
-       som = SIZE(mesh)+1
-       ALLOCATE(tmp_mesh(0:som))
-!
-!--    give the allocatable variables in tmp_mesh their respctive sizes
-       DO im = 1, som-1
-          noc = mesh(im)%noc
-          ALLOCATE(tmp_mesh(im)%connected_vertices(1:noc))
-          ALLOCATE(tmp_mesh(im)%distance_to_vertex(1:noc))
-       ENDDO
-!
-!--    copy mesh to tmp_mesh
-       tmp_mesh(1:som-1) = mesh(1:som-1)
-!
-!--    Add target point ...
-       CALL mas_nav_init_mesh_point(tmp_mesh(0),-1_iwp,-1_iwp,t_x, t_y)
-       CALL mas_nav_add_vertex_to_mesh(tmp_mesh(0),0_iwp)
-!
-!--    ... and start point to temp mesh
-       CALL mas_nav_init_mesh_point(tmp_mesh(som),-1_iwp,-1_iwp,a_x, a_y)
-       CALL mas_nav_add_vertex_to_mesh(tmp_mesh(som),som)
-
-    END SUBROUTINE mas_nav_create_tmp_mesh
-    
-
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_nav_create_tmp_mesh( a_x, a_y, t_x, t_y, som )
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  im   !< local mesh point counter
+    INTEGER(iwp) ::  noc  !< number of connetions
+    INTEGER(iwp) ::  som  !< size of mesh
+
+    REAL(wp) ::  a_x  !< x-coordinate agent
+    REAL(wp) ::  a_y  !< y-coordinate agent
+    REAL(wp) ::  t_x  !< x-coordinate target
+    REAL(wp) ::  t_y  !< y-coordinate target
+!
+!-- Give tmp_mesh the size of mesh
+    som = SIZE( mesh ) + 1
+    ALLOCATE( tmp_mesh(0:som) )
+!
+!-- Give the allocatable variables in tmp_mesh their respctive sizes
+    DO  im = 1, som-1
+       noc = mesh(im)%noc
+       ALLOCATE( tmp_mesh(im)%connected_vertices(1:noc) )
+       ALLOCATE( tmp_mesh(im)%distance_to_vertex(1:noc) )
+    ENDDO
+!
+!-- Copy mesh to tmp_mesh
+    tmp_mesh(1:som-1) = mesh(1:som-1)
+!
+!-- Add target point ...
+    CALL mas_nav_init_mesh_point(tmp_mesh(0),-1_iwp,-1_iwp,t_x, t_y)
+    CALL mas_nav_add_vertex_to_mesh(tmp_mesh(0),0_iwp)
+!
+!-- ... and start point to temp mesh
+    CALL mas_nav_init_mesh_point(tmp_mesh(som),-1_iwp,-1_iwp,a_x, a_y)
+    CALL mas_nav_add_vertex_to_mesh(tmp_mesh(som),som)
+
+ END SUBROUTINE mas_nav_create_tmp_mesh
+
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> Finds the shortest path from an agents' position to her target. As the
-!> actual pathfinding algorithm uses the obstacle corners and then shifts them
-!> outward after pathfinding, cases can uccur in which the connection between
-!> these intermittent targets then intersect with obstacles. To remedy this
-!> the pathfinding algorithm is then run on every two subsequent intermittent
-!> targets iteratively and new intermittent targets may be added to the path
-!> this way.
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_nav_find_path( nl )
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  nl            !< local agent counter
-       INTEGER(iwp) ::  il            !< local counter
-       INTEGER(iwp) ::  jl            !< local counter
-       INTEGER(iwp) ::  kl            !< local counter
-       INTEGER(iwp) ::  nsteps_total  !< number of steps on path
-       INTEGER(iwp) ::  nsteps_dummy  !< number of steps on path
-       
-       REAL(wp), DIMENSION(0:30) ::  ld_path_x !< local dummy agent path to target (x)
-       REAL(wp), DIMENSION(0:30) ::  ld_path_y !< local dummy agent path to target (y)
-!
-!--    Initialize agent path arrays
-       agents(nl)%path_x    = -1
-       agents(nl)%path_y    = -1
-       agents(nl)%path_x(0) = agents(nl)%x
-       agents(nl)%path_y(0) = agents(nl)%y
-!
-!--    Calculate initial path
-       CALL mas_nav_a_star( agents(nl)%x,   agents(nl)%y,                      &
-                            agents(nl)%t_x, agents(nl)%t_y, nsteps_total )
-!
-!--    Set the rest of the agent path that was just calculated
-       agents(nl)%path_x(1:nsteps_total) = dummy_path_x(1:nsteps_total)
-       agents(nl)%path_y(1:nsteps_total) = dummy_path_y(1:nsteps_total)
-!
-!--    Iterate through found path and check more intermittent targets need
-!--    to be added. For this, run pathfinding between every two consecutive
-!--    intermittent targets.
-       DO il = 0, MIN(agt_path_size-1, nsteps_total-1)
-!
-!--       pathfinding between two consecutive intermittent targets
-          CALL mas_nav_a_star( agents(nl)%path_x(il),   agents(nl)%path_y(il), &
-                              agents(nl)%path_x(il+1), agents(nl)%path_y(il+1),&
-                              nsteps_dummy )
-          nsteps_dummy = nsteps_dummy - 1
-!
-!--       If additional intermittent targets are found, add them to the path
-          IF ( nsteps_dummy > 0 ) THEN
-             ld_path_x = -1
-             ld_path_y = -1
-             ld_path_x(il+1:il+nsteps_dummy) = dummy_path_x(1:nsteps_dummy)
-             ld_path_y(il+1:il+nsteps_dummy) = dummy_path_y(1:nsteps_dummy)
-             kl = 1
-             DO jl = il+1,nsteps_total
-               ld_path_x( il+nsteps_dummy+kl ) = agents(nl)%path_x(jl)
-               ld_path_y( il+nsteps_dummy+kl ) = agents(nl)%path_y(jl)
-               kl = kl + 1
-               IF ( kl > agt_path_size ) EXIT
-             ENDDO
-             nsteps_total = MIN(nsteps_total + nsteps_dummy, agt_path_size)
-             agents(nl)%path_x(il+1:nsteps_total) = ld_path_x(il+1:nsteps_total)
-             agents(nl)%path_y(il+1:nsteps_total) = ld_path_y(il+1:nsteps_total)
-          ENDIF
-
-       ENDDO
-!
-!--    reset path counter to first intermittent target
-       agents(nl)%path_counter = 1
-
-    END SUBROUTINE mas_nav_find_path
-
-!------------------------------------------------------------------------------!
+!> Finds the shortest path from an agents' position to her target. As the actual pathfinding
+!> algorithm uses the obstacle corners and then shifts them outward after pathfinding, cases can
+!> occur in which the connection between these intermittent targets then intersect with obstacles.
+!> To remedy this the pathfinding algorithm is then run on every two subsequent intermittent targets
+!> iteratively and new intermittent targets may be added to the path this way.
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_nav_find_path( nl )
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  il            !< local counter
+    INTEGER(iwp) ::  jl            !< local counter
+    INTEGER(iwp) ::  kl            !< local counter
+    INTEGER(iwp) ::  nl            !< local agent counter
+    INTEGER(iwp) ::  nsteps_dummy  !< number of steps on path
+    INTEGER(iwp) ::  nsteps_total  !< number of steps on path
+
+    REAL(wp), DIMENSION(0:30) ::  ld_path_x  !< local dummy agent path to target (x)
+    REAL(wp), DIMENSION(0:30) ::  ld_path_y  !< local dummy agent path to target (y)
+!
+!-- Initialize agent path arrays
+    agents(nl)%path_x    = -1
+    agents(nl)%path_y    = -1
+    agents(nl)%path_x(0) = agents(nl)%x
+    agents(nl)%path_y(0) = agents(nl)%y
+!
+!-- Calculate initial path
+    CALL mas_nav_a_star( agents(nl)%x, agents(nl)%y, agents(nl)%t_x, agents(nl)%t_y, nsteps_total )
+!
+!-- Set the rest of the agent path that was just calculated
+    agents(nl)%path_x(1:nsteps_total) = dummy_path_x(1:nsteps_total)
+    agents(nl)%path_y(1:nsteps_total) = dummy_path_y(1:nsteps_total)
+!
+!-- Iterate through found path and check more intermittent targets need to be added. For this, run
+!-- pathfinding between every two consecutive intermittent targets.
+    DO  il = 0, MIN( agt_path_size-1, nsteps_total-1 )
+!
+!--    Pathfinding between two consecutive intermittent targets
+       CALL mas_nav_a_star( agents(nl)%path_x(il),   agents(nl)%path_y(il),                        &
+                            agents(nl)%path_x(il+1), agents(nl)%path_y(il+1),                      &
+                            nsteps_dummy )
+       nsteps_dummy = nsteps_dummy - 1
+!
+!--    If additional intermittent targets are found, add them to the path
+       IF ( nsteps_dummy > 0 )  THEN
+          ld_path_x = -1
+          ld_path_y = -1
+          ld_path_x(il+1:il+nsteps_dummy) = dummy_path_x(1:nsteps_dummy)
+          ld_path_y(il+1:il+nsteps_dummy) = dummy_path_y(1:nsteps_dummy)
+          kl = 1
+          DO  jl = il+1,nsteps_total
+            ld_path_x( il+nsteps_dummy+kl ) = agents(nl)%path_x(jl)
+            ld_path_y( il+nsteps_dummy+kl ) = agents(nl)%path_y(jl)
+            kl = kl + 1
+            IF ( kl > agt_path_size )  EXIT
+          ENDDO
+          nsteps_total = MIN( nsteps_total + nsteps_dummy, agt_path_size )
+          agents(nl)%path_x(il+1:nsteps_total) = ld_path_x(il+1:nsteps_total)
+          agents(nl)%path_y(il+1:nsteps_total) = ld_path_y(il+1:nsteps_total)
+       ENDIF
+
+    ENDDO
+!
+!-- Reset path counter to first intermittent target
+    agents(nl)%path_counter = 1
+
+ END SUBROUTINE mas_nav_find_path
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> Reduces the size of connection array to the amount of actual connections
-!> after all connetions were added to a mesh point
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_nav_reduce_connections ( in_mp )
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  noc  !< number of connections
-
-       INTEGER, DIMENSION(:), ALLOCATABLE ::  dum_cv   !< dummy connected_vertices
-
-       REAL(wp), DIMENSION(:), ALLOCATABLE ::  dum_dtv !< dummy distance_to_vertex
-
-       TYPE(mesh_point) ::  in_mp
-
-       noc = in_mp%noc
-       ALLOCATE( dum_cv(1:noc),dum_dtv(1:noc) )
-       dum_cv  = in_mp%connected_vertices(1:noc)
-       dum_dtv = in_mp%distance_to_vertex(1:noc)
-       DEALLOCATE( in_mp%connected_vertices, in_mp%distance_to_vertex )
-       ALLOCATE( in_mp%connected_vertices(1:noc),                    &
-                 in_mp%distance_to_vertex(1:noc) )
-       in_mp%connected_vertices(1:noc) = dum_cv(1:noc)
-       in_mp%distance_to_vertex(1:noc) = dum_dtv(1:noc)
-
-    END SUBROUTINE mas_nav_reduce_connections
-
-!------------------------------------------------------------------------------!
+!> Reduces the size of connection array to the amount of actual connections after all connetions
+!> were added to a mesh point
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_nav_reduce_connections ( in_mp )
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  noc  !< number of connections
+
+    INTEGER, DIMENSION(:), ALLOCATABLE ::  dum_cv    !< dummy connected_vertices
+
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  dum_dtv  !< dummy distance_to_vertex
+
+    TYPE(mesh_point) ::  in_mp
+
+    noc = in_mp%noc
+    ALLOCATE( dum_cv(1:noc),dum_dtv(1:noc) )
+    dum_cv  = in_mp%connected_vertices(1:noc)
+    dum_dtv = in_mp%distance_to_vertex(1:noc)
+    DEALLOCATE( in_mp%connected_vertices, in_mp%distance_to_vertex )
+    ALLOCATE( in_mp%connected_vertices(1:noc),                                                     &
+              in_mp%distance_to_vertex(1:noc) )
+    in_mp%connected_vertices(1:noc) = dum_cv(1:noc)
+    in_mp%distance_to_vertex(1:noc) = dum_dtv(1:noc)
+
+ END SUBROUTINE mas_nav_reduce_connections
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Initializes a point of the navigation mesh
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_nav_init_mesh_point ( in_mp, pid, vid, x, y )
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  pid !< polygon ID
-       INTEGER(iwp) ::  vid !< vertex ID
-
-       REAL(wp) ::  x !< x-coordinate
-       REAL(wp) ::  y !< y-coordinate
-
-       TYPE(mesh_point) ::  in_mp !< mesh point to be initialized
-
-       in_mp%origin_id          = -1
-       in_mp%polygon_id         = pid
-       in_mp%vertex_id          = vid
-       in_mp%cost_so_far        = 1.d12
-       in_mp%x                  = x
-       in_mp%y                  = y
-       in_mp%x_s                = x
-       in_mp%y_s                = y
-       ALLOCATE(in_mp%connected_vertices(1:100),                               &
-                in_mp%distance_to_vertex(1:100))
-       in_mp%connected_vertices = -999
-       in_mp%distance_to_vertex = -999.
-       in_mp%noc                = 0
-
-    END SUBROUTINE mas_nav_init_mesh_point
-
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_nav_init_mesh_point ( in_mp, pid, vid, x, y )
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  pid  !< polygon ID
+    INTEGER(iwp) ::  vid  !< vertex ID
+
+    REAL(wp) ::  x  !< x-coordinate
+    REAL(wp) ::  y  !< y-coordinate
+
+    TYPE(mesh_point) ::  in_mp  !< mesh point to be initialized
+
+    in_mp%origin_id          = -1
+    in_mp%polygon_id         = pid
+    in_mp%vertex_id          = vid
+    in_mp%cost_so_far        = 1.d12
+    in_mp%x                  = x
+    in_mp%y                  = y
+    in_mp%x_s                = x
+    in_mp%y_s                = y
+    ALLOCATE( in_mp%connected_vertices(1:100),                                                     &
+              in_mp%distance_to_vertex(1:100) )
+    in_mp%connected_vertices = -999
+    in_mp%distance_to_vertex = -999.
+    in_mp%noc                = 0
+
+ END SUBROUTINE mas_nav_init_mesh_point
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Reading of namlist from parin file
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_parin
-
-       USE control_parameters,                                                 &
-           ONLY: agent_time_unlimited, multi_agent_system_end,                 &
-                 multi_agent_system_start
-
-       IMPLICIT NONE
-
-       CHARACTER (LEN=80) ::  line  !<
-
-       NAMELIST /agent_parameters/  a_rand_target,                             &
-                                    adx,                                       &
-                                    ady,                                       &
-                                    agent_maximum_age,                         &
-                                    agent_time_unlimited,                      &
-                                    alloc_factor_mas,                          &
-                                    asl,                                       &
-                                    asn,                                       &
-                                    asr,                                       &
-                                    ass,                                       &
-                                    at_x,                                      &
-                                    at_y,                                      &
-                                    bc_mas_lr,                                 &
-                                    bc_mas_ns,                                 &
-                                    coll_t_0,                                  &
-                                    corner_gate_start,                         &
-                                    corner_gate_width,                         &
-                                    dim_size_agtnum_manual,                    &
-                                    dim_size_factor_agtnum,                    &
-                                    deallocate_memory_mas,                     &
-                                    dist_to_int_target,                        &
-                                    dt_agent,                                  &
-                                    dt_arel,                                   &
-                                    dt_write_agent_data,                       &
-                                    end_time_arel,                             &
-                                    max_dist_from_path,                        &
-                                    min_nr_agent,                              &
-                                    multi_agent_system_end,                    &
-                                    multi_agent_system_start,                  &
-                                    number_of_agent_groups,                    &
-                                    radius_agent,                              &
-                                    random_start_position_agents,              &
-                                    read_agents_from_restartfile,              &
-                                    repuls_agent,                              &
-                                    repuls_wall,                               &
-                                    scan_radius_agent,                         &
-                                    sigma_rep_agent,                           &
-                                    sigma_rep_wall,                            &
-                                    step_dealloc_mas,                          &
-                                    tau_accel_agent
-
-!
-!--    Try to find agent package
-       REWIND ( 11 )
-       line = ' '
-       DO WHILE ( INDEX( line, '&agent_parameters' ) == 0 )
-          READ ( 11, '(A)', END=20 )  line
-       ENDDO
-       BACKSPACE ( 11 )
-
-!
-!--    Read user-defined namelist
-       READ ( 11, agent_parameters, ERR = 10, END = 20 )
-
-!
-!--    Set flag that indicates that agents are switched on
-       agents_active = .TRUE.
-       GOTO 20
-
- 10    BACKSPACE( 11 )
-       READ( 11 , '(A)') line
-       CALL parin_fail_message( 'agent_parameters', line )
-
- 20    CONTINUE
-
-    END SUBROUTINE mas_parin
-
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_parin
+
+    USE control_parameters,                                                                        &
+        ONLY: agent_time_unlimited, multi_agent_system_end, multi_agent_system_start
+
+    IMPLICIT NONE
+
+    CHARACTER (LEN=80) ::  line  !<
+
+    NAMELIST /agent_parameters/  a_rand_target,                                                    &
+                                 adx,                                                              &
+                                 ady,                                                              &
+                                 agent_maximum_age,                                                &
+                                 agent_time_unlimited,                                             &
+                                 alloc_factor_mas,                                                 &
+                                 asl,                                                              &
+                                 asn,                                                              &
+                                 asr,                                                              &
+                                 ass,                                                              &
+                                 at_x,                                                             &
+                                 at_y,                                                             &
+                                 bc_mas_lr,                                                        &
+                                 bc_mas_ns,                                                        &
+                                 coll_t_0,                                                         &
+                                 corner_gate_start,                                                &
+                                 corner_gate_width,                                                &
+                                 deallocate_memory_mas,                                            &
+                                 dim_size_agtnum_manual,                                           &
+                                 dim_size_factor_agtnum,                                           &
+                                 dist_to_int_target,                                               &
+                                 dt_agent,                                                         &
+                                 dt_arel,                                                          &
+                                 dt_write_agent_data,                                              &
+                                 end_time_arel,                                                    &
+                                 max_dist_from_path,                                               &
+                                 min_nr_agent,                                                     &
+                                 multi_agent_system_end,                                           &
+                                 multi_agent_system_start,                                         &
+                                 number_of_agent_groups,                                           &
+                                 radius_agent,                                                     &
+                                 random_start_position_agents,                                     &
+                                 read_agents_from_restartfile,                                     &
+                                 repuls_agent,                                                     &
+                                 repuls_wall,                                                      &
+                                 scan_radius_agent,                                                &
+                                 sigma_rep_agent,                                                  &
+                                 sigma_rep_wall,                                                   &
+                                 step_dealloc_mas,                                                 &
+                                 tau_accel_agent
+
+!
+!-- Try to find agent package
+    REWIND ( 11 )
+    line = ' '
+    DO WHILE ( INDEX( line, '&agent_parameters' ) == 0 )
+       READ ( 11, '(A)', END=20 )  line
+    ENDDO
+    BACKSPACE ( 11 )
+
+!
+!-- Read user-defined namelist
+    READ ( 11, agent_parameters, ERR = 10, END = 20 )
+
+!
+!-- Set flag that indicates that agents are switched on
+    agents_active = .TRUE.
+    GOTO 20
+
+10    BACKSPACE( 11 )
+    READ( 11 , '(A)') line
+    CALL parin_fail_message( 'agent_parameters', line )
+
+20    CONTINUE
+
+ END SUBROUTINE mas_parin
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Routine for the whole processor
 !> Sort all agents into the 4 respective subgrid boxes
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_ps_sort_in_subboxes
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  i           !< grid box (x)
-       INTEGER(iwp) ::  ip          !< counter (x)
-       INTEGER(iwp) ::  is          !< box counter
-       INTEGER(iwp) ::  j           !< grid box (y)
-       INTEGER(iwp) ::  jp          !< counter (y)
-       INTEGER(iwp) ::  m           !< sorting index
-       INTEGER(iwp) ::  n           !< agent index
-       INTEGER(iwp) ::  nn          !< agent counter
-       INTEGER(iwp) ::  sort_index  !< sorting index
-
-       INTEGER(iwp), DIMENSION(0:3) ::  sort_count  !< number of agents in one subbox
-
-       TYPE(agent_type), DIMENSION(:,:), ALLOCATABLE ::  sort_agents  !< sorted agent array
-
-       DO  ip = nxl, nxr
-          DO  jp = nys, nyn
-             number_of_agents = agt_count(jp,ip)
-             IF ( number_of_agents <= 0 )  CYCLE
-             agents => grid_agents(jp,ip)%agents(1:number_of_agents)
-
-             nn = 0
-             sort_count = 0
-             ALLOCATE( sort_agents(number_of_agents, 0:3) )
-
-             DO  n = 1, number_of_agents
-                sort_index = 0
-
-                IF ( agents(n)%agent_mask )  THEN
-                   nn = nn + 1
-!
-!--                Sorting agents with a binary scheme
-!--                sort_index=11_2=3_10 -> agent at the left,south subgridbox
-!--                sort_index=10_2=2_10 -> agent at the left,north subgridbox
-!--                sort_index=01_2=1_10 -> agent at the right,south subgridbox
-!--                sort_index=00_2=0_10 -> agent at the right,north subgridbox
-!--                For this the center of the gridbox is calculated 
-                   i = (agents(n)%x + 0.5_wp * dx) * ddx
-                   j = (agents(n)%y + 0.5_wp * dy) * ddy
-
-                   IF ( i == ip )  sort_index = sort_index + 2
-                   IF ( j == jp )  sort_index = sort_index + 1
-
-                   sort_count(sort_index) = sort_count(sort_index) + 1
-                   m = sort_count(sort_index)
-                   sort_agents(m,sort_index) = agents(n)
-                   sort_agents(m,sort_index)%block_nr = sort_index
-                ENDIF
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_ps_sort_in_subboxes
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  i           !< grid box (x)
+    INTEGER(iwp) ::  ip          !< counter (x)
+    INTEGER(iwp) ::  is          !< box counter
+    INTEGER(iwp) ::  j           !< grid box (y)
+    INTEGER(iwp) ::  jp          !< counter (y)
+    INTEGER(iwp) ::  m           !< sorting index
+    INTEGER(iwp) ::  n           !< agent index
+    INTEGER(iwp) ::  nn          !< agent counter
+    INTEGER(iwp) ::  sort_index  !< sorting index
+
+    INTEGER(iwp), DIMENSION(0:3) ::  sort_count  !< number of agents in one subbox
+
+    TYPE(agent_type), DIMENSION(:,:), ALLOCATABLE ::  sort_agents  !< sorted agent array
+
+    DO  ip = nxl, nxr
+       DO  jp = nys, nyn
+          number_of_agents = agt_count(jp,ip)
+          IF ( number_of_agents <= 0 )  CYCLE
+          agents => grid_agents(jp,ip)%agents(1:number_of_agents)
+
+          nn = 0
+          sort_count = 0
+          ALLOCATE( sort_agents(number_of_agents, 0:3) )
+
+          DO  n = 1, number_of_agents
+             sort_index = 0
+
+             IF ( agents(n)%agent_mask )  THEN
+                nn = nn + 1
+!
+!--             Sorting agents with a binary scheme
+!--             sort_index=11_2=3_10 -> agent at the left,south subgridbox
+!--             sort_index=10_2=2_10 -> agent at the left,north subgridbox
+!--             sort_index=01_2=1_10 -> agent at the right,south subgridbox
+!--             sort_index=00_2=0_10 -> agent at the right,north subgridbox
+!--             For this the center of the gridbox is calculated
+                i = (agents(n)%x + 0.5_wp * dx) * ddx
+                j = (agents(n)%y + 0.5_wp * dy) * ddy
+
+                IF ( i == ip )  sort_index = sort_index + 2
+                IF ( j == jp )  sort_index = sort_index + 1
+
+                sort_count(sort_index) = sort_count(sort_index) + 1
+                m = sort_count(sort_index)
+                sort_agents(m,sort_index) = agents(n)
+                sort_agents(m,sort_index)%block_nr = sort_index
+             ENDIF
+          ENDDO
+
+          nn = 0
+          DO  is = 0,3
+             grid_agents(jp,ip)%start_index(is) = nn + 1
+             DO  n = 1,sort_count(is)
+                nn = nn + 1
+                agents(nn) = sort_agents(n,is)
              ENDDO
-
-             nn = 0
-             DO is = 0,3
-                grid_agents(jp,ip)%start_index(is) = nn + 1
-                DO n = 1,sort_count(is)
-                   nn = nn + 1
-                   agents(nn) = sort_agents(n,is)
-                ENDDO
-                grid_agents(jp,ip)%end_index(is) = nn
-             ENDDO
-
-             number_of_agents = nn
-             agt_count(jp,ip) = number_of_agents
-             DEALLOCATE(sort_agents)
+             grid_agents(jp,ip)%end_index(is) = nn
           ENDDO
+
+          number_of_agents = nn
+          agt_count(jp,ip) = number_of_agents
+          DEALLOCATE( sort_agents )
        ENDDO
-
-    END SUBROUTINE mas_ps_sort_in_subboxes
+    ENDDO
+
+ END SUBROUTINE mas_ps_sort_in_subboxes
 
 #if defined( __parallel )
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Move all agents not marked for deletion to lowest indices (packing)
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_ps_pack
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  n  !< agent counter
-       INTEGER(iwp) ::  nn !< number of agents
-!
-!--    Find out elements marked for deletion and move data from highest index
-!--    values to these free indices
-       nn = number_of_agents
-
-       DO WHILE ( .NOT. agents(nn)%agent_mask )
-          nn = nn-1
-          IF ( nn == 0 )  EXIT
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_ps_pack
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  n   !< agent counter
+    INTEGER(iwp) ::  nn  !< number of agents
+!
+!-- Find out elements marked for deletion and move data from highest index values to these free
+!-- indices.
+    nn = number_of_agents
+
+    DO WHILE ( .NOT. agents(nn)%agent_mask )
+       nn = nn-1
+       IF ( nn == 0 )  EXIT
+    ENDDO
+
+    IF ( nn > 0 )  THEN
+       DO  n = 1, number_of_agents
+          IF ( .NOT. agents(n)%agent_mask )  THEN
+             agents(n) = agents(nn)
+             nn = nn - 1
+             DO WHILE ( .NOT. agents(nn)%agent_mask )
+                nn = nn-1
+                IF ( n == nn )  EXIT
+             ENDDO
+          ENDIF
+          IF ( n == nn )  EXIT
        ENDDO
-
-       IF ( nn > 0 )  THEN
-          DO  n = 1, number_of_agents
-             IF ( .NOT. agents(n)%agent_mask )  THEN
-                agents(n) = agents(nn)
-                nn = nn - 1
-                DO WHILE ( .NOT. agents(nn)%agent_mask )
-                   nn = nn-1
-                   IF ( n == nn )  EXIT
-                ENDDO
-             ENDIF
-             IF ( n == nn )  EXIT
-          ENDDO
-       ENDIF
-
-!
-!--    The number of deleted agents has been determined in routines
-!--    mas_boundary_conds, mas_droplet_collision, and mas_eh_exchange_horiz
-       number_of_agents = nn
-
-    END SUBROUTINE mas_ps_pack
+    ENDIF
+
+!
+!-- The number of deleted agents has been determined in routines mas_boundary_conds,
+!-- mas_droplet_collision, and mas_eh_exchange_horiz.
+    number_of_agents = nn
+
+ END SUBROUTINE mas_ps_pack
 #endif
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> Sort agents in each sub-grid box into two groups: agents that already
-!> completed the LES timestep, and agents that need further timestepping to
-!> complete the LES timestep.
-!------------------------------------------------------------------------------!
+!> Sort agents in each sub-grid box into two groups: agents that already completed the LES
+!> timestep, and agents that need further timestepping to complete the LES timestep.
+!--------------------------------------------------------------------------------------------------!
 !    SUBROUTINE mas_ps_sort_timeloop_done
 !
 !       IMPLICIT NONE
 !
-!       INTEGER(iwp) :: end_index     !< agent end index for each sub-box
-!       INTEGER(iwp) :: i             !< index of agent grid box in x-direction
-!       INTEGER(iwp) :: j             !< index of agent grid box in y-direction
-!       INTEGER(iwp) :: n             !< running index for number of agents
-!       INTEGER(iwp) :: nb            !< index of subgrid boux
-!       INTEGER(iwp) :: nf            !< indices for agents in each sub-box that already finalized their substeps
-!       INTEGER(iwp) :: nnf           !< indices for agents in each sub-box that need further treatment
-!       INTEGER(iwp) :: num_finalized !< number of agents in each sub-box that already finalized their substeps
-!       INTEGER(iwp) :: start_index   !< agent start index for each sub-box
+!       INTEGER(iwp) :: end_index      !< agent end index for each sub-box
+!       INTEGER(iwp) :: i              !< index of agent grid box in x-direction
+!       INTEGER(iwp) :: j              !< index of agent grid box in y-direction
+!       INTEGER(iwp) :: n              !< running index for number of agents
+!       INTEGER(iwp) :: nb             !< index of subgrid boux
+!       INTEGER(iwp) :: nf             !< indices for agents in each sub-box that already finalized their substeps
+!       INTEGER(iwp) :: nnf            !< indices for agents in each sub-box that need further treatment
+!       INTEGER(iwp) :: num_finalized  !< number of agents in each sub-box that already finalized their substeps
+!       INTEGER(iwp) :: start_index    !< agent start index for each sub-box
 !
 !       TYPE(agent_type), DIMENSION(:), ALLOCATABLE :: sort_agents  !< temporary agent array
@@ -4028,5 +3884,5 @@
 !                ALLOCATE( sort_agents(start_index:end_index) )
 !
-!--             Determine number of agents already completed the LES 
+!--             Determine number of agents already completed the LES
 !--             timestep, and write them into a temporary array
 !                nf = start_index
@@ -4040,5 +3896,5 @@
 !                ENDDO
 !
-!--             Determine number of agents that not completed the LES 
+!--             Determine number of agents that not completed the LES
 !--             timestep, and write them into a temporary array
 !                nnf = nf
@@ -4054,6 +3910,6 @@
 !                                        sort_agents(start_index:end_index)
 !
-!--             Determine updated start_index, used to masked already 
-!--             completed agents. 
+!--             Determine updated start_index, used to masked already
+!--             completed agents.
 !                grid_agents(j,i)%start_index(nb) =                     &
 !                                   grid_agents(j,i)%start_index(nb)    &
@@ -4063,6 +3919,6 @@
 !                DEALLOCATE ( sort_agents )
 !
-!--             Finally, if number of non-completed agents is non zero 
-!--             in any of the sub-boxes, set control flag appropriately. 
+!--             Finally, if number of non-completed agents is non zero
+!--             in any of the sub-boxes, set control flag appropriately.
 !                IF ( nnf > nf )                                             &
 !                   grid_agents(j,i)%time_loop_done = .FALSE.
@@ -4074,452 +3930,246 @@
 !    END SUBROUTINE mas_ps_sort_timeloop_done
 
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Calls social forces calculations
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_timestep_forces_call ( ip, jp )
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  ip  !< counter, x-direction
-       INTEGER(iwp) ::  jp  !< counter, y-direction
-       INTEGER(iwp) ::  n   !< loop variable over all agents in a grid box
-
-!
-!--    Get direction for all agents in current grid cell
-       CALL mas_agent_direction
-
-       DO n = 1, number_of_agents
-
-          force_x = 0.0_wp
-          force_y = 0.0_wp
-
-          CALL mas_timestep_social_forces ( 'acceleration', n, ip, jp )
-
-          CALL mas_timestep_social_forces ( 'other_agents', n, ip, jp )
-
-          CALL mas_timestep_social_forces ( 'walls',        n, ip, jp )
-!
-!--       Update forces
-          agents(n)%force_x = force_x
-          agents(n)%force_y = force_y
-       ENDDO
-
-    END SUBROUTINE mas_timestep_forces_call
-
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_timestep_forces_call ( ip, jp )
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  ip  !< counter, x-direction
+    INTEGER(iwp) ::  jp  !< counter, y-direction
+    INTEGER(iwp) ::  n   !< loop variable over all agents in a grid box
+
+!
+!-- Get direction for all agents in current grid cell
+    CALL mas_agent_direction
+
+    DO  n = 1, number_of_agents
+
+       force_x = 0.0_wp
+       force_y = 0.0_wp
+
+       CALL mas_timestep_social_forces ( 'acceleration', n, ip, jp )
+
+       CALL mas_timestep_social_forces ( 'other_agents', n, ip, jp )
+
+       CALL mas_timestep_social_forces ( 'walls',        n, ip, jp )
+!
+!--    Update forces
+       agents(n)%force_x = force_x
+       agents(n)%force_y = force_y
+    ENDDO
+
+ END SUBROUTINE mas_timestep_forces_call
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
 !> Euler timestep of agent transport
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_timestep
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  n !< loop variable over all agents in a grid box
-
-       REAL(wp) ::  abs_v !< absolute value of velocity
-       REAL(wp) ::  abs_f !< absolute value of force
-
-       DO n = 1, number_of_agents
-!
-!--       Limit absolute force to a maximum to prevent unrealistic acceleration
-          abs_f = SQRT((agents(n)%force_x)**2 + (agents(n)%force_y)**2)
-          IF ( abs_f > 20. ) THEN
-             agents(n)%force_x = agents(n)%force_x * 20. / abs_f
-             agents(n)%force_y = agents(n)%force_y * 20. / abs_f
-          ENDIF
-!
-!--       Update agent speed
-          agents(n)%speed_x = agents(n)%speed_x + agents(n)%force_x * dt_agent
-          agents(n)%speed_y = agents(n)%speed_y + agents(n)%force_y * dt_agent
-!
-!--       Reduction of agent speed to maximum agent speed
-          abs_v = SQRT((agents(n)%speed_x)**2 + (agents(n)%speed_y)**2)
-          IF ( abs_v > v_max_agent ) THEN
-             agents(n)%speed_x = agents(n)%speed_x * v_max_agent / abs_v
-             agents(n)%speed_y = agents(n)%speed_y * v_max_agent / abs_v
-          ENDIF
-!
-!--       Update agent position
-          agents(n)%x = agents(n)%x + agents(n)%speed_x * dt_agent
-          agents(n)%y = agents(n)%y + agents(n)%speed_y * dt_agent
-!
-!--       Update absolute value of agent speed
-          agents(n)%speed_abs = abs_v
-!
-!--       Increment the agent age and the total time that the agent
-!--       has advanced within the agent timestep procedure
-          agents(n)%age_m  = agents(n)%age
-          agents(n)%age    = agents(n)%age    + dt_agent
-          agents(n)%dt_sum = agents(n)%dt_sum + dt_agent
-!
-!--       Check whether there is still an agent that has not yet completed 
-!--       the total LES timestep
-          IF ( ( dt_3d - agents(n)%dt_sum ) > 1E-8_wp )  THEN
-             dt_3d_reached_l_mas = .FALSE.
-          ENDIF
-
-       ENDDO
-
-    END SUBROUTINE mas_timestep
-
-!------------------------------------------------------------------------------!
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_timestep
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  n  !< loop variable over all agents in a grid box
+
+    REAL(wp) ::  abs_v  !< absolute value of velocity
+    REAL(wp) ::  abs_f  !< absolute value of force
+
+    DO  n = 1, number_of_agents
+!
+!--    Limit absolute force to a maximum to prevent unrealistic acceleration
+       abs_f = SQRT( ( agents(n)%force_x )**2 + ( agents(n)%force_y )**2 )
+       IF ( abs_f > 20. )  THEN
+          agents(n)%force_x = agents(n)%force_x * 20. / abs_f
+          agents(n)%force_y = agents(n)%force_y * 20. / abs_f
+       ENDIF
+!
+!--    Update agent speed
+       agents(n)%speed_x = agents(n)%speed_x + agents(n)%force_x * dt_agent
+       agents(n)%speed_y = agents(n)%speed_y + agents(n)%force_y * dt_agent
+!
+!--    Reduction of agent speed to maximum agent speed
+       abs_v = SQRT( ( agents(n)%speed_x )**2 + ( agents(n)%speed_y )**2 )
+       IF ( abs_v > v_max_agent )  THEN
+          agents(n)%speed_x = agents(n)%speed_x * v_max_agent / abs_v
+          agents(n)%speed_y = agents(n)%speed_y * v_max_agent / abs_v
+       ENDIF
+!
+!--    Update agent position
+       agents(n)%x = agents(n)%x + agents(n)%speed_x * dt_agent
+       agents(n)%y = agents(n)%y + agents(n)%speed_y * dt_agent
+!
+!--    Update absolute value of agent speed
+       agents(n)%speed_abs = abs_v
+!
+!--    Increment the agent age and the total time that the agent has advanced within the agent
+!--    timestep procedure
+       agents(n)%age_m  = agents(n)%age
+       agents(n)%age    = agents(n)%age    + dt_agent
+       agents(n)%dt_sum = agents(n)%dt_sum + dt_agent
+!
+!--    Check whether there is still an agent that has not yet completed the total LES timestep
+       IF ( ( dt_3d - agents(n)%dt_sum ) > 1E-8_wp )  THEN
+          dt_3d_reached_l_mas = .FALSE.
+       ENDIF
+
+    ENDDO
+
+ END SUBROUTINE mas_timestep
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> Calculates the Social Forces (Helbing and Molnar, 1995) that the agent
-!> experiences due to acceleration towards target and repulsion by obstacles
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_timestep_social_forces ( mode, nl, ip, jp )
-
-       IMPLICIT NONE
-
-       CHARACTER (LEN=*) ::  mode  !< identifier for the mode of calculation
-
-       INTEGER(iwp) ::  ij_dum      !< index of nearest wall
-       INTEGER(iwp) ::  il          !< index variable along x 
-       INTEGER(iwp) ::  ip          !< index variable along x 
-       INTEGER(iwp) ::  jl          !< index variable along y
-       INTEGER(iwp) ::  jp          !< index variable along y
-       INTEGER(iwp) ::  nl          !< loop variable over all agents in a grid box
-       INTEGER(iwp) ::  no          !< loop variable over all agents in a grid box
-       INTEGER(iwp) ::  noa         !< amount of agents in a grid box 
-       INTEGER(iwp) ::  sc_x_end    !< index for scan for topography/other agents
-       INTEGER(iwp) ::  sc_x_start  !< index for scan for topography/other agents
-       INTEGER(iwp) ::  sc_y_end    !< index for scan for topography/other agents
-       INTEGER(iwp) ::  sc_y_start  !< index for scan for topography/other agents
-
-       LOGICAL ::  corner_found  !< flag that indicates a corner has been found near agent
-
-       REAL(wp) ::  a_pl             !< factor for collision avoidance
-       REAL(wp) ::  ax_semimaj       !< semiminor axis of repulsive ellipse
-       REAL(wp) ::  b_pl             !< factor for collision avoidance
-       REAL(wp) ::  c_pl             !< factor for collision avoidance
-       REAL(wp) ::  coll_t           !< time at which the next collision would happen
-       REAL(wp) ::  d_coll_t_0       !< inverse of collision cutoff time
-       REAL(wp) ::  d_pl             !< factor for collision avoidance
-       REAL(wp) ::  ddum_f           !< dummy devisor collision avoidance
-       REAL(wp) ::  dist             !< distance to obstacle
-       REAL(wp) ::  dist_sq          !< distance to obstacle squared
-       REAL(wp) ::  pos_rel_x        !< relative position of two agents (x)
-       REAL(wp) ::  pos_rel_y        !< relative position of two agents (y)
-       REAL(wp) ::  r_sq             !< y-position
-       REAL(wp) ::  sra              !< scan radius (agents)
-       REAL(wp) ::  srw              !< local variable for scan radius (walls)
-       REAL(wp) ::  v_rel_x          !< relative velocity (x); collision avoidance
-       REAL(wp) ::  v_rel_y          !< relative velocity (y); collision avoidance
-       REAL(wp) ::  x_a              !< x-position
-       REAL(wp) ::  x_wall           !< x-position of wall
-       REAL(wp) ::  y_a              !< y-position
-       REAL(wp) ::  y_wall           !< y-position of wall
-
-       REAL(wp), PARAMETER ::  k_pl = 1.5  !< factor for collision avoidance
-
-       TYPE(agent_type), DIMENSION(:), POINTER ::  l_agts !< agents that repulse current agent
-
-!
-!--    Initialization
-       x_a = agents(nl)%x
-       y_a = agents(nl)%y
-
-       SELECT CASE ( TRIM( mode ) )
-!
-!--       Calculation of force due to agent trying to approach desired velocity
-          CASE ( 'acceleration' )
-
-             force_x = force_x + d_tau_accel_agent                             &
-                          * ( agents(nl)%speed_des*agents(nl)%speed_e_x        &
-                             -agents(nl)%speed_x )
-
-             force_y = force_y + d_tau_accel_agent                             &
-                          * ( agents(nl)%speed_des*agents(nl)%speed_e_y        &
-                             -agents(nl)%speed_y )
-
-!
-!--       Calculation of repulsive forces by other agents in a radius around the
-!--       current one
-          CASE ( 'other_agents' )
-
-             sra = scan_radius_agent
-             d_coll_t_0 = 1./coll_t_0
-!
-!--          Find relevant gridboxes (those that could contain agents within
-!--          scan radius)
-             sc_x_start = FLOOR( (x_a - sra) * ddx )
-             sc_x_end   = FLOOR( (x_a + sra) * ddx )
-             sc_y_start = FLOOR( (y_a - sra) * ddx )
-             sc_y_end   = FLOOR( (y_a + sra) * ddx )
-             IF ( sc_x_start < nxlg ) sc_x_start = nxlg
-             IF ( sc_x_end   > nxrg ) sc_x_end   = nxrg
-             IF ( sc_y_start < nysg ) sc_y_start = nysg
-             IF ( sc_y_end   > nyng ) sc_y_end   = nyng
-
-             sra = sra**2
-!
-!--          Loop over all previously found relevant gridboxes
-             DO il = sc_x_start, sc_x_end
-                DO jl = sc_y_start, sc_y_end
-                   noa = agt_count(jl,il)
-                   IF ( noa <= 0 )  CYCLE
-                   l_agts => grid_agents(jl,il)%agents(1:noa)
-                   DO no = 1, noa
-!
-!--                   Skip self
-                      IF ( jl == jp .AND. il == ip .AND. no == nl ) CYCLE
-                      pos_rel_x = l_agts(no)%x - x_a
-                      pos_rel_y = l_agts(no)%y - y_a
-                      dist_sq = pos_rel_x**2 + pos_rel_y**2
-                      IF ( dist_sq > sra ) CYCLE
-                      r_sq    = (2*radius_agent)**2
-                      v_rel_x   = agents(nl)%speed_x - l_agts(no)%speed_x
-                      v_rel_y   = agents(nl)%speed_y - l_agts(no)%speed_y
-!
-!--                   Collision is already occuring, default to standard
-!--                   social forces
-                      IF ( dist_sq <= r_sq ) THEN
-                         dist = SQRT(dist_sq) + 1.0d-12
-                         ax_semimaj = .5_wp*SQRT( dist )
-
-                         force_x = force_x - 0.125_wp * repuls_agent           &
-                                        * d_sigma_rep_agent / ax_semimaj       &
-                                        * EXP( -ax_semimaj*d_sigma_rep_agent ) &
-                                        * (pos_rel_x/dist)
-
-                         force_y = force_y - 0.125_wp * repuls_agent           &
-                                        * d_sigma_rep_agent / ax_semimaj       &
-                                        * EXP( -ax_semimaj*d_sigma_rep_agent ) &
-                                        * (pos_rel_y/dist)
-!
-!--                   Currently no collision, calculate collision avoidance
-!--                   force according to Karamouzas et al (2014, PRL 113,238701)
-                      ELSE
-!
-!--                     factors
-                         a_pl = v_rel_x**2 +  v_rel_y**2
-                         b_pl = pos_rel_x*v_rel_x + pos_rel_y*v_rel_y
-                         c_pl = dist_sq - r_sq
-                         d_pl = b_pl**2 - a_pl*c_pl
-!
-!--                      If the two agents are moving non-parallel, calculate
-!--                      collision avoidance social force
-                         IF ( d_pl > 0.0_wp .AND.                              &
-                            ( a_pl < -0.00001 .OR. a_pl > 0.00001 ) )          &
-                         THEN
-
-                            d_pl   = SQRT(d_pl)
-                            coll_t = (b_pl - d_pl)/a_pl
-                            IF ( coll_t > 0.0_wp ) THEN
-!
-!--                            Dummy factor
-                               ddum_f = 1. / ( a_pl * coll_t**2 )              &
-                                           * ( 2. / coll_t + 1.0 * d_coll_t_0 )
-!
-!--                            x-component of social force
-                               force_x = force_x - k_pl *                      &
-                                         EXP( -coll_t * d_coll_t_0 ) *         &
-                                         ( v_rel_x -                           &
-                                           ( b_pl * v_rel_x -                  &
-                                             a_pl * pos_rel_x ) / d_pl ) *     &
-                                         ddum_f
-!
-!--                            y-component of social force
-                               force_y = force_y - k_pl *                      &
-                                         EXP( -coll_t * d_coll_t_0 ) *         &
-                                         ( v_rel_y -                           &
-                                           ( b_pl * v_rel_y -                  &
-                                             a_pl * pos_rel_y ) / d_pl ) *     &
-                                         ddum_f
-
-                            ENDIF
-                         ENDIF
-                      ENDIF
-                   ENDDO
-                ENDDO
-             ENDDO
-
-          CASE ( 'walls' )
-
-             srw = scan_radius_wall
-             corner_found = .FALSE.
-!
-!--          find relevant grid boxes (those that could contain topography
-!--          within radius)
-             sc_x_start = (x_a - srw) * ddx
-             sc_x_end   = (x_a + srw) * ddx
-             sc_y_start = (y_a - srw) * ddx
-             sc_y_end   = (y_a + srw) * ddx
-             IF ( sc_x_start < nxlg ) sc_x_start = nxlg
-             IF ( sc_x_end   > nxrg ) sc_x_end   = nxrg
-             IF ( sc_y_start < nysg ) sc_y_start = nysg
-             IF ( sc_y_end   > nyng ) sc_y_end   = nyng
-!
-!--          Find "walls" ( i.e. topography steps (up or down) higher than one
-!--          grid box ) that are perpendicular to the agent within the defined
-!--          search radius. Such obstacles cannot be passed and a social force
-!--          to that effect is applied.
-!--          Walls only apply a force perpendicular to the wall to the agent.
-!--          There is therefore a search for walls directly right, left, south 
-!--          and north of the agent. All other walls are ignored.
-!--
-!--          Check for wall left of current agent
-             ij_dum = 0
-             IF ( sc_x_start < ip ) THEN
-                DO il = ip - 1, sc_x_start, -1
-!
-!--                Going left from the agent, check for a right wall
-                   IF ( BTEST( obstacle_flags(jp,il), 2 ) ) THEN
-!
-!--                   obstacle found in grid box il, wall at right side
-                      x_wall = (il+1)*dx
-!
-!--                   Calculate force of found wall on agent
-                      CALL mas_timestep_wall_corner_force( x_a, x_wall, y_a,   &
-                                                           y_a )
-!
-!--                   calculate new x starting index for later scan for corners
-                      ij_dum = il + 1
-                      EXIT
-                   ENDIF
-                ENDDO
-             ENDIF
-             IF ( ij_dum /= 0 ) sc_x_start = ij_dum 
-
-!
-!--          Check for wall right of current agent
-             ij_dum = 0
-             IF ( sc_x_end > ip ) THEN
-                DO il = ip + 1, sc_x_end
-!
-!--                Going right from the agent, check for a left wall
-                   IF ( BTEST( obstacle_flags(jp,il), 6 ) ) THEN
-!
-!--                   obstacle found in grid box il, wall at left side
-                      x_wall = il*dx
-!
-!--                   Calculate force of found wall on agent
-                      CALL mas_timestep_wall_corner_force( x_a, x_wall, y_a,   &
-                                                           y_a )
-!
-!--                   calculate new x end index for later scan for corners
-                      ij_dum = il - 1
-                      EXIT
-                   ENDIF
-                ENDDO
-             ENDIF
-             IF ( ij_dum /= 0 ) sc_x_end = ij_dum 
-
-!
-!--          Check for wall south of current agent
-             ij_dum = 0
-             IF ( sc_y_start < jp ) THEN
-                DO jl = jp - 1, sc_y_start, -1
-!
-!--                Going south from the agent, check for a north wall
-                   IF ( BTEST( obstacle_flags(jl,ip), 0 ) ) THEN
-!
-!--                   obstacle found in grid box jl, wall at left side
-                      y_wall = (jl+1)*dy
-
-                      CALL mas_timestep_wall_corner_force( x_a, x_a, y_a,      &
-                                                           y_wall )
-!
-!--                   calculate new y starting index for later scan for corners
-                      ij_dum = jl + 1
-                      EXIT
-                   ENDIF
-                ENDDO
-             ENDIF
-             IF ( ij_dum /= 0 ) sc_y_start = ij_dum 
-
-!
-!--          Check for wall north of current agent
-             ij_dum = 0
-             IF ( sc_y_end > jp ) THEN
-                DO jl = jp + 1, sc_y_end 
-!
-!--                Going north from the agent, check for a south wall
-                   IF ( BTEST( obstacle_flags(jl,ip), 4 ) ) THEN
-!
-!--                   obstacle found in grid box jl, wall at left side
-                      y_wall = jl*dy
-
-                      CALL mas_timestep_wall_corner_force( x_a, x_a, y_a,      &
-                                                           y_wall )
-!
-!--                   calculate new y end index for later scan for corners
-                      ij_dum = jl - 1
-                   ENDIF
-                ENDDO
-             ENDIF
-             IF ( ij_dum /= 0 ) sc_y_end = ij_dum 
-
-!
-!--          Scan for corners surrounding current agent.
-!--          Only gridcells that are closer than the closest wall in each
-!--          direction (n,s,r,l) are considered in the search since those
-!--          further away would have a significantly smaller resulting force
-!--          than the closer wall. 
-             DO il = sc_x_start, sc_x_end
-                DO jl = sc_y_start, sc_y_end
-                   IF ( il == ip .OR. jl == jp ) CYCLE
-!
-!--                corners left of agent
-                   IF ( il < ip ) THEN
-!
-!--                   south left quadrant: look for north right corner
-                      IF ( jl < jp ) THEN
-                         IF ( BTEST( obstacle_flags(jl,il), 1 ) ) THEN
-!
-!--                         calculate coordinates of the found corner
-                            x_wall = (il+1)*dx
-                            y_wall = (jl+1)*dy
-
-                            CALL mas_timestep_wall_corner_force( x_a, x_wall,  &
-                                                                 y_a, y_wall )
-
-                         ENDIF
-!
-!--                   north left quadrant: look for south right corner
-                      ELSEIF ( jl > jp ) THEN
-                         IF ( BTEST( obstacle_flags(jl,il), 3 ) ) THEN
-!
-!--                         calculate coordinates of the corner of said gridcell
-!--                         that is closest to the current agent
-                            x_wall = (il+1)*dx
-                            y_wall = jl*dy
-
-                            CALL mas_timestep_wall_corner_force( x_a, x_wall,  &
-                                                                 y_a, y_wall )
-
-                         ENDIF
-                      ENDIF
-                   ELSEIF ( il > ip ) THEN
-!
-!--                   south right quadrant: look for north left corner
-                      IF ( jl < jp ) THEN
-                         IF ( BTEST( obstacle_flags(jl,il), 7 ) ) THEN
-!
-!--                         calculate coordinates of the corner of said gridcell
-!--                         that is closest to the current agent
-                            x_wall = il*dx
-                            y_wall = (jl+1)*dy
-
-                            CALL mas_timestep_wall_corner_force( x_a, x_wall,  &
-                                                                 y_a, y_wall )
-
-                         ENDIF
-!
-!--                   north right quadrant: look for south left corner
-                      ELSEIF ( jl > jp ) THEN
-                         IF ( BTEST( obstacle_flags(jl,il), 5 ) ) THEN
-!
-!--                         calculate coordinates of the corner of said gridcell
-!--                         that is closest to the current agent
-                            x_wall = il*dx
-                            y_wall = jl*dy
-
-                            CALL mas_timestep_wall_corner_force( x_a, x_wall,  &
-                                                                 y_a, y_wall )
+!> Calculates the Social Forces (Helbing and Molnar, 1995) that the agent experiences due to
+!> acceleration towards target and repulsion by obstacles
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_timestep_social_forces ( mode, nl, ip, jp )
+
+    IMPLICIT NONE
+
+    REAL(wp), PARAMETER ::  k_pl = 1.5  !< factor for collision avoidance
+
+    CHARACTER (LEN=*) ::  mode  !< identifier for the mode of calculation
+
+    INTEGER(iwp) ::  ij_dum      !< index of nearest wall
+    INTEGER(iwp) ::  il          !< index variable along x
+    INTEGER(iwp) ::  ip          !< index variable along x
+    INTEGER(iwp) ::  jl          !< index variable along y
+    INTEGER(iwp) ::  jp          !< index variable along y
+    INTEGER(iwp) ::  nl          !< loop variable over all agents in a grid box
+    INTEGER(iwp) ::  no          !< loop variable over all agents in a grid box
+    INTEGER(iwp) ::  noa         !< amount of agents in a grid box
+    INTEGER(iwp) ::  sc_x_end    !< index for scan for topography/other agents
+    INTEGER(iwp) ::  sc_x_start  !< index for scan for topography/other agents
+    INTEGER(iwp) ::  sc_y_end    !< index for scan for topography/other agents
+    INTEGER(iwp) ::  sc_y_start  !< index for scan for topography/other agents
+
+    LOGICAL ::  corner_found  !< flag that indicates a corner has been found near agent
+
+    REAL(wp) ::  a_pl             !< factor for collision avoidance
+    REAL(wp) ::  ax_semimaj       !< semiminor axis of repulsive ellipse
+    REAL(wp) ::  b_pl             !< factor for collision avoidance
+    REAL(wp) ::  c_pl             !< factor for collision avoidance
+    REAL(wp) ::  coll_t           !< time at which the next collision would happen
+    REAL(wp) ::  d_coll_t_0       !< inverse of collision cutoff time
+    REAL(wp) ::  d_pl             !< factor for collision avoidance
+    REAL(wp) ::  ddum_f           !< dummy devisor collision avoidance
+    REAL(wp) ::  dist             !< distance to obstacle
+    REAL(wp) ::  dist_sq          !< distance to obstacle squared
+    REAL(wp) ::  pos_rel_x        !< relative position of two agents (x)
+    REAL(wp) ::  pos_rel_y        !< relative position of two agents (y)
+    REAL(wp) ::  r_sq             !< y-position
+    REAL(wp) ::  sra              !< scan radius (agents)
+    REAL(wp) ::  srw              !< local variable for scan radius (walls)
+    REAL(wp) ::  v_rel_x          !< relative velocity (x); collision avoidance
+    REAL(wp) ::  v_rel_y          !< relative velocity (y); collision avoidance
+    REAL(wp) ::  x_a              !< x-position
+    REAL(wp) ::  x_wall           !< x-position of wall
+    REAL(wp) ::  y_a              !< y-position
+    REAL(wp) ::  y_wall           !< y-position of wall
+
+    TYPE(agent_type), DIMENSION(:), POINTER ::  l_agts  !< agents that repulse current agent
+
+!
+!-- Initialization
+    x_a = agents(nl)%x
+    y_a = agents(nl)%y
+
+    SELECT CASE ( TRIM( mode ) )
+!
+!--    Calculation of force due to agent trying to approach desired velocity
+       CASE ( 'acceleration' )
+
+          force_x = force_x + d_tau_accel_agent                                                    &
+                              * ( agents(nl)%speed_des*agents(nl)%speed_e_x - agents(nl)%speed_x )
+
+          force_y = force_y + d_tau_accel_agent                                                    &
+                              * ( agents(nl)%speed_des*agents(nl)%speed_e_y - agents(nl)%speed_y )
+
+!
+!--    Calculation of repulsive forces by other agents in a radius around the current one
+       CASE ( 'other_agents' )
+
+          sra = scan_radius_agent
+          d_coll_t_0 = 1./coll_t_0
+!
+!--       Find relevant gridboxes (those that could contain agents within scan radius)
+          sc_x_start = FLOOR( (x_a - sra) * ddx )
+          sc_x_end   = FLOOR( (x_a + sra) * ddx )
+          sc_y_start = FLOOR( (y_a - sra) * ddx )
+          sc_y_end   = FLOOR( (y_a + sra) * ddx )
+          IF ( sc_x_start < nxlg ) sc_x_start = nxlg
+          IF ( sc_x_end   > nxrg ) sc_x_end   = nxrg
+          IF ( sc_y_start < nysg ) sc_y_start = nysg
+          IF ( sc_y_end   > nyng ) sc_y_end   = nyng
+
+          sra = sra**2
+!
+!--       Loop over all previously found relevant gridboxes
+          DO  il = sc_x_start, sc_x_end
+             DO  jl = sc_y_start, sc_y_end
+                noa = agt_count(jl,il)
+                IF ( noa <= 0 )  CYCLE
+                l_agts => grid_agents(jl,il)%agents(1:noa)
+                DO  no = 1, noa
+!
+!--                Skip self
+                   IF ( jl == jp  .AND.  il == ip  .AND.  no == nl )  CYCLE
+                   pos_rel_x = l_agts(no)%x - x_a
+                   pos_rel_y = l_agts(no)%y - y_a
+                   dist_sq = pos_rel_x**2 + pos_rel_y**2
+                   IF ( dist_sq > sra )  CYCLE
+                   r_sq    = (2*radius_agent)**2
+                   v_rel_x   = agents(nl)%speed_x - l_agts(no)%speed_x
+                   v_rel_y   = agents(nl)%speed_y - l_agts(no)%speed_y
+!
+!--                Collision is already occuring, default to standard social forces.
+                   IF ( dist_sq <= r_sq )  THEN
+                      dist = SQRT( dist_sq ) + 1.0d-12
+                      ax_semimaj = 0.5_wp * SQRT( dist )
+
+                      force_x = force_x - 0.125_wp * repuls_agent                                  &
+                                     * d_sigma_rep_agent / ax_semimaj                              &
+                                     * EXP( - ax_semimaj * d_sigma_rep_agent )                     &
+                                     * ( pos_rel_x / dist )
+
+                      force_y = force_y - 0.125_wp * repuls_agent                                  &
+                                     * d_sigma_rep_agent / ax_semimaj                              &
+                                     * EXP( - ax_semimaj * d_sigma_rep_agent )                     &
+                                     * ( pos_rel_y / dist )
+!
+!--                Currently no collision, calculate collision avoidance force according to
+!--                Karamouzas et al (2014, PRL 113,238701)
+                   ELSE
+!
+!--                   Factors
+                      a_pl = v_rel_x**2 +  v_rel_y**2
+                      b_pl = pos_rel_x*v_rel_x + pos_rel_y*v_rel_y
+                      c_pl = dist_sq - r_sq
+                      d_pl = b_pl**2 - a_pl*c_pl
+!
+!--                   If the two agents are moving non-parallel, calculate collision avoidance
+!--                   social force
+                      IF ( d_pl > 0.0_wp  .AND.  ( a_pl < -0.00001 .OR. a_pl > 0.00001 ) )  THEN
+
+                         d_pl   = SQRT( d_pl )
+                         coll_t = ( b_pl - d_pl ) / a_pl
+                         IF ( coll_t > 0.0_wp )  THEN
+!
+!--                         Dummy factor
+                            ddum_f = 1. / ( a_pl * coll_t**2 ) * ( 2. / coll_t + 1.0 * d_coll_t_0 )
+!
+!--                         x-component of social force
+                            force_x = force_x - k_pl * EXP( -coll_t * d_coll_t_0 ) *               &
+                                      ( v_rel_x - ( b_pl * v_rel_x - a_pl * pos_rel_x ) / d_pl ) * &
+                                      ddum_f
+!
+!--                         y-component of social force
+                            force_y = force_y - k_pl * EXP( -coll_t * d_coll_t_0 ) *               &
+                                      ( v_rel_y - ( b_pl * v_rel_y - a_pl * pos_rel_y ) / d_pl ) * &
+                                      ddum_f
 
                          ENDIF
@@ -4528,50 +4178,228 @@
                 ENDDO
              ENDDO
-
-          CASE DEFAULT
-
-       END SELECT
-
-    END SUBROUTINE mas_timestep_social_forces
-
-!------------------------------------------------------------------------------!
+          ENDDO
+
+       CASE ( 'walls' )
+
+          srw = scan_radius_wall
+          corner_found = .FALSE.
+!
+!--       Find relevant grid boxes (those that could contain topography within radius)
+          sc_x_start = (x_a - srw) * ddx
+          sc_x_end   = (x_a + srw) * ddx
+          sc_y_start = (y_a - srw) * ddx
+          sc_y_end   = (y_a + srw) * ddx
+          IF ( sc_x_start < nxlg ) sc_x_start = nxlg
+          IF ( sc_x_end   > nxrg ) sc_x_end   = nxrg
+          IF ( sc_y_start < nysg ) sc_y_start = nysg
+          IF ( sc_y_end   > nyng ) sc_y_end   = nyng
+!
+!--       Find "walls" ( i.e. topography steps (up or down) higher than one grid box ) that are
+!--       perpendicular to the agent within the defined search radius. Such obstacles cannot be
+!--       passed and a social force to that effect is applied.
+!--       Walls only apply a force perpendicular to the wall to the agent.
+!--       There is therefore a search for walls directly right, left, south and north of the agent.
+!--       All other walls are ignored.
+!--
+!--       Check for wall left of current agent
+          ij_dum = 0
+          IF ( sc_x_start < ip )  THEN
+             DO  il = ip - 1, sc_x_start, -1
+!
+!--             Going left from the agent, check for a right wall
+                IF ( BTEST( obstacle_flags(jp,il), 2 ) )  THEN
+!
+!--                Obstacle found in grid box il, wall at right side
+                   x_wall = (il+1)*dx
+!
+!--                Calculate force of found wall on agent
+                   CALL mas_timestep_wall_corner_force( x_a, x_wall, y_a, y_a )
+!
+!--                Calculate new x starting index for later scan for corners
+                   ij_dum = il + 1
+                   EXIT
+                ENDIF
+             ENDDO
+          ENDIF
+          IF ( ij_dum /= 0 ) sc_x_start = ij_dum
+
+!
+!--       Check for wall right of current agent
+          ij_dum = 0
+          IF ( sc_x_end > ip )  THEN
+             DO  il = ip + 1, sc_x_end
+!
+!--             Going right from the agent, check for a left wall
+                IF ( BTEST( obstacle_flags(jp,il), 6 ) )  THEN
+!
+!--                Obstacle found in grid box il, wall at left side
+                   x_wall = il*dx
+!
+!--                Calculate force of found wall on agent
+                   CALL mas_timestep_wall_corner_force( x_a, x_wall, y_a, y_a )
+!
+!--                Calculate new x end index for later scan for corners
+                   ij_dum = il - 1
+                   EXIT
+                ENDIF
+             ENDDO
+          ENDIF
+          IF ( ij_dum /= 0 ) sc_x_end = ij_dum
+
+!
+!--          Check for wall south of current agent
+          ij_dum = 0
+          IF ( sc_y_start < jp )  THEN
+             DO  jl = jp - 1, sc_y_start, -1
+!
+!--             Going south from the agent, check for a north wall
+                IF ( BTEST( obstacle_flags(jl,ip), 0 ) )  THEN
+!
+!--                Obstacle found in grid box jl, wall at left side
+                   y_wall = ( jl + 1 ) * dy
+
+                   CALL mas_timestep_wall_corner_force( x_a, x_a, y_a, y_wall )
+!
+!--                Calculate new y starting index for later scan for corners
+                   ij_dum = jl + 1
+                   EXIT
+                ENDIF
+             ENDDO
+          ENDIF
+          IF ( ij_dum /= 0 ) sc_y_start = ij_dum
+
+!
+!--       Check for wall north of current agent
+          ij_dum = 0
+          IF ( sc_y_end > jp )  THEN
+             DO  jl = jp + 1, sc_y_end
+!
+!--             Going north from the agent, check for a south wall
+                IF ( BTEST( obstacle_flags(jl,ip), 4 ) )  THEN
+!
+!--                   obstacle found in grid box jl, wall at left side
+                   y_wall = jl * dy
+
+                   CALL mas_timestep_wall_corner_force( x_a, x_a, y_a, y_wall )
+!
+!--                Calculate new y end index for later scan for corners
+                   ij_dum = jl - 1
+                ENDIF
+             ENDDO
+          ENDIF
+          IF ( ij_dum /= 0 ) sc_y_end = ij_dum
+
+!
+!--       Scan for corners surrounding current agent.
+!--       Only gridcells that are closer than the closest wall in each direction (n,s,r,l) are
+!--       considered in the search since those further away would have a significantly smaller
+!--       resulting force than the closer wall.
+          DO  il = sc_x_start, sc_x_end
+             DO  jl = sc_y_start, sc_y_end
+                IF ( il == ip  .OR.  jl == jp )  CYCLE
+!
+!--             Corners left of agent
+                IF ( il < ip )  THEN
+!
+!--                South left quadrant: look for north right corner
+                   IF ( jl < jp )  THEN
+                      IF ( BTEST( obstacle_flags(jl,il), 1 ) )  THEN
+!
+!--                      Calculate coordinates of the found corner
+                         x_wall = ( il + 1 ) * dx
+                         y_wall = ( jl + 1 ) * dy
+
+                         CALL mas_timestep_wall_corner_force( x_a, x_wall, y_a, y_wall )
+
+                      ENDIF
+!
+!--                North left quadrant: look for south right corner
+                   ELSEIF ( jl > jp )  THEN
+                      IF ( BTEST( obstacle_flags(jl,il), 3 ) )  THEN
+!
+!--                      Calculate coordinates of the corner of mentioned gridcell that is closest
+!--                      to the current agent.
+                         x_wall = ( il + 1 ) * dx
+                         y_wall = jl * dy
+
+                         CALL mas_timestep_wall_corner_force( x_a, x_wall, y_a, y_wall )
+
+                      ENDIF
+                   ENDIF
+                ELSEIF ( il > ip )  THEN
+!
+!--                South right quadrant: look for north left corner
+                   IF ( jl < jp )  THEN
+                      IF ( BTEST( obstacle_flags(jl,il), 7 ) )  THEN
+!
+!--                      Calculate coordinates of the corner of mentioned gridcell that is closest
+!--                      to the current agent.
+                         x_wall = il * dx
+                         y_wall = ( jl + 1 ) * dy
+
+                         CALL mas_timestep_wall_corner_force( x_a, x_wall, y_a, y_wall )
+
+                      ENDIF
+!
+!--                North right quadrant: look for south left corner
+                   ELSEIF ( jl > jp )  THEN
+                      IF ( BTEST( obstacle_flags(jl,il), 5 ) )  THEN
+!
+!--                      Calculate coordinates of the corner of mentioned gridcell that is closest
+!--                      to the current agent.
+                         x_wall = il * dx
+                         y_wall = jl * dy
+
+                         CALL mas_timestep_wall_corner_force( x_a, x_wall, y_a, y_wall )
+
+                      ENDIF
+                   ENDIF
+                ENDIF
+             ENDDO
+          ENDDO
+
+       CASE DEFAULT
+
+    END SELECT
+
+ END SUBROUTINE mas_timestep_social_forces
+
+!--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> Given a distance to the current agent, calculates the force a found corner
-!> or wall exerts on that agent
-!------------------------------------------------------------------------------!
-    SUBROUTINE mas_timestep_wall_corner_force( xa, xw, ya, yw )
-
-       IMPLICIT NONE
-
-       REAL(wp) ::  dist_l     !< distance to obstacle
-       REAL(wp) ::  force_d_x  !< increment of social force, x-direction
-       REAL(wp) ::  force_d_y  !< increment of social force, x-direction
-       REAL(wp) ::  xa         !< x-position of agent
-       REAL(wp) ::  xw         !< x-position of wall
-       REAL(wp) ::  ya         !< x-position of agent
-       REAL(wp) ::  yw         !< y-position of wall
-
-       force_d_x = 0.0_wp
-       force_d_y = 0.0_wp
-!
-!--    calculate coordinates of corner relative to agent 
-!--    postion and distance between corner and agent
-       xw = xa - xw
-       yw = ya - yw
-       dist_l = SQRT( (xw)**2 + (yw)**2 )
-!
-!--    calculate x and y component of repulsive force 
-!--    induced by previously found corner
-       IF ( dist_l > 0 ) THEN
-          force_d_x = repuls_wall * d_sigma_rep_wall         &
-                      * EXP( -dist_l * d_sigma_rep_wall )      &
-                      * xw / (dist_l)
-          force_d_y = repuls_wall * d_sigma_rep_wall         &
-                      * EXP( -dist_l * d_sigma_rep_wall )      &
-                      * yw / (dist_l)
-       ENDIF
-
-! !--    forces that are located outside of a sight radius of 
+!> Given a distance to the current agent, calculates the force a found corner or wall exerts on that
+!> agent
+!--------------------------------------------------------------------------------------------------!
+ SUBROUTINE mas_timestep_wall_corner_force( xa, xw, ya, yw )
+
+    IMPLICIT NONE
+
+    REAL(wp) ::  dist_l     !< distance to obstacle
+    REAL(wp) ::  force_d_x  !< increment of social force, x-direction
+    REAL(wp) ::  force_d_y  !< increment of social force, x-direction
+    REAL(wp) ::  xa         !< x-position of agent
+    REAL(wp) ::  xw         !< x-position of wall
+    REAL(wp) ::  ya         !< x-position of agent
+    REAL(wp) ::  yw         !< y-position of wall
+
+    force_d_x = 0.0_wp
+    force_d_y = 0.0_wp
+!
+!-- Calculate coordinates of corner relative to agent postion and distance between corner and agent.
+    xw = xa - xw
+    yw = ya - yw
+    dist_l = SQRT( ( xw )**2 + ( yw )**2 )
+!
+!-- Calculate x and y component of repulsive force induced by previously found corner
+    IF ( dist_l > 0 )  THEN
+       force_d_x = repuls_wall * d_sigma_rep_wall                                                  &
+                   * EXP( -dist_l * d_sigma_rep_wall )                                             &
+                   * xw / (dist_l)
+       force_d_y = repuls_wall * d_sigma_rep_wall                                                  &
+                   * EXP( -dist_l * d_sigma_rep_wall )                                             &
+                   * yw / (dist_l)
+    ENDIF
+
+! !--    forces that are located outside of a sight radius of
 ! !--    200 degrees (-> COS(100./180.*pi) = COS(.555*pi)) of
 ! !--    current agent are considered to have an effect of 50%
@@ -4586,207 +4414,198 @@
 
 !
-!--    add force increment to total force of current agent
-       force_x = force_x + force_d_x
-       force_y = force_y + force_d_y
-
-    END SUBROUTINE mas_timestep_wall_corner_force
-
-!
-!-- Calculates distance of point P to edge (A,B). If A = B, calculates
-!-- point-to-point distance from A/B to P
-    FUNCTION dist_point_to_edge ( a_x, a_y, b_x, b_y, p_x, p_y )
-
-       IMPLICIT NONE
-
-       REAL(wp)  :: ab_x                !< x-coordinate of vector from A to B
-       REAL(wp)  :: ab_y                !< y-coordinate of vector from A to B
-       REAL(wp)  :: ab_d                !< inverse length of vector from A to B
-       REAL(wp)  :: ab_u_x              !< x-coordinate of vector with direction of ab and length 1
-       REAL(wp)  :: ab_u_y              !< y-coordinate of vector with direction of ab and length 1
-       REAL(wp)  :: ba_x                !< x-coordinate of vector from B to A
-       REAL(wp)  :: ba_y                !< y-coordinate of vector from B to A
-       REAL(wp)  :: ap_x                !< x-coordinate of vector from A to P
-       REAL(wp)  :: ap_y                !< y-coordinate of vector from A to P
-       REAL(wp)  :: bp_x                !< x-coordinate of vector from B to P
-       REAL(wp)  :: bp_y                !< y-coordinate of vector from B to P
-       REAL(wp)  :: a_x                 !< x-coordinate of point A of edge
-       REAL(wp)  :: a_y                 !< y-coordinate of point A of edge
-       REAL(wp)  :: b_x                 !< x-coordinate of point B of edge
-       REAL(wp)  :: b_y                 !< y-coordinate of point B of edge
-       REAL(wp)  :: p_x                 !< x-coordinate of point P
-       REAL(wp)  :: p_y                 !< y-coordinate of point P
-       REAL(wp)  :: dist_x              !< x-coordinate of point P
-       REAL(wp)  :: dist_y              !< y-coordinate of point P
-       REAL(wp)  :: dist_point_to_edge  !< y-coordinate of point P
-
-       ab_x = - a_x + b_x
-       ab_y = - a_y + b_y
-       ba_x = - b_x + a_x 
-       ba_y = - b_y + a_y 
-       ap_x = - a_x + p_x
-       ap_y = - a_y + p_y
-       bp_x = - b_x + p_x
-       bp_y = - b_y + p_y
-
-       IF ( ab_x * ap_x + ab_y * ap_y <= 0. ) THEN
-          dist_point_to_edge = SQRT((a_x - p_x)**2 + (a_y - p_y)**2)
-       ELSEIF ( ba_x * bp_x + ba_y * bp_y <= 0. ) THEN
-          dist_point_to_edge = SQRT((b_x - p_x)**2 + (b_y - p_y)**2)
-       ELSE
-          ab_d = 1./SQRT((ab_x)**2+(ab_y)**2)
-          ab_u_x = ab_x*ab_d
-          ab_u_y = ab_y*ab_d
-          dist_x = ap_x - (ap_x*ab_u_x+ap_y*ab_u_y)*ab_u_x
-          dist_y = ap_y - (ap_x*ab_u_x+ap_y*ab_u_y)*ab_u_y
-          dist_point_to_edge = SQRT( dist_x**2 + dist_y**2 )
+!-- Add force increment to total force of current agent
+    force_x = force_x + force_d_x
+    force_y = force_y + force_d_y
+
+ END SUBROUTINE mas_timestep_wall_corner_force
+
+
+!
+!-- Calculates distance of point P to edge (A,B). If A = B, calculates point-to-point distance from
+!-- A/B to P.
+ FUNCTION dist_point_to_edge( a_x, a_y, b_x, b_y, p_x, p_y )
+
+    IMPLICIT NONE
+
+    REAL(wp)  :: a_x                 !< x-coordinate of point A of edge
+    REAL(wp)  :: a_y                 !< y-coordinate of point A of edge
+    REAL(wp)  :: ab_x                !< x-coordinate of vector from A to B
+    REAL(wp)  :: ab_y                !< y-coordinate of vector from A to B
+    REAL(wp)  :: ab_d                !< inverse length of vector from A to B
+    REAL(wp)  :: ab_u_x              !< x-coordinate of vector with direction of ab and length 1
+    REAL(wp)  :: ab_u_y              !< y-coordinate of vector with direction of ab and length 1
+    REAL(wp)  :: ap_x                !< x-coordinate of vector from A to P
+    REAL(wp)  :: ap_y                !< y-coordinate of vector from A to P
+    REAL(wp)  :: b_x                 !< x-coordinate of point B of edge
+    REAL(wp)  :: b_y                 !< y-coordinate of point B of edge
+    REAL(wp)  :: ba_x                !< x-coordinate of vector from B to A
+    REAL(wp)  :: ba_y                !< y-coordinate of vector from B to A
+    REAL(wp)  :: bp_x                !< x-coordinate of vector from B to P
+    REAL(wp)  :: bp_y                !< y-coordinate of vector from B to P
+    REAL(wp)  :: dist_x              !< x-coordinate of point P
+    REAL(wp)  :: dist_y              !< y-coordinate of point P
+    REAL(wp)  :: dist_point_to_edge  !< y-coordinate of point P
+    REAL(wp)  :: p_x                 !< x-coordinate of point P
+    REAL(wp)  :: p_y                 !< y-coordinate of point P
+
+    ab_x = - a_x + b_x
+    ab_y = - a_y + b_y
+    ba_x = - b_x + a_x
+    ba_y = - b_y + a_y
+    ap_x = - a_x + p_x
+    ap_y = - a_y + p_y
+    bp_x = - b_x + p_x
+    bp_y = - b_y + p_y
+
+    IF ( ab_x * ap_x + ab_y * ap_y <= 0. )  THEN
+       dist_point_to_edge = SQRT( ( a_x - p_x )**2 + ( a_y - p_y )**2 )
+    ELSEIF ( ba_x * bp_x + ba_y * bp_y <= 0. )  THEN
+       dist_point_to_edge = SQRT( ( b_x - p_x )**2 + ( b_y - p_y )**2)
+    ELSE
+       ab_d = 1.0_wp / SQRT( ( ab_x )**2 + ( ab_y )**2 )
+       ab_u_x = ab_x * ab_d
+       ab_u_y = ab_y * ab_d
+       dist_x = ap_x - ( ap_x * ab_u_x + ap_y * ab_u_y ) * ab_u_x
+       dist_y = ap_y - ( ap_x * ab_u_x + ap_y * ab_u_y ) * ab_u_y
+       dist_point_to_edge = SQRT( dist_x**2 + dist_y**2 )
+    ENDIF
+
+ END FUNCTION dist_point_to_edge
+
+!
+!-- Returns the heuristic between points A and B (currently the straight distance)
+ FUNCTION heuristic( ax, ay, bx, by )
+
+    IMPLICIT NONE
+
+    REAL(wp)  :: ax           !< x-coordinate of point A
+    REAL(wp)  :: ay           !< y-coordinate of point A
+    REAL(wp)  :: bx           !< x-coordinate of point B
+    REAL(wp)  :: by           !< y-coordinate of point B
+    REAL(wp)  :: heuristic    !< return value
+
+    heuristic = SQRT( ( ax - bx )**2 + ( ay - by )**2 )
+
+ END FUNCTION heuristic
+
+!
+!-- Calculates if point P is left of the infinite line that contains A and B (direction: A to B).
+!-- Concept: 2D rotation of two vectors
+ FUNCTION is_left( ax, ay, bx, by, px, py )
+
+    IMPLICIT NONE
+
+    LOGICAL  :: is_left !< return value; TRUE if P is left of AB
+
+    REAL(wp)  :: ax     !< x-coordinate of point A
+    REAL(wp)  :: ay     !< y-coordinate of point A
+    REAL(wp)  :: bx     !< x-coordinate of point B
+    REAL(wp)  :: by     !< y-coordinate of point B
+    REAL(wp)  :: px     !< x-coordinate of point P
+    REAL(wp)  :: py     !< y-coordinate of point P
+
+    is_left = (bx-ax)*(py-ay)-(px-ax)*(by-ay) > 0
+    IF ( ( ABS( ax - px ) < .001 .AND. ABS( ay - py ) < .001 )  .OR.                               &
+         ( ABS( bx - px ) < .001 .AND. ABS( by - py ) < .001) )                                    &
+    THEN
+       is_left = .FALSE.
+    ENDIF
+
+ END FUNCTION is_left
+
+!
+!-- Calculates if point P is right of the infinite line that contains A and B (direction: A to B)
+!-- Concept: 2D rotation of two vectors
+ FUNCTION is_right( ax, ay, bx, by, px, py )
+
+    IMPLICIT NONE
+
+    LOGICAL  :: is_right !< return value; TRUE if P is right of AB
+
+    REAL(wp), INTENT(IN)  :: ax     !< x-coordinate of point A
+    REAL(wp), INTENT(IN)  :: ay     !< y-coordinate of point A
+    REAL(wp), INTENT(IN)  :: bx     !< x-coordinate of point B
+    REAL(wp), INTENT(IN)  :: by     !< y-coordinate of point B
+    REAL(wp), INTENT(IN)  :: px     !< x-coordinate of point P
+    REAL(wp), INTENT(IN)  :: py     !< y-coordinate of point P
+
+    is_right = (bx-ax)*(py-ay)-(px-ax)*(by-ay) < 0
+    IF ( ( ABS( ax - px ) < 0.001_wp .AND. ABS( ay - py ) < 0.001_wp )  .OR.                       &
+         ( ABS( bx - px ) < 0.001_wp .AND. ABS( by - py ) < 0.001_wp ) )                           &
+    THEN
+       is_right = .FALSE.
+    ENDIF
+
+ END FUNCTION is_right
+
+!
+!-- Returns true if the line segments AB and PQ share an intersection
+ FUNCTION intersect( ax, ay, bx, by, px, py, qx, qy )
+
+    IMPLICIT NONE
+
+    LOGICAL  :: intersect !< return value; TRUE if intersection was found
+    LOGICAL  :: la        !< T if a is left of PQ
+    LOGICAL  :: lb        !< T if b is left of PQ
+    LOGICAL  :: lp        !< T if p is left of AB
+    LOGICAL  :: lq        !< T if q is left of AB
+    LOGICAL  :: poss      !< flag that indicates if an intersection is still possible
+    LOGICAL  :: ra        !< T if a is right of PQ
+    LOGICAL  :: rb        !< T if b is right of PQ
+    LOGICAL  :: rp        !< T if p is right of AB
+    LOGICAL  :: rq        !< T if q is right of AB
+
+    REAL(wp)  :: ax     !< x-coordinate of point A
+    REAL(wp)  :: ay     !< y-coordinate of point A
+    REAL(wp)  :: bx     !< x-coordinate of point B
+    REAL(wp)  :: by     !< y-coordinate of point B
+    REAL(wp)  :: px     !< x-coordinate of point P
+    REAL(wp)  :: py     !< y-coordinate of point P
+    REAL(wp)  :: qx     !< x-coordinate of point Q
+    REAL(wp)  :: qy     !< y-coordinate of point Q
+
+    intersect = .FALSE.
+    poss      = .FALSE.
+!
+!-- Intersection is possible only if P and Q are on opposing sides of AB
+    lp = is_left(ax,ay,bx,by,px,py)
+    rq = is_right(ax,ay,bx,by,qx,qy)
+    IF ( lp  .AND.  rq )  poss = .TRUE.
+    IF ( .NOT. poss )  THEN
+       lq = is_left(ax,ay,bx,by,qx,qy)
+       rp = is_right(ax,ay,bx,by,px,py)
+       IF ( lq  .AND.  rp )  poss = .TRUE.
+    ENDIF
+!
+!-- Intersection occurs only if above test (poss) was true AND A and B are on opposing sides of PQ.
+    IF ( poss )  THEN
+       la = is_left(px,py,qx,qy,ax,ay)
+       rb = is_right(px,py,qx,qy,bx,by)
+       IF ( la  .AND.  rb )  intersect = .TRUE.
+       IF ( .NOT. intersect )  THEN
+          lb = is_left(px,py,qx,qy,bx,by)
+          ra = is_right(px,py,qx,qy,ax,ay)
+          IF ( lb  .AND.  ra )  intersect = .TRUE.
        ENDIF
-
-    END FUNCTION dist_point_to_edge
-
-!
-!-- Returns the heuristic between points A and B (currently the straight 
-!-- distance)
-    FUNCTION heuristic ( ax, ay, bx, by )
-
-       IMPLICIT NONE
-
-       REAL(wp)  :: ax           !< x-coordinate of point A
-       REAL(wp)  :: ay           !< y-coordinate of point A
-       REAL(wp)  :: bx           !< x-coordinate of point B
-       REAL(wp)  :: by           !< y-coordinate of point B
-       REAL(wp)  :: heuristic    !< return value
-
-       heuristic = SQRT(( ax - bx )**2 + ( ay - by )**2)
-
-    END FUNCTION heuristic 
-
-!
-!-- Calculates if point P is left of the infinite
-!-- line that contains A and B (direction: A to B) 
-!-- Concept: 2D rotation of two vectors
-    FUNCTION is_left ( ax, ay, bx, by, px, py )
-
-       IMPLICIT NONE
-
-       LOGICAL  :: is_left !< return value; TRUE if P is left of AB
-
-       REAL(wp)  :: ax     !< x-coordinate of point A
-       REAL(wp)  :: ay     !< y-coordinate of point A
-       REAL(wp)  :: bx     !< x-coordinate of point B
-       REAL(wp)  :: by     !< y-coordinate of point B
-       REAL(wp)  :: px     !< x-coordinate of point P
-       REAL(wp)  :: py     !< y-coordinate of point P
-
-       is_left = (bx-ax)*(py-ay)-(px-ax)*(by-ay) > 0
-       IF ( (ABS(ax-px) < .001 .AND. ABS(ay-py) < .001) .OR.                  &
-            (ABS(bx-px) < .001 .AND. ABS(by-py) < .001) )                     &
-       THEN
-          is_left = .FALSE.
-       ENDIF
-
-       RETURN
-
-    END FUNCTION is_left 
-
-!
-!-- Calculates if point P is right of the infinite
-!-- line that contains A and B (direction: A to B) 
-!-- Concept: 2D rotation of two vectors
-    FUNCTION is_right ( ax, ay, bx, by, px, py )
-
-       IMPLICIT NONE
-
-       LOGICAL  :: is_right !< return value; TRUE if P is right of AB
-
-       REAL(wp), INTENT(IN)  :: ax     !< x-coordinate of point A
-       REAL(wp), INTENT(IN)  :: ay     !< y-coordinate of point A
-       REAL(wp), INTENT(IN)  :: bx     !< x-coordinate of point B
-       REAL(wp), INTENT(IN)  :: by     !< y-coordinate of point B
-       REAL(wp), INTENT(IN)  :: px     !< x-coordinate of point P
-       REAL(wp), INTENT(IN)  :: py     !< y-coordinate of point P
-
-       is_right = (bx-ax)*(py-ay)-(px-ax)*(by-ay) < 0
-       IF ( (ABS(ax-px) < .001 .AND. ABS(ay-py) < .001) .OR.                  &
-            (ABS(bx-px) < .001 .AND. ABS(by-py) < .001) )                     &
-       THEN
-          is_right = .FALSE.
-       ENDIF
-
-       RETURN
-
-    END FUNCTION is_right 
-
-!
-!-- Returns true if the line segments AB and PQ share an intersection
-    FUNCTION intersect ( ax, ay, bx, by, px, py, qx, qy )
-
-       IMPLICIT NONE
-
-       LOGICAL  :: intersect !< return value; TRUE if intersection was found
-       LOGICAL  :: la        !< T if a is left of PQ
-       LOGICAL  :: lb        !< T if b is left of PQ
-       LOGICAL  :: lp        !< T if p is left of AB
-       LOGICAL  :: lq        !< T if q is left of AB
-       LOGICAL  :: poss      !< flag that indicates if an intersection is still possible
-       LOGICAL  :: ra        !< T if a is right of PQ
-       LOGICAL  :: rb        !< T if b is right of PQ
-       LOGICAL  :: rp        !< T if p is right of AB
-       LOGICAL  :: rq        !< T if q is right of AB
-
-       REAL(wp)  :: ax     !< x-coordinate of point A
-       REAL(wp)  :: ay     !< y-coordinate of point A
-       REAL(wp)  :: bx     !< x-coordinate of point B
-       REAL(wp)  :: by     !< y-coordinate of point B
-       REAL(wp)  :: px     !< x-coordinate of point P
-       REAL(wp)  :: py     !< y-coordinate of point P
-       REAL(wp)  :: qx     !< x-coordinate of point Q
-       REAL(wp)  :: qy     !< y-coordinate of point Q
-
-       intersect = .FALSE.
-       poss      = .FALSE.
-!
-!--    Intersection is possible only if P and Q are on opposing sides of AB
-       lp = is_left(ax,ay,bx,by,px,py)
-       rq = is_right(ax,ay,bx,by,qx,qy)
-       IF ( lp .AND. rq ) poss = .TRUE.
-       IF ( .NOT. poss ) THEN
-          lq = is_left(ax,ay,bx,by,qx,qy)
-          rp = is_right(ax,ay,bx,by,px,py)
-          IF ( lq .AND. rp ) poss = .TRUE.
-       ENDIF
-!
-!--    Intersection occurs only if above test (poss) was true AND
-!--    A and B are on opposing sides of PQ
-       IF ( poss ) THEN
-          la = is_left(px,py,qx,qy,ax,ay)
-          rb = is_right(px,py,qx,qy,bx,by)
-          IF ( la .AND. rb ) intersect = .TRUE.
-          IF ( .NOT. intersect ) THEN
-             lb = is_left(px,py,qx,qy,bx,by)
-             ra = is_right(px,py,qx,qy,ax,ay)
-             IF ( lb .AND. ra ) intersect = .TRUE.
-          ENDIF
-       ENDIF
-
-       RETURN
-
-    END FUNCTION intersect 
+    ENDIF
+
+ END FUNCTION intersect
 
 !
 !-- Gives a nuber randomly distributed around an average
-    FUNCTION random_normal ( avg, variation )
-
-       IMPLICIT NONE
-
-       REAL(wp)  :: avg            !< x-coordinate of vector from A to B
-       REAL(wp)  :: variation      !< y-coordinate of vector from A to B
-       REAL(wp)  :: random_normal  !< y-coordinate of vector from A to B
-
-       REAL(wp), DIMENSION(12)  :: random_arr  !< inverse length of vector from A to B
-
-       CALL RANDOM_NUMBER(random_arr)
-       random_normal = avg + variation*(SUM(random_arr)-6.)
-
-    END FUNCTION random_normal
+ FUNCTION random_normal( avg, variation )
+
+    IMPLICIT NONE
+
+    REAL(wp)  :: avg            !< x-coordinate of vector from A to B
+    REAL(wp)  :: random_normal  !< y-coordinate of vector from A to B
+    REAL(wp)  :: variation      !< y-coordinate of vector from A to B
+
+    REAL(wp), DIMENSION(12)  :: random_arr  !< inverse length of vector from A to B
+
+    CALL RANDOM_NUMBER( random_arr )
+    random_normal = avg + variation * ( SUM( random_arr ) - 6.0_wp )
+
+ END FUNCTION random_normal
 
 
