Index: palm/trunk/SOURCE/data_output_module.f90
===================================================================
--- palm/trunk/SOURCE/data_output_module.f90	(revision 4070)
+++ palm/trunk/SOURCE/data_output_module.f90	(revision 4106)
@@ -20,6 +20,6 @@
 ! Current revisions:
 ! ------------------
-! 
-! 
+!
+!
 ! Former revisions:
 ! -----------------
@@ -43,7 +43,4 @@
 !>
 !> @todo Convert variable if type of given values do not fit specified type.
-!> @todo Remove unused variables
-!> @todo How to deal with definition calls after dom_start_output is called? Should it be allowed
-!>       to define new files after that (which is technically possible)?
 !> @todo Remove iwp from index (and similar) variables.
 !--------------------------------------------------------------------------------------------------!
@@ -52,25 +49,14 @@
    USE kinds
 
-   USE data_output_netcdf4_serial_module, &
-      ONLY: netcdf4_serial_init_dimension, &
-            netcdf4_serial_get_error_message, &
-            netcdf4_serial_init_end, &
-            netcdf4_serial_init_module, &
-            netcdf4_serial_init_variable, &
-            netcdf4_serial_finalize, &
-            netcdf4_serial_open_file, &
-            netcdf4_serial_write_attribute, &
-            netcdf4_serial_write_variable
-
-   USE data_output_netcdf4_parallel_module, &
-      ONLY: netcdf4_parallel_init_dimension, &
-            netcdf4_parallel_get_error_message, &
-            netcdf4_parallel_init_end, &
-            netcdf4_parallel_init_module, &
-            netcdf4_parallel_init_variable, &
-            netcdf4_parallel_finalize, &
-            netcdf4_parallel_open_file, &
-            netcdf4_parallel_write_attribute, &
-            netcdf4_parallel_write_variable
+   USE data_output_netcdf4_module, &
+      ONLY: netcdf4_init_dimension, &
+            netcdf4_get_error_message, &
+            netcdf4_init_end, &
+            netcdf4_init_module, &
+            netcdf4_init_variable, &
+            netcdf4_finalize, &
+            netcdf4_open_file, &
+            netcdf4_write_attribute, &
+            netcdf4_write_variable
 
    USE data_output_binary_module, &
@@ -105,5 +91,4 @@
       INTEGER(iwp)           ::  id = 0               !< id within file
       LOGICAL                ::  is_global = .FALSE.  !< true if global variable
-      LOGICAL                ::  is_init = .FALSE.    !< true if initialized
       CHARACTER(LEN=charlen), DIMENSION(:), ALLOCATABLE ::  dimension_names  !< list of dimension names
       INTEGER(iwp),           DIMENSION(:), ALLOCATABLE ::  dimension_ids    !< list of dimension ids
@@ -118,5 +103,4 @@
       INTEGER(iwp)           ::  length_mask          !< length of masked dimension
       INTEGER(iwp)           ::  var_id = 0           !< associated variable id within file
-      LOGICAL                ::  is_init = .FALSE.    !< true if initialized
       LOGICAL                ::  is_masked = .FALSE.  !< true if masked
       INTEGER(iwp),    DIMENSION(2)              ::  bounds                !< lower and upper bound of dimension
@@ -125,5 +109,5 @@
       INTEGER(KIND=2), DIMENSION(:), ALLOCATABLE ::  masked_values_int16   !< masked dimension values if 16bit integer
       INTEGER(KIND=4), DIMENSION(:), ALLOCATABLE ::  masked_values_int32   !< masked dimension values if 32bit integer
-      INTEGER(iwp),    DIMENSION(:), ALLOCATABLE ::  masked_values_intwp   !< masked dimension values if working-precision integer
+      INTEGER(iwp),    DIMENSION(:), ALLOCATABLE ::  masked_values_intwp   !< masked dimension values if working-precision int
       INTEGER(KIND=1), DIMENSION(:), ALLOCATABLE ::  values_int8           !< dimension values if 16bit integer
       INTEGER(KIND=2), DIMENSION(:), ALLOCATABLE ::  values_int16          !< dimension values if 16bit integer
@@ -252,7 +236,5 @@
    CALL binary_init_module( debug_output_unit, debug_output, no_var_id )
 
-   CALL netcdf4_serial_init_module( debug_output_unit, debug_output, no_var_id )
-
-   CALL netcdf4_parallel_init_module( debug_output_unit, debug_output, no_var_id )
+   CALL netcdf4_init_module( debug_output_unit, debug_output, no_var_id )
 
 END SUBROUTINE dom_init
@@ -310,5 +292,6 @@
    !             WRITE(*,'(10X,5(I5,1X),A)') files(f)%dimensions(d)%values_int32(0:MIN(4,files(f)%dimensions(d)%length)), '...'
    !          ELSE
-   !             WRITE(*,'(10X,5(F8.2,1X),A)') files(f)%dimensions(d)%values_real64(0:MIN(4,files(f)%dimensions(d)%length)), '...'
+   !             WRITE(*,'(10X,5(F8.2,1X),A)')  &
+   !                files(f)%dimensions(d)%values_real64(0:MIN(4,files(f)%dimensions(d)%length)), '...'
    !          ENDIF
    !          IF ( ALLOCATED(files(f)%dimensions(d)%mask) )  THEN
@@ -716,7 +699,7 @@
 
       return_value = 1
-      CALL internal_message( 'error', routine_name //                   &
-                                      ': dimension ' // TRIM( name ) // &
-                                      ': At least one but no more than two bounds must be given' )
+      CALL internal_message( 'error', routine_name //          &
+                             ': dimension ' // TRIM( name ) // &
+                             ': At least one but no more than two bounds must be given' )
 
    ENDIF
@@ -729,5 +712,13 @@
          IF ( TRIM( filename ) == files(f)%name )  THEN
 
-            IF ( .NOT. ALLOCATED( files(f)%dimensions ) )  THEN
+            IF ( files(f)%is_init )  THEN
+
+               return_value = 1
+               CALL internal_message( 'error',                           &
+                       routine_name // ': file "' // TRIM( filename ) // &
+                       '" is already initialized. No further dimension definition allowed!' )
+               EXIT
+
+            ELSEIF ( .NOT. ALLOCATED( files(f)%dimensions ) )  THEN
 
                ndim = 1
@@ -736,27 +727,45 @@
             ELSE
 
