Index: palm/trunk/SOURCE/data_output_binary_module.f90
===================================================================
--- palm/trunk/SOURCE/data_output_binary_module.f90	(revision 4106)
+++ palm/trunk/SOURCE/data_output_binary_module.f90	(revision 4107)
@@ -56,6 +56,7 @@
    CHARACTER(LEN=*), PARAMETER ::  config_file_name = 'BINARY_TO_NETCDF_CONFIG'  !< name of config file
    CHARACTER(LEN=*), PARAMETER ::  mode_binary = 'binary'                        !< string to select operation mode of module
-   CHARACTER(LEN=*), PARAMETER ::  prefix = 'BIN_'                               !< file prefix for binary files
-
+   CHARACTER(LEN=*), PARAMETER ::  file_prefix = 'BIN_'                          !< file prefix for binary files
+
+   CHARACTER(LEN=charlen)      ::  file_suffix = ''             !< file suffix added to each file name
    CHARACTER(LEN=800)          ::  internal_error_message = ''  !< string containing the last error message
    CHARACTER(LEN=800)          ::  temp_string                  !< dummy string
@@ -65,5 +66,7 @@
    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(iwp) ::  next_available_unit             !< next unit number available for new file
+   INTEGER      ::  output_group_comm               !< MPI communicator addressing all MPI ranks which participate in output
 
    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  files_highest_var_id  !< highest assigned ID of variable or dimension in a file
@@ -133,7 +136,14 @@
 !> Initialize data-output module.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE binary_init_module( program_debug_output_unit, debug_output, dom_global_id )
+SUBROUTINE binary_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
 
@@ -141,10 +151,12 @@
 
 
+   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
 
    global_id_in_file = dom_global_id
-
 
 END SUBROUTINE binary_init_module
@@ -159,5 +171,5 @@
    CHARACTER(LEN=charlen)             ::  bin_filename = ''  !< actual name of binary file
    CHARACTER(LEN=charlen), INTENT(IN) ::  filename           !< name of file
-   CHARACTER(LEN=7)                   ::  myid_char          !< string containing value of myid with leading zeros
+   CHARACTER(LEN=7)                   ::  my_rank_char       !< string containing value of my_rank with leading zeros
    CHARACTER(LEN=*),       INTENT(IN) ::  mode               !< operation mode
 
@@ -165,5 +177,6 @@
 
    INTEGER(iwp), INTENT(OUT) ::  file_id       !< file ID
-   INTEGER(iwp)              ::  myid          !< id of local processor id (MPI rank)
+   INTEGER                   ::  my_rank       !< MPI rank of local processor
+   INTEGER                   ::  nrank         !< number of MPI ranks participating in output
    INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
 
@@ -176,13 +189,15 @@
 
 #if defined( __parallel )
-   CALL MPI_COMM_RANK( MPI_COMM_WORLD, myid, return_value )
+   CALL MPI_COMM_SIZE( output_group_comm, nrank, return_value )
+   IF ( return_value == 0 )  CALL MPI_COMM_RANK( output_group_comm, my_rank, return_value )
    IF ( return_value == 0 )  THEN
-      WRITE( myid_char, '("_",I6.6)' )  myid
+      WRITE( my_rank_char, '("_",I6.6)' )  my_rank
    ELSE
