Index: /palm/trunk/SCRIPTS/palmbuild
===================================================================
--- /palm/trunk/SCRIPTS/palmbuild	(revision 3493)
+++ /palm/trunk/SCRIPTS/palmbuild	(revision 3494)
@@ -27,4 +27,7 @@
 # -----------------
 # $Id$
+# Add tool for surface-output post-processing
+# 
+# 3455 2018-10-30 14:12:31Z raasch
 # options -h and -d renamed -c and -r, respectively
 # 
@@ -497,7 +500,8 @@
  cp -p  ../UTIL/compare_palm_logs.f90  .
  cp -p  ../UTIL/agent_preprocessing/agent_preprocessing.f90  .
+ cp -p  ../UTIL/surface_output_processing/surface_output_to_vtk.f90  .
  cp -p  ../UTIL/Makefile_utilities  .
  tar -cf  ${program_name}_sources.tar  Makefile*  *.$suf
- rm  combine_plot_fields.f90  compare_palm_logs.f90 agent_preprocessing.f90  Makefile_utilities
+ rm  combine_plot_fields.f90  compare_palm_logs.f90 agent_preprocessing.f90  surface_output_to_vtk.f90 Makefile_utilities
 
 
Index: /palm/trunk/SOURCE/Makefile
===================================================================
--- /palm/trunk/SOURCE/Makefile	(revision 3493)
+++ /palm/trunk/SOURCE/Makefile	(revision 3494)
@@ -25,4 +25,7 @@
 # -----------------
 # $Id$
+# Surface output revised
+# 
+# 3474 2018-10-30 21:07:39Z kanani
 # Add virtual measurement module
 # 
@@ -1424,4 +1427,5 @@
 	salsa_mod.o \
 	surface_layer_fluxes_mod.o \
+	surface_output_mod.o \
 	time_to_string.o \
 	write_restart_data_mod.o
Index: /palm/trunk/SOURCE/palm.f90
===================================================================
--- /palm/trunk/SOURCE/palm.f90	(revision 3493)
+++ /palm/trunk/SOURCE/palm.f90	(revision 3494)
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! Last actions for surface output added
+! 
+! 3487 2018-11-05 07:18:02Z maronga
 ! Updates version to 6.0
 ! 
@@ -315,4 +318,7 @@
         ONLY:  nested_run, pmci_child_initialize, pmci_init,                   &
                pmci_modelconfiguration, pmci_parent_initialize
+               
+    USE surface_output_mod,                                                    &
+        ONLY:  surface_output_last_action
 
     USE write_restart_data_mod,                                                &
@@ -576,4 +582,8 @@
        
     ENDIF
+!
+!-- Last actions for surface output, for instantaneous and time-averaged data
+    CALL surface_output_last_action( 0 )
+    CALL surface_output_last_action( 1 )
 
 !
Index: /palm/trunk/SOURCE/surface_output_mod.f90
===================================================================
--- /palm/trunk/SOURCE/surface_output_mod.f90	(revision 3493)
+++ /palm/trunk/SOURCE/surface_output_mod.f90	(revision 3494)
@@ -25,4 +25,10 @@
 ! -----------------
 ! $Id$
+! 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
 ! 
@@ -30,12 +36,18 @@
 ! Add output variables
 ! Write data into binary file
+!
+! 3420 2018-10-24 17:30:08Z gronemeier
 ! Initial implementation from Klaus Ketelsen and Matthias Suehring
 !
-! 3420 2018-10-24 17:30:08Z gronemeier
+!
+! Authors:
+! --------
+! @author Klaus Ketelsen, Matthias Suehring
 !
 ! Description:
 ! ------------
 !> Generate output for surface data.
-!
+!> 
+!> @todo Create namelist file for post-processing tool. 
 !------------------------------------------------------------------------------!
 
@@ -46,10 +58,16 @@
    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,                  &
@@ -58,8 +76,10 @@
    IMPLICIT NONE
 
-   TYPE surf_out                !< data structure which contains all surfaces elements of all types on subdomain
+   TYPE surf_out                      !< data structure which contains all surfaces elements of all types on subdomain
    
-      INTEGER(iwp) ::  ns       !< number of surface elements
-      INTEGER(iwp) ::  npoints  !< number of points  / vertices which define a surface element
+      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
@@ -111,4 +131,8 @@
    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
@@ -119,5 +143,5 @@
    PUBLIC surface_output, surface_output_averaging,                            &
           surface_output_check_parameters, surface_output_init,                &
-          surface_output_parin
+          surface_output_last_action, surface_output_parin
 !
 !--Public variables
@@ -134,17 +158,18 @@
 !------------------------------------------------------------------------------!
    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) ::  ilen  !< string length
-      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) ::  n_out !< running index for number of output variables
+      INTEGER(iwp) ::  i                 !< grid index in x-direction, also running variable for counting non-average data output
+      INTEGER(iwp) ::  ilen              !< string length
+      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) ::  n_out             !< running index for number of output variables   
+      INTEGER(iwp) ::  npg               !< counter variable for all surface elements ( or polygons )
+      INTEGER(iwp) ::  point_index_count !< local counter variable for point index
       
-     
-      INTEGER(iwp) ::  npg   !< counter variable for all surface elements ( or polygons )
-      
+      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
            
@@ -187,20 +212,20 @@
 !--         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
+            IF ( point_index(k,j,i) < 0 )  THEN
                surfaces%npoints   = surfaces%npoints + 1    
-               point_index(k,j,i) = surfaces%npoints
+               point_index(k,j,i) = surfaces%npoints - 1
             ENDIF
