Index: /palm/trunk/SOURCE/Makefile
===================================================================
--- /palm/trunk/SOURCE/Makefile	(revision 3647)
+++ /palm/trunk/SOURCE/Makefile	(revision 3648)
@@ -25,4 +25,7 @@
 # -----------------
 # $Id$
+# Rename surface_output_mod into surface_data_output_mod
+# 
+# 3637 2018-12-20 01:51:36Z knoop
 # Implementation of the PALM module interface
 # 
@@ -672,5 +675,5 @@
 	surface_layer_fluxes_mod.f90 \
 	surface_mod.f90 \
-	surface_output_mod.f90 \
+	surface_data_output_mod.f90 \
 	swap_timelevel.f90 \
 	synthetic_turbulence_generator_mod.f90 \
@@ -1107,5 +1110,5 @@
 	surface_layer_fluxes_mod.o \
 	surface_mod.o \
-	surface_output_mod.o \
+	surface_data_output_mod.o \
 	synthetic_turbulence_generator_mod.o \
 	turbulence_closure_mod.o \
@@ -1337,5 +1340,5 @@
 	salsa_mod.o \
 	spectra_mod.o \
-	surface_output_mod.o \
+	surface_data_output_mod.o \
 	synthetic_turbulence_generator_mod.o \
 	urban_surface_mod.o \
@@ -1422,5 +1425,5 @@
 	salsa_mod.o \
 	surface_layer_fluxes_mod.o \
-	surface_output_mod.o \
+	surface_data_output_mod.o \
 	time_to_string.o \
 	write_restart_data_mod.o
@@ -1654,5 +1657,5 @@
 	mod_kinds.o \
 	modules.o
-surface_output_mod.o: \
+surface_data_output_mod.o: \
 	mod_kinds.o \
 	modules.o \
@@ -1707,5 +1710,5 @@
 	surface_layer_fluxes_mod.o \
 	surface_mod.o \
-	surface_output_mod.o \
+	surface_data_output_mod.o \
 	synthetic_turbulence_generator_mod.o \
 	time_to_string.o \
Index: /palm/trunk/SOURCE/init_3d_model.f90
===================================================================
--- /palm/trunk/SOURCE/init_3d_model.f90	(revision 3647)
+++ /palm/trunk/SOURCE/init_3d_model.f90	(revision 3648)
@@ -15,5 +15,5 @@
 ! PALM. If not, see <http://www.gnu.org/licenses/>.
 !
-! Copyright 1997-2018 Leibniz Universitaet Hannover
+! Copyright 1997-2019 Leibniz Universitaet Hannover
 !------------------------------------------------------------------------------!
 !
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! Rename subroutines for surface-data output
+! 
+! 3636 2018-12-19 13:48:34Z raasch
 ! nopointer option removed
 ! 
@@ -687,6 +690,6 @@
                 surf_usm_h, get_topography_top_index_ji, vertical_surfaces_exist
    
-    USE surface_output_mod,                                                    &
-        ONLY:  surface_output_init
+    USE surface_data_output_mod,                                               &
+        ONLY:  surface_data_output_init
    
     USE transpose_indices
@@ -2446,6 +2449,6 @@
 !
 !-- Initialize surface data output
-    IF ( surface_data_output )  THEN
-       CALL surface_output_init
+    IF ( surface_output )  THEN
+       CALL surface_data_output_init
     ENDIF
 !
Index: /palm/trunk/SOURCE/module_interface.f90
===================================================================
--- /palm/trunk/SOURCE/module_interface.f90	(revision 3647)
+++ /palm/trunk/SOURCE/module_interface.f90	(revision 3648)
@@ -15,5 +15,5 @@
 ! PALM. If not, see <http://www.gnu.org/licenses/>.
 !
-! Copyright 1997-2018 Leibniz Universitaet Hannover
+! Copyright 1997-2019 Leibniz Universitaet Hannover
 !------------------------------------------------------------------------------!
 !
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! Rename subroutines for surface-data output
+! 
+! 3641 2018-12-23 22:10:01Z knoop
 ! Initial implementation of the PALM module interface
 !
@@ -53,5 +56,5 @@
               plant_canopy,                                                    &
               salsa,                                                           &
-              surface_data_output,                                             &
+              surface_output,                                                  &
               syn_turb_gen,                                                    &
               urban_surface,                                                   &
@@ -204,7 +207,7 @@
               spectra_header
 
-   USE surface_output_mod,                                                     &
-       ONLY:  surface_output_parin,                                            &
-              surface_output_check_parameters
+   USE surface_data_output_mod,                                                &
+       ONLY:  surface_data_output_parin,                                       &
+              surface_data_output_check_parameters
 
    USE synthetic_turbulence_generator_mod,                                     &
@@ -368,5 +371,5 @@
    CALL salsa_parin
    CALL spectra_parin
-   CALL surface_output_parin
+   CALL surface_data_output_parin
    CALL stg_parin
    CALL user_parin ! ToDo: make user code a single Fortran module
@@ -401,5 +404,5 @@
    IF ( salsa )                CALL salsa_check_parameters
    IF ( calculate_spectra )    CALL spectra_check_parameters
-   IF ( surface_data_output )  CALL surface_output_check_parameters
+   IF ( surface_output )       CALL surface_data_output_check_parameters
    IF ( syn_turb_gen )         CALL stg_check_parameters
    IF ( urban_surface )        CALL usm_check_parameters
Index: /palm/trunk/SOURCE/modules.f90
===================================================================
--- /palm/trunk/SOURCE/modules.f90	(revision 3647)
+++ /palm/trunk/SOURCE/modules.f90	(revision 3648)
@@ -15,5 +15,5 @@
 ! PALM. If not, see <http://www.gnu.org/licenses/>.
 !
-! Copyright 1997-2018 Leibniz Universitaet Hannover
+! Copyright 1997-2019 Leibniz Universitaet Hannover
 !------------------------------------------------------------------------------!
 !
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! -surface_data_output +surface_output
+! 
+! 3636 2018-12-19 13:48:34Z raasch
 ! nopointer option removed
 ! 
@@ -1351,5 +1354,5 @@
     LOGICAL ::  sloping_surface = .FALSE.                        !< use sloped surface? (namelist parameter alpha_surface)
     LOGICAL ::  spinup = .FALSE.                                 !< perform model spinup without atmosphere code?
-    LOGICAL ::  surface_data_output = .FALSE.                    !< output of surface data
+    LOGICAL ::  surface_output = .FALSE.                         !< output of surface data
     LOGICAL ::  stop_dt = .FALSE.                                !< internal switch to stop the time stepping
     LOGICAL ::  synchronous_exchange = .FALSE.                   !< namelist parameter
Index: /palm/trunk/SOURCE/palm.f90
===================================================================
--- /palm/trunk/SOURCE/palm.f90	(revision 3647)
+++ /palm/trunk/SOURCE/palm.f90	(revision 3648)
@@ -15,5 +15,5 @@
 ! PALM. If not, see <http://www.gnu.org/licenses/>.
 !
-! Copyright 1997-2018 Leibniz Universitaet Hannover
+! Copyright 1997-2019 Leibniz Universitaet Hannover
 !------------------------------------------------------------------------------!
 !
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! Rename subroutines for surface-data output
+! 
+! 3524 2018-11-14 13:36:44Z raasch
 ! unused variable removed
 ! 
@@ -321,6 +324,6 @@
                pmci_modelconfiguration, pmci_parent_initialize
                
-    USE surface_output_mod,                                                    &
-        ONLY:  surface_output_last_action
+    USE surface_data_output_mod,                                               &
+        ONLY:  surface_data_output_last_action
 
     USE write_restart_data_mod,                                                &
@@ -578,6 +581,6 @@
 !
 !-- Last actions for surface output, for instantaneous and time-averaged data
-    CALL surface_output_last_action( 0 )
-    CALL surface_output_last_action( 1 )
+    CALL surface_data_output_last_action( 0 )
+    CALL surface_data_output_last_action( 1 )
 
 !
