Index: palm/trunk/SOURCE/data_output_2d.f90
===================================================================
--- palm/trunk/SOURCE/data_output_2d.f90	(revision 1116)
+++ palm/trunk/SOURCE/data_output_2d.f90	(revision 1308)
@@ -20,5 +20,10 @@
 ! Current revisions:
 ! -----------------
-!
+! +local_2d_sections, local_2d_sections_l, ns
+! Check, if the limit of the time dimension is exceeded for parallel output
+! To increase the performance for parallel output, the following is done:
+! - Update of time axis is only done by PE0
+! - Cross sections are first stored on a local array and are written
+!   collectively to the output file by all PEs.
 !
 ! Former revisions:
@@ -143,6 +148,6 @@
     CHARACTER (LEN=25) ::  section_chr
     CHARACTER (LEN=50) ::  rtext
-    INTEGER ::  av, ngp, file_id, i, if, is, iis, j, k, l, layer_xy, n, psi, &
-                s, sender, &
+    INTEGER ::  av, ngp, file_id, i, if, is, iis, j, k, l, layer_xy, n, ns, &
+                psi, s, sender, &
                 ind(4)
     LOGICAL ::  found, resorted, two_d
@@ -150,5 +155,6 @@
     REAL, DIMENSION(:), ALLOCATABLE ::      level_z
     REAL, DIMENSION(:,:), ALLOCATABLE ::    local_2d, local_2d_l
-    REAL, DIMENSION(:,:,:), ALLOCATABLE ::  local_pf
+    REAL, DIMENSION(:,:,:), ALLOCATABLE ::  local_pf, local_2d_sections, &
+                                            local_2d_sections_l
 #if defined( __parallel )
     REAL, DIMENSION(:,:),   ALLOCATABLE ::  total_2d
@@ -157,6 +163,4 @@
 
     NAMELIST /LOCAL/  rtext
-
-    CALL cpu_log (log_point(3),'data_output_2d','start')
 
 !
@@ -166,4 +170,36 @@
     IF ( mode == 'xz'  .AND.  .NOT. data_output_xz(av) )  RETURN
     IF ( mode == 'yz'  .AND.  .NOT. data_output_yz(av) )  RETURN
+!
+!-- For parallel netcdf output the time axis must be limited. Return, if this
+!-- limit is exceeded. This could be the case, if the simulated time exceeds 
+!-- the given end time by the length of the given output interval.
+    IF ( netcdf_data_format > 4 )  THEN
+       IF ( mode == 'xy'  .AND.  do2d_xy_time_count(av) + 1 > &
+            ntdim_2d_xy(av) )  THEN
+          WRITE ( message_string, * ) 'Output of xy cross-sections is not ', &
+                          'given at t=', simulated_time, '&because the', & 
+                          ' maximum number of output time levels is exceeded.'
+          CALL message( 'data_output_2d', 'PA0384', 0, 1, 0, 6, 0 )          
+          RETURN
+       ENDIF
+       IF ( mode == 'xz'  .AND.  do2d_xz_time_count(av) + 1 > &
+            ntdim_2d_xz(av) )  THEN
+          WRITE ( message_string, * ) 'Output of xz cross-sections is not ',  &
+                          'given at t=', simulated_time, '&because the', & 
+                          ' maximum number of output time levels is exceeded.'
+          CALL message( 'data_output_2d', 'PA0385', 0, 1, 0, 6, 0 )          
+          RETURN
+       ENDIF
+       IF ( mode == 'yz'  .AND.  do2d_yz_time_count(av) + 1 > &
+            ntdim_2d_yz(av) )  THEN
+          WRITE ( message_string, * ) 'Output of yz cross-sections is not ', &
+                          'given at t=', simulated_time, '&because the', & 
+                          ' maximum number of output time levels is exceeded.'
+          CALL message( 'data_output_2d', 'PA0386', 0, 1, 0, 6, 0 )          
+          RETURN
+       ENDIF
+    ENDIF
+
+    CALL cpu_log (log_point(3),'data_output_2d','start')
 
     two_d = .FALSE.    ! local variable to distinguish between output of pure 2D