-            IF ( point_index(k,j+1,i) < 0)  THEN
+            IF ( point_index(k,j,i+1) < 0 )  THEN
                surfaces%npoints     = surfaces%npoints + 1    
-               point_index(k,j+1,i) = surfaces%npoints
+               point_index(k,j,i+1) = surfaces%npoints - 1
             ENDIF
-            IF ( point_index(k,j,i+1) < 0)  THEN
+            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,i+1) = surfaces%npoints
-            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
-            ENDIF
+               point_index(k,j+1,i) = surfaces%npoints - 1
+            ENDIF           
          ENDDO
       ENDDO
@@ -210,20 +235,20 @@
          k = surf_lsm_h%k(m) + surf_lsm_h%koff
 
-         IF ( point_index(k,j,i) < 0)  THEN
+         IF ( point_index(k,j,i) < 0 )  THEN
             surfaces%npoints   = surfaces%npoints + 1    
-            point_index(k,j,i) = surfaces%npoints
+            point_index(k,j,i) = surfaces%npoints - 1
          ENDIF
-         IF ( point_index(k,j+1,i) < 0)  THEN
+         IF ( point_index(k,j,i+1) < 0 )  THEN
             surfaces%npoints     = surfaces%npoints + 1    
-            point_index(k,j+1,i) = surfaces%npoints
+            point_index(k,j,i+1) = surfaces%npoints - 1
          ENDIF
-         IF ( point_index(k,j,i+1) < 0)  THEN
+         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,i+1) = surfaces%npoints
-         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
-         ENDIF   
+            point_index(k,j+1,i) = surfaces%npoints - 1
+         ENDIF    
       ENDDO
       DO  m = 1, surf_usm_h%ns
@@ -232,19 +257,19 @@
          k = surf_usm_h%k(m) + surf_usm_h%koff
 
-         IF ( point_index(k,j,i) < 0)  THEN
+         IF ( point_index(k,j,i) < 0 )  THEN
             surfaces%npoints   = surfaces%npoints + 1    
-            point_index(k,j,i) = surfaces%npoints
+            point_index(k,j,i) = surfaces%npoints - 1
          ENDIF
-         IF ( point_index(k,j+1,i) < 0)  THEN
+         IF ( point_index(k,j,i+1) < 0 )  THEN
             surfaces%npoints     = surfaces%npoints + 1    
-            point_index(k,j+1,i) = surfaces%npoints
+            point_index(k,j,i+1) = surfaces%npoints - 1
          ENDIF
-         IF ( point_index(k,j,i+1) < 0)  THEN
+         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,i+1) = surfaces%npoints
-         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
+            point_index(k,j+1,i) = surfaces%npoints - 1
          ENDIF     
       ENDDO
@@ -258,124 +283,125 @@
 !--         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 == 2 )
-            j = surf_def_v(l)%j(m) + MERGE( surf_def_v(l)%joff, 0, l == 0 )
+            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
+            IF ( point_index(k,j,i) < 0 )  THEN
                surfaces%npoints   = surfaces%npoints + 1    
-               point_index(k,j,i) = surfaces%npoints
+               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
+            IF ( point_index(k+1,j,i) < 0 )  THEN
                surfaces%npoints     = surfaces%npoints + 1    
-               point_index(k+1,j,i) = surfaces%npoints
+               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  .AND.  l == 1 )  THEN
-               IF ( point_index(k,j,i+1) < 0)  THEN
+            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
+                  point_index(k,j,i+1) = surfaces%npoints - 1
                ENDIF 
-               IF ( point_index(k+1,j,i+1) < 0)  THEN
+               IF ( point_index(k+1,j,i+1) < 0 )  THEN
                   surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j,i+1) = surfaces%npoints
+                  point_index(k+1,j,i+1) = surfaces%npoints - 1
                ENDIF
 !
 !--         Upper / lower front index for east- and west-facing surfaces
-            ELSEIF ( l == 2  .AND.  l == 3 )  THEN
-               IF ( point_index(k,j+1,i) < 0)  THEN
+            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
+                  point_index(k,j+1,i) = surfaces%npoints - 1
                ENDIF 
-               IF ( point_index(k+1,j+1,i) < 0)  THEN
+               IF ( point_index(k+1,j+1,i) < 0 )  THEN
                   surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j+1,i) = surfaces%npoints
-               ENDIF
-            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 == 2 )
-            j = surf_lsm_v(l)%j(m) + MERGE( surf_lsm_v(l)%joff, 0, l == 0 )
-            k = surf_lsm_v(l)%k(m) + surf_lsm_v(l)%koff
+                  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
+            IF ( point_index(k,j,i) < 0 )  THEN
                surfaces%npoints   = surfaces%npoints + 1    
-               point_index(k,j,i) = surfaces%npoints
+               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
+            IF ( point_index(k+1,j,i) < 0 )  THEN
                surfaces%npoints     = surfaces%npoints + 1    
-               point_index(k+1,j,i) = surfaces%npoints
+               point_index(k+1,j,i) = surfaces%npoints - 1
             ENDIF
