Index: /palm/trunk/SOURCE/module_interface.f90
===================================================================
--- /palm/trunk/SOURCE/module_interface.f90	(revision 3730)
+++ /palm/trunk/SOURCE/module_interface.f90	(revision 3731)
@@ -20,5 +20,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! Add required restart data for surface output module
 ! 
 ! Former revisions:
@@ -57,4 +57,5 @@
 !> This is the interface between the PALM model core and all its modules.
 !>
+!> @todo Re-format module to be consistent with coding standard
 !------------------------------------------------------------------------------!
 MODULE module_interface
@@ -259,5 +260,10 @@
    USE surface_data_output_mod,                                                &
        ONLY:  surface_data_output_parin,                                       &
-              surface_data_output_check_parameters
+              surface_data_output_check_parameters,                            &
+              surface_data_output_init_arrays,                                 &
+              surface_data_output_rrd_local,                                   &
+              surface_data_output_rrd_global,                                  &
+              surface_data_output_wrd_local,                                   &
+              surface_data_output_wrd_global
 
    USE synthetic_turbulence_generator_mod,                                     &
@@ -723,4 +729,5 @@
    IF ( salsa               )  CALL salsa_init_arrays
    IF ( urban_surface       )  CALL usm_init_arrays
+   IF ( surface_output      )  CALL surface_data_output_init_arrays
    IF ( wind_turbine        )  CALL wtm_init_arrays
 
@@ -1079,4 +1086,5 @@
    IF ( .NOT. found )  CALL stg_rrd_global ( found ) ! ToDo: change interface to pass variable
    IF ( .NOT. found )  CALL wtm_rrd_global( found ) ! ToDo: change interface to pass variable
+   IF ( .NOT. found )  CALL surface_data_output_rrd_global( found )
    IF ( .NOT. found )  CALL user_rrd_global( found ) ! ToDo: change interface to pass variable
 
@@ -1100,4 +1108,5 @@
    IF ( syn_turb_gen )         CALL stg_wrd_global
    IF ( wind_turbine )         CALL wtm_wrd_global
+   IF ( surface_output )       CALL surface_data_output_wrd_global
    IF ( user_module_enabled )  CALL user_wrd_global
 
@@ -1215,5 +1224,14 @@
                               found                                            &
                            ) ! ToDo: change interface to pass variable