@@ -178,4 +214,14 @@
           s = 1
           ALLOCATE( level_z(nzb:nzt+1), local_2d(nxlg:nxrg,nysg:nyng) )
+
+          IF ( netcdf_data_format > 4 )  THEN
+             ns = 1
+             DO WHILE ( section(ns,s) /= -9999  .AND.  ns <= 100 )
+                ns = ns + 1
+             ENDDO
+             ns = ns - 1
+             ALLOCATE( local_2d_sections(nxlg:nxrg,nysg:nyng,1:ns) )
+             local_2d_sections = 0.0
+          ENDIF
 
 !
@@ -201,4 +247,15 @@
           ALLOCATE( local_2d(nxlg:nxrg,nzb:nzt+1) )
 
+          IF ( netcdf_data_format > 4 )  THEN
+             ns = 1
+             DO WHILE ( section(ns,s) /= -9999  .AND.  ns <= 100 )
+                ns = ns + 1
+             ENDDO
+             ns = ns - 1
+             ALLOCATE( local_2d_sections(nxlg:nxrg,1:ns,nzb:nzt+1) )
+             ALLOCATE( local_2d_sections_l(nxlg:nxrg,1:ns,nzb:nzt+1) )
+             local_2d_sections = 0.0; local_2d_sections_l = 0.0
+          ENDIF
+
 !
 !--       Parallel netCDF4/HDF5 output is done on all PEs, all other on PE0 only
@@ -220,7 +277,17 @@
 
        CASE ( 'yz' )
-
           s = 3
           ALLOCATE( local_2d(nysg:nyng,nzb:nzt+1) )
+
+          IF ( netcdf_data_format > 4 )  THEN
+             ns = 1
+             DO WHILE ( section(ns,s) /= -9999  .AND.  ns <= 100 )
+                ns = ns + 1
+             ENDDO
+             ns = ns - 1
+             ALLOCATE( local_2d_sections(1:ns,nysg:nyng,nzb:nzt+1) )
+             ALLOCATE( local_2d_sections_l(1:ns,nysg:nyng,nzb:nzt+1) )
+             local_2d_sections = 0.0; local_2d_sections_l = 0.0
+          ENDIF
 
 !
@@ -243,5 +310,4 @@
 
        CASE DEFAULT
-
           message_string = 'unknown cross-section: ' // TRIM( mode )
           CALL message( 'data_output_2d', 'PA0180', 1, 2, 0, 6, 0 )
@@ -799,9 +865,11 @@
 
 !
-!--                Update the netCDF xy cross section time axis
-                   IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
-                      IF ( simulated_time /= do2d_xy_last_time(av) )  THEN
-                         do2d_xy_time_count(av) = do2d_xy_time_count(av) + 1
-                         do2d_xy_last_time(av)  = simulated_time
+!--                Update the netCDF xy cross section time axis.
+!--                In case of parallel output, this is only done by PE0
+!--                to increase the performance.
+                   IF ( simulated_time /= do2d_xy_last_time(av) )  THEN
+                      do2d_xy_time_count(av) = do2d_xy_time_count(av) + 1
+                      do2d_xy_last_time(av)  = simulated_time
+                      IF ( myid == 0 )  THEN
                          IF ( ( .NOT. data_output_2d_on_each_pe  .AND. &
                               netcdf_output )  .OR.  netcdf_data_format > 4 ) &
@@ -846,6 +914,4 @@
 !
 !--                   Parallel output in netCDF4/HDF5 format.
-!--                   Do not output redundant ghost point data except for the
-!--                   boundaries of the total domain.
                       IF ( two_d ) THEN
                          iis = 1
@@ -855,39 +921,13 @@
 
 #if defined( __netcdf )