-!
-!--         Upper / lower right index for north- and south-facing surfaces
-            IF ( l == 0  .AND.  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
-               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
-               ENDIF
-!
-!--         Upper / lower front index for east- and west-facing surfaces
-            ELSEIF ( l == 2  .AND.  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
-               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
-               ENDIF
-            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 == 2 )
-            j = surf_usm_v(l)%j(m) + MERGE( surf_usm_v(l)%joff, 0, l == 0 )
-            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
-            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
-            ENDIF
-!
-!--         Upper / lower right index for north- and south-facing surfaces
-            IF ( l == 0  .AND.  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
-               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
-               ENDIF
-!
-!--         Upper / lower front index for east- and west-facing surfaces
-            ELSEIF ( l == 2  .AND.  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
-               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
-               ENDIF
-            ENDIF    
-         ENDDO
+         ENDDO
+
       ENDDO
 !
@@ -384,12 +410,34 @@
 !--   of points (vertices) which define the polygons can be larger.
       ALLOCATE( surfaces%points(3,1:surfaces%npoints) )
-      ALLOCATE( surfaces%polygons(5,1:surfaces%ns)    )      
+      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
+!--   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
@@ -402,39 +450,43 @@
 !--         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
+            IF ( point_index(k,j,i) < 0 )  THEN
                surfaces%npoints   = surfaces%npoints + 1    
-               point_index(k,j,i) = surfaces%npoints
+               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+1,i) < 0)  THEN
+            IF ( point_index(k,j,i+1) < 0 )  THEN
                surfaces%npoints     = surfaces%npoints + 1    
-               point_index(k,j+1,i) = surfaces%npoints
+               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
-            IF ( point_index(k,j,i+1) < 0)  THEN
-               surfaces%npoints     = surfaces%npoints + 1    
-               point_index(k,j,i+1) = surfaces%npoints
-               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) = surfaces%npoints
-               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
             
-            npg = npg + 1
+            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+1,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,i+1)  
+            surfaces%polygons(5,npg)   = point_index(k,j+1,i)
          ENDDO
       ENDDO
@@ -443,82 +495,91 @@
          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
+         IF ( point_index(k,j,i) < 0 )  THEN
             surfaces%npoints   = surfaces%npoints + 1    
-            point_index(k,j,i) = surfaces%npoints
+            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+1,i) < 0)  THEN
+         IF ( point_index(k,j,i+1) < 0 )  THEN
             surfaces%npoints     = surfaces%npoints + 1    
-            point_index(k,j+1,i) = surfaces%npoints
+            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
-         IF ( point_index(k,j,i+1) < 0)  THEN
+         
+         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) = surfaces%npoints
+            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
+         IF ( point_index(k,j+1,i+1) < 0 )  THEN
             surfaces%npoints       = surfaces%npoints + 1    
-            point_index(k,j+1,i+1) = surfaces%npoints
+            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
-         
-         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+1,i)
-         surfaces%polygons(4,npg)   = point_index(k,j+1,i+1)
-         surfaces%polygons(5,npg)   = point_index(k,j,i+1)  
-      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
-            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+1,i) < 0)  THEN
+         IF ( point_index(k,j+1,i) < 0 )  THEN
             surfaces%npoints     = surfaces%npoints + 1    
-            point_index(k,j+1,i) = surfaces%npoints
+            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
-         IF ( point_index(k,j,i+1) < 0)  THEN
-            surfaces%npoints     = surfaces%npoints + 1    
-            point_index(k,j,i+1) = surfaces%npoints
-            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) = surfaces%npoints
-            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
          
-         npg = npg + 1
+         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+1,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,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
@@ -528,63 +589,69 @@
 !--         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 == 2 )
-            j = surf_def_v(l)%j(m) + MERGE( surf_def_v(l)%joff, 0, l == 0 )
+            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
+            IF ( point_index(k,j,i) < 0 )  THEN
                surfaces%npoints   = surfaces%npoints + 1    
-               point_index(k,j,i) = surfaces%npoints
+               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 
-!
-!--         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
-               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  .AND.  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
-                  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+1,j,i+1) < 0)  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j,i+1) = surfaces%npoints
-                  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
-!
-!--         Upper / lower front index for east- and west-facing surfaces
-            ELSEIF ( l == 2  .AND.  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
-                  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 
-               IF ( point_index(k+1,j+1,i) < 0)  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j+1,i) = surfaces%npoints
-                  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
-            ENDIF          
             
             npg = npg + 1            
-            IF ( l == 0  .AND.  l == 1 )  THEN
+            IF ( l == 0  .OR.  l == 1 )  THEN
                surfaces%polygons(1,npg)   = 4
                surfaces%polygons(2,npg)   = point_index(k,j,i)
@@ -601,64 +668,71 @@
             
          ENDDO
+
          DO  m = 1, surf_lsm_v(l)%ns
-            i = surf_lsm_v(l)%i(m) + MERGE( surf_lsm_v(l)%ioff, 0, l == 2 )
-            j = surf_lsm_v(l)%j(m) + MERGE( surf_lsm_v(l)%joff, 0, l == 0 )
+            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
+            IF ( point_index(k,j,i) < 0 )  THEN
                surfaces%npoints   = surfaces%npoints + 1    
-               point_index(k,j,i) = surfaces%npoints
+               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 
-!
-!--         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
-               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  .AND.  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
-                  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+1,j,i+1) < 0)  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j,i+1) = surfaces%npoints
-                  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
-!
-!--         Upper / lower front index for east- and west-facing surfaces
-            ELSEIF ( l == 2  .AND.  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
-                  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 
-               IF ( point_index(k+1,j+1,i) < 0)  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j+1,i) = surfaces%npoints
-                  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
             ENDIF
             
             npg = npg + 1            
-            IF ( l == 0  .AND.  l == 1 )  THEN
+            IF ( l == 0  .OR.  l == 1 )  THEN
                surfaces%polygons(1,npg)   = 4
                surfaces%polygons(2,npg)   = point_index(k,j,i)
@@ -675,63 +749,69 @@
          ENDDO
          DO  m = 1, surf_usm_v(l)%ns