-               ndim = SIZE( files(f)%dimensions )
-
-               !-- Check if dimension already exists in file
-               DO  d = 1, ndim
-                  IF ( files(f)%dimensions(d)%name == dimension%name )  THEN
-                     return_value = 1
-                     CALL internal_message( 'error',            &
-                             routine_name //                    &
-                             ': dimension "' // TRIM( name ) // &
-                             '" already exists in file "' // TRIM( filename ) // '"' )
-                     EXIT
+               !-- Check if any variable of the same name as the new dimension is already defined
+               IF ( ALLOCATED( files(f)%variables ) )  THEN
+                  DO  i = 1, SIZE( files(f)%variables )
+                     IF ( files(f)%variables(i)%name == dimension%name )  THEN
+                        return_value = 1
+                        CALL internal_message( 'error', routine_name //                   &
+                                               ': file "' // TRIM( filename ) //          &
+                                               '" already has a variable of name "' //    &
+                                               TRIM( dimension%name ) // '" defined. ' // &
+                                               'Defining a dimension of the same ' //     &
+                                               'name is not allowed.' )
+                        EXIT
+                     ENDIF
+                  ENDDO
+               ENDIF
+
+               IF ( return_value == 0 )  THEN
+                  !-- Check if dimension already exists in file
+                  ndim = SIZE( files(f)%dimensions )
+
+                  DO  d = 1, ndim
+                     IF ( files(f)%dimensions(d)%name == dimension%name )  THEN
+                        return_value = 1
+                        CALL internal_message( 'error',            &
+                                routine_name //                    &
+                                ': dimension "' // TRIM( name ) // &
+                                '" already exists in file "' // TRIM( filename ) // '"' )
+                        EXIT
+                     ENDIF
+                  ENDDO
+
+                  !-- Extend dimension list
+                  IF ( return_value == 0 )  THEN
+                     ALLOCATE( dims_tmp(ndim) )
+                     dims_tmp = files(f)%dimensions
+                     DEALLOCATE( files(f)%dimensions )
+                     ndim = ndim + 1
+                     ALLOCATE( files(f)%dimensions(ndim) )
+                     files(f)%dimensions(:ndim-1) = dims_tmp
+                     DEALLOCATE( dims_tmp )
                   ENDIF
-               ENDDO
-
-               !-- Extend dimension list
-               IF ( return_value == 0 )  THEN
-                  ALLOCATE( dims_tmp(ndim) )
-                  dims_tmp = files(f)%dimensions
-                  DEALLOCATE( files(f)%dimensions )
-                  ndim = ndim + 1
-                  ALLOCATE( files(f)%dimensions(ndim) )
-                  files(f)%dimensions(:ndim-1) = dims_tmp
-                  DEALLOCATE( dims_tmp )
                ENDIF
 
@@ -832,26 +841,49 @@
       IF ( TRIM( filename ) == files(f)%name )  THEN
 
-         !-- Check if dimensions assigned to variable are defined within file
-         IF ( ALLOCATED( files(f)%dimensions ) )  THEN
-
-            DO  i = 1, SIZE( variable%dimension_names )
-               found = .FALSE.
-               DO  d = 1, SIZE( files(f)%dimensions )
-                  IF ( files(f)%dimensions(d)%name == variable%dimension_names(i) )  THEN
-                     found = .TRUE.
+         IF ( files(f)%is_init )  THEN
+
+            return_value = 1
+            CALL internal_message( 'error', routine_name // ': file "' // TRIM( filename ) // &
+                    '" is already initialized. No further variable definition allowed!' )
+            EXIT
+
+         ELSEIF ( ALLOCATED( files(f)%dimensions ) )  THEN
+
+            !-- Check if any dimension of the same name as the new variable is already defined
+            DO  d = 1, SIZE( files(f)%dimensions )
+               IF ( files(f)%dimensions(d)%name == variable%name )  THEN
+                  return_value = 1
+                  CALL internal_message( 'error', routine_name //                  &
+                                         ': file "' // TRIM( filename ) //         &
+                                         '" already has a dimension of name "' //  &
+                                         TRIM( variable%name ) // '" defined. ' // &
+                                         'Defining a variable of the same name is not allowed.' )
+                  EXIT
+               ENDIF
+            ENDDO
+
+            !-- Check if dimensions assigned to variable are defined within file
+            IF ( return_value == 0 )  THEN
+               DO  i = 1, SIZE( variable%dimension_names )
+                  found = .FALSE.
+                  DO  d = 1, SIZE( files(f)%dimensions )
+                     IF ( files(f)%dimensions(d)%name == variable%dimension_names(i) )  THEN
+                        found = .TRUE.
+                        EXIT
+                     ENDIF
+                  ENDDO
+                  IF ( .NOT. found )  THEN
+                     return_value = 1
+                     CALL internal_message( 'error',                               &
+                                            routine_name //                        &
+                                            ': variable "' // TRIM( name ) //      &
+                                            '" in file "' // TRIM( filename ) //   &
+                                            '": required dimension "' //           &
+                                            TRIM( variable%dimension_names(i) ) // &
+                                            '" not defined' )
                      EXIT
                   ENDIF
                ENDDO
-               IF ( .NOT. found )  THEN
-                  return_value = 1
-                  CALL internal_message( 'error', &
-                                         routine_name //                      &
-                                         ': variable "' // TRIM( name ) //    &
-                                         '" in file "' // TRIM( filename ) // &
-                                         '": required dimension "' //         &
-                                         TRIM( variable%dimension_names(i) ) // '" not defined' )
-                  EXIT
-               ENDIF
-            ENDDO
+            ENDIF
 
          ELSE
@@ -1263,4 +1295,11 @@
 
       IF ( TRIM( filename ) == files(f)%name )  THEN
+
+         IF ( files(f)%is_init )  THEN
+            return_value = 1
+            CALL internal_message( 'error', routine_name // ': file "' // TRIM( filename ) // &
+                    '" is already initialized. No further attribute definition allowed!' )
+            EXIT
+         ENDIF
 
          !-- Add attribute to file
@@ -1328,5 +1367,6 @@
                         !-- Check if attribute already exists
                         DO  a = 1, natt
-                           IF ( files(f)%dimensions(d)%attributes(a)%name == attribute%name )  THEN
+                           IF ( files(f)%dimensions(d)%attributes(a)%name == attribute%name ) &
+                           THEN
                               IF ( append )  THEN
                                  !-- Append existing character attribute
@@ -1385,5 +1425,6 @@
                         !-- Check if attribute already exists
                         DO  a = 1, natt
-                           IF ( files(f)%variables(d)%attributes(a)%name == attribute%name )  THEN
+                           IF ( files(f)%variables(d)%attributes(a)%name == attribute%name )  &
+                           THEN
                               IF ( append )  THEN
                                  !-- Append existing character attribute
@@ -1497,7 +1538,13 @@
       DO  f = 1, nf
 
+         !-- Skip initialization if file is already initialized
+         IF ( files(f)%is_init )  CYCLE
+
+         CALL internal_message( 'debug', routine_name // ': initialize file "' // &
+                                TRIM( files(f)%name ) // '"' )
+
          !-- Open file
          CALL open_output_file( files(f)%format, files(f)%name, files(f)%id, &
-                                files(f)%is_init, return_value=return_value )
+                                return_value=return_value )
 
          !-- Initialize file header:
@@ -1508,9 +1555,12 @@
          !-- End file definition
          IF ( return_value == 0 )  &
-            CALL dom_init_end( files(f)%format, files(f)%id, return_value=return_value )
-
-         !-- Write dimension values into file
+            CALL dom_init_end( files(f)%format, files(f)%id, files(f)%name, return_value )
+
          IF ( return_value == 0 )  THEN
 
+            !-- Flag file as initialized
+            files(f)%is_init = .TRUE.
+
+            !-- Write dimension values into file
             DO  d = 1, SIZE( files(f)%dimensions )
                IF ( ALLOCATED( files(f)%dimensions(d)%values_int8 ) )  THEN
@@ -1628,5 +1678,5 @@
    return_value = 0
 
-   !-- Set flag for files to be initialized
+   !-- Flag files which contain output variables as used
    file_is_used(:) = .FALSE.
    DO  f = 1, nf
@@ -1657,4 +1707,7 @@
    DO  f = 1, nf
 
+      !-- If a file is already initialized, it was already checked previously
+      IF ( files(f)%is_init )  CYCLE
+
       !-- Get number of defined dimensions
       ndim = SIZE( files(f)%dimensions )
@@ -1699,5 +1752,5 @@
 !> Open requested output file.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE open_output_file( file_format, filename, file_id, is_init, return_value )
+SUBROUTINE open_output_file( file_format, filename, file_id, return_value )
 
    CHARACTER(LEN=*), INTENT(IN) ::  file_format  !< file format chosen for file
@@ -1706,31 +1759,38 @@
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'open_output_file'  !< name of routine
 
-   INTEGER(iwp), INTENT(OUT) ::  file_id       !< file ID
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
-
-   LOGICAL, INTENT(OUT) ::  is_init  !< true if file is opened
-
+   INTEGER(iwp), INTENT(OUT) ::  file_id              !< file ID
+   INTEGER(iwp)              ::  output_return_value  !< return value of a called output routine
+   INTEGER(iwp), INTENT(OUT) ::  return_value         !< return value
+
+
+   return_value = 0
+   output_return_value = 0
 
    SELECT CASE ( TRIM( file_format ) )
 
       CASE ( 'binary' )
-         CALL binary_open_file( filename, file_id, return_value )
+         CALL binary_open_file( 'binary', filename, file_id, output_return_value )
 
       CASE ( 'netcdf4-serial' )
-         CALL netcdf4_serial_open_file( filename, file_id, return_value )
+         CALL netcdf4_open_file( 'serial', filename, file_id, output_return_value )
 
       CASE ( 'netcdf4-parallel' )
-         CALL netcdf4_parallel_open_file( filename, file_id, return_value )
+         CALL netcdf4_open_file( 'parallel', filename, file_id, output_return_value )
 
       CASE DEFAULT
          return_value = 1
-         CALL internal_message( 'error', routine_name //                              &
-                                         ': file "' // TRIM( filename ) //            &
-                                         '": file format "' // TRIM( file_format ) // &
-                                         '" not supported' )
 
    END SELECT
 
-   is_init = return_value == 0
+   IF ( output_return_value /= 0 )  THEN
+      return_value = output_return_value
+      CALL internal_message( 'error', routine_name // &
+                             ': error while opening file "' // TRIM( filename ) // '"' )
+   ELSEIF ( return_value /= 0 )  THEN
+      CALL internal_message( 'error', routine_name //                              &
+                                      ': file "' // TRIM( filename ) //            &
+                                      '": file format "' // TRIM( file_format ) // &
+                                      '" not supported' )
+   ENDIF
 
 END SUBROUTINE open_output_file
@@ -1757,6 +1817,6 @@
    IF ( ALLOCATED( file%attributes ) )  THEN
       DO  a = 1, SIZE( file%attributes )
-         return_value = write_attribute( file%format, file%id, var_id=no_var_id, &
-                                attribute=file%attributes(a) )
+         return_value = write_attribute( file%format, file%id, file%name, var_id=no_var_id, &
+                                         attribute=file%attributes(a) )
          IF ( return_value /= 0 )  EXIT
       ENDDO
@@ -1771,16 +1831,16 @@
 
             !-- Initialize non-masked dimension
-            CALL init_file_dimension( file%format,                             &
-                    file%id, file%dimensions(d)%id, file%dimensions(d)%var_id, &
-                    file%dimensions(d)%name, file%dimensions(d)%data_type,     &
-                    file%dimensions(d)%length, file%dimensions(d)%is_init, return_value )
+            CALL init_file_dimension( file%format, file%id, file%name,     &
+                    file%dimensions(d)%id, file%dimensions(d)%var_id,      &
+                    file%dimensions(d)%name, file%dimensions(d)%data_type, &
+                    file%dimensions(d)%length, return_value )
 
          ELSE
 
             !-- Initialize masked dimension
-            CALL init_file_dimension( file%format,                             &
-                    file%id, file%dimensions(d)%id, file%dimensions(d)%var_id, &
-                    file%dimensions(d)%name, file%dimensions(d)%data_type,     &
-                    file%dimensions(d)%length_mask, file%dimensions(d)%is_init, return_value )
+            CALL init_file_dimension( file%format, file%id, file%name,     &
+                    file%dimensions(d)%id, file%dimensions(d)%var_id,      &
+                    file%dimensions(d)%name, file%dimensions(d)%data_type, &
+                    file%dimensions(d)%length_mask, return_value )
 
          ENDIF
@@ -1789,5 +1849,7 @@
             !-- Write dimension attributes
             DO  a = 1, SIZE( file%dimensions(d)%attributes )
-               return_value = write_attribute( file%format, file%id, file%dimensions(d)%var_id, &
+               return_value = write_attribute( file%format, file%id, file%name, &
+                                 var_id=file%dimensions(d)%var_id,              &
+                                 var_name=file%dimensions(d)%name,              &
                                  attribute=file%dimensions(d)%attributes(a) )
                IF ( return_value /= 0 )  EXIT
@@ -1807,13 +1869,15 @@
          DO  d = 1, SIZE( file%variables )
 
-            CALL init_file_variable( file%format, file%id, file%variables(d)%id, &
-                    file%variables(d)%name, file%variables(d)%data_type,         &
-                    file%variables(d)%dimension_ids,                             &
-                    file%variables(d)%is_init, file%variables(d)%is_global, return_value )
+            CALL init_file_variable( file%format, file%id, file%name,                          &
+                    file%variables(d)%id, file%variables(d)%name, file%variables(d)%data_type, &
+                    file%variables(d)%dimension_ids,                                           &
+                    file%variables(d)%is_global, return_value )
 
             IF ( return_value == 0  .AND.  ALLOCATED( file%variables(d)%attributes ) )  THEN
                !-- Write variable attribures
                DO  a = 1, SIZE( file%variables(d)%attributes )
-                  return_value = write_attribute( file%format, file%id, file%variables(d)%id, &
+                  return_value = write_attribute( file%format, file%id, file%name, &
+                                    var_id=file%variables(d)%id,                   &
+                                    var_name=file%variables(d)%name,               &
                                     attribute=file%variables(d)%attributes(a) )
                   IF ( return_value /= 0 )  EXIT
@@ -1835,17 +1899,34 @@
 !> Write attribute to file.
 !--------------------------------------------------------------------------------------------------!
-FUNCTION write_attribute( file_format, file_id, var_id, attribute ) RESULT( return_value )
+FUNCTION write_attribute( file_format, file_id, file_name, var_id, var_name, attribute ) RESULT( return_value )
 
    CHARACTER(LEN=*), INTENT(IN) ::  file_format  !< file format chosen for file
+   CHARACTER(LEN=*), INTENT(IN) ::  file_name    !< file name
+   CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  var_name     !< variable name
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'write_attribute'  !< file format chosen for file
 
-   INTEGER(iwp) ::  file_id       !< file ID
-   INTEGER(iwp) ::  return_value  !< return value
-   INTEGER(iwp) ::  var_id        !< variable ID
+   INTEGER(iwp), INTENT(IN) ::  file_id              !< file ID
+   INTEGER(iwp)             ::  return_value         !< return value
+   INTEGER(iwp)             ::  output_return_value  !< return value of a called output routine
+   INTEGER(iwp), INTENT(IN) ::  var_id               !< variable ID
 
    TYPE(attribute_type), INTENT(IN) ::  attribute  !< attribute to be written
 
 
+   return_value = 0
+   output_return_value = 0
+
+   !-- Prepare for possible error message
+   IF ( PRESENT( var_name ) )  THEN
+      temp_string = '(file "' // TRIM( file_name ) //      &
+                    '", variable "' // TRIM( var_name ) // &
+                    '", attribute "' // TRIM( attribute%name ) // '")'
+   ELSE
+      temp_string = '(file "' // TRIM( file_name ) // &
+                    '", attribute "' // TRIM( attribute%name ) // '")'
+   ENDIF
+
+   !-- Write attribute to file
    SELECT CASE ( TRIM( file_format ) )
 
@@ -1857,126 +1938,82 @@
                CALL binary_write_attribute( file_id=file_id, var_id=var_id,          &
                        att_name=attribute%name, att_value_char=attribute%value_char, &
-                       return_value=return_value )
+                       return_value=output_return_value )
 
             CASE( 'int8' )
                CALL binary_write_attribute( file_id=file_id, var_id=var_id,          &
                        att_name=attribute%name, att_value_int8=attribute%value_int8, &
-                       return_value=return_value )
+                       return_value=output_return_value )
 
             CASE( 'int16' )
                CALL binary_write_attribute( file_id=file_id, var_id=var_id,            &
                        att_name=attribute%name, att_value_int16=attribute%value_int16, &
-                       return_value=return_value )
+                       return_value=output_return_value )
 
             CASE( 'int32' )
                CALL binary_write_attribute( file_id=file_id, var_id=var_id,            &
                        att_name=attribute%name, att_value_int32=attribute%value_int32, &
-                       return_value=return_value )
+                       return_value=output_return_value )
 
             CASE( 'real32' )
                CALL binary_write_attribute( file_id=file_id, var_id=var_id,              &
                        att_name=attribute%name, att_value_real32=attribute%value_real32, &
-                       return_value=return_value )
+                       return_value=output_return_value )
 
             CASE( 'real64' )
                CALL binary_write_attribute( file_id=file_id, var_id=var_id,              &
                        att_name=attribute%name, att_value_real64=attribute%value_real64, &
-                       return_value=return_value )
+                       return_value=output_return_value )
 
             CASE DEFAULT
                return_value = 1