-                      IF ( nxr == nx  .AND.  nyn /= ny )  THEN
-                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
-                                                 id_var_do2d(av,if),           &
-                                                 local_2d(nxl:nxr+1,nys:nyn),  &
-                                                 start = (/ nxl+1, nys+1, iis, &
-                                                    do2d_xy_time_count(av) /), &
-                                                 count = (/ nxr-nxl+2,         &
-                                                            nyn-nys+1, 1, 1 /) )
-                      ELSEIF ( nxr /= nx  .AND.  nyn == ny )  THEN
-                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
-                                                 id_var_do2d(av,if),           &
-                                                 local_2d(nxl:nxr,nys:nyn+1),  &
-                                                 start = (/ nxl+1, nys+1, iis, &
-                                                    do2d_xy_time_count(av) /), &
-                                                 count = (/ nxr-nxl+1,         &
-                                                            nyn-nys+2, 1, 1 /) )
-                      ELSEIF ( nxr == nx  .AND.  nyn == ny )  THEN
-                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
-                                                 id_var_do2d(av,if),           &
-                                                 local_2d(nxl:nxr+1,nys:nyn+1),&
-                                                 start = (/ nxl+1, nys+1, iis, &
-                                                    do2d_xy_time_count(av) /), &
-                                                 count = (/ nxr-nxl+2,          &
-                                                            nyn-nys+2, 1, 1 /) )
-                      ELSE
-                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
-                                                 id_var_do2d(av,if),           &
-                                                 local_2d(nxl:nxr,nys:nyn),    &
-                                                 start = (/ nxl+1, nys+1, iis, &
-                                                    do2d_xy_time_count(av) /), &
-                                                 count = (/ nxr-nxl+1,         &
-                                                            nyn-nys+1, 1, 1 /) )
-                      ENDIF
-
-                      CALL handle_netcdf_error( 'data_output_2d', 55 )
+!
+!--                   For parallel output, all cross sections are first stored
+!--                   here on a local array and will be written to the output
+!--                   file afterwards to increase the performance.
+                      DO  i = nxlg, nxrg
+                         DO  j = nysg, nyng
+                            local_2d_sections(i,j,iis) = local_2d(i,j)
+                         ENDDO
+                      ENDDO
 #endif
                    ELSE
@@ -1047,10 +1087,11 @@
                 CASE ( 'xz' )
 !
-!--                Update the netCDF xz cross section time axis
-                   IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
-
-                      IF ( simulated_time /= do2d_xz_last_time(av) )  THEN
-                         do2d_xz_time_count(av) = do2d_xz_time_count(av) + 1
-                         do2d_xz_last_time(av)  = simulated_time
+!--                Update the netCDF xz cross section time axis.
+!--                In case of parallel output, this is only done by PE0
+!--                to increase the performance.
+                   IF ( simulated_time /= do2d_xz_last_time(av) )  THEN
+                      do2d_xz_time_count(av) = do2d_xz_time_count(av) + 1
+                      do2d_xz_last_time(av)  = simulated_time
+                      IF ( myid == 0 )  THEN
                          IF ( ( .NOT. data_output_2d_on_each_pe  .AND.        &
                               netcdf_output )  .OR.  netcdf_data_format > 4 ) &
@@ -1066,5 +1107,4 @@
                          ENDIF
                       ENDIF
-
                    ENDIF
 
@@ -1114,35 +1154,4 @@
                    IF ( netcdf_output  .AND.  netcdf_data_format > 4 )  THEN
 !
-!--                   ATTENTION: The following lines are a workaround, because
-!--                              independet output does not work with the
-!--                              current netCDF4 installation. Therefore, data
-!--                              are transferred from PEs having the cross
-!--                              sections to other PEs along y having no cross
-!--                              section data. Some of these data are the
-!--                              output.
-!--                   BEGIN WORKAROUND---------------------------------------
-                      IF ( npey /= 1  .AND.  section(is,s) /= -1)  THEN
-                         ALLOCATE( local_2d_l(nxlg:nxrg,nzb:nzt+1) )
-                         local_2d_l = 0.0
-                         IF ( section(is,s) >= nys .AND. section(is,s) <= nyn )&
-                         THEN
-                            local_2d_l = local_2d
-                         ENDIF
-#if defined( __parallel )
-!
-!--                      Distribute data over all PEs along y
-                         ngp = ( nxrg-nxlg+1 ) * ( nzt-nzb+2 )
-                         IF ( collective_wait ) CALL MPI_BARRIER( comm2d, ierr )
-                         CALL MPI_ALLREDUCE( local_2d_l(nxlg,nzb),            &
-                                             local_2d(nxlg,nzb), ngp,         &
-                                             MPI_REAL, MPI_SUM, comm1dy, ierr )
-#else
-                         local_2d = local_2d_l
-#endif
-                         DEALLOCATE( local_2d_l )
-                      ENDIF
-!--                   END WORKAROUND-----------------------------------------
-
-!
 !--                   Output in netCDF4/HDF5 format.
 !--                   Output only on those PEs where the respective cross