-            i = surf_usm_v(l)%i(m) + MERGE( surf_usm_v(l)%ioff, 0, l == 2 )
-            j = surf_usm_v(l)%j(m) + MERGE( surf_usm_v(l)%joff, 0, l == 0 )
+            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
+            IF ( point_index(k,j,i) < 0 )  THEN
                surfaces%npoints   = surfaces%npoints + 1    
-               point_index(k,j,i) = surfaces%npoints
+               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 
-!
-!--         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
-               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  .AND.  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
-                  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+1,j,i+1) < 0)  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j,i+1) = surfaces%npoints
-                  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
-!
-!--         Upper / lower front index for east- and west-facing surfaces
-            ELSEIF ( l == 2  .AND.  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
-                  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 
-               IF ( point_index(k+1,j+1,i) < 0)  THEN
-                  surfaces%npoints       = surfaces%npoints + 1    
-                  point_index(k+1,j+1,i) = surfaces%npoints
-                  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
             ENDIF
             
             npg = npg + 1            
-            IF ( l == 0  .AND.  l == 1 )  THEN
+            IF ( l == 0  .OR.  l == 1 )  THEN
                surfaces%polygons(1,npg)   = 4
                surfaces%polygons(2,npg)   = point_index(k,j,i)
@@ -747,4 +827,5 @@
             ENDIF
          ENDDO
+
       ENDDO
 !
@@ -752,6 +833,17 @@
       DEALLOCATE ( point_index )
 !
-!--   Now, vertices as well as polygon data can be written to NetCDF files
-!--   ... 
+!--   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
    
@@ -795,5 +887,7 @@
             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
@@ -805,17 +899,4 @@
          ENDDO
       ENDIF
-!
-!--   Write time coordinate
-      DO  i = 0, io_blocks-1
-         IF ( i == io_group )  THEN
-            trimvar = 'time'
-            WRITE ( 25+av )  trimvar
-            WRITE ( 25+av )  simulated_time
-         ENDIF
-#if defined( __parallel )
-         CALL MPI_BARRIER( comm2d, ierr )
-#endif
-      ENDDO
-
 
       n_out = 0
@@ -835,19 +916,19 @@
                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 ) 
+                                               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
 !
@@ -1945,5 +2026,9 @@
          DO  i = 0, io_blocks-1
             IF ( i == io_group )  THEN
-               WRITE ( 25+av )  trimvar
+               WRITE ( 25+av )  LEN_TRIM( 'time' )
+               WRITE ( 25+av )  'time'
+               WRITE ( 25+av )  simulated_time
+               WRITE ( 25+av )  LEN_TRIM( trimvar )
+               WRITE ( 25+av )  TRIM( trimvar )
                WRITE ( 25+av )  surfaces%var_out
             ENDIF
@@ -1956,6 +2041,6 @@
       
 !
-!--      If averaged output was written to NetCDF file, set the counter to zero
-         IF ( av == 1 )  average_count_surf = 0
+!--   If averaged output was written to NetCDF file, set the counter to zero
+      IF ( av == 1 )  average_count_surf = 0
              
    END SUBROUTINE surface_output
@@ -2679,12 +2764,10 @@
 !> 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_def_v1, var_def_v2,      &
-                                      var_def_v3,                              &
-                                      var_lsm_v0, var_lsm_v1, var_lsm_v2,      &
-                                      var_lsm_v3,                              &
-                                      var_usm_v0, var_usm_v1, var_usm_v2,      &
-                                      var_usm_v3, n_out )
+   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
@@ -2886,11 +2969,9 @@
 !------------------------------------------------------------------------------!
    SUBROUTINE surface_output_collect( var_def_h0, var_def_h1,                  &
-                                      var_lsm_h, var_usm_h,                    &
-                                      var_def_v0, var_def_v1, var_def_v2,      &
-                                      var_def_v3,                              &
-                                      var_lsm_v0, var_lsm_v1, var_lsm_v2,      &
-                                      var_lsm_v3,                              &
-                                      var_usm_v0, var_usm_v1, var_usm_v2,      &
-                                      var_usm_v3 )
+                                      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
@@ -3246,4 +3327,43 @@
 
     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)
Index: /palm/trunk/SOURCE/virtual_measurement_mod.f90
===================================================================
--- /palm/trunk/SOURCE/virtual_measurement_mod.f90	(revision 3493)
+++ /palm/trunk/SOURCE/virtual_measurement_mod.f90	(revision 3494)
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! Bugfixing
+! 
+! 3473 2018-10-30 20:50:15Z suehring
 ! Initial revision
 ! 
@@ -31,5 +34,5 @@
 ! Authors:
 ! --------
-! @author Matthias Suehring
+! @author Matthias Suehring and Klaus Ketelsen
 ! 
 !
@@ -554,14 +557,9 @@
 !--          lower-left origin of the model domain, which is the difference 
 !--          betwen UTM coordinates.
-!              write(9,*) l, "before Eutm", e_utm(t,1:vmea(l)%dim_t(t)), "orig", init_model%origin_x
-!              write(9,*) l, "before Nutm", n_utm(t,1:vmea(l)%dim_t(t)), "orig", init_model%origin_y
-             
+            
              e_utm(t,1:vmea(l)%dim_t(t)) = e_utm(t,1:vmea(l)%dim_t(t))         &
                                          - init_model%origin_x
              n_utm(t,1:vmea(l)%dim_t(t)) = n_utm(t,1:vmea(l)%dim_t(t))         &
                                          - init_model%origin_y