-      CALL internal_message( 'debug', routine_name // ': MPI_COMM_RANK error' )
+      CALL internal_message( 'error', routine_name // ': MPI error' )
    ENDIF
 #else
-   myid = 0
-   myid_char = '_' // REPEAT('0', 6)
+   nrank = 1
+   my_rank = master_rank
+   WRITE( my_rank_char, '("_",I6.6)' )  my_rank
 #endif
 
@@ -196,17 +211,20 @@
       config_file_unit = binary_file_lowest_unit
 
-      IF ( myid == 0 )  THEN
+      IF ( my_rank == master_rank )  THEN
 
          !-- Remove any pre-existing file
-         INQUIRE( FILE=TRIM( config_file_name ), EXIST=file_exists )
+         INQUIRE( FILE=TRIM( config_file_name ) // TRIM( file_suffix ), &
+                  EXIST=file_exists )
 
          IF ( file_exists )  THEN
-            CALL internal_message( 'debug', routine_name //              &
-                                            ': Remove existing file ' // &
-                                            TRIM( config_file_name ) )
-            CALL SYSTEM( 'rm ' // TRIM( config_file_name ) )
+            CALL internal_message( 'debug', routine_name //     &
+                                   ': Remove existing file ' // &
+                                   TRIM( config_file_name ) // TRIM( file_suffix ) )
+            CALL EXECUTE_COMMAND_LINE(                                                &
+                    COMMAND='rm ' // TRIM( config_file_name ) // TRIM( file_suffix ), &
+                    WAIT=.TRUE., EXITSTAT=return_value )
          ENDIF
 
-         OPEN( config_file_unit, FILE=TRIM( config_file_name ), &
+         OPEN( config_file_unit, FILE=TRIM( config_file_name ) // TRIM( file_suffix ), &
                FORM='UNFORMATTED', STATUS='NEW', IOSTAT=return_value )
 
@@ -216,6 +234,8 @@
 
             !-- Write some general information to config file
-            WRITE( config_file_unit )  LEN( prefix )
-            WRITE( config_file_unit )  prefix
+            WRITE( config_file_unit )  nrank
+            WRITE( config_file_unit )  master_rank
+            WRITE( config_file_unit )  LEN( file_prefix )
+            WRITE( config_file_unit )  file_prefix
             WRITE( config_file_unit )  charlen
             WRITE( config_file_unit )  global_id_in_file
@@ -237,5 +257,5 @@
    IF ( return_value == 0 )  THEN
 
-      bin_filename = prefix // TRIM( filename ) // myid_char
+      bin_filename = file_prefix // TRIM( filename ) // TRIM( file_suffix ) // my_rank_char
 
       !-- Remove any pre-existing file
@@ -245,5 +265,6 @@
          CALL internal_message( 'debug', routine_name // &
                                          ': remove existing file ' // TRIM( bin_filename ) )
-         CALL SYSTEM( 'rm ' // TRIM( bin_filename ) )
+         CALL EXECUTE_COMMAND_LINE( COMMAND='rm ' // TRIM( bin_filename ), &
+                                    WAIT=.TRUE., EXITSTAT=return_value )
       ENDIF
 
@@ -256,5 +277,5 @@
 
          !-- Add filename to config file
-         IF ( myid == 0 )  THEN
+         IF ( my_rank == master_rank )  THEN
             WRITE( config_file_unit )  filename
          ENDIF
Index: palm/trunk/SOURCE/data_output_module.f90
===================================================================
--- palm/trunk/SOURCE/data_output_module.f90	(revision 4106)
+++ palm/trunk/SOURCE/data_output_module.f90	(revision 4107)
@@ -136,4 +136,5 @@
 
    CHARACTER(LEN=charlen) ::  output_file_format = 'binary'  !< file format (namelist parameter)
+   CHARACTER(LEN=charlen) ::  output_file_suffix = ''        !< file suffix added to each file name
 
    CHARACTER(LEN=800) ::  internal_error_message = '' !< string containing the last error message
@@ -142,4 +143,6 @@
    INTEGER(iwp) ::  debug_output_unit  !< Fortran Unit Number of the debug-output file
    INTEGER(iwp) ::  nf                 !< number of files
+   INTEGER      ::  master_rank = 0    !< 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
 
    INTEGER(iwp), PARAMETER ::  no_var_id = -1  !< value of var_id if no variable is selected
@@ -223,18 +226,32 @@
 !> Initialize data-output module
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE dom_init( program_debug_output_unit, debug_output )
-
-   INTEGER(iwp), INTENT(IN) ::  program_debug_output_unit  !< file unit number for debug output
+SUBROUTINE dom_init( file_suffix_of_output_group, mpi_comm_of_output_group, master_output_rank, &
+                     program_debug_output_unit, debug_output )
+
+   CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  file_suffix_of_output_group  !< file-name suffix added to each file;
+                                                                           !> must be unique for each output group
+
+   INTEGER, INTENT(IN), OPTIONAL ::  master_output_rank         !< MPI rank executing tasks which must
+                                                                !> be executed by a single PE only
+   INTEGER, INTENT(IN)           ::  mpi_comm_of_output_group   !< MPI communicator specifying the MPI group
+                                                                !> which participate in the output
+   INTEGER, INTENT(IN)           ::  program_debug_output_unit  !< file unit number for debug output
 
    LOGICAL, INTENT(IN) ::  debug_output  !< if true, debug output is printed
 
 
+   IF ( PRESENT( file_suffix_of_output_group ) )  output_file_suffix = file_suffix_of_output_group
+   IF ( PRESENT( master_output_rank ) )  master_rank = master_output_rank
+
+   output_group_comm = mpi_comm_of_output_group
+
    debug_output_unit = program_debug_output_unit
-
    print_debug_output = debug_output
 
-   CALL binary_init_module( debug_output_unit, debug_output, no_var_id )
-
-   CALL netcdf4_init_module( debug_output_unit, debug_output, no_var_id )
+   CALL binary_init_module( output_file_suffix, output_group_comm, master_rank, &
+                            debug_output_unit, debug_output, no_var_id )
+
+   CALL netcdf4_init_module( output_file_suffix, output_group_comm, master_rank, &
+                            debug_output_unit, debug_output, no_var_id )
 
 END SUBROUTINE dom_init
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