@@ -1152,41 +1161,14 @@
                              section(is,s) <= nyn )  .OR.  &
                            ( section(is,s) == -1  .AND.  myidy == 0 ) )  THEN
-!
-!--                      Do not output redundant ghost point data except for the
-!--                      boundaries of the total domain.
 #if defined( __netcdf )
-                         IF ( nxr == nx )  THEN
-                            nc_stat = NF90_PUT_VAR( id_set_xz(av),             &
-                                                id_var_do2d(av,if),            &
-                                                local_2d(nxl:nxr+1,nzb:nzt+1), &
-                                                start = (/ nxl+1, is, 1,       &
-                                                    do2d_xz_time_count(av) /), &
-                                                count = (/ nxr-nxl+2, 1,       &
-                                                           nzt+2, 1 /) )
-                         ELSE
-                            nc_stat = NF90_PUT_VAR( id_set_xz(av),             &
-                                                id_var_do2d(av,if),            &
-                                                local_2d(nxl:nxr,nzb:nzt+1),   &
-                                                start = (/ nxl+1, is, 1,       &
-                                                    do2d_xz_time_count(av) /), &
-                                                count = (/ nxr-nxl+1, 1,       &
-                                                           nzt+2, 1 /) )
-                         ENDIF
-
-                         CALL handle_netcdf_error( 'data_output_2d', 57 )
-
-                      ELSE
-!
-!--                      Output on other PEs. Only one point is output!!
-!--                      ATTENTION: This is a workaround (see above)!!
-                         IF ( npey /= 1 )  THEN
-                            nc_stat = NF90_PUT_VAR( id_set_xz(av),             &
-                                                    id_var_do2d(av,if),        &
-                                                    local_2d(nxl:nxl,nzb:nzb), &
-                                                    start = (/ nxl+1, is, 1,   &
-                                                    do2d_xz_time_count(av) /), &
-                                                    count = (/ 1, 1, 1, 1 /) )
-                            CALL handle_netcdf_error( 'data_output_2d', 451 )
-                         ENDIF
+!
+!--                      For parallel output, all cross sections are first
+!--                      stored here on a local array and will be written to the
+!--                      output file afterwards to increase the performance.
+                         DO  i = nxlg, nxrg
+                            DO  k = nzb, nzt+1
+                               local_2d_sections_l(i,is,k) = local_2d(i,k)
+                            ENDDO
+                         ENDDO
 #endif
                       ENDIF
@@ -1357,10 +1339,11 @@
                 CASE ( 'yz' )
 !
-!--                Update the netCDF yz cross section time axis
-                   IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
-
-                      IF ( simulated_time /= do2d_yz_last_time(av) )  THEN
-                         do2d_yz_time_count(av) = do2d_yz_time_count(av) + 1
-                         do2d_yz_last_time(av)  = simulated_time
+!--                Update the netCDF yz cross section time axis.
+!--                In case of parallel output, this is only done by PE0
+!--                to increase the performance.
+                   IF ( simulated_time /= do2d_yz_last_time(av) )  THEN
+                      do2d_yz_time_count(av) = do2d_yz_time_count(av) + 1
+                      do2d_yz_last_time(av)  = simulated_time
+                      IF ( myid == 0 )  THEN
                          IF ( ( .NOT. data_output_2d_on_each_pe  .AND.        &
                               netcdf_output )  .OR.  netcdf_data_format > 4 ) &
@@ -1376,6 +1359,6 @@
                          ENDIF
                       ENDIF
-
-                   ENDIF
+                   ENDIF
+
 !
 !--                If required, carry out averaging along x
@@ -1423,35 +1406,4 @@
                    IF ( netcdf_output  .AND.  netcdf_data_format > 4 )  THEN
 !