-!              write(9,*) l, "Eutm", e_utm(t,1:vmea(l)%dim_t(t)) 
-!              write(9,*) l, "Nutm", n_utm(t,1:vmea(l)%dim_t(t)) 
-!              write(9,*)
 !
 !--          Compute grid indices relative to origin and check if these are
@@ -585,5 +583,4 @@
                 ks = k_prev
                 IF ( on_pe )  THEN
-!                    write(9,*) " I am onpe" 
                    ksurf = get_topography_top_index_ji( js, is, 's' )
                    ks = MINLOC( ABS( zu - zw(ksurf) - z_ag(t,n) ), DIM = 1 ) - 1
@@ -655,5 +652,4 @@
                    vmea(l)%j(ns)  = js
                    vmea(l)%k(ns)  = ks
-!                    write(9,*) TRIM( vmea(l)%feature_type), l, "ns", ns, "ijk", vmea(l)%i(ns), vmea(l)%j(ns), vmea(l)%k(ns)
                 ENDIF
 !
@@ -713,6 +709,4 @@
      INTEGER(iwp) ::  mm, j, i
 
-!      write(9,*) "sampling"
-!      flush(9)
 !
 !--  Loop over all stations. For each possible variable loop over all 
@@ -723,7 +717,7 @@
 !--     the same indices for scalar and velocity components are used.
 !--     ToDo: Revise this later.