Index: /palm/trunk/SOURCE/surface_data_output_mod.f90
===================================================================
--- /palm/trunk/SOURCE/surface_data_output_mod.f90	(revision 3648)
+++ /palm/trunk/SOURCE/surface_data_output_mod.f90	(revision 3648)
@@ -0,0 +1,3683 @@
+!> @file surface_data_output_mod.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/>.
+!
+! Copyright 1997-2019 Leibniz Universitaet Hannover
+!------------------------------------------------------------------------------!
+!
+! Current revisions:
+! ------------------
+! 
+! 
+! Former revisions:
+! -----------------
+! $Id$
+! Rename module and subroutines
+! 
+! 3646 2018-12-28 17:58:49Z kanani
+! Bugfix: use time_since_reference_point instead of simulated_time (relevant
+! when using wall/soil spinup)
+! 
+! 3614 2018-12-10 07:05:46Z raasch
+! unused variables removed
+! 
+! 3572 2018-11-28 11:40:28Z suehring
+! Added short- and longwave radiation flux arrays (e.g. diffuse, direct,
+! reflected, resedual) for all surfaces (M. Salim)
+! 
+! 3494 2018-11-06 14:51:27Z suehring
+! Bugfix in gathering surface data from different types and orientation. 
+! Output of total number of surfaces and vertices added.
+! String length is output, for more convinient post-processing.
+! Last actions added.
+! 
+! 3483 2018-11-02 14:19:26Z raasch
+! bugfix: missed directives for MPI added
+! 
+! 3421 2018-10-24 18:39:32Z gronemeier
+! Add output variables
+! Write data into binary file
+!
+! 3420 2018-10-24 17:30:08Z gronemeier
+! Initial implementation from Klaus Ketelsen and Matthias Suehring
+!
+!
+! Authors:
+! --------
+! @author Klaus Ketelsen, Matthias Suehring
+!
+! Description:
+! ------------
+!> Generate output for surface data.
+!> 
+!> @todo Create namelist file for post-processing tool. 
+!------------------------------------------------------------------------------!
+
+MODULE surface_data_output_mod
+
+   USE kinds
+
+   USE arrays_3d,                                                              &
+       ONLY:  zu, zw
+       
+   USE control_parameters,                                                     &
+       ONLY:  surface_output
+       
+   USE grid_variables,                                                         &
+       ONLY: dx,dy
+       
+   USE indices,                                                                &
+       ONLY: nxl, nxr, nys, nyn, nzb, nzt
+       
+   USE pegrid
+       
+   USE surface_mod,                                                            &
+       ONLY:  surf_def_h, surf_def_v, surf_lsm_h, surf_lsm_v,                  &
+              surf_usm_h, surf_usm_v
+
+   IMPLICIT NONE
+
+   TYPE surf_out                      !< data structure which contains all surfaces elements of all types on subdomain
+   
+      INTEGER(iwp) ::  ns             !< number of surface elements on subdomain
+      INTEGER(iwp) ::  ns_total       !< total number of surface elements
+      INTEGER(iwp) ::  npoints        !< number of points / vertices which define a surface element (on subdomain)
+      INTEGER(iwp) ::  npoints_total  !< total number of points / vertices which define a surface element
+      
+      REAL(wp) ::  fillvalue = -9999.9_wp !< fillvalue for surface elements which are not defined
+      
+      REAL(wp), DIMENSION(:), ALLOCATABLE   ::  var_out  !< output variables
+      REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  var_av   !< variables used for averaging
+      REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  points   !< points  / vertices of a surface element
+      REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  polygons !< polygon data of a surface element
+   END TYPE surf_out
+   
+   CHARACTER(LEN=100), DIMENSION(300)       ::  data_output_surf = ' '  !< namelist variable which describes the output variables
+   CHARACTER(LEN=100), DIMENSION(0:1,300)   ::  dosurf = ' '            !< internal variable which describes the output variables and separates 
+                                                                        !< averaged from non-averaged output
+   
+   INTEGER(iwp) ::  average_count_surf = 0                !< number of ensemble members used for averaging
+   INTEGER(iwp) ::  dosurf_no(0:1) = 0                    !< number of surface output quantities
+
+   LOGICAL :: first_output(0:1) = .FALSE.                 !< true if first output was already called
+
+   REAL(wp) ::  averaging_interval_surf  = 9999999.9_wp   !< averaging interval
+   REAL(wp) ::  dt_dosurf = 9999999.9_wp                  !< time interval for instantaneous data output
+   REAL(wp) ::  dt_dosurf_av = 9999999.9_wp               !< time interval for averaged data output
+   REAL(wp) ::  skip_time_dosurf = 0.0_wp                 !< skip time for instantaneous data output
+   REAL(wp) ::  skip_time_dosurf_av = 0.0_wp              !< skip time for averaged data output
+   REAL(wp) ::  time_dosurf = 0.0_wp                      !< internal counter variable to check for instantaneous data output
+   REAL(wp) ::  time_dosurf_av = 0.0_wp                   !< internal counter variable to check for averaged data output
+
+   
+   TYPE(surf_out) ::  surfaces      !< variable which contains all required output information
+   
+   SAVE
+
+   PRIVATE
+
+   INTERFACE  surface_data_output
+      MODULE PROCEDURE surface_data_output
+   END INTERFACE  surface_data_output
+   
+   INTERFACE  surface_data_output_averaging
+      MODULE PROCEDURE surface_data_output_averaging
+   END INTERFACE  surface_data_output_averaging
+   
+   INTERFACE  surface_data_output_check_parameters
+      MODULE PROCEDURE surface_data_output_check_parameters
+   END INTERFACE  surface_data_output_check_parameters
+   
+   INTERFACE  surface_data_output_init
+      MODULE PROCEDURE surface_data_output_init
+   END INTERFACE  surface_data_output_init
+   
+   INTERFACE  surface_data_output_last_action
+      MODULE PROCEDURE surface_data_output_last_action
+   END INTERFACE  surface_data_output_last_action
+      
+   INTERFACE  surface_data_output_parin
+      MODULE PROCEDURE surface_data_output_parin
+   END INTERFACE  surface_data_output_parin
+
+!
+!--Public subroutines
+   PUBLIC surface_data_output, surface_data_output_averaging,                  &
+          surface_data_output_check_parameters, surface_data_output_init,      &
+          surface_data_output_last_action, surface_data_output_parin
+!
+!--Public variables
+   PUBLIC average_count_surf, averaging_interval_surf, dt_dosurf, dt_dosurf_av,&
+          skip_time_dosurf, skip_time_dosurf_av, time_dosurf, time_dosurf_av
+
+ CONTAINS
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Initialization surface-data output data structure: calculation of vertices
+!> and polygon data for the surface elements, allocation of required arrays.
+!------------------------------------------------------------------------------!
+   SUBROUTINE surface_data_output_init
+   
+      IMPLICIT NONE
+
+      INTEGER(iwp) ::  i                 !< grid index in x-direction, also running variable for counting non-average data output
+      INTEGER(iwp) ::  j                 !< grid index in y-direction, also running variable for counting average data output
+      INTEGER(iwp) ::  k                 !< grid index in z-direction
+      INTEGER(iwp) ::  l                 !< running index for surface-element orientation
+      INTEGER(iwp) ::  m                 !< running index for surface elements
+      INTEGER(iwp) ::  npg               !< counter variable for all surface elements ( or polygons )
+      INTEGER(iwp) ::  point_index_count !< local counter variable for point index
+      
+      INTEGER(iwp), DIMENSION(0:numprocs-1) :: num_points_on_pe   !< array which contains the number of points on all mpi ranks
+      INTEGER(iwp), ALLOCATABLE, DIMENSION(:,:,:) ::  point_index !< dummy array used to check where the reference points for surface polygons are located
+           
+!
+!--   Determine the number of surface elements on subdomain
+      surfaces%ns = surf_def_h(0)%ns + surf_lsm_h%ns + surf_usm_h%ns           & !horizontal upward-facing
+                  + surf_def_h(1)%ns                                           & !horizontal downard-facing    
+                  + surf_def_v(0)%ns + surf_lsm_v(0)%ns + surf_usm_v(0)%ns     & !northward-facing
+                  + surf_def_v(1)%ns + surf_lsm_v(1)%ns + surf_usm_v(1)%ns     & !southward-facing   
+                  + surf_def_v(2)%ns + surf_lsm_v(2)%ns + surf_usm_v(2)%ns     & !westward-facing   
+                  + surf_def_v(3)%ns + surf_lsm_v(3)%ns + surf_usm_v(3)%ns       !eastward-facing   
+!
+!--   Allocate output variable and set to _FillValue attribute
+      ALLOCATE ( surfaces%var_out(1:surfaces%ns) )
+      surfaces%var_out = surfaces%fillvalue
+!
+!--   If there is an output of time average output variables, allocate the 
+!--   required array.
+      IF ( dosurf_no(1) > 0 )  THEN
+         ALLOCATE ( surfaces%var_av(1:surfaces%ns,1:dosurf_no(1)) )
+         surfaces%var_av = 0.0_wp
+      ENDIF
+!
+!--   Initialize points and polygon data. 
+!--   In a first step, count the number of points which are defining 
+!--   the surfaces and the polygons.
+      ALLOCATE( point_index(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
+      point_index = -1
+!
+!--   Horizontal default surfaces
+      surfaces%npoints = 0
+      DO  l = 0, 1
+         DO  m = 1, surf_def_h(0)%ns
+!
+!--         Determine the indices of the respective grid cell inside the topography
+            i = surf_def_h(0)%i(m) + surf_def_h(0)%ioff
+            j = surf_def_h(0)%j(m) + surf_def_h(0)%joff
+            k = surf_def_h(0)%k(m) + surf_def_h(0)%koff
+!
+!--         Check if the vertices that define the surface element are already 
+!--         defined, if not, increment the counter.
+            IF ( point_index(k,j,i) < 0 )  THEN
+               surfaces%npoints   = surfaces%npoints + 1    
+               point_index(k,j,i) = surfaces%npoints - 1
+            ENDIF
+            IF ( point_index(k,j,i+1) < 0 )  THEN
+               surfaces%npoints     = surfaces%npoints + 1    
+               point_index(k,j,i+1) = surfaces%npoints - 1
+            ENDIF
+            IF ( point_index(k,j+1,i+1) < 0 )  THEN
+               surfaces%npoints       = surfaces%npoints + 1    
+               point_index(k,j+1,i+1) = surfaces%npoints - 1
+            ENDIF
+            IF ( point_index(k,j+1,i) < 0 )  THEN
+               surfaces%npoints     = surfaces%npoints + 1    
+               point_index(k,j+1,i) = surfaces%npoints - 1
+            ENDIF           
+         ENDDO
+      ENDDO
+      DO  m = 1, surf_lsm_h%ns
+         i = surf_lsm_h%i(m) + surf_lsm_h%ioff
+         j = surf_lsm_h%j(m) + surf_lsm_h%joff
+         k = surf_lsm_h%k(m) + surf_lsm_h%koff
+
+         IF ( point_index(k,j,i) < 0 )  THEN
+            surfaces%npoints   = surfaces%npoints + 1    
+            point_index(k,j,i) = surfaces%npoints - 1
+         ENDIF
+         IF ( point_index(k,j,i+1) < 0 )  THEN
+            surfaces%npoints     = surfaces%npoints + 1    
+            point_index(k,j,i+1) = surfaces%npoints - 1
+         ENDIF
+         IF ( point_index(k,j+1,i+1) < 0 )  THEN
+            surfaces%npoints       = surfaces%npoints + 1    
+            point_index(k,j+1,i+1) = surfaces%npoints - 1
+         ENDIF
+         IF ( point_index(k,j+1,i) < 0 )  THEN
+            surfaces%npoints     = surfaces%npoints + 1    
+            point_index(k,j+1,i) = surfaces%npoints - 1
+         ENDIF    
+      ENDDO
+      DO  m = 1, surf_usm_h%ns
+         i = surf_usm_h%i(m) + surf_usm_h%ioff
+         j = surf_usm_h%j(m) + surf_usm_h%joff
+         k = surf_usm_h%k(m) + surf_usm_h%koff
+
+         IF ( point_index(k,j,i) < 0 )  THEN
+            surfaces%npoints   = surfaces%npoints + 1    
+            point_index(k,j,i) = surfaces%npoints - 1
+         ENDIF
+         IF ( point_index(k,j,i+1) < 0 )  THEN
+            surfaces%npoints     = surfaces%npoints + 1    
+            point_index(k,j,i+1) = surfaces%npoints - 1
+         ENDIF
+         IF ( point_index(k,j+1,i+1) < 0 )  THEN
+            surfaces%npoints       = surfaces%npoints + 1    
+            point_index(k,j+1,i+1) = surfaces%npoints - 1
+         ENDIF
+         IF ( point_index(k,j+1,i) < 0 )  THEN
+            surfaces%npoints     = surfaces%npoints + 1    
+            point_index(k,j+1,i) = surfaces%npoints - 1
+         ENDIF     
+      ENDDO
+!
+!--   Vertical surfaces
+      DO  l = 0, 3
+         DO  m = 1, surf_def_v(l)%ns
+!
+!--         Determine the indices of the respective grid cell inside the topography.
+!--         Please note, j-index for north-facing surfaces ( l==0 ) is 
+!--         identical to the reference j-index outside the grid box. 
+!--         Equivalent for east-facing surfaces and i-index.
+            i = surf_def_v(l)%i(m) + MERGE( surf_def_v(l)%ioff, 0, l == 3 )
+            j = surf_def_v(l)%j(m) + MERGE( surf_def_v(l)%joff, 0, l == 1 )
+            k = surf_def_v(l)%k(m) + surf_def_v(l)%koff
+!
+!--         Lower left /front vertex
+            IF ( point_index(k,j,i) < 0 )  THEN
+               surfaces%npoints   = surfaces%npoints + 1    
+               point_index(k,j,i) = surfaces%npoints - 1
+            ENDIF 
+!
+!--         Upper / lower right index for north- and south-facing surfaces
+            IF ( l == 0  .OR.  l == 1 )  THEN
+               IF ( point_index(k,j,i+1) < 0 )  THEN
+                  surfaces%npoints     = surfaces%npoints + 1    
+                  point_index(k,j,i+1) = surfaces%npoints - 1
+               ENDIF 
+               IF ( point_index(k+1,j,i+1) < 0 )  THEN
+                  surfaces%npoints       = surfaces%npoints + 1    
+                  point_index(k+1,j,i+1) = surfaces%npoints - 1
+               ENDIF
+!
+!--         Upper / lower front index for east- and west-facing surfaces
+            ELSEIF ( l == 2  .OR.  l == 3 )  THEN
+               IF ( point_index(k,j+1,i) < 0 )  THEN
+                  surfaces%npoints     = surfaces%npoints + 1    
+                  point_index(k,j+1,i) = surfaces%npoints - 1
+               ENDIF 
+               IF ( point_index(k+1,j+1,i) < 0 )  THEN
+                  surfaces%npoints       = surfaces%npoints + 1    
+                  point_index(k+1,j+1,i) = surfaces%npoints - 1
+               ENDIF
+            ENDIF
+!
+!--         Upper left / front vertex
+            IF ( point_index(k+1,j,i) < 0 )  THEN
+               surfaces%npoints     = surfaces%npoints + 1    
+               point_index(k+1,j,i) = surfaces%npoints - 1
+            ENDIF
+         ENDDO
+         DO  m = 1, surf_lsm_v(l)%ns
+            i = surf_lsm_v(l)%i(m) + MERGE( surf_lsm_v(l)%ioff, 0, l == 3 )
+            j = surf_lsm_v(l)%j(m) + MERGE( surf_lsm_v(l)%joff, 0, l == 1 )
+            k = surf_lsm_v(l)%k(m) + surf_lsm_v(l)%koff
+!
+!--         Lower left /front vertex
+            IF ( point_index(k,j,i) < 0 )  THEN
+               surfaces%npoints   = surfaces%npoints + 1    
+               point_index(k,j,i) = surfaces%npoints - 1
+            ENDIF 
+!
+!--         Upper / lower right index for north- and south-facing surfaces
+            IF ( l == 0  .OR.  l == 1 )  THEN
+               IF ( point_index(k,j,i+1) < 0 )  THEN
+                  surfaces%npoints     = surfaces%npoints + 1    
+                  point_index(k,j,i+1) = surfaces%npoints - 1
+               ENDIF 
+               IF ( point_index(k+1,j,i+1) < 0 )  THEN
+                  surfaces%npoints       = surfaces%npoints + 1    
+                  point_index(k+1,j,i+1) = surfaces%npoints - 1
+               ENDIF
+!
+!--         Upper / lower front index for east- and west-facing surfaces
+            ELSEIF ( l == 2  .OR.  l == 3 )  THEN
+               IF ( point_index(k,j+1,i) < 0 )  THEN
+                  surfaces%npoints     = surfaces%npoints + 1    
+                  point_index(k,j+1,i) = surfaces%npoints - 1
+               ENDIF 
+               IF ( point_index(k+1,j+1,i) < 0 )  THEN
+                  surfaces%npoints       = surfaces%npoints + 1    
+                  point_index(k+1,j+1,i) = surfaces%npoints - 1
+               ENDIF
+            ENDIF
+!
+!--         Upper left / front vertex
+            IF ( point_index(k+1,j,i) < 0 )  THEN
+               surfaces%npoints     = surfaces%npoints + 1    
+               point_index(k+1,j,i) = surfaces%npoints - 1
+            ENDIF
+         ENDDO
+
+         DO  m = 1, surf_usm_v(l)%ns
+            i = surf_usm_v(l)%i(m) + MERGE( surf_usm_v(l)%ioff, 0, l == 3 )
+            j = surf_usm_v(l)%j(m) + MERGE( surf_usm_v(l)%joff, 0, l == 1 )
+            k = surf_usm_v(l)%k(m) + surf_usm_v(l)%koff
+!
+!--         Lower left /front vertex
+            IF ( point_index(k,j,i) < 0 )  THEN
+               surfaces%npoints   = surfaces%npoints + 1    
+               point_index(k,j,i) = surfaces%npoints - 1
+            ENDIF 
+!
+!--         Upper / lower right index for north- and south-facing surfaces
+            IF ( l == 0  .OR.  l == 1 )  THEN
+               IF ( point_index(k,j,i+1) < 0 )  THEN
+                  surfaces%npoints     = surfaces%npoints + 1    
+                  point_index(k,j,i+1) = surfaces%npoints - 1
+               ENDIF 
+               IF ( point_index(k+1,j,i+1) < 0 )  THEN
+                  surfaces%npoints       = surfaces%npoints + 1    
+                  point_index(k+1,j,i+1) = surfaces%npoints - 1
+               ENDIF
+!
+!--         Upper / lower front index for east- and west-facing surfaces
+            ELSEIF ( l == 2  .OR.  l == 3 )  THEN
+               IF ( point_index(k,j+1,i) < 0 )  THEN
+                  surfaces%npoints     = surfaces%npoints + 1    
+                  point_index(k,j+1,i) = surfaces%npoints - 1
+               ENDIF 
+               IF ( point_index(k+1,j+1,i) < 0 )  THEN
+                  surfaces%npoints       = surfaces%npoints + 1    
+                  point_index(k+1,j+1,i) = surfaces%npoints - 1
+               ENDIF
+            ENDIF
+!
+!--         Upper left / front vertex
+            IF ( point_index(k+1,j,i) < 0 )  THEN
+               surfaces%npoints     = surfaces%npoints + 1    
+               point_index(k+1,j,i) = surfaces%npoints - 1
+            ENDIF
+         ENDDO
+
+      ENDDO
+!
+!--   Allocate the number of points and polygons. Note, the number of polygons
+!--   is identical to the number of surfaces elements, whereas the number
+!--   of points (vertices) which define the polygons can be larger.
+      ALLOCATE( surfaces%points(3,1:surfaces%npoints) )
+      ALLOCATE( surfaces%polygons(5,1:surfaces%ns)    )
+!
+!--   Note, PARAVIEW expects consecutively ordered points which can be
+!--   unambiguously identified. 
+!--   Hence, all PEs know where they should start counting, depending on the
+!--   number of points on the other PE's with lower MPI rank. 
+#if defined( __parallel )
+      CALL MPI_ALLGATHER( surfaces%npoints, 1, MPI_INTEGER,                    &
+                          num_points_on_pe, 1, MPI_INTEGER, comm2d, ierr  )
+#else
+      num_points_on_pe = surfaces%npoints
+#endif
+
+!
+!--   After the number of vertices is counted, repeat the loops and define the
+!--   vertices. Start with the horizontal default surfaces
+!--   First, however, determine the offset where couting of points should be
+!--   started, which is the sum of points of all PE's with lower MPI rank.
+      i                 = 0
+      point_index_count = 0
+      DO WHILE ( i < myid  .AND.  i <= SIZE( num_points_on_pe ) )
+         point_index_count = point_index_count + num_points_on_pe(i)
+         i                 = i + 1
+      ENDDO
+          
+      
+      surfaces%npoints = 0
+      point_index      = -1
+      npg              = 0
+      
+      DO  l = 0, 1
+         DO  m = 1, surf_def_h(0)%ns
+!
+!--         Determine the indices of the respective grid cell inside the topography
+            i = surf_def_h(0)%i(m) + surf_def_h(0)%ioff
+            j = surf_def_h(0)%j(m) + surf_def_h(0)%joff
+            k = surf_def_h(0)%k(m) + surf_def_h(0)%koff
+!
+!--         Check if the vertices that define the surface element are already 
+!--         defined, if not, increment the counter.
+            IF ( point_index(k,j,i) < 0 )  THEN
+               surfaces%npoints   = surfaces%npoints + 1    
+               point_index(k,j,i) = point_index_count
+               point_index_count  = point_index_count + 1               
+               surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
+               surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
+               surfaces%points(3,surfaces%npoints) = zw(k)
+            ENDIF
+            IF ( point_index(k,j,i+1) < 0 )  THEN
+               surfaces%npoints     = surfaces%npoints + 1    
+               point_index(k,j,i+1) = point_index_count
+               point_index_count    = point_index_count + 1 
+               surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
+               surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
+               surfaces%points(3,surfaces%npoints) = zw(k)
+            ENDIF
+            IF ( point_index(k,j+1,i+1) < 0 )  THEN
+               surfaces%npoints       = surfaces%npoints + 1    
+               point_index(k,j+1,i+1) = point_index_count
+               point_index_count      = point_index_count + 1 
+               surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
+               surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
+               surfaces%points(3,surfaces%npoints) = zw(k)
+            ENDIF
+            IF ( point_index(k,j+1,i) < 0 )  THEN
+               surfaces%npoints     = surfaces%npoints + 1    
+               point_index(k,j+1,i) = point_index_count
+               point_index_count    = point_index_count + 1 
+               surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
+               surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
+               surfaces%points(3,surfaces%npoints) = zw(k)
+            ENDIF
+            
+            npg                        = npg + 1
+            surfaces%polygons(1,npg)   = 4
+            surfaces%polygons(2,npg)   = point_index(k,j,i)
+            surfaces%polygons(3,npg)   = point_index(k,j,i+1)
+            surfaces%polygons(4,npg)   = point_index(k,j+1,i+1)
+            surfaces%polygons(5,npg)   = point_index(k,j+1,i)
+         ENDDO
+      ENDDO
+      DO  m = 1, surf_lsm_h%ns
+         i = surf_lsm_h%i(m) + surf_lsm_h%ioff
+         j = surf_lsm_h%j(m) + surf_lsm_h%joff
+         k = surf_lsm_h%k(m) + surf_lsm_h%koff
+         IF ( point_index(k,j,i) < 0 )  THEN
+            surfaces%npoints   = surfaces%npoints + 1    
+            point_index(k,j,i) = point_index_count
+            point_index_count  = point_index_count + 1 
+            surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
+            surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
+            surfaces%points(3,surfaces%npoints) = zw(k)
+         ENDIF
+         IF ( point_index(k,j,i+1) < 0 )  THEN
+            surfaces%npoints     = surfaces%npoints + 1    
+            point_index(k,j,i+1) = point_index_count
+            point_index_count    = point_index_count + 1 
+            surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
+            surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
+            surfaces%points(3,surfaces%npoints) = zw(k)
+         ENDIF
+         IF ( point_index(k,j+1,i+1) < 0 )  THEN
+            surfaces%npoints       = surfaces%npoints + 1    
+            point_index(k,j+1,i+1) = point_index_count
+            point_index_count      = point_index_count + 1 
+            surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
+            surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
+            surfaces%points(3,surfaces%npoints) = zw(k)
+         ENDIF
+         IF ( point_index(k,j+1,i) < 0 )  THEN
+            surfaces%npoints     = surfaces%npoints + 1    
+            point_index(k,j+1,i) = point_index_count
+            point_index_count    = point_index_count + 1 
+            surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
+            surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
+            surfaces%points(3,surfaces%npoints) = zw(k)
+         ENDIF
+         
+         npg                        = npg + 1
+         surfaces%polygons(1,npg)   = 4
+         surfaces%polygons(2,npg)   = point_index(k,j,i)
+         surfaces%polygons(3,npg)   = point_index(k,j,i+1)
+         surfaces%polygons(4,npg)   = point_index(k,j+1,i+1)
+         surfaces%polygons(5,npg)   = point_index(k,j+1,i) 
+      ENDDO
+
+      DO  m = 1, surf_usm_h%ns
+         i = surf_usm_h%i(m) + surf_usm_h%ioff
+         j = surf_usm_h%j(m) + surf_usm_h%joff
+         k = surf_usm_h%k(m) + surf_usm_h%koff
+
+         IF ( point_index(k,j,i) < 0 )  THEN
+            surfaces%npoints   = surfaces%npoints + 1    
+            point_index(k,j,i) = point_index_count
+            point_index_count  = point_index_count + 1 
+            surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
+            surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
+            surfaces%points(3,surfaces%npoints) = zw(k)
+         ENDIF
+         IF ( point_index(k,j,i+1) < 0 )  THEN
+            surfaces%npoints     = surfaces%npoints + 1    
+            point_index(k,j,i+1) = point_index_count
+            point_index_count    = point_index_count + 1 
+            surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
+            surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
+            surfaces%points(3,surfaces%npoints) = zw(k)
+         ENDIF
+         IF ( point_index(k,j+1,i+1) < 0 )  THEN
+            surfaces%npoints       = surfaces%npoints + 1    
+            point_index(k,j+1,i+1) = point_index_count
+            point_index_count      = point_index_count + 1 
+            surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
+            surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
+            surfaces%points(3,surfaces%npoints) = zw(k)
+         ENDIF
+         IF ( point_index(k,j+1,i) < 0 )  THEN
+            surfaces%npoints     = surfaces%npoints + 1    
+            point_index(k,j+1,i) = point_index_count
+            point_index_count    = point_index_count + 1 
+            surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
+            surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
+            surfaces%points(3,surfaces%npoints) = zw(k)
+         ENDIF
+         
+         npg                        = npg + 1
+         surfaces%polygons(1,npg)   = 4
+         surfaces%polygons(2,npg)   = point_index(k,j,i)
+         surfaces%polygons(3,npg)   = point_index(k,j,i+1)
+         surfaces%polygons(4,npg)   = point_index(k,j+1,i+1)
+         surfaces%polygons(5,npg)   = point_index(k,j+1,i)    
+      ENDDO
+
+      DO  l = 0, 3
+         DO  m = 1, surf_def_v(l)%ns
+!
+!--         Determine the indices of the respective grid cell inside the topography.
+!--         Please note, j-index for north-facing surfaces ( l==0 ) is 
+!--         identical to the reference j-index outside the grid box. 
+!--         Equivalent for east-facing surfaces and i-index.
+            i = surf_def_v(l)%i(m) + MERGE( surf_def_v(l)%ioff, 0, l == 3 )
+            j = surf_def_v(l)%j(m) + MERGE( surf_def_v(l)%joff, 0, l == 1 )
+            k = surf_def_v(l)%k(m) + surf_def_v(l)%koff
+!
+!--         Lower left /front vertex
+            IF ( point_index(k,j,i) < 0 )  THEN
+               surfaces%npoints   = surfaces%npoints + 1    
+               point_index(k,j,i) = point_index_count
+               point_index_count  = point_index_count + 1 
+               surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
+               surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
+               surfaces%points(3,surfaces%npoints) = zw(k-1)
+            ENDIF 
+!
+!--         Upper / lower right index for north- and south-facing surfaces
+            IF ( l == 0  .OR.  l == 1 )  THEN
+               IF ( point_index(k,j,i+1) < 0 )  THEN
+                  surfaces%npoints     = surfaces%npoints + 1    
+                  point_index(k,j,i+1) = point_index_count
+                  point_index_count    = point_index_count + 1
+                  surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
+                  surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
+                  surfaces%points(3,surfaces%npoints) = zw(k-1)
+               ENDIF 
+               IF ( point_index(k+1,j,i+1) < 0 )  THEN
+                  surfaces%npoints       = surfaces%npoints + 1    
+                  point_index(k+1,j,i+1) = point_index_count
+                  point_index_count      = point_index_count + 1
+                  surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
+                  surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
+                  surfaces%points(3,surfaces%npoints) = zw(k)
+               ENDIF
+!
+!--         Upper / lower front index for east- and west-facing surfaces
+            ELSEIF ( l == 2  .OR.  l == 3 )  THEN
+               IF ( point_index(k,j+1,i) < 0 )  THEN
+                  surfaces%npoints     = surfaces%npoints + 1    
+                  point_index(k,j+1,i) = point_index_count
+                  point_index_count    = point_index_count + 1
+                  surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
+                  surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
+                  surfaces%points(3,surfaces%npoints) = zw(k-1)
+               ENDIF 
+               IF ( point_index(k+1,j+1,i) < 0 )  THEN
+                  surfaces%npoints       = surfaces%npoints + 1    
+                  point_index(k+1,j+1,i) = point_index_count
+                  point_index_count      = point_index_count + 1
+                  surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
+                  surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
+                  surfaces%points(3,surfaces%npoints) = zw(k)
+               ENDIF
+            ENDIF
+!
+!--         Upper left / front vertex
+            IF ( point_index(k+1,j,i) < 0 )  THEN
+               surfaces%npoints     = surfaces%npoints + 1    
+               point_index(k+1,j,i) = point_index_count
+               point_index_count    = point_index_count + 1
+               surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
+               surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
+               surfaces%points(3,surfaces%npoints) = zw(k)
+            ENDIF
+            
+            npg = npg + 1            
+            IF ( l == 0  .OR.  l == 1 )  THEN
+               surfaces%polygons(1,npg)   = 4
+               surfaces%polygons(2,npg)   = point_index(k,j,i)
+               surfaces%polygons(3,npg)   = point_index(k,j,i+1)
+               surfaces%polygons(4,npg)   = point_index(k+1,j,i+1)
+               surfaces%polygons(5,npg)   = point_index(k+1,j,i)  
+            ELSE
+               surfaces%polygons(1,npg)   = 4
+               surfaces%polygons(2,npg)   = point_index(k,j,i)
+               surfaces%polygons(3,npg)   = point_index(k,j+1,i)
+               surfaces%polygons(4,npg)   = point_index(k+1,j+1,i)
+               surfaces%polygons(5,npg)   = point_index(k+1,j,i)
+            ENDIF
+            
+         ENDDO
+
+         DO  m = 1, surf_lsm_v(l)%ns
+            i = surf_lsm_v(l)%i(m) + MERGE( surf_lsm_v(l)%ioff, 0, l == 3 )
+            j = surf_lsm_v(l)%j(m) + MERGE( surf_lsm_v(l)%joff, 0, l == 1 )
+            k = surf_lsm_v(l)%k(m) + surf_lsm_v(l)%koff
+!
+!--         Lower left /front vertex
+            IF ( point_index(k,j,i) < 0 )  THEN
+               surfaces%npoints   = surfaces%npoints + 1    
+               point_index(k,j,i) = point_index_count
+               point_index_count  = point_index_count + 1
+               surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
+               surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
+               surfaces%points(3,surfaces%npoints) = zw(k-1)
+            ENDIF 
+!
+!--         Upper / lower right index for north- and south-facing surfaces
+            IF ( l == 0  .OR.  l == 1 )  THEN
+               IF ( point_index(k,j,i+1) < 0 )  THEN
+                  surfaces%npoints     = surfaces%npoints + 1    
+                  point_index(k,j,i+1) = point_index_count
+                  point_index_count    = point_index_count + 1
+                  surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
+                  surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
+                  surfaces%points(3,surfaces%npoints) = zw(k-1)
+               ENDIF 
+               IF ( point_index(k+1,j,i+1) < 0 )  THEN
+                  surfaces%npoints       = surfaces%npoints + 1    
+                  point_index(k+1,j,i+1) = point_index_count
+                  point_index_count      = point_index_count + 1
+                  surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
+                  surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
+                  surfaces%points(3,surfaces%npoints) = zw(k)
+               ENDIF
+!
+!--         Upper / lower front index for east- and west-facing surfaces
+            ELSEIF ( l == 2  .OR.  l == 3 )  THEN
+               IF ( point_index(k,j+1,i) < 0 )  THEN
+                  surfaces%npoints     = surfaces%npoints + 1    
+                  point_index(k,j+1,i) = point_index_count
+                  point_index_count    = point_index_count + 1
+                  surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
+                  surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
+                  surfaces%points(3,surfaces%npoints) = zw(k-1)
+               ENDIF 
+               IF ( point_index(k+1,j+1,i) < 0 )  THEN
+                  surfaces%npoints       = surfaces%npoints + 1    
+                  point_index(k+1,j+1,i) = point_index_count
+                  point_index_count      = point_index_count + 1
+                  surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
+                  surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
+                  surfaces%points(3,surfaces%npoints) = zw(k)
+               ENDIF
+            ENDIF
+!
+!--         Upper left / front vertex
+            IF ( point_index(k+1,j,i) < 0 )  THEN
+               surfaces%npoints     = surfaces%npoints + 1    
+               point_index(k+1,j,i) = point_index_count
+               point_index_count    = point_index_count + 1
+               surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
+               surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
+               surfaces%points(3,surfaces%npoints) = zw(k)
+            ENDIF
+            
+            npg = npg + 1            
+            IF ( l == 0  .OR.  l == 1 )  THEN
+               surfaces%polygons(1,npg)   = 4
+               surfaces%polygons(2,npg)   = point_index(k,j,i)
+               surfaces%polygons(3,npg)   = point_index(k,j,i+1)
+               surfaces%polygons(4,npg)   = point_index(k+1,j,i+1)
+               surfaces%polygons(5,npg)   = point_index(k+1,j,i)  
+            ELSE
+               surfaces%polygons(1,npg)   = 4
+               surfaces%polygons(2,npg)   = point_index(k,j,i)
+               surfaces%polygons(3,npg)   = point_index(k,j+1,i)
+               surfaces%polygons(4,npg)   = point_index(k+1,j+1,i)
+               surfaces%polygons(5,npg)   = point_index(k+1,j,i)
+            ENDIF
+         ENDDO
+         DO  m = 1, surf_usm_v(l)%ns
+            i = surf_usm_v(l)%i(m) + MERGE( surf_usm_v(l)%ioff, 0, l == 3 )
+            j = surf_usm_v(l)%j(m) + MERGE( surf_usm_v(l)%joff, 0, l == 1 )
+            k = surf_usm_v(l)%k(m) + surf_usm_v(l)%koff
+!
+!--         Lower left /front vertex
+            IF ( point_index(k,j,i) < 0 )  THEN
+               surfaces%npoints   = surfaces%npoints + 1    
+               point_index(k,j,i) = point_index_count
+               point_index_count  = point_index_count + 1
+               surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
+               surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
+               surfaces%points(3,surfaces%npoints) = zw(k-1)
+            ENDIF 
+!
+!--         Upper / lower right index for north- and south-facing surfaces
+            IF ( l == 0  .OR.  l == 1 )  THEN
+               IF ( point_index(k,j,i+1) < 0 )  THEN
+                  surfaces%npoints     = surfaces%npoints + 1    
+                  point_index(k,j,i+1) = point_index_count
+                  point_index_count    = point_index_count + 1
+                  surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
+                  surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
+                  surfaces%points(3,surfaces%npoints) = zw(k-1)
+               ENDIF 
+               IF ( point_index(k+1,j,i+1) < 0 )  THEN
+                  surfaces%npoints       = surfaces%npoints + 1    
+                  point_index(k+1,j,i+1) = point_index_count
+                  point_index_count      = point_index_count + 1
+                  surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
+                  surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
+                  surfaces%points(3,surfaces%npoints) = zw(k)
+               ENDIF
+!
+!--         Upper / lower front index for east- and west-facing surfaces
+            ELSEIF ( l == 2  .OR.  l == 3 )  THEN
+               IF ( point_index(k,j+1,i) < 0 )  THEN
+                  surfaces%npoints     = surfaces%npoints + 1    
+                  point_index(k,j+1,i) = point_index_count
+                  point_index_count    = point_index_count + 1
+                  surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
+                  surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
+                  surfaces%points(3,surfaces%npoints) = zw(k-1)
+               ENDIF 
+               IF ( point_index(k+1,j+1,i) < 0 )  THEN
+                  surfaces%npoints       = surfaces%npoints + 1    
+                  point_index(k+1,j+1,i) = point_index_count
+                  point_index_count      = point_index_count + 1
+                  surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
+                  surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
+                  surfaces%points(3,surfaces%npoints) = zw(k)
+               ENDIF
+            ENDIF
+!
+!--         Upper left / front vertex
+            IF ( point_index(k+1,j,i) < 0 )  THEN
+               surfaces%npoints     = surfaces%npoints + 1    
+               point_index(k+1,j,i) = point_index_count
+               point_index_count    = point_index_count + 1
+               surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
+               surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
+               surfaces%points(3,surfaces%npoints) = zw(k)
+            ENDIF
+            
+            npg = npg + 1            
+            IF ( l == 0  .OR.  l == 1 )  THEN
+               surfaces%polygons(1,npg)   = 4
+               surfaces%polygons(2,npg)   = point_index(k,j,i)
+               surfaces%polygons(3,npg)   = point_index(k,j,i+1)
+               surfaces%polygons(4,npg)   = point_index(k+1,j,i+1)
+               surfaces%polygons(5,npg)   = point_index(k+1,j,i)  
+            ELSE
+               surfaces%polygons(1,npg)   = 4
+               surfaces%polygons(2,npg)   = point_index(k,j,i)
+               surfaces%polygons(3,npg)   = point_index(k,j+1,i)
+               surfaces%polygons(4,npg)   = point_index(k+1,j+1,i)
+               surfaces%polygons(5,npg)   = point_index(k+1,j,i)
+            ENDIF
+         ENDDO
+
+      ENDDO
+!
+!--   Deallocate temporary dummy variable
+      DEALLOCATE ( point_index )
+!
+!--   Sum-up total number of surface elements and vertices on domain. This
+!--   will be needed for post-processing. 
+      surfaces%npoints_total = 0
+#if defined( __parallel )
+       CALL MPI_ALLREDUCE( surfaces%npoints, surfaces%npoints_total, 1,        &
+                           MPI_INTEGER, MPI_SUM, comm2d, ierr )
+       CALL MPI_ALLREDUCE( surfaces%ns, surfaces%ns_total, 1,                  &
+                           MPI_INTEGER, MPI_SUM, comm2d, ierr )
+#else
+       surfaces%npoints_total = surfaces%npoints
+       surfaces%ns_total      = surfaces%ns
+#endif
+
+   END SUBROUTINE surface_data_output_init
+   
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Routine for controlling the data output. Surface data is collected from 
+!> different types of surfaces (default, natural, urban) and different 
+!> orientation and written to one 1D-output array. Further, NetCDF routines
+!> are called to write the surface data in the respective NetCDF files.
+!------------------------------------------------------------------------------!   
+   SUBROUTINE surface_data_output( av )
+   
+      USE control_parameters,                                                  &
+          ONLY:  io_blocks, io_group, time_since_reference_point
+
+      USE pegrid,                                                              &
+          ONLY:  comm2d, ierr
+
+   
+      IMPLICIT NONE
+
+      CHARACTER(LEN=100) ::  trimvar = ' ' !< dummy for single output variable
+      
+      INTEGER(iwp) ::  av     !< id indicating average or non-average data output
+      INTEGER(iwp) ::  i      !< loop index
+      INTEGER(iwp) ::  n_out  !< counter variables for surface output
+
+!
+!--   Return, if nothing to output
+      IF ( dosurf_no(av) == 0 )  RETURN
+!
+!--   Open files
+      CALL check_open( 25+av )
+!
+!--   Write coordinates 
+      IF ( .NOT. first_output(av) )  THEN
+         DO  i = 0, io_blocks-1
+            IF ( i == io_group )  THEN
+               WRITE ( 25+av )  surfaces%npoints
+               WRITE ( 25+av )  surfaces%npoints_total
+               WRITE ( 25+av )  surfaces%ns
+               WRITE ( 25+av )  surfaces%ns_total
+               WRITE ( 25+av )  surfaces%points
+               WRITE ( 25+av )  surfaces%polygons
+            ENDIF
+#if defined( __parallel )
+            CALL MPI_BARRIER( comm2d, ierr )
+#endif
+            first_output(av) = .TRUE.
+         ENDDO
+      ENDIF
+
+      n_out = 0
+      DO  WHILE ( dosurf(av,n_out+1)(1:1) /= ' ' )
+
+         n_out   = n_out + 1
+         trimvar = TRIM( dosurf(av,n_out) )
+!
+!--      Set the output array to the _FillValue in case it is not 
+!--      defined for each type of surface.
+         surfaces%var_out = surfaces%fillvalue
+         SELECT CASE ( trimvar )
+
+            CASE ( 'us' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%us,          &
+                                               surf_def_h(1)%us,               &
+                                               surf_lsm_h%us,                  &
+                                               surf_usm_h%us,                  &
+                                               surf_def_v(0)%us,               &
+                                               surf_lsm_v(0)%us,               &
+                                               surf_usm_v(0)%us,               &
+                                               surf_def_v(1)%us,               &
+                                               surf_lsm_v(1)%us,               &
+                                               surf_usm_v(1)%us,               &
+                                               surf_def_v(2)%us,               &
+                                               surf_lsm_v(2)%us,               &
+                                               surf_usm_v(2)%us,               &
+                                               surf_def_v(3)%us,               &
+                                               surf_lsm_v(3)%us,               &
+                                               surf_usm_v(3)%us ) 
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'ts' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%ts,          &
+                                               surf_def_h(1)%ts,               &
+                                               surf_lsm_h%ts,                  &
+                                               surf_usm_h%ts,                  &
+                                               surf_def_v(0)%ts,               &
+                                               surf_lsm_v(0)%ts,               &
+                                               surf_usm_v(0)%ts,               &
+                                               surf_def_v(1)%ts,               &
+                                               surf_lsm_v(1)%ts,               &
+                                               surf_usm_v(1)%ts,               &
+                                               surf_def_v(2)%ts,               &
+                                               surf_lsm_v(2)%ts,               &
+                                               surf_usm_v(2)%ts,               &
+                                               surf_def_v(3)%ts,               &
+                                               surf_lsm_v(3)%ts,               &
+                                               surf_usm_v(3)%ts ) 
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'qs' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%qs,          &
+                                               surf_def_h(1)%qs,               &
+                                               surf_lsm_h%qs,                  &
+                                               surf_usm_h%qs,                  &
+                                               surf_def_v(0)%qs,               &
+                                               surf_lsm_v(0)%qs,               &
+                                               surf_usm_v(0)%qs,               &
+                                               surf_def_v(1)%qs,               &
+                                               surf_lsm_v(1)%qs,               &
+                                               surf_usm_v(1)%qs,               &
+                                               surf_def_v(2)%qs,               &
+                                               surf_lsm_v(2)%qs,               &
+                                               surf_usm_v(2)%qs,               &
+                                               surf_def_v(3)%qs,               &
+                                               surf_lsm_v(3)%qs,               &
+                                               surf_usm_v(3)%qs ) 
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'ss' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%ss,          &
+                                               surf_def_h(1)%ss,               &
+                                               surf_lsm_h%ss,                  &
+                                               surf_usm_h%ss,                  &
+                                               surf_def_v(0)%ss,               &
+                                               surf_lsm_v(0)%ss,               &
+                                               surf_usm_v(0)%ss,               &
+                                               surf_def_v(1)%ss,               &
+                                               surf_lsm_v(1)%ss,               &
+                                               surf_usm_v(1)%ss,               &
+                                               surf_def_v(2)%ss,               &
+                                               surf_lsm_v(2)%ss,               &
+                                               surf_usm_v(2)%ss,               &
+                                               surf_def_v(3)%ss,               &
+                                               surf_lsm_v(3)%ss,               &
+                                               surf_usm_v(3)%ss ) 
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'qcs' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%qcs,         &
+                                               surf_def_h(1)%qcs,              &
+                                               surf_lsm_h%qcs,                 &
+                                               surf_usm_h%qcs,                 &
+                                               surf_def_v(0)%qcs,              &
+                                               surf_lsm_v(0)%qcs,              &
+                                               surf_usm_v(0)%qcs,              &
+                                               surf_def_v(1)%qcs,              &
+                                               surf_lsm_v(1)%qcs,              &
+                                               surf_usm_v(1)%qcs,              &
+                                               surf_def_v(2)%qcs,              &
+                                               surf_lsm_v(2)%qcs,              &
+                                               surf_usm_v(2)%qcs,              &
+                                               surf_def_v(3)%qcs,              &
+                                               surf_lsm_v(3)%qcs,              &
+                                               surf_usm_v(3)%qcs ) 
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'ncs' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%ncs,         &
+                                               surf_def_h(1)%ncs,              &
+                                               surf_lsm_h%ncs,                 &
+                                               surf_usm_h%ncs,                 &
+                                               surf_def_v(0)%ncs,              &
+                                               surf_lsm_v(0)%ncs,              &
+                                               surf_usm_v(0)%ncs,              &
+                                               surf_def_v(1)%ncs,              &
+                                               surf_lsm_v(1)%ncs,              &
+                                               surf_usm_v(1)%ncs,              &
+                                               surf_def_v(2)%ncs,              &
+                                               surf_lsm_v(2)%ncs,              &
+                                               surf_usm_v(2)%ncs,              &
+                                               surf_def_v(3)%ncs,              &
+                                               surf_lsm_v(3)%ncs,              &
+                                               surf_usm_v(3)%ncs ) 
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'qrs' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%qrs,         &
+                                               surf_def_h(1)%qrs,              &
+                                               surf_lsm_h%qrs,                 &
+                                               surf_usm_h%qrs,                 &
+                                               surf_def_v(0)%qrs,              &
+                                               surf_lsm_v(0)%qrs,              &
+                                               surf_usm_v(0)%qrs,              &
+                                               surf_def_v(1)%qrs,              &
+                                               surf_lsm_v(1)%qrs,              &
+                                               surf_usm_v(1)%qrs,              &
+                                               surf_def_v(2)%qrs,              &
+                                               surf_lsm_v(2)%qrs,              &
+                                               surf_usm_v(2)%qrs,              &
+                                               surf_def_v(3)%qrs,              &
+                                               surf_lsm_v(3)%qrs,              &
+                                               surf_usm_v(3)%qrs ) 
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'nrs' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%nrs,         &
+                                               surf_def_h(1)%nrs,              &
+                                               surf_lsm_h%nrs,                 &
+                                               surf_usm_h%nrs,                 &
+                                               surf_def_v(0)%nrs,              &
+                                               surf_lsm_v(0)%nrs,              &
+                                               surf_usm_v(0)%nrs,              &
+                                               surf_def_v(1)%nrs,              &
+                                               surf_lsm_v(1)%nrs,              &
+                                               surf_usm_v(1)%nrs,              &
+                                               surf_def_v(2)%nrs,              &
+                                               surf_lsm_v(2)%nrs,              &
+                                               surf_usm_v(2)%nrs,              &
+                                               surf_def_v(3)%nrs,              &
+                                               surf_lsm_v(3)%nrs,              &
+                                               surf_usm_v(3)%nrs ) 
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'ol' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%ol,          &
+                                               surf_def_h(1)%ol,               &
+                                               surf_lsm_h%ol,                  &
+                                               surf_usm_h%ol,                  &
+                                               surf_def_v(0)%ol,               &
+                                               surf_lsm_v(0)%ol,               &
+                                               surf_usm_v(0)%ol,               &
+                                               surf_def_v(1)%ol,               &
+                                               surf_lsm_v(1)%ol,               &
+                                               surf_usm_v(1)%ol,               &
+                                               surf_def_v(2)%ol,               &
+                                               surf_lsm_v(2)%ol,               &
+                                               surf_usm_v(2)%ol,               &
+                                               surf_def_v(3)%ol,               &
+                                               surf_lsm_v(3)%ol,               &
+                                               surf_usm_v(3)%ol ) 
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'z0' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%z0,          &
+                                               surf_def_h(1)%z0,               &
+                                               surf_lsm_h%z0,                  &
+                                               surf_usm_h%z0,                  &
+                                               surf_def_v(0)%z0,               &
+                                               surf_lsm_v(0)%z0,               &
+                                               surf_usm_v(0)%z0,               &
+                                               surf_def_v(1)%z0,               &
+                                               surf_lsm_v(1)%z0,               &
+                                               surf_usm_v(1)%z0,               &
+                                               surf_def_v(2)%z0,               &
+                                               surf_lsm_v(2)%z0,               &
+                                               surf_usm_v(2)%z0,               &
+                                               surf_def_v(3)%z0,               &
+                                               surf_lsm_v(3)%z0,               &
+                                               surf_usm_v(3)%z0 )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'z0h' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%z0h,         &
+                                               surf_def_h(1)%z0h,              &
+                                               surf_lsm_h%z0h,                 &
+                                               surf_usm_h%z0h,                 &
+                                               surf_def_v(0)%z0h,              &
+                                               surf_lsm_v(0)%z0h,              &
+                                               surf_usm_v(0)%z0h,              &
+                                               surf_def_v(1)%z0h,              &
+                                               surf_lsm_v(1)%z0h,              &
+                                               surf_usm_v(1)%z0h,              &
+                                               surf_def_v(2)%z0h,              &
+                                               surf_lsm_v(2)%z0h,              &
+                                               surf_usm_v(2)%z0h,              &
+                                               surf_def_v(3)%z0h,              &
+                                               surf_lsm_v(3)%z0h,              &
+                                               surf_usm_v(3)%z0h )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'z0q' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%z0q,         &
+                                               surf_def_h(1)%z0q,              &
+                                               surf_lsm_h%z0q,                 &
+                                               surf_usm_h%z0q,                 &
+                                               surf_def_v(0)%z0q,              &
+                                               surf_lsm_v(0)%z0q,              &
+                                               surf_usm_v(0)%z0q,              &
+                                               surf_def_v(1)%z0q,              &
+                                               surf_lsm_v(1)%z0q,              &
+                                               surf_usm_v(1)%z0q,              &
+                                               surf_def_v(2)%z0q,              &
+                                               surf_lsm_v(2)%z0q,              &
+                                               surf_usm_v(2)%z0q,              &
+                                               surf_def_v(3)%z0q,              &
+                                               surf_lsm_v(3)%z0q,              &
+                                               surf_usm_v(3)%z0q ) 
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+
+               ENDIF
+
+            CASE ( 'theta1' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%pt1,         &
+                                               surf_def_h(1)%pt1,              &
+                                               surf_lsm_h%pt1,                 &
+                                               surf_usm_h%pt1,                 &
+                                               surf_def_v(0)%pt1,              &
+                                               surf_lsm_v(0)%pt1,              &
+                                               surf_usm_v(0)%pt1,              &
+                                               surf_def_v(1)%pt1,              &
+                                               surf_lsm_v(1)%pt1,              &
+                                               surf_usm_v(1)%pt1,              &
+                                               surf_def_v(2)%pt1,              &
+                                               surf_lsm_v(2)%pt1,              &
+                                               surf_usm_v(2)%pt1,              &
+                                               surf_def_v(3)%pt1,              &
+                                               surf_lsm_v(3)%pt1,              &
+                                               surf_usm_v(3)%pt1 )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'qv1' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%qv1,         &
+                                               surf_def_h(1)%qv1,              &
+                                               surf_lsm_h%qv1,                 &
+                                               surf_usm_h%qv1,                 &
+                                               surf_def_v(0)%qv1,              &
+                                               surf_lsm_v(0)%qv1,              &
+                                               surf_usm_v(0)%qv1,              &
+                                               surf_def_v(1)%qv1,              &
+                                               surf_lsm_v(1)%qv1,              &
+                                               surf_usm_v(1)%qv1,              &
+                                               surf_def_v(2)%qv1,              &
+                                               surf_lsm_v(2)%qv1,              &
+                                               surf_usm_v(2)%qv1,              &
+                                               surf_def_v(3)%qv1,              &
+                                               surf_lsm_v(3)%qv1,              &
+                                               surf_usm_v(3)%qv1 )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'thetav1' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%vpt1,        &
+                                               surf_def_h(1)%vpt1,             &
+                                               surf_lsm_h%vpt1,                &
+                                               surf_usm_h%vpt1,                &
+                                               surf_def_v(0)%vpt1,             &
+                                               surf_lsm_v(0)%vpt1,             &
+                                               surf_usm_v(0)%vpt1,             &
+                                               surf_def_v(1)%vpt1,             &
+                                               surf_lsm_v(1)%vpt1,             &
+                                               surf_usm_v(1)%vpt1,             &
+                                               surf_def_v(2)%vpt1,             &
+                                               surf_lsm_v(2)%vpt1,             &
+                                               surf_usm_v(2)%vpt1,             &
+                                               surf_def_v(3)%vpt1,             &
+                                               surf_lsm_v(3)%vpt1,             &
+                                               surf_usm_v(3)%vpt1 )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'usws' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%usws,        &
+                                               surf_def_h(1)%usws,             &
+                                               surf_lsm_h%usws,                &
+                                               surf_usm_h%usws,                &
+                                               surf_def_v(0)%usws,             &
+                                               surf_lsm_v(0)%usws,             &
+                                               surf_usm_v(0)%usws,             &
+                                               surf_def_v(1)%usws,             &
+                                               surf_lsm_v(1)%usws,             &
+                                               surf_usm_v(1)%usws,             &
+                                               surf_def_v(2)%usws,             &
+                                               surf_lsm_v(2)%usws,             &
+                                               surf_usm_v(2)%usws,             &
+                                               surf_def_v(3)%usws,             &
+                                               surf_lsm_v(3)%usws,             &
+                                               surf_usm_v(3)%usws )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'vsws' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%vsws,        &
+                                               surf_def_h(1)%vsws,             &
+                                               surf_lsm_h%vsws,                &
+                                               surf_usm_h%vsws,                &
+                                               surf_def_v(0)%vsws,             &
+                                               surf_lsm_v(0)%vsws,             &
+                                               surf_usm_v(0)%vsws,             &
+                                               surf_def_v(1)%vsws,             &
+                                               surf_lsm_v(1)%vsws,             &
+                                               surf_usm_v(1)%vsws,             &
+                                               surf_def_v(2)%vsws,             &
+                                               surf_lsm_v(2)%vsws,             &
+                                               surf_usm_v(2)%vsws,             &
+                                               surf_def_v(3)%vsws,             &
+                                               surf_lsm_v(3)%vsws,             &
+                                               surf_usm_v(3)%vsws )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'shf' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%shf,         &
+                                               surf_def_h(1)%shf,              &
+                                               surf_lsm_h%shf,                 &
+                                               surf_usm_h%shf,                 &
+                                               surf_def_v(0)%shf,              &
+                                               surf_lsm_v(0)%shf,              &
+                                               surf_usm_v(0)%shf,              &
+                                               surf_def_v(1)%shf,              &
+                                               surf_lsm_v(1)%shf,              &
+                                               surf_usm_v(1)%shf,              &
+                                               surf_def_v(2)%shf,              &
+                                               surf_lsm_v(2)%shf,              &
+                                               surf_usm_v(2)%shf,              &
+                                               surf_def_v(3)%shf,              &
+                                               surf_lsm_v(3)%shf,              &
+                                               surf_usm_v(3)%shf )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+               ENDIF
+
+            CASE ( 'qsws' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%qsws,        &
+                                               surf_def_h(1)%qsws,             &
+                                               surf_lsm_h%qsws,                &
+                                               surf_usm_h%qsws,                &
+                                               surf_def_v(0)%qsws,             &
+                                               surf_lsm_v(0)%qsws,             &
+                                               surf_usm_v(0)%qsws,             &
+                                               surf_def_v(1)%qsws,             &
+                                               surf_lsm_v(1)%qsws,             &
+                                               surf_usm_v(1)%qsws,             &
+                                               surf_def_v(2)%qsws,             &
+                                               surf_lsm_v(2)%qsws,             &
+                                               surf_usm_v(2)%qsws,             &
+                                               surf_def_v(3)%qsws,             &
+                                               surf_lsm_v(3)%qsws,             &
+                                               surf_usm_v(3)%qsws )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'ssws' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%ssws,        &
+                                               surf_def_h(1)%ssws,             &
+                                               surf_lsm_h%ssws,                &
+                                               surf_usm_h%ssws,                &
+                                               surf_def_v(0)%ssws,             &
+                                               surf_lsm_v(0)%ssws,             &
+                                               surf_usm_v(0)%ssws,             &
+                                               surf_def_v(1)%ssws,             &
+                                               surf_lsm_v(1)%ssws,             &
+                                               surf_usm_v(1)%ssws,             &
+                                               surf_def_v(2)%ssws,             &
+                                               surf_lsm_v(2)%ssws,             &
+                                               surf_usm_v(2)%ssws,             &
+                                               surf_def_v(3)%ssws,             &
+                                               surf_lsm_v(3)%ssws,             &
+                                               surf_usm_v(3)%ssws )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'qcsws' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%qcsws,       &
+                                               surf_def_h(1)%qcsws,            &
+                                               surf_lsm_h%qcsws,               &
+                                               surf_usm_h%qcsws,               &
+                                               surf_def_v(0)%qcsws,            &
+                                               surf_lsm_v(0)%qcsws,            &
+                                               surf_usm_v(0)%qcsws,            &
+                                               surf_def_v(1)%qcsws,            &
+                                               surf_lsm_v(1)%qcsws,            &
+                                               surf_usm_v(1)%qcsws,            &
+                                               surf_def_v(2)%qcsws,            &
+                                               surf_lsm_v(2)%qcsws,            &
+                                               surf_usm_v(2)%qcsws,            &
+                                               surf_def_v(3)%qcsws,            &
+                                               surf_lsm_v(3)%qcsws,            &
+                                               surf_usm_v(3)%qcsws )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'ncsws' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%ncsws,       &
+                                               surf_def_h(1)%ncsws,            &
+                                               surf_lsm_h%ncsws,               &
+                                               surf_usm_h%ncsws,               &
+                                               surf_def_v(0)%ncsws,            &
+                                               surf_lsm_v(0)%ncsws,            &
+                                               surf_usm_v(0)%ncsws,            &
+                                               surf_def_v(1)%ncsws,            &
+                                               surf_lsm_v(1)%ncsws,            &
+                                               surf_usm_v(1)%ncsws,            &
+                                               surf_def_v(2)%ncsws,            &
+                                               surf_lsm_v(2)%ncsws,            &
+                                               surf_usm_v(2)%ncsws,            &
+                                               surf_def_v(3)%ncsws,            &
+                                               surf_lsm_v(3)%ncsws,            &
+                                               surf_usm_v(3)%ncsws )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'qrsws' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%qrsws,       &
+                                               surf_def_h(1)%qrsws,            &
+                                               surf_lsm_h%qrsws,               &
+                                               surf_usm_h%qrsws,               &
+                                               surf_def_v(0)%qrsws,            &
+                                               surf_lsm_v(0)%qrsws,            &
+                                               surf_usm_v(0)%qrsws,            &
+                                               surf_def_v(1)%qrsws,            &
+                                               surf_lsm_v(1)%qrsws,            &
+                                               surf_usm_v(1)%qrsws,            &
+                                               surf_def_v(2)%qrsws,            &
+                                               surf_lsm_v(2)%qrsws,            &
+                                               surf_usm_v(2)%qrsws,            &
+                                               surf_def_v(3)%qrsws,            &
+                                               surf_lsm_v(3)%qrsws,            &
+                                               surf_usm_v(3)%qrsws )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'nrsws' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%nrsws,       &
+                                               surf_def_h(1)%nrsws,            &
+                                               surf_lsm_h%nrsws,               &
+                                               surf_usm_h%nrsws,               &
+                                               surf_def_v(0)%nrsws,            &
+                                               surf_lsm_v(0)%nrsws,            &
+                                               surf_usm_v(0)%nrsws,            &
+                                               surf_def_v(1)%nrsws,            &
+                                               surf_lsm_v(1)%nrsws,            &
+                                               surf_usm_v(1)%nrsws,            &
+                                               surf_def_v(2)%nrsws,            &
+                                               surf_lsm_v(2)%nrsws,            &
+                                               surf_usm_v(2)%nrsws,            &
+                                               surf_def_v(3)%nrsws,            &
+                                               surf_lsm_v(3)%nrsws,            &
+                                               surf_usm_v(3)%nrsws )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'sasws' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%sasws,       &
+                                               surf_def_h(1)%sasws,            &
+                                               surf_lsm_h%sasws,               &
+                                               surf_usm_h%sasws,               &
+                                               surf_def_v(0)%sasws,            &
+                                               surf_lsm_v(0)%sasws,            &
+                                               surf_usm_v(0)%sasws,            &
+                                               surf_def_v(1)%sasws,            &
+                                               surf_lsm_v(1)%sasws,            &
+                                               surf_usm_v(1)%sasws,            &
+                                               surf_def_v(2)%sasws,            &
+                                               surf_lsm_v(2)%sasws,            &
+                                               surf_usm_v(2)%sasws,            &
+                                               surf_def_v(3)%sasws,            &
+                                               surf_lsm_v(3)%sasws,            &
+                                               surf_usm_v(3)%sasws )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'q_surface' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%q_surface,   &
+                                               surf_def_h(1)%q_surface,        &
+                                               surf_lsm_h%q_surface,           &
+                                               surf_usm_h%q_surface,           &
+                                               surf_def_v(0)%q_surface,        &
+                                               surf_lsm_v(0)%q_surface,        &
+                                               surf_usm_v(0)%q_surface,        &
+                                               surf_def_v(1)%q_surface,        &
+                                               surf_lsm_v(1)%q_surface,        &
+                                               surf_usm_v(1)%q_surface,        &
+                                               surf_def_v(2)%q_surface,        &
+                                               surf_lsm_v(2)%q_surface,        &
+                                               surf_usm_v(2)%q_surface,        &
+                                               surf_def_v(3)%q_surface,        &
+                                               surf_lsm_v(3)%q_surface,        &
+                                               surf_usm_v(3)%q_surface )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'theta_surface' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%pt_surface,  &
+                                               surf_def_h(1)%pt_surface,       &
+                                               surf_lsm_h%pt_surface,          &
+                                               surf_usm_h%pt_surface,          &
+                                               surf_def_v(0)%pt_surface,       &
+                                               surf_lsm_v(0)%pt_surface,       &
+                                               surf_usm_v(0)%pt_surface,       &
+                                               surf_def_v(1)%pt_surface,       &
+                                               surf_lsm_v(1)%pt_surface,       &
+                                               surf_usm_v(1)%pt_surface,       &
+                                               surf_def_v(2)%pt_surface,       &
+                                               surf_lsm_v(2)%pt_surface,       &
+                                               surf_usm_v(2)%pt_surface,       &
+                                               surf_def_v(3)%pt_surface,       &
+                                               surf_lsm_v(3)%pt_surface,       &
+                                               surf_usm_v(3)%pt_surface )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'thetav_surface' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%vpt_surface, &
+                                               surf_def_h(1)%vpt_surface,      &
+                                               surf_lsm_h%vpt_surface,         &
+                                               surf_usm_h%vpt_surface,         &
+                                               surf_def_v(0)%vpt_surface,      &
+                                               surf_lsm_v(0)%vpt_surface,      &
+                                               surf_usm_v(0)%vpt_surface,      &
+                                               surf_def_v(1)%vpt_surface,      &
+                                               surf_lsm_v(1)%vpt_surface,      &
+                                               surf_usm_v(1)%vpt_surface,      &
+                                               surf_def_v(2)%vpt_surface,      &
+                                               surf_lsm_v(2)%vpt_surface,      &
+                                               surf_usm_v(2)%vpt_surface,      &
+                                               surf_def_v(3)%vpt_surface,      &
+                                               surf_lsm_v(3)%vpt_surface,      &
+                                               surf_usm_v(3)%vpt_surface)
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'rad_net' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%rad_net,     &
+                                               surf_def_h(1)%rad_net,          &
+                                               surf_lsm_h%rad_net,             &
+                                               surf_usm_h%rad_net,             &
+                                               surf_def_v(0)%rad_net,          &
+                                               surf_lsm_v(0)%rad_net,          &
+                                               surf_usm_v(0)%rad_net,          &
+                                               surf_def_v(1)%rad_net,          &
+                                               surf_lsm_v(1)%rad_net,          &
+                                               surf_usm_v(1)%rad_net,          &
+                                               surf_def_v(2)%rad_net,          &
+                                               surf_lsm_v(2)%rad_net,          &
+                                               surf_usm_v(2)%rad_net,          &
+                                               surf_def_v(3)%rad_net,          &
+                                               surf_lsm_v(3)%rad_net,          &
+                                               surf_usm_v(3)%rad_net ) 
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'rad_lw_in' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%rad_lw_in,   &
+                                               surf_def_h(1)%rad_lw_in,        &
+                                               surf_lsm_h%rad_lw_in,           &
+                                               surf_usm_h%rad_lw_in,           &
+                                               surf_def_v(0)%rad_lw_in,        &
+                                               surf_lsm_v(0)%rad_lw_in,        &
+                                               surf_usm_v(0)%rad_lw_in,        &
+                                               surf_def_v(1)%rad_lw_in,        &
+                                               surf_lsm_v(1)%rad_lw_in,        &
+                                               surf_usm_v(1)%rad_lw_in,        &
+                                               surf_def_v(2)%rad_lw_in,        &
+                                               surf_lsm_v(2)%rad_lw_in,        &
+                                               surf_usm_v(2)%rad_lw_in,        &
+                                               surf_def_v(3)%rad_lw_in,        &
+                                               surf_lsm_v(3)%rad_lw_in,        &
+                                               surf_usm_v(3)%rad_lw_in )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'rad_lw_out' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%rad_lw_out,  &
+                                               surf_def_h(1)%rad_lw_out,       &
+                                               surf_lsm_h%rad_lw_out,          &
+                                               surf_usm_h%rad_lw_out,          &
+                                               surf_def_v(0)%rad_lw_out,       &
+                                               surf_lsm_v(0)%rad_lw_out,       &
+                                               surf_usm_v(0)%rad_lw_out,       &
+                                               surf_def_v(1)%rad_lw_out,       &
+                                               surf_lsm_v(1)%rad_lw_out,       &
+                                               surf_usm_v(1)%rad_lw_out,       &
+                                               surf_def_v(2)%rad_lw_out,       &
+                                               surf_lsm_v(2)%rad_lw_out,       &
+                                               surf_usm_v(2)%rad_lw_out,       &
+                                               surf_def_v(3)%rad_lw_out,       &
+                                               surf_lsm_v(3)%rad_lw_out,       &
+                                               surf_usm_v(3)%rad_lw_out )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'rad_sw_in' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%rad_sw_in,   &
+                                               surf_def_h(1)%rad_sw_in,        &
+                                               surf_lsm_h%rad_sw_in,           &
+                                               surf_usm_h%rad_sw_in,           &
+                                               surf_def_v(0)%rad_sw_in,        &
+                                               surf_lsm_v(0)%rad_sw_in,        &
+                                               surf_usm_v(0)%rad_sw_in,        &
+                                               surf_def_v(1)%rad_sw_in,        &
+                                               surf_lsm_v(1)%rad_sw_in,        &
+                                               surf_usm_v(1)%rad_sw_in,        &
+                                               surf_def_v(2)%rad_sw_in,        &
+                                               surf_lsm_v(2)%rad_sw_in,        &
+                                               surf_usm_v(2)%rad_sw_in,        &
+                                               surf_def_v(3)%rad_sw_in,        &
+                                               surf_lsm_v(3)%rad_sw_in,        &
+                                               surf_usm_v(3)%rad_sw_in )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'rad_sw_out' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%rad_sw_out,  &
+                                               surf_def_h(1)%rad_sw_out,       &
+                                               surf_lsm_h%rad_sw_out,          &
+                                               surf_usm_h%rad_sw_out,          &
+                                               surf_def_v(0)%rad_sw_out,       &
+                                               surf_lsm_v(0)%rad_sw_out,       &
+                                               surf_usm_v(0)%rad_sw_out,       &
+                                               surf_def_v(1)%rad_sw_out,       &
+                                               surf_lsm_v(1)%rad_sw_out,       &
+                                               surf_usm_v(1)%rad_sw_out,       &
+                                               surf_def_v(2)%rad_sw_out,       &
+                                               surf_lsm_v(2)%rad_sw_out,       &
+                                               surf_usm_v(2)%rad_sw_out,       &
+                                               surf_def_v(3)%rad_sw_out,       &
+                                               surf_lsm_v(3)%rad_sw_out,       &
+                                               surf_usm_v(3)%rad_sw_out )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'ghf' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%ghf,         &
+                                               surf_def_h(1)%ghf,              &
+                                               surf_lsm_h%ghf,                 &
+                                               surf_usm_h%ghf,                 &
+                                               surf_def_v(0)%ghf,              &
+                                               surf_lsm_v(0)%ghf,              &
+                                               surf_usm_v(0)%ghf,              &
+                                               surf_def_v(1)%ghf,              &
+                                               surf_lsm_v(1)%ghf,              &
+                                               surf_usm_v(1)%ghf,              &
+                                               surf_def_v(2)%ghf,              &
+                                               surf_lsm_v(2)%ghf,              &
+                                               surf_usm_v(2)%ghf,              &
+                                               surf_def_v(3)%ghf,              &
+                                               surf_lsm_v(3)%ghf,              &
+                                               surf_usm_v(3)%ghf )      
+                                                                        ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+                      
+            CASE ( 'r_a' )                                                     
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%r_a,         &
+                                               surf_def_h(1)%r_a,              &
+                                               surf_lsm_h%r_a,                 &
+                                               surf_usm_h%r_a,                 &
+                                               surf_def_v(0)%r_a,              &
+                                               surf_lsm_v(0)%r_a,              &
+                                               surf_usm_v(0)%r_a,              &
+                                               surf_def_v(1)%r_a,              &
+                                               surf_lsm_v(1)%r_a,              &
+                                               surf_usm_v(1)%r_a,              &
+                                               surf_def_v(2)%r_a,              &
+                                               surf_lsm_v(2)%r_a,              &
+                                               surf_usm_v(2)%r_a,              &
+                                               surf_def_v(3)%r_a,              &
+                                               surf_lsm_v(3)%r_a,              &
+                                               surf_usm_v(3)%r_a )      
+                                                                      ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+                        
+            CASE ( 'r_soil' )                                                  
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%r_soil,      &
+                                               surf_def_h(1)%r_soil,           &
+                                               surf_lsm_h%r_soil,              &
+                                               surf_usm_h%r_soil,              &
+                                               surf_def_v(0)%r_soil,           &
+                                               surf_lsm_v(0)%r_soil,           &
+                                               surf_usm_v(0)%r_soil,           &
+                                               surf_def_v(1)%r_soil,           &
+                                               surf_lsm_v(1)%r_soil,           &
+                                               surf_usm_v(1)%r_soil,           &
+                                               surf_def_v(2)%r_soil,           &
+                                               surf_lsm_v(2)%r_soil,           &
+                                               surf_usm_v(2)%r_soil,           &
+                                               surf_def_v(3)%r_soil,           &
+                                               surf_lsm_v(3)%r_soil,           &
+                                               surf_usm_v(3)%r_soil ) 
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'r_canopy' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%r_canopy,    &
+                                               surf_def_h(1)%r_canopy,         &
+                                               surf_lsm_h%r_canopy,            &
+                                               surf_usm_h%r_canopy,            &
+                                               surf_def_v(0)%r_canopy,         &
+                                               surf_lsm_v(0)%r_canopy,         &
+                                               surf_usm_v(0)%r_canopy,         &
+                                               surf_def_v(1)%r_canopy,         &
+                                               surf_lsm_v(1)%r_canopy,         &
+                                               surf_usm_v(1)%r_canopy,         &
+                                               surf_def_v(2)%r_canopy,         &
+                                               surf_lsm_v(2)%r_canopy,         &
+                                               surf_usm_v(2)%r_canopy,         &
+                                               surf_def_v(3)%r_canopy,         &
+                                               surf_lsm_v(3)%r_canopy,         &
+                                               surf_usm_v(3)%r_canopy ) 
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'r_s' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%r_s,         &
+                                               surf_def_h(1)%r_s,              &
+                                               surf_lsm_h%r_s,                 &
+                                               surf_usm_h%r_s,                 &
+                                               surf_def_v(0)%r_s,              &
+                                               surf_lsm_v(0)%r_s,              &
+                                               surf_usm_v(0)%r_s,              &
+                                               surf_def_v(1)%r_s,              &
+                                               surf_lsm_v(1)%r_s,              &
+                                               surf_usm_v(1)%r_s,              &
+                                               surf_def_v(2)%r_s,              &
+                                               surf_lsm_v(2)%r_s,              &
+                                               surf_usm_v(2)%r_s,              &
+                                               surf_def_v(3)%r_s,              &
+                                               surf_lsm_v(3)%r_s,              &
+                                               surf_usm_v(3)%r_s ) 
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'rad_sw_dir' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%rad_sw_dir,  &
+                                               surf_def_h(1)%rad_sw_dir,       &
+                                               surf_lsm_h%rad_sw_dir,          &
+                                               surf_usm_h%rad_sw_dir,          &
+                                               surf_def_v(0)%rad_sw_dir,       &
+                                               surf_lsm_v(0)%rad_sw_dir,       &
+                                               surf_usm_v(0)%rad_sw_dir,       &
+                                               surf_def_v(1)%rad_sw_dir,       &
+                                               surf_lsm_v(1)%rad_sw_dir,       &
+                                               surf_usm_v(1)%rad_sw_dir,       &
+                                               surf_def_v(2)%rad_sw_dir,       &
+                                               surf_lsm_v(2)%rad_sw_dir,       &
+                                               surf_usm_v(2)%rad_sw_dir,       &
+                                               surf_def_v(3)%rad_sw_dir,       &
+                                               surf_lsm_v(3)%rad_sw_dir,       &
+                                               surf_usm_v(3)%rad_sw_dir )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'rad_sw_dif' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%rad_sw_dif,  &
+                                               surf_def_h(1)%rad_sw_dif,       &
+                                               surf_lsm_h%rad_sw_dif,          &
+                                               surf_usm_h%rad_sw_dif,          &
+                                               surf_def_v(0)%rad_sw_dif,       &
+                                               surf_lsm_v(0)%rad_sw_dif,       &
+                                               surf_usm_v(0)%rad_sw_dif,       &
+                                               surf_def_v(1)%rad_sw_dif,       &
+                                               surf_lsm_v(1)%rad_sw_dif,       &
+                                               surf_usm_v(1)%rad_sw_dif,       &
+                                               surf_def_v(2)%rad_sw_dif,       &
+                                               surf_lsm_v(2)%rad_sw_dif,       &
+                                               surf_usm_v(2)%rad_sw_dif,       &
+                                               surf_def_v(3)%rad_sw_dif,       &
+                                               surf_lsm_v(3)%rad_sw_dif,       &
+                                               surf_usm_v(3)%rad_sw_dif )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'rad_sw_ref' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%rad_sw_ref,  &
+                                               surf_def_h(1)%rad_sw_ref,       &
+                                               surf_lsm_h%rad_sw_ref,          &
+                                               surf_usm_h%rad_sw_ref,          &
+                                               surf_def_v(0)%rad_sw_ref,       &
+                                               surf_lsm_v(0)%rad_sw_ref,       &
+                                               surf_usm_v(0)%rad_sw_ref,       &
+                                               surf_def_v(1)%rad_sw_ref,       &
+                                               surf_lsm_v(1)%rad_sw_ref,       &
+                                               surf_usm_v(1)%rad_sw_ref,       &
+                                               surf_def_v(2)%rad_sw_ref,       &
+                                               surf_lsm_v(2)%rad_sw_ref,       &
+                                               surf_usm_v(2)%rad_sw_ref,       &
+                                               surf_def_v(3)%rad_sw_ref,       &
+                                               surf_lsm_v(3)%rad_sw_ref,       &
+                                               surf_usm_v(3)%rad_sw_ref )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'rad_sw_res' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%rad_sw_res,  &
+                                               surf_def_h(1)%rad_sw_res,       &
+                                               surf_lsm_h%rad_sw_res,          &
+                                               surf_usm_h%rad_sw_res,          &
+                                               surf_def_v(0)%rad_sw_res,       &
+                                               surf_lsm_v(0)%rad_sw_res,       &
+                                               surf_usm_v(0)%rad_sw_res,       &
+                                               surf_def_v(1)%rad_sw_res,       &
+                                               surf_lsm_v(1)%rad_sw_res,       &
+                                               surf_usm_v(1)%rad_sw_res,       &
+                                               surf_def_v(2)%rad_sw_res,       &
+                                               surf_lsm_v(2)%rad_sw_res,       &
+                                               surf_usm_v(2)%rad_sw_res,       &
+                                               surf_def_v(3)%rad_sw_res,       &
+                                               surf_lsm_v(3)%rad_sw_res,       &
+                                               surf_usm_v(3)%rad_sw_res )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'rad_lw_dif' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%rad_lw_dif,  &
+                                               surf_def_h(1)%rad_lw_dif,       &
+                                               surf_lsm_h%rad_lw_dif,          &
+                                               surf_usm_h%rad_lw_dif,          &
+                                               surf_def_v(0)%rad_lw_dif,       &
+                                               surf_lsm_v(0)%rad_lw_dif,       &
+                                               surf_usm_v(0)%rad_lw_dif,       &
+                                               surf_def_v(1)%rad_lw_dif,       &
+                                               surf_lsm_v(1)%rad_lw_dif,       &
+                                               surf_usm_v(1)%rad_lw_dif,       &
+                                               surf_def_v(2)%rad_lw_dif,       &
+                                               surf_lsm_v(2)%rad_lw_dif,       &
+                                               surf_usm_v(2)%rad_lw_dif,       &
+                                               surf_def_v(3)%rad_lw_dif,       &
+                                               surf_lsm_v(3)%rad_lw_dif,       &
+                                               surf_usm_v(3)%rad_lw_dif )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'rad_lw_ref' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%rad_lw_ref,  &
+                                               surf_def_h(1)%rad_lw_ref,       &
+                                               surf_lsm_h%rad_lw_ref,          &
+                                               surf_usm_h%rad_lw_ref,          &
+                                               surf_def_v(0)%rad_lw_ref,       &
+                                               surf_lsm_v(0)%rad_lw_ref,       &
+                                               surf_usm_v(0)%rad_lw_ref,       &
+                                               surf_def_v(1)%rad_lw_ref,       &
+                                               surf_lsm_v(1)%rad_lw_ref,       &
+                                               surf_usm_v(1)%rad_lw_ref,       &
+                                               surf_def_v(2)%rad_lw_ref,       &
+                                               surf_lsm_v(2)%rad_lw_ref,       &
+                                               surf_usm_v(2)%rad_lw_ref,       &
+                                               surf_def_v(3)%rad_lw_ref,       &
+                                               surf_lsm_v(3)%rad_lw_ref,       &
+                                               surf_usm_v(3)%rad_lw_ref )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+            CASE ( 'rad_lw_res' )
+!
+!--            Output of instantaneous data
+               IF ( av == 0 )  THEN
+                  CALL surface_data_output_collect( surf_def_h(0)%rad_lw_res,  &
+                                               surf_def_h(1)%rad_lw_res,       &
+                                               surf_lsm_h%rad_lw_res,          &
+                                               surf_usm_h%rad_lw_res,          &
+                                               surf_def_v(0)%rad_lw_res,       &
+                                               surf_lsm_v(0)%rad_lw_res,       &
+                                               surf_usm_v(0)%rad_lw_res,       &
+                                               surf_def_v(1)%rad_lw_res,       &
+                                               surf_lsm_v(1)%rad_lw_res,       &
+                                               surf_usm_v(1)%rad_lw_res,       &
+                                               surf_def_v(2)%rad_lw_res,       &
+                                               surf_lsm_v(2)%rad_lw_res,       &
+                                               surf_usm_v(2)%rad_lw_res,       &
+                                               surf_def_v(3)%rad_lw_res,       &
+                                               surf_lsm_v(3)%rad_lw_res,       &
+                                               surf_usm_v(3)%rad_lw_res )
+               ELSE
+!
+!--               Output of averaged data
+                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
+                                        REAL( average_count_surf, KIND=wp )
+                  surfaces%var_av(:,n_out) = 0.0_wp
+                                                        
+               ENDIF
+
+!
+!--            Add further variables:
+!--            'css', 'cssws', 'qsws_liq', 'qsws_soil', 'qsws_veg'
+
+         END SELECT
+!
+!--      Write to binary file: 
+!--      - surfaces%points ( 3, 1-npoints )
+!--      - surfaces%polygons ( 5, 1-ns )
+!--      - surfaces%var_out ( 1-ns, time )
+!--      - Dimension: 1-nsurfaces, 1-npoints - can be ordered consecutively
+!--      - Distinguish between average and non-average data
+         DO  i = 0, io_blocks-1
+            IF ( i == io_group )  THEN
+               WRITE ( 25+av )  LEN_TRIM( 'time' )
+               WRITE ( 25+av )  'time'
+               WRITE ( 25+av )  time_since_reference_point
+               WRITE ( 25+av )  LEN_TRIM( trimvar )
+               WRITE ( 25+av )  TRIM( trimvar )
+               WRITE ( 25+av )  surfaces%var_out
+            ENDIF
+#if defined( __parallel )
+            CALL MPI_BARRIER( comm2d, ierr )
+#endif
+         ENDDO
+
+      ENDDO
+      
+!
+!--   If averaged output was written to NetCDF file, set the counter to zero
+      IF ( av == 1 )  average_count_surf = 0
+             
+   END SUBROUTINE surface_data_output
+   
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Routine for controlling the data averaging. 
+!------------------------------------------------------------------------------!     
+   SUBROUTINE surface_data_output_averaging
+   
+      IMPLICIT NONE
+      
+      CHARACTER(LEN=100) ::  trimvar !< dummy variable for current output variable
+       
+      INTEGER(iwp) ::  n_out  !< counter variables for surface output
+      
+      n_out = 0
+      DO  WHILE ( dosurf(1,n_out+1)(1:1) /= ' ' )
+         
+         n_out   = n_out + 1
+         trimvar = TRIM( dosurf(1,n_out) )
+
+         SELECT CASE ( trimvar )
+
+            CASE ( 'us' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%us,              &
+                                           surf_def_h(1)%us,                   &
+                                           surf_lsm_h%us,                      &
+                                           surf_usm_h%us,                      &
+                                           surf_def_v(0)%us,                   &
+                                           surf_lsm_v(0)%us,                   &
+                                           surf_usm_v(0)%us,                   &
+                                           surf_def_v(1)%us,                   &
+                                           surf_lsm_v(1)%us,                   &
+                                           surf_usm_v(1)%us,                   &
+                                           surf_def_v(2)%us,                   &
+                                           surf_lsm_v(2)%us,                   &
+                                           surf_usm_v(2)%us,                   &
+                                           surf_def_v(3)%us,                   &
+                                           surf_lsm_v(3)%us,                   &
+                                           surf_usm_v(3)%us, n_out ) 
+
+            CASE ( 'ts' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%ts,              &
+                                           surf_def_h(1)%ts,                   &
+                                           surf_lsm_h%ts,                      &
+                                           surf_usm_h%ts,                      &
+                                           surf_def_v(0)%ts,                   &
+                                           surf_lsm_v(0)%ts,                   &
+                                           surf_usm_v(0)%ts,                   &
+                                           surf_def_v(1)%ts,                   &
+                                           surf_lsm_v(1)%ts,                   &
+                                           surf_usm_v(1)%ts,                   &
+                                           surf_def_v(2)%ts,                   &
+                                           surf_lsm_v(2)%ts,                   &
+                                           surf_usm_v(2)%ts,                   &
+                                           surf_def_v(3)%ts,                   &
+                                           surf_lsm_v(3)%ts,                   &
+                                           surf_usm_v(3)%ts, n_out ) 
+
+            CASE ( 'qs' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%qs,              &
+                                           surf_def_h(1)%qs,                   &
+                                           surf_lsm_h%qs,                      &
+                                           surf_usm_h%qs,                      &
+                                           surf_def_v(0)%qs,                   &
+                                           surf_lsm_v(0)%qs,                   &
+                                           surf_usm_v(0)%qs,                   &
+                                           surf_def_v(1)%qs,                   &
+                                           surf_lsm_v(1)%qs,                   &
+                                           surf_usm_v(1)%qs,                   &
+                                           surf_def_v(2)%qs,                   &
+                                           surf_lsm_v(2)%qs,                   &
+                                           surf_usm_v(2)%qs,                   &
+                                           surf_def_v(3)%qs,                   &
+                                           surf_lsm_v(3)%qs,                   &
+                                           surf_usm_v(3)%qs, n_out ) 
+
+            CASE ( 'ss' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%ss,              &
+                                           surf_def_h(1)%ss,                   &
+                                           surf_lsm_h%ss,                      &
+                                           surf_usm_h%ss,                      &
+                                           surf_def_v(0)%ss,                   &
+                                           surf_lsm_v(0)%ss,                   &
+                                           surf_usm_v(0)%ss,                   &
+                                           surf_def_v(1)%ss,                   &
+                                           surf_lsm_v(1)%ss,                   &
+                                           surf_usm_v(1)%ss,                   &
+                                           surf_def_v(2)%ss,                   &
+                                           surf_lsm_v(2)%ss,                   &
+                                           surf_usm_v(2)%ss,                   &
+                                           surf_def_v(3)%ss,                   &
+                                           surf_lsm_v(3)%ss,                   &
+                                           surf_usm_v(3)%ss, n_out ) 
+
+            CASE ( 'qcs' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%qcs,             &
+                                           surf_def_h(1)%qcs,                  &
+                                           surf_lsm_h%qcs,                     &
+                                           surf_usm_h%qcs,                     &
+                                           surf_def_v(0)%qcs,                  &
+                                           surf_lsm_v(0)%qcs,                  &
+                                           surf_usm_v(0)%qcs,                  &
+                                           surf_def_v(1)%qcs,                  &
+                                           surf_lsm_v(1)%qcs,                  &
+                                           surf_usm_v(1)%qcs,                  &
+                                           surf_def_v(2)%qcs,                  &
+                                           surf_lsm_v(2)%qcs,                  &
+                                           surf_usm_v(2)%qcs,                  &
+                                           surf_def_v(3)%qcs,                  &
+                                           surf_lsm_v(3)%qcs,                  &
+                                           surf_usm_v(3)%qcs, n_out ) 
+
+            CASE ( 'ncs' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%ncs,             &
+                                           surf_def_h(1)%ncs,                  &
+                                           surf_lsm_h%ncs,                     &
+                                           surf_usm_h%ncs,                     &
+                                           surf_def_v(0)%ncs,                  &
+                                           surf_lsm_v(0)%ncs,                  &
+                                           surf_usm_v(0)%ncs,                  &
+                                           surf_def_v(1)%ncs,                  &
+                                           surf_lsm_v(1)%ncs,                  &
+                                           surf_usm_v(1)%ncs,                  &
+                                           surf_def_v(2)%ncs,                  &
+                                           surf_lsm_v(2)%ncs,                  &
+                                           surf_usm_v(2)%ncs,                  &
+                                           surf_def_v(3)%ncs,                  &
+                                           surf_lsm_v(3)%ncs,                  &
+                                           surf_usm_v(3)%ncs, n_out ) 
+
+            CASE ( 'qrs' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%qrs,             &
+                                           surf_def_h(1)%qrs,                  &
+                                           surf_lsm_h%qrs,                     &
+                                           surf_usm_h%qrs,                     &
+                                           surf_def_v(0)%qrs,                  &
+                                           surf_lsm_v(0)%qrs,                  &
+                                           surf_usm_v(0)%qrs,                  &
+                                           surf_def_v(1)%qrs,                  &
+                                           surf_lsm_v(1)%qrs,                  &
+                                           surf_usm_v(1)%qrs,                  &
+                                           surf_def_v(2)%qrs,                  &
+                                           surf_lsm_v(2)%qrs,                  &
+                                           surf_usm_v(2)%qrs,                  &
+                                           surf_def_v(3)%qrs,                  &
+                                           surf_lsm_v(3)%qrs,                  &
+                                           surf_usm_v(3)%qrs, n_out ) 
+
+            CASE ( 'nrs' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%nrs,             &
+                                           surf_def_h(1)%nrs,                  &
+                                           surf_lsm_h%nrs,                     &
+                                           surf_usm_h%nrs,                     &
+                                           surf_def_v(0)%nrs,                  &
+                                           surf_lsm_v(0)%nrs,                  &
+                                           surf_usm_v(0)%nrs,                  &
+                                           surf_def_v(1)%nrs,                  &
+                                           surf_lsm_v(1)%nrs,                  &
+                                           surf_usm_v(1)%nrs,                  &
+                                           surf_def_v(2)%nrs,                  &
+                                           surf_lsm_v(2)%nrs,                  &
+                                           surf_usm_v(2)%nrs,                  &
+                                           surf_def_v(3)%nrs,                  &
+                                           surf_lsm_v(3)%nrs,                  &
+                                           surf_usm_v(3)%nrs, n_out ) 
+
+            CASE ( 'ol' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%ol,              &
+                                           surf_def_h(1)%ol,                   &
+                                           surf_lsm_h%ol,                      &
+                                           surf_usm_h%ol,                      &
+                                           surf_def_v(0)%ol,                   &
+                                           surf_lsm_v(0)%ol,                   &
+                                           surf_usm_v(0)%ol,                   &
+                                           surf_def_v(1)%ol,                   &
+                                           surf_lsm_v(1)%ol,                   &
+                                           surf_usm_v(1)%ol,                   &
+                                           surf_def_v(2)%ol,                   &
+                                           surf_lsm_v(2)%ol,                   &
+                                           surf_usm_v(2)%ol,                   &
+                                           surf_def_v(3)%ol,                   &
+                                           surf_lsm_v(3)%ol,                   &
+                                           surf_usm_v(3)%ol, n_out ) 
+
+            CASE ( 'z0' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%z0,              &
+                                           surf_def_h(1)%z0,                   &
+                                           surf_lsm_h%z0,                      &
+                                           surf_usm_h%z0,                      &
+                                           surf_def_v(0)%z0,                   &
+                                           surf_lsm_v(0)%z0,                   &
+                                           surf_usm_v(0)%z0,                   &
+                                           surf_def_v(1)%z0,                   &
+                                           surf_lsm_v(1)%z0,                   &
+                                           surf_usm_v(1)%z0,                   &
+                                           surf_def_v(2)%z0,                   &
+                                           surf_lsm_v(2)%z0,                   &
+                                           surf_usm_v(2)%z0,                   &
+                                           surf_def_v(3)%z0,                   &
+                                           surf_lsm_v(3)%z0,                   &
+                                           surf_usm_v(3)%z0, n_out ) 
+
+            CASE ( 'z0h' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%z0h,             &
+                                           surf_def_h(1)%z0h,                  &
+                                           surf_lsm_h%z0h,                     &
+                                           surf_usm_h%z0h,                     &
+                                           surf_def_v(0)%z0h,                  &
+                                           surf_lsm_v(0)%z0h,                  &
+                                           surf_usm_v(0)%z0h,                  &
+                                           surf_def_v(1)%z0h,                  &
+                                           surf_lsm_v(1)%z0h,                  &
+                                           surf_usm_v(1)%z0h,                  &
+                                           surf_def_v(2)%z0h,                  &
+                                           surf_lsm_v(2)%z0h,                  &
+                                           surf_usm_v(2)%z0h,                  &
+                                           surf_def_v(3)%z0h,                  &
+                                           surf_lsm_v(3)%z0h,                  &
+                                           surf_usm_v(3)%z0h, n_out )          
+
+            CASE ( 'z0q' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%z0q,             &
+                                           surf_def_h(1)%z0q,                  &
+                                           surf_lsm_h%z0q,                     &
+                                           surf_usm_h%z0q,                     &
+                                           surf_def_v(0)%z0q,                  &
+                                           surf_lsm_v(0)%z0q,                  &
+                                           surf_usm_v(0)%z0q,                  &
+                                           surf_def_v(1)%z0q,                  &
+                                           surf_lsm_v(1)%z0q,                  &
+                                           surf_usm_v(1)%z0q,                  &
+                                           surf_def_v(2)%z0q,                  &
+                                           surf_lsm_v(2)%z0q,                  &
+                                           surf_usm_v(2)%z0q,                  &
+                                           surf_def_v(3)%z0q,                  &
+                                           surf_lsm_v(3)%z0q,                  &
+                                           surf_usm_v(3)%z0q, n_out ) 
+
+            CASE ( 'theta1' )                                                  
+               CALL surface_data_output_sum_up( surf_def_h(0)%pt1,             &
+                                           surf_def_h(1)%pt1,                  &
+                                           surf_lsm_h%pt1,                     &
+                                           surf_usm_h%pt1,                     &
+                                           surf_def_v(0)%pt1,                  &
+                                           surf_lsm_v(0)%pt1,                  &
+                                           surf_usm_v(0)%pt1,                  &
+                                           surf_def_v(1)%pt1,                  &
+                                           surf_lsm_v(1)%pt1,                  &
+                                           surf_usm_v(1)%pt1,                  &
+                                           surf_def_v(2)%pt1,                  &
+                                           surf_lsm_v(2)%pt1,                  &
+                                           surf_usm_v(2)%pt1,                  &
+                                           surf_def_v(3)%pt1,                  &
+                                           surf_lsm_v(3)%pt1,                  &
+                                           surf_usm_v(3)%pt1, n_out )          
+                                                                               
+            CASE ( 'qv1' )                                                     
+               CALL surface_data_output_sum_up( surf_def_h(0)%qv1,             &
+                                           surf_def_h(1)%qv1,                  &
+                                           surf_lsm_h%qv1,                     &
+                                           surf_usm_h%qv1,                     &
+                                           surf_def_v(0)%qv1,                  &
+                                           surf_lsm_v(0)%qv1,                  &
+                                           surf_usm_v(0)%qv1,                  &
+                                           surf_def_v(1)%qv1,                  &
+                                           surf_lsm_v(1)%qv1,                  &
+                                           surf_usm_v(1)%qv1,                  &
+                                           surf_def_v(2)%qv1,                  &
+                                           surf_lsm_v(2)%qv1,                  &
+                                           surf_usm_v(2)%qv1,                  &
+                                           surf_def_v(3)%qv1,                  &
+                                           surf_lsm_v(3)%qv1,                  &
+                                           surf_usm_v(3)%qv1, n_out ) 
+
+            CASE ( 'thetav1' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%vpt1,            &
+                                           surf_def_h(1)%vpt1,                 &
+                                           surf_lsm_h%vpt1,                    &
+                                           surf_usm_h%vpt1,                    &
+                                           surf_def_v(0)%vpt1,                 &
+                                           surf_lsm_v(0)%vpt1,                 &
+                                           surf_usm_v(0)%vpt1,                 &
+                                           surf_def_v(1)%vpt1,                 &
+                                           surf_lsm_v(1)%vpt1,                 &
+                                           surf_usm_v(1)%vpt1,                 &
+                                           surf_def_v(2)%vpt1,                 &
+                                           surf_lsm_v(2)%vpt1,                 &
+                                           surf_usm_v(2)%vpt1,                 &
+                                           surf_def_v(3)%vpt1,                 &
+                                           surf_lsm_v(3)%vpt1,                 &
+                                           surf_usm_v(3)%vpt1, n_out ) 
+
+            CASE ( 'usws' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%usws,            &
+                                           surf_def_h(1)%usws,                 &
+                                           surf_lsm_h%usws,                    &
+                                           surf_usm_h%usws,                    &
+                                           surf_def_v(0)%usws,                 &
+                                           surf_lsm_v(0)%usws,                 &
+                                           surf_usm_v(0)%usws,                 &
+                                           surf_def_v(1)%usws,                 &
+                                           surf_lsm_v(1)%usws,                 &
+                                           surf_usm_v(1)%usws,                 &
+                                           surf_def_v(2)%usws,                 &
+                                           surf_lsm_v(2)%usws,                 &
+                                           surf_usm_v(2)%usws,                 &
+                                           surf_def_v(3)%usws,                 &
+                                           surf_lsm_v(3)%usws,                 &
+                                           surf_usm_v(3)%usws, n_out ) 
+
+            CASE ( 'vsws' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%vsws,            &
+                                           surf_def_h(1)%vsws,                 &
+                                           surf_lsm_h%vsws,                    &
+                                           surf_usm_h%vsws,                    &
+                                           surf_def_v(0)%vsws,                 &
+                                           surf_lsm_v(0)%vsws,                 &
+                                           surf_usm_v(0)%vsws,                 &
+                                           surf_def_v(1)%vsws,                 &
+                                           surf_lsm_v(1)%vsws,                 &
+                                           surf_usm_v(1)%vsws,                 &
+                                           surf_def_v(2)%vsws,                 &
+                                           surf_lsm_v(2)%vsws,                 &
+                                           surf_usm_v(2)%vsws,                 &
+                                           surf_def_v(3)%vsws,                 &
+                                           surf_lsm_v(3)%vsws,                 &
+                                           surf_usm_v(3)%vsws, n_out ) 
+
+            CASE ( 'shf' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%shf,             &
+                                           surf_def_h(1)%shf,                  &
+                                           surf_lsm_h%shf,                     &
+                                           surf_usm_h%shf,                     &  
+                                           surf_def_v(0)%shf,                  &
+                                           surf_lsm_v(0)%shf,                  &
+                                           surf_usm_v(0)%shf,                  &
+                                           surf_def_v(1)%shf,                  &
+                                           surf_lsm_v(1)%shf,                  &
+                                           surf_usm_v(1)%shf,                  &
+                                           surf_def_v(2)%shf,                  &
+                                           surf_lsm_v(2)%shf,                  &
+                                           surf_usm_v(2)%shf,                  &
+                                           surf_def_v(3)%shf,                  &
+                                           surf_lsm_v(3)%shf,                  &
+                                           surf_usm_v(3)%shf, n_out )
+
+            CASE ( 'qsws' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%qsws,            &
+                                           surf_def_h(1)%qsws,                 &
+                                           surf_lsm_h%qsws,                    &
+                                           surf_usm_h%qsws,                    &
+                                           surf_def_v(0)%qsws,                 &
+                                           surf_lsm_v(0)%qsws,                 &
+                                           surf_usm_v(0)%qsws,                 &
+                                           surf_def_v(1)%qsws,                 &
+                                           surf_lsm_v(1)%qsws,                 &
+                                           surf_usm_v(1)%qsws,                 &
+                                           surf_def_v(2)%qsws,                 &
+                                           surf_lsm_v(2)%qsws,                 &
+                                           surf_usm_v(2)%qsws,                 &
+                                           surf_def_v(3)%qsws,                 &
+                                           surf_lsm_v(3)%qsws,                 &
+                                           surf_usm_v(3)%qsws, n_out ) 
+
+            CASE ( 'ssws' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%ssws,            &
+                                           surf_def_h(1)%ssws,                 &
+                                           surf_lsm_h%ssws,                    &
+                                           surf_usm_h%ssws,                    &
+                                           surf_def_v(0)%ssws,                 &
+                                           surf_lsm_v(0)%ssws,                 &
+                                           surf_usm_v(0)%ssws,                 &
+                                           surf_def_v(1)%ssws,                 &
+                                           surf_lsm_v(1)%ssws,                 &
+                                           surf_usm_v(1)%ssws,                 &
+                                           surf_def_v(2)%ssws,                 &
+                                           surf_lsm_v(2)%ssws,                 &
+                                           surf_usm_v(2)%ssws,                 &
+                                           surf_def_v(3)%ssws,                 &
+                                           surf_lsm_v(3)%ssws,                 &
+                                           surf_usm_v(3)%ssws, n_out ) 
+
+            CASE ( 'qcsws' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%qcsws,           &
+                                           surf_def_h(1)%qcsws,                &
+                                           surf_lsm_h%qcsws,                   &
+                                           surf_usm_h%qcsws,                   &
+                                           surf_def_v(0)%qcsws,                &
+                                           surf_lsm_v(0)%qcsws,                &
+                                           surf_usm_v(0)%qcsws,                &
+                                           surf_def_v(1)%qcsws,                &
+                                           surf_lsm_v(1)%qcsws,                &
+                                           surf_usm_v(1)%qcsws,                &
+                                           surf_def_v(2)%qcsws,                &
+                                           surf_lsm_v(2)%qcsws,                &
+                                           surf_usm_v(2)%qcsws,                &
+                                           surf_def_v(3)%qcsws,                &
+                                           surf_lsm_v(3)%qcsws,                &
+                                           surf_usm_v(3)%qcsws, n_out ) 
+
+            CASE ( 'ncsws' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%ncsws,           &
+                                           surf_def_h(1)%ncsws,                &
+                                           surf_lsm_h%ncsws,                   &
+                                           surf_usm_h%ncsws,                   &
+                                           surf_def_v(0)%ncsws,                &
+                                           surf_lsm_v(0)%ncsws,                &
+                                           surf_usm_v(0)%ncsws,                &
+                                           surf_def_v(1)%ncsws,                &
+                                           surf_lsm_v(1)%ncsws,                &
+                                           surf_usm_v(1)%ncsws,                &
+                                           surf_def_v(2)%ncsws,                &
+                                           surf_lsm_v(2)%ncsws,                &
+                                           surf_usm_v(2)%ncsws,                &
+                                           surf_def_v(3)%ncsws,                &
+                                           surf_lsm_v(3)%ncsws,                &
+                                           surf_usm_v(3)%ncsws, n_out ) 
+
+            CASE ( 'qrsws' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%qrsws,           &
+                                           surf_def_h(1)%qrsws,                &
+                                           surf_lsm_h%qrsws,                   &
+                                           surf_usm_h%qrsws,                   &
+                                           surf_def_v(0)%qrsws,                &
+                                           surf_lsm_v(0)%qrsws,                &
+                                           surf_usm_v(0)%qrsws,                &
+                                           surf_def_v(1)%qrsws,                &
+                                           surf_lsm_v(1)%qrsws,                &
+                                           surf_usm_v(1)%qrsws,                &
+                                           surf_def_v(2)%qrsws,                &
+                                           surf_lsm_v(2)%qrsws,                &
+                                           surf_usm_v(2)%qrsws,                &
+                                           surf_def_v(3)%qrsws,                &
+                                           surf_lsm_v(3)%qrsws,                &
+                                           surf_usm_v(3)%qrsws, n_out ) 
+
+            CASE ( 'nrsws' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%nrsws,           &
+                                           surf_def_h(1)%nrsws,                &
+                                           surf_lsm_h%nrsws,                   &
+                                           surf_usm_h%nrsws,                   &
+                                           surf_def_v(0)%nrsws,                &
+                                           surf_lsm_v(0)%nrsws,                &
+                                           surf_usm_v(0)%nrsws,                &
+                                           surf_def_v(1)%nrsws,                &
+                                           surf_lsm_v(1)%nrsws,                &
+                                           surf_usm_v(1)%nrsws,                &
+                                           surf_def_v(2)%nrsws,                &
+                                           surf_lsm_v(2)%nrsws,                &
+                                           surf_usm_v(2)%nrsws,                &
+                                           surf_def_v(3)%nrsws,                &
+                                           surf_lsm_v(3)%nrsws,                &
+                                           surf_usm_v(3)%nrsws, n_out ) 
+
+            CASE ( 'sasws' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%sasws,           &
+                                           surf_def_h(1)%sasws,                &
+                                           surf_lsm_h%sasws,                   &
+                                           surf_usm_h%sasws,                   &
+                                           surf_def_v(0)%sasws,                &
+                                           surf_lsm_v(0)%sasws,                &
+                                           surf_usm_v(0)%sasws,                &
+                                           surf_def_v(1)%sasws,                &
+                                           surf_lsm_v(1)%sasws,                &
+                                           surf_usm_v(1)%sasws,                &
+                                           surf_def_v(2)%sasws,                &
+                                           surf_lsm_v(2)%sasws,                &
+                                           surf_usm_v(2)%sasws,                &
+                                           surf_def_v(3)%sasws,                &
+                                           surf_lsm_v(3)%sasws,                &
+                                           surf_usm_v(3)%sasws, n_out ) 
+
+            CASE ( 'q_surface' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%q_surface,       &
+                                           surf_def_h(1)%q_surface,            &
+                                           surf_lsm_h%q_surface,               &
+                                           surf_usm_h%q_surface,               &
+                                           surf_def_v(0)%q_surface,            &
+                                           surf_lsm_v(0)%q_surface,            &
+                                           surf_usm_v(0)%q_surface,            &
+                                           surf_def_v(1)%q_surface,            &
+                                           surf_lsm_v(1)%q_surface,            &
+                                           surf_usm_v(1)%q_surface,            &
+                                           surf_def_v(2)%q_surface,            &
+                                           surf_lsm_v(2)%q_surface,            &
+                                           surf_usm_v(2)%q_surface,            &
+                                           surf_def_v(3)%q_surface,            &
+                                           surf_lsm_v(3)%q_surface,            &
+                                           surf_usm_v(3)%q_surface, n_out ) 
+
+            CASE ( 'theta_surface' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%pt_surface,      &
+                                           surf_def_h(1)%pt_surface,           &
+                                           surf_lsm_h%pt_surface,              &
+                                           surf_usm_h%pt_surface,              &
+                                           surf_def_v(0)%pt_surface,           &
+                                           surf_lsm_v(0)%pt_surface,           &
+                                           surf_usm_v(0)%pt_surface,           &
+                                           surf_def_v(1)%pt_surface,           &
+                                           surf_lsm_v(1)%pt_surface,           &
+                                           surf_usm_v(1)%pt_surface,           &
+                                           surf_def_v(2)%pt_surface,           &
+                                           surf_lsm_v(2)%pt_surface,           &
+                                           surf_usm_v(2)%pt_surface,           &
+                                           surf_def_v(3)%pt_surface,           &
+                                           surf_lsm_v(3)%pt_surface,           &
+                                           surf_usm_v(3)%pt_surface, n_out ) 
+
+            CASE ( 'thetav_surface' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%vpt_surface,     &
+                                           surf_def_h(1)%vpt_surface,          &
+                                           surf_lsm_h%vpt_surface,             &
+                                           surf_usm_h%vpt_surface,             &
+                                           surf_def_v(0)%vpt_surface,          &
+                                           surf_lsm_v(0)%vpt_surface,          &
+                                           surf_usm_v(0)%vpt_surface,          &
+                                           surf_def_v(1)%vpt_surface,          &
+                                           surf_lsm_v(1)%vpt_surface,          &
+                                           surf_usm_v(1)%vpt_surface,          &
+                                           surf_def_v(2)%vpt_surface,          &
+                                           surf_lsm_v(2)%vpt_surface,          &
+                                           surf_usm_v(2)%vpt_surface,          &
+                                           surf_def_v(3)%vpt_surface,          &
+                                           surf_lsm_v(3)%vpt_surface,          &
+                                           surf_usm_v(3)%vpt_surface, n_out ) 
+
+            CASE ( 'rad_net' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%rad_net,         &
+                                           surf_def_h(1)%rad_net,              &
+                                           surf_lsm_h%rad_net,                 &
+                                           surf_usm_h%rad_net,                 &
+                                           surf_def_v(0)%rad_net,              &
+                                           surf_lsm_v(0)%rad_net,              &
+                                           surf_usm_v(0)%rad_net,              &
+                                           surf_def_v(1)%rad_net,              &
+                                           surf_lsm_v(1)%rad_net,              &
+                                           surf_usm_v(1)%rad_net,              &
+                                           surf_def_v(2)%rad_net,              &
+                                           surf_lsm_v(2)%rad_net,              &
+                                           surf_usm_v(2)%rad_net,              &
+                                           surf_def_v(3)%rad_net,              &
+                                           surf_lsm_v(3)%rad_net,              &
+                                           surf_usm_v(3)%rad_net, n_out ) 
+
+            CASE ( 'rad_lw_in' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%rad_lw_in,       &
+                                           surf_def_h(1)%rad_lw_in,            &
+                                           surf_lsm_h%rad_lw_in,               &
+                                           surf_usm_h%rad_lw_in,               &
+                                           surf_def_v(0)%rad_lw_in,            &
+                                           surf_lsm_v(0)%rad_lw_in,            &
+                                           surf_usm_v(0)%rad_lw_in,            &
+                                           surf_def_v(1)%rad_lw_in,            &
+                                           surf_lsm_v(1)%rad_lw_in,            &
+                                           surf_usm_v(1)%rad_lw_in,            &
+                                           surf_def_v(2)%rad_lw_in,            &
+                                           surf_lsm_v(2)%rad_lw_in,            &
+                                           surf_usm_v(2)%rad_lw_in,            &
+                                           surf_def_v(3)%rad_lw_in,            &
+                                           surf_lsm_v(3)%rad_lw_in,            &
+                                           surf_usm_v(3)%rad_lw_in, n_out ) 
+
+            CASE ( 'rad_lw_out' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%rad_lw_out,      &
+                                           surf_def_h(1)%rad_lw_out,           &
+                                           surf_lsm_h%rad_lw_out,              &
+                                           surf_usm_h%rad_lw_out,              &
+                                           surf_def_v(0)%rad_lw_out,           &
+                                           surf_lsm_v(0)%rad_lw_out,           &
+                                           surf_usm_v(0)%rad_lw_out,           &
+                                           surf_def_v(1)%rad_lw_out,           &
+                                           surf_lsm_v(1)%rad_lw_out,           &
+                                           surf_usm_v(1)%rad_lw_out,           &
+                                           surf_def_v(2)%rad_lw_out,           &
+                                           surf_lsm_v(2)%rad_lw_out,           &
+                                           surf_usm_v(2)%rad_lw_out,           &
+                                           surf_def_v(3)%rad_lw_out,           &
+                                           surf_lsm_v(3)%rad_lw_out,           &
+                                           surf_usm_v(3)%rad_lw_out, n_out ) 
+
+            CASE ( 'rad_sw_in' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%rad_sw_in,       &
+                                           surf_def_h(1)%rad_sw_in,            &
+                                           surf_lsm_h%rad_sw_in,               &
+                                           surf_usm_h%rad_sw_in,               &
+                                           surf_def_v(0)%rad_sw_in,            &
+                                           surf_lsm_v(0)%rad_sw_in,            &
+                                           surf_usm_v(0)%rad_sw_in,            &
+                                           surf_def_v(1)%rad_sw_in,            &
+                                           surf_lsm_v(1)%rad_sw_in,            &
+                                           surf_usm_v(1)%rad_sw_in,            &
+                                           surf_def_v(2)%rad_sw_in,            &
+                                           surf_lsm_v(2)%rad_sw_in,            &
+                                           surf_usm_v(2)%rad_sw_in,            &
+                                           surf_def_v(3)%rad_sw_in,            &
+                                           surf_lsm_v(3)%rad_sw_in,            &
+                                           surf_usm_v(3)%rad_sw_in, n_out ) 
+
+            CASE ( 'rad_sw_out' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%rad_sw_out,      &
+                                           surf_def_h(1)%rad_sw_out,           &
+                                           surf_lsm_h%rad_sw_out,              &
+                                           surf_usm_h%rad_sw_out,              &
+                                           surf_def_v(0)%rad_sw_out,           &
+                                           surf_lsm_v(0)%rad_sw_out,           &
+                                           surf_usm_v(0)%rad_sw_out,           &
+                                           surf_def_v(1)%rad_sw_out,           &
+                                           surf_lsm_v(1)%rad_sw_out,           &
+                                           surf_usm_v(1)%rad_sw_out,           &
+                                           surf_def_v(2)%rad_sw_out,           &
+                                           surf_lsm_v(2)%rad_sw_out,           &
+                                           surf_usm_v(2)%rad_sw_out,           &
+                                           surf_def_v(3)%rad_sw_out,           &
+                                           surf_lsm_v(3)%rad_sw_out,           &
+                                           surf_usm_v(3)%rad_sw_out, n_out ) 
+
+            CASE ( 'ghf' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%ghf,             &
+                                           surf_def_h(1)%ghf,                  &
+                                           surf_lsm_h%ghf,                     &
+                                           surf_usm_h%ghf,                     &
+                                           surf_def_v(0)%ghf,                  &
+                                           surf_lsm_v(0)%ghf,                  &
+                                           surf_usm_v(0)%ghf,                  &
+                                           surf_def_v(1)%ghf,                  &
+                                           surf_lsm_v(1)%ghf,                  &
+                                           surf_usm_v(1)%ghf,                  &
+                                           surf_def_v(2)%ghf,                  &
+                                           surf_lsm_v(2)%ghf,                  &
+                                           surf_usm_v(2)%ghf,                  &
+                                           surf_def_v(3)%ghf,                  &
+                                           surf_lsm_v(3)%ghf,                  &
+                                           surf_usm_v(3)%ghf, n_out )          
+                                                                               
+            CASE ( 'r_a' )                                                     
+               CALL surface_data_output_sum_up( surf_def_h(0)%r_a,             &
+                                           surf_def_h(1)%r_a,                  &
+                                           surf_lsm_h%r_a,                     &
+                                           surf_usm_h%r_a,                     &
+                                           surf_def_v(0)%r_a,                  &
+                                           surf_lsm_v(0)%r_a,                  &
+                                           surf_usm_v(0)%r_a,                  &
+                                           surf_def_v(1)%r_a,                  &
+                                           surf_lsm_v(1)%r_a,                  &
+                                           surf_usm_v(1)%r_a,                  &
+                                           surf_def_v(2)%r_a,                  &
+                                           surf_lsm_v(2)%r_a,                  &
+                                           surf_usm_v(2)%r_a,                  &
+                                           surf_def_v(3)%r_a,                  &
+                                           surf_lsm_v(3)%r_a,                  &
+                                           surf_usm_v(3)%r_a, n_out )          
+                                                                               
+            CASE ( 'r_soil' )                                                  
+               CALL surface_data_output_sum_up( surf_def_h(0)%r_soil,          &
+                                           surf_def_h(1)%r_soil,               &
+                                           surf_lsm_h%r_soil,                  &
+                                           surf_usm_h%r_soil,                  &
+                                           surf_def_v(0)%r_soil,               &
+                                           surf_lsm_v(0)%r_soil,               &
+                                           surf_usm_v(0)%r_soil,               &
+                                           surf_def_v(1)%r_soil,               &
+                                           surf_lsm_v(1)%r_soil,               &
+                                           surf_usm_v(1)%r_soil,               &
+                                           surf_def_v(2)%r_soil,               &
+                                           surf_lsm_v(2)%r_soil,               &
+                                           surf_usm_v(2)%r_soil,               &
+                                           surf_def_v(3)%r_soil,               &
+                                           surf_lsm_v(3)%r_soil,               &
+                                           surf_usm_v(3)%r_soil, n_out ) 
+
+            CASE ( 'r_canopy' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%r_canopy,        &
+                                           surf_def_h(1)%r_canopy,             &
+                                           surf_lsm_h%r_canopy,                &
+                                           surf_usm_h%r_canopy,                &
+                                           surf_def_v(0)%r_canopy,             &
+                                           surf_lsm_v(0)%r_canopy,             &
+                                           surf_usm_v(0)%r_canopy,             &
+                                           surf_def_v(1)%r_canopy,             &
+                                           surf_lsm_v(1)%r_canopy,             &
+                                           surf_usm_v(1)%r_canopy,             &
+                                           surf_def_v(2)%r_canopy,             &
+                                           surf_lsm_v(2)%r_canopy,             &
+                                           surf_usm_v(2)%r_canopy,             &
+                                           surf_def_v(3)%r_canopy,             &
+                                           surf_lsm_v(3)%r_canopy,             &
+                                           surf_usm_v(3)%r_canopy, n_out ) 
+
+            CASE ( 'r_s' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%r_s,             &
+                                           surf_def_h(1)%r_s,                  &
+                                           surf_lsm_h%r_s,                     &
+                                           surf_usm_h%r_s,                     &
+                                           surf_def_v(0)%r_s,                  &
+                                           surf_lsm_v(0)%r_s,                  &
+                                           surf_usm_v(0)%r_s,                  &
+                                           surf_def_v(1)%r_s,                  &
+                                           surf_lsm_v(1)%r_s,                  &
+                                           surf_usm_v(1)%r_s,                  &
+                                           surf_def_v(2)%r_s,                  &
+                                           surf_lsm_v(2)%r_s,                  &
+                                           surf_usm_v(2)%r_s,                  &
+                                           surf_def_v(3)%r_s,                  &
+                                           surf_lsm_v(3)%r_s,                  &
+                                           surf_usm_v(3)%r_s, n_out ) 
+
+
+            CASE ( 'rad_sw_dir' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%rad_sw_dir,      &
+                                           surf_def_h(1)%rad_sw_dir,           &
+                                           surf_lsm_h%rad_sw_dir,              &
+                                           surf_usm_h%rad_sw_dir,              &
+                                           surf_def_v(0)%rad_sw_dir,           &
+                                           surf_lsm_v(0)%rad_sw_dir,           &
+                                           surf_usm_v(0)%rad_sw_dir,           &
+                                           surf_def_v(1)%rad_sw_dir,           &
+                                           surf_lsm_v(1)%rad_sw_dir,           &
+                                           surf_usm_v(1)%rad_sw_dir,           &
+                                           surf_def_v(2)%rad_sw_dir,           &
+                                           surf_lsm_v(2)%rad_sw_dir,           &
+                                           surf_usm_v(2)%rad_sw_dir,           &
+                                           surf_def_v(3)%rad_sw_dir,           &
+                                           surf_lsm_v(3)%rad_sw_dir,           &
+                                           surf_usm_v(3)%rad_sw_dir, n_out )
+            CASE ( 'rad_sw_dif' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%rad_sw_dif,      &
+                                           surf_def_h(1)%rad_sw_dif,           &
+                                           surf_lsm_h%rad_sw_dif,              &
+                                           surf_usm_h%rad_sw_dif,              &
+                                           surf_def_v(0)%rad_sw_dif,           &
+                                           surf_lsm_v(0)%rad_sw_dif,           &
+                                           surf_usm_v(0)%rad_sw_dif,           &
+                                           surf_def_v(1)%rad_sw_dif,           &
+                                           surf_lsm_v(1)%rad_sw_dif,           &
+                                           surf_usm_v(1)%rad_sw_dif,           &
+                                           surf_def_v(2)%rad_sw_dif,           &
+                                           surf_lsm_v(2)%rad_sw_dif,           &
+                                           surf_usm_v(2)%rad_sw_dif,           &
+                                           surf_def_v(3)%rad_sw_dif,           &
+                                           surf_lsm_v(3)%rad_sw_dif,           &
+                                           surf_usm_v(3)%rad_sw_dif, n_out )
+
+            CASE ( 'rad_sw_ref' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%rad_sw_ref,      &
+                                           surf_def_h(1)%rad_sw_ref,           &
+                                           surf_lsm_h%rad_sw_ref,              &
+                                           surf_usm_h%rad_sw_ref,              &
+                                           surf_def_v(0)%rad_sw_ref,           &
+                                           surf_lsm_v(0)%rad_sw_ref,           &
+                                           surf_usm_v(0)%rad_sw_ref,           &
+                                           surf_def_v(1)%rad_sw_ref,           &
+                                           surf_lsm_v(1)%rad_sw_ref,           &
+                                           surf_usm_v(1)%rad_sw_ref,           &
+                                           surf_def_v(2)%rad_sw_ref,           &
+                                           surf_lsm_v(2)%rad_sw_ref,           &
+                                           surf_usm_v(2)%rad_sw_ref,           &
+                                           surf_def_v(3)%rad_sw_ref,           &
+                                           surf_lsm_v(3)%rad_sw_ref,           &
+                                           surf_usm_v(3)%rad_sw_ref, n_out )
+               
+            CASE ( 'rad_sw_res' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%rad_sw_res,      &
+                                           surf_def_h(1)%rad_sw_res,           &
+                                           surf_lsm_h%rad_sw_res,              &
+                                           surf_usm_h%rad_sw_res,              &
+                                           surf_def_v(0)%rad_sw_res,           &
+                                           surf_lsm_v(0)%rad_sw_res,           &
+                                           surf_usm_v(0)%rad_sw_res,           &
+                                           surf_def_v(1)%rad_sw_res,           &
+                                           surf_lsm_v(1)%rad_sw_res,           &
+                                           surf_usm_v(1)%rad_sw_res,           &
+                                           surf_def_v(2)%rad_sw_res,           &
+                                           surf_lsm_v(2)%rad_sw_res,           &
+                                           surf_usm_v(2)%rad_sw_res,           &
+                                           surf_def_v(3)%rad_sw_res,           &
+                                           surf_lsm_v(3)%rad_sw_res,           &
+                                           surf_usm_v(3)%rad_sw_res, n_out )
+
+            CASE ( 'rad_lw_dif' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%rad_lw_dif,      &
+                                           surf_def_h(1)%rad_lw_dif,           &
+                                           surf_lsm_h%rad_lw_dif,              &
+                                           surf_usm_h%rad_lw_dif,              &
+                                           surf_def_v(0)%rad_lw_dif,           &
+                                           surf_lsm_v(0)%rad_lw_dif,           &
+                                           surf_usm_v(0)%rad_lw_dif,           &
+                                           surf_def_v(1)%rad_lw_dif,           &
+                                           surf_lsm_v(1)%rad_lw_dif,           &
+                                           surf_usm_v(1)%rad_lw_dif,           &
+                                           surf_def_v(2)%rad_lw_dif,           &
+                                           surf_lsm_v(2)%rad_lw_dif,           &
+                                           surf_usm_v(2)%rad_lw_dif,           &
+                                           surf_def_v(3)%rad_lw_dif,           &
+                                           surf_lsm_v(3)%rad_lw_dif,           &
+                                           surf_usm_v(3)%rad_lw_dif, n_out )
+
+            CASE ( 'rad_lw_ref' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%rad_lw_ref,      &
+                                           surf_def_h(1)%rad_lw_ref,           &
+                                           surf_lsm_h%rad_lw_ref,              &
+                                           surf_usm_h%rad_lw_ref,              &
+                                           surf_def_v(0)%rad_lw_ref,           &
+                                           surf_lsm_v(0)%rad_lw_ref,           &
+                                           surf_usm_v(0)%rad_lw_ref,           &
+                                           surf_def_v(1)%rad_lw_ref,           &
+                                           surf_lsm_v(1)%rad_lw_ref,           &
+                                           surf_usm_v(1)%rad_lw_ref,           &
+                                           surf_def_v(2)%rad_lw_ref,           &
+                                           surf_lsm_v(2)%rad_lw_ref,           &
+                                           surf_usm_v(2)%rad_lw_ref,           &
+                                           surf_def_v(3)%rad_lw_ref,           &
+                                           surf_lsm_v(3)%rad_lw_ref,           &
+                                           surf_usm_v(3)%rad_lw_ref, n_out )
+
+            CASE ( 'rad_lw_res' )
+               CALL surface_data_output_sum_up( surf_def_h(0)%rad_lw_res,      &
+                                           surf_def_h(1)%rad_lw_res,           &
+                                           surf_lsm_h%rad_lw_res,              &
+                                           surf_usm_h%rad_lw_res,              &
+                                           surf_def_v(0)%rad_lw_res,           &
+                                           surf_lsm_v(0)%rad_lw_res,           &
+                                           surf_usm_v(0)%rad_lw_res,           &
+                                           surf_def_v(1)%rad_lw_res,           &
+                                           surf_lsm_v(1)%rad_lw_res,           &
+                                           surf_usm_v(1)%rad_lw_res,           &
+                                           surf_def_v(2)%rad_lw_res,           &
+                                           surf_lsm_v(2)%rad_lw_res,           &
+                                           surf_usm_v(2)%rad_lw_res,           &
+                                           surf_def_v(3)%rad_lw_res,           &
+                                           surf_lsm_v(3)%rad_lw_res,           &
+                                           surf_usm_v(3)%rad_lw_res, n_out )
+
+         END SELECT
+      ENDDO
+      
+   
+   END SUBROUTINE surface_data_output_averaging
+   
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Sum-up the surface data for average output variables.  
+!------------------------------------------------------------------------------!   
+   SUBROUTINE surface_data_output_sum_up( var_def_h0, var_def_h1,              &
+                                     var_lsm_h,  var_usm_h,                    &
+                                     var_def_v0, var_lsm_v0, var_usm_v0,       &
+                                     var_def_v1, var_lsm_v1, var_usm_v1,       &
+                                     var_def_v2, var_lsm_v2, var_usm_v2,       &
+                                     var_def_v3, var_lsm_v3, var_usm_v3, n_out )
+   
+      IMPLICIT NONE
+
+      INTEGER(iwp) ::  m          !< running index for surface elements
+      INTEGER(iwp) ::  n_out      !< index for output variable
+      INTEGER(iwp) ::  n_surf     !< running index for surface elements
+      
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_h0 !< output variable at upward-facing default-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_h1 !< output variable at downward-facing default-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_h  !< output variable at upward-facing natural-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_h  !< output variable at upward-facing urban-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_v0 !< output variable at northward-facing default-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_v1 !< output variable at southward-facing default-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_v2 !< output variable at eastward-facing default-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_v3 !< output variable at westward-facing default-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_v0 !< output variable at northward-facing natural-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_v1 !< output variable at southward-facing natural-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_v2 !< output variable at eastward-facing natural-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_v3 !< output variable at westward-facing natural-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_v0 !< output variable at northward-facing urban-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_v1 !< output variable at southward-facing urban-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_v2 !< output variable at eastward-facing urban-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_v3 !< output variable at westward-facing urban-type surfaces
+     
+!     
+!--   Set counter variable to zero before the variable is written to 
+!--   the output array. 
+      n_surf = 0
+      
+!
+!--   Write the horizontal surfaces.
+!--   Before each the variable is written to the output data structure, first 
+!--   check if the variable for the respective surface type is defined. 
+!--   If a variable is not defined, skip the block and increment the counter
+!--   variable by the number of surface elements of this type. Usually this 
+!--   is zere, however, there might be the situation that e.g. urban surfaces
+!--   are defined but the respective variable is not allocated for this surface
+!--   type. To write the data on the exact position, increment the counter. 
+      IF ( ALLOCATED( var_def_h0 ) )  THEN
+         DO  m = 1, surf_def_h(0)%ns 
+            n_surf                        = n_surf + 1
+            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
+                                          + var_def_h0(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_def_h(0)%ns  
+      ENDIF
+      IF ( ALLOCATED( var_def_h1 ) )  THEN
+         DO  m = 1, surf_def_h(1)%ns 
+            n_surf                   = n_surf + 1
+            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
+                                          + var_def_h1(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_def_h(1)%ns
+      ENDIF
+      IF ( ALLOCATED( var_lsm_h ) )  THEN
+         DO  m = 1, surf_lsm_h%ns 
+            n_surf                        = n_surf + 1
+            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
+                                          + var_lsm_h(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_lsm_h%ns
+      ENDIF
+      IF ( ALLOCATED( var_usm_h ) )  THEN
+         DO  m = 1, surf_usm_h%ns 
+            n_surf                        = n_surf + 1
+            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
+                                          + var_usm_h(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_usm_h%ns
+      ENDIF
+!
+!--   Write northward-facing
+      IF ( ALLOCATED( var_def_v0 ) )  THEN
+         DO  m = 1, surf_def_v(0)%ns 
+            n_surf                        = n_surf + 1
+            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
+                                          + var_def_v0(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_def_v(0)%ns
+      ENDIF
+      IF ( ALLOCATED( var_lsm_v0 ) )  THEN
+         DO  m = 1, surf_lsm_v(0)%ns 
+            n_surf                        = n_surf + 1
+            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
+                                          + var_lsm_v0(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_lsm_v(0)%ns
+      ENDIF
+      IF ( ALLOCATED( var_usm_v0 ) )  THEN
+         DO  m = 1, surf_usm_v(0)%ns 
+            n_surf                        = n_surf + 1
+            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
+                                          + var_usm_v0(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_usm_v(0)%ns
+      ENDIF
+!
+!--   Write southward-facing
+      IF ( ALLOCATED( var_def_v1 ) )  THEN
+         DO  m = 1, surf_def_v(1)%ns 
+            n_surf                        = n_surf + 1
+            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
+                                          + var_def_v1(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_def_v(1)%ns
+      ENDIF
+      IF ( ALLOCATED( var_lsm_v1 ) )  THEN
+         DO  m = 1, surf_lsm_v(1)%ns 
+            n_surf                        = n_surf + 1
+            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
+                                          + var_lsm_v1(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_lsm_v(1)%ns
+      ENDIF
+      IF ( ALLOCATED( var_usm_v1 ) )  THEN
+         DO  m = 1, surf_usm_v(1)%ns 
+            n_surf                        = n_surf + 1
+            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
+                                          + var_usm_v1(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_usm_v(1)%ns
+      ENDIF
+!
+!--   Write eastward-facing
+      IF ( ALLOCATED( var_def_v2 ) )  THEN
+         DO  m = 1, surf_def_v(2)%ns 
+            n_surf                        = n_surf + 1
+            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
+                                          + var_def_v2(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_def_v(2)%ns
+      ENDIF
+      IF ( ALLOCATED( var_lsm_v2 ) )  THEN
+         DO  m = 1, surf_lsm_v(2)%ns 
+            n_surf                        = n_surf + 1
+            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
+                                          + var_lsm_v2(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_lsm_v(2)%ns
+      ENDIF
+      IF ( ALLOCATED( var_usm_v2 ) )  THEN
+         DO  m = 1, surf_usm_v(2)%ns 
+            n_surf                        = n_surf + 1
+            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
+                                          + var_usm_v2(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_usm_v(2)%ns
+      ENDIF
+!
+!--   Write westward-facing
+      IF ( ALLOCATED( var_def_v3 ) )  THEN
+         DO  m = 1, surf_def_v(3)%ns 
+            n_surf                        = n_surf + 1
+            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
+                                          + var_def_v3(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_def_v(3)%ns
+      ENDIF
+      IF ( ALLOCATED( var_lsm_v3 ) )  THEN
+         DO  m = 1, surf_lsm_v(3)%ns 
+            n_surf                        = n_surf + 1
+            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
+                                          + var_lsm_v3(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_lsm_v(3)%ns
+      ENDIF
+      IF ( ALLOCATED( var_usm_v3 ) )  THEN
+         DO  m = 1, surf_usm_v(3)%ns 
+            n_surf                        = n_surf + 1
+            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
+                                          + var_usm_v3(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_usm_v(3)%ns
+      ENDIF
+   
+   END SUBROUTINE surface_data_output_sum_up
+   
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Collect the surface data from different types and different orientation.
+!------------------------------------------------------------------------------!
+   SUBROUTINE surface_data_output_collect( var_def_h0, var_def_h1,             &
+                                      var_lsm_h,  var_usm_h,                   &
+                                      var_def_v0, var_lsm_v0, var_usm_v0,      &
+                                      var_def_v1, var_lsm_v1, var_usm_v1,      &
+                                      var_def_v2, var_lsm_v2, var_usm_v2,      &
+                                      var_def_v3, var_lsm_v3, var_usm_v3 )                                               
+   
+      IMPLICIT NONE
+
+      INTEGER(iwp) ::  m      !< running index for surface elements
+      INTEGER(iwp) ::  n_surf !< running index for surface elements
+      
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_h0 !< output variable at upward-facing default-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_h1 !< output variable at downward-facing default-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_h  !< output variable at upward-facing natural-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_h  !< output variable at upward-facing urban-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_v0 !< output variable at northward-facing default-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_v1 !< output variable at southward-facing default-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_v2 !< output variable at eastward-facing default-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_v3 !< output variable at westward-facing default-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_v0 !< output variable at northward-facing natural-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_v1 !< output variable at southward-facing natural-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_v2 !< output variable at eastward-facing natural-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_v3 !< output variable at westward-facing natural-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_v0 !< output variable at northward-facing urban-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_v1 !< output variable at southward-facing urban-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_v2 !< output variable at eastward-facing urban-type surfaces
+      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_v3 !< output variable at westward-facing urban-type surfaces
+     
+!     
+!--   Set counter variable to zero before the variable is written to 
+!--   the output array. 
+      n_surf = 0
+      
+!
+!--   Write the horizontal surfaces.
+!--   Before each the variable is written to the output data structure, first 
+!--   check if the variable for the respective surface type is defined. 
+!--   If a variable is not defined, skip the block and increment the counter
+!--   variable by the number of surface elements of this type. Usually this 
+!--   is zere, however, there might be the situation that e.g. urban surfaces
+!--   are defined but the respective variable is not allocated for this surface
+!--   type. To write the data on the exact position, increment the counter. 
+      IF ( ALLOCATED( var_def_h0 ) )  THEN
+         DO  m = 1, surf_def_h(0)%ns 
+            n_surf                   = n_surf + 1
+            surfaces%var_out(n_surf) = var_def_h0(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_def_h(0)%ns  
+      ENDIF
+      IF ( ALLOCATED( var_def_h1 ) )  THEN
+         DO  m = 1, surf_def_h(1)%ns 
+            n_surf                   = n_surf + 1
+            surfaces%var_out(n_surf) = var_def_h1(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_def_h(1)%ns
+      ENDIF
+      IF ( ALLOCATED( var_lsm_h ) )  THEN
+         DO  m = 1, surf_lsm_h%ns 
+            n_surf                   = n_surf + 1
+            surfaces%var_out(n_surf) = var_lsm_h(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_lsm_h%ns
+      ENDIF
+      IF ( ALLOCATED( var_usm_h ) )  THEN
+         DO  m = 1, surf_usm_h%ns 
+            n_surf                   = n_surf + 1
+            surfaces%var_out(n_surf) = var_usm_h(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_usm_h%ns
+      ENDIF
+!
+!--   Write northward-facing
+      IF ( ALLOCATED( var_def_v0 ) )  THEN
+         DO  m = 1, surf_def_v(0)%ns 
+            n_surf                   = n_surf + 1
+            surfaces%var_out(n_surf) = var_def_v0(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_def_v(0)%ns
+      ENDIF
+      IF ( ALLOCATED( var_lsm_v0 ) )  THEN
+         DO  m = 1, surf_lsm_v(0)%ns 
+            n_surf                   = n_surf + 1
+            surfaces%var_out(n_surf) = var_lsm_v0(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_lsm_v(0)%ns
+      ENDIF
+      IF ( ALLOCATED( var_usm_v0 ) )  THEN
+         DO  m = 1, surf_usm_v(0)%ns 
+            n_surf                   = n_surf + 1
+            surfaces%var_out(n_surf) = var_usm_v0(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_usm_v(0)%ns
+      ENDIF
+!
+!--   Write southward-facing
+      IF ( ALLOCATED( var_def_v1 ) )  THEN
+         DO  m = 1, surf_def_v(1)%ns 
+            n_surf                   = n_surf + 1
+            surfaces%var_out(n_surf) = var_def_v1(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_def_v(1)%ns
+      ENDIF
+      IF ( ALLOCATED( var_lsm_v1 ) )  THEN
+         DO  m = 1, surf_lsm_v(1)%ns 
+            n_surf                   = n_surf + 1
+            surfaces%var_out(n_surf) = var_lsm_v1(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_lsm_v(1)%ns
+      ENDIF
+      IF ( ALLOCATED( var_usm_v1 ) )  THEN
+         DO  m = 1, surf_usm_v(1)%ns 
+            n_surf                   = n_surf + 1
+            surfaces%var_out(n_surf) = var_usm_v1(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_usm_v(1)%ns
+      ENDIF
+!
+!--   Write eastward-facing
+      IF ( ALLOCATED( var_def_v2 ) )  THEN
+         DO  m = 1, surf_def_v(2)%ns 
+            n_surf                   = n_surf + 1
+            surfaces%var_out(n_surf) = var_def_v2(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_def_v(2)%ns
+      ENDIF
+      IF ( ALLOCATED( var_lsm_v2 ) )  THEN
+         DO  m = 1, surf_lsm_v(2)%ns 
+            n_surf                   = n_surf + 1
+            surfaces%var_out(n_surf) = var_lsm_v2(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_lsm_v(2)%ns
+      ENDIF
+      IF ( ALLOCATED( var_usm_v2 ) )  THEN
+         DO  m = 1, surf_usm_v(2)%ns 
+            n_surf                   = n_surf + 1
+            surfaces%var_out(n_surf) = var_usm_v2(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_usm_v(2)%ns
+      ENDIF
+!
+!--   Write westward-facing
+      IF ( ALLOCATED( var_def_v3 ) )  THEN
+         DO  m = 1, surf_def_v(3)%ns 
+            n_surf                   = n_surf + 1
+            surfaces%var_out(n_surf) = var_def_v3(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_def_v(3)%ns
+      ENDIF
+      IF ( ALLOCATED( var_lsm_v3 ) )  THEN
+         DO  m = 1, surf_lsm_v(3)%ns 
+            n_surf                   = n_surf + 1
+            surfaces%var_out(n_surf) = var_lsm_v3(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_lsm_v(3)%ns
+      ENDIF
+      IF ( ALLOCATED( var_usm_v3 ) )  THEN
+         DO  m = 1, surf_usm_v(3)%ns 
+            n_surf                   = n_surf + 1
+            surfaces%var_out(n_surf) = var_usm_v3(m)
+         ENDDO
+      ELSE
+         n_surf = n_surf + surf_usm_v(3)%ns
+      ENDIF
+   
+   END SUBROUTINE surface_data_output_collect
+   
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Parin for output of surface parameters
+!------------------------------------------------------------------------------!
+    SUBROUTINE surface_data_output_parin
+
+       IMPLICIT NONE
+
+       CHARACTER (LEN=80) ::  line  !< dummy string that contains the current line of the parameter file 
+       
+
+       NAMELIST /surface_data_output_parameters/                               &
+                                  averaging_interval_surf, data_output_surf,   &
+                                  dt_dosurf, dt_dosurf_av,                     &
+                                  skip_time_dosurf, skip_time_dosurf_av
+                                  
+       line = ' '
+  
+!
+!--    Try to find the namelist
+       REWIND ( 11 )
+       line = ' '
+       DO WHILE ( INDEX( line, '&surface_data_output_parameters' ) == 0 )
+          READ ( 11, '(A)', END=14 )  line
+       ENDDO
+       BACKSPACE ( 11 )
+
+!
+!--    Read namelist
+       READ ( 11, surface_data_output_parameters, ERR = 10 )
+!
+!--    Set flag that indicates that surface data output is switched on
+       surface_output = .TRUE.
+       GOTO 14
+
+ 10    BACKSPACE( 11 )
+       READ( 11 , '(A)') line
+       CALL parin_fail_message( 'surface_data_output_parameters', line )      
+       
+ 14    CONTINUE
+       
+
+    END SUBROUTINE surface_data_output_parin
+    
+    
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Check the input parameters for consistency. Further pre-process the given 
+!> output variables, i.e. separate them into average and non-average output
+!> variables and map them onto internal output array. 
+!------------------------------------------------------------------------------!
+    SUBROUTINE surface_data_output_check_parameters
+
+       USE control_parameters,                                                 &
+           ONLY:  averaging_interval, dt_data_output, message_string
+
+       IMPLICIT NONE
+
+       CHARACTER(LEN=100) ::  trimvar !< dummy for single output variable
+
+       INTEGER(iwp) ::  av    !< id indicating average or non-average data output
+       INTEGER(iwp) ::  ilen  !< string length
+       INTEGER(iwp) ::  n_out !< running index for number of output variables
+
+!
+!--    Check the average interval
+       IF ( averaging_interval_surf == 9999999.9_wp )  THEN
+          averaging_interval_surf = averaging_interval
+       ENDIF
+!
+!--    Set the default data-output interval dt_data_output if necessary
+       IF ( dt_dosurf    == 9999999.9_wp )  dt_dosurf    = dt_data_output
+       IF ( dt_dosurf_av == 9999999.9_wp )  dt_dosurf_av = dt_data_output
+
+       IF ( averaging_interval_surf > dt_dosurf_av )  THEN
+          WRITE( message_string, * )  'averaging_interval_surf = ',            &
+                averaging_interval_surf, ' must be <= dt_dosurf_av = ',        &
+                dt_dosurf_av
+          CALL message( 'surface_data_output_check_parameters',                &
+                        'PA0087', 1, 2, 0, 6, 0 )
+       ENDIF
+
+!
+!--   Count number of output variables and separate output strings for 
+!--   average and non-average output variables.
+      n_out = 0
+      DO WHILE ( data_output_surf(n_out+1)(1:1) /= ' ' )
+
+         n_out = n_out + 1
+         ilen = LEN_TRIM( data_output_surf(n_out) )
+         trimvar = TRIM( data_output_surf(n_out) )
+
+!
+!--      Check for data averaging
+         av = 0
+         IF ( ilen > 3 )  THEN
+            IF ( data_output_surf(n_out)(ilen-2:ilen) == '_av' )  THEN
+               trimvar = data_output_surf(n_out)(1:ilen-3)
+               av      = 1
+            ENDIF
+         ENDIF
+
+         dosurf_no(av) = dosurf_no(av) + 1
+         dosurf(av,dosurf_no(av)) = TRIM( trimvar )
+
+!
+!--      Check if all output variables are known
+         SELECT CASE ( TRIM( trimvar ) )
+
+            CASE ( 'css', 'cssws', 'qsws_liq', 'qsws_soil', 'qsws_veg' )
+               message_string = TRIM( trimvar ) //                             &
+                             ' is not yet implemented in the surface output'
+               CALL message( 'surface_data_output_check_parameters',           &
+                             'PA0087', 1, 2, 0, 6, 0 )
+
+            CASE ( 'us', 'ts', 'qs', 'ss', 'qcs', 'ncs', 'qrs', 'nrs', 'ol' )
+
+            CASE ( 'z0', 'z0h', 'z0q' )
+
+            CASE ( 'theta1', 'qv1', 'thetav1' )
+
+            CASE ( 'usws', 'vsws' )
+
+            CASE ( 'qcsws', 'ncsws', 'qrsws', 'nrsws', 'sasws' )
+
+            CASE ( 'shf', 'qsws', 'ssws' )
+
+            CASE ( 'q_surface', 'theta_surface', 'thetav_surface' )
+
+            CASE ( 'rad_net' )
+
+            CASE ( 'rad_lw_in', 'rad_lw_out', 'rad_lw_dif', 'rad_lw_ref',     &
+                   'rad_lw_res' )
+
+            CASE ( 'rad_sw_in', 'rad_sw_out', 'rad_sw_dif', 'rad_sw_ref',     &
+                   'rad_sw_res', 'rad_sw_dir' )
+
+            CASE ( 'ghf' )
+
+            CASE ( 'r_a', 'r_canopy', 'r_soil', 'r_s' )
+
+            CASE DEFAULT
+               message_string = TRIM( trimvar ) //                             &
+                             ' is not part of the surface output'
+               CALL message( 'surface_data_output_check_parameters',           &
+                             'PA0087', 1, 2, 0, 6, 0 )
+         END SELECT
+
+       ENDDO
+
+    END SUBROUTINE surface_data_output_check_parameters
+    
+    
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Last action.
+!------------------------------------------------------------------------------!
+    SUBROUTINE surface_data_output_last_action( av )
+  
+      USE control_parameters,                                                  &
+          ONLY:  io_blocks, io_group
+
+      USE pegrid,                                                              &
+          ONLY:  comm2d, ierr
+
+      IMPLICIT NONE
+      
+      INTEGER(iwp) ::  av     !< id indicating average or non-average data output
+      INTEGER(iwp) ::  i      !< loop index
+
+!
+!--   Return, if nothing to output
+      IF ( dosurf_no(av) == 0 )  RETURN
+!
+!--   Open files
+      CALL check_open( 25+av )
+!
+!--   Write time coordinate
+      DO  i = 0, io_blocks-1
+         IF ( i == io_group )  THEN
+            WRITE ( 25+av )  LEN_TRIM( 'END' )
+            WRITE ( 25+av )  'END'
+         ENDIF
+#if defined( __parallel )
+         CALL MPI_BARRIER( comm2d, ierr )
+#endif
+      ENDDO    
+
+    END SUBROUTINE surface_data_output_last_action    
+
+
+END MODULE surface_data_output_mod
Index: lm/trunk/SOURCE/surface_output_mod.f90
===================================================================
--- /palm/trunk/SOURCE/surface_output_mod.f90	(revision 3647)
+++ 	(revision )
@@ -1,3748 +1,0 @@
-!> @file surface_output_mod.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/>.
-!
-! Copyright 1997-2018 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! ------------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! Bugfix: use time_since_reference_point instead of simulated_time (relevant
-! when using wall/soil spinup)
-! 
-! 3614 2018-12-10 07:05:46Z raasch
-! unused variables removed
-! 
-! 3572 2018-11-28 11:40:28Z suehring
-! Added short- and longwave radiation flux arrays (e.g. diffuse, direct,
-! reflected, resedual) for all surfaces (M. Salim)
-! 
-! 3494 2018-11-06 14:51:27Z suehring
-! Bugfix in gathering surface data from different types and orientation. 
-! Output of total number of surfaces and vertices added.
-! String length is output, for more convinient post-processing.
-! Last actions added.
-! 
-! 3483 2018-11-02 14:19:26Z raasch
-! bugfix: missed directives for MPI added
-! 
-! 3421 2018-10-24 18:39:32Z gronemeier
-! Add output variables
-! Write data into binary file
-!
-! 3420 2018-10-24 17:30:08Z gronemeier
-! Initial implementation from Klaus Ketelsen and Matthias Suehring
-!
-!
-! Authors:
-! --------
-! @author Klaus Ketelsen, Matthias Suehring
-!
-! Description:
-! ------------
-!> Generate output for surface data.
-!> 
-!> @todo Create namelist file for post-processing tool. 
-!------------------------------------------------------------------------------!
-
-MODULE surface_output_mod
-
-   USE kinds
-
-   USE arrays_3d,                                                              &
-       ONLY:  zu, zw
-       
-   USE control_parameters,                                                     &
-       ONLY:  surface_data_output
-       
-   USE grid_variables,                                                         &
-       ONLY: dx,dy
-       
-   USE indices,                                                                &
-       ONLY: nxl, nxr, nys, nyn, nzb, nzt
-       
-   USE pegrid
-       
-   USE surface_mod,                                                            &
-       ONLY:  surf_def_h, surf_def_v, surf_lsm_h, surf_lsm_v,                  &
-              surf_usm_h, surf_usm_v
-
-   IMPLICIT NONE
-
-   TYPE surf_out                      !< data structure which contains all surfaces elements of all types on subdomain
-   
-      INTEGER(iwp) ::  ns             !< number of surface elements on subdomain
-      INTEGER(iwp) ::  ns_total       !< total number of surface elements
-      INTEGER(iwp) ::  npoints        !< number of points / vertices which define a surface element (on subdomain)
-      INTEGER(iwp) ::  npoints_total  !< total number of points / vertices which define a surface element
-      
-      REAL(wp) ::  fillvalue = -9999.9_wp !< fillvalue for surface elements which are not defined
-      
-      REAL(wp), DIMENSION(:), ALLOCATABLE   ::  var_out  !< output variables
-      REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  var_av   !< variables used for averaging
-      REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  points   !< points  / vertices of a surface element
-      REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  polygons !< polygon data of a surface element
-   END TYPE surf_out
-   
-   CHARACTER(LEN=100), DIMENSION(300)       ::  data_output_surf = ' '  !< namelist variable which describes the output variables
-   CHARACTER(LEN=100), DIMENSION(0:1,300)   ::  dosurf = ' '            !< internal variable which describes the output variables and separates 
-                                                                        !< averaged from non-averaged output
-   
-   INTEGER(iwp) ::  average_count_surf = 0                !< number of ensemble members used for averaging
-   INTEGER(iwp) ::  dosurf_no(0:1) = 0                    !< number of surface output quantities
-
-   LOGICAL :: first_output(0:1) = .FALSE.                 !< true if first output was already called
-
-   REAL(wp) ::  averaging_interval_surf  = 9999999.9_wp   !< averaging interval
-   REAL(wp) ::  dt_dosurf = 9999999.9_wp                  !< time interval for instantaneous data output
-   REAL(wp) ::  dt_dosurf_av = 9999999.9_wp               !< time interval for averaged data output
-   REAL(wp) ::  skip_time_dosurf = 0.0_wp                 !< skip time for instantaneous data output
-   REAL(wp) ::  skip_time_dosurf_av = 0.0_wp              !< skip time for averaged data output
-   REAL(wp) ::  time_dosurf = 0.0_wp                      !< internal counter variable to check for instantaneous data output
-   REAL(wp) ::  time_dosurf_av = 0.0_wp                   !< internal counter variable to check for averaged data output
-
-   
-   TYPE(surf_out) ::  surfaces      !< variable which contains all required output information
-   
-   SAVE
-
-   PRIVATE
-
-   INTERFACE  surface_output
-      MODULE PROCEDURE surface_output
-   END INTERFACE  surface_output
-   
-   INTERFACE  surface_output_averaging
-      MODULE PROCEDURE surface_output_averaging
-   END INTERFACE  surface_output_averaging
-   
-   INTERFACE  surface_output_check_parameters
-      MODULE PROCEDURE surface_output_check_parameters
-   END INTERFACE  surface_output_check_parameters
-   
-   INTERFACE  surface_output_init
-      MODULE PROCEDURE surface_output_init
-   END INTERFACE  surface_output_init
-   
-   INTERFACE  surface_output_last_action
-      MODULE PROCEDURE surface_output_last_action
-   END INTERFACE  surface_output_last_action
-      
-   INTERFACE  surface_output_parin
-      MODULE PROCEDURE surface_output_parin
-   END INTERFACE  surface_output_parin
-
-!
-!--Public subroutines
-   PUBLIC surface_output, surface_output_averaging,                            &
-          surface_output_check_parameters, surface_output_init,                &
-          surface_output_last_action, surface_output_parin
-!
-!--Public variables
-   PUBLIC average_count_surf, averaging_interval_surf, dt_dosurf, dt_dosurf_av,&
-          skip_time_dosurf, skip_time_dosurf_av, time_dosurf, time_dosurf_av
-
- CONTAINS
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Initialization surface-data output data structure: calculation of vertices
-!> and polygon data for the surface elements, allocation of required arrays.
-!------------------------------------------------------------------------------!
-   SUBROUTINE surface_output_init
-   
-      IMPLICIT NONE
-
-      INTEGER(iwp) ::  i                 !< grid index in x-direction, also running variable for counting non-average data output
-      INTEGER(iwp) ::  j                 !< grid index in y-direction, also running variable for counting average data output
-      INTEGER(iwp) ::  k                 !< grid index in z-direction
-      INTEGER(iwp) ::  l                 !< running index for surface-element orientation
-      INTEGER(iwp) ::  m                 !< running index for surface elements
-      INTEGER(iwp) ::  npg               !< counter variable for all surface elements ( or polygons )
-      INTEGER(iwp) ::  point_index_count !< local counter variable for point index
-      
-      INTEGER(iwp), DIMENSION(0:numprocs-1) :: num_points_on_pe   !< array which contains the number of points on all mpi ranks
-      INTEGER(iwp), ALLOCATABLE, DIMENSION(:,:,:) ::  point_index !< dummy array used to check where the reference points for surface polygons are located
-           
-!
-!--   Determine the number of surface elements on subdomain
-      surfaces%ns = surf_def_h(0)%ns + surf_lsm_h%ns + surf_usm_h%ns           & !horizontal upward-facing
-                  + surf_def_h(1)%ns                                           & !horizontal downard-facing    
-                  + surf_def_v(0)%ns + surf_lsm_v(0)%ns + surf_usm_v(0)%ns     & !northward-facing
-                  + surf_def_v(1)%ns + surf_lsm_v(1)%ns + surf_usm_v(1)%ns     & !southward-facing   
-                  + surf_def_v(2)%ns + surf_lsm_v(2)%ns + surf_usm_v(2)%ns     & !westward-facing   
-                  + surf_def_v(3)%ns + surf_lsm_v(3)%ns + surf_usm_v(3)%ns       !eastward-facing   
-!
-!--   Allocate output variable and set to _FillValue attribute
-      ALLOCATE ( surfaces%var_out(1:surfaces%ns) )
-      surfaces%var_out = surfaces%fillvalue
-!
-!--   If there is an output of time average output variables, allocate the 
-!--   required array.
-      IF ( dosurf_no(1) > 0 )  THEN
-         ALLOCATE ( surfaces%var_av(1:surfaces%ns,1:dosurf_no(1)) )
-         surfaces%var_av = 0.0_wp
-      ENDIF
-!
-!--   Initialize points and polygon data. 
-!--   In a first step, count the number of points which are defining 
-!--   the surfaces and the polygons.
-      ALLOCATE( point_index(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
-      point_index = -1
-!
-!--   Horizontal default surfaces
-      surfaces%npoints = 0
-      DO  l = 0, 1
-         DO  m = 1, surf_def_h(0)%ns
-!
-!--         Determine the indices of the respective grid cell inside the topography
-            i = surf_def_h(0)%i(m) + surf_def_h(0)%ioff
-            j = surf_def_h(0)%j(m) + surf_def_h(0)%joff
-            k = surf_def_h(0)%k(m) + surf_def_h(0)%koff
-!
-!--         Check if the vertices that define the surface element are already 
-!--         defined, if not, increment the counter.
-            IF ( point_index(k,j,i) < 0 )  THEN
-               surfaces%npoints   = surfaces%npoints + 1    
-               point_index(k,j,i) = surfaces%npoints - 1
-            ENDIF
-            IF ( point_index(k,j,i+1) < 0 )  THEN
-               surfaces%npoints     = surfaces%npoints + 1    
-               point_index(k,j,i+1) = surfaces%npoints - 1
-            ENDIF
-            IF ( point_index(k,j+1,i+1) < 0 )  THEN
-               surfaces%npoints       = surfaces%npoints + 1    
-               point_index(k,j+1,i+1) = surfaces%npoints - 1
-            ENDIF
-            IF ( point_index(k,j+1,i) < 0 )  THEN
-               surfaces%npoints     = surfaces%npoints + 1    
-               point_index(k,j+1,i) = surfaces%npoints - 1
-            ENDIF           
-         ENDDO
-      ENDDO
-      DO  m = 1, surf_lsm_h%ns
-         i = surf_lsm_h%i(m) + surf_lsm_h%ioff
-         j = surf_lsm_h%j(m) + surf_lsm_h%joff
-         k = surf_lsm_h%k(m) + surf_lsm_h%koff
-
-         IF ( point_index(k,j,i) < 0 )  THEN
-            surfaces%npoints   = surfaces%npoints + 1    
-            point_index(k,j,i) = surfaces%npoints - 1
-         ENDIF
-         IF ( point_index(k,j,i+1) < 0 )  THEN
-            surfaces%npoints     = surfaces%npoints + 1    
-            point_index(k,j,i+1) = surfaces%npoints - 1
-         ENDIF
-         IF ( point_index(k,j+1,i+1) < 0 )  THEN
-            surfaces%npoints       = surfaces%npoints + 1    
-            point_index(k,j+1,i+1) = surfaces%npoints - 1
-         ENDIF
-         IF ( point_index(k,j+1,i) < 0 )  THEN
-            surfaces%npoints     = surfaces%npoints + 1    
-            point_index(k,j+1,i) = surfaces%npoints - 1
-         ENDIF    
-      ENDDO
-      DO  m = 1, surf_usm_h%ns
-         i = surf_usm_h%i(m) + surf_usm_h%ioff
-         j = surf_usm_h%j(m) + surf_usm_h%joff
-         k = surf_usm_h%k(m) + surf_usm_h%koff
-
-         IF ( point_index(k,j,i) < 0 )  THEN
-            surfaces%npoints   = surfaces%npoints + 1    
-            point_index(k,j,i) = surfaces%npoints - 1
-         ENDIF
-         IF ( point_index(k,j,i+1) < 0 )  THEN
-            surfaces%npoints     = surfaces%npoints + 1    
-            point_index(k,j,i+1) = surfaces%npoints - 1
-         ENDIF
-         IF ( point_index(k,j+1,i+1) < 0 )  THEN
-            surfaces%npoints       = surfaces%npoints + 1    
-            point_index(k,j+1,i+1) = surfaces%npoints - 1
-         ENDIF
-         IF ( point_index(k,j+1,i) < 0 )  THEN
-            surfaces%npoints     = surfaces%npoints + 1    
-            point_index(k,j+1,i) = surfaces%npoints - 1
-         ENDIF     
-      ENDDO
-!
-!--   Vertical surfaces
-      DO  l = 0, 3
-         DO  m = 1, surf_def_v(l)%ns
-!
-!--         Determine the indices of the respective grid cell inside the topography.
-!--         Please note, j-index for north-facing surfaces ( l==0 ) is 
-!--         identical to the reference j-index outside the grid box. 
-!--         Equivalent for east-facing surfaces and i-index.
-            i = surf_def_v(l)%i(m) + MERGE( surf_def_v(l)%ioff, 0, l == 3 )
-            j = surf_def_v(l)%j(m) + MERGE( surf_def_v(l)%joff, 0, l == 1 )
-            k = surf_def_v(l)%k(m) + surf_def_v(l)%koff
-!
-!--         Lower left /front vertex
-            IF ( point_index(k,j,i) < 0 )  THEN
-               surfaces%npoints   = surfaces%npoints + 1    
-               point_index(k,j,i) = surfaces%npoints - 1
-            ENDIF 
-!
-!--         Upper / lower right index for north- and south-facing surfaces
-            IF ( l == 0  .OR.  l == 1 )  THEN
-               IF ( point_index(k,j,i+1) < 0 )  THEN
-                  surfaces%npoints     = surfaces%npoints + 1    
-                  point_index(k,j,i+1) = surfaces%npoints - 1
-               ENDIF 
-               IF ( point_index(k+1,j,i+1) < 0 )  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j,i+1) = surfaces%npoints - 1
-               ENDIF
-!
-!--         Upper / lower front index for east- and west-facing surfaces
-            ELSEIF ( l == 2  .OR.  l == 3 )  THEN
-               IF ( point_index(k,j+1,i) < 0 )  THEN
-                  surfaces%npoints     = surfaces%npoints + 1    
-                  point_index(k,j+1,i) = surfaces%npoints - 1
-               ENDIF 
-               IF ( point_index(k+1,j+1,i) < 0 )  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j+1,i) = surfaces%npoints - 1
-               ENDIF
-            ENDIF
-!
-!--         Upper left / front vertex
-            IF ( point_index(k+1,j,i) < 0 )  THEN
-               surfaces%npoints     = surfaces%npoints + 1    
-               point_index(k+1,j,i) = surfaces%npoints - 1
-            ENDIF
-         ENDDO
-         DO  m = 1, surf_lsm_v(l)%ns
-            i = surf_lsm_v(l)%i(m) + MERGE( surf_lsm_v(l)%ioff, 0, l == 3 )
-            j = surf_lsm_v(l)%j(m) + MERGE( surf_lsm_v(l)%joff, 0, l == 1 )
-            k = surf_lsm_v(l)%k(m) + surf_lsm_v(l)%koff
-!
-!--         Lower left /front vertex
-            IF ( point_index(k,j,i) < 0 )  THEN
-               surfaces%npoints   = surfaces%npoints + 1    
-               point_index(k,j,i) = surfaces%npoints - 1
-            ENDIF 
-!
-!--         Upper / lower right index for north- and south-facing surfaces
-            IF ( l == 0  .OR.  l == 1 )  THEN
-               IF ( point_index(k,j,i+1) < 0 )  THEN
-                  surfaces%npoints     = surfaces%npoints + 1    
-                  point_index(k,j,i+1) = surfaces%npoints - 1
-               ENDIF 
-               IF ( point_index(k+1,j,i+1) < 0 )  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j,i+1) = surfaces%npoints - 1
-               ENDIF
-!
-!--         Upper / lower front index for east- and west-facing surfaces
-            ELSEIF ( l == 2  .OR.  l == 3 )  THEN
-               IF ( point_index(k,j+1,i) < 0 )  THEN
-                  surfaces%npoints     = surfaces%npoints + 1    
-                  point_index(k,j+1,i) = surfaces%npoints - 1
-               ENDIF 
-               IF ( point_index(k+1,j+1,i) < 0 )  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j+1,i) = surfaces%npoints - 1
-               ENDIF
-            ENDIF
-!
-!--         Upper left / front vertex
-            IF ( point_index(k+1,j,i) < 0 )  THEN
-               surfaces%npoints     = surfaces%npoints + 1    
-               point_index(k+1,j,i) = surfaces%npoints - 1
-            ENDIF
-         ENDDO
-
-         DO  m = 1, surf_usm_v(l)%ns
-            i = surf_usm_v(l)%i(m) + MERGE( surf_usm_v(l)%ioff, 0, l == 3 )
-            j = surf_usm_v(l)%j(m) + MERGE( surf_usm_v(l)%joff, 0, l == 1 )
-            k = surf_usm_v(l)%k(m) + surf_usm_v(l)%koff
-!
-!--         Lower left /front vertex
-            IF ( point_index(k,j,i) < 0 )  THEN
-               surfaces%npoints   = surfaces%npoints + 1    
-               point_index(k,j,i) = surfaces%npoints - 1
-            ENDIF 
-!
-!--         Upper / lower right index for north- and south-facing surfaces
-            IF ( l == 0  .OR.  l == 1 )  THEN
-               IF ( point_index(k,j,i+1) < 0 )  THEN
-                  surfaces%npoints     = surfaces%npoints + 1    
-                  point_index(k,j,i+1) = surfaces%npoints - 1
-               ENDIF 
-               IF ( point_index(k+1,j,i+1) < 0 )  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j,i+1) = surfaces%npoints - 1
-               ENDIF
-!
-!--         Upper / lower front index for east- and west-facing surfaces
-            ELSEIF ( l == 2  .OR.  l == 3 )  THEN
-               IF ( point_index(k,j+1,i) < 0 )  THEN
-                  surfaces%npoints     = surfaces%npoints + 1    
-                  point_index(k,j+1,i) = surfaces%npoints - 1
-               ENDIF 
-               IF ( point_index(k+1,j+1,i) < 0 )  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j+1,i) = surfaces%npoints - 1
-               ENDIF
-            ENDIF
-!
-!--         Upper left / front vertex
-            IF ( point_index(k+1,j,i) < 0 )  THEN
-               surfaces%npoints     = surfaces%npoints + 1    
-               point_index(k+1,j,i) = surfaces%npoints - 1
-            ENDIF
-         ENDDO
-
-      ENDDO
-!
-!--   Allocate the number of points and polygons. Note, the number of polygons
-!--   is identical to the number of surfaces elements, whereas the number
-!--   of points (vertices) which define the polygons can be larger.
-      ALLOCATE( surfaces%points(3,1:surfaces%npoints) )
-      ALLOCATE( surfaces%polygons(5,1:surfaces%ns)    )
-!
-!--   Note, PARAVIEW expects consecutively ordered points which can be
-!--   unambiguously identified. 
-!--   Hence, all PEs know where they should start counting, depending on the
-!--   number of points on the other PE's with lower MPI rank. 
-#if defined( __parallel )
-      CALL MPI_ALLGATHER( surfaces%npoints, 1, MPI_INTEGER,                    &
-                          num_points_on_pe, 1, MPI_INTEGER, comm2d, ierr  )
-#else
-      num_points_on_pe = surfaces%npoints
-#endif
-
-!
-!--   After the number of vertices is counted, repeat the loops and define the
-!--   vertices. Start with the horizontal default surfaces
-!--   First, however, determine the offset where couting of points should be
-!--   started, which is the sum of points of all PE's with lower MPI rank.
-      i                 = 0
-      point_index_count = 0
-      DO WHILE ( i < myid  .AND.  i <= SIZE( num_points_on_pe ) )
-         point_index_count = point_index_count + num_points_on_pe(i)
-         i                 = i + 1
-      ENDDO
-          
-      
-      surfaces%npoints = 0
-      point_index      = -1
-      npg              = 0
-      
-      DO  l = 0, 1
-         DO  m = 1, surf_def_h(0)%ns
-!
-!--         Determine the indices of the respective grid cell inside the topography
-            i = surf_def_h(0)%i(m) + surf_def_h(0)%ioff
-            j = surf_def_h(0)%j(m) + surf_def_h(0)%joff
-            k = surf_def_h(0)%k(m) + surf_def_h(0)%koff
-!
-!--         Check if the vertices that define the surface element are already 
-!--         defined, if not, increment the counter.
-            IF ( point_index(k,j,i) < 0 )  THEN
-               surfaces%npoints   = surfaces%npoints + 1    
-               point_index(k,j,i) = point_index_count
-               point_index_count  = point_index_count + 1               
-               surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
-               surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
-               surfaces%points(3,surfaces%npoints) = zw(k)
-            ENDIF
-            IF ( point_index(k,j,i+1) < 0 )  THEN
-               surfaces%npoints     = surfaces%npoints + 1    
-               point_index(k,j,i+1) = point_index_count
-               point_index_count    = point_index_count + 1 
-               surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
-               surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
-               surfaces%points(3,surfaces%npoints) = zw(k)
-            ENDIF
-            IF ( point_index(k,j+1,i+1) < 0 )  THEN
-               surfaces%npoints       = surfaces%npoints + 1    
-               point_index(k,j+1,i+1) = point_index_count
-               point_index_count      = point_index_count + 1 
-               surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
-               surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
-               surfaces%points(3,surfaces%npoints) = zw(k)
-            ENDIF
-            IF ( point_index(k,j+1,i) < 0 )  THEN
-               surfaces%npoints     = surfaces%npoints + 1    
-               point_index(k,j+1,i) = point_index_count
-               point_index_count    = point_index_count + 1 
-               surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
-               surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
-               surfaces%points(3,surfaces%npoints) = zw(k)
-            ENDIF
-            
-            npg                        = npg + 1
-            surfaces%polygons(1,npg)   = 4
-            surfaces%polygons(2,npg)   = point_index(k,j,i)
-            surfaces%polygons(3,npg)   = point_index(k,j,i+1)
-            surfaces%polygons(4,npg)   = point_index(k,j+1,i+1)
-            surfaces%polygons(5,npg)   = point_index(k,j+1,i)
-         ENDDO
-      ENDDO
-      DO  m = 1, surf_lsm_h%ns
-         i = surf_lsm_h%i(m) + surf_lsm_h%ioff
-         j = surf_lsm_h%j(m) + surf_lsm_h%joff
-         k = surf_lsm_h%k(m) + surf_lsm_h%koff
-         IF ( point_index(k,j,i) < 0 )  THEN
-            surfaces%npoints   = surfaces%npoints + 1    
-            point_index(k,j,i) = point_index_count
-            point_index_count  = point_index_count + 1 
-            surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
-            surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
-            surfaces%points(3,surfaces%npoints) = zw(k)
-         ENDIF
-         IF ( point_index(k,j,i+1) < 0 )  THEN
-            surfaces%npoints     = surfaces%npoints + 1    
-            point_index(k,j,i+1) = point_index_count
-            point_index_count    = point_index_count + 1 
-            surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
-            surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
-            surfaces%points(3,surfaces%npoints) = zw(k)
-         ENDIF
-         IF ( point_index(k,j+1,i+1) < 0 )  THEN
-            surfaces%npoints       = surfaces%npoints + 1    
-            point_index(k,j+1,i+1) = point_index_count
-            point_index_count      = point_index_count + 1 
-            surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
-            surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
-            surfaces%points(3,surfaces%npoints) = zw(k)
-         ENDIF
-         IF ( point_index(k,j+1,i) < 0 )  THEN
-            surfaces%npoints     = surfaces%npoints + 1    
-            point_index(k,j+1,i) = point_index_count
-            point_index_count    = point_index_count + 1 
-            surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
-            surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
-            surfaces%points(3,surfaces%npoints) = zw(k)
-         ENDIF
-         
-         npg                        = npg + 1
-         surfaces%polygons(1,npg)   = 4
-         surfaces%polygons(2,npg)   = point_index(k,j,i)
-         surfaces%polygons(3,npg)   = point_index(k,j,i+1)
-         surfaces%polygons(4,npg)   = point_index(k,j+1,i+1)
-         surfaces%polygons(5,npg)   = point_index(k,j+1,i) 
-      ENDDO
-
-      DO  m = 1, surf_usm_h%ns
-         i = surf_usm_h%i(m) + surf_usm_h%ioff
-         j = surf_usm_h%j(m) + surf_usm_h%joff
-         k = surf_usm_h%k(m) + surf_usm_h%koff
-
-         IF ( point_index(k,j,i) < 0 )  THEN
-            surfaces%npoints   = surfaces%npoints + 1    
-            point_index(k,j,i) = point_index_count
-            point_index_count  = point_index_count + 1 
-            surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
-            surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
-            surfaces%points(3,surfaces%npoints) = zw(k)
-         ENDIF
-         IF ( point_index(k,j,i+1) < 0 )  THEN
-            surfaces%npoints     = surfaces%npoints + 1    
-            point_index(k,j,i+1) = point_index_count
-            point_index_count    = point_index_count + 1 
-            surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
-            surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
-            surfaces%points(3,surfaces%npoints) = zw(k)
-         ENDIF
-         IF ( point_index(k,j+1,i+1) < 0 )  THEN
-            surfaces%npoints       = surfaces%npoints + 1    
-            point_index(k,j+1,i+1) = point_index_count
-            point_index_count      = point_index_count + 1 
-            surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
-            surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
-            surfaces%points(3,surfaces%npoints) = zw(k)
-         ENDIF
-         IF ( point_index(k,j+1,i) < 0 )  THEN
-            surfaces%npoints     = surfaces%npoints + 1    
-            point_index(k,j+1,i) = point_index_count
-            point_index_count    = point_index_count + 1 
-            surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
-            surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
-            surfaces%points(3,surfaces%npoints) = zw(k)
-         ENDIF
-         
-         npg                        = npg + 1
-         surfaces%polygons(1,npg)   = 4
-         surfaces%polygons(2,npg)   = point_index(k,j,i)
-         surfaces%polygons(3,npg)   = point_index(k,j,i+1)
-         surfaces%polygons(4,npg)   = point_index(k,j+1,i+1)
-         surfaces%polygons(5,npg)   = point_index(k,j+1,i)    
-      ENDDO
-
-      DO  l = 0, 3
-         DO  m = 1, surf_def_v(l)%ns
-!
-!--         Determine the indices of the respective grid cell inside the topography.
-!--         Please note, j-index for north-facing surfaces ( l==0 ) is 
-!--         identical to the reference j-index outside the grid box. 
-!--         Equivalent for east-facing surfaces and i-index.
-            i = surf_def_v(l)%i(m) + MERGE( surf_def_v(l)%ioff, 0, l == 3 )
-            j = surf_def_v(l)%j(m) + MERGE( surf_def_v(l)%joff, 0, l == 1 )
-            k = surf_def_v(l)%k(m) + surf_def_v(l)%koff
-!
-!--         Lower left /front vertex
-            IF ( point_index(k,j,i) < 0 )  THEN
-               surfaces%npoints   = surfaces%npoints + 1    
-               point_index(k,j,i) = point_index_count
-               point_index_count  = point_index_count + 1 
-               surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
-               surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
-               surfaces%points(3,surfaces%npoints) = zw(k-1)
-            ENDIF 
-!
-!--         Upper / lower right index for north- and south-facing surfaces
-            IF ( l == 0  .OR.  l == 1 )  THEN
-               IF ( point_index(k,j,i+1) < 0 )  THEN
-                  surfaces%npoints     = surfaces%npoints + 1    
-                  point_index(k,j,i+1) = point_index_count
-                  point_index_count    = point_index_count + 1
-                  surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
-                  surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
-                  surfaces%points(3,surfaces%npoints) = zw(k-1)
-               ENDIF 
-               IF ( point_index(k+1,j,i+1) < 0 )  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j,i+1) = point_index_count
-                  point_index_count      = point_index_count + 1
-                  surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
-                  surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
-                  surfaces%points(3,surfaces%npoints) = zw(k)
-               ENDIF
-!
-!--         Upper / lower front index for east- and west-facing surfaces
-            ELSEIF ( l == 2  .OR.  l == 3 )  THEN
-               IF ( point_index(k,j+1,i) < 0 )  THEN
-                  surfaces%npoints     = surfaces%npoints + 1    
-                  point_index(k,j+1,i) = point_index_count
-                  point_index_count    = point_index_count + 1
-                  surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
-                  surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
-                  surfaces%points(3,surfaces%npoints) = zw(k-1)
-               ENDIF 
-               IF ( point_index(k+1,j+1,i) < 0 )  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j+1,i) = point_index_count
-                  point_index_count      = point_index_count + 1
-                  surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
-                  surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
-                  surfaces%points(3,surfaces%npoints) = zw(k)
-               ENDIF
-            ENDIF
-!
-!--         Upper left / front vertex
-            IF ( point_index(k+1,j,i) < 0 )  THEN
-               surfaces%npoints     = surfaces%npoints + 1    
-               point_index(k+1,j,i) = point_index_count
-               point_index_count    = point_index_count + 1
-               surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
-               surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
-               surfaces%points(3,surfaces%npoints) = zw(k)
-            ENDIF
-            
-            npg = npg + 1            
-            IF ( l == 0  .OR.  l == 1 )  THEN
-               surfaces%polygons(1,npg)   = 4
-               surfaces%polygons(2,npg)   = point_index(k,j,i)
-               surfaces%polygons(3,npg)   = point_index(k,j,i+1)
-               surfaces%polygons(4,npg)   = point_index(k+1,j,i+1)
-               surfaces%polygons(5,npg)   = point_index(k+1,j,i)  
-            ELSE
-               surfaces%polygons(1,npg)   = 4
-               surfaces%polygons(2,npg)   = point_index(k,j,i)
-               surfaces%polygons(3,npg)   = point_index(k,j+1,i)
-               surfaces%polygons(4,npg)   = point_index(k+1,j+1,i)
-               surfaces%polygons(5,npg)   = point_index(k+1,j,i)
-            ENDIF
-            
-         ENDDO
-
-         DO  m = 1, surf_lsm_v(l)%ns
-            i = surf_lsm_v(l)%i(m) + MERGE( surf_lsm_v(l)%ioff, 0, l == 3 )
-            j = surf_lsm_v(l)%j(m) + MERGE( surf_lsm_v(l)%joff, 0, l == 1 )
-            k = surf_lsm_v(l)%k(m) + surf_lsm_v(l)%koff
-!
-!--         Lower left /front vertex
-            IF ( point_index(k,j,i) < 0 )  THEN
-               surfaces%npoints   = surfaces%npoints + 1    
-               point_index(k,j,i) = point_index_count
-               point_index_count  = point_index_count + 1
-               surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
-               surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
-               surfaces%points(3,surfaces%npoints) = zw(k-1)
-            ENDIF 
-!
-!--         Upper / lower right index for north- and south-facing surfaces
-            IF ( l == 0  .OR.  l == 1 )  THEN
-               IF ( point_index(k,j,i+1) < 0 )  THEN
-                  surfaces%npoints     = surfaces%npoints + 1    
-                  point_index(k,j,i+1) = point_index_count
-                  point_index_count    = point_index_count + 1
-                  surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
-                  surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
-                  surfaces%points(3,surfaces%npoints) = zw(k-1)
-               ENDIF 
-               IF ( point_index(k+1,j,i+1) < 0 )  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j,i+1) = point_index_count
-                  point_index_count      = point_index_count + 1
-                  surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
-                  surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
-                  surfaces%points(3,surfaces%npoints) = zw(k)
-               ENDIF
-!
-!--         Upper / lower front index for east- and west-facing surfaces
-            ELSEIF ( l == 2  .OR.  l == 3 )  THEN
-               IF ( point_index(k,j+1,i) < 0 )  THEN
-                  surfaces%npoints     = surfaces%npoints + 1    
-                  point_index(k,j+1,i) = point_index_count
-                  point_index_count    = point_index_count + 1
-                  surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
-                  surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
-                  surfaces%points(3,surfaces%npoints) = zw(k-1)
-               ENDIF 
-               IF ( point_index(k+1,j+1,i) < 0 )  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j+1,i) = point_index_count
-                  point_index_count      = point_index_count + 1
-                  surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
-                  surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
-                  surfaces%points(3,surfaces%npoints) = zw(k)
-               ENDIF
-            ENDIF
-!
-!--         Upper left / front vertex
-            IF ( point_index(k+1,j,i) < 0 )  THEN
-               surfaces%npoints     = surfaces%npoints + 1    
-               point_index(k+1,j,i) = point_index_count
-               point_index_count    = point_index_count + 1
-               surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
-               surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
-               surfaces%points(3,surfaces%npoints) = zw(k)
-            ENDIF
-            
-            npg = npg + 1            
-            IF ( l == 0  .OR.  l == 1 )  THEN
-               surfaces%polygons(1,npg)   = 4
-               surfaces%polygons(2,npg)   = point_index(k,j,i)
-               surfaces%polygons(3,npg)   = point_index(k,j,i+1)
-               surfaces%polygons(4,npg)   = point_index(k+1,j,i+1)
-               surfaces%polygons(5,npg)   = point_index(k+1,j,i)  
-            ELSE
-               surfaces%polygons(1,npg)   = 4
-               surfaces%polygons(2,npg)   = point_index(k,j,i)
-               surfaces%polygons(3,npg)   = point_index(k,j+1,i)
-               surfaces%polygons(4,npg)   = point_index(k+1,j+1,i)
-               surfaces%polygons(5,npg)   = point_index(k+1,j,i)
-            ENDIF
-         ENDDO
-         DO  m = 1, surf_usm_v(l)%ns
-            i = surf_usm_v(l)%i(m) + MERGE( surf_usm_v(l)%ioff, 0, l == 3 )
-            j = surf_usm_v(l)%j(m) + MERGE( surf_usm_v(l)%joff, 0, l == 1 )
-            k = surf_usm_v(l)%k(m) + surf_usm_v(l)%koff
-!
-!--         Lower left /front vertex
-            IF ( point_index(k,j,i) < 0 )  THEN
-               surfaces%npoints   = surfaces%npoints + 1    
-               point_index(k,j,i) = point_index_count
-               point_index_count  = point_index_count + 1
-               surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
-               surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
-               surfaces%points(3,surfaces%npoints) = zw(k-1)
-            ENDIF 
-!
-!--         Upper / lower right index for north- and south-facing surfaces
-            IF ( l == 0  .OR.  l == 1 )  THEN
-               IF ( point_index(k,j,i+1) < 0 )  THEN
-                  surfaces%npoints     = surfaces%npoints + 1    
-                  point_index(k,j,i+1) = point_index_count
-                  point_index_count    = point_index_count + 1
-                  surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
-                  surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
-                  surfaces%points(3,surfaces%npoints) = zw(k-1)
-               ENDIF 
-               IF ( point_index(k+1,j,i+1) < 0 )  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j,i+1) = point_index_count
-                  point_index_count      = point_index_count + 1
-                  surfaces%points(1,surfaces%npoints) = ( i + 1 - 0.5_wp ) * dx
-                  surfaces%points(2,surfaces%npoints) = ( j     - 0.5_wp ) * dy
-                  surfaces%points(3,surfaces%npoints) = zw(k)
-               ENDIF
-!
-!--         Upper / lower front index for east- and west-facing surfaces
-            ELSEIF ( l == 2  .OR.  l == 3 )  THEN
-               IF ( point_index(k,j+1,i) < 0 )  THEN
-                  surfaces%npoints     = surfaces%npoints + 1    
-                  point_index(k,j+1,i) = point_index_count
-                  point_index_count    = point_index_count + 1
-                  surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
-                  surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
-                  surfaces%points(3,surfaces%npoints) = zw(k-1)
-               ENDIF 
-               IF ( point_index(k+1,j+1,i) < 0 )  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j+1,i) = point_index_count
-                  point_index_count      = point_index_count + 1
-                  surfaces%points(1,surfaces%npoints) = ( i     - 0.5_wp ) * dx
-                  surfaces%points(2,surfaces%npoints) = ( j + 1 - 0.5_wp ) * dy
-                  surfaces%points(3,surfaces%npoints) = zw(k)
-               ENDIF
-            ENDIF
-!
-!--         Upper left / front vertex
-            IF ( point_index(k+1,j,i) < 0 )  THEN
-               surfaces%npoints     = surfaces%npoints + 1    
-               point_index(k+1,j,i) = point_index_count
-               point_index_count    = point_index_count + 1
-               surfaces%points(1,surfaces%npoints) = ( i - 0.5_wp ) * dx
-               surfaces%points(2,surfaces%npoints) = ( j - 0.5_wp ) * dy
-               surfaces%points(3,surfaces%npoints) = zw(k)
-            ENDIF
-            
-            npg = npg + 1            
-            IF ( l == 0  .OR.  l == 1 )  THEN
-               surfaces%polygons(1,npg)   = 4
-               surfaces%polygons(2,npg)   = point_index(k,j,i)
-               surfaces%polygons(3,npg)   = point_index(k,j,i+1)
-               surfaces%polygons(4,npg)   = point_index(k+1,j,i+1)
-               surfaces%polygons(5,npg)   = point_index(k+1,j,i)  
-            ELSE
-               surfaces%polygons(1,npg)   = 4
-               surfaces%polygons(2,npg)   = point_index(k,j,i)
-               surfaces%polygons(3,npg)   = point_index(k,j+1,i)
-               surfaces%polygons(4,npg)   = point_index(k+1,j+1,i)
-               surfaces%polygons(5,npg)   = point_index(k+1,j,i)
-            ENDIF
-         ENDDO
-
-      ENDDO
-!
-!--   Deallocate temporary dummy variable
-      DEALLOCATE ( point_index )
-!
-!--   Sum-up total number of surface elements and vertices on domain. This
-!--   will be needed for post-processing. 
-      surfaces%npoints_total = 0
-#if defined( __parallel )
-       CALL MPI_ALLREDUCE( surfaces%npoints, surfaces%npoints_total, 1,        &
-                           MPI_INTEGER, MPI_SUM, comm2d, ierr )
-       CALL MPI_ALLREDUCE( surfaces%ns, surfaces%ns_total, 1,                  &
-                           MPI_INTEGER, MPI_SUM, comm2d, ierr )
-#else
-       surfaces%npoints_total = surfaces%npoints
-       surfaces%ns_total      = surfaces%ns
-#endif
-
-   END SUBROUTINE surface_output_init
-   
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Routine for controlling the data output. Surface data is collected from 
-!> different types of surfaces (default, natural, urban) and different 
-!> orientation and written to one 1D-output array. Further, NetCDF routines
-!> are called to write the surface data in the respective NetCDF files.
-!------------------------------------------------------------------------------!   
-   SUBROUTINE surface_output( av )
-   
-      USE control_parameters,                                                  &
-          ONLY:  io_blocks, io_group, time_since_reference_point
-
-      USE pegrid,                                                              &
-          ONLY:  comm2d, ierr
-
-   
-      IMPLICIT NONE
-
-      CHARACTER(LEN=100) ::  trimvar = ' ' !< dummy for single output variable
-      
-      INTEGER(iwp) ::  av     !< id indicating average or non-average data output
-      INTEGER(iwp) ::  i      !< loop index
-      INTEGER(iwp) ::  n_out  !< counter variables for surface output
-
-!
-!--   Return, if nothing to output
-      IF ( dosurf_no(av) == 0 )  RETURN
-!
-!--   Open files
-      CALL check_open( 25+av )
-!
-!--   Write coordinates 
-      IF ( .NOT. first_output(av) )  THEN
-         DO  i = 0, io_blocks-1
-            IF ( i == io_group )  THEN
-               WRITE ( 25+av )  surfaces%npoints
-               WRITE ( 25+av )  surfaces%npoints_total
-               WRITE ( 25+av )  surfaces%ns
-               WRITE ( 25+av )  surfaces%ns_total
-               WRITE ( 25+av )  surfaces%points
-               WRITE ( 25+av )  surfaces%polygons
-            ENDIF
-#if defined( __parallel )
-            CALL MPI_BARRIER( comm2d, ierr )
-#endif
-            first_output(av) = .TRUE.
-         ENDDO
-      ENDIF
-
-      n_out = 0
-      DO  WHILE ( dosurf(av,n_out+1)(1:1) /= ' ' )
-
-         n_out   = n_out + 1
-         trimvar = TRIM( dosurf(av,n_out) )
-!
-!--      Set the output array to the _FillValue in case it is not 
-!--      defined for each type of surface.
-         surfaces%var_out = surfaces%fillvalue
-         SELECT CASE ( trimvar )
-
-            CASE ( 'us' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%us,               &
-                                               surf_def_h(1)%us,               &
-                                               surf_lsm_h%us,                  &
-                                               surf_usm_h%us,                  &
-                                               surf_def_v(0)%us,               &
-                                               surf_lsm_v(0)%us,               &
-                                               surf_usm_v(0)%us,               &
-                                               surf_def_v(1)%us,               &
-                                               surf_lsm_v(1)%us,               &
-                                               surf_usm_v(1)%us,               &
-                                               surf_def_v(2)%us,               &
-                                               surf_lsm_v(2)%us,               &
-                                               surf_usm_v(2)%us,               &
-                                               surf_def_v(3)%us,               &
-                                               surf_lsm_v(3)%us,               &
-                                               surf_usm_v(3)%us ) 
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'ts' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%ts,               &
-                                               surf_def_h(1)%ts,               &
-                                               surf_lsm_h%ts,                  &
-                                               surf_usm_h%ts,                  &
-                                               surf_def_v(0)%ts,               &
-                                               surf_lsm_v(0)%ts,               &
-                                               surf_usm_v(0)%ts,               &
-                                               surf_def_v(1)%ts,               &
-                                               surf_lsm_v(1)%ts,               &
-                                               surf_usm_v(1)%ts,               &
-                                               surf_def_v(2)%ts,               &
-                                               surf_lsm_v(2)%ts,               &
-                                               surf_usm_v(2)%ts,               &
-                                               surf_def_v(3)%ts,               &
-                                               surf_lsm_v(3)%ts,               &
-                                               surf_usm_v(3)%ts ) 
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'qs' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%qs,               &
-                                               surf_def_h(1)%qs,               &
-                                               surf_lsm_h%qs,                  &
-                                               surf_usm_h%qs,                  &
-                                               surf_def_v(0)%qs,               &
-                                               surf_lsm_v(0)%qs,               &
-                                               surf_usm_v(0)%qs,               &
-                                               surf_def_v(1)%qs,               &
-                                               surf_lsm_v(1)%qs,               &
-                                               surf_usm_v(1)%qs,               &
-                                               surf_def_v(2)%qs,               &
-                                               surf_lsm_v(2)%qs,               &
-                                               surf_usm_v(2)%qs,               &
-                                               surf_def_v(3)%qs,               &
-                                               surf_lsm_v(3)%qs,               &
-                                               surf_usm_v(3)%qs ) 
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'ss' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%ss,               &
-                                               surf_def_h(1)%ss,               &
-                                               surf_lsm_h%ss,                  &
-                                               surf_usm_h%ss,                  &
-                                               surf_def_v(0)%ss,               &
-                                               surf_lsm_v(0)%ss,               &
-                                               surf_usm_v(0)%ss,               &
-                                               surf_def_v(1)%ss,               &
-                                               surf_lsm_v(1)%ss,               &
-                                               surf_usm_v(1)%ss,               &
-                                               surf_def_v(2)%ss,               &
-                                               surf_lsm_v(2)%ss,               &
-                                               surf_usm_v(2)%ss,               &
-                                               surf_def_v(3)%ss,               &
-                                               surf_lsm_v(3)%ss,               &
-                                               surf_usm_v(3)%ss ) 
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'qcs' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%qcs,              &
-                                               surf_def_h(1)%qcs,              &
-                                               surf_lsm_h%qcs,                 &
-                                               surf_usm_h%qcs,                 &
-                                               surf_def_v(0)%qcs,              &
-                                               surf_lsm_v(0)%qcs,              &
-                                               surf_usm_v(0)%qcs,              &
-                                               surf_def_v(1)%qcs,              &
-                                               surf_lsm_v(1)%qcs,              &
-                                               surf_usm_v(1)%qcs,              &
-                                               surf_def_v(2)%qcs,              &
-                                               surf_lsm_v(2)%qcs,              &
-                                               surf_usm_v(2)%qcs,              &
-                                               surf_def_v(3)%qcs,              &
-                                               surf_lsm_v(3)%qcs,              &
-                                               surf_usm_v(3)%qcs ) 
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'ncs' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%ncs,              &
-                                               surf_def_h(1)%ncs,              &
-                                               surf_lsm_h%ncs,                 &
-                                               surf_usm_h%ncs,                 &
-                                               surf_def_v(0)%ncs,              &
-                                               surf_lsm_v(0)%ncs,              &
-                                               surf_usm_v(0)%ncs,              &
-                                               surf_def_v(1)%ncs,              &
-                                               surf_lsm_v(1)%ncs,              &
-                                               surf_usm_v(1)%ncs,              &
-                                               surf_def_v(2)%ncs,              &
-                                               surf_lsm_v(2)%ncs,              &
-                                               surf_usm_v(2)%ncs,              &
-                                               surf_def_v(3)%ncs,              &
-                                               surf_lsm_v(3)%ncs,              &
-                                               surf_usm_v(3)%ncs ) 
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'qrs' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%qrs,              &
-                                               surf_def_h(1)%qrs,              &
-                                               surf_lsm_h%qrs,                 &
-                                               surf_usm_h%qrs,                 &
-                                               surf_def_v(0)%qrs,              &
-                                               surf_lsm_v(0)%qrs,              &
-                                               surf_usm_v(0)%qrs,              &
-                                               surf_def_v(1)%qrs,              &
-                                               surf_lsm_v(1)%qrs,              &
-                                               surf_usm_v(1)%qrs,              &
-                                               surf_def_v(2)%qrs,              &
-                                               surf_lsm_v(2)%qrs,              &
-                                               surf_usm_v(2)%qrs,              &
-                                               surf_def_v(3)%qrs,              &
-                                               surf_lsm_v(3)%qrs,              &
-                                               surf_usm_v(3)%qrs ) 
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'nrs' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%nrs,              &
-                                               surf_def_h(1)%nrs,              &
-                                               surf_lsm_h%nrs,                 &
-                                               surf_usm_h%nrs,                 &
-                                               surf_def_v(0)%nrs,              &
-                                               surf_lsm_v(0)%nrs,              &
-                                               surf_usm_v(0)%nrs,              &
-                                               surf_def_v(1)%nrs,              &
-                                               surf_lsm_v(1)%nrs,              &
-                                               surf_usm_v(1)%nrs,              &
-                                               surf_def_v(2)%nrs,              &
-                                               surf_lsm_v(2)%nrs,              &
-                                               surf_usm_v(2)%nrs,              &
-                                               surf_def_v(3)%nrs,              &
-                                               surf_lsm_v(3)%nrs,              &
-                                               surf_usm_v(3)%nrs ) 
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'ol' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%ol,               &
-                                               surf_def_h(1)%ol,               &
-                                               surf_lsm_h%ol,                  &
-                                               surf_usm_h%ol,                  &
-                                               surf_def_v(0)%ol,               &
-                                               surf_lsm_v(0)%ol,               &
-                                               surf_usm_v(0)%ol,               &
-                                               surf_def_v(1)%ol,               &
-                                               surf_lsm_v(1)%ol,               &
-                                               surf_usm_v(1)%ol,               &
-                                               surf_def_v(2)%ol,               &
-                                               surf_lsm_v(2)%ol,               &
-                                               surf_usm_v(2)%ol,               &
-                                               surf_def_v(3)%ol,               &
-                                               surf_lsm_v(3)%ol,               &
-                                               surf_usm_v(3)%ol ) 
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'z0' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%z0,               &
-                                               surf_def_h(1)%z0,               &
-                                               surf_lsm_h%z0,                  &
-                                               surf_usm_h%z0,                  &
-                                               surf_def_v(0)%z0,               &
-                                               surf_lsm_v(0)%z0,               &
-                                               surf_usm_v(0)%z0,               &
-                                               surf_def_v(1)%z0,               &
-                                               surf_lsm_v(1)%z0,               &
-                                               surf_usm_v(1)%z0,               &
-                                               surf_def_v(2)%z0,               &
-                                               surf_lsm_v(2)%z0,               &
-                                               surf_usm_v(2)%z0,               &
-                                               surf_def_v(3)%z0,               &
-                                               surf_lsm_v(3)%z0,               &
-                                               surf_usm_v(3)%z0 )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'z0h' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%z0h,              &
-                                               surf_def_h(1)%z0h,              &
-                                               surf_lsm_h%z0h,                 &
-                                               surf_usm_h%z0h,                 &
-                                               surf_def_v(0)%z0h,              &
-                                               surf_lsm_v(0)%z0h,              &
-                                               surf_usm_v(0)%z0h,              &
-                                               surf_def_v(1)%z0h,              &
-                                               surf_lsm_v(1)%z0h,              &
-                                               surf_usm_v(1)%z0h,              &
-                                               surf_def_v(2)%z0h,              &
-                                               surf_lsm_v(2)%z0h,              &
-                                               surf_usm_v(2)%z0h,              &
-                                               surf_def_v(3)%z0h,              &
-                                               surf_lsm_v(3)%z0h,              &
-                                               surf_usm_v(3)%z0h )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'z0q' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%z0q,              &
-                                               surf_def_h(1)%z0q,              &
-                                               surf_lsm_h%z0q,                 &
-                                               surf_usm_h%z0q,                 &
-                                               surf_def_v(0)%z0q,              &
-                                               surf_lsm_v(0)%z0q,              &
-                                               surf_usm_v(0)%z0q,              &
-                                               surf_def_v(1)%z0q,              &
-                                               surf_lsm_v(1)%z0q,              &
-                                               surf_usm_v(1)%z0q,              &
-                                               surf_def_v(2)%z0q,              &
-                                               surf_lsm_v(2)%z0q,              &
-                                               surf_usm_v(2)%z0q,              &
-                                               surf_def_v(3)%z0q,              &
-                                               surf_lsm_v(3)%z0q,              &
-                                               surf_usm_v(3)%z0q ) 
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-
-               ENDIF
-
-            CASE ( 'theta1' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%pt1,              &
-                                               surf_def_h(1)%pt1,              &
-                                               surf_lsm_h%pt1,                 &
-                                               surf_usm_h%pt1,                 &
-                                               surf_def_v(0)%pt1,              &
-                                               surf_lsm_v(0)%pt1,              &
-                                               surf_usm_v(0)%pt1,              &
-                                               surf_def_v(1)%pt1,              &
-                                               surf_lsm_v(1)%pt1,              &
-                                               surf_usm_v(1)%pt1,              &
-                                               surf_def_v(2)%pt1,              &
-                                               surf_lsm_v(2)%pt1,              &
-                                               surf_usm_v(2)%pt1,              &
-                                               surf_def_v(3)%pt1,              &
-                                               surf_lsm_v(3)%pt1,              &
-                                               surf_usm_v(3)%pt1 )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'qv1' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%qv1,              &
-                                               surf_def_h(1)%qv1,              &
-                                               surf_lsm_h%qv1,                 &
-                                               surf_usm_h%qv1,                 &
-                                               surf_def_v(0)%qv1,              &
-                                               surf_lsm_v(0)%qv1,              &
-                                               surf_usm_v(0)%qv1,              &
-                                               surf_def_v(1)%qv1,              &
-                                               surf_lsm_v(1)%qv1,              &
-                                               surf_usm_v(1)%qv1,              &
-                                               surf_def_v(2)%qv1,              &
-                                               surf_lsm_v(2)%qv1,              &
-                                               surf_usm_v(2)%qv1,              &
-                                               surf_def_v(3)%qv1,              &
-                                               surf_lsm_v(3)%qv1,              &
-                                               surf_usm_v(3)%qv1 )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'thetav1' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%vpt1,             &
-                                               surf_def_h(1)%vpt1,             &
-                                               surf_lsm_h%vpt1,                &
-                                               surf_usm_h%vpt1,                &
-                                               surf_def_v(0)%vpt1,             &
-                                               surf_lsm_v(0)%vpt1,             &
-                                               surf_usm_v(0)%vpt1,             &
-                                               surf_def_v(1)%vpt1,             &
-                                               surf_lsm_v(1)%vpt1,             &
-                                               surf_usm_v(1)%vpt1,             &
-                                               surf_def_v(2)%vpt1,             &
-                                               surf_lsm_v(2)%vpt1,             &
-                                               surf_usm_v(2)%vpt1,             &
-                                               surf_def_v(3)%vpt1,             &
-                                               surf_lsm_v(3)%vpt1,             &
-                                               surf_usm_v(3)%vpt1 )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'usws' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%usws,             &
-                                               surf_def_h(1)%usws,             &
-                                               surf_lsm_h%usws,                &
-                                               surf_usm_h%usws,                &
-                                               surf_def_v(0)%usws,             &
-                                               surf_lsm_v(0)%usws,             &
-                                               surf_usm_v(0)%usws,             &
-                                               surf_def_v(1)%usws,             &
-                                               surf_lsm_v(1)%usws,             &
-                                               surf_usm_v(1)%usws,             &
-                                               surf_def_v(2)%usws,             &
-                                               surf_lsm_v(2)%usws,             &
-                                               surf_usm_v(2)%usws,             &
-                                               surf_def_v(3)%usws,             &
-                                               surf_lsm_v(3)%usws,             &
-                                               surf_usm_v(3)%usws )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'vsws' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%vsws,             &
-                                               surf_def_h(1)%vsws,             &
-                                               surf_lsm_h%vsws,                &
-                                               surf_usm_h%vsws,                &
-                                               surf_def_v(0)%vsws,             &
-                                               surf_lsm_v(0)%vsws,             &
-                                               surf_usm_v(0)%vsws,             &
-                                               surf_def_v(1)%vsws,             &
-                                               surf_lsm_v(1)%vsws,             &
-                                               surf_usm_v(1)%vsws,             &
-                                               surf_def_v(2)%vsws,             &
-                                               surf_lsm_v(2)%vsws,             &
-                                               surf_usm_v(2)%vsws,             &
-                                               surf_def_v(3)%vsws,             &
-                                               surf_lsm_v(3)%vsws,             &
-                                               surf_usm_v(3)%vsws )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'shf' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%shf,              &
-                                               surf_def_h(1)%shf,              &
-                                               surf_lsm_h%shf,                 &
-                                               surf_usm_h%shf,                 &
-                                               surf_def_v(0)%shf,              &
-                                               surf_lsm_v(0)%shf,              &
-                                               surf_usm_v(0)%shf,              &
-                                               surf_def_v(1)%shf,              &
-                                               surf_lsm_v(1)%shf,              &
-                                               surf_usm_v(1)%shf,              &
-                                               surf_def_v(2)%shf,              &
-                                               surf_lsm_v(2)%shf,              &
-                                               surf_usm_v(2)%shf,              &
-                                               surf_def_v(3)%shf,              &
-                                               surf_lsm_v(3)%shf,              &
-                                               surf_usm_v(3)%shf )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-               ENDIF
-
-            CASE ( 'qsws' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%qsws,             &
-                                               surf_def_h(1)%qsws,             &
-                                               surf_lsm_h%qsws,                &
-                                               surf_usm_h%qsws,                &
-                                               surf_def_v(0)%qsws,             &
-                                               surf_lsm_v(0)%qsws,             &
-                                               surf_usm_v(0)%qsws,             &
-                                               surf_def_v(1)%qsws,             &
-                                               surf_lsm_v(1)%qsws,             &
-                                               surf_usm_v(1)%qsws,             &
-                                               surf_def_v(2)%qsws,             &
-                                               surf_lsm_v(2)%qsws,             &
-                                               surf_usm_v(2)%qsws,             &
-                                               surf_def_v(3)%qsws,             &
-                                               surf_lsm_v(3)%qsws,             &
-                                               surf_usm_v(3)%qsws )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'ssws' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%ssws,             &
-                                               surf_def_h(1)%ssws,             &
-                                               surf_lsm_h%ssws,                &
-                                               surf_usm_h%ssws,                &
-                                               surf_def_v(0)%ssws,             &
-                                               surf_lsm_v(0)%ssws,             &
-                                               surf_usm_v(0)%ssws,             &
-                                               surf_def_v(1)%ssws,             &
-                                               surf_lsm_v(1)%ssws,             &
-                                               surf_usm_v(1)%ssws,             &
-                                               surf_def_v(2)%ssws,             &
-                                               surf_lsm_v(2)%ssws,             &
-                                               surf_usm_v(2)%ssws,             &
-                                               surf_def_v(3)%ssws,             &
-                                               surf_lsm_v(3)%ssws,             &
-                                               surf_usm_v(3)%ssws )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'qcsws' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%qcsws,            &
-                                               surf_def_h(1)%qcsws,            &
-                                               surf_lsm_h%qcsws,               &
-                                               surf_usm_h%qcsws,               &
-                                               surf_def_v(0)%qcsws,            &
-                                               surf_lsm_v(0)%qcsws,            &
-                                               surf_usm_v(0)%qcsws,            &
-                                               surf_def_v(1)%qcsws,            &
-                                               surf_lsm_v(1)%qcsws,            &
-                                               surf_usm_v(1)%qcsws,            &
-                                               surf_def_v(2)%qcsws,            &
-                                               surf_lsm_v(2)%qcsws,            &
-                                               surf_usm_v(2)%qcsws,            &
-                                               surf_def_v(3)%qcsws,            &
-                                               surf_lsm_v(3)%qcsws,            &
-                                               surf_usm_v(3)%qcsws )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'ncsws' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%ncsws,            &
-                                               surf_def_h(1)%ncsws,            &
-                                               surf_lsm_h%ncsws,               &
-                                               surf_usm_h%ncsws,               &
-                                               surf_def_v(0)%ncsws,            &
-                                               surf_lsm_v(0)%ncsws,            &
-                                               surf_usm_v(0)%ncsws,            &
-                                               surf_def_v(1)%ncsws,            &
-                                               surf_lsm_v(1)%ncsws,            &
-                                               surf_usm_v(1)%ncsws,            &
-                                               surf_def_v(2)%ncsws,            &
-                                               surf_lsm_v(2)%ncsws,            &
-                                               surf_usm_v(2)%ncsws,            &
-                                               surf_def_v(3)%ncsws,            &
-                                               surf_lsm_v(3)%ncsws,            &
-                                               surf_usm_v(3)%ncsws )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'qrsws' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%qrsws,            &
-                                               surf_def_h(1)%qrsws,            &
-                                               surf_lsm_h%qrsws,               &
-                                               surf_usm_h%qrsws,               &
-                                               surf_def_v(0)%qrsws,            &
-                                               surf_lsm_v(0)%qrsws,            &
-                                               surf_usm_v(0)%qrsws,            &
-                                               surf_def_v(1)%qrsws,            &
-                                               surf_lsm_v(1)%qrsws,            &
-                                               surf_usm_v(1)%qrsws,            &
-                                               surf_def_v(2)%qrsws,            &
-                                               surf_lsm_v(2)%qrsws,            &
-                                               surf_usm_v(2)%qrsws,            &
-                                               surf_def_v(3)%qrsws,            &
-                                               surf_lsm_v(3)%qrsws,            &
-                                               surf_usm_v(3)%qrsws )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'nrsws' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%nrsws,            &
-                                               surf_def_h(1)%nrsws,            &
-                                               surf_lsm_h%nrsws,               &
-                                               surf_usm_h%nrsws,               &
-                                               surf_def_v(0)%nrsws,            &
-                                               surf_lsm_v(0)%nrsws,            &
-                                               surf_usm_v(0)%nrsws,            &
-                                               surf_def_v(1)%nrsws,            &
-                                               surf_lsm_v(1)%nrsws,            &
-                                               surf_usm_v(1)%nrsws,            &
-                                               surf_def_v(2)%nrsws,            &
-                                               surf_lsm_v(2)%nrsws,            &
-                                               surf_usm_v(2)%nrsws,            &
-                                               surf_def_v(3)%nrsws,            &
-                                               surf_lsm_v(3)%nrsws,            &
-                                               surf_usm_v(3)%nrsws )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'sasws' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%sasws,            &
-                                               surf_def_h(1)%sasws,            &
-                                               surf_lsm_h%sasws,               &
-                                               surf_usm_h%sasws,               &
-                                               surf_def_v(0)%sasws,            &
-                                               surf_lsm_v(0)%sasws,            &
-                                               surf_usm_v(0)%sasws,            &
-                                               surf_def_v(1)%sasws,            &
-                                               surf_lsm_v(1)%sasws,            &
-                                               surf_usm_v(1)%sasws,            &
-                                               surf_def_v(2)%sasws,            &
-                                               surf_lsm_v(2)%sasws,            &
-                                               surf_usm_v(2)%sasws,            &
-                                               surf_def_v(3)%sasws,            &
-                                               surf_lsm_v(3)%sasws,            &
-                                               surf_usm_v(3)%sasws )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'q_surface' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%q_surface,        &
-                                               surf_def_h(1)%q_surface,        &
-                                               surf_lsm_h%q_surface,           &
-                                               surf_usm_h%q_surface,           &
-                                               surf_def_v(0)%q_surface,        &
-                                               surf_lsm_v(0)%q_surface,        &
-                                               surf_usm_v(0)%q_surface,        &
-                                               surf_def_v(1)%q_surface,        &
-                                               surf_lsm_v(1)%q_surface,        &
-                                               surf_usm_v(1)%q_surface,        &
-                                               surf_def_v(2)%q_surface,        &
-                                               surf_lsm_v(2)%q_surface,        &
-                                               surf_usm_v(2)%q_surface,        &
-                                               surf_def_v(3)%q_surface,        &
-                                               surf_lsm_v(3)%q_surface,        &
-                                               surf_usm_v(3)%q_surface )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'theta_surface' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%pt_surface,       &
-                                               surf_def_h(1)%pt_surface,       &
-                                               surf_lsm_h%pt_surface,          &
-                                               surf_usm_h%pt_surface,          &
-                                               surf_def_v(0)%pt_surface,       &
-                                               surf_lsm_v(0)%pt_surface,       &
-                                               surf_usm_v(0)%pt_surface,       &
-                                               surf_def_v(1)%pt_surface,       &
-                                               surf_lsm_v(1)%pt_surface,       &
-                                               surf_usm_v(1)%pt_surface,       &
-                                               surf_def_v(2)%pt_surface,       &
-                                               surf_lsm_v(2)%pt_surface,       &
-                                               surf_usm_v(2)%pt_surface,       &
-                                               surf_def_v(3)%pt_surface,       &
-                                               surf_lsm_v(3)%pt_surface,       &
-                                               surf_usm_v(3)%pt_surface )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'thetav_surface' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%vpt_surface,      &
-                                               surf_def_h(1)%vpt_surface,      &
-                                               surf_lsm_h%vpt_surface,         &
-                                               surf_usm_h%vpt_surface,         &
-                                               surf_def_v(0)%vpt_surface,      &
-                                               surf_lsm_v(0)%vpt_surface,      &
-                                               surf_usm_v(0)%vpt_surface,      &
-                                               surf_def_v(1)%vpt_surface,      &
-                                               surf_lsm_v(1)%vpt_surface,      &
-                                               surf_usm_v(1)%vpt_surface,      &
-                                               surf_def_v(2)%vpt_surface,      &
-                                               surf_lsm_v(2)%vpt_surface,      &
-                                               surf_usm_v(2)%vpt_surface,      &
-                                               surf_def_v(3)%vpt_surface,      &
-                                               surf_lsm_v(3)%vpt_surface,      &
-                                               surf_usm_v(3)%vpt_surface)
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'rad_net' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%rad_net,          &
-                                               surf_def_h(1)%rad_net,          &
-                                               surf_lsm_h%rad_net,             &
-                                               surf_usm_h%rad_net,             &
-                                               surf_def_v(0)%rad_net,          &
-                                               surf_lsm_v(0)%rad_net,          &
-                                               surf_usm_v(0)%rad_net,          &
-                                               surf_def_v(1)%rad_net,          &
-                                               surf_lsm_v(1)%rad_net,          &
-                                               surf_usm_v(1)%rad_net,          &
-                                               surf_def_v(2)%rad_net,          &
-                                               surf_lsm_v(2)%rad_net,          &
-                                               surf_usm_v(2)%rad_net,          &
-                                               surf_def_v(3)%rad_net,          &
-                                               surf_lsm_v(3)%rad_net,          &
-                                               surf_usm_v(3)%rad_net ) 
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'rad_lw_in' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%rad_lw_in,        &
-                                               surf_def_h(1)%rad_lw_in,        &
-                                               surf_lsm_h%rad_lw_in,           &
-                                               surf_usm_h%rad_lw_in,           &
-                                               surf_def_v(0)%rad_lw_in,        &
-                                               surf_lsm_v(0)%rad_lw_in,        &
-                                               surf_usm_v(0)%rad_lw_in,        &
-                                               surf_def_v(1)%rad_lw_in,        &
-                                               surf_lsm_v(1)%rad_lw_in,        &
-                                               surf_usm_v(1)%rad_lw_in,        &
-                                               surf_def_v(2)%rad_lw_in,        &
-                                               surf_lsm_v(2)%rad_lw_in,        &
-                                               surf_usm_v(2)%rad_lw_in,        &
-                                               surf_def_v(3)%rad_lw_in,        &
-                                               surf_lsm_v(3)%rad_lw_in,        &
-                                               surf_usm_v(3)%rad_lw_in )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'rad_lw_out' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%rad_lw_out,       &
-                                               surf_def_h(1)%rad_lw_out,       &
-                                               surf_lsm_h%rad_lw_out,          &
-                                               surf_usm_h%rad_lw_out,          &
-                                               surf_def_v(0)%rad_lw_out,       &
-                                               surf_lsm_v(0)%rad_lw_out,       &
-                                               surf_usm_v(0)%rad_lw_out,       &
-                                               surf_def_v(1)%rad_lw_out,       &
-                                               surf_lsm_v(1)%rad_lw_out,       &
-                                               surf_usm_v(1)%rad_lw_out,       &
-                                               surf_def_v(2)%rad_lw_out,       &
-                                               surf_lsm_v(2)%rad_lw_out,       &
-                                               surf_usm_v(2)%rad_lw_out,       &
-                                               surf_def_v(3)%rad_lw_out,       &
-                                               surf_lsm_v(3)%rad_lw_out,       &
-                                               surf_usm_v(3)%rad_lw_out )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'rad_sw_in' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%rad_sw_in,        &
-                                               surf_def_h(1)%rad_sw_in,        &
-                                               surf_lsm_h%rad_sw_in,           &
-                                               surf_usm_h%rad_sw_in,           &
-                                               surf_def_v(0)%rad_sw_in,        &
-                                               surf_lsm_v(0)%rad_sw_in,        &
-                                               surf_usm_v(0)%rad_sw_in,        &
-                                               surf_def_v(1)%rad_sw_in,        &
-                                               surf_lsm_v(1)%rad_sw_in,        &
-                                               surf_usm_v(1)%rad_sw_in,        &
-                                               surf_def_v(2)%rad_sw_in,        &
-                                               surf_lsm_v(2)%rad_sw_in,        &
-                                               surf_usm_v(2)%rad_sw_in,        &
-                                               surf_def_v(3)%rad_sw_in,        &
-                                               surf_lsm_v(3)%rad_sw_in,        &
-                                               surf_usm_v(3)%rad_sw_in )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'rad_sw_out' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%rad_sw_out,       &
-                                               surf_def_h(1)%rad_sw_out,       &
-                                               surf_lsm_h%rad_sw_out,          &
-                                               surf_usm_h%rad_sw_out,          &
-                                               surf_def_v(0)%rad_sw_out,       &
-                                               surf_lsm_v(0)%rad_sw_out,       &
-                                               surf_usm_v(0)%rad_sw_out,       &
-                                               surf_def_v(1)%rad_sw_out,       &
-                                               surf_lsm_v(1)%rad_sw_out,       &
-                                               surf_usm_v(1)%rad_sw_out,       &
-                                               surf_def_v(2)%rad_sw_out,       &
-                                               surf_lsm_v(2)%rad_sw_out,       &
-                                               surf_usm_v(2)%rad_sw_out,       &
-                                               surf_def_v(3)%rad_sw_out,       &
-                                               surf_lsm_v(3)%rad_sw_out,       &
-                                               surf_usm_v(3)%rad_sw_out )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'ghf' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%ghf,              &
-                                               surf_def_h(1)%ghf,              &
-                                               surf_lsm_h%ghf,                 &
-                                               surf_usm_h%ghf,                 &
-                                               surf_def_v(0)%ghf,              &
-                                               surf_lsm_v(0)%ghf,              &
-                                               surf_usm_v(0)%ghf,              &
-                                               surf_def_v(1)%ghf,              &
-                                               surf_lsm_v(1)%ghf,              &
-                                               surf_usm_v(1)%ghf,              &
-                                               surf_def_v(2)%ghf,              &
-                                               surf_lsm_v(2)%ghf,              &
-                                               surf_usm_v(2)%ghf,              &
-                                               surf_def_v(3)%ghf,              &
-                                               surf_lsm_v(3)%ghf,              &
-                                               surf_usm_v(3)%ghf )      
-                                                                        ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-                      
-            CASE ( 'r_a' )                                                     
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%r_a,              &
-                                               surf_def_h(1)%r_a,              &
-                                               surf_lsm_h%r_a,                 &
-                                               surf_usm_h%r_a,                 &
-                                               surf_def_v(0)%r_a,              &
-                                               surf_lsm_v(0)%r_a,              &
-                                               surf_usm_v(0)%r_a,              &
-                                               surf_def_v(1)%r_a,              &
-                                               surf_lsm_v(1)%r_a,              &
-                                               surf_usm_v(1)%r_a,              &
-                                               surf_def_v(2)%r_a,              &
-                                               surf_lsm_v(2)%r_a,              &
-                                               surf_usm_v(2)%r_a,              &
-                                               surf_def_v(3)%r_a,              &
-                                               surf_lsm_v(3)%r_a,              &
-                                               surf_usm_v(3)%r_a )      
-                                                                      ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-                        
-            CASE ( 'r_soil' )                                                  
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%r_soil,           &
-                                               surf_def_h(1)%r_soil,           &
-                                               surf_lsm_h%r_soil,              &
-                                               surf_usm_h%r_soil,              &
-                                               surf_def_v(0)%r_soil,           &
-                                               surf_lsm_v(0)%r_soil,           &
-                                               surf_usm_v(0)%r_soil,           &
-                                               surf_def_v(1)%r_soil,           &
-                                               surf_lsm_v(1)%r_soil,           &
-                                               surf_usm_v(1)%r_soil,           &
-                                               surf_def_v(2)%r_soil,           &
-                                               surf_lsm_v(2)%r_soil,           &
-                                               surf_usm_v(2)%r_soil,           &
-                                               surf_def_v(3)%r_soil,           &
-                                               surf_lsm_v(3)%r_soil,           &
-                                               surf_usm_v(3)%r_soil ) 
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'r_canopy' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%r_canopy,         &
-                                               surf_def_h(1)%r_canopy,         &
-                                               surf_lsm_h%r_canopy,            &
-                                               surf_usm_h%r_canopy,            &
-                                               surf_def_v(0)%r_canopy,         &
-                                               surf_lsm_v(0)%r_canopy,         &
-                                               surf_usm_v(0)%r_canopy,         &
-                                               surf_def_v(1)%r_canopy,         &
-                                               surf_lsm_v(1)%r_canopy,         &
-                                               surf_usm_v(1)%r_canopy,         &
-                                               surf_def_v(2)%r_canopy,         &
-                                               surf_lsm_v(2)%r_canopy,         &
-                                               surf_usm_v(2)%r_canopy,         &
-                                               surf_def_v(3)%r_canopy,         &
-                                               surf_lsm_v(3)%r_canopy,         &
-                                               surf_usm_v(3)%r_canopy ) 
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'r_s' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%r_s,              &
-                                               surf_def_h(1)%r_s,              &
-                                               surf_lsm_h%r_s,                 &
-                                               surf_usm_h%r_s,                 &
-                                               surf_def_v(0)%r_s,              &
-                                               surf_lsm_v(0)%r_s,              &
-                                               surf_usm_v(0)%r_s,              &
-                                               surf_def_v(1)%r_s,              &
-                                               surf_lsm_v(1)%r_s,              &
-                                               surf_usm_v(1)%r_s,              &
-                                               surf_def_v(2)%r_s,              &
-                                               surf_lsm_v(2)%r_s,              &
-                                               surf_usm_v(2)%r_s,              &
-                                               surf_def_v(3)%r_s,              &
-                                               surf_lsm_v(3)%r_s,              &
-                                               surf_usm_v(3)%r_s ) 
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'rad_sw_dir' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%rad_sw_dir,       &
-                                               surf_def_h(1)%rad_sw_dir,       &
-                                               surf_lsm_h%rad_sw_dir,          &
-                                               surf_usm_h%rad_sw_dir,          &
-                                               surf_def_v(0)%rad_sw_dir,       &
-                                               surf_lsm_v(0)%rad_sw_dir,       &
-                                               surf_usm_v(0)%rad_sw_dir,       &
-                                               surf_def_v(1)%rad_sw_dir,       &
-                                               surf_lsm_v(1)%rad_sw_dir,       &
-                                               surf_usm_v(1)%rad_sw_dir,       &
-                                               surf_def_v(2)%rad_sw_dir,       &
-                                               surf_lsm_v(2)%rad_sw_dir,       &
-                                               surf_usm_v(2)%rad_sw_dir,       &
-                                               surf_def_v(3)%rad_sw_dir,       &
-                                               surf_lsm_v(3)%rad_sw_dir,       &
-                                               surf_usm_v(3)%rad_sw_dir )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'rad_sw_dif' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%rad_sw_dif,       &
-                                               surf_def_h(1)%rad_sw_dif,       &
-                                               surf_lsm_h%rad_sw_dif,          &
-                                               surf_usm_h%rad_sw_dif,          &
-                                               surf_def_v(0)%rad_sw_dif,       &
-                                               surf_lsm_v(0)%rad_sw_dif,       &
-                                               surf_usm_v(0)%rad_sw_dif,       &
-                                               surf_def_v(1)%rad_sw_dif,       &
-                                               surf_lsm_v(1)%rad_sw_dif,       &
-                                               surf_usm_v(1)%rad_sw_dif,       &
-                                               surf_def_v(2)%rad_sw_dif,       &
-                                               surf_lsm_v(2)%rad_sw_dif,       &
-                                               surf_usm_v(2)%rad_sw_dif,       &
-                                               surf_def_v(3)%rad_sw_dif,       &
-                                               surf_lsm_v(3)%rad_sw_dif,       &
-                                               surf_usm_v(3)%rad_sw_dif )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'rad_sw_ref' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%rad_sw_ref,       &
-                                               surf_def_h(1)%rad_sw_ref,       &
-                                               surf_lsm_h%rad_sw_ref,          &
-                                               surf_usm_h%rad_sw_ref,          &
-                                               surf_def_v(0)%rad_sw_ref,       &
-                                               surf_lsm_v(0)%rad_sw_ref,       &
-                                               surf_usm_v(0)%rad_sw_ref,       &
-                                               surf_def_v(1)%rad_sw_ref,       &
-                                               surf_lsm_v(1)%rad_sw_ref,       &
-                                               surf_usm_v(1)%rad_sw_ref,       &
-                                               surf_def_v(2)%rad_sw_ref,       &
-                                               surf_lsm_v(2)%rad_sw_ref,       &
-                                               surf_usm_v(2)%rad_sw_ref,       &
-                                               surf_def_v(3)%rad_sw_ref,       &
-                                               surf_lsm_v(3)%rad_sw_ref,       &
-                                               surf_usm_v(3)%rad_sw_ref )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'rad_sw_res' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%rad_sw_res,       &
-                                               surf_def_h(1)%rad_sw_res,       &
-                                               surf_lsm_h%rad_sw_res,          &
-                                               surf_usm_h%rad_sw_res,          &
-                                               surf_def_v(0)%rad_sw_res,       &
-                                               surf_lsm_v(0)%rad_sw_res,       &
-                                               surf_usm_v(0)%rad_sw_res,       &
-                                               surf_def_v(1)%rad_sw_res,       &
-                                               surf_lsm_v(1)%rad_sw_res,       &
-                                               surf_usm_v(1)%rad_sw_res,       &
-                                               surf_def_v(2)%rad_sw_res,       &
-                                               surf_lsm_v(2)%rad_sw_res,       &
-                                               surf_usm_v(2)%rad_sw_res,       &
-                                               surf_def_v(3)%rad_sw_res,       &
-                                               surf_lsm_v(3)%rad_sw_res,       &
-                                               surf_usm_v(3)%rad_sw_res )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'rad_lw_dif' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%rad_lw_dif,       &
-                                               surf_def_h(1)%rad_lw_dif,       &
-                                               surf_lsm_h%rad_lw_dif,          &
-                                               surf_usm_h%rad_lw_dif,          &
-                                               surf_def_v(0)%rad_lw_dif,       &
-                                               surf_lsm_v(0)%rad_lw_dif,       &
-                                               surf_usm_v(0)%rad_lw_dif,       &
-                                               surf_def_v(1)%rad_lw_dif,       &
-                                               surf_lsm_v(1)%rad_lw_dif,       &
-                                               surf_usm_v(1)%rad_lw_dif,       &
-                                               surf_def_v(2)%rad_lw_dif,       &
-                                               surf_lsm_v(2)%rad_lw_dif,       &
-                                               surf_usm_v(2)%rad_lw_dif,       &
-                                               surf_def_v(3)%rad_lw_dif,       &
-                                               surf_lsm_v(3)%rad_lw_dif,       &
-                                               surf_usm_v(3)%rad_lw_dif )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'rad_lw_ref' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%rad_lw_ref,       &
-                                               surf_def_h(1)%rad_lw_ref,       &
-                                               surf_lsm_h%rad_lw_ref,          &
-                                               surf_usm_h%rad_lw_ref,          &
-                                               surf_def_v(0)%rad_lw_ref,       &
-                                               surf_lsm_v(0)%rad_lw_ref,       &
-                                               surf_usm_v(0)%rad_lw_ref,       &
-                                               surf_def_v(1)%rad_lw_ref,       &
-                                               surf_lsm_v(1)%rad_lw_ref,       &
-                                               surf_usm_v(1)%rad_lw_ref,       &
-                                               surf_def_v(2)%rad_lw_ref,       &
-                                               surf_lsm_v(2)%rad_lw_ref,       &
-                                               surf_usm_v(2)%rad_lw_ref,       &
-                                               surf_def_v(3)%rad_lw_ref,       &
-                                               surf_lsm_v(3)%rad_lw_ref,       &
-                                               surf_usm_v(3)%rad_lw_ref )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-            CASE ( 'rad_lw_res' )
-!
-!--            Output of instantaneous data
-               IF ( av == 0 )  THEN
-                  CALL surface_output_collect( surf_def_h(0)%rad_lw_res,       &
-                                               surf_def_h(1)%rad_lw_res,       &
-                                               surf_lsm_h%rad_lw_res,          &
-                                               surf_usm_h%rad_lw_res,          &
-                                               surf_def_v(0)%rad_lw_res,       &
-                                               surf_lsm_v(0)%rad_lw_res,       &
-                                               surf_usm_v(0)%rad_lw_res,       &
-                                               surf_def_v(1)%rad_lw_res,       &
-                                               surf_lsm_v(1)%rad_lw_res,       &
-                                               surf_usm_v(1)%rad_lw_res,       &
-                                               surf_def_v(2)%rad_lw_res,       &
-                                               surf_lsm_v(2)%rad_lw_res,       &
-                                               surf_usm_v(2)%rad_lw_res,       &
-                                               surf_def_v(3)%rad_lw_res,       &
-                                               surf_lsm_v(3)%rad_lw_res,       &
-                                               surf_usm_v(3)%rad_lw_res )
-               ELSE
-!
-!--               Output of averaged data
-                  surfaces%var_out(:) = surfaces%var_av(:,n_out) /             &
-                                        REAL( average_count_surf, KIND=wp )
-                  surfaces%var_av(:,n_out) = 0.0_wp
-                                                        
-               ENDIF
-
-!
-!--            Add further variables:
-!--            'css', 'cssws', 'qsws_liq', 'qsws_soil', 'qsws_veg'
-
-         END SELECT
-!
-!--      Write to binary file: 
-!--      - surfaces%points ( 3, 1-npoints )
-!--      - surfaces%polygons ( 5, 1-ns )
-!--      - surfaces%var_out ( 1-ns, time )
-!--      - Dimension: 1-nsurfaces, 1-npoints - can be ordered consecutively
-!--      - Distinguish between average and non-average data
-         DO  i = 0, io_blocks-1
-            IF ( i == io_group )  THEN
-               WRITE ( 25+av )  LEN_TRIM( 'time' )
-               WRITE ( 25+av )  'time'
-               WRITE ( 25+av )  time_since_reference_point
-               WRITE ( 25+av )  LEN_TRIM( trimvar )
-               WRITE ( 25+av )  TRIM( trimvar )
-               WRITE ( 25+av )  surfaces%var_out
-            ENDIF
-#if defined( __parallel )
-            CALL MPI_BARRIER( comm2d, ierr )
-#endif
-         ENDDO
-
-      ENDDO
-      
-!
-!--   If averaged output was written to NetCDF file, set the counter to zero
-      IF ( av == 1 )  average_count_surf = 0
-             
-   END SUBROUTINE surface_output
-   
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Routine for controlling the data averaging. 
-!------------------------------------------------------------------------------!     
-   SUBROUTINE surface_output_averaging
-   
-      IMPLICIT NONE
-      
-      CHARACTER(LEN=100) ::  trimvar !< dummy variable for current output variable
-       
-      INTEGER(iwp) ::  n_out  !< counter variables for surface output
-      
-      n_out = 0
-      DO  WHILE ( dosurf(1,n_out+1)(1:1) /= ' ' )
-         
-         n_out   = n_out + 1
-         trimvar = TRIM( dosurf(1,n_out) )
-
-         SELECT CASE ( trimvar )
-
-            CASE ( 'us' )
-               CALL surface_output_sum_up( surf_def_h(0)%us,                   &
-                                           surf_def_h(1)%us,                   &
-                                           surf_lsm_h%us,                      &
-                                           surf_usm_h%us,                      &
-                                           surf_def_v(0)%us,                   &
-                                           surf_lsm_v(0)%us,                   &
-                                           surf_usm_v(0)%us,                   &
-                                           surf_def_v(1)%us,                   &
-                                           surf_lsm_v(1)%us,                   &
-                                           surf_usm_v(1)%us,                   &
-                                           surf_def_v(2)%us,                   &
-                                           surf_lsm_v(2)%us,                   &
-                                           surf_usm_v(2)%us,                   &
-                                           surf_def_v(3)%us,                   &
-                                           surf_lsm_v(3)%us,                   &
-                                           surf_usm_v(3)%us, n_out ) 
-
-            CASE ( 'ts' )
-               CALL surface_output_sum_up( surf_def_h(0)%ts,                   &
-                                           surf_def_h(1)%ts,                   &
-                                           surf_lsm_h%ts,                      &
-                                           surf_usm_h%ts,                      &
-                                           surf_def_v(0)%ts,                   &
-                                           surf_lsm_v(0)%ts,                   &
-                                           surf_usm_v(0)%ts,                   &
-                                           surf_def_v(1)%ts,                   &
-                                           surf_lsm_v(1)%ts,                   &
-                                           surf_usm_v(1)%ts,                   &
-                                           surf_def_v(2)%ts,                   &
-                                           surf_lsm_v(2)%ts,                   &
-                                           surf_usm_v(2)%ts,                   &
-                                           surf_def_v(3)%ts,                   &
-                                           surf_lsm_v(3)%ts,                   &
-                                           surf_usm_v(3)%ts, n_out ) 
-
-            CASE ( 'qs' )
-               CALL surface_output_sum_up( surf_def_h(0)%qs,                   &
-                                           surf_def_h(1)%qs,                   &
-                                           surf_lsm_h%qs,                      &
-                                           surf_usm_h%qs,                      &
-                                           surf_def_v(0)%qs,                   &
-                                           surf_lsm_v(0)%qs,                   &
-                                           surf_usm_v(0)%qs,                   &
-                                           surf_def_v(1)%qs,                   &
-                                           surf_lsm_v(1)%qs,                   &
-                                           surf_usm_v(1)%qs,                   &
-                                           surf_def_v(2)%qs,                   &
-                                           surf_lsm_v(2)%qs,                   &
-                                           surf_usm_v(2)%qs,                   &
-                                           surf_def_v(3)%qs,                   &
-                                           surf_lsm_v(3)%qs,                   &
-                                           surf_usm_v(3)%qs, n_out ) 
-
-            CASE ( 'ss' )
-               CALL surface_output_sum_up( surf_def_h(0)%ss,                   &
-                                           surf_def_h(1)%ss,                   &
-                                           surf_lsm_h%ss,                      &
-                                           surf_usm_h%ss,                      &
-                                           surf_def_v(0)%ss,                   &
-                                           surf_lsm_v(0)%ss,                   &
-                                           surf_usm_v(0)%ss,                   &
-                                           surf_def_v(1)%ss,                   &
-                                           surf_lsm_v(1)%ss,                   &
-                                           surf_usm_v(1)%ss,                   &
-                                           surf_def_v(2)%ss,                   &
-                                           surf_lsm_v(2)%ss,                   &
-                                           surf_usm_v(2)%ss,                   &
-                                           surf_def_v(3)%ss,                   &
-                                           surf_lsm_v(3)%ss,                   &
-                                           surf_usm_v(3)%ss, n_out ) 
-
-            CASE ( 'qcs' )
-               CALL surface_output_sum_up( surf_def_h(0)%qcs,                  &
-                                           surf_def_h(1)%qcs,                  &
-                                           surf_lsm_h%qcs,                     &
-                                           surf_usm_h%qcs,                     &
-                                           surf_def_v(0)%qcs,                  &
-                                           surf_lsm_v(0)%qcs,                  &
-                                           surf_usm_v(0)%qcs,                  &
-                                           surf_def_v(1)%qcs,                  &
-                                           surf_lsm_v(1)%qcs,                  &
-                                           surf_usm_v(1)%qcs,                  &
-                                           surf_def_v(2)%qcs,                  &
-                                           surf_lsm_v(2)%qcs,                  &
-                                           surf_usm_v(2)%qcs,                  &
-                                           surf_def_v(3)%qcs,                  &
-                                           surf_lsm_v(3)%qcs,                  &
-                                           surf_usm_v(3)%qcs, n_out ) 
-
-            CASE ( 'ncs' )
-               CALL surface_output_sum_up( surf_def_h(0)%ncs,                  &
-                                           surf_def_h(1)%ncs,                  &
-                                           surf_lsm_h%ncs,                     &
-                                           surf_usm_h%ncs,                     &
-                                           surf_def_v(0)%ncs,                  &
-                                           surf_lsm_v(0)%ncs,                  &
-                                           surf_usm_v(0)%ncs,                  &
-                                           surf_def_v(1)%ncs,                  &
-                                           surf_lsm_v(1)%ncs,                  &
-                                           surf_usm_v(1)%ncs,                  &
-                                           surf_def_v(2)%ncs,                  &
-                                           surf_lsm_v(2)%ncs,                  &
-                                           surf_usm_v(2)%ncs,                  &
-                                           surf_def_v(3)%ncs,                  &
-                                           surf_lsm_v(3)%ncs,                  &
-                                           surf_usm_v(3)%ncs, n_out ) 
-
-            CASE ( 'qrs' )
-               CALL surface_output_sum_up( surf_def_h(0)%qrs,                  &
-                                           surf_def_h(1)%qrs,                  &
-                                           surf_lsm_h%qrs,                     &
-                                           surf_usm_h%qrs,                     &
-                                           surf_def_v(0)%qrs,                  &
-                                           surf_lsm_v(0)%qrs,                  &
-                                           surf_usm_v(0)%qrs,                  &
-                                           surf_def_v(1)%qrs,                  &
-                                           surf_lsm_v(1)%qrs,                  &
-                                           surf_usm_v(1)%qrs,                  &
-                                           surf_def_v(2)%qrs,                  &
-                                           surf_lsm_v(2)%qrs,                  &
-                                           surf_usm_v(2)%qrs,                  &
-                                           surf_def_v(3)%qrs,                  &
-                                           surf_lsm_v(3)%qrs,                  &
-                                           surf_usm_v(3)%qrs, n_out ) 
-
-            CASE ( 'nrs' )
-               CALL surface_output_sum_up( surf_def_h(0)%nrs,                  &
-                                           surf_def_h(1)%nrs,                  &
-                                           surf_lsm_h%nrs,                     &
-                                           surf_usm_h%nrs,                     &
-                                           surf_def_v(0)%nrs,                  &
-                                           surf_lsm_v(0)%nrs,                  &
-                                           surf_usm_v(0)%nrs,                  &
-                                           surf_def_v(1)%nrs,                  &
-                                           surf_lsm_v(1)%nrs,                  &
-                                           surf_usm_v(1)%nrs,                  &
-                                           surf_def_v(2)%nrs,                  &
-                                           surf_lsm_v(2)%nrs,                  &
-                                           surf_usm_v(2)%nrs,                  &
-                                           surf_def_v(3)%nrs,                  &
-                                           surf_lsm_v(3)%nrs,                  &
-                                           surf_usm_v(3)%nrs, n_out ) 
-
-            CASE ( 'ol' )
-               CALL surface_output_sum_up( surf_def_h(0)%ol,                   &
-                                           surf_def_h(1)%ol,                   &
-                                           surf_lsm_h%ol,                      &
-                                           surf_usm_h%ol,                      &
-                                           surf_def_v(0)%ol,                   &
-                                           surf_lsm_v(0)%ol,                   &
-                                           surf_usm_v(0)%ol,                   &
-                                           surf_def_v(1)%ol,                   &
-                                           surf_lsm_v(1)%ol,                   &
-                                           surf_usm_v(1)%ol,                   &
-                                           surf_def_v(2)%ol,                   &
-                                           surf_lsm_v(2)%ol,                   &
-                                           surf_usm_v(2)%ol,                   &
-                                           surf_def_v(3)%ol,                   &
-                                           surf_lsm_v(3)%ol,                   &
-                                           surf_usm_v(3)%ol, n_out ) 
-
-            CASE ( 'z0' )
-               CALL surface_output_sum_up( surf_def_h(0)%z0,                   &
-                                           surf_def_h(1)%z0,                   &
-                                           surf_lsm_h%z0,                      &
-                                           surf_usm_h%z0,                      &
-                                           surf_def_v(0)%z0,                   &
-                                           surf_lsm_v(0)%z0,                   &
-                                           surf_usm_v(0)%z0,                   &
-                                           surf_def_v(1)%z0,                   &
-                                           surf_lsm_v(1)%z0,                   &
-                                           surf_usm_v(1)%z0,                   &
-                                           surf_def_v(2)%z0,                   &
-                                           surf_lsm_v(2)%z0,                   &
-                                           surf_usm_v(2)%z0,                   &
-                                           surf_def_v(3)%z0,                   &
-                                           surf_lsm_v(3)%z0,                   &
-                                           surf_usm_v(3)%z0, n_out ) 
-
-            CASE ( 'z0h' )
-               CALL surface_output_sum_up( surf_def_h(0)%z0h,                  &
-                                           surf_def_h(1)%z0h,                  &
-                                           surf_lsm_h%z0h,                     &
-                                           surf_usm_h%z0h,                     &
-                                           surf_def_v(0)%z0h,                  &
-                                           surf_lsm_v(0)%z0h,                  &
-                                           surf_usm_v(0)%z0h,                  &
-                                           surf_def_v(1)%z0h,                  &
-                                           surf_lsm_v(1)%z0h,                  &
-                                           surf_usm_v(1)%z0h,                  &
-                                           surf_def_v(2)%z0h,                  &
-                                           surf_lsm_v(2)%z0h,                  &
-                                           surf_usm_v(2)%z0h,                  &
-                                           surf_def_v(3)%z0h,                  &
-                                           surf_lsm_v(3)%z0h,                  &
-                                           surf_usm_v(3)%z0h, n_out )          
-
-            CASE ( 'z0q' )
-               CALL surface_output_sum_up( surf_def_h(0)%z0q,                  &
-                                           surf_def_h(1)%z0q,                  &
-                                           surf_lsm_h%z0q,                     &
-                                           surf_usm_h%z0q,                     &
-                                           surf_def_v(0)%z0q,                  &
-                                           surf_lsm_v(0)%z0q,                  &
-                                           surf_usm_v(0)%z0q,                  &
-                                           surf_def_v(1)%z0q,                  &
-                                           surf_lsm_v(1)%z0q,                  &
-                                           surf_usm_v(1)%z0q,                  &
-                                           surf_def_v(2)%z0q,                  &
-                                           surf_lsm_v(2)%z0q,                  &
-                                           surf_usm_v(2)%z0q,                  &
-                                           surf_def_v(3)%z0q,                  &
-                                           surf_lsm_v(3)%z0q,                  &
-                                           surf_usm_v(3)%z0q, n_out ) 
-
-            CASE ( 'theta1' )                                                  
-               CALL surface_output_sum_up( surf_def_h(0)%pt1,                  &
-                                           surf_def_h(1)%pt1,                  &
-                                           surf_lsm_h%pt1,                     &
-                                           surf_usm_h%pt1,                     &
-                                           surf_def_v(0)%pt1,                  &
-                                           surf_lsm_v(0)%pt1,                  &
-                                           surf_usm_v(0)%pt1,                  &
-                                           surf_def_v(1)%pt1,                  &
-                                           surf_lsm_v(1)%pt1,                  &
-                                           surf_usm_v(1)%pt1,                  &
-                                           surf_def_v(2)%pt1,                  &
-                                           surf_lsm_v(2)%pt1,                  &
-                                           surf_usm_v(2)%pt1,                  &
-                                           surf_def_v(3)%pt1,                  &
-                                           surf_lsm_v(3)%pt1,                  &
-                                           surf_usm_v(3)%pt1, n_out )          
-                                                                               
-            CASE ( 'qv1' )                                                     
-               CALL surface_output_sum_up( surf_def_h(0)%qv1,                  &
-                                           surf_def_h(1)%qv1,                  &
-                                           surf_lsm_h%qv1,                     &
-                                           surf_usm_h%qv1,                     &
-                                           surf_def_v(0)%qv1,                  &
-                                           surf_lsm_v(0)%qv1,                  &
-                                           surf_usm_v(0)%qv1,                  &
-                                           surf_def_v(1)%qv1,                  &
-                                           surf_lsm_v(1)%qv1,                  &
-                                           surf_usm_v(1)%qv1,                  &
-                                           surf_def_v(2)%qv1,                  &
-                                           surf_lsm_v(2)%qv1,                  &
-                                           surf_usm_v(2)%qv1,                  &
-                                           surf_def_v(3)%qv1,                  &
-                                           surf_lsm_v(3)%qv1,                  &
-                                           surf_usm_v(3)%qv1, n_out ) 
-
-            CASE ( 'thetav1' )
-               CALL surface_output_sum_up( surf_def_h(0)%vpt1,                 &
-                                           surf_def_h(1)%vpt1,                 &
-                                           surf_lsm_h%vpt1,                    &
-                                           surf_usm_h%vpt1,                    &
-                                           surf_def_v(0)%vpt1,                 &
-                                           surf_lsm_v(0)%vpt1,                 &
-                                           surf_usm_v(0)%vpt1,                 &
-                                           surf_def_v(1)%vpt1,                 &
-                                           surf_lsm_v(1)%vpt1,                 &
-                                           surf_usm_v(1)%vpt1,                 &
-                                           surf_def_v(2)%vpt1,                 &
-                                           surf_lsm_v(2)%vpt1,                 &
-                                           surf_usm_v(2)%vpt1,                 &
-                                           surf_def_v(3)%vpt1,                 &
-                                           surf_lsm_v(3)%vpt1,                 &
-                                           surf_usm_v(3)%vpt1, n_out ) 
-
-            CASE ( 'usws' )
-               CALL surface_output_sum_up( surf_def_h(0)%usws,                 &
-                                           surf_def_h(1)%usws,                 &
-                                           surf_lsm_h%usws,                    &
-                                           surf_usm_h%usws,                    &
-                                           surf_def_v(0)%usws,                 &
-                                           surf_lsm_v(0)%usws,                 &
-                                           surf_usm_v(0)%usws,                 &
-                                           surf_def_v(1)%usws,                 &
-                                           surf_lsm_v(1)%usws,                 &
-                                           surf_usm_v(1)%usws,                 &
-                                           surf_def_v(2)%usws,                 &
-                                           surf_lsm_v(2)%usws,                 &
-                                           surf_usm_v(2)%usws,                 &
-                                           surf_def_v(3)%usws,                 &
-                                           surf_lsm_v(3)%usws,                 &
-                                           surf_usm_v(3)%usws, n_out ) 
-
-            CASE ( 'vsws' )
-               CALL surface_output_sum_up( surf_def_h(0)%vsws,                 &
-                                           surf_def_h(1)%vsws,                 &
-                                           surf_lsm_h%vsws,                    &
-                                           surf_usm_h%vsws,                    &
-                                           surf_def_v(0)%vsws,                 &
-                                           surf_lsm_v(0)%vsws,                 &
-                                           surf_usm_v(0)%vsws,                 &
-                                           surf_def_v(1)%vsws,                 &
-                                           surf_lsm_v(1)%vsws,                 &
-                                           surf_usm_v(1)%vsws,                 &
-                                           surf_def_v(2)%vsws,                 &
-                                           surf_lsm_v(2)%vsws,                 &
-                                           surf_usm_v(2)%vsws,                 &
-                                           surf_def_v(3)%vsws,                 &
-                                           surf_lsm_v(3)%vsws,                 &
-                                           surf_usm_v(3)%vsws, n_out ) 
-
-            CASE ( 'shf' )
-               CALL surface_output_sum_up( surf_def_h(0)%shf,                  &
-                                           surf_def_h(1)%shf,                  &
-                                           surf_lsm_h%shf,                     &
-                                           surf_usm_h%shf,                     &  
-                                           surf_def_v(0)%shf,                  &
-                                           surf_lsm_v(0)%shf,                  &
-                                           surf_usm_v(0)%shf,                  &
-                                           surf_def_v(1)%shf,                  &
-                                           surf_lsm_v(1)%shf,                  &
-                                           surf_usm_v(1)%shf,                  &
-                                           surf_def_v(2)%shf,                  &
-                                           surf_lsm_v(2)%shf,                  &
-                                           surf_usm_v(2)%shf,                  &
-                                           surf_def_v(3)%shf,                  &
-                                           surf_lsm_v(3)%shf,                  &
-                                           surf_usm_v(3)%shf, n_out )
-
-            CASE ( 'qsws' )
-               CALL surface_output_sum_up( surf_def_h(0)%qsws,                 &
-                                           surf_def_h(1)%qsws,                 &
-                                           surf_lsm_h%qsws,                    &
-                                           surf_usm_h%qsws,                    &
-                                           surf_def_v(0)%qsws,                 &
-                                           surf_lsm_v(0)%qsws,                 &
-                                           surf_usm_v(0)%qsws,                 &
-                                           surf_def_v(1)%qsws,                 &
-                                           surf_lsm_v(1)%qsws,                 &
-                                           surf_usm_v(1)%qsws,                 &
-                                           surf_def_v(2)%qsws,                 &
-                                           surf_lsm_v(2)%qsws,                 &
-                                           surf_usm_v(2)%qsws,                 &
-                                           surf_def_v(3)%qsws,                 &
-                                           surf_lsm_v(3)%qsws,                 &
-                                           surf_usm_v(3)%qsws, n_out ) 
-
-            CASE ( 'ssws' )
-               CALL surface_output_sum_up( surf_def_h(0)%ssws,                 &
-                                           surf_def_h(1)%ssws,                 &
-                                           surf_lsm_h%ssws,                    &
-                                           surf_usm_h%ssws,                    &
-                                           surf_def_v(0)%ssws,                 &
-                                           surf_lsm_v(0)%ssws,                 &
-                                           surf_usm_v(0)%ssws,                 &
-                                           surf_def_v(1)%ssws,                 &
-                                           surf_lsm_v(1)%ssws,                 &
-                                           surf_usm_v(1)%ssws,                 &
-                                           surf_def_v(2)%ssws,                 &
-                                           surf_lsm_v(2)%ssws,                 &
-                                           surf_usm_v(2)%ssws,                 &
-                                           surf_def_v(3)%ssws,                 &
-                                           surf_lsm_v(3)%ssws,                 &
-                                           surf_usm_v(3)%ssws, n_out ) 
-
-            CASE ( 'qcsws' )
-               CALL surface_output_sum_up( surf_def_h(0)%qcsws,                &
-                                           surf_def_h(1)%qcsws,                &
-                                           surf_lsm_h%qcsws,                   &
-                                           surf_usm_h%qcsws,                   &
-                                           surf_def_v(0)%qcsws,                &
-                                           surf_lsm_v(0)%qcsws,                &
-                                           surf_usm_v(0)%qcsws,                &
-                                           surf_def_v(1)%qcsws,                &
-                                           surf_lsm_v(1)%qcsws,                &
-                                           surf_usm_v(1)%qcsws,                &
-                                           surf_def_v(2)%qcsws,                &
-                                           surf_lsm_v(2)%qcsws,                &
-                                           surf_usm_v(2)%qcsws,                &
-                                           surf_def_v(3)%qcsws,                &
-                                           surf_lsm_v(3)%qcsws,                &
-                                           surf_usm_v(3)%qcsws, n_out ) 
-
-            CASE ( 'ncsws' )
-               CALL surface_output_sum_up( surf_def_h(0)%ncsws,                &
-                                           surf_def_h(1)%ncsws,                &
-                                           surf_lsm_h%ncsws,                   &
-                                           surf_usm_h%ncsws,                   &
-                                           surf_def_v(0)%ncsws,                &
-                                           surf_lsm_v(0)%ncsws,                &
-                                           surf_usm_v(0)%ncsws,                &
-                                           surf_def_v(1)%ncsws,                &
-                                           surf_lsm_v(1)%ncsws,                &
-                                           surf_usm_v(1)%ncsws,                &
-                                           surf_def_v(2)%ncsws,                &
-                                           surf_lsm_v(2)%ncsws,                &
-                                           surf_usm_v(2)%ncsws,                &
-                                           surf_def_v(3)%ncsws,                &
-                                           surf_lsm_v(3)%ncsws,                &
-                                           surf_usm_v(3)%ncsws, n_out ) 
-
-            CASE ( 'qrsws' )
-               CALL surface_output_sum_up( surf_def_h(0)%qrsws,                &
-                                           surf_def_h(1)%qrsws,                &
-                                           surf_lsm_h%qrsws,                   &
-                                           surf_usm_h%qrsws,                   &
-                                           surf_def_v(0)%qrsws,                &
-                                           surf_lsm_v(0)%qrsws,                &
-                                           surf_usm_v(0)%qrsws,                &
-                                           surf_def_v(1)%qrsws,                &
-                                           surf_lsm_v(1)%qrsws,                &
-                                           surf_usm_v(1)%qrsws,                &
-                                           surf_def_v(2)%qrsws,                &
-                                           surf_lsm_v(2)%qrsws,                &
-                                           surf_usm_v(2)%qrsws,                &
-                                           surf_def_v(3)%qrsws,                &
-                                           surf_lsm_v(3)%qrsws,                &
-                                           surf_usm_v(3)%qrsws, n_out ) 
-
-            CASE ( 'nrsws' )
-               CALL surface_output_sum_up( surf_def_h(0)%nrsws,                &
-                                           surf_def_h(1)%nrsws,                &
-                                           surf_lsm_h%nrsws,                   &
-                                           surf_usm_h%nrsws,                   &
-                                           surf_def_v(0)%nrsws,                &
-                                           surf_lsm_v(0)%nrsws,                &
-                                           surf_usm_v(0)%nrsws,                &
-                                           surf_def_v(1)%nrsws,                &
-                                           surf_lsm_v(1)%nrsws,                &
-                                           surf_usm_v(1)%nrsws,                &
-                                           surf_def_v(2)%nrsws,                &
-                                           surf_lsm_v(2)%nrsws,                &
-                                           surf_usm_v(2)%nrsws,                &
-                                           surf_def_v(3)%nrsws,                &
-                                           surf_lsm_v(3)%nrsws,                &
-                                           surf_usm_v(3)%nrsws, n_out ) 
-
-            CASE ( 'sasws' )
-               CALL surface_output_sum_up( surf_def_h(0)%sasws,                &
-                                           surf_def_h(1)%sasws,                &
-                                           surf_lsm_h%sasws,                   &
-                                           surf_usm_h%sasws,                   &
-                                           surf_def_v(0)%sasws,                &
-                                           surf_lsm_v(0)%sasws,                &
-                                           surf_usm_v(0)%sasws,                &
-                                           surf_def_v(1)%sasws,                &
-                                           surf_lsm_v(1)%sasws,                &
-                                           surf_usm_v(1)%sasws,                &
-                                           surf_def_v(2)%sasws,                &
-                                           surf_lsm_v(2)%sasws,                &
-                                           surf_usm_v(2)%sasws,                &
-                                           surf_def_v(3)%sasws,                &
-                                           surf_lsm_v(3)%sasws,                &
-                                           surf_usm_v(3)%sasws, n_out ) 
-
-            CASE ( 'q_surface' )
-               CALL surface_output_sum_up( surf_def_h(0)%q_surface,            &
-                                           surf_def_h(1)%q_surface,            &
-                                           surf_lsm_h%q_surface,               &
-                                           surf_usm_h%q_surface,               &
-                                           surf_def_v(0)%q_surface,            &
-                                           surf_lsm_v(0)%q_surface,            &
-                                           surf_usm_v(0)%q_surface,            &
-                                           surf_def_v(1)%q_surface,            &
-                                           surf_lsm_v(1)%q_surface,            &
-                                           surf_usm_v(1)%q_surface,            &
-                                           surf_def_v(2)%q_surface,            &
-                                           surf_lsm_v(2)%q_surface,            &
-                                           surf_usm_v(2)%q_surface,            &
-                                           surf_def_v(3)%q_surface,            &
-                                           surf_lsm_v(3)%q_surface,            &
-                                           surf_usm_v(3)%q_surface, n_out ) 
-
-            CASE ( 'theta_surface' )
-               CALL surface_output_sum_up( surf_def_h(0)%pt_surface,           &
-                                           surf_def_h(1)%pt_surface,           &
-                                           surf_lsm_h%pt_surface,              &
-                                           surf_usm_h%pt_surface,              &
-                                           surf_def_v(0)%pt_surface,           &
-                                           surf_lsm_v(0)%pt_surface,           &
-                                           surf_usm_v(0)%pt_surface,           &
-                                           surf_def_v(1)%pt_surface,           &
-                                           surf_lsm_v(1)%pt_surface,           &
-                                           surf_usm_v(1)%pt_surface,           &
-                                           surf_def_v(2)%pt_surface,           &
-                                           surf_lsm_v(2)%pt_surface,           &
-                                           surf_usm_v(2)%pt_surface,           &
-                                           surf_def_v(3)%pt_surface,           &
-                                           surf_lsm_v(3)%pt_surface,           &
-                                           surf_usm_v(3)%pt_surface, n_out ) 
-
-            CASE ( 'thetav_surface' )
-               CALL surface_output_sum_up( surf_def_h(0)%vpt_surface,          &
-                                           surf_def_h(1)%vpt_surface,          &
-                                           surf_lsm_h%vpt_surface,             &
-                                           surf_usm_h%vpt_surface,             &
-                                           surf_def_v(0)%vpt_surface,          &
-                                           surf_lsm_v(0)%vpt_surface,          &
-                                           surf_usm_v(0)%vpt_surface,          &
-                                           surf_def_v(1)%vpt_surface,          &
-                                           surf_lsm_v(1)%vpt_surface,          &
-                                           surf_usm_v(1)%vpt_surface,          &
-                                           surf_def_v(2)%vpt_surface,          &
-                                           surf_lsm_v(2)%vpt_surface,          &
-                                           surf_usm_v(2)%vpt_surface,          &
-                                           surf_def_v(3)%vpt_surface,          &
-                                           surf_lsm_v(3)%vpt_surface,          &
-                                           surf_usm_v(3)%vpt_surface, n_out ) 
-
-            CASE ( 'rad_net' )
-               CALL surface_output_sum_up( surf_def_h(0)%rad_net,              &
-                                           surf_def_h(1)%rad_net,              &
-                                           surf_lsm_h%rad_net,                 &
-                                           surf_usm_h%rad_net,                 &
-                                           surf_def_v(0)%rad_net,              &
-                                           surf_lsm_v(0)%rad_net,              &
-                                           surf_usm_v(0)%rad_net,              &
-                                           surf_def_v(1)%rad_net,              &
-                                           surf_lsm_v(1)%rad_net,              &
-                                           surf_usm_v(1)%rad_net,              &
-                                           surf_def_v(2)%rad_net,              &
-                                           surf_lsm_v(2)%rad_net,              &
-                                           surf_usm_v(2)%rad_net,              &
-                                           surf_def_v(3)%rad_net,              &
-                                           surf_lsm_v(3)%rad_net,              &
-                                           surf_usm_v(3)%rad_net, n_out ) 
-
-            CASE ( 'rad_lw_in' )
-               CALL surface_output_sum_up( surf_def_h(0)%rad_lw_in,            &
-                                           surf_def_h(1)%rad_lw_in,            &
-                                           surf_lsm_h%rad_lw_in,               &
-                                           surf_usm_h%rad_lw_in,               &
-                                           surf_def_v(0)%rad_lw_in,            &
-                                           surf_lsm_v(0)%rad_lw_in,            &
-                                           surf_usm_v(0)%rad_lw_in,            &
-                                           surf_def_v(1)%rad_lw_in,            &
-                                           surf_lsm_v(1)%rad_lw_in,            &
-                                           surf_usm_v(1)%rad_lw_in,            &
-                                           surf_def_v(2)%rad_lw_in,            &
-                                           surf_lsm_v(2)%rad_lw_in,            &
-                                           surf_usm_v(2)%rad_lw_in,            &
-                                           surf_def_v(3)%rad_lw_in,            &
-                                           surf_lsm_v(3)%rad_lw_in,            &
-                                           surf_usm_v(3)%rad_lw_in, n_out ) 
-
-            CASE ( 'rad_lw_out' )
-               CALL surface_output_sum_up( surf_def_h(0)%rad_lw_out,           &
-                                           surf_def_h(1)%rad_lw_out,           &
-                                           surf_lsm_h%rad_lw_out,              &
-                                           surf_usm_h%rad_lw_out,              &
-                                           surf_def_v(0)%rad_lw_out,           &
-                                           surf_lsm_v(0)%rad_lw_out,           &
-                                           surf_usm_v(0)%rad_lw_out,           &
-                                           surf_def_v(1)%rad_lw_out,           &
-                                           surf_lsm_v(1)%rad_lw_out,           &
-                                           surf_usm_v(1)%rad_lw_out,           &
-                                           surf_def_v(2)%rad_lw_out,           &
-                                           surf_lsm_v(2)%rad_lw_out,           &
-                                           surf_usm_v(2)%rad_lw_out,           &
-                                           surf_def_v(3)%rad_lw_out,           &
-                                           surf_lsm_v(3)%rad_lw_out,           &
-                                           surf_usm_v(3)%rad_lw_out, n_out ) 
-
-            CASE ( 'rad_sw_in' )
-               CALL surface_output_sum_up( surf_def_h(0)%rad_sw_in,            &
-                                           surf_def_h(1)%rad_sw_in,            &
-                                           surf_lsm_h%rad_sw_in,               &
-                                           surf_usm_h%rad_sw_in,               &
-                                           surf_def_v(0)%rad_sw_in,            &
-                                           surf_lsm_v(0)%rad_sw_in,            &
-                                           surf_usm_v(0)%rad_sw_in,            &
-                                           surf_def_v(1)%rad_sw_in,            &
-                                           surf_lsm_v(1)%rad_sw_in,            &
-                                           surf_usm_v(1)%rad_sw_in,            &
-                                           surf_def_v(2)%rad_sw_in,            &
-                                           surf_lsm_v(2)%rad_sw_in,            &
-                                           surf_usm_v(2)%rad_sw_in,            &
-                                           surf_def_v(3)%rad_sw_in,            &
-                                           surf_lsm_v(3)%rad_sw_in,            &
-                                           surf_usm_v(3)%rad_sw_in, n_out ) 
-
-            CASE ( 'rad_sw_out' )
-               CALL surface_output_sum_up( surf_def_h(0)%rad_sw_out,           &
-                                           surf_def_h(1)%rad_sw_out,           &
-                                           surf_lsm_h%rad_sw_out,              &
-                                           surf_usm_h%rad_sw_out,              &
-                                           surf_def_v(0)%rad_sw_out,           &
-                                           surf_lsm_v(0)%rad_sw_out,           &
-                                           surf_usm_v(0)%rad_sw_out,           &
-                                           surf_def_v(1)%rad_sw_out,           &
-                                           surf_lsm_v(1)%rad_sw_out,           &
-                                           surf_usm_v(1)%rad_sw_out,           &
-                                           surf_def_v(2)%rad_sw_out,           &
-                                           surf_lsm_v(2)%rad_sw_out,           &
-                                           surf_usm_v(2)%rad_sw_out,           &
-                                           surf_def_v(3)%rad_sw_out,           &
-                                           surf_lsm_v(3)%rad_sw_out,           &
-                                           surf_usm_v(3)%rad_sw_out, n_out ) 
-
-            CASE ( 'ghf' )
-               CALL surface_output_sum_up( surf_def_h(0)%ghf,                  &
-                                           surf_def_h(1)%ghf,                  &
-                                           surf_lsm_h%ghf,                     &
-                                           surf_usm_h%ghf,                     &
-                                           surf_def_v(0)%ghf,                  &
-                                           surf_lsm_v(0)%ghf,                  &
-                                           surf_usm_v(0)%ghf,                  &
-                                           surf_def_v(1)%ghf,                  &
-                                           surf_lsm_v(1)%ghf,                  &
-                                           surf_usm_v(1)%ghf,                  &
-                                           surf_def_v(2)%ghf,                  &
-                                           surf_lsm_v(2)%ghf,                  &
-                                           surf_usm_v(2)%ghf,                  &
-                                           surf_def_v(3)%ghf,                  &
-                                           surf_lsm_v(3)%ghf,                  &
-                                           surf_usm_v(3)%ghf, n_out )          
-                                                                               
-            CASE ( 'r_a' )                                                     
-               CALL surface_output_sum_up( surf_def_h(0)%r_a,                  &
-                                           surf_def_h(1)%r_a,                  &
-                                           surf_lsm_h%r_a,                     &
-                                           surf_usm_h%r_a,                     &
-                                           surf_def_v(0)%r_a,                  &
-                                           surf_lsm_v(0)%r_a,                  &
-                                           surf_usm_v(0)%r_a,                  &
-                                           surf_def_v(1)%r_a,                  &
-                                           surf_lsm_v(1)%r_a,                  &
-                                           surf_usm_v(1)%r_a,                  &
-                                           surf_def_v(2)%r_a,                  &
-                                           surf_lsm_v(2)%r_a,                  &
-                                           surf_usm_v(2)%r_a,                  &
-                                           surf_def_v(3)%r_a,                  &
-                                           surf_lsm_v(3)%r_a,                  &
-                                           surf_usm_v(3)%r_a, n_out )          
-                                                                               
-            CASE ( 'r_soil' )                                                  
-               CALL surface_output_sum_up( surf_def_h(0)%r_soil,               &
-                                           surf_def_h(1)%r_soil,               &
-                                           surf_lsm_h%r_soil,                  &
-                                           surf_usm_h%r_soil,                  &
-                                           surf_def_v(0)%r_soil,               &
-                                           surf_lsm_v(0)%r_soil,               &
-                                           surf_usm_v(0)%r_soil,               &
-                                           surf_def_v(1)%r_soil,               &
-                                           surf_lsm_v(1)%r_soil,               &
-                                           surf_usm_v(1)%r_soil,               &
-                                           surf_def_v(2)%r_soil,               &
-                                           surf_lsm_v(2)%r_soil,               &
-                                           surf_usm_v(2)%r_soil,               &
-                                           surf_def_v(3)%r_soil,               &
-                                           surf_lsm_v(3)%r_soil,               &
-                                           surf_usm_v(3)%r_soil, n_out ) 
-
-            CASE ( 'r_canopy' )
-               CALL surface_output_sum_up( surf_def_h(0)%r_canopy,             &
-                                           surf_def_h(1)%r_canopy,             &
-                                           surf_lsm_h%r_canopy,                &
-                                           surf_usm_h%r_canopy,                &
-                                           surf_def_v(0)%r_canopy,             &
-                                           surf_lsm_v(0)%r_canopy,             &
-                                           surf_usm_v(0)%r_canopy,             &
-                                           surf_def_v(1)%r_canopy,             &
-                                           surf_lsm_v(1)%r_canopy,             &
-                                           surf_usm_v(1)%r_canopy,             &
-                                           surf_def_v(2)%r_canopy,             &
-                                           surf_lsm_v(2)%r_canopy,             &
-                                           surf_usm_v(2)%r_canopy,             &
-                                           surf_def_v(3)%r_canopy,             &
-                                           surf_lsm_v(3)%r_canopy,             &
-                                           surf_usm_v(3)%r_canopy, n_out ) 
-
-            CASE ( 'r_s' )
-               CALL surface_output_sum_up( surf_def_h(0)%r_s,                  &
-                                           surf_def_h(1)%r_s,                  &
-                                           surf_lsm_h%r_s,                     &
-                                           surf_usm_h%r_s,                     &
-                                           surf_def_v(0)%r_s,                  &
-                                           surf_lsm_v(0)%r_s,                  &
-                                           surf_usm_v(0)%r_s,                  &
-                                           surf_def_v(1)%r_s,                  &
-                                           surf_lsm_v(1)%r_s,                  &
-                                           surf_usm_v(1)%r_s,                  &
-                                           surf_def_v(2)%r_s,                  &
-                                           surf_lsm_v(2)%r_s,                  &
-                                           surf_usm_v(2)%r_s,                  &
-                                           surf_def_v(3)%r_s,                  &
-                                           surf_lsm_v(3)%r_s,                  &
-                                           surf_usm_v(3)%r_s, n_out ) 
-
-
-            CASE ( 'rad_sw_dir' )
-               CALL surface_output_sum_up( surf_def_h(0)%rad_sw_dir,           &
-                                           surf_def_h(1)%rad_sw_dir,           &
-                                           surf_lsm_h%rad_sw_dir,              &
-                                           surf_usm_h%rad_sw_dir,              &
-                                           surf_def_v(0)%rad_sw_dir,           &
-                                           surf_lsm_v(0)%rad_sw_dir,           &
-                                           surf_usm_v(0)%rad_sw_dir,           &
-                                           surf_def_v(1)%rad_sw_dir,           &
-                                           surf_lsm_v(1)%rad_sw_dir,           &
-                                           surf_usm_v(1)%rad_sw_dir,           &
-                                           surf_def_v(2)%rad_sw_dir,           &
-                                           surf_lsm_v(2)%rad_sw_dir,           &
-                                           surf_usm_v(2)%rad_sw_dir,           &
-                                           surf_def_v(3)%rad_sw_dir,           &
-                                           surf_lsm_v(3)%rad_sw_dir,           &
-                                           surf_usm_v(3)%rad_sw_dir, n_out )
-            CASE ( 'rad_sw_dif' )
-               CALL surface_output_sum_up( surf_def_h(0)%rad_sw_dif,           &
-                                           surf_def_h(1)%rad_sw_dif,           &
-                                           surf_lsm_h%rad_sw_dif,              &
-                                           surf_usm_h%rad_sw_dif,              &
-                                           surf_def_v(0)%rad_sw_dif,           &
-                                           surf_lsm_v(0)%rad_sw_dif,           &
-                                           surf_usm_v(0)%rad_sw_dif,           &
-                                           surf_def_v(1)%rad_sw_dif,           &
-                                           surf_lsm_v(1)%rad_sw_dif,           &
-                                           surf_usm_v(1)%rad_sw_dif,           &
-                                           surf_def_v(2)%rad_sw_dif,           &
-                                           surf_lsm_v(2)%rad_sw_dif,           &
-                                           surf_usm_v(2)%rad_sw_dif,           &
-                                           surf_def_v(3)%rad_sw_dif,           &
-                                           surf_lsm_v(3)%rad_sw_dif,           &
-                                           surf_usm_v(3)%rad_sw_dif, n_out )
-
-            CASE ( 'rad_sw_ref' )
-               CALL surface_output_sum_up( surf_def_h(0)%rad_sw_ref,           &
-                                           surf_def_h(1)%rad_sw_ref,           &
-                                           surf_lsm_h%rad_sw_ref,              &
-                                           surf_usm_h%rad_sw_ref,              &
-                                           surf_def_v(0)%rad_sw_ref,           &
-                                           surf_lsm_v(0)%rad_sw_ref,           &
-                                           surf_usm_v(0)%rad_sw_ref,           &
-                                           surf_def_v(1)%rad_sw_ref,           &
-                                           surf_lsm_v(1)%rad_sw_ref,           &
-                                           surf_usm_v(1)%rad_sw_ref,           &
-                                           surf_def_v(2)%rad_sw_ref,           &
-                                           surf_lsm_v(2)%rad_sw_ref,           &
-                                           surf_usm_v(2)%rad_sw_ref,           &
-                                           surf_def_v(3)%rad_sw_ref,           &
-                                           surf_lsm_v(3)%rad_sw_ref,           &
-                                           surf_usm_v(3)%rad_sw_ref, n_out )
-               
-            CASE ( 'rad_sw_res' )
-               CALL surface_output_sum_up( surf_def_h(0)%rad_sw_res,           &
-                                           surf_def_h(1)%rad_sw_res,           &
-                                           surf_lsm_h%rad_sw_res,              &
-                                           surf_usm_h%rad_sw_res,              &
-                                           surf_def_v(0)%rad_sw_res,           &
-                                           surf_lsm_v(0)%rad_sw_res,           &
-                                           surf_usm_v(0)%rad_sw_res,           &
-                                           surf_def_v(1)%rad_sw_res,           &
-                                           surf_lsm_v(1)%rad_sw_res,           &
-                                           surf_usm_v(1)%rad_sw_res,           &
-                                           surf_def_v(2)%rad_sw_res,           &
-                                           surf_lsm_v(2)%rad_sw_res,           &
-                                           surf_usm_v(2)%rad_sw_res,           &
-                                           surf_def_v(3)%rad_sw_res,           &
-                                           surf_lsm_v(3)%rad_sw_res,           &
-                                           surf_usm_v(3)%rad_sw_res, n_out )
-
-            CASE ( 'rad_lw_dif' )
-               CALL surface_output_sum_up( surf_def_h(0)%rad_lw_dif,           &
-                                           surf_def_h(1)%rad_lw_dif,           &
-                                           surf_lsm_h%rad_lw_dif,              &
-                                           surf_usm_h%rad_lw_dif,              &
-                                           surf_def_v(0)%rad_lw_dif,           &
-                                           surf_lsm_v(0)%rad_lw_dif,           &
-                                           surf_usm_v(0)%rad_lw_dif,           &
-                                           surf_def_v(1)%rad_lw_dif,           &
-                                           surf_lsm_v(1)%rad_lw_dif,           &
-                                           surf_usm_v(1)%rad_lw_dif,           &
-                                           surf_def_v(2)%rad_lw_dif,           &
-                                           surf_lsm_v(2)%rad_lw_dif,           &
-                                           surf_usm_v(2)%rad_lw_dif,           &
-                                           surf_def_v(3)%rad_lw_dif,           &
-                                           surf_lsm_v(3)%rad_lw_dif,           &
-                                           surf_usm_v(3)%rad_lw_dif, n_out )
-
-            CASE ( 'rad_lw_ref' )
-               CALL surface_output_sum_up( surf_def_h(0)%rad_lw_ref,           &
-                                           surf_def_h(1)%rad_lw_ref,           &
-                                           surf_lsm_h%rad_lw_ref,              &
-                                           surf_usm_h%rad_lw_ref,              &
-                                           surf_def_v(0)%rad_lw_ref,           &
-                                           surf_lsm_v(0)%rad_lw_ref,           &
-                                           surf_usm_v(0)%rad_lw_ref,           &
-                                           surf_def_v(1)%rad_lw_ref,           &
-                                           surf_lsm_v(1)%rad_lw_ref,           &
-                                           surf_usm_v(1)%rad_lw_ref,           &
-                                           surf_def_v(2)%rad_lw_ref,           &
-                                           surf_lsm_v(2)%rad_lw_ref,           &
-                                           surf_usm_v(2)%rad_lw_ref,           &
-                                           surf_def_v(3)%rad_lw_ref,           &
-                                           surf_lsm_v(3)%rad_lw_ref,           &
-                                           surf_usm_v(3)%rad_lw_ref, n_out )
-
-            CASE ( 'rad_lw_res' )
-               CALL surface_output_sum_up( surf_def_h(0)%rad_lw_res,           &
-                                           surf_def_h(1)%rad_lw_res,           &
-                                           surf_lsm_h%rad_lw_res,              &
-                                           surf_usm_h%rad_lw_res,              &
-                                           surf_def_v(0)%rad_lw_res,           &
-                                           surf_lsm_v(0)%rad_lw_res,           &
-                                           surf_usm_v(0)%rad_lw_res,           &
-                                           surf_def_v(1)%rad_lw_res,           &
-                                           surf_lsm_v(1)%rad_lw_res,           &
-                                           surf_usm_v(1)%rad_lw_res,           &
-                                           surf_def_v(2)%rad_lw_res,           &
-                                           surf_lsm_v(2)%rad_lw_res,           &
-                                           surf_usm_v(2)%rad_lw_res,           &
-                                           surf_def_v(3)%rad_lw_res,           &
-                                           surf_lsm_v(3)%rad_lw_res,           &
-                                           surf_usm_v(3)%rad_lw_res, n_out )
-
-         END SELECT
-      ENDDO
-      
-   
-   END SUBROUTINE surface_output_averaging
-   
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Sum-up the surface data for average output variables.  
-!------------------------------------------------------------------------------!   
-   SUBROUTINE surface_output_sum_up( var_def_h0, var_def_h1,                   &
-                                     var_lsm_h,  var_usm_h,                    &
-                                     var_def_v0, var_lsm_v0, var_usm_v0,       &
-                                     var_def_v1, var_lsm_v1, var_usm_v1,       &
-                                     var_def_v2, var_lsm_v2, var_usm_v2,       &
-                                     var_def_v3, var_lsm_v3, var_usm_v3, n_out )
-   
-      IMPLICIT NONE
-
-      INTEGER(iwp) ::  m          !< running index for surface elements
-      INTEGER(iwp) ::  n_out      !< index for output variable
-      INTEGER(iwp) ::  n_surf     !< running index for surface elements
-      
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_h0 !< output variable at upward-facing default-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_h1 !< output variable at downward-facing default-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_h  !< output variable at upward-facing natural-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_h  !< output variable at upward-facing urban-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_v0 !< output variable at northward-facing default-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_v1 !< output variable at southward-facing default-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_v2 !< output variable at eastward-facing default-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_v3 !< output variable at westward-facing default-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_v0 !< output variable at northward-facing natural-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_v1 !< output variable at southward-facing natural-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_v2 !< output variable at eastward-facing natural-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_v3 !< output variable at westward-facing natural-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_v0 !< output variable at northward-facing urban-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_v1 !< output variable at southward-facing urban-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_v2 !< output variable at eastward-facing urban-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_v3 !< output variable at westward-facing urban-type surfaces
-     
-!     
-!--   Set counter variable to zero before the variable is written to 
-!--   the output array. 
-      n_surf = 0
-      
-!
-!--   Write the horizontal surfaces.
-!--   Before each the variable is written to the output data structure, first 
-!--   check if the variable for the respective surface type is defined. 
-!--   If a variable is not defined, skip the block and increment the counter
-!--   variable by the number of surface elements of this type. Usually this 
-!--   is zere, however, there might be the situation that e.g. urban surfaces
-!--   are defined but the respective variable is not allocated for this surface
-!--   type. To write the data on the exact position, increment the counter. 
-      IF ( ALLOCATED( var_def_h0 ) )  THEN
-         DO  m = 1, surf_def_h(0)%ns 
-            n_surf                        = n_surf + 1
-            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
-                                          + var_def_h0(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_def_h(0)%ns  
-      ENDIF
-      IF ( ALLOCATED( var_def_h1 ) )  THEN
-         DO  m = 1, surf_def_h(1)%ns 
-            n_surf                   = n_surf + 1
-            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
-                                          + var_def_h1(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_def_h(1)%ns
-      ENDIF
-      IF ( ALLOCATED( var_lsm_h ) )  THEN
-         DO  m = 1, surf_lsm_h%ns 
-            n_surf                        = n_surf + 1
-            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
-                                          + var_lsm_h(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_lsm_h%ns
-      ENDIF
-      IF ( ALLOCATED( var_usm_h ) )  THEN
-         DO  m = 1, surf_usm_h%ns 
-            n_surf                        = n_surf + 1
-            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
-                                          + var_usm_h(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_usm_h%ns
-      ENDIF
-!
-!--   Write northward-facing
-      IF ( ALLOCATED( var_def_v0 ) )  THEN
-         DO  m = 1, surf_def_v(0)%ns 
-            n_surf                        = n_surf + 1
-            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
-                                          + var_def_v0(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_def_v(0)%ns
-      ENDIF
-      IF ( ALLOCATED( var_lsm_v0 ) )  THEN
-         DO  m = 1, surf_lsm_v(0)%ns 
-            n_surf                        = n_surf + 1
-            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
-                                          + var_lsm_v0(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_lsm_v(0)%ns
-      ENDIF
-      IF ( ALLOCATED( var_usm_v0 ) )  THEN
-         DO  m = 1, surf_usm_v(0)%ns 
-            n_surf                        = n_surf + 1
-            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
-                                          + var_usm_v0(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_usm_v(0)%ns
-      ENDIF
-!
-!--   Write southward-facing
-      IF ( ALLOCATED( var_def_v1 ) )  THEN
-         DO  m = 1, surf_def_v(1)%ns 
-            n_surf                        = n_surf + 1
-            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
-                                          + var_def_v1(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_def_v(1)%ns
-      ENDIF
-      IF ( ALLOCATED( var_lsm_v1 ) )  THEN
-         DO  m = 1, surf_lsm_v(1)%ns 
-            n_surf                        = n_surf + 1
-            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
-                                          + var_lsm_v1(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_lsm_v(1)%ns
-      ENDIF
-      IF ( ALLOCATED( var_usm_v1 ) )  THEN
-         DO  m = 1, surf_usm_v(1)%ns 
-            n_surf                        = n_surf + 1
-            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
-                                          + var_usm_v1(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_usm_v(1)%ns
-      ENDIF
-!
-!--   Write eastward-facing
-      IF ( ALLOCATED( var_def_v2 ) )  THEN
-         DO  m = 1, surf_def_v(2)%ns 
-            n_surf                        = n_surf + 1
-            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
-                                          + var_def_v2(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_def_v(2)%ns
-      ENDIF
-      IF ( ALLOCATED( var_lsm_v2 ) )  THEN
-         DO  m = 1, surf_lsm_v(2)%ns 
-            n_surf                        = n_surf + 1
-            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
-                                          + var_lsm_v2(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_lsm_v(2)%ns
-      ENDIF
-      IF ( ALLOCATED( var_usm_v2 ) )  THEN
-         DO  m = 1, surf_usm_v(2)%ns 
-            n_surf                        = n_surf + 1
-            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
-                                          + var_usm_v2(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_usm_v(2)%ns
-      ENDIF
-!
-!--   Write westward-facing
-      IF ( ALLOCATED( var_def_v3 ) )  THEN
-         DO  m = 1, surf_def_v(3)%ns 
-            n_surf                        = n_surf + 1
-            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
-                                          + var_def_v3(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_def_v(3)%ns
-      ENDIF
-      IF ( ALLOCATED( var_lsm_v3 ) )  THEN
-         DO  m = 1, surf_lsm_v(3)%ns 
-            n_surf                        = n_surf + 1
-            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
-                                          + var_lsm_v3(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_lsm_v(3)%ns
-      ENDIF
-      IF ( ALLOCATED( var_usm_v3 ) )  THEN
-         DO  m = 1, surf_usm_v(3)%ns 
-            n_surf                        = n_surf + 1
-            surfaces%var_av(n_surf,n_out) = surfaces%var_av(n_surf,n_out)      &
-                                          + var_usm_v3(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_usm_v(3)%ns
-      ENDIF
-   
-   END SUBROUTINE surface_output_sum_up
-   
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Collect the surface data from different types and different orientation.
-!------------------------------------------------------------------------------!
-   SUBROUTINE surface_output_collect( var_def_h0, var_def_h1,                  &
-                                      var_lsm_h,  var_usm_h,                   &
-                                      var_def_v0, var_lsm_v0, var_usm_v0,      &
-                                      var_def_v1, var_lsm_v1, var_usm_v1,      &
-                                      var_def_v2, var_lsm_v2, var_usm_v2,      &
-                                      var_def_v3, var_lsm_v3, var_usm_v3 )                                               
-   
-      IMPLICIT NONE
-
-      INTEGER(iwp) ::  m      !< running index for surface elements
-      INTEGER(iwp) ::  n_surf !< running index for surface elements
-      
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_h0 !< output variable at upward-facing default-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_h1 !< output variable at downward-facing default-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_h  !< output variable at upward-facing natural-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_h  !< output variable at upward-facing urban-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_v0 !< output variable at northward-facing default-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_v1 !< output variable at southward-facing default-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_v2 !< output variable at eastward-facing default-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_def_v3 !< output variable at westward-facing default-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_v0 !< output variable at northward-facing natural-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_v1 !< output variable at southward-facing natural-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_v2 !< output variable at eastward-facing natural-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_lsm_v3 !< output variable at westward-facing natural-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_v0 !< output variable at northward-facing urban-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_v1 !< output variable at southward-facing urban-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_v2 !< output variable at eastward-facing urban-type surfaces
-      REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(IN) ::  var_usm_v3 !< output variable at westward-facing urban-type surfaces
-     
-!     
-!--   Set counter variable to zero before the variable is written to 
-!--   the output array. 
-      n_surf = 0
-      
-!
-!--   Write the horizontal surfaces.
-!--   Before each the variable is written to the output data structure, first 
-!--   check if the variable for the respective surface type is defined. 
-!--   If a variable is not defined, skip the block and increment the counter
-!--   variable by the number of surface elements of this type. Usually this 
-!--   is zere, however, there might be the situation that e.g. urban surfaces
-!--   are defined but the respective variable is not allocated for this surface
-!--   type. To write the data on the exact position, increment the counter. 
-      IF ( ALLOCATED( var_def_h0 ) )  THEN
-         DO  m = 1, surf_def_h(0)%ns 
-            n_surf                   = n_surf + 1
-            surfaces%var_out(n_surf) = var_def_h0(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_def_h(0)%ns  
-      ENDIF
-      IF ( ALLOCATED( var_def_h1 ) )  THEN
-         DO  m = 1, surf_def_h(1)%ns 
-            n_surf                   = n_surf + 1
-            surfaces%var_out(n_surf) = var_def_h1(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_def_h(1)%ns
-      ENDIF
-      IF ( ALLOCATED( var_lsm_h ) )  THEN
-         DO  m = 1, surf_lsm_h%ns 
-            n_surf                   = n_surf + 1
-            surfaces%var_out(n_surf) = var_lsm_h(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_lsm_h%ns
-      ENDIF
-      IF ( ALLOCATED( var_usm_h ) )  THEN
-         DO  m = 1, surf_usm_h%ns 
-            n_surf                   = n_surf + 1
-            surfaces%var_out(n_surf) = var_usm_h(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_usm_h%ns
-      ENDIF
-!
-!--   Write northward-facing
-      IF ( ALLOCATED( var_def_v0 ) )  THEN
-         DO  m = 1, surf_def_v(0)%ns 
-            n_surf                   = n_surf + 1
-            surfaces%var_out(n_surf) = var_def_v0(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_def_v(0)%ns
-      ENDIF
-      IF ( ALLOCATED( var_lsm_v0 ) )  THEN
-         DO  m = 1, surf_lsm_v(0)%ns 
-            n_surf                   = n_surf + 1
-            surfaces%var_out(n_surf) = var_lsm_v0(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_lsm_v(0)%ns
-      ENDIF
-      IF ( ALLOCATED( var_usm_v0 ) )  THEN
-         DO  m = 1, surf_usm_v(0)%ns 
-            n_surf                   = n_surf + 1
-            surfaces%var_out(n_surf) = var_usm_v0(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_usm_v(0)%ns
-      ENDIF
-!
-!--   Write southward-facing
-      IF ( ALLOCATED( var_def_v1 ) )  THEN
-         DO  m = 1, surf_def_v(1)%ns 
-            n_surf                   = n_surf + 1
-            surfaces%var_out(n_surf) = var_def_v1(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_def_v(1)%ns
-      ENDIF
-      IF ( ALLOCATED( var_lsm_v1 ) )  THEN
-         DO  m = 1, surf_lsm_v(1)%ns 
-            n_surf                   = n_surf + 1
-            surfaces%var_out(n_surf) = var_lsm_v1(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_lsm_v(1)%ns
-      ENDIF
-      IF ( ALLOCATED( var_usm_v1 ) )  THEN
-         DO  m = 1, surf_usm_v(1)%ns 
-            n_surf                   = n_surf + 1
-            surfaces%var_out(n_surf) = var_usm_v1(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_usm_v(1)%ns
-      ENDIF
-!
-!--   Write eastward-facing
-      IF ( ALLOCATED( var_def_v2 ) )  THEN
-         DO  m = 1, surf_def_v(2)%ns 
-            n_surf                   = n_surf + 1
-            surfaces%var_out(n_surf) = var_def_v2(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_def_v(2)%ns
-      ENDIF
-      IF ( ALLOCATED( var_lsm_v2 ) )  THEN
-         DO  m = 1, surf_lsm_v(2)%ns 
-            n_surf                   = n_surf + 1
-            surfaces%var_out(n_surf) = var_lsm_v2(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_lsm_v(2)%ns
-      ENDIF
-      IF ( ALLOCATED( var_usm_v2 ) )  THEN
-         DO  m = 1, surf_usm_v(2)%ns 
-            n_surf                   = n_surf + 1
-            surfaces%var_out(n_surf) = var_usm_v2(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_usm_v(2)%ns
-      ENDIF
-!
-!--   Write westward-facing
-      IF ( ALLOCATED( var_def_v3 ) )  THEN
-         DO  m = 1, surf_def_v(3)%ns 
-            n_surf                   = n_surf + 1
-            surfaces%var_out(n_surf) = var_def_v3(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_def_v(3)%ns
-      ENDIF
-      IF ( ALLOCATED( var_lsm_v3 ) )  THEN
-         DO  m = 1, surf_lsm_v(3)%ns 
-            n_surf                   = n_surf + 1
-            surfaces%var_out(n_surf) = var_lsm_v3(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_lsm_v(3)%ns
-      ENDIF
-      IF ( ALLOCATED( var_usm_v3 ) )  THEN
-         DO  m = 1, surf_usm_v(3)%ns 
-            n_surf                   = n_surf + 1
-            surfaces%var_out(n_surf) = var_usm_v3(m)
-         ENDDO
-      ELSE
-         n_surf = n_surf + surf_usm_v(3)%ns
-      ENDIF
-   
-   END SUBROUTINE surface_output_collect
-   
-   
-   
-!    SUBROUTINE data_output_surf_driver (time)
-!       IMPLICIT NONE
-! 
-!       REAL(kind=wp),INTENT(IN)    :: time
-!       CHARACTER(LEN=64)           :: filename
-!       INTEGER,SAVE                :: file_number=1
-! 
-!       if(output_mode == "legacy_VTK")   then
-!           CALL prepare_vtk_data
-! 
-!           write(filename,'(a,i5.5,a)') 'surface_output_',file_number,'.vtk'
-!           if(myid == surf_io_pe)   write(9,*) 'Legacy VTK surface output to file: ',trim(filename)
-!           file_number = file_number+1
-! 
-!           CALL write_ascii_vtk (filename, time)
-!       END IF
-! 
-!       RETURN
-!    END SUBROUTINE data_output_surf_driver
-   
-   
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Parin for output of surface parameters
-!------------------------------------------------------------------------------!
-    SUBROUTINE surface_output_parin
-
-       IMPLICIT NONE
-
-       CHARACTER (LEN=80) ::  line  !< dummy string that contains the current line of the parameter file 
-       
-
-       NAMELIST /surface_output_parameters/                                    &
-                                  averaging_interval_surf, data_output_surf,   &
-                                  dt_dosurf, dt_dosurf_av,                     &
-                                  skip_time_dosurf, skip_time_dosurf_av
-                                  
-       line = ' '
-  
-!
-!--    Try to find the namelist
-       REWIND ( 11 )
-       line = ' '
-       DO WHILE ( INDEX( line, '&surface_output_parameters' ) == 0 )
-          READ ( 11, '(A)', END=14 )  line
-       ENDDO
-       BACKSPACE ( 11 )
-
-!
-!--    Read namelist
-       READ ( 11, surface_output_parameters, ERR = 10 )
-!
-!--    Set flag that indicates that surface data output is switched on
-       surface_data_output = .TRUE.
-       GOTO 14
-
- 10    BACKSPACE( 11 )
-       READ( 11 , '(A)') line
-       CALL parin_fail_message( 'surface_output_parameters', line )      
-       
- 14    CONTINUE
-       
-
-    END SUBROUTINE surface_output_parin
-    
-    
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Check the input parameters for consistency. Further pre-process the given 
-!> output variables, i.e. separate them into average and non-average output
-!> variables and map them onto internal output array. 
-!------------------------------------------------------------------------------!
-    SUBROUTINE surface_output_check_parameters
-
-       USE control_parameters,                                                 &
-           ONLY:  averaging_interval, dt_data_output, message_string
-
-       IMPLICIT NONE
-
-       CHARACTER(LEN=100) ::  trimvar !< dummy for single output variable
-
-       INTEGER(iwp) ::  av    !< id indicating average or non-average data output
-       INTEGER(iwp) ::  ilen  !< string length
-       INTEGER(iwp) ::  n_out !< running index for number of output variables
-
-!
-!--    Check the average interval
-       IF ( averaging_interval_surf == 9999999.9_wp )  THEN
-          averaging_interval_surf = averaging_interval
-       ENDIF
-!
-!--    Set the default data-output interval dt_data_output if necessary
-       IF ( dt_dosurf    == 9999999.9_wp )  dt_dosurf    = dt_data_output
-       IF ( dt_dosurf_av == 9999999.9_wp )  dt_dosurf_av = dt_data_output
-
-       IF ( averaging_interval_surf > dt_dosurf_av )  THEN
-          WRITE( message_string, * )  'averaging_interval_surf = ',            &
-                averaging_interval_surf, ' must be <= dt_dosurf_av = ',        &
-                dt_dosurf_av
-          CALL message( 'surface_output_check_parameters',                     &
-                        'PA0087', 1, 2, 0, 6, 0 )
-       ENDIF
-
-!
-!--   Count number of output variables and separate output strings for 
-!--   average and non-average output variables.
-      n_out = 0
-      DO WHILE ( data_output_surf(n_out+1)(1:1) /= ' ' )
-
-         n_out = n_out + 1
-         ilen = LEN_TRIM( data_output_surf(n_out) )
-         trimvar = TRIM( data_output_surf(n_out) )
-
-!
-!--      Check for data averaging
-         av = 0
-         IF ( ilen > 3 )  THEN
-            IF ( data_output_surf(n_out)(ilen-2:ilen) == '_av' )  THEN
-               trimvar = data_output_surf(n_out)(1:ilen-3)
-               av      = 1
-            ENDIF
-         ENDIF
-
-         dosurf_no(av) = dosurf_no(av) + 1
-         dosurf(av,dosurf_no(av)) = TRIM( trimvar )
-
-!
-!--      Check if all output variables are known
-         SELECT CASE ( TRIM( trimvar ) )
-
-            CASE ( 'css', 'cssws', 'qsws_liq', 'qsws_soil', 'qsws_veg' )
-               message_string = TRIM( trimvar ) //                             &
-                             ' is not yet implemented in the surface output'
-               CALL message( 'surface_output_check_parameters',                &
-                             'PA0087', 1, 2, 0, 6, 0 )
-
-            CASE ( 'us', 'ts', 'qs', 'ss', 'qcs', 'ncs', 'qrs', 'nrs', 'ol' )
-
-            CASE ( 'z0', 'z0h', 'z0q' )
-
-            CASE ( 'theta1', 'qv1', 'thetav1' )
-
-            CASE ( 'usws', 'vsws' )
-
-            CASE ( 'qcsws', 'ncsws', 'qrsws', 'nrsws', 'sasws' )
-
-            CASE ( 'shf', 'qsws', 'ssws' )
-
-            CASE ( 'q_surface', 'theta_surface', 'thetav_surface' )
-
-            CASE ( 'rad_net' )
-
-            CASE ( 'rad_lw_in', 'rad_lw_out', 'rad_lw_dif', 'rad_lw_ref',     &
-                   'rad_lw_res' )
-
-            CASE ( 'rad_sw_in', 'rad_sw_out', 'rad_sw_dif', 'rad_sw_ref',     &
-                   'rad_sw_res', 'rad_sw_dir' )
-
-            CASE ( 'ghf' )
-
-            CASE ( 'r_a', 'r_canopy', 'r_soil', 'r_s' )
-
-            CASE DEFAULT
-               message_string = TRIM( trimvar ) //                             &
-                             ' is not part of the surface output'
-               CALL message( 'surface_output_check_parameters',                &
-                             'PA0087', 1, 2, 0, 6, 0 )
-         END SELECT
-
-       ENDDO
-
-    END SUBROUTINE surface_output_check_parameters
-    
-    
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Last action.
-!------------------------------------------------------------------------------!
-    SUBROUTINE surface_output_last_action( av )
-  
-      USE control_parameters,                                                  &
-          ONLY:  io_blocks, io_group
-
-      USE pegrid,                                                              &
-          ONLY:  comm2d, ierr
-
-      IMPLICIT NONE
-      
-      INTEGER(iwp) ::  av     !< id indicating average or non-average data output
-      INTEGER(iwp) ::  i      !< loop index
-
-!
-!--   Return, if nothing to output
-      IF ( dosurf_no(av) == 0 )  RETURN
-!
-!--   Open files
-      CALL check_open( 25+av )
-!
-!--   Write time coordinate
-      DO  i = 0, io_blocks-1
-         IF ( i == io_group )  THEN
-            WRITE ( 25+av )  LEN_TRIM( 'END' )
-            WRITE ( 25+av )  'END'
-         ENDIF
-#if defined( __parallel )
-         CALL MPI_BARRIER( comm2d, ierr )
-#endif
-      ENDDO    
-
-    END SUBROUTINE surface_output_last_action    
-
-!--Private Subroutines (Only called from tinside his module)
-
-
-
-!    SUBROUTINE write_ascii_vtk (filename,time)
-!       IMPLICIT NONE
-!       CHARACTER(LEN=*),INTENT(IN)   :: filename
-!       REAL(kind=wp),INTENT(IN)      :: time
-! 
-!       INTEGER                       :: file_id=251
-!       INTEGER                       :: n
-! 
-!       IF(myid == surf_io_pe)  then
-!          open(file_id,file=TRIM(filename))                                 ! Please use check_open in the final version
-! 
-!          write(file_id,'(a)') "# vtk DataFile Version 3.0"
-!          write(file_id,'(a,f8.2,a)') "legacy vtk File generated by PALM,  simulation time = ",time," sec"
-!          write(file_id,'(a)') "ASCII"
-! 
-!          write(file_id,'(a)') "DATASET POLYDATA"
-!          write(file_id,'(a,i5,a)') "POINTS ",npo," float"
-!          do n=1,npo
-!             write (file_id,'(8f10.1)') points(1:3,n)
-!          end do
-! 
-!          write(file_id,'(a,8i10)') "POLYGONS ",npg, 5*npg
-!          do n=1,npg
-!             write(file_id,'(8i10)') polygons(:,n)
-!          end do
-! 
-!          write(file_id,'(a,i10)') "CELL_DATA ",npg
-!          write(file_id,'(a,i10)') "SCALARS cell_scalars float 1 "
-!          write(file_id,'(a,i10)') "LOOKUP_TABLE default "
-! 
-!          do n=1,npg
-!             write(file_id,'(f10.2)') cell_scalars(n)
-!          end do
-! 
-!          close (file_id)
-! 
-!          DEALLOCATE (points,polygons,cell_scalars)
-!       ENDIF
-! 
-!       RETURN
-!    END SUBROUTINE write_ascii_vtk
-
-END MODULE surface_output_mod
Index: /palm/trunk/SOURCE/time_integration.f90
===================================================================
--- /palm/trunk/SOURCE/time_integration.f90	(revision 3647)
+++ /palm/trunk/SOURCE/time_integration.f90	(revision 3648)
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! Rename subroutines for surface-data output
+! 
+! 3647 2019-01-02 14:10:44Z kanani
 ! Bugfix: add time_since_reference_point to IF clause for data_output calls
 ! (otherwise skip_time_* values don't come into affect with dt_do* = 0.0).
@@ -488,5 +491,5 @@
                skip_time_do3d, skip_time_domask, skip_time_dopr,               &
                skip_time_data_output_av, sloping_surface, stop_dt,             &
-               surface_data_output, terminate_coupled, terminate_run,          &
+               surface_output, terminate_coupled, terminate_run,               &
                timestep_scheme,                                                &
                time_coupling, time_do2d_xy, time_do2d_xz, time_do2d_yz,        &
@@ -595,9 +598,9 @@
                enter_surface_arrays, exit_surface_arrays
 
-    USE surface_output_mod,                                                    &
+    USE surface_data_output_mod,                                               &
         ONLY:  average_count_surf, averaging_interval_surf, dt_dosurf,         &
-               dt_dosurf_av, surface_output, surface_output_averaging,         &
-               skip_time_dosurf, skip_time_dosurf_av, time_dosurf,             &
-               time_dosurf_av
+               dt_dosurf_av, surface_data_output,                              &
+               surface_data_output_averaging, skip_time_dosurf,                &
+               skip_time_dosurf_av, time_dosurf, time_dosurf_av
 
     USE turbulence_closure_mod,                                                &
@@ -1466,5 +1469,5 @@
 !
 !--    Increment time-counter for surface output
-       IF ( surface_data_output )  THEN
+       IF ( surface_output )  THEN
           IF ( time_since_reference_point >= skip_time_dosurf )  THEN
              time_dosurf    = time_dosurf + dt_3d
@@ -1592,10 +1595,10 @@
 !
 !--    Average surface data
-       IF ( surface_data_output )  THEN
+       IF ( surface_output )  THEN
           IF (         averaging_interval_surf /= 0.0_wp                               &
                 .AND.  ( dt_dosurf_av - time_dosurf_av ) <= averaging_interval_surf    &
                 .AND.  time_since_reference_point >= skip_time_dosurf_av )  THEN
              IF ( time_dosurf_av >= dt_averaging_input )  THEN        
-                CALL surface_output_averaging
+                CALL surface_data_output_averaging
                 average_count_surf = average_count_surf + 1
              ENDIF
@@ -1714,13 +1717,13 @@
 !
 !--    Output of surface data, instantaneous and averaged data
-       IF ( surface_data_output )  THEN
+       IF ( surface_output )  THEN
           IF (         time_dosurf >= dt_dosurf                                    &
                 .AND.  time_since_reference_point >= skip_time_dosurf )  THEN
-             CALL surface_output( 0 )
+             CALL surface_data_output( 0 )
              time_dosurf = MOD( time_dosurf, MAX( dt_dosurf, dt_3d ) )
           ENDIF
           IF (         time_dosurf_av >= dt_dosurf_av                              &
                 .AND.  time_since_reference_point >= skip_time_dosurf_av )  THEN
-             CALL surface_output( 1 )
+             CALL surface_data_output( 1 )
              time_dosurf_av = MOD( time_dosurf_av, MAX( dt_dosurf_av, dt_3d ) )
           ENDIF