-!--                   ATTENTION: The following lines are a workaround, because
-!--                              independet output does not work with the
-!--                              current netCDF4 installation. Therefore, data
-!--                              are transferred from PEs having the cross
-!--                              sections to other PEs along y having no cross
-!--                              section data. Some of these data are the
-!--                              output.
-!--                   BEGIN WORKAROUND---------------------------------------
-                      IF ( npex /= 1  .AND.  section(is,s) /= -1)  THEN
-                         ALLOCATE( local_2d_l(nysg:nyng,nzb:nzt+1) )
-                         local_2d_l = 0.0
-                         IF ( section(is,s) >= nxl .AND. section(is,s) <= nxr )&
-                         THEN
-                            local_2d_l = local_2d
-                         ENDIF
-#if defined( __parallel )
-!
-!--                      Distribute data over all PEs along x
-                         ngp = ( nyng-nysg+1 ) * ( nzt-nzb + 2 )
-                         IF ( collective_wait ) CALL MPI_BARRIER( comm2d, ierr )
-                         CALL MPI_ALLREDUCE( local_2d_l(nysg,nzb),            &
-                                             local_2d(nysg,nzb), ngp,         &
-                                             MPI_REAL, MPI_SUM, comm1dx, ierr )
-#else
-                         local_2d = local_2d_l
-#endif
-                         DEALLOCATE( local_2d_l )
-                      ENDIF
-!--                   END WORKAROUND-----------------------------------------
-
-!
 !--                   Output in netCDF4/HDF5 format.
 !--                   Output only on those PEs where the respective cross
@@ -1461,41 +1413,14 @@
                              section(is,s) <= nxr )  .OR.  &
                            ( section(is,s) == -1  .AND.  myidx == 0 ) )  THEN
-!
-!--                      Do not output redundant ghost point data except for the
-!--                      boundaries of the total domain.
 #if defined( __netcdf )
-                         IF ( nyn == ny )  THEN
-                            nc_stat = NF90_PUT_VAR( id_set_yz(av),             &
-                                                id_var_do2d(av,if),            &
-                                                local_2d(nys:nyn+1,nzb:nzt+1), &
-                                                start = (/ is, nys+1, 1,       &
-                                                    do2d_yz_time_count(av) /), &
-                                                count = (/ 1, nyn-nys+2,       &
-                                                           nzt+2, 1 /) )
-                         ELSE
-                            nc_stat = NF90_PUT_VAR( id_set_yz(av),             &
-                                                id_var_do2d(av,if),            &
-                                                local_2d(nys:nyn,nzb:nzt+1),   &
-                                                start = (/ is, nys+1, 1,       &
-                                                    do2d_yz_time_count(av) /), &
-                                                count = (/ 1, nyn-nys+1,       &
-                                                           nzt+2, 1 /) )
-                         ENDIF
-
-                         CALL handle_netcdf_error( 'data_output_2d', 60 )
-
-                      ELSE
-!
-!--                      Output on other PEs. Only one point is output!!
-!--                      ATTENTION: This is a workaround (see above)!!
-                         IF ( npex /= 1 )  THEN
-                            nc_stat = NF90_PUT_VAR( id_set_yz(av),             &
-                                                    id_var_do2d(av,if),        &
-                                                    local_2d(nys:nys,nzb:nzb), &
-                                                    start = (/ is, nys+1, 1,   &
-                                                    do2d_yz_time_count(av) /), &
-                                                    count = (/ 1, 1, 1, 1 /) )
-                            CALL handle_netcdf_error( 'data_output_2d', 452 )
-                         ENDIF
+!
+!--                      For parallel output, all cross sections are first
+!--                      stored here on a local array and will be written to the
+!--                      output file afterwards to increase the performance.
+                         DO  j = nysg, nyng
+                            DO  k = nzb, nzt+1
+                               local_2d_sections_l(is,j,k) = local_2d(j,k)
+                            ENDDO
+                         ENDDO
 #endif
                       ENDIF
@@ -1669,4 +1594,174 @@
           ENDDO loop1
 
