Index: palm/trunk/SOURCE/urban_surface_mod.f90
===================================================================
--- palm/trunk/SOURCE/urban_surface_mod.f90	(revision 4693)
+++ palm/trunk/SOURCE/urban_surface_mod.f90	(revision 4694)
@@ -27,4 +27,7 @@
 ! -----------------
 ! $Id$
+! Fix writing and reading of surface data to/from MPI restart file
+!
+! 4693 2020-09-22 19:47:04Z maronga
 ! Bugfix for last commit
 !
@@ -6498,4 +6501,6 @@
                                          global_start )
 
+       IF ( MAXVAL( surf_usm_h(l)%end_index ) <= 0 )  CYCLE
+
        IF ( .NOT.  ALLOCATED( t_surf_wall_h_1(l)%val ) )                                             &
           ALLOCATE( t_surf_wall_h_1(l)%val(1:surf_usm_h(l)%ns) )
@@ -6510,4 +6515,14 @@
        CALL rrd_mpi_io_surface( 't_surf_green_h(' // dum // ')', t_surf_green_h_1(l)%val )
 
+       IF ( .NOT.  ALLOCATED( m_liq_usm_h_1(l)%val ) )                                            &
+          ALLOCATE( m_liq_usm_h_1(l)%val(1:surf_usm_h(l)%ns) )
+       CALL rrd_mpi_io_surface( 'm_liq_usm_h(' // dum // ')', m_liq_usm_h_1(l)%val )
+
+       IF ( indoor_model )  THEN
+          IF ( .NOT.  ALLOCATED( surf_usm_h(l)%waste_heat ) )                                     &
+             ALLOCATE( surf_usm_h(l)%waste_heat(1:surf_usm_h(l)%ns) )
+          CALL rrd_mpi_io_surface( 'waste_heat_h(' // dum // ')', surf_usm_h(l)%waste_heat )
+       ENDIF
+
     ENDDO
     DO  l = 0, 3
@@ -6522,4 +6537,6 @@
                                          global_start )
 
+       IF ( MAXVAL( surf_usm_v(l)%end_index ) <= 0 )  CYCLE
+
        IF ( .NOT.  ALLOCATED( t_surf_wall_v_1(l)%val ) )                                             &
           ALLOCATE( t_surf_wall_v_1(l)%val(1:surf_usm_v(l)%ns) )
@@ -6547,4 +6564,6 @@
                                          global_start )
 
+       IF ( MAXVAL( surf_usm_h(l)%end_index ) <= 0 )  CYCLE
+
        IF ( .NOT.  ALLOCATED( t_wall_h_1(l)%val ) )                                                          &
           ALLOCATE( t_wall_h_1(l)%val(nzb_wall:nzt_wall+1,1:surf_usm_h(l)%ns) )
@@ -6558,4 +6577,5 @@
           ALLOCATE( t_green_h_1(l)%val(nzb_wall:nzt_wall+1,1:surf_usm_h(l)%ns) )
        CALL rrd_mpi_io_surface( 't_green_h(' // dum // ')', t_green_h_1(l)%val )
+
     ENDDO
 
@@ -6570,4 +6590,6 @@
        CALL rd_mpi_io_surface_filetypes( surf_usm_v(l)%start_index, surf_usm_v(l)%end_index, ldum, &
                                          global_start )
+
+       IF ( MAXVAL( surf_usm_v(l)%end_index ) <= 0 )  CYCLE
 
        IF ( .NOT. ALLOCATED( t_wall_v_1(l)%val ) )                                                   &
@@ -7392,5 +7414,4 @@
           WRITE( dum, '(I1)')  l
 
-          END DO
           CALL rd_mpi_io_surface_filetypes( surf_usm_h(l)%start_index, surf_usm_h(l)%end_index,             &
                                             surface_data_to_write, global_start_index )
@@ -7400,12 +7421,16 @@
           CALL wrd_mpi_io( 'usm_global_start_h_' // dum, global_start_index )
 
-          CALL wrd_mpi_io_surface( 't_surf_wall_h_' // dum,  t_surf_wall_h(l)%val )
-          CALL wrd_mpi_io_surface( 't_surf_window_h_' // dum, t_surf_window_h(l)%val )
-          CALL wrd_mpi_io_surface( 't_surf_green_h_' // dum, t_surf_green_h(l)%val )
-
-          CALL wrd_mpi_io_surface( 'm_liq_usm_h_' // dum, m_liq_usm_h(l)%val )
+          IF ( .NOT. surface_data_to_write )  CYCLE
+
+          CALL wrd_mpi_io_surface( 't_surf_wall_h(' // dum // ')',  t_surf_wall_h(l)%val )
+          CALL wrd_mpi_io_surface( 't_surf_window_h(' // dum // ')', t_surf_window_h(l)%val )
+          CALL wrd_mpi_io_surface( 't_surf_green_h(' // dum // ')', t_surf_green_h(l)%val )
+
+          CALL wrd_mpi_io_surface( 'm_liq_usm_h(' // dum // ')', m_liq_usm_h(l)%val )
           IF ( indoor_model )  THEN
-             CALL wrd_mpi_io_surface( 'waste_heat_h_' // dum, surf_usm_h(l)%waste_heat ) ! NEED TO BE CHECKED!!!!!
-          ENDIF
+             CALL wrd_mpi_io_surface( 'waste_heat_h(' // dum // ')', surf_usm_h(l)%waste_heat ) ! NEED TO BE CHECKED!!!!!
+          ENDIF
+
+       ENDDO
 
        DO  l = 0, 3
@@ -7439,7 +7464,9 @@
           CALL wrd_mpi_io( 'usm_global_start_h_2_' // dum, global_start_index )
 
-          CALL wrd_mpi_io_surface( 't_wall_h_' // dum, t_wall_h(l)%val )
-          CALL wrd_mpi_io_surface( 't_window_h_' // dum, t_window_h(l)%val )
-          CALL wrd_mpi_io_surface( 't_green_h_' // dum, t_green_h(l)%val )
+          IF ( .NOT. surface_data_to_write )  CYCLE
+
+          CALL wrd_mpi_io_surface( 't_wall_h(' // dum // ')', t_wall_h(l)%val )
+          CALL wrd_mpi_io_surface( 't_window_h(' // dum // ')', t_window_h(l)%val )
+          CALL wrd_mpi_io_surface( 't_green_h(' // dum // ')', t_green_h(l)%val )
 
        ENDDO