-               CALL internal_message( 'error',                                          &
-                                      routine_name //                                   &
-                                      ': attribute "' // TRIM( attribute%name ) //      &
-                                      '": data type "'// TRIM( attribute%data_type ) // &
-                                      '" not supported for file format "binary".' )
+               CALL internal_message( 'error', routine_name //                     &
+                                      ': file format "' // TRIM( file_format ) //  &
+                                      '" does not support attribute data type "'// &
+                                      TRIM( attribute%data_type ) //               &
+                                      '" ' // TRIM( temp_string ) )
 
          END SELECT
 
-      CASE ( 'netcdf4-serial' )
+      CASE ( 'netcdf4-parallel', 'netcdf4-serial' )
 
          SELECT CASE ( TRIM( attribute%data_type ) )
 
             CASE( 'char' )
-               CALL netcdf4_serial_write_attribute( file_id=file_id, var_id=var_id,  &
+               CALL netcdf4_write_attribute( file_id=file_id, var_id=var_id,  &
                        att_name=attribute%name, att_value_char=attribute%value_char, &
-                       return_value=return_value )
+                       return_value=output_return_value )
 
             CASE( 'int8' )
-               CALL netcdf4_serial_write_attribute( file_id=file_id, var_id=var_id,  &
+               CALL netcdf4_write_attribute( file_id=file_id, var_id=var_id,  &
                        att_name=attribute%name, att_value_int8=attribute%value_int8, &
-                       return_value=return_value )
+                       return_value=output_return_value )
 
             CASE( 'int16' )
-               CALL netcdf4_serial_write_attribute( file_id=file_id, var_id=var_id,    &
+               CALL netcdf4_write_attribute( file_id=file_id, var_id=var_id,    &
                        att_name=attribute%name, att_value_int16=attribute%value_int16, &
-                       return_value=return_value )
+                       return_value=output_return_value )
 
             CASE( 'int32' )
-               CALL netcdf4_serial_write_attribute( file_id=file_id, var_id=var_id,    &
+               CALL netcdf4_write_attribute( file_id=file_id, var_id=var_id,    &
                        att_name=attribute%name, att_value_int32=attribute%value_int32, &
-                       return_value=return_value )
+                       return_value=output_return_value )
 
             CASE( 'real32' )
-               CALL netcdf4_serial_write_attribute( file_id=file_id, var_id=var_id,      &
+               CALL netcdf4_write_attribute( file_id=file_id, var_id=var_id,      &
                        att_name=attribute%name, att_value_real32=attribute%value_real32, &
-                       return_value=return_value )
+                       return_value=output_return_value )
 
             CASE( 'real64' )
-               CALL netcdf4_serial_write_attribute( file_id=file_id, var_id=var_id,      &
+               CALL netcdf4_write_attribute( file_id=file_id, var_id=var_id,      &
                        att_name=attribute%name, att_value_real64=attribute%value_real64, &
-                       return_value=return_value )
+                       return_value=output_return_value )
 
             CASE DEFAULT
                return_value = 1
-               CALL internal_message( 'error',                                          &
-                                      routine_name //                                   &
-                                      ': attribute "' // TRIM( attribute%name ) //      &
-                                      '": data type "'// TRIM( attribute%data_type ) // &
-                                      '" not supported for file format "netcdf4-serial".' )
-
-         END SELECT
-
-      CASE ( 'netcdf4-parallel' )
-
-         SELECT CASE ( TRIM( attribute%data_type ) )
-
-            CASE( 'char' )
-               CALL netcdf4_parallel_write_attribute( file_id=file_id, var_id=var_id, &
-                       att_name=attribute%name, att_value_char=attribute%value_char,  &
-                       return_value=return_value )
-
-            CASE( 'int8' )
-               CALL netcdf4_parallel_write_attribute( file_id=file_id, var_id=var_id, &
-                       att_name=attribute%name, att_value_int8=attribute%value_int8,  &
-                       return_value=return_value )
-
-            CASE( 'int16' )
-               CALL netcdf4_parallel_write_attribute( file_id=file_id, var_id=var_id,  &
-                       att_name=attribute%name, att_value_int16=attribute%value_int16, &
-                       return_value=return_value )
-
-            CASE( 'int32' )
-               CALL netcdf4_parallel_write_attribute( file_id=file_id, var_id=var_id,  &
-                       att_name=attribute%name, att_value_int32=attribute%value_int32, &
-                       return_value=return_value )
-
-            CASE( 'real32' )
-               CALL netcdf4_parallel_write_attribute( file_id=file_id, var_id=var_id,    &
-                       att_name=attribute%name, att_value_real32=attribute%value_real32, &
-                       return_value=return_value )
-
-            CASE( 'real64' )
-               CALL netcdf4_parallel_write_attribute( file_id=file_id, var_id=var_id,    &
-                       att_name=attribute%name, att_value_real64=attribute%value_real64, &
-                       return_value=return_value )
-
-            CASE DEFAULT
-               return_value = 1
-               CALL internal_message( 'error',                                          &
-                                      routine_name //                                   &
-                                      ': attribute "' // TRIM( attribute%name ) //      &
-                                      '": data type "'// TRIM( attribute%data_type ) // &
-                                      '" not supported for file format "netcdf4-parallel".' )
+               CALL internal_message( 'error', routine_name //                     &
+                                      ': file format "' // TRIM( file_format ) //  &
+                                      '" does not support attribute data type "'// &
+                                      TRIM( attribute%data_type ) //               &
+                                      '" ' // TRIM( temp_string ) )
 
          END SELECT
@@ -1986,7 +2023,15 @@
          CALL internal_message( 'error',        &
                                 routine_name // &
-                                ': unsupported file format "' // TRIM( file_format ) // '"' )
+                                ': unsupported file format "' // TRIM( file_format ) // &
+                                '" ' // TRIM( temp_string ) )
 
    END SELECT
+   
+   IF ( output_return_value /= 0 )  THEN
+      return_value = output_return_value
+      CALL internal_message( 'error',        &
+                             routine_name // &
+                             ': error while writing attribute ' // TRIM( temp_string ) )
+   ENDIF
 
 END FUNCTION write_attribute
@@ -1997,45 +2042,55 @@
 !> Initialize dimension in file.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE init_file_dimension( file_format, file_id, dim_id, var_id,        &
-                                dim_name, dim_type, dim_length, is_init, return_value )
+SUBROUTINE init_file_dimension( file_format, file_id, file_name, dim_id, var_id, &
+                                dim_name, dim_type, dim_length, return_value )
 
    CHARACTER(LEN=*), INTENT(IN) ::  dim_name     !< name of dimension
    CHARACTER(LEN=*), INTENT(IN) ::  dim_type     !< data type of dimension
    CHARACTER(LEN=*), INTENT(IN) ::  file_format  !< file format chosen for file
+   CHARACTER(LEN=*), INTENT(IN) ::  file_name    !< name of file
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'init_file_dimension'  !< file format chosen for file
 
-   INTEGER(iwp), INTENT(OUT) ::  dim_id        !< dimension ID
-   INTEGER(iwp), INTENT(IN)  ::  dim_length    !< length of dimension
-   INTEGER(iwp), INTENT(IN)  ::  file_id       !< file ID
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
-   INTEGER(iwp), INTENT(OUT) ::  var_id        !< associated variable ID
-
-   LOGICAL, INTENT(OUT) ::  is_init  !< true if dimension is initialized
-
+   INTEGER(iwp), INTENT(OUT) ::  dim_id               !< dimension ID
+   INTEGER(iwp), INTENT(IN)  ::  dim_length           !< length of dimension
+   INTEGER(iwp), INTENT(IN)  ::  file_id              !< file ID
+   INTEGER(iwp)              ::  output_return_value  !< return value of a called output routine
+   INTEGER(iwp), INTENT(OUT) ::  return_value         !< return value
+   INTEGER(iwp), INTENT(OUT) ::  var_id               !< associated variable ID
+
+
+   return_value = 0
+   output_return_value = 0
+
+   temp_string = '(file "' // TRIM( file_name ) // &
+                 '", dimension "' // TRIM( dim_name ) // '")'
 
    SELECT CASE ( TRIM( file_format ) )
 
       CASE ( 'binary' )
-         CALL binary_init_dimension( file_id, dim_id, var_id, &
-                 dim_name, dim_type, dim_length, is_init, return_value )
+         CALL binary_init_dimension( 'binary', file_id, dim_id, var_id, &
+                 dim_name, dim_type, dim_length, return_value=output_return_value )
 
       CASE ( 'netcdf4-serial' )
-         CALL netcdf4_serial_init_dimension( file_id, dim_id, var_id, &
-                 dim_name, dim_type, dim_length, is_init, return_value )
+         CALL netcdf4_init_dimension( 'serial', file_id, dim_id, var_id, &
+                 dim_name, dim_type, dim_length, return_value=output_return_value )
 
       CASE ( 'netcdf4-parallel' )
-         CALL netcdf4_parallel_init_dimension( file_id, dim_id, var_id, &
-                 dim_name, dim_type, dim_length, is_init, return_value )
+         CALL netcdf4_init_dimension( 'parallel', file_id, dim_id, var_id, &
+                 dim_name, dim_type, dim_length, return_value=output_return_value )
 
       CASE DEFAULT
          return_value = 1
-         WRITE( temp_string, * ) file_id
-         CALL internal_message( 'error', routine_name //                              &
-                                         ': file id = ' // TRIM( temp_string ) //     &
-                                         '": file format "' // TRIM( file_format ) // &
-                                         '" not supported' )
+         CALL internal_message( 'error', routine_name //                    &
+                                ': file format "' // TRIM( file_format ) // &
+                                '" not supported ' // TRIM( temp_string ) )
 
    END SELECT
+
+   IF ( output_return_value /= 0 )  THEN
+      return_value = output_return_value
+      CALL internal_message( 'error', routine_name // &
+                             ': error while defining dimension ' // TRIM( temp_string ) )
+   ENDIF
 
 END SUBROUTINE init_file_dimension
@@ -2097,9 +2152,10 @@
 !> Initialize variable.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE init_file_variable( file_format, file_id, var_id,   &
-                               var_name, var_type, var_dim_id, &
-                               is_init, is_global, return_value )
+SUBROUTINE init_file_variable( file_format, file_id, file_name,        &
+                               var_id, var_name, var_type, var_dim_id, &
+                               is_global, return_value )
 
    CHARACTER(LEN=*), INTENT(IN) ::  file_format  !< file format chosen for file
+   CHARACTER(LEN=*), INTENT(IN) ::  file_name    !< file name
    CHARACTER(LEN=*), INTENT(IN) ::  var_name     !< name of variable
    CHARACTER(LEN=*), INTENT(IN) ::  var_type     !< data type of variable
@@ -2107,35 +2163,47 @@
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'init_file_variable'  !< file format chosen for file
 
-   INTEGER(iwp), INTENT(IN)  ::  file_id       !< file ID
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
-   INTEGER(iwp), INTENT(OUT) ::  var_id        !< variable ID
+   INTEGER(iwp), INTENT(IN)  ::  file_id              !< file ID
+   INTEGER(iwp)              ::  output_return_value  !< return value of a called output routine
+   INTEGER(iwp), INTENT(OUT) ::  return_value         !< return value
+   INTEGER(iwp), INTENT(OUT) ::  var_id               !< variable ID
 
    INTEGER(iwp), DIMENSION(:), INTENT(IN) ::  var_dim_id  !< list of dimension IDs used by variable
 
    LOGICAL, INTENT(IN)  ::  is_global  !< true if variable is global
-   LOGICAL, INTENT(OUT) ::  is_init    !< true if variable is initialized
-
+
+
+   return_value = 0
+   output_return_value = 0
+
+   temp_string = '(file "' // TRIM( file_name ) // &
+                 '", variable "' // TRIM( var_name ) // '")'
 
    SELECT CASE ( TRIM( file_format ) )
 
       CASE ( 'binary' )
-         CALL binary_init_variable( file_id, var_id, var_name, var_type, var_dim_id, &
-                                    is_init, is_global, return_value )
+         CALL binary_init_variable( 'binary', file_id, var_id, var_name, var_type, &
+                                    var_dim_id, is_global, return_value=output_return_value )
 
       CASE ( 'netcdf4-serial' )
-         CALL netcdf4_serial_init_variable( file_id, var_id, var_name, var_type, var_dim_id, &
-                                            is_init, is_global, return_value )
+         CALL netcdf4_init_variable( 'serial', file_id, var_id, var_name, var_type, &
+                                     var_dim_id, is_global, return_value=output_return_value )
 
       CASE ( 'netcdf4-parallel' )
-         CALL netcdf4_parallel_init_variable( file_id, var_id, var_name, var_type, var_dim_id, &
-                                              is_init, is_global, return_value )
+         CALL netcdf4_init_variable( 'parallel', file_id, var_id, var_name, var_type, &
+                                     var_dim_id, is_global, return_value=output_return_value )
 
       CASE DEFAULT
          return_value = 1
-         is_init = .FALSE.
-         CALL internal_message( 'error', routine_name // &
-                                ': unsupported file format "' // TRIM( file_format ) )
+         CALL internal_message( 'error', routine_name //                    &
+                                ': file format "' // TRIM( file_format ) // &
+                                '" not supported ' // TRIM( temp_string ) )
 
    END SELECT
+
+   IF ( output_return_value /= 0 )  THEN
+      return_value = output_return_value
+      CALL internal_message( 'error', routine_name // &
+                             ': error while defining variable ' // TRIM( temp_string ) )
+   ENDIF
 
 END SUBROUTINE init_file_variable
@@ -2148,34 +2216,43 @@
 !> @todo Do we need an MPI barrier at the end?
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE dom_init_end( file_format, file_id, return_value )
+SUBROUTINE dom_init_end( file_format, file_id, file_name, return_value )
 
    CHARACTER(LEN=*), INTENT(IN) ::  file_format  !< file format
+   CHARACTER(LEN=*), INTENT(IN) ::  file_name    !< file name
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_init_end'  !< name of routine
 
-   INTEGER(iwp), INTENT(IN)  ::  file_id       !< file id
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
-
+   INTEGER(iwp), INTENT(IN)  ::  file_id              !< file id
+   INTEGER(iwp)              ::  output_return_value  !< return value of a called output routine
+   INTEGER(iwp), INTENT(OUT) ::  return_value         !< return value
+
+
+   return_value = 0
+   output_return_value = 0
+
+   temp_string = '(file "' // TRIM( file_name ) // '")'
 
    SELECT CASE ( TRIM( file_format ) )
 
       CASE ( 'binary' )
-         CALL binary_init_end( file_id, return_value )
-
-      CASE ( 'netcdf4-serial' )
-         CALL netcdf4_serial_init_end( file_id, return_value )
-
-      CASE ( 'netcdf4-parallel' )
-         CALL netcdf4_parallel_init_end( file_id, return_value )
+         CALL binary_init_end( file_id, output_return_value )
+
+      CASE ( 'netcdf4-parallel', 'netcdf4-serial' )
+         CALL netcdf4_init_end( file_id, output_return_value )
 
       CASE DEFAULT
          return_value = 1
-         WRITE( temp_string, * ) file_id
-         CALL internal_message( 'error', routine_name //                             &
-                                         ': file id = ' // TRIM( temp_string ) //    &
-                                         ': file format "' // TRIM( file_format ) // &
-                                         '" not supported' )
+         CALL internal_message( 'error', routine_name //                    &
+                                ': file format "' // TRIM( file_format ) // &
+                                '" not supported ' // TRIM( temp_string ) )
 
    END SELECT
+
+   IF ( output_return_value /= 0 )  THEN
+      return_value = output_return_value
+      CALL internal_message( 'error', routine_name // &
+                             ': error while leaving file-definition state ' // &
+                             TRIM( temp_string ) )
+   ENDIF
 
    ! CALL MPI_Barrier( MPI_COMM_WORLD, return_value )
@@ -2211,17 +2288,18 @@
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_write_var'  !< name of routine
 
-   INTEGER(iwp) ::  d             !< loop index
-   INTEGER(iwp) ::  file_id       !< file ID
-   INTEGER(iwp) ::  i             !< loop index
-   INTEGER(iwp) ::  j             !< loop index
-   INTEGER(iwp) ::  k             !< loop index
-   INTEGER(iwp) ::  return_value  !< return value
-   INTEGER(iwp) ::  var_id        !< variable ID
+   INTEGER(iwp) ::  d                    !< loop index
+   INTEGER(iwp) ::  file_id              !< file ID
+   INTEGER(iwp) ::  i                    !< loop index
+   INTEGER(iwp) ::  j                    !< loop index
+   INTEGER(iwp) ::  k                    !< loop index
+   INTEGER(iwp) ::  output_return_value  !< return value of a called output routine
+   INTEGER(iwp) ::  return_value         !< return value
+   INTEGER(iwp) ::  var_id               !< variable ID
 
    INTEGER(iwp), DIMENSION(:),   INTENT(IN)  ::  bounds_end        !< end index (upper bound) of variable at each dimension
    INTEGER(iwp), DIMENSION(:),   INTENT(IN)  ::  bounds_start      !< start index (lower bound) of variable at each dimension
    INTEGER(iwp), DIMENSION(:),   ALLOCATABLE ::  bounds_dim_start  !< start index (lower bound) of each dimension of variable
-   INTEGER(iwp), DIMENSION(:),   ALLOCATABLE ::  bounds_end_new    !< start index (upper bound) of masked variable at each dimension
-   INTEGER(iwp), DIMENSION(:),   ALLOCATABLE ::  bounds_start_new  !< start index (lower bound) of masked variable at each dimension
+   INTEGER(iwp), DIMENSION(:),   ALLOCATABLE ::  bounds_end_new    !< start index (upper bound) of msked var at each dim
+   INTEGER(iwp), DIMENSION(:),   ALLOCATABLE ::  bounds_start_new  !< start index (lower bound) of msked var at each dim
    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  masked_indices    !< dummy list holding all masked indices along a dimension
 
@@ -2330,4 +2408,7 @@
 
 
+   return_value = 0
+   output_return_value = 0
+
    CALL internal_message( 'debug', routine_name // ': write ' // TRIM( name ) // &
                                    ' into file ' // TRIM( filename ) )
@@ -2343,8 +2424,8 @@
            SIZE( bounds_end ) /= SIZE( dimension_list ) )  THEN
          return_value = 1
-         CALL internal_message( 'error', routine_name //                      &
-                                         ': variable "' // TRIM( name ) //    &
-                                         '" in file "' // TRIM( filename ) // &
-                                         '": given bounds do not match with number of dimensions' )
+         CALL internal_message( 'error', routine_name //             &
+                                ': variable "' // TRIM( name ) //    &
+                                '" in file "' // TRIM( filename ) // &
+                                '": given bounds do not match with number of dimensions' )
       ENDIF
 
@@ -2819,384 +2900,255 @@
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int8_0d=var_int8_0d_pointer, return_value=return_value )
+                       var_int8_0d=var_int8_0d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int8_1d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int8_1d=var_int8_1d_pointer, return_value=return_value )
+                       var_int8_1d=var_int8_1d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int8_2d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int8_2d=var_int8_2d_pointer, return_value=return_value )
+                       var_int8_2d=var_int8_2d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int8_3d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int8_3d=var_int8_3d_pointer, return_value=return_value )
+                       var_int8_3d=var_int8_3d_pointer, return_value=output_return_value )
             !-- 16bit integer output
             ELSEIF ( PRESENT( var_int16_0d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int16_0d=var_int16_0d_pointer, return_value=return_value )
+                       var_int16_0d=var_int16_0d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int16_1d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int16_1d=var_int16_1d_pointer, return_value=return_value )
+                       var_int16_1d=var_int16_1d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int16_2d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int16_2d=var_int16_2d_pointer, return_value=return_value )
+                       var_int16_2d=var_int16_2d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int16_3d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int16_3d=var_int16_3d_pointer, return_value=return_value )
+                       var_int16_3d=var_int16_3d_pointer, return_value=output_return_value )
             !-- 32bit integer output
             ELSEIF ( PRESENT( var_int32_0d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int32_0d=var_int32_0d_pointer, return_value=return_value )
+                       var_int32_0d=var_int32_0d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int32_1d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int32_1d=var_int32_1d_pointer, return_value=return_value )
+                       var_int32_1d=var_int32_1d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int32_2d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int32_2d=var_int32_2d_pointer, return_value=return_value )
+                       var_int32_2d=var_int32_2d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int32_3d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int32_3d=var_int32_3d_pointer, return_value=return_value )
+                       var_int32_3d=var_int32_3d_pointer, return_value=output_return_value )
             !-- working-precision integer output
             ELSEIF ( PRESENT( var_intwp_0d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_intwp_0d=var_intwp_0d_pointer, return_value=return_value )
+                       var_intwp_0d=var_intwp_0d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_intwp_1d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_intwp_1d=var_intwp_1d_pointer, return_value=return_value )
+                       var_intwp_1d=var_intwp_1d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_intwp_2d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_intwp_2d=var_intwp_2d_pointer, return_value=return_value )
+                       var_intwp_2d=var_intwp_2d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_intwp_3d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_intwp_3d=var_intwp_3d_pointer, return_value=return_value )
+                       var_intwp_3d=var_intwp_3d_pointer, return_value=output_return_value )
             !-- 32bit real output
             ELSEIF ( PRESENT( var_real32_0d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real32_0d=var_real32_0d_pointer, return_value=return_value )
+                       var_real32_0d=var_real32_0d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_real32_1d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real32_1d=var_real32_1d_pointer, return_value=return_value )
+                       var_real32_1d=var_real32_1d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_real32_2d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real32_2d=var_real32_2d_pointer, return_value=return_value )
+                       var_real32_2d=var_real32_2d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_real32_3d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real32_3d=var_real32_3d_pointer, return_value=return_value )
+                       var_real32_3d=var_real32_3d_pointer, return_value=output_return_value )
             !-- 64bit real output
             ELSEIF ( PRESENT( var_real64_0d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real64_0d=var_real64_0d_pointer, return_value=return_value )
+                       var_real64_0d=var_real64_0d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_real64_1d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real64_1d=var_real64_1d_pointer, return_value=return_value )
+                       var_real64_1d=var_real64_1d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_real64_2d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real64_2d=var_real64_2d_pointer, return_value=return_value )
+                       var_real64_2d=var_real64_2d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_real64_3d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real64_3d=var_real64_3d_pointer, return_value=return_value )
+                       var_real64_3d=var_real64_3d_pointer, return_value=output_return_value )
             !-- working-precision real output
             ELSEIF ( PRESENT( var_realwp_0d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_realwp_0d=var_realwp_0d_pointer, return_value=return_value )
+                       var_realwp_0d=var_realwp_0d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_realwp_1d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_realwp_1d=var_realwp_1d_pointer, return_value=return_value )
+                       var_realwp_1d=var_realwp_1d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_realwp_2d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_realwp_2d=var_realwp_2d_pointer, return_value=return_value )
+                       var_realwp_2d=var_realwp_2d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_realwp_3d ) )  THEN
                CALL binary_write_variable( file_id, var_id,                                      &
                        bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_realwp_3d=var_realwp_3d_pointer, return_value=return_value )