+!
+!--       For parallel output, all data were collected before on a local array
+!--       and are written now to the netcdf file. This must be done to increase
+!--       the performance of the parallel output.
+#if defined( __netcdf )
+          IF ( netcdf_output .AND. netcdf_data_format > 4 )  THEN
+
+                SELECT CASE ( mode )
+
+                   CASE ( 'xy' )
+                      IF ( two_d ) THEN
+                         iis = 1
+                      ELSE
+                         iis = is-1
+                      ENDIF
+!
+!--                   Do not output redundant ghost point data except for the
+!--                   boundaries of the total domain.
+                      IF ( nxr == nx  .AND.  nyn /= ny )  THEN
+                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
+                                                 id_var_do2d(av,if),           &
+                                                 local_2d_sections(nxl:nxr+1,  &
+                                                    nys:nyn,1:ns),             &
+                                                 start = (/ nxl+1, nys+1, 1,   &
+                                                    do2d_xy_time_count(av) /), &
+                                                 count = (/ nxr-nxl+2,         &
+                                                            nyn-nys+1, ns, 1   &
+                                                          /) )
+                      ELSEIF ( nxr /= nx  .AND.  nyn == ny )  THEN
+                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
+                                                 id_var_do2d(av,if),           &
+                                                 local_2d_sections(nxl:nxr,    &
+                                                    nys:nyn+1,1:ns),           &
+                                                 start = (/ nxl+1, nys+1, 1,   &
+                                                    do2d_xy_time_count(av) /), &
+                                                 count = (/ nxr-nxl+1,         &
+                                                            nyn-nys+2, ns, 1   &
+                                                          /) )
+                      ELSEIF ( nxr == nx  .AND.  nyn == ny )  THEN
+                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
+                                                 id_var_do2d(av,if),           &
+                                                 local_2d_sections(nxl:nxr+1,  &
+                                                    nys:nyn+1,1:ns),           &
+                                                 start = (/ nxl+1, nys+1, 1,   &
+                                                    do2d_xy_time_count(av) /), &
+                                                 count = (/ nxr-nxl+2,         &
+                                                            nyn-nys+2, ns, 1   &
+                                                          /) )
+                      ELSE
+                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
+                                                 id_var_do2d(av,if),           &
+                                                 local_2d_sections(nxl:nxr,    &
+                                                    nys:nyn,1:ns),             &
+                                                 start = (/ nxl+1, nys+1, 1,   &
+                                                    do2d_xy_time_count(av) /), &
+                                                 count = (/ nxr-nxl+1,         &
+                                                            nyn-nys+1, ns, 1   &
+                                                          /) )
+                      ENDIF    
+
+                      CALL handle_netcdf_error( 'data_output_2d', 55 ) 
+
+                   CASE ( 'xz' )
+!
+!--                   First, all PEs get the information of all cross-sections.
+!--                   Then the data are written to the output file by all PEs
+!--                   while NF90_COLLECTIVE is set in subroutine 
+!--                   define_netcdf_header. Although redundant information are
+!--                   written to the output file in that case, the performance 
+!--                   is significantly better compared to the case where only
+!--                   the first row of PEs in x-direction (myidx = 0) is given
+!--                   the output while NF90_INDEPENDENT is set.
+                      IF ( npey /= 1 )  THEN
+                         
+#if defined( __parallel )
+!
+!--                      Distribute data over all PEs along y
+                         ngp = ( nxrg-nxlg+1 ) * ( nzt-nzb+2 ) * ns
+                         IF ( collective_wait ) CALL MPI_BARRIER( comm2d, ierr )
+                         CALL MPI_ALLREDUCE( local_2d_sections_l(nxlg,1,nzb),  &
+                                             local_2d_sections(nxlg,1,nzb),    &
+                                             ngp, MPI_REAL, MPI_SUM, comm1dy,  &
+                                             ierr )
+#else
+                         local_2d_sections = local_2d_sections_l
+#endif
+                      ENDIF
+!
+!--                   Do not output redundant ghost point data except for the
+!--                   boundaries of the total domain.
+                      IF ( nxr == nx )  THEN
+                         nc_stat = NF90_PUT_VAR( id_set_xz(av),                &
+                                             id_var_do2d(av,if),               & 
+                                             local_2d_sections(nxl:nxr+1,1:ns, &
+                                                nzb:nzt+1),                    &
+                                             start = (/ nxl+1, 1, 1,           &
+                                                do2d_xz_time_count(av) /),     &
+                                             count = (/ nxr-nxl+2, ns, nzt+2,  &
+                                                        1 /) )
+                      ELSE
+                         nc_stat = NF90_PUT_VAR( id_set_xz(av),                &
+                                             id_var_do2d(av,if),               &
+                                             local_2d_sections(nxl:nxr,1:ns,   &
+                                                nzb:nzt+1),                    &
+                                             start = (/ nxl+1, 1, 1,           &
+                                                do2d_xz_time_count(av) /),     &
+                                             count = (/ nxr-nxl+1, ns, nzt+2,  &
+                                                1 /) )
+                      ENDIF
+
+                      CALL handle_netcdf_error( 'data_output_2d', 57 )
+
+                   CASE ( 'yz' )
+!
+!--                   First, all PEs get the information of all cross-sections.
+!--                   Then the data are written to the output file by all PEs
+!--                   while NF90_COLLECTIVE is set in subroutine 
+!--                   define_netcdf_header. Although redundant information are
+!--                   written to the output file in that case, the performance 
+!--                   is significantly better compared to the case where only
+!--                   the first row of PEs in y-direction (myidy = 0) is given
+!--                   the output while NF90_INDEPENDENT is set.
+                      IF ( npex /= 1 )  THEN
+
+#if defined( __parallel )
+!
+!--                      Distribute data over all PEs along x
+                         ngp = ( nyng-nysg+1 ) * ( nzt-nzb + 2 ) * ns
+                         IF ( collective_wait ) CALL MPI_BARRIER( comm2d, ierr )
+                         CALL MPI_ALLREDUCE( local_2d_sections_l(1,nysg,nzb),  &
+                                             local_2d_sections(1,nysg,nzb),    &
+                                             ngp, MPI_REAL, MPI_SUM, comm1dx,  &
+                                             ierr )
+#else
+                         local_2d_sections = local_2d_sections_l
+#endif
+                      ENDIF
+!
+!--                   Do not output redundant ghost point data except for the
+!--                   boundaries of the total domain.
+                      IF ( nyn == ny )  THEN
+                         nc_stat = NF90_PUT_VAR( id_set_yz(av),                &
+                                             id_var_do2d(av,if),               &
+                                             local_2d_sections(1:ns,           &
+                                                nys:nyn+1,nzb:nzt+1),          &
+                                             start = (/ 1, nys+1, 1,           &
+                                                do2d_yz_time_count(av) /),     &
+                                             count = (/ ns, nyn-nys+2,         &
+                                                        nzt+2, 1 /) )
+                      ELSE
+                         nc_stat = NF90_PUT_VAR( id_set_yz(av),                &
+                                             id_var_do2d(av,if),               &
+                                             local_2d_sections(1:ns,nys:nyn,   &
+                                                nzb:nzt+1),                    &
+                                             start = (/ 1, nys+1, 1,           &
+                                                do2d_yz_time_count(av) /),     &
+                                             count = (/ ns, nyn-nys+1,         &
+                                                        nzt+2, 1 /) )
+                      ENDIF
+
+                      CALL handle_netcdf_error( 'data_output_2d', 60 )
+
+                   CASE DEFAULT
+                      message_string = 'unknown cross-section: ' // TRIM( mode )
+                      CALL message( 'data_output_2d', 'PA0180', 1, 2, 0, 6, 0 )
+
+                END SELECT                     
+
+          ENDIF
+
        ENDIF
 
@@ -1680,5 +1775,8 @@
 !-- Deallocate temporary arrays.
     IF ( ALLOCATED( level_z ) )  DEALLOCATE( level_z )
-    DEALLOCATE( local_pf, local_2d )
+    IF ( netcdf_data_format > 4 )  THEN
+       DEALLOCATE( local_pf, local_2d, local_2d_sections )
+       IF( mode == 'xz' .OR. mode == 'yz' ) DEALLOCATE( local_2d_sections_l )
+    ENDIF
 #if defined( __parallel )
     IF ( .NOT.  data_output_2d_on_each_pe  .AND.  myid == 0 )  THEN
@@ -1704,5 +1802,4 @@
     ENDIF
 
-
     CALL cpu_log (log_point(3),'data_output_2d','stop','nobarrier')
 