-!         DO  m = 1, vmea(l)%ns
-!            j = vmea(l)%j(m)
-!            i = vmea(l)%i(m)
+        DO  m = 1, vmea(l)%ns
+           j = vmea(l)%j(m)
+           i = vmea(l)%i(m)
 !            
 !            IF ( i >= nxl  .AND.  i <= nxr  .AND.                               &
@@ -731,7 +725,4 @@
 !               IF ( surf_def_h(0)%start_index(j,i) <= &
 !                    surf_def_h(0)%end_index(j,i) )  THEN
-!                 
-!                  write(9,*) "sampled"
-!                  flush(9)
 !                  mm = surf_def_h(0)%end_index(j,i)
 !               
@@ -763,5 +754,5 @@
 !               ENDDO
 !            ENDIF
-!         ENDDO
+        ENDDO
 
      ENDDO
Index: /palm/trunk/UTIL/Makefile_utilities
===================================================================
--- /palm/trunk/UTIL/Makefile_utilities	(revision 3493)
+++ /palm/trunk/UTIL/Makefile_utilities	(revision 3494)
@@ -23,4 +23,7 @@
 # -----------------
 # $Id$
+# Added make directive for surface-output post-processing
+# 
+# 3208 2018-08-27 13:10:50Z sward
 # Added make directive for agent_preprocessing tool
 # 
@@ -54,8 +57,10 @@
 PROG2 =  compare_palm_logs.x
 PROG3 =  agent_preprocessing
+PROG4 =  surface_output_to_vtk
 
 OBJS1 =  combine_plot_fields.o
 OBJS2 =  compare_palm_logs.o
 OBJS3 =  agent_preprocessing.o
+OBJS4 =  surface_output_to_vtk.o
 
 CC = cc
@@ -71,5 +76,5 @@
 
 
-all: $(PROG1) $(PROG2) $(PROG3)
+all: $(PROG1) $(PROG2) $(PROG3) $(PROG4)
 
 $(PROG1): $(OBJS1)
@@ -82,4 +87,7 @@
 	$(F90_SER) -o $(PROG3) $(OBJS3) $(LDFLAGS)
 
+$(PROG4): $(OBJS4)
+	$(F90_SER) -o $(PROG4) $(OBJS4) $(LDFLAGS)
+
 .f90.o:
 	$(F90_SER) $(F90FLAGS) $(COPT) -c $<
Index: /palm/trunk/UTIL/surface_output_processing/surface_output_parin
===================================================================
--- /palm/trunk/UTIL/surface_output_processing/surface_output_parin	(revision 3494)
+++ /palm/trunk/UTIL/surface_output_processing/surface_output_parin	(revision 3494)
@@ -0,0 +1,18 @@
+&surface_output
+
+!
+!-- Number of processors of the run - equal the number of binary files with surface data.
+    num_pe = 4, 
+!
+!-- Path to the output binary files.
+    path = '~/palm/current_version/JOBS/test_urban/OUTPUT/', 
+!
+!-- Name of the run.
+    run = 'test_urban',
+!
+!-- Cycle number of the output data which should be processed.
+    cycle_number = 0, 
+!
+!-- Convert instantaneous or average data.
+    convert_average_data = .F., 
+/  
Index: /palm/trunk/UTIL/surface_output_processing/surface_output_to_vtk.f90
===================================================================
--- /palm/trunk/UTIL/surface_output_processing/surface_output_to_vtk.f90	(revision 3494)
+++ /palm/trunk/UTIL/surface_output_processing/surface_output_to_vtk.f90	(revision 3494)
@@ -0,0 +1,424 @@
+!> @file surface_output_merge.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$
+! Initial version
+! 
+! 3241 2018-09-12 15:02:00Z raasch
+!
+!
+! Authors:
+! --------
+! @author Matthias Suehring and Klaus Ketelsen
+!
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This routine combines surface output from PALM-subdomains into one file. 
+!> Output from every processor element is opened and read and output 
+!> from all processor elements are written into one file for each timestep 
+!> according to Paraview's VTK standard. 
+!> Output is distinguished between instantaneous and time-averaged data. 
+!------------------------------------------------------------------------------!
+ PROGRAM surface_output_merge
+
+    IMPLICIT NONE
+    
+    CHARACTER(LEN=4)   ::  char_time            !< string indicating simulated time
+    CHARACTER(LEN=4)   ::  file_suffix = '.bin' !< string which contain the suffix indicating surface data
+    
+    CHARACTER(LEN=10)  ::  char_dum             !< dummy string
+    
+    CHARACTER(LEN=30)  ::  myid_char            !< combined string indicating binary file
+    
+    CHARACTER(LEN=100) ::  path                 !< path to the binary data 
+    CHARACTER(LEN=100) ::  run                  !< name of the run
+    CHARACTER(LEN=100) ::  variable_name        !< name of the processed output variable    
+    
+    INTEGER(4)   ::  ftell                      !< intrinsic function, get current position in file
+    INTEGER(4)   ::  fseek                      !< intrinsic function, go to given position in file
+    INTEGER(4)   ::  ndum                       !< return parameter of intrinsic function fseek
+    
+    INTEGER, PARAMETER ::  iwp = 4              !< integer precision
+    INTEGER, PARAMETER ::  wp  = 8              !< float precision
+    
+    INTEGER(iwp) ::  cycle_number               !< cycle number
+    INTEGER(iwp) ::  f                          !< running index over all binary files
+    INTEGER(iwp) ::  file_id_in = 110           !< file unit for input binaray file    
+    INTEGER(iwp) ::  file_id_out = 20           !< file unit for output VTK file        
+    INTEGER(iwp) ::  file_id_out_header = 19    !< file unit for temporary header file
+    INTEGER(iwp) ::  length                     !< length of string on file
+    INTEGER(iwp) ::  n                          !< running index over all points and polygons
+    INTEGER(iwp) ::  npoints_total              !< total number of points
+    INTEGER(iwp) ::  ns_total                   !< total number of polygons
+    INTEGER(iwp) ::  num_pe                     !< number of processors of the run
+    
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  filepos !< current fileposition in binary file
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  npoints !< number of points/vertices in a binaray file
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  ns      !< number of surface elements in a binaray file 
+    
+    LOGICAL ::  convert_average_data = .FALSE.          !< namelist parameter to decide whether average or instantaneous data should be converted
+    LOGICAL, DIMENSION(:), ALLOCATABLE      ::  eof     !< flag indicating that end of binary file is reached
+    
+    REAL(wp)                              ::  simulated_time !< output time   
+    
+    REAL(wp), DIMENSION(:), ALLOCATABLE   ::  var            !< actual surface data
+    
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  points         !< point / vertex data
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  polygons       !< polygon data
+
+!
+!-- Read namelist.
+    CALL surface_output_merge_parin
+!
+!-- Allocate array which contains the file position in each output file,
+!-- in order to skip some reading.
+    ALLOCATE( eof( 0:num_pe-1 )     )
+    ALLOCATE( filepos( 0:num_pe-1 ) )
+    ALLOCATE( npoints( 0:num_pe-1 ) )
+    ALLOCATE( ns( 0:num_pe-1 )      )
+!
+!-- Initialize file position.
+    filepos = 0
+!
+!-- Open a temporary file which contains all necessary header information for the
+!-- VTK format.
+    OPEN ( file_id_out_header, FILE = 'HEADER', STATUS = 'REPLACE',            &
+           FORM = 'FORMATTED' ) 
+!
+!-- READ grid setup, i.e. the number and position of vertices and surface elements
+!-- and merge all this information into one file. Further, create all the required
+!-- header information of the VTK file.
+!-- Note, PARAVIEW expects one VTK file for each variable and each timestep. 
+!-- Hence, header information needs to be duplicated multiple times, which will be
+!-- be done later in a bash script. 
+!-- Moreover, Paraview expects consecutive vertex and polygon data, which are 
+!-- all distributed over the binaray files. Hence, first read vertex data from 
+!-- binary file, write this to the HEADER file, close the binary file and read
+!-- data from the next binary file and so on. This requires several openenings 
+!-- and closings of the binary files and temporarily storage of the 
+!-- file-positions.
+    DO  f = 0, num_pe - 1
+!
+!--    Create filename of the treated binary file.
+       CALL surface_output_merge_create_file_string
+!
+!--    Open file with surface output for processor f.
+       OPEN ( file_id_in + f, FILE = TRIM( path ) // TRIM( run ) //            &
+              TRIM( myid_char ), FORM='UNFORMATTED' )
+!
+!--    Read number of vertices / points and surface elements
+       READ ( file_id_in + f )  npoints(f)
+       READ ( file_id_in + f )  npoints_total
+       READ ( file_id_in + f )  ns(f)
+       READ ( file_id_in + f )  ns_total
+
+!
+!--    Allocate arrays where all the surfaces and vertices will be stored.
+       ALLOCATE( points(3,1:npoints(f))   )
+       
+!
+!--    Read polygon data and store them in a temporary file.
+       READ ( file_id_in + f )  points
+!
+!--    Obtain current file position. Will be stored for next file opening.
+       filepos(f) = ftell( file_id_in + f )
+!
+!--    Write header information. Only one time required.
+       IF ( f == 0 )  THEN
+          WRITE( file_id_out_header,'(A)' ) "# vtk DataFile Version 3.0"
+          WRITE( file_id_out_header,'(A,F8.2,A)' ) "legacy vtk File generated by PALM,  simulation time = xxx sec"
+          WRITE( file_id_out_header,'(A)') "ASCII"
+
+          WRITE( file_id_out_header,'(A)') "DATASET POLYDATA"
+          WRITE( file_id_out_header,'(A,I5,A)') "POINTS ", npoints_total, " float"
+       ENDIF      
+!
+!--    Write the vertex data into header file.
+       DO  n = 1, npoints(f)
+          WRITE( file_id_out_header, '(8F10.1)' )  points(1:3,n)
+       ENDDO
+!
+!--    Deallocate vertex data and close binary file.
+       DEALLOCATE( points )
+       
+       CLOSE ( file_id_in + f )
+    ENDDO
+!
+!-- Now, treat polygon data.
+    DO  f = 0, num_pe - 1
+!
+!--    Create filename of the treated binary file .   
+       CALL surface_output_merge_create_file_string
+!
+!--    Open file with surface output for processor f.
+       OPEN ( file_id_in + f, FILE = TRIM( path ) // TRIM( run ) //            &
+              TRIM( myid_char ), FORM='UNFORMATTED' )
+!
+!--    Move to last postion. 
+       ndum = fseek( file_id_in + f, filepos(f), 0 )
+!
+!--    Allocate array for polygon data
+       ALLOCATE( polygons(5,1:ns(f)) )
+!
+!--    Read polygon data and store them in a temporary file.
+       READ ( file_id_in + f )  polygons
+!
+!--    Obtain current file position after reading the local polygon data. 
+!--    Will be used for next file opening.
+       filepos(f) = ftell( file_id_in + f )
+!
+!--    Write further header information. Only one time required.
+       IF ( f == 0 )                                                           &
+          WRITE ( file_id_out_header, '(A,8I10)') "POLYGONS ",                 &
+                                                  ns_total, 5 * ns_total
+!
+!--    Write the polygons into the header file. Note, polygon data is of type
+!--    integer, as it just connects the point-indices which describe the given
+!--    surface element.
+       DO n = 1, ns(f)
+          WRITE ( file_id_out_header, '(8I10)' )  INT (polygons(1:5,n) )
+       ENDDO
+!
+!--    Deallocate array for polygon data and close the file.
+       DEALLOCATE( polygons )
+       
+       CLOSE ( file_id_in + f )
+       
+    ENDDO
+    
+    f = 0
+    CALL surface_output_merge_create_file_string
+!
+!-- Write further header information. Only once required.
+    WRITE ( file_id_out_header, '(A,I10)') "CELL_DATA ", ns_total
+    WRITE ( file_id_out_header, '(A,I10)') "SCALARS cell_scalars float 1 "
+    WRITE ( file_id_out_header, '(A,I10)') "LOOKUP_TABLE default "
+!
+!-- Header creation has now been completed. File can be closed.    
+    CLOSE( file_id_out_header )
+!
+!-- Now, read all the actual data from the surface output. Please note, Paraview
+!-- VTK format expects 1 file per variable and time step. 
+!-- The output file is only created once and includes the variable and the 
+!-- simulated time. 
+!-- In the binaray files, several variables and timesteps are stored, data for a 
+!-- given variable, however, is distributed over all binary files. Hence, read 
+!-- variable data for a given timestep from the binary file, write this data into
+!-- the target output file, remember file position in binary file and close it, 
+!-- open nex binary file and read the variable, and so on, until all variables
+!-- for all timesteps are processed. 
+    eof = .FALSE.
+    DO
+       DO  f = 0, num_pe - 1
+!
+!--       Clean up strings-
+          char_time = ''
+          variable_name = ''
+!
+!--       Create filename of the treated binary file.            
+          CALL surface_output_merge_create_file_string
+!
+!--       Open binary file with surface output for processor f.
+          OPEN ( file_id_in + f, FILE = TRIM( path ) // TRIM( run ) //         &
+                 TRIM( myid_char ), FORM='UNFORMATTED' )
+!
+!--       Move to last postion. 
+          ndum = fseek( file_id_in + f, filepos(f), 0 )
+!
+!--       Read string length and string indicating the output time step. 
+          READ ( file_id_in + f ) length
+          READ ( file_id_in + f ) char_time(1:length)
+!
+!--       If string for the output time indicates the end-of-file, set the eof 
+!--       flag and skip the read of the loop. 
+          IF ( char_time(1:length) == 'END' )  THEN
+             eof(f) = .TRUE. 
+             CLOSE ( file_id_in + f )
+             CYCLE
+          ENDIF
+!
+!--       Read output time, and variable name.
+          READ ( file_id_in + f ) simulated_time
+          READ ( file_id_in + f ) length
+          READ ( file_id_in + f ) variable_name(1:length)
+!
+!--       For first loop index, open the target output file. First create the
+!--       filename string. Further, copy HEADER file with the given filename 
+!--       string. The header information must be given in each VTK file!
+          IF ( f == 0 )  THEN
+             IF ( simulated_time < 10.0_wp )  THEN
+                WRITE( char_dum, '(I1)' )  INT( simulated_time ) 
+             ELSEIF ( simulated_time < 100.0_wp )  THEN
+                WRITE( char_dum, '(I2)' )  INT( simulated_time )
+             ELSEIF ( simulated_time < 1000.0_wp )  THEN
+                WRITE( char_dum, '(I3)' )  INT( simulated_time )
+             ELSEIF ( simulated_time < 10000.0_wp )  THEN
+                WRITE( char_dum, '(I4)' )  INT( simulated_time )   
+             ELSEIF ( simulated_time < 100000.0_wp )  THEN
+                WRITE( char_dum, '(I5)' )  INT( simulated_time )   
+             ELSEIF ( simulated_time < 1000000.0_wp )  THEN
+                WRITE( char_dum, '(I6)' )  INT( simulated_time )
+             ELSEIF ( simulated_time < 10000000.0_wp )  THEN
+                WRITE( char_dum, '(I7)' )  INT( simulated_time )   
+             ELSEIF ( simulated_time < 100000000.0_wp )  THEN
+                WRITE( char_dum, '(I8)' )  INT( simulated_time )   
+             ELSEIF ( simulated_time < 1000000000.0_wp )  THEN
+                WRITE( char_dum, '(I9)' )  INT( simulated_time )
+             ENDIF             
+!
+!--          Copy HEADER file
+             CALL system('cp HEADER ' // TRIM( path ) // TRIM( char_dum ) //   & 
+                                      's_' // TRIM( variable_name ) // '.vtk')
+!--          Open VTK file. 
+             OPEN ( file_id_out, FILE = TRIM( path ) // TRIM( char_dum ) //    & 
+                    's_' // TRIM( variable_name ) // '.vtk', FORM='FORMATTED', &
+                    POSITION='APPEND' )           
+          ENDIF
+!
+!--       Allocate and read array for variable data.  
+          ALLOCATE( var(1:ns(f)) )
+       
+          READ( file_id_in + f ) var
+!
+!--       Write variable data into output VTK file.
+          DO n = 1, ns(f)
+             WRITE( file_id_out, * ) var(n)
+          ENDDO
+!
+!--       Remember file position in binary file and close it.
+          filepos(f) = ftell( file_id_in + f )
+       
+          CLOSE ( file_id_in + f )
+!
+!--       Deallocate temporary array for variable data.
+          DEALLOCATE( var )
+       
+       ENDDO
+!
+!--    After data for a variable for one time step is read, close the output
+!--    VTK file and go to next variable or timestep.
+       CLOSE ( file_id_out )
+!
+!--    If all files reached the end-of-file, exit the loop. 
+       IF ( ALL( eof ) )  EXIT
+       
+    ENDDO
+!
+!-- Finally, remove HEADER file
+    CALL system( 'rm HEADER' )
+    
+ CONTAINS
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This subroutine read the namelist file.
+!------------------------------------------------------------------------------!
+    SUBROUTINE surface_output_merge_parin
+       
+       IMPLICIT NONE
+       
+       INTEGER(iwp) ::  file_id_parin = 90
+       
+       NAMELIST /surface_output/  convert_average_data, cycle_number, num_pe,  &
+                                  path, run
+
+!
+!--    Open namelist file.
+       OPEN( file_id_parin, FILE='surface_output_parin',                       &
+             STATUS='OLD', FORM='FORMATTED')
+!
+!--    Read namelist.
+       READ ( file_id_parin, surface_output )
+!
+!--    Close namelist file.
+       CLOSE( file_id_parin )
+       
+    END SUBROUTINE surface_output_merge_parin
+      
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This subroutine creates the filename string of the treated binary file.
+!------------------------------------------------------------------------------!
+    SUBROUTINE surface_output_merge_create_file_string
+       
+       IMPLICIT NONE
+       
+       CHARACTER(LEN=3) ::  char_av = ''
+       CHARACTER(LEN=4) ::  char_cycle = ''
+       
+!
+!--    Create substring for the cycle number.
+       IF ( cycle_number /= 0 )  THEN
+          IF ( cycle_number < 10 )  THEN
+             WRITE( char_cycle, '(I1)')  cycle_number
+             char_cycle = '.00' // TRIM( char_cycle )
+          ELSEIF ( cycle_number < 100 )  THEN
+             WRITE( char_cycle, '(I2)')  cycle_number
+             char_cycle = '.0' // TRIM( char_cycle )
+          ELSEIF ( cycle_number < 1000 )  THEN
+             WRITE( char_cycle, '(I3)')  cycle_number
+             char_cycle = '.' // TRIM( char_cycle )
+          ENDIF
+       ENDIF
+!
+!--    Create substring for averaged data. 
+       IF ( convert_average_data )  char_av = '_av'
+!
+!--    Create substring for the processor id and combine all substrings.
+       IF ( f < 10 )  THEN
+          WRITE( char_dum, '(I1)')  f
+          myid_char = TRIM( char_av ) // '_surf_00000' // TRIM( char_dum ) //  &
+                      TRIM( char_cycle ) // file_suffix
+       ELSEIF ( f < 100     )  THEN
+          WRITE( char_dum, '(I2)')  f
+          myid_char = TRIM( char_av ) // '_surf_0000'  // TRIM( char_dum ) //  &
+                      TRIM( char_cycle ) // file_suffix
+       ELSEIF ( f < 1000    )  THEN
+          WRITE( char_dum, '(I3)')  f
+          myid_char = TRIM( char_av ) // '_surf_000'   // TRIM( char_dum ) //  &
+                      TRIM( char_cycle ) // file_suffix
+       ELSEIF ( f < 10000   )  THEN
+          WRITE( char_dum, '(I4)')  f
+          myid_char = TRIM( char_av ) // '_surf_00'    // TRIM( char_dum ) //  &
+                      TRIM( char_cycle ) // file_suffix
+       ELSEIF ( f < 100000  )  THEN
+          WRITE( char_dum, '(I5)')  f
+          myid_char = TRIM( char_av ) // '_surf_0'     // TRIM( char_dum ) //  &
+                      TRIM( char_cycle ) // file_suffix
+       ELSEIF ( f < 1000000 )  THEN
+          WRITE( char_dum, '(I6)')  f
+          myid_char = TRIM( char_av ) // '_surf_'      // TRIM( char_dum ) //  &
+                      TRIM( char_cycle ) // file_suffix
+       ENDIF
+       
+    END SUBROUTINE surface_output_merge_create_file_string
+    
+ END PROGRAM surface_output_merge
+
+
+
