Index: palm/trunk/SOURCE/data_output_netcdf4_module.f90
===================================================================
--- palm/trunk/SOURCE/data_output_netcdf4_module.f90	(revision 4106)
+++ palm/trunk/SOURCE/data_output_netcdf4_module.f90	(revision 4107)
@@ -56,4 +56,5 @@
 
    CHARACTER(LEN=800) ::  internal_error_message = ''  !< string containing the last error message
+   CHARACTER(LEN=100) ::  file_suffix = ''             !< file suffix added to each file name
    CHARACTER(LEN=800) ::  temp_string                  !< dummy string
 
@@ -63,4 +64,6 @@
    INTEGER(iwp) ::  debug_output_unit       !< Fortran Unit Number of the debug-output file
    INTEGER(iwp) ::  global_id_in_file = -1  !< value of global ID within a file
+   INTEGER      ::  master_rank             !< master rank for tasks to be executed by single PE only
+   INTEGER      ::  output_group_comm       !< MPI communicator addressing all MPI ranks which participate in output
 
    LOGICAL ::  print_debug_output = .FALSE.  !< if true, debug output is printed
@@ -126,7 +129,14 @@
 !> Initialize data-output module.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE netcdf4_init_module( program_debug_output_unit, debug_output, dom_global_id )
+SUBROUTINE netcdf4_init_module( file_suffix_of_output_group, mpi_comm_of_output_group, &
+                                master_output_rank,                                    &
+                                program_debug_output_unit, debug_output, dom_global_id )
+
+   CHARACTER(LEN=*), INTENT(IN) ::  file_suffix_of_output_group  !> file-name suffix added to each file;
+                                                                 !> must be unique for each output group
 
    INTEGER(iwp), INTENT(IN) ::  dom_global_id              !< global id within a file defined by DOM
+   INTEGER,      INTENT(IN) ::  master_output_rank         !< MPI rank executing tasks which must be executed by a single PE
+   INTEGER,      INTENT(IN) ::  mpi_comm_of_output_group   !< MPI communicator specifying the rank group participating in output
    INTEGER(iwp), INTENT(IN) ::  program_debug_output_unit  !< file unit number for debug output
 
@@ -134,6 +144,9 @@
 
 
+   file_suffix = file_suffix_of_output_group
+   output_group_comm = mpi_comm_of_output_group
+   master_rank = master_output_rank
+
    debug_output_unit = program_debug_output_unit
-
    print_debug_output = debug_output
 
@@ -155,4 +168,5 @@
 
    INTEGER(iwp), INTENT(OUT) ::  file_id       !< file ID
+   INTEGER                   ::  my_rank       !< MPI rank of processor
    INTEGER(iwp)              ::  nc_stat       !< netcdf return value
    INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
@@ -160,4 +174,5 @@
 
    return_value = 0
+   file_id = -1
 
    !-- Open new file
@@ -167,9 +182,26 @@
 
 #if defined( __netcdf4 )
-      nc_stat = NF90_CREATE( TRIM( filename ),                    &
-                             IOR( NF90_NOCLOBBER, NF90_NETCDF4 ), &
-                             file_id )
-#else
-      file_id = -1
+#if defined( __parallel )
+      CALL MPI_COMM_RANK( output_group_comm, my_rank, return_value )
+      IF ( return_value /= 0 )  THEN
+         CALL internal_message( 'error', routine_name // ': MPI_COMM_RANK error' )
+      ENDIF
+      IF ( my_rank /= master_rank )  THEN
+         return_value = 1
+         CALL internal_message( 'error', routine_name // &
+                                ': trying to define a NetCDF file in serial mode by an MPI ' // &
+                                'rank other than the master output rank. Serial NetCDF ' // &
+                                'files can only be defined by the master output rank!' )
+      ENDIF
+#else
+      my_rank = master_rank
+      return_value = 0
+#endif
+
+      IF ( return_value == 0 )  &
+         nc_stat = NF90_CREATE( TRIM( filename ) // TRIM( file_suffix ), &
+                                IOR( NF90_NOCLOBBER, NF90_NETCDF4 ), &
+                                file_id )
+#else
       nc_stat = 0
       return_value = 1
@@ -182,9 +214,8 @@
 
 #if defined( __parallel ) && defined( __netcdf4 ) && defined( __netcdf4_parallel )
-      nc_stat = NF90_CREATE( TRIM( filename ),                                       &
+      nc_stat = NF90_CREATE( TRIM( filename ) // TRIM( file_suffix ),                &
                              IOR( NF90_NOCLOBBER, IOR( NF90_NETCDF4, NF90_MPIIO ) ), &
-                             file_id, COMM = MPI_COMM_WORLD, INFO = MPI_INFO_NULL )
-#else
-      file_id = -1
+                             file_id, COMM = output_group_comm, INFO = MPI_INFO_NULL )
+#else
       nc_stat = 0
       return_value = 1
@@ -196,5 +227,4 @@
 
    ELSE
-      file_id = -1
       nc_stat = 0
       return_value = 1
@@ -205,5 +235,5 @@
 
 #if defined( __netcdf4 )
-   IF ( nc_stat /= NF90_NOERR )  THEN
+   IF ( nc_stat /= NF90_NOERR  .AND.  return_value == 0 )  THEN
       return_value = 1
       CALL internal_message( 'error', routine_name // ': NetCDF error while opening file "' // &
@@ -477,5 +507,5 @@
    INTEGER(iwp)              ::  d             !< loop index
    INTEGER(iwp), INTENT(IN)  ::  file_id       !< file ID
-   INTEGER(iwp)              ::  myid          !< id number of processor element
+   INTEGER                   ::  my_rank       !< MPI rank of processor
    INTEGER(iwp)              ::  nc_stat       !< netcdf return value
    INTEGER(iwp)              ::  ndim          !< number of dimensions of variable in file
@@ -532,14 +562,14 @@
 
 #if defined( __parallel )
-   CALL MPI_COMM_RANK( MPI_COMM_WORLD, myid, return_value )
+   CALL MPI_COMM_RANK( output_group_comm, my_rank, return_value )
    IF ( return_value /= 0 )  THEN
       CALL internal_message( 'error', routine_name // ': MPI_COMM_RANK error' )
    ENDIF
 #else
-   myid = 0
+   my_rank = master_rank
    return_value = 0
 #endif
 
-   IF ( return_value == 0  .AND.  ( .NOT. is_global  .OR.  myid == 0 ) )  THEN
+   IF ( return_value == 0  .AND.  ( .NOT. is_global  .OR.  my_rank == master_rank ) )  THEN
 
       WRITE( temp_string, * ) var_id