-
+          
+   IF ( .NOT. found ) CALL surface_data_output_rrd_local(                      &
+                              file_index, map_index,                           &
+                              nxlf, nxlc, nxl_on_file,                         &
+                              nxrf, nxrc, nxr_on_file,                         &
+                              nynf, nync, nyn_on_file,                         &
+                              nysf, nysc, nys_on_file,                         &
+                              found                                            &
+                           ) ! ToDo: change interface to pass variable
+                           
    IF ( .NOT. found ) CALL user_rrd_local(                                     &
                               file_index, map_index,                           &
@@ -1246,4 +1264,5 @@
    IF ( salsa )                CALL salsa_wrd_local
    IF ( urban_surface )        CALL usm_wrd_local
+   IF ( surface_output )       CALL surface_data_output_wrd_local
    IF ( user_module_enabled )  CALL user_wrd_local
 
Index: /palm/trunk/SOURCE/surface_data_output_mod.f90
===================================================================
--- /palm/trunk/SOURCE/surface_data_output_mod.f90	(revision 3730)
+++ /palm/trunk/SOURCE/surface_data_output_mod.f90	(revision 3731)
@@ -20,5 +20,8 @@
 ! Current revisions:
 ! ------------------
-! 
+! - Split initialization into initialization of arrays and further initialization
+!   in order to enable reading of restart data.
+! - Consider restarts in surface data averaging.
+! - Correct error message numbers
 ! 
 ! Former revisions:
@@ -197,4 +200,8 @@
    END INTERFACE  surface_data_output_init
    
+   INTERFACE  surface_data_output_init_arrays
+      MODULE PROCEDURE surface_data_output_init_arrays
+   END INTERFACE  surface_data_output_init_arrays
+   
    INTERFACE  surface_data_output_last_action
       MODULE PROCEDURE surface_data_output_last_action
@@ -204,4 +211,20 @@
       MODULE PROCEDURE surface_data_output_parin
    END INTERFACE  surface_data_output_parin
+   
+   INTERFACE  surface_data_output_rrd_global
+      MODULE PROCEDURE surface_data_output_rrd_global
+   END INTERFACE  surface_data_output_rrd_global
+   
+   INTERFACE  surface_data_output_rrd_local
+      MODULE PROCEDURE surface_data_output_rrd_local
+   END INTERFACE  surface_data_output_rrd_local
+   
+   INTERFACE  surface_data_output_wrd_global
+      MODULE PROCEDURE surface_data_output_wrd_global
+   END INTERFACE  surface_data_output_wrd_global
+   
+   INTERFACE  surface_data_output_wrd_local
+      MODULE PROCEDURE surface_data_output_wrd_local
+   END INTERFACE  surface_data_output_wrd_local
 
 !
@@ -209,5 +232,8 @@
    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
+          surface_data_output_init_arrays, surface_data_output_last_action,    &
+          surface_data_output_parin, surface_data_output_rrd_global,           &
+          surface_data_output_rrd_local, surface_data_output_wrd_local,        &
+          surface_data_output_wrd_global
 !
 !--Public variables
@@ -217,4 +243,45 @@
  CONTAINS
 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This routine counts the number of surfaces on each core and allocates
+!> arrays. 
+!------------------------------------------------------------------------------!
+   SUBROUTINE surface_data_output_init_arrays
+   
+      IMPLICIT NONE
+
+!
+!--   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   
+!
+!--    Determine the total number of surfaces in the model domain
+#if defined( __parallel )
+       CALL MPI_ALLREDUCE( surfaces%ns, surfaces%ns_total, 1,                  &
+                           MPI_INTEGER, MPI_SUM, comm2d, ierr )
+#else
+       surfaces%ns_total = surfaces%ns
+#endif
+!
+!--   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
+      
+   END SUBROUTINE surface_data_output_init_arrays
+ 
+ 
 !------------------------------------------------------------------------------!
 ! Description:
@@ -252,32 +319,4 @@
       REAL(wp), DIMENSION(:), ALLOCATABLE ::  netcdf_data_1d  !< dummy array to output 1D data into netcdf file
 
-
-!
-!--   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   
-!
-!--    Determine the total number of surfaces in the model domain
-#if defined( __parallel )
-       CALL MPI_ALLREDUCE( surfaces%ns, surfaces%ns_total, 1,                  &
-                           MPI_INTEGER, MPI_SUM, comm2d, ierr )
-#else
-       surfaces%ns_total = surfaces%ns
-#endif
-!
-!--   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
 !
 !--   If output to VTK format is enabled, initialize point and polygon data. 
@@ -1333,5 +1372,5 @@
                                         time_since_reference_point, 's because the maximum ', & 
                                         'number of output time levels is exceeded.'
-            CALL message( 'surface_data_output', 'PA0???', 0, 1, 0, 6, 0 )
+            CALL message( 'surface_data_output', 'PA0539', 0, 1, 0, 6, 0 )
             
             RETURN
@@ -4051,5 +4090,9 @@
 
        USE control_parameters,                                                 &
-           ONLY:  averaging_interval, dt_data_output, message_string
+           ONLY:  averaging_interval, dt_data_output, initializing_actions,    &
+                  message_string
+                  
+       USE pegrid,                                                             &
+           ONLY:  numprocs_previous_run
 
        IMPLICIT NONE
@@ -4077,5 +4120,5 @@
                 dt_dosurf_av
           CALL message( 'surface_data_output_check_parameters',                &
-                        'PA0087', 1, 2, 0, 6, 0 )
+                        'PA0536', 1, 2, 0, 6, 0 )
        ENDIF
        
@@ -4089,4 +4132,15 @@
        ENDIF
 #endif
+!
+!--   In case of restart runs, check it the number of cores has been changed.
+!--   With surface output this is not allowed.
+      IF ( TRIM( initializing_actions ) == 'read_restart_data'  .AND.          &
+           numprocs_previous_run /= numprocs ) THEN
+         message_string = 'The number of cores has been changed between ' //   &
+                          'restart runs. This is not allowed when surface ' // &
+                          'data output is used.'
+          CALL message( 'surface_data_output_check_parameters',                &
+                        'PA0585', 1, 2, 0, 6, 0 )
+      ENDIF
 !
 !--   Count number of output variables and separate output strings for 
@@ -4121,5 +4175,5 @@
                              ' is not yet implemented in the surface output'
                CALL message( 'surface_data_output_check_parameters',           &
-                             'PA0087', 1, 2, 0, 6, 0 )
+                             'PA0537', 1, 2, 0, 6, 0 )
 
             CASE ( 'us', 'uvw1' )