+                       var_realwp_3d=var_realwp_3d_pointer, return_value=output_return_value )
             ELSE
                return_value = 1
-               CALL internal_message( 'error', routine_name //                                    &
-                                               ': variable "' // TRIM( name ) //                  &
-                                               '" in file "' // TRIM( filename ) //               &
-                                               '": output_type not supported by file format "' // &
-                                               TRIM( file_format ) // '"' )
+               CALL internal_message( 'error', routine_name //                           &
+                                      ': variable "' // TRIM( name ) //                  &
+                                      '" in file "' // TRIM( filename ) //               &
+                                      '": output_type not supported by file format "' // &
+                                      TRIM( file_format ) // '"' )
             ENDIF
 
-         CASE ( 'netcdf4-serial' )
+         CASE ( 'netcdf4-parallel', 'netcdf4-serial' )
             !-- 8bit integer output
             IF ( PRESENT( var_int8_0d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int8_0d=var_int8_0d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_int8_0d=var_int8_0d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int8_1d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int8_1d=var_int8_1d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_int8_1d=var_int8_1d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int8_2d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int8_2d=var_int8_2d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_int8_2d=var_int8_2d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int8_3d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int8_3d=var_int8_3d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_int8_3d=var_int8_3d_pointer, return_value=output_return_value )
             !-- 16bit integer output
             ELSEIF ( PRESENT( var_int16_0d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int16_0d=var_int16_0d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_int16_0d=var_int16_0d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int16_1d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int16_1d=var_int16_1d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_int16_1d=var_int16_1d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int16_2d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int16_2d=var_int16_2d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_int16_2d=var_int16_2d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int16_3d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int16_3d=var_int16_3d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_int16_3d=var_int16_3d_pointer, return_value=output_return_value )
             !-- 32bit integer output
             ELSEIF ( PRESENT( var_int32_0d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int32_0d=var_int32_0d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_int32_0d=var_int32_0d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int32_1d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int32_1d=var_int32_1d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_int32_1d=var_int32_1d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int32_2d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int32_2d=var_int32_2d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_int32_2d=var_int32_2d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_int32_3d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int32_3d=var_int32_3d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_int32_3d=var_int32_3d_pointer, return_value=output_return_value )
             !-- working-precision integer output
             ELSEIF ( PRESENT( var_intwp_0d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_intwp_0d=var_intwp_0d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_intwp_0d=var_intwp_0d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_intwp_1d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_intwp_1d=var_intwp_1d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_intwp_1d=var_intwp_1d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_intwp_2d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_intwp_2d=var_intwp_2d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_intwp_2d=var_intwp_2d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_intwp_3d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_intwp_3d=var_intwp_3d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_intwp_3d=var_intwp_3d_pointer, return_value=output_return_value )
             !-- 32bit real output
             ELSEIF ( PRESENT( var_real32_0d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real32_0d=var_real32_0d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_real32_0d=var_real32_0d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_real32_1d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real32_1d=var_real32_1d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_real32_1d=var_real32_1d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_real32_2d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real32_2d=var_real32_2d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_real32_2d=var_real32_2d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_real32_3d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real32_3d=var_real32_3d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_real32_3d=var_real32_3d_pointer, return_value=output_return_value )
             !-- 64bit real output
             ELSEIF ( PRESENT( var_real64_0d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real64_0d=var_real64_0d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_real64_0d=var_real64_0d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_real64_1d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real64_1d=var_real64_1d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_real64_1d=var_real64_1d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_real64_2d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real64_2d=var_real64_2d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_real64_2d=var_real64_2d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_real64_3d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real64_3d=var_real64_3d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_real64_3d=var_real64_3d_pointer, return_value=output_return_value )
             !-- working-precision real output
             ELSEIF ( PRESENT( var_realwp_0d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_realwp_0d=var_realwp_0d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_realwp_0d=var_realwp_0d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_realwp_1d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_realwp_1d=var_realwp_1d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_realwp_1d=var_realwp_1d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_realwp_2d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_realwp_2d=var_realwp_2d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_realwp_2d=var_realwp_2d_pointer, return_value=output_return_value )
             ELSEIF ( PRESENT( var_realwp_3d ) )  THEN
-               CALL netcdf4_serial_write_variable( file_id, var_id,                              &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_realwp_3d=var_realwp_3d_pointer, return_value=return_value )
+               CALL netcdf4_write_variable( file_id, var_id,                              &
+                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
+                       var_realwp_3d=var_realwp_3d_pointer, return_value=output_return_value )
             ELSE
                return_value = 1
-               CALL internal_message( 'error', routine_name //                                    &
-                                               ': variable "' // TRIM( name ) //                  &
-                                               '" in file "' // TRIM( filename ) //               &
-                                               '": output_type not supported by file format "' // &
-                                               TRIM( file_format ) // '"' )
-            ENDIF
-
-         CASE ( 'netcdf4-parallel' )
-            !-- 8bit integer output
-            IF ( PRESENT( var_int8_0d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int8_0d=var_int8_0d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_int8_1d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int8_1d=var_int8_1d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_int8_2d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int8_2d=var_int8_2d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_int8_3d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int8_3d=var_int8_3d_pointer, return_value=return_value )
-            !-- 16bit integer output
-            ELSEIF ( PRESENT( var_int16_0d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int16_0d=var_int16_0d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_int16_1d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int16_1d=var_int16_1d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_int16_2d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int16_2d=var_int16_2d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_int16_3d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int16_3d=var_int16_3d_pointer, return_value=return_value )
-            !-- 32bit integer output
-            ELSEIF ( PRESENT( var_int32_0d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int32_0d=var_int32_0d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_int32_1d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int32_1d=var_int32_1d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_int32_2d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int32_2d=var_int32_2d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_int32_3d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_int32_3d=var_int32_3d_pointer, return_value=return_value )
-            !-- working-precision integer output
-            ELSEIF ( PRESENT( var_intwp_0d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_intwp_0d=var_intwp_0d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_intwp_1d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_intwp_1d=var_intwp_1d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_intwp_2d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_intwp_2d=var_intwp_2d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_intwp_3d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_intwp_3d=var_intwp_3d_pointer, return_value=return_value )
-            !-- 32bit real output
-            ELSEIF ( PRESENT( var_real32_0d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real32_0d=var_real32_0d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_real32_1d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real32_1d=var_real32_1d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_real32_2d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real32_2d=var_real32_2d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_real32_3d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real32_3d=var_real32_3d_pointer, return_value=return_value )
-            !-- 64bit real output
-            ELSEIF ( PRESENT( var_real64_0d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real64_0d=var_real64_0d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_real64_1d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real64_1d=var_real64_1d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_real64_2d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real64_2d=var_real64_2d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_real64_3d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_real64_3d=var_real64_3d_pointer, return_value=return_value )
-            !-- working-precision real output
-            ELSEIF ( PRESENT( var_realwp_0d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_realwp_0d=var_realwp_0d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_realwp_1d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_realwp_1d=var_realwp_1d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_realwp_2d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_realwp_2d=var_realwp_2d_pointer, return_value=return_value )
-            ELSEIF ( PRESENT( var_realwp_3d ) )  THEN
-               CALL netcdf4_parallel_write_variable( file_id, var_id,                            &
-                       bounds_start_new, bounds_end_new, bounds_dim_start, do_output, is_global, &
-                       var_realwp_3d=var_realwp_3d_pointer, return_value=return_value )
-            ELSE
-               return_value = 1
-               CALL internal_message( 'error', routine_name //                                    &
-                                               ': variable "' // TRIM( name ) //                  &
-                                               '" in file "' // TRIM( filename ) //               &
-                                               '": output_type not supported by file format "' // &
-                                               TRIM( file_format ) // '"' )
+               CALL internal_message( 'error', routine_name //                           &
+                                      ': variable "' // TRIM( name ) //                  &
+                                      '" in file "' // TRIM( filename ) //               &
+                                      '": output_type not supported by file format "' // &
+                                      TRIM( file_format ) // '"' )
             ENDIF
 
@@ -3210,4 +3162,11 @@
       END SELECT
 
+      IF ( return_value == 0  .AND.  output_return_value /= 0 )  THEN
+         return_value = 1
+         CALL internal_message( 'error', routine_name //                              &
+                                ': error while writing variable "' // TRIM( name ) // &
+                                '" in file "' // TRIM( filename ) // '"' )
+      ENDIF
+
    ENDIF
 
@@ -3248,4 +3207,15 @@
    DO  f = 1, nf
       IF ( TRIM( filename ) == TRIM( files(f)%name ) )  THEN
+         
+         IF ( .NOT. files(f)%is_init )  THEN
+            return_value = 1
+            CALL internal_message( 'error', routine_name //                    &
+                                   ': file "' // TRIM( filename ) //           &
+                                   '" is not initialized. ' //                 &
+                                   'Writing variable "' // TRIM( var_name ) // &
+                                   '" to file is impossible.' )
+            EXIT
+         ENDIF
+         
          file_id     = files(f)%id
          file_format = files(f)%format
@@ -3421,5 +3391,6 @@
 
    INTEGER(iwp) ::  return_value           !< return value
-   INTEGER(iwp) ::  return_value_internal  !< return value from called routines
+   INTEGER(iwp) ::  return_value_internal  !< error code after closing a single file
+   INTEGER(iwp) ::  output_return_value    !< return value from called routines
    INTEGER(iwp) ::  f                      !< loop index
 
@@ -3429,25 +3400,35 @@
    DO  f = 1, nf
 
-      return_value_internal = 0
-
-      SELECT CASE ( TRIM( files(f)%format ) )
-
-         CASE ( 'binary' )
-            CALL binary_finalize( files(f)%id, return_value_internal )
-
-         CASE ( 'netcdf4-serial' )
-            CALL netcdf4_serial_finalize( files(f)%id, return_value_internal )
-
-         CASE ( 'netcdf4-parallel' )
-            CALL netcdf4_parallel_finalize( files(f)%id, return_value_internal )
-
-         CASE DEFAULT
-            return_value_internal = 1
-            CALL internal_message( 'error', routine_name // &
-                                   ': unsupported file format "' // TRIM( files(f)%format ) )
-
-      END SELECT
-
-      IF ( return_value_internal /= 0 )  return_value = return_value_internal
+      IF ( files(f)%is_init )  THEN
+
+         output_return_value = 0
+         return_value_internal = 0
+
+         SELECT CASE ( TRIM( files(f)%format ) )
+
+            CASE ( 'binary' )
+               CALL binary_finalize( files(f)%id, output_return_value )
+
+            CASE ( 'netcdf4-parallel', 'netcdf4-serial' )
+               CALL netcdf4_finalize( files(f)%id, output_return_value )
+
+            CASE DEFAULT
+               return_value_internal = 1
+
+         END SELECT
+
+         IF ( output_return_value /= 0 )  THEN
+            return_value = output_return_value
+            CALL internal_message( 'error', routine_name //             &
+                                   ': error while finalizing file "' // &
+                                   TRIM( files(f)%name ) // '"' )
+         ELSEIF ( return_value_internal /= 0 )  THEN
+            return_value = return_value_internal
+            CALL internal_message( 'error', routine_name //         &
+                                   ': unsupported file format "' // &
+                                   TRIM( files(f)%format ) // '"' )
+         ENDIF
+
+      ENDIF
 
    ENDDO
@@ -3488,15 +3469,12 @@
 
    CHARACTER(LEN=800), INTENT(OUT) ::  error_message         !< return error message to main program
-   CHARACTER(LEN=800)              ::  module_error_message  !< error message created by other module
-
-
-   CALL binary_get_error_message( module_error_message )
-   internal_error_message = TRIM( internal_error_message ) // module_error_message
-
-   CALL netcdf4_serial_get_error_message( module_error_message )
-   internal_error_message = TRIM( internal_error_message ) // module_error_message
-
-   CALL netcdf4_parallel_get_error_message( module_error_message )
-   internal_error_message = TRIM( internal_error_message ) // module_error_message
+   CHARACTER(LEN=800)              ::  output_error_message  !< error message created by other module
+
+
+   CALL binary_get_error_message( output_error_message )
+   internal_error_message = TRIM( internal_error_message ) // output_error_message
+
+   CALL netcdf4_get_error_message( output_error_message )
+   internal_error_message = TRIM( internal_error_message ) // output_error_message
 
    error_message = internal_error_message