@@ -4173,5 +4227,5 @@
                              ' is not part of the surface output'
                CALL message( 'surface_data_output_check_parameters',           &
-                             'PA0087', 1, 2, 0, 6, 0 )
+                             'PA0538', 1, 2, 0, 6, 0 )
          END SELECT
 
@@ -4223,4 +4277,118 @@
 
     END SUBROUTINE surface_data_output_last_action    
+    
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This routine reads globally used restart data.
+!------------------------------------------------------------------------------!
+    SUBROUTINE surface_data_output_rrd_global( found )
+
+
+       USE control_parameters,                                                 &
+           ONLY: length, restart_string
+       
+       IMPLICIT NONE
+       
+       LOGICAL, INTENT(OUT)  ::  found !< flag indicating if variable was found
+       
+       found = .TRUE.
+       
+       SELECT CASE ( restart_string(1:length) )
+       
+          CASE ( 'average_count_surf' )
+             READ ( 13 )  average_count_surf
+       
+          CASE DEFAULT
+       
+             found = .FALSE.
+       
+       END SELECT
+
+
+    END SUBROUTINE surface_data_output_rrd_global
+    
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This routine reads the respective restart data.
+!------------------------------------------------------------------------------!
+    SUBROUTINE surface_data_output_rrd_local( i, k, nxlf, nxlc, nxl_on_file,   &
+                              nxrf, nxrc, nxr_on_file, nynf, nync, nyn_on_file,&
+                              nysf, nysc, nys_on_file, found )
+
+
+       USE control_parameters,                                                 &
+           ONLY: length, restart_string
+           
+       IMPLICIT NONE
+
+       INTEGER(iwp)       ::  l                 !< index variable for surface type
+       INTEGER(iwp)       ::  i                 !< running index over input files
+       INTEGER(iwp)       ::  k                 !< running index over previous input files covering current local domain
+       INTEGER(iwp)       ::  ns_h_on_file_usm  !< number of horizontal surface elements (urban type) on file
+       INTEGER(iwp)       ::  nxlc              !< index of left boundary on current subdomain
+       INTEGER(iwp)       ::  nxlf              !< index of left boundary on former subdomain 
+       INTEGER(iwp)       ::  nxl_on_file       !< index of left boundary on former local domain 
+       INTEGER(iwp)       ::  nxrc              !< index of right boundary on current subdomain
+       INTEGER(iwp)       ::  nxrf              !< index of right boundary on former subdomain
+       INTEGER(iwp)       ::  nxr_on_file       !< index of right boundary on former local domain 
+       INTEGER(iwp)       ::  nync              !< index of north boundary on current subdomain
+       INTEGER(iwp)       ::  nynf              !< index of north boundary on former subdomain
+       INTEGER(iwp)       ::  nyn_on_file       !< index of north boundary on former local domain 
+       INTEGER(iwp)       ::  nysc              !< index of south boundary on current subdomain 
+       INTEGER(iwp)       ::  nysf              !< index of south boundary on former subdomain
+       INTEGER(iwp)       ::  nys_on_file       !< index of south boundary on former local domain
+
+       LOGICAL, INTENT(OUT)  ::  found
+!
+!--    Here the reading of user-defined restart data follows:
+!--    Sample for user-defined output
+       found = .TRUE.
+
+       SELECT CASE ( restart_string(1:length) )
+
+          CASE ( 'surfaces%var_av' )
+             IF ( k == 1 )  READ ( 13 )  surfaces%var_av
+             
+          CASE DEFAULT
+
+             found = .FALSE.
+
+          END SELECT
+
+
+    END SUBROUTINE surface_data_output_rrd_local
+    
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This routine writes the respective restart data.
+!------------------------------------------------------------------------------!
+    SUBROUTINE surface_data_output_wrd_global
+
+       IMPLICIT NONE
+
+       CALL wrd_write_string( 'average_count_surf' )
+       WRITE ( 14 )  average_count_surf
+
+    END SUBROUTINE surface_data_output_wrd_global
+    
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This routine writes restart data which individual on each PE
+!------------------------------------------------------------------------------!
+    SUBROUTINE surface_data_output_wrd_local
+
+       IMPLICIT NONE
+
+         IF ( ALLOCATED( surfaces%var_av ) )  THEN
+            CALL wrd_write_string( 'surfaces%var_av' )
+            WRITE ( 14 )  surfaces%var_av
+         ENDIF
+
+
+    END SUBROUTINE surface_data_output_wrd_local
 
 
