Index: /palm/trunk/SOURCE/data_output_binary_module.f90
===================================================================
--- /palm/trunk/SOURCE/data_output_binary_module.f90	(revision 4140)
+++ /palm/trunk/SOURCE/data_output_binary_module.f90	(revision 4141)
@@ -52,5 +52,5 @@
    IMPLICIT NONE
 
-   INTEGER(iwp), PARAMETER ::  charlen = 100_iwp  !< maximum length of character variables
+   INTEGER, PARAMETER ::  charlen = 100  !< maximum length of character variables
 
    CHARACTER(LEN=*), PARAMETER ::  config_file_name = 'BINARY_TO_NETCDF_CONFIG'  !< name of config file
@@ -62,13 +62,13 @@
    CHARACTER(LEN=800)          ::  temp_string                  !< dummy string
 
-   INTEGER(iwp) ::  binary_file_lowest_unit = 1000  !< lowest unit number of all binary files created by this module
-   INTEGER(iwp) ::  config_file_unit                !< unit number of config file
-   INTEGER(iwp) ::  debug_output_unit               !< Fortran Unit Number of the debug-output file
-   INTEGER(iwp) ::  global_id_in_file = -1          !< value of global ID within a file
-   INTEGER      ::  master_rank                     !< master rank for tasks to be executed by single PE only
-   INTEGER(iwp) ::  next_available_unit             !< next unit number available for new file
-   INTEGER      ::  output_group_comm               !< MPI communicator addressing all MPI ranks which participate in output
-
-   INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  files_highest_var_id  !< highest assigned ID of variable or dimension in a file
+   INTEGER ::  binary_file_lowest_unit = 1000  !< lowest unit number of all binary files created by this module
+   INTEGER ::  config_file_unit                !< unit number of config file
+   INTEGER ::  debug_output_unit               !< Fortran Unit Number of the debug-output file
+   INTEGER ::  global_id_in_file = -1          !< value of global ID within a file
+   INTEGER ::  master_rank                     !< master rank for tasks to be executed by single PE only
+   INTEGER ::  next_available_unit             !< next unit number available for new file
+   INTEGER ::  output_group_comm               !< MPI communicator addressing all MPI ranks which participate in output
+
+   INTEGER, DIMENSION(:), ALLOCATABLE ::  files_highest_variable_id  !< highest assigned ID of variable or dimension in a file
 
    LOGICAL ::  binary_open_file_first_call = .TRUE.  !< true if binary_open_file routine was not called yet
@@ -100,7 +100,7 @@
    END INTERFACE binary_write_attribute
 
-   INTERFACE binary_init_end
-      MODULE PROCEDURE binary_init_end
-   END INTERFACE binary_init_end
+   INTERFACE binary_stop_file_header_definition
+      MODULE PROCEDURE binary_stop_file_header_definition
+   END INTERFACE binary_stop_file_header_definition
 
    INTERFACE binary_write_variable
@@ -120,5 +120,5 @@
       binary_get_error_message, &
       binary_init_dimension, &
-      binary_init_end, &
+      binary_stop_file_header_definition, &
       binary_init_module, &
       binary_init_variable, &
@@ -143,8 +143,8 @@
                                                                  !> must be unique for each output group
 
-   INTEGER(iwp), INTENT(IN) ::  dom_global_id              !< global id within a file defined by DOM
-   INTEGER,      INTENT(IN) ::  master_output_rank         !< MPI rank executing tasks which must be executed by a single PE
-   INTEGER,      INTENT(IN) ::  mpi_comm_of_output_group   !< MPI communicator specifying the rank group participating in output
-   INTEGER(iwp), INTENT(IN) ::  program_debug_output_unit  !< file unit number for debug output
+   INTEGER, INTENT(IN) ::  dom_global_id              !< global id within a file defined by DOM
+   INTEGER, INTENT(IN) ::  master_output_rank         !< MPI rank executing tasks which must be executed by a single PE
+   INTEGER, INTENT(IN) ::  mpi_comm_of_output_group   !< MPI communicator specifying the rank group participating in output
+   INTEGER, INTENT(IN) ::  program_debug_output_unit  !< file unit number for debug output
 
    LOGICAL, INTENT(IN) ::  debug_output  !< if true, debug output is printed
@@ -167,8 +167,8 @@
 !> Open binary file.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE binary_open_file( mode, filename, file_id, return_value )
+SUBROUTINE binary_open_file( mode, file_name, file_id, return_value )
 
    CHARACTER(LEN=charlen)             ::  bin_filename = ''  !< actual name of binary file
-   CHARACTER(LEN=charlen), INTENT(IN) ::  filename           !< name of file
+   CHARACTER(LEN=charlen), INTENT(IN) ::  file_name          !< name of file
    CHARACTER(LEN=7)                   ::  my_rank_char       !< string containing value of my_rank with leading zeros
    CHARACTER(LEN=*),       INTENT(IN) ::  mode               !< operation mode
@@ -176,10 +176,10 @@
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'binary_open_file'  !< name of this routine
 
-   INTEGER(iwp), INTENT(OUT) ::  file_id       !< file ID
-   INTEGER                   ::  my_rank       !< MPI rank of local processor
-   INTEGER                   ::  nrank         !< number of MPI ranks participating in output
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
-
-   INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  files_highest_var_id_tmp  !< temporary list of given variable IDs in file
+   INTEGER, INTENT(OUT) ::  file_id       !< file ID
+   INTEGER              ::  my_rank       !< MPI rank of local processor
+   INTEGER              ::  nranks        !< number of MPI ranks participating in output
+   INTEGER, INTENT(OUT) ::  return_value  !< return value
+
+   INTEGER, DIMENSION(:), ALLOCATABLE ::  files_highest_variable_id_tmp  !< temporary list of given variable IDs in file
 
    LOGICAL ::  file_exists  !< true if file to be opened already exists
@@ -189,5 +189,5 @@
 
 #if defined( __parallel )
-   CALL MPI_COMM_SIZE( output_group_comm, nrank, return_value )
+   CALL MPI_COMM_SIZE( output_group_comm, nranks, return_value )
    IF ( return_value == 0 )  CALL MPI_COMM_RANK( output_group_comm, my_rank, return_value )
    IF ( return_value == 0 )  THEN
@@ -197,5 +197,5 @@
    ENDIF
 #else
-   nrank = 1
+   nranks = 1
    my_rank = master_rank
    WRITE( my_rank_char, '("_",I6.6)' )  my_rank
@@ -237,5 +237,5 @@
 
             !-- Write some general information to config file
-            WRITE( config_file_unit )  nrank
+            WRITE( config_file_unit )  nranks
             WRITE( config_file_unit )  master_rank
             WRITE( config_file_unit )  LEN( file_prefix )
@@ -260,5 +260,5 @@
    IF ( return_value == 0 )  THEN
 
-      bin_filename = file_prefix // TRIM( filename ) // TRIM( file_suffix ) // my_rank_char
+      bin_filename = file_prefix // TRIM( file_name ) // TRIM( file_suffix ) // my_rank_char
 
       !-- Remove any pre-existing file
@@ -267,5 +267,5 @@
       IF ( file_exists )  THEN
          CALL internal_message( 'debug', routine_name // &
-                                         ': remove existing file ' // TRIM( bin_filename ) )
+                                ': remove existing file ' // TRIM( bin_filename ) )
          !> @note Fortran2008 feature 'EXECUTE_COMMAND_LINE' not yet supported by
          !>       PGI 18.10 compiler. Hence, non-standard 'SYSTEM' call must be used
@@ -282,7 +282,7 @@
       IF ( return_value == 0 )  THEN
 
-         !-- Add filename to config file
+         !-- Add file_name to config file
          IF ( my_rank == master_rank )  THEN
-            WRITE( config_file_unit )  filename
+            WRITE( config_file_unit )  file_name
          ENDIF
 
@@ -294,23 +294,23 @@
          WRITE ( file_id )  charlen
          WRITE ( file_id )  file_id
-         WRITE ( file_id )  filename
+         WRITE ( file_id )  file_name
 
          !-- Extend file-variable/dimension-ID list by 1 and set it to 0 for new file.
-         IF ( ALLOCATED( files_highest_var_id ) )  THEN
-            ALLOCATE( files_highest_var_id_tmp(SIZE( files_highest_var_id )) )
-            files_highest_var_id_tmp = files_highest_var_id
-            DEALLOCATE( files_highest_var_id )
-            ALLOCATE( files_highest_var_id(binary_file_lowest_unit+1:file_id) )
-            files_highest_var_id(:file_id-1) = files_highest_var_id_tmp
-            DEALLOCATE( files_highest_var_id_tmp )
+         IF ( ALLOCATED( files_highest_variable_id ) )  THEN
+            ALLOCATE( files_highest_variable_id_tmp(SIZE( files_highest_variable_id )) )
+            files_highest_variable_id_tmp = files_highest_variable_id
+            DEALLOCATE( files_highest_variable_id )
+            ALLOCATE( files_highest_variable_id(binary_file_lowest_unit+1:file_id) )
+            files_highest_variable_id(:file_id-1) = files_highest_variable_id_tmp
+            DEALLOCATE( files_highest_variable_id_tmp )
          ELSE
-            ALLOCATE( files_highest_var_id(binary_file_lowest_unit+1:file_id) )
+            ALLOCATE( files_highest_variable_id(binary_file_lowest_unit+1:file_id) )
          ENDIF
-         files_highest_var_id(file_id) = 0_iwp
+         files_highest_variable_id(file_id) = 0
 
       ELSE
          return_value = 1
          CALL internal_message( 'error', routine_name // &
-                                         ': could not open file "' // TRIM( filename ) // '"')
+                                ': could not open file "' // TRIM( file_name ) // '"')
       ENDIF
 
@@ -324,25 +324,25 @@
 !> Write attribute to file.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE binary_write_attribute( file_id, var_id, att_name, att_value_char, &
-              att_value_int8, att_value_int16, att_value_int32,               &
-              att_value_real32, att_value_real64, return_value )
+SUBROUTINE binary_write_attribute( file_id, variable_id, attribute_name, &
+              value_char, value_int8, value_int16, value_int32,          &
+              value_real32, value_real64, return_value )
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'binary_write_attribute'  !< name of this routine
 
-   CHARACTER(LEN=charlen), INTENT(IN)           ::  att_name        !< name of attribute
-   CHARACTER(LEN=charlen), INTENT(IN), OPTIONAL ::  att_value_char  !< value of attribute
-   CHARACTER(LEN=charlen)                       ::  att_type        !< data type of attribute
-   CHARACTER(LEN=charlen)                       ::  out_str         !< output string
-
-   INTEGER(KIND=1), INTENT(IN), OPTIONAL ::  att_value_int8   !< value of attribute
-   INTEGER(KIND=2), INTENT(IN), OPTIONAL ::  att_value_int16  !< value of attribute
-   INTEGER(KIND=4), INTENT(IN), OPTIONAL ::  att_value_int32  !< value of attribute
-
-   INTEGER(iwp), INTENT(IN)  ::  file_id       !< file ID
-   INTEGER(iwp), INTENT(IN)  ::  var_id        !< variable ID
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
-
-   REAL(KIND=4), INTENT(IN), OPTIONAL ::  att_value_real32  !< value of attribute
-   REAL(KIND=8), INTENT(IN), OPTIONAL ::  att_value_real64  !< value of attribute
+   CHARACTER(LEN=charlen), INTENT(IN)           ::  attribute_name        !< name of attribute
+   CHARACTER(LEN=charlen), INTENT(IN), OPTIONAL ::  value_char  !< value of attribute
+   CHARACTER(LEN=charlen)                       ::  attribute_type        !< data type of attribute
+   CHARACTER(LEN=charlen)                       ::  output_string         !< output string
+
+   INTEGER(KIND=1), INTENT(IN), OPTIONAL ::  value_int8   !< value of attribute
+   INTEGER(KIND=2), INTENT(IN), OPTIONAL ::  value_int16  !< value of attribute
+   INTEGER(KIND=4), INTENT(IN), OPTIONAL ::  value_int32  !< value of attribute
+
+   INTEGER, INTENT(IN)  ::  file_id       !< file ID
+   INTEGER, INTENT(IN)  ::  variable_id   !< variable ID
+   INTEGER, INTENT(OUT) ::  return_value  !< return value
+
+   REAL(KIND=4), INTENT(IN), OPTIONAL ::  value_real32  !< value of attribute
+   REAL(KIND=8), INTENT(IN), OPTIONAL ::  value_real64  !< value of attribute
 
 
@@ -350,41 +350,41 @@
 
    CALL internal_message( 'debug', TRIM( routine_name ) // &
-                                   ': write attribute ' // TRIM( att_name ) )
+                          ': write attribute ' // TRIM( attribute_name ) )
 
    !-- Write attribute to file
-   out_str = 'attribute'
-   WRITE( file_id )  out_str
-
-   WRITE( file_id )  var_id
-   WRITE( file_id )  att_name
-
-   IF ( PRESENT( att_value_char ) )  THEN
-      att_type = 'char'
-      WRITE( file_id )  att_type
-      WRITE( file_id )  att_value_char
-   ELSEIF ( PRESENT( att_value_int8 ) )  THEN
-      att_type = 'int8'
-      WRITE( file_id )  att_type
-      WRITE( file_id )  att_value_int8
-   ELSEIF ( PRESENT( att_value_int16 ) )  THEN
-      att_type = 'int16'
-      WRITE( file_id )  att_type
-      WRITE( file_id )  att_value_int16
-   ELSEIF ( PRESENT( att_value_int32 ) )  THEN
-      att_type = 'int32'
-      WRITE( file_id )  att_type
-      WRITE( file_id )  att_value_int32
-   ELSEIF ( PRESENT( att_value_real32 ) )  THEN
-      att_type = 'real32'
-      WRITE( file_id )  att_type
-      WRITE( file_id )  att_value_real32
-   ELSEIF ( PRESENT( att_value_real64 ) )  THEN
-      att_type = 'real64'
-      WRITE( file_id )  att_type
-      WRITE( file_id )  att_value_real64
+   output_string = 'attribute'
+   WRITE( file_id )  output_string
+
+   WRITE( file_id )  variable_id
+   WRITE( file_id )  attribute_name
+
+   IF ( PRESENT( value_char ) )  THEN
+      attribute_type = 'char'
+      WRITE( file_id )  attribute_type
+      WRITE( file_id )  value_char
+   ELSEIF ( PRESENT( value_int8 ) )  THEN
+      attribute_type = 'int8'
+      WRITE( file_id )  attribute_type
+      WRITE( file_id )  value_int8
+   ELSEIF ( PRESENT( value_int16 ) )  THEN
+      attribute_type = 'int16'
+      WRITE( file_id )  attribute_type
+      WRITE( file_id )  value_int16
+   ELSEIF ( PRESENT( value_int32 ) )  THEN
+      attribute_type = 'int32'
+      WRITE( file_id )  attribute_type
+      WRITE( file_id )  value_int32
+   ELSEIF ( PRESENT( value_real32 ) )  THEN
+      attribute_type = 'real32'
+      WRITE( file_id )  attribute_type
+      WRITE( file_id )  value_real32
+   ELSEIF ( PRESENT( value_real64 ) )  THEN
+      attribute_type = 'real64'
+      WRITE( file_id )  attribute_type
+      WRITE( file_id )  value_real64
    ELSE
       return_value = 1
       CALL internal_message( 'error', TRIM( routine_name ) // &
-                             ': attribute "' // TRIM( att_name ) // '": no value given' )
+                             ': no value given for attribute "' // TRIM( attribute_name ) // '"' )
    ENDIF
 
@@ -394,27 +394,27 @@
 ! Description:
 ! ------------
-!> Initialize dimension. Write information in file header and save dimension
-!> values to be later written to file.
-!--------------------------------------------------------------------------------------------------!
-SUBROUTINE binary_init_dimension( mode, file_id, dim_id, var_id, &
-              dim_name, dim_type, dim_length, return_value )
-
-   CHARACTER(LEN=charlen), INTENT(IN) ::  dim_name  !< name of dimension
-   CHARACTER(LEN=charlen), INTENT(IN) ::  dim_type  !< data type of dimension
-   CHARACTER(LEN=charlen)             ::  out_str   !< output string
-   CHARACTER(LEN=*),       INTENT(IN) ::  mode      !< operation mode
+!> Initialize dimension. Write information in file header
+!> and save dimension values to be later written to file.
+!--------------------------------------------------------------------------------------------------!
+SUBROUTINE binary_init_dimension( mode, file_id, dimension_id, variable_id, &
+              dimension_name, dimension_type, dimension_length, return_value )
+
+   CHARACTER(LEN=charlen), INTENT(IN) ::  dimension_name  !< name of dimension
+   CHARACTER(LEN=charlen), INTENT(IN) ::  dimension_type  !< data type of dimension
+   CHARACTER(LEN=charlen)             ::  output_string   !< output string
+   CHARACTER(LEN=*),       INTENT(IN) ::  mode            !< operation mode
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'binary_init_dimension'  !< name of this routine
 
-   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        !< variable ID
+   INTEGER, INTENT(OUT) ::  dimension_id      !< dimension ID
+   INTEGER, INTENT(IN)  ::  dimension_length  !< length of dimension
+   INTEGER, INTENT(IN)  ::  file_id           !< file ID
+   INTEGER, INTENT(OUT) ::  return_value      !< return value
+   INTEGER, INTENT(OUT) ::  variable_id       !< variable ID
 
 
    return_value = 0
 
-   CALL internal_message( 'debug', routine_name // ': init dimension ' // TRIM( dim_name ) )
+   CALL internal_message( 'debug', routine_name // ': init dimension ' // TRIM( dimension_name ) )
 
    !-- Check mode (not required, added for compatibility reasons only)
@@ -422,21 +422,21 @@
 
    !-- Assign dimension ID
-   dim_id = files_highest_var_id( file_id ) + 1
-   files_highest_var_id( file_id ) = dim_id
+   dimension_id = files_highest_variable_id( file_id ) + 1
+   files_highest_variable_id( file_id ) = dimension_id
 
    !-- Define dimension in file
-   out_str = 'dimension'
-   WRITE( file_id )  out_str
-   WRITE( file_id )  dim_name
-   WRITE( file_id )  dim_id
-   WRITE( file_id )  dim_type
-   WRITE( file_id )  dim_length
+   output_string = 'dimension'
+   WRITE( file_id )  output_string
+   WRITE( file_id )  dimension_name
+   WRITE( file_id )  dimension_id
+   WRITE( file_id )  dimension_type
+   WRITE( file_id )  dimension_length
 
    !-- Define variable associated with dimension
-   CALL binary_init_variable( mode, file_id, var_id, dim_name, dim_type, (/dim_id/), &
-                              is_global=.TRUE., return_value=return_value )
+   CALL binary_init_variable( mode, file_id, variable_id, dimension_name, dimension_type, &
+                              (/ dimension_id /), is_global=.TRUE., return_value=return_value )
    IF ( return_value /= 0 )  THEN
       CALL internal_message( 'error', routine_name // &
-                                      ': init dimension "' // TRIM( dim_name ) // '"' )
+                             ': init dimension "' // TRIM( dimension_name ) // '"' )
    ENDIF
 
@@ -448,19 +448,19 @@
 !> Initialize variable. Write information of variable into file header.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE binary_init_variable( mode, file_id, var_id, var_name, var_type, &
-                                 var_dim_ids, is_global, return_value )
-
-   CHARACTER(LEN=charlen)             ::  out_str   !< output string
-   CHARACTER(LEN=charlen), INTENT(IN) ::  var_name  !< name of variable
-   CHARACTER(LEN=charlen), INTENT(IN) ::  var_type  !< data type of variable
-   CHARACTER(LEN=*),       INTENT(IN) ::  mode      !< operation mode
+SUBROUTINE binary_init_variable( mode, file_id, variable_id, variable_name, variable_type, &
+                                 dimension_ids, is_global, return_value )
+
+   CHARACTER(LEN=charlen)             ::  output_string   !< output string
+   CHARACTER(LEN=charlen), INTENT(IN) ::  variable_name   !< name of variable
+   CHARACTER(LEN=charlen), INTENT(IN) ::  variable_type   !< data type of variable
+   CHARACTER(LEN=*),       INTENT(IN) ::  mode            !< operation mode
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'binary_init_variable'  !< name of this routine
 
-   INTEGER(iwp), INTENT(IN)  ::  file_id       !< file ID
-   INTEGER(iwp), INTENT(OUT) ::  var_id        !< variable ID
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
-
-   INTEGER(iwp), DIMENSION(:), INTENT(IN) ::  var_dim_ids  !< list of dimension IDs used by variable
+   INTEGER, INTENT(IN)  ::  file_id       !< file ID
+   INTEGER, INTENT(OUT) ::  variable_id   !< variable ID
+   INTEGER, INTENT(OUT) ::  return_value  !< return value
+
+   INTEGER, DIMENSION(:), INTENT(IN) ::  dimension_ids  !< list of dimension IDs used by variable
 
    LOGICAL, INTENT(IN)  ::  is_global  !< true if variable is global (same on all PE)
@@ -469,5 +469,5 @@
    return_value = 0
 
-   CALL internal_message( 'debug', routine_name // ': init variable ' // TRIM( var_name ) )
+   CALL internal_message( 'debug', routine_name // ': init variable ' // TRIM( variable_name ) )
 
    !-- Check mode (not required, added for compatibility reasons only)
@@ -478,15 +478,15 @@
 
    !-- Assign variable ID
-   var_id = files_highest_var_id( file_id ) + 1
-   files_highest_var_id( file_id ) = var_id
+   variable_id = files_highest_variable_id( file_id ) + 1
+   files_highest_variable_id( file_id ) = variable_id
 
    !-- Write variable information in file
-   out_str = 'variable'
-   WRITE( file_id )  out_str
-   WRITE( file_id )  var_name
-   WRITE( file_id )  var_id
-   WRITE( file_id )  var_type
-   WRITE( file_id )  SIZE( var_dim_ids )
-   WRITE( file_id )  var_dim_ids
+   output_string = 'variable'
+   WRITE( file_id )  output_string
+   WRITE( file_id )  variable_name
+   WRITE( file_id )  variable_id
+   WRITE( file_id )  variable_type
+   WRITE( file_id )  SIZE( dimension_ids )
+   WRITE( file_id )  dimension_ids
 
 END SUBROUTINE binary_init_variable
@@ -497,12 +497,12 @@
 !> Leave file definition state.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE binary_init_end( file_id, return_value )
-
-   CHARACTER(LEN=charlen) ::  out_str  !< output string
-
-   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'binary_init_end'  !< name of this routine
-
-   INTEGER(iwp), INTENT(IN)  ::  file_id       !< file ID
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
+SUBROUTINE binary_stop_file_header_definition( file_id, return_value )
+
+   CHARACTER(LEN=charlen) ::  output_string  !< output string
+
+   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'binary_stop_file_header_definition'  !< name of this routine
+
+   INTEGER, INTENT(IN)  ::  file_id       !< file ID
+   INTEGER, INTENT(OUT) ::  return_value  !< return value
 
 
@@ -510,12 +510,11 @@
 
    WRITE( temp_string, * ) file_id
-   CALL internal_message( 'debug', &
-                          routine_name // &
+   CALL internal_message( 'debug', routine_name // &
                           ': finalize file definition (file_id=' // TRIM( temp_string ) // ')' )
 
-   out_str = '*** end file header ***'
-   WRITE( file_id )  out_str
-
-END SUBROUTINE binary_init_end
+   output_string = '*** end file header ***'
+   WRITE( file_id )  output_string
+
+END SUBROUTINE binary_stop_file_header_definition
 
 !--------------------------------------------------------------------------------------------------!
@@ -524,69 +523,64 @@
 !> Write variable to file.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE binary_write_variable(                                         &
-              file_id, var_id, bounds_start, value_counts, bounds_origin, &
-              is_global,                                                  &
-              var_int8_0d,   var_int8_1d,   var_int8_2d,   var_int8_3d,   &
-              var_int16_0d,  var_int16_1d,  var_int16_2d,  var_int16_3d,  &
-              var_int32_0d,  var_int32_1d,  var_int32_2d,  var_int32_3d,  &
-              var_intwp_0d,  var_intwp_1d,  var_intwp_2d,  var_intwp_3d,  &
-              var_real32_0d, var_real32_1d, var_real32_2d, var_real32_3d, &
-              var_real64_0d, var_real64_1d, var_real64_2d, var_real64_3d, &
-              var_realwp_0d, var_realwp_1d, var_realwp_2d, var_realwp_3d, &
+SUBROUTINE binary_write_variable(                                                     &
+              file_id, variable_id, bounds_start, value_counts, bounds_origin,        &
+              is_global,                                                              &
+              values_int8_0d,   values_int8_1d,   values_int8_2d,   values_int8_3d,   &
+              values_int16_0d,  values_int16_1d,  values_int16_2d,  values_int16_3d,  &
+              values_int32_0d,  values_int32_1d,  values_int32_2d,  values_int32_3d,  &
+              values_intwp_0d,  values_intwp_1d,  values_intwp_2d,  values_intwp_3d,  &
+              values_real32_0d, values_real32_1d, values_real32_2d, values_real32_3d, &
+              values_real64_0d, values_real64_1d, values_real64_2d, values_real64_3d, &
+              values_realwp_0d, values_realwp_1d, values_realwp_2d, values_realwp_3d, &
               return_value )
 
-   CHARACTER(LEN=charlen) ::  out_str  !< output string
+   CHARACTER(LEN=charlen) ::  output_string  !< output string
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'binary_write_variable'  !< name of this routine
 
-   INTEGER(iwp), INTENT(IN)  ::  file_id       !< file ID
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
-   INTEGER(iwp), INTENT(IN)  ::  var_id        !< variable ID
-
-   INTEGER(iwp), DIMENSION(:), INTENT(IN) ::  bounds_origin  !< starting index of each dimension
-   INTEGER(iwp), DIMENSION(:), INTENT(IN) ::  bounds_start   !< starting index of variable
-   INTEGER(iwp), DIMENSION(:), INTENT(IN) ::  value_counts   !< count of values along each dimension to be written
-
-   INTEGER(KIND=1), POINTER, INTENT(IN), OPTIONAL                               ::  var_int8_0d  !< output variable
-   INTEGER(KIND=1), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_int8_1d  !< output variable
-   INTEGER(KIND=1), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_int8_2d  !< output variable
-   INTEGER(KIND=1), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_int8_3d  !< output variable
-
-   INTEGER(KIND=2), POINTER, INTENT(IN), OPTIONAL                               ::  var_int16_0d  !< output variable
-   INTEGER(KIND=2), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_int16_1d  !< output variable
-   INTEGER(KIND=2), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_int16_2d  !< output variable
-   INTEGER(KIND=2), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_int16_3d  !< output variable
-
-   INTEGER(KIND=4), POINTER, INTENT(IN), OPTIONAL                               ::  var_int32_0d  !< output variable
-   INTEGER(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_int32_1d  !< output variable
-   INTEGER(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_int32_2d  !< output variable
-   INTEGER(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_int32_3d  !< output variable
-
-   INTEGER(iwp), POINTER, INTENT(IN), OPTIONAL                               ::  var_intwp_0d  !< output variable
-   INTEGER(iwp), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_intwp_1d  !< output variable
-   INTEGER(iwp), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_intwp_2d  !< output variable
-   INTEGER(iwp), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_intwp_3d  !< output variable
+   INTEGER, INTENT(IN)  ::  file_id       !< file ID
+   INTEGER, INTENT(OUT) ::  return_value  !< return value
+   INTEGER, INTENT(IN)  ::  variable_id   !< variable ID
+
+   INTEGER, DIMENSION(:), INTENT(IN) ::  bounds_origin  !< starting index of each dimension
+   INTEGER, DIMENSION(:), INTENT(IN) ::  bounds_start   !< starting index of variable
+   INTEGER, DIMENSION(:), INTENT(IN) ::  value_counts   !< count of values along each dimension to be written
+
+   INTEGER(KIND=1), POINTER,             INTENT(IN), OPTIONAL                   ::  values_int8_0d   !< output variable
+   INTEGER(KIND=2), POINTER,             INTENT(IN), OPTIONAL                   ::  values_int16_0d  !< output variable
+   INTEGER(KIND=4), POINTER,             INTENT(IN), OPTIONAL                   ::  values_int32_0d  !< output variable
+   INTEGER(iwp),    POINTER,             INTENT(IN), OPTIONAL                   ::  values_intwp_0d  !< output variable
+   INTEGER(KIND=1), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_int8_1d   !< output variable
+   INTEGER(KIND=2), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_int16_1d  !< output variable
+   INTEGER(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_int32_1d  !< output variable
+   INTEGER(iwp),    POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_intwp_1d  !< output variable
+   INTEGER(KIND=1), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_int8_2d   !< output variable
+   INTEGER(KIND=2), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_int16_2d  !< output variable
+   INTEGER(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_int32_2d  !< output variable
+   INTEGER(iwp),    POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_intwp_2d  !< output variable
+   INTEGER(KIND=1), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_int8_3d   !< output variable
+   INTEGER(KIND=2), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_int16_3d  !< output variable
+   INTEGER(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_int32_3d  !< output variable
+   INTEGER(iwp),    POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_intwp_3d  !< output variable
 
    LOGICAL, INTENT(IN) ::  is_global  !< true if variable is global (same on all PE)
 
-   REAL(KIND=4), POINTER, INTENT(IN), OPTIONAL                               ::  var_real32_0d  !< output variable
-   REAL(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_real32_1d  !< output variable
-   REAL(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_real32_2d  !< output variable
-   REAL(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_real32_3d  !< output variable
-
-   REAL(KIND=8), POINTER, INTENT(IN), OPTIONAL                               ::  var_real64_0d  !< output variable
-   REAL(KIND=8), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_real64_1d  !< output variable
-   REAL(KIND=8), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_real64_2d  !< output variable
-   REAL(KIND=8), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_real64_3d  !< output variable
-
-   REAL(wp), POINTER, INTENT(IN), OPTIONAL                               ::  var_realwp_0d  !< output variable
-   REAL(wp), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_realwp_1d  !< output variable
-   REAL(wp), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_realwp_2d  !< output variable
-   REAL(wp), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_realwp_3d  !< output variable
+   REAL(KIND=4), POINTER,             INTENT(IN), OPTIONAL                   ::  values_real32_0d  !< output variable
+   REAL(KIND=8), POINTER,             INTENT(IN), OPTIONAL                   ::  values_real64_0d  !< output variable
+   REAL(wp),     POINTER,             INTENT(IN), OPTIONAL                   ::  values_realwp_0d  !< output variable
+   REAL(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_real32_1d  !< output variable
+   REAL(KIND=8), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_real64_1d  !< output variable
+   REAL(wp),     POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_realwp_1d  !< output variable
+   REAL(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_real32_2d  !< output variable
+   REAL(KIND=8), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_real64_2d  !< output variable
+   REAL(wp),     POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_realwp_2d  !< output variable
+   REAL(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_real32_3d  !< output variable
+   REAL(KIND=8), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_real64_3d  !< output variable
+   REAL(wp),     POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_realwp_3d  !< output variable
 
 
    return_value = 0
 
-   WRITE( temp_string, '(": write variable ",I6," into file ",I6)' ) var_id, file_id
+   WRITE( temp_string, '(": write variable ",I6," into file ",I6)' ) variable_id, file_id
    CALL internal_message( 'debug', routine_name // TRIM( temp_string ) )
 
@@ -594,127 +588,127 @@
 
    IF ( .NOT. ANY( value_counts == 0 ) )  THEN
-      WRITE( file_id )  var_id
+      WRITE( file_id )  variable_id
       WRITE( file_id )  bounds_start
       WRITE( file_id )  value_counts
       WRITE( file_id )  bounds_origin
       !-- 8bit integer output
-      IF ( PRESENT( var_int8_0d ) )  THEN
-         out_str = 'int8'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_int8_0d
-      ELSEIF ( PRESENT( var_int8_1d ) )  THEN
-         out_str = 'int8'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_int8_1d
-      ELSEIF ( PRESENT( var_int8_2d ) )  THEN
-         out_str = 'int8'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_int8_2d
-      ELSEIF ( PRESENT( var_int8_3d ) )  THEN
-         out_str = 'int8'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_int8_3d
+      IF ( PRESENT( values_int8_0d ) )  THEN
+         output_string = 'int8'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_int8_0d
+      ELSEIF ( PRESENT( values_int8_1d ) )  THEN
+         output_string = 'int8'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_int8_1d
+      ELSEIF ( PRESENT( values_int8_2d ) )  THEN
+         output_string = 'int8'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_int8_2d
+      ELSEIF ( PRESENT( values_int8_3d ) )  THEN
+         output_string = 'int8'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_int8_3d
       !-- 16bit integer output
-      ELSEIF ( PRESENT( var_int16_0d ) )  THEN
-         out_str = 'int16'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_int16_0d
-      ELSEIF ( PRESENT( var_int16_1d ) )  THEN
-         out_str = 'int16'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_int16_1d
-      ELSEIF ( PRESENT( var_int16_2d ) )  THEN
-         out_str = 'int16'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_int16_2d
-      ELSEIF ( PRESENT( var_int16_3d ) )  THEN
-         out_str = 'int16'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_int16_3d
+      ELSEIF ( PRESENT( values_int16_0d ) )  THEN
+         output_string = 'int16'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_int16_0d
+      ELSEIF ( PRESENT( values_int16_1d ) )  THEN
+         output_string = 'int16'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_int16_1d
+      ELSEIF ( PRESENT( values_int16_2d ) )  THEN
+         output_string = 'int16'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_int16_2d
+      ELSEIF ( PRESENT( values_int16_3d ) )  THEN
+         output_string = 'int16'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_int16_3d
       !-- 32bit integer output
-      ELSEIF ( PRESENT( var_int32_0d ) )  THEN
-         out_str = 'int32'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_int32_0d
-      ELSEIF ( PRESENT( var_int32_1d ) )  THEN
-         out_str = 'int32'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_int32_1d
-      ELSEIF ( PRESENT( var_int32_2d ) )  THEN
-         out_str = 'int32'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_int32_2d
-      ELSEIF ( PRESENT( var_int32_3d ) )  THEN
-         out_str = 'int32'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_int32_3d
+      ELSEIF ( PRESENT( values_int32_0d ) )  THEN
+         output_string = 'int32'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_int32_0d
+      ELSEIF ( PRESENT( values_int32_1d ) )  THEN
+         output_string = 'int32'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_int32_1d
+      ELSEIF ( PRESENT( values_int32_2d ) )  THEN
+         output_string = 'int32'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_int32_2d
+      ELSEIF ( PRESENT( values_int32_3d ) )  THEN
+         output_string = 'int32'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_int32_3d
       !-- working-precision integer output
-      ELSEIF ( PRESENT( var_intwp_0d ) )  THEN
-         out_str = 'intwp'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_intwp_0d
-      ELSEIF ( PRESENT( var_intwp_1d ) )  THEN
-         out_str = 'intwp'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_intwp_1d
-      ELSEIF ( PRESENT( var_intwp_2d ) )  THEN
-         out_str = 'intwp'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_intwp_2d
-      ELSEIF ( PRESENT( var_intwp_3d ) )  THEN
-         out_str = 'intwp'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_intwp_3d
+      ELSEIF ( PRESENT( values_intwp_0d ) )  THEN
+         output_string = 'intwp'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_intwp_0d
+      ELSEIF ( PRESENT( values_intwp_1d ) )  THEN
+         output_string = 'intwp'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_intwp_1d
+      ELSEIF ( PRESENT( values_intwp_2d ) )  THEN
+         output_string = 'intwp'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_intwp_2d
+      ELSEIF ( PRESENT( values_intwp_3d ) )  THEN
+         output_string = 'intwp'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_intwp_3d
       !-- 32bit real output
-      ELSEIF ( PRESENT( var_real32_0d ) )  THEN
-         out_str = 'real32'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_real32_0d
-      ELSEIF ( PRESENT( var_real32_1d ) )  THEN
-         out_str = 'real32'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_real32_1d
-      ELSEIF ( PRESENT( var_real32_2d ) )  THEN
-         out_str = 'real32'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_real32_2d
-      ELSEIF ( PRESENT( var_real32_3d ) )  THEN
-         out_str = 'real32'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_real32_3d
+      ELSEIF ( PRESENT( values_real32_0d ) )  THEN
+         output_string = 'real32'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_real32_0d
+      ELSEIF ( PRESENT( values_real32_1d ) )  THEN
+         output_string = 'real32'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_real32_1d
+      ELSEIF ( PRESENT( values_real32_2d ) )  THEN
+         output_string = 'real32'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_real32_2d
+      ELSEIF ( PRESENT( values_real32_3d ) )  THEN
+         output_string = 'real32'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_real32_3d
       !-- 64bit real output
-      ELSEIF ( PRESENT( var_real64_0d ) )  THEN
-         out_str = 'real64'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_real64_0d
-      ELSEIF ( PRESENT( var_real64_1d ) )  THEN
-         out_str = 'real64'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_real64_1d
-      ELSEIF ( PRESENT( var_real64_2d ) )  THEN
-         out_str = 'real64'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_real64_2d
-      ELSEIF ( PRESENT( var_real64_3d ) )  THEN
-         out_str = 'real64'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_real64_3d
+      ELSEIF ( PRESENT( values_real64_0d ) )  THEN
+         output_string = 'real64'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_real64_0d
+      ELSEIF ( PRESENT( values_real64_1d ) )  THEN
+         output_string = 'real64'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_real64_1d
+      ELSEIF ( PRESENT( values_real64_2d ) )  THEN
+         output_string = 'real64'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_real64_2d
+      ELSEIF ( PRESENT( values_real64_3d ) )  THEN
+         output_string = 'real64'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_real64_3d
       !-- working-precision real output
-      ELSEIF ( PRESENT( var_realwp_0d ) )  THEN
-         out_str = 'realwp'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_realwp_0d
-      ELSEIF ( PRESENT( var_realwp_1d ) )  THEN
-         out_str = 'realwp'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_realwp_1d
-      ELSEIF ( PRESENT( var_realwp_2d ) )  THEN
-         out_str = 'realwp'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_realwp_2d
-      ELSEIF ( PRESENT( var_realwp_3d ) )  THEN
-         out_str = 'realwp'
-         WRITE( file_id )  out_str
-         WRITE( file_id )  var_realwp_3d
+      ELSEIF ( PRESENT( values_realwp_0d ) )  THEN
+         output_string = 'realwp'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_realwp_0d
+      ELSEIF ( PRESENT( values_realwp_1d ) )  THEN
+         output_string = 'realwp'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_realwp_1d
+      ELSEIF ( PRESENT( values_realwp_2d ) )  THEN
+         output_string = 'realwp'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_realwp_2d
+      ELSEIF ( PRESENT( values_realwp_3d ) )  THEN
+         output_string = 'realwp'
+         WRITE( file_id )  output_string
+         WRITE( file_id )  values_realwp_3d
       ELSE
          return_value = 1
@@ -733,16 +727,16 @@
 SUBROUTINE binary_finalize( file_id, return_value )
 
-   CHARACTER(LEN=charlen) ::  out_str  !< output string
+   CHARACTER(LEN=charlen) ::  output_string  !< output string
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'binary_finalize'  !< name of this routine
 
-   INTEGER(iwp), INTENT(IN)  ::  file_id       !< file ID
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
+   INTEGER, INTENT(IN)  ::  file_id       !< file ID
+   INTEGER, INTENT(OUT) ::  return_value  !< return value
 
 
    IF ( config_file_open )  THEN
 
-      out_str = '*** end config file ***'
-      WRITE( config_file_unit )  out_str
+      output_string = '*** end config file ***'
+      WRITE( config_file_unit )  output_string
 
       CLOSE( config_file_unit, IOSTAT=return_value )
@@ -762,13 +756,12 @@
    IF ( return_value == 0 )  THEN
 
-      WRITE(temp_string,*) file_id
+      WRITE( temp_string, * ) file_id
       CALL internal_message( 'debug', routine_name // &
-                                      ': close file (file_id=' // TRIM( temp_string ) // ')' )
+                             ': close file (file_id=' // TRIM( temp_string ) // ')' )
 
       CLOSE( file_id, IOSTAT=return_value )
       IF ( return_value /= 0 )  THEN
-         WRITE(temp_string,*) file_id
-         CALL internal_message( 'error',        &
-                                routine_name // &
+         WRITE( temp_string, * ) file_id
+         CALL internal_message( 'error', routine_name // &
                                 ': cannot close file (file_id=' // TRIM( temp_string ) // ')' )
       ENDIF
@@ -778,5 +771,4 @@
 END SUBROUTINE binary_finalize
 
-
 !--------------------------------------------------------------------------------------------------!
 ! Description:
@@ -809,13 +801,14 @@
 !> Return the last created error message.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE binary_get_error_message( error_message )
-
-   CHARACTER(LEN=800), INTENT(OUT) ::  error_message  !< return error message to main program
-
-
-   error_message = internal_error_message
-
-END SUBROUTINE binary_get_error_message
-
+FUNCTION binary_get_error_message() RESULT( error_message )
+
+   CHARACTER(LEN=800) ::  error_message  !< return error message to main program
+
+
+   error_message = TRIM( internal_error_message )
+   
+   internal_error_message = ''
+
+END FUNCTION binary_get_error_message
 
 END MODULE data_output_binary_module
Index: /palm/trunk/SOURCE/data_output_module.f90
===================================================================
--- /palm/trunk/SOURCE/data_output_module.f90	(revision 4140)
+++ /palm/trunk/SOURCE/data_output_module.f90	(revision 4141)
@@ -38,10 +38,24 @@
 !> Data-output module to handle output of variables into output files.
 !>
-!> The module first creates an interal database containing all meta data of all
-!> output quantities. Output files are then inititialized and prepared for
-!> storing data, which are finally written to file.
+!> The module first creates an interal database containing all meta data of all output quantities.
+!> After defining all meta data, the output files are initialized and prepared for writing. When
+!> writing is finished, files can be finalized and closed.
+!> The order of calls are as follows:
+!>   1. Initialize the module via
+!>      'dom_init'
+!>   2. Define output files via (multiple calls of)
+!>      'dom_def_file', 'dom_def_att', 'dom_def_dim', 'dom_def_var'
+!>   3. Leave definition stage via
+!>      'dom_def_end'
+!>   4. Write output data into file via
+!>      'dom_write_var'
+!>   5. Finalize the output via
+!>      'dom_finalize_output'
+!> If any routine exits with a non-zero return value, the error message of the last encountered
+!> error can be fetched via 'dom_get_error_message'.
+!> For debugging purposes, the content of the database can be written to the debug output via
+!> 'dom_database_debug_output'.
 !>
 !> @todo Convert variable if type of given values do not fit specified type.
-!> @todo Remove iwp from index (and similar) variables.
 !--------------------------------------------------------------------------------------------------!
 MODULE data_output_module
@@ -52,5 +66,5 @@
       ONLY: netcdf4_init_dimension, &
             netcdf4_get_error_message, &
-            netcdf4_init_end, &
+            netcdf4_stop_file_header_definition, &
             netcdf4_init_module, &
             netcdf4_init_variable, &
@@ -64,5 +78,5 @@
             binary_get_error_message, &
             binary_init_dimension, &
-            binary_init_end, &
+            binary_stop_file_header_definition, &
             binary_init_module, &
             binary_init_variable, &
@@ -73,5 +87,6 @@
    IMPLICIT NONE
 
-   INTEGER(iwp), PARAMETER ::  charlen = 100_iwp  !< maximum length of character variables
+   INTEGER, PARAMETER ::  charlen = 100  !< maximum length of character variables
+   INTEGER, PARAMETER ::  no_id = -1     !< default ID if no ID was assigned
 
    TYPE attribute_type
@@ -87,23 +102,23 @@
 
    TYPE variable_type
-      CHARACTER(LEN=charlen) ::  data_type = ''       !< data type
-      CHARACTER(LEN=charlen) ::  name                 !< variable name
-      INTEGER(iwp)           ::  id = -1              !< id within file
-      LOGICAL                ::  is_global = .FALSE.  !< true if global variable
-      CHARACTER(LEN=charlen), DIMENSION(:), ALLOCATABLE ::  dimension_names  !< list of dimension names
-      INTEGER(iwp),           DIMENSION(:), ALLOCATABLE ::  dimension_ids    !< list of dimension ids
-      TYPE(attribute_type),   DIMENSION(:), ALLOCATABLE ::  attributes       !< list of attributes
+      CHARACTER(LEN=charlen)                            ::  data_type = ''       !< data type
+      CHARACTER(LEN=charlen)                            ::  name                 !< variable name
+      INTEGER                                           ::  id = no_id           !< id within file
+      LOGICAL                                           ::  is_global = .FALSE.  !< true if global variable
+      CHARACTER(LEN=charlen), DIMENSION(:), ALLOCATABLE ::  dimension_names      !< list of dimension names used by variable
+      INTEGER,                DIMENSION(:), ALLOCATABLE ::  dimension_ids        !< list of dimension ids used by variable
+      TYPE(attribute_type),   DIMENSION(:), ALLOCATABLE ::  attributes           !< list of attributes
    END TYPE variable_type
 
    TYPE dimension_type
-      CHARACTER(LEN=charlen) ::  data_type = ''       !< data type
-      CHARACTER(LEN=charlen) ::  name                 !< dimension name
-      INTEGER(iwp)           ::  id = -1              !< dimension id within file
-      INTEGER(iwp)           ::  length               !< length of dimension
-      INTEGER(iwp)           ::  length_mask          !< length of masked dimension
-      INTEGER(iwp)           ::  var_id = -1          !< associated variable id within file
-      LOGICAL                ::  is_masked = .FALSE.  !< true if masked
-      INTEGER(iwp),    DIMENSION(2)              ::  bounds                !< lower and upper bound of dimension
-      INTEGER(iwp),    DIMENSION(:), ALLOCATABLE ::  masked_indices        !< list of masked indices of dimension
+      CHARACTER(LEN=charlen)                     ::  data_type = ''        !< data type
+      CHARACTER(LEN=charlen)                     ::  name                  !< dimension name
+      INTEGER                                    ::  id = no_id            !< dimension id within file
+      INTEGER                                    ::  length                !< length of dimension
+      INTEGER                                    ::  length_mask           !< length of masked dimension
+      INTEGER                                    ::  variable_id = no_id   !< associated variable id within file
+      LOGICAL                                    ::  is_masked = .FALSE.   !< true if masked
+      INTEGER,         DIMENSION(2)              ::  bounds                !< lower and upper bound of dimension
+      INTEGER,         DIMENSION(:), ALLOCATABLE ::  masked_indices        !< list of masked indices of dimension
       INTEGER(KIND=1), DIMENSION(:), ALLOCATABLE ::  masked_values_int8    !< masked dimension values if 16bit integer
       INTEGER(KIND=2), DIMENSION(:), ALLOCATABLE ::  masked_values_int16   !< masked dimension values if 16bit integer
@@ -125,26 +140,22 @@
 
    TYPE file_type
-      CHARACTER(LEN=charlen) ::  format = ''        !< file format
-      CHARACTER(LEN=charlen) ::  name = ''          !< file name
-      INTEGER(iwp)           ::  id = -1            !< id of file
-      LOGICAL                ::  is_init = .FALSE.  !< true if initialized
-      TYPE(attribute_type), DIMENSION(:), ALLOCATABLE ::  attributes  !< list of attributes
-      TYPE(dimension_type), DIMENSION(:), ALLOCATABLE ::  dimensions  !< list of dimensions
-      TYPE(variable_type),  DIMENSION(:), ALLOCATABLE ::  variables   !< list of variables
+      CHARACTER(LEN=charlen)                          ::  format = ''        !< file format
+      CHARACTER(LEN=charlen)                          ::  name = ''          !< file name
+      INTEGER                                         ::  id = no_id         !< id of file
+      LOGICAL                                         ::  is_init = .FALSE.  !< true if initialized
+      TYPE(attribute_type), DIMENSION(:), ALLOCATABLE ::  attributes         !< list of attributes
+      TYPE(dimension_type), DIMENSION(:), ALLOCATABLE ::  dimensions         !< list of dimensions
+      TYPE(variable_type),  DIMENSION(:), ALLOCATABLE ::  variables          !< list of variables
    END TYPE file_type
 
 
-   CHARACTER(LEN=charlen) ::  output_file_format = 'binary'  !< file format (namelist parameter)
-   CHARACTER(LEN=charlen) ::  output_file_suffix = ''        !< file suffix added to each file name
-
-   CHARACTER(LEN=800) ::  internal_error_message = '' !< string containing the last error message
-   CHARACTER(LEN=800) ::  temp_string                 !< dummy string
-
-   INTEGER(iwp) ::  debug_output_unit  !< Fortran Unit Number of the debug-output file
-   INTEGER      ::  nf = 0             !< number of files
-   INTEGER      ::  master_rank = 0    !< master rank for tasks to be executed by single PE only
-   INTEGER      ::  output_group_comm  !< MPI communicator addressing all MPI ranks which participate in output
-
-   INTEGER(iwp), PARAMETER ::  no_var_id = -1  !< value of var_id if no variable is selected
+   CHARACTER(LEN=charlen) ::  output_file_suffix = ''      !< file suffix added to each file name
+   CHARACTER(LEN=800)     ::  internal_error_message = ''  !< string containing the last error message
+   CHARACTER(LEN=800)     ::  temp_string                  !< dummy string
+
+   INTEGER ::  debug_output_unit  !< Fortran Unit Number of the debug-output file
+   INTEGER ::  nfiles = 0         !< number of files
+   INTEGER ::  master_rank = 0    !< master rank for tasks to be executed by single PE only
+   INTEGER ::  output_group_comm  !< MPI communicator addressing all MPI ranks which participate in output
 
    LOGICAL ::  print_debug_output = .FALSE.  !< if true, debug output is printed
@@ -187,7 +198,7 @@
 
    !> Prepare for output: evaluate database and create files
-   INTERFACE dom_start_output
-      MODULE PROCEDURE dom_start_output
-   END INTERFACE dom_start_output
+   INTERFACE dom_def_end
+      MODULE PROCEDURE dom_def_end
+   END INTERFACE dom_def_end
 
    !> Write variables to file
@@ -206,15 +217,20 @@
    END INTERFACE dom_get_error_message
 
+   !> Write database to debug output
+   INTERFACE dom_database_debug_output
+      MODULE PROCEDURE dom_database_debug_output
+   END INTERFACE dom_database_debug_output
+
    PUBLIC &
-      dom_database_debug_output, &
+      dom_init, &
+      dom_def_file, &
+      dom_def_dim, &
+      dom_def_var, &
       dom_def_att, &
-      dom_def_dim, &
-      dom_def_file, &
-      dom_def_var, &
+      dom_def_end, &
+      dom_write_var, &
       dom_finalize_output, &
       dom_get_error_message, &
-      dom_init, &
-      dom_start_output, &
-      dom_write_var
+      dom_database_debug_output
 
 CONTAINS
@@ -224,5 +240,10 @@
 ! Description:
 ! ------------
-!> Initialize data-output module
+!> Initialize data-output module.
+!> Provide some general information of the main program.
+!> The optional argument 'file_suffix_of_output_group' defines a file suffix which is added to all
+!> output files. If multiple output groups (groups of MPI ranks, defined by
+!> 'mpi_comm_of_output_group') exist, a unique file suffix must be given for each group. This
+!> prevents that multiple groups try to open and write to the same output file.
 !--------------------------------------------------------------------------------------------------!
 SUBROUTINE dom_init( file_suffix_of_output_group, mpi_comm_of_output_group, master_output_rank, &
@@ -238,5 +259,5 @@
    INTEGER, INTENT(IN)           ::  program_debug_output_unit  !< file unit number for debug output
 
-   LOGICAL, INTENT(IN) ::  debug_output  !< if true, debug output is printed
+   LOGICAL, INTENT(IN)           ::  debug_output               !< if true, debug output is printed
 
 
@@ -250,411 +271,10 @@
 
    CALL binary_init_module( output_file_suffix, output_group_comm, master_rank, &
-                            debug_output_unit, debug_output, no_var_id )
+                            debug_output_unit, debug_output, no_id )
 
    CALL netcdf4_init_module( output_file_suffix, output_group_comm, master_rank, &
-                            debug_output_unit, debug_output, no_var_id )
+                            debug_output_unit, debug_output, no_id )
 
 END SUBROUTINE dom_init
-
-!--------------------------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Debugging output. Print contents of output database to debug_output_unit.
-!--------------------------------------------------------------------------------------------------!
-SUBROUTINE dom_database_debug_output
-
-   CHARACTER(LEN=*), PARAMETER ::  separation_string = '---'                   !< string separating blocks in output
-   CHARACTER(LEN=50)           ::  format1                                     !< format for write statements
-   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_database_debug_output'  !< name of this routine
-
-   INTEGER            ::  f                       !< loop index
-   INTEGER, PARAMETER ::  indent_depth = 3        !< space per indentation
-   INTEGER            ::  indent_level            !< indentation level
-   INTEGER, PARAMETER ::  max_keyname_length = 6  !< length of longest key name
-   INTEGER            ::  natt                    !< number of attributes
-   INTEGER            ::  ndim                    !< number of dimensions
-   INTEGER            ::  nvar                    !< number of variables
-
-
-   CALL internal_message( 'debug', routine_name // ': write data base to debug output' )
-
-   WRITE( debug_output_unit, '(A)' ) 'DOM data base:'
-   WRITE( debug_output_unit, '(A)' ) REPEAT( separation_string // ' ', 4 )
-
-   IF ( .NOT. ALLOCATED( files ) .OR. nf == 0 )  THEN
-
-      WRITE( debug_output_unit, '(A)' ) 'database is empty'
-
-   ELSE
-
-      indent_level = 1
-      WRITE( format1, '(A,I3,A,I3,A)' ) '(T', indent_level * indent_depth + 1, ',A,T',        &
-                                        indent_level * indent_depth + 1 + max_keyname_length, &
-                                        ',(": ")'
-
-      DO  f = 1, nf
-
-         natt = 0
-         ndim = 0
-         nvar = 0
-         IF ( ALLOCATED( files(f)%attributes ) ) natt = SIZE( files(f)%attributes )
-         IF ( ALLOCATED( files(f)%dimensions ) ) ndim = SIZE( files(f)%dimensions )
-         IF ( ALLOCATED( files(f)%variables  ) ) nvar = SIZE( files(f)%variables  )
-
-         WRITE( debug_output_unit, '(A)' ) 'file:'
-         WRITE( debug_output_unit, TRIM( format1 ) // ',A)' ) 'name', TRIM( files(f)%name )
-         WRITE( debug_output_unit, TRIM( format1 ) // ',A)' ) 'format', TRIM( files(f)%format )
-         WRITE( debug_output_unit, TRIM( format1 ) // ',I7)' ) 'id', files(f)%id
-         WRITE( debug_output_unit, TRIM( format1 ) // ',L1)' ) 'is init', files(f)%is_init
-         WRITE( debug_output_unit, TRIM( format1 ) // ',I7)' ) '#atts', natt
-         WRITE( debug_output_unit, TRIM( format1 ) // ',I7)' ) '#dims', ndim
-         WRITE( debug_output_unit, TRIM( format1 ) // ',I7)' ) '#vars', nvar
-
-         IF ( natt /= 0 )  CALL print_attributes( indent_level, files(f)%attributes )
-         IF ( ndim /= 0 )  CALL print_dimensions( indent_level, files(f)%dimensions )
-         IF ( nvar /= 0 )  CALL print_variables( indent_level, files(f)%variables )
-
-         WRITE( debug_output_unit, '(/A/)' ) REPEAT( separation_string // ' ', 4 )
-
-      ENDDO
-
-   ENDIF
-
-   CONTAINS
-
-      !--------------------------------------------------------------------------------------------!
-      ! Description:
-      ! ------------
-      !> Print list of attributes.
-      !--------------------------------------------------------------------------------------------!
-      SUBROUTINE print_attributes( indent_level, attributes )
-
-         CHARACTER(LEN=50) ::  format1  !< format for write statements
-         CHARACTER(LEN=50) ::  format2  !< format for write statements
-
-         INTEGER             ::  i                       !< loop index
-         INTEGER, INTENT(IN) ::  indent_level            !< indentation level
-         INTEGER, PARAMETER  ::  max_keyname_length = 6  !< length of longest key name
-         INTEGER             ::  nelement                !< number of elements to print
-
-         TYPE(attribute_type), DIMENSION(:), INTENT(IN) ::  attributes  !< list of attributes
-
-
-         WRITE( format1, '(A,I3,A)' ) '(T', indent_level * indent_depth + 1, ',A'
-         WRITE( format2, '(A,I3,A,I3,A)' ) &
-            '(T', ( indent_level + 1 ) * indent_depth + 1, ',A,T', &
-            ( indent_level + 1 ) * indent_depth + 1 + max_keyname_length, ',(": ")'
-
-         WRITE( debug_output_unit, TRIM( format1 ) // ',A)' ) REPEAT( separation_string // ' ', 4 )
-         WRITE( debug_output_unit, TRIM( format1 ) // ')' ) 'attributes:'
-
-         nelement = SIZE( attributes )
-         DO  i = 1, nelement
-            WRITE( debug_output_unit, TRIM( format2 ) // ',A)' ) &
-               'name', TRIM( attributes(i)%name )
-            WRITE( debug_output_unit, TRIM( format2 ) // ',A)' ) &
-               'type', TRIM( attributes(i)%data_type )
-
-            IF ( TRIM( attributes(i)%data_type ) == 'char' )  THEN
-               WRITE( debug_output_unit, TRIM( format2 ) // ',A)' ) &
-                  'value', TRIM( attributes(i)%value_char )
-            ELSEIF ( TRIM( attributes(i)%data_type ) == 'int8' )  THEN
-               WRITE( debug_output_unit, TRIM( format2 ) // ',I4)' ) &
-                  'value', attributes(i)%value_int8
-            ELSEIF ( TRIM( attributes(i)%data_type ) == 'int16' )  THEN
-               WRITE( debug_output_unit, TRIM( format2 ) // ',I6)' ) &
-                  'value', attributes(i)%value_int16
-            ELSEIF ( TRIM( attributes(i)%data_type ) == 'int32' )  THEN
-               WRITE( debug_output_unit, TRIM( format2 ) // ',I11)' ) &
-                  'value', attributes(i)%value_int32
-            ELSEIF ( TRIM( attributes(i)%data_type ) == 'real32' )  THEN
-               WRITE( debug_output_unit, TRIM( format2 ) // ',E14.7)' ) &
-                  'value', attributes(i)%value_real32
-            ELSEIF (  TRIM(attributes(i)%data_type) == 'real64' )  THEN
-               WRITE( debug_output_unit, TRIM( format2 ) // ',E22.15)' ) &
-                  'value', attributes(i)%value_real64
-            ENDIF
-            IF ( i < nelement )  &
-               WRITE( debug_output_unit, TRIM( format1 ) // ')' ) separation_string
-         ENDDO
-
-      END SUBROUTINE print_attributes
-
-      !--------------------------------------------------------------------------------------------!
-      ! Description:
-      ! ------------
-      !> Print list of dimensions.
-      !--------------------------------------------------------------------------------------------!
-      SUBROUTINE print_dimensions( indent_level, dimensions )
-
-         CHARACTER(LEN=50) ::  format1  !< format for write statements
-         CHARACTER(LEN=50) ::  format2  !< format for write statements
-
-         INTEGER             ::  i                        !< loop index
-         INTEGER, INTENT(IN) ::  indent_level             !< indentation level
-         INTEGER             ::  j                        !< loop index
-         INTEGER, PARAMETER  ::  max_keyname_length = 15  !< length of longest key name
-         INTEGER             ::  nelement                 !< number of elements to print
-
-         LOGICAL ::  is_masked  !< true if dimension is masked
-
-         TYPE(dimension_type), DIMENSION(:), INTENT(IN) ::  dimensions  !< list of dimensions
-
-
-         WRITE( format1, '(A,I3,A)' ) '(T', indent_level * indent_depth + 1, ',A'
-         WRITE( format2, '(A,I3,A,I3,A)' ) &
-            '(T', ( indent_level + 1 ) * indent_depth + 1, ',A,T', &
-            ( indent_level + 1 ) * indent_depth + 1 + max_keyname_length, ',(": ")'
-
-         WRITE( debug_output_unit, TRIM( format1 ) // ',A)' ) REPEAT( separation_string // ' ', 4 )
-         WRITE( debug_output_unit, TRIM( format1 ) // ')' ) 'dimensions:'
-
-         nelement = SIZE( dimensions )
-         DO  i = 1, nelement
-            is_masked = dimensions(i)%is_masked
-
-            !-- Print general information
-            WRITE( debug_output_unit, TRIM( format2 ) // ',A)' ) &
-               'name', TRIM( dimensions(i)%name )
-            WRITE( debug_output_unit, TRIM( format2 ) // ',A)' ) &
-               'type', TRIM( dimensions(i)%data_type )
-            WRITE( debug_output_unit, TRIM( format2 ) // ',I7)' ) &
-               'id', dimensions(i)%id
-            WRITE( debug_output_unit, TRIM( format2 ) // ',I7)' ) &
-               'length', dimensions(i)%length
-            WRITE( debug_output_unit, TRIM( format2 ) // ',I7,A,I7)' ) &
-               'bounds', dimensions(i)%bounds(1), ',', dimensions(i)%bounds(2)
-            WRITE( debug_output_unit, TRIM( format2 ) // ',L1)' ) &
-               'is masked', dimensions(i)%is_masked
-
-            !-- Print information about mask
-            IF ( is_masked )  THEN
-               WRITE( debug_output_unit, TRIM( format2 ) // ',I7)' ) &
-                  'masked length', dimensions(i)%length_mask
-
-               WRITE( debug_output_unit, TRIM( format2 ) // ',L1)', ADVANCE='no' ) &
-                  'mask', dimensions(i)%mask(dimensions(i)%bounds(1))
-               DO  j = dimensions(i)%bounds(1)+1, dimensions(i)%bounds(2)
-                  WRITE( debug_output_unit, '(A,L1)', ADVANCE='no' ) ',', dimensions(i)%mask(j)
-               ENDDO
-               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-
-               WRITE( debug_output_unit, TRIM( format2 ) // ',I6)', ADVANCE='no' ) &
-                  'masked indices', dimensions(i)%masked_indices(0)
-               DO  j = 1, dimensions(i)%length_mask-1
-                  WRITE( debug_output_unit, '(A,I6)', ADVANCE='no' ) &
-                     ',', dimensions(i)%masked_indices(j)
-               ENDDO
-               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-            ENDIF
-
-            !-- Print saved values
-            IF ( ALLOCATED( dimensions(i)%values_int8 ) )  THEN
-
-               WRITE( debug_output_unit, TRIM( format2 ) // ',I4)', ADVANCE='no' ) &
-                  'values', dimensions(i)%values_int8(dimensions(i)%bounds(1))
-               DO  j = dimensions(i)%bounds(1)+1, dimensions(i)%bounds(2)
-                  WRITE( debug_output_unit, '(A,I4)', ADVANCE='no' ) &
-                     ',', dimensions(i)%values_int8(j)
-               ENDDO
-               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-               IF ( is_masked )  THEN
-                  WRITE( debug_output_unit, TRIM( format2 ) // ',I4)', ADVANCE='no' ) &
-                     'masked values', dimensions(i)%masked_values_int8(0)
-                  DO  j = 1, dimensions(i)%length_mask-1
-                     WRITE( debug_output_unit, '(A,I4)', ADVANCE='no' ) &
-                        ',', dimensions(i)%masked_values_int8(j)
-                  ENDDO
-                  WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-               ENDIF
-
-            ELSEIF ( ALLOCATED( dimensions(i)%values_int16 ) )  THEN
-
-               WRITE( debug_output_unit, TRIM( format2 ) // ',I6)', ADVANCE='no' ) &
-                  'values', dimensions(i)%values_int16(dimensions(i)%bounds(1))
-               DO  j = dimensions(i)%bounds(1)+1, dimensions(i)%bounds(2)
-                  WRITE( debug_output_unit, '(A,I6)', ADVANCE='no' ) &
-                     ',', dimensions(i)%values_int16(j)
-               ENDDO
-               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-               IF ( is_masked )  THEN
-                  WRITE( debug_output_unit, TRIM( format2 ) // ',I6)', ADVANCE='no' ) &
-                     'masked values', dimensions(i)%masked_values_int16(0)
-                  DO  j = 1, dimensions(i)%length_mask-1
-                     WRITE( debug_output_unit, '(A,I6)', ADVANCE='no' ) &
-                        ',', dimensions(i)%masked_values_int16(j)
-                  ENDDO
-                  WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-               ENDIF
-
-            ELSEIF ( ALLOCATED( dimensions(i)%values_int32 ) )  THEN
-
-               WRITE( debug_output_unit, TRIM( format2 ) // ',I11)', ADVANCE='no' ) &
-                  'values', dimensions(i)%values_int32(dimensions(i)%bounds(1))
-               DO  j = dimensions(i)%bounds(1)+1, dimensions(i)%bounds(2)
-                  WRITE( debug_output_unit, '(A,I11)', ADVANCE='no' ) &
-                     ',', dimensions(i)%values_int32(j)
-               ENDDO
-               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-               IF ( is_masked )  THEN
-                  WRITE( debug_output_unit, TRIM( format2 ) // ',I11)', ADVANCE='no' ) &
-                     'masked values', dimensions(i)%masked_values_int32(0)
-                  DO  j = 1, dimensions(i)%length_mask-1
-                     WRITE( debug_output_unit, '(A,I11)', ADVANCE='no' ) &
-                        ',', dimensions(i)%masked_values_int32(j)
-                  ENDDO
-                  WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-               ENDIF
-
-            ELSEIF ( ALLOCATED( dimensions(i)%values_intwp ) )  THEN
-
-               WRITE( debug_output_unit, TRIM( format2 ) // ',I11)', ADVANCE='no' ) &
-                  'values', dimensions(i)%values_intwp(dimensions(i)%bounds(1))
-               DO  j = dimensions(i)%bounds(1)+1, dimensions(i)%bounds(2)
-                  WRITE( debug_output_unit, '(A,I11)', ADVANCE='no' ) &
-                     ',', dimensions(i)%values_intwp(j)
-               ENDDO
-               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-               IF ( is_masked )  THEN
-                  WRITE( debug_output_unit, TRIM( format2 ) // ',I11)', ADVANCE='no' ) &
-                     'masked values', dimensions(i)%masked_values_intwp(0)
-                  DO  j = 1, dimensions(i)%length_mask-1
-                     WRITE( debug_output_unit, '(A,I11)', ADVANCE='no' ) &
-                        ',', dimensions(i)%masked_values_intwp(j)
-                  ENDDO
-                  WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-               ENDIF
-
-            ELSEIF ( ALLOCATED( dimensions(i)%values_real32 ) )  THEN
-
-               WRITE( debug_output_unit, TRIM( format2 ) // ',E14.7)', ADVANCE='no' ) &
-                  'values', dimensions(i)%values_real32(dimensions(i)%bounds(1))
-               DO  j = dimensions(i)%bounds(1)+1, dimensions(i)%bounds(2)
-                  WRITE( debug_output_unit, '(A,E14.7)', ADVANCE='no' ) &
-                     ',', dimensions(i)%values_real32(j)
-               ENDDO
-               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-               IF ( is_masked )  THEN
-                  WRITE( debug_output_unit, TRIM( format2 ) // ',E14.7)', ADVANCE='no' ) &
-                     'masked values', dimensions(i)%masked_values_real32(0)
-                  DO  j = 1, dimensions(i)%length_mask-1
-                     WRITE( debug_output_unit, '(A,E14.7)', ADVANCE='no' ) &
-                        ',', dimensions(i)%masked_values_real32(j)
-                  ENDDO
-                  WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-               ENDIF
-
-            ELSEIF ( ALLOCATED( dimensions(i)%values_real64 ) )  THEN
-
-               WRITE( debug_output_unit, TRIM( format2 ) // ',E22.15)', ADVANCE='no' ) &
-                  'values', dimensions(i)%values_real64(dimensions(i)%bounds(1))
-               DO  j = dimensions(i)%bounds(1)+1, dimensions(i)%bounds(2)
-                  WRITE( debug_output_unit, '(A,E22.15)', ADVANCE='no' ) &
-                     ',', dimensions(i)%values_real64(j)
-               ENDDO
-               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-               IF ( is_masked )  THEN
-                  WRITE( debug_output_unit, TRIM( format2 ) // ',E22.15)', ADVANCE='no' ) &
-                     'masked values', dimensions(i)%masked_values_real64(0)
-                  DO  j = 1, dimensions(i)%length_mask-1
-                     WRITE( debug_output_unit, '(A,E22.15)', ADVANCE='no' ) &
-                        ',', dimensions(i)%masked_values_real64(j)
-                  ENDDO
-                  WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-               ENDIF
-
-            ELSEIF ( ALLOCATED( dimensions(i)%values_realwp ) )  THEN
-
-               WRITE( debug_output_unit, TRIM( format2 ) // ',E22.15)', ADVANCE='no' ) &
-                  'values', dimensions(i)%values_realwp(dimensions(i)%bounds(1))
-               DO  j = dimensions(i)%bounds(1)+1, dimensions(i)%bounds(2)
-                  WRITE( debug_output_unit, '(A,E22.15)', ADVANCE='no' ) &
-                     ',', dimensions(i)%values_realwp(j)
-               ENDDO
-               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-               IF ( is_masked )  THEN
-                  WRITE( debug_output_unit, TRIM( format2 ) // ',E22.15)', ADVANCE='no' ) &
-                     'masked values', dimensions(i)%masked_values_realwp(0)
-                  DO  j = 1, dimensions(i)%length_mask-1
-                     WRITE( debug_output_unit, '(A,E22.15)', ADVANCE='no' ) &
-                        ',', dimensions(i)%masked_values_realwp(j)
-                  ENDDO
-                  WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-               ENDIF
-
-            ENDIF
-
-            IF ( ALLOCATED( dimensions(i)%attributes ) )  &
-               CALL print_attributes( indent_level+1, dimensions(i)%attributes )
-
-            IF ( i < nelement )  &
-               WRITE( debug_output_unit, TRIM( format1 ) // ')' ) separation_string
-         ENDDO
-
-      END SUBROUTINE print_dimensions
-
-      !--------------------------------------------------------------------------------------------!
-      ! Description:
-      ! ------------
-      !> Print list of variables.
-      !--------------------------------------------------------------------------------------------!
-      SUBROUTINE print_variables( indent_level, variables )
-
-         CHARACTER(LEN=50) ::  format1  !< format for write statements
-         CHARACTER(LEN=50) ::  format2  !< format for write statements
-
-         INTEGER             ::  i                        !< loop index
-         INTEGER, INTENT(IN) ::  indent_level             !< indentation level
-         INTEGER             ::  j                        !< loop index
-         INTEGER, PARAMETER  ::  max_keyname_length = 16  !< length of longest key name
-         INTEGER             ::  nelement                 !< number of elements to print
-
-         TYPE(variable_type), DIMENSION(:), INTENT(IN) ::  variables  !< list of variables
-
-
-         WRITE( format1, '(A,I3,A)' ) '(T', indent_level * indent_depth + 1, ',A'
-         WRITE( format2, '(A,I3,A,I3,A)' ) &
-            '(T', ( indent_level + 1 ) * indent_depth + 1, ',A,T', &
-            ( indent_level + 1 ) * indent_depth + 1 + max_keyname_length, ',(": ")'
-
-         WRITE( debug_output_unit, TRIM( format1 ) // ',A)' ) REPEAT( separation_string // ' ', 4 )
-         WRITE( debug_output_unit, TRIM( format1 ) // ')' ) 'variables:'
-
-         nelement = SIZE( variables )
-         DO  i = 1, nelement
-            WRITE( debug_output_unit, TRIM( format2 ) // ',A)' ) &
-               'name', TRIM( variables(i)%name )
-            WRITE( debug_output_unit, TRIM( format2 ) // ',A)' ) &
-               'type', TRIM( variables(i)%data_type )
-            WRITE( debug_output_unit, TRIM( format2 ) // ',I7)' ) &
-               'id', variables(i)%id
-            WRITE( debug_output_unit, TRIM( format2 ) // ',L1)' ) &
-               'is global', variables(i)%is_global
-
-            WRITE( debug_output_unit, TRIM( format2 ) // ',A)', ADVANCE='no' ) &
-               'dimension names', TRIM( variables(i)%dimension_names(1) )
-            DO  j = 2, SIZE( variables(i)%dimension_names )
-               WRITE( debug_output_unit, '(A,A)', ADVANCE='no' ) &
-                  ',', TRIM( variables(i)%dimension_names(j) )
-            ENDDO
-            WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-
-            WRITE( debug_output_unit, TRIM( format2 ) // ',I7)', ADVANCE='no' ) &
-               'dimension ids', variables(i)%dimension_ids(1)
-            DO  j = 2, SIZE( variables(i)%dimension_names )
-               WRITE( debug_output_unit, '(A,I7)', ADVANCE='no' ) &
-                  ',', variables(i)%dimension_ids(j)
-            ENDDO
-            WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
-
-            IF ( ALLOCATED( variables(i)%attributes ) )  &
-               CALL print_attributes( indent_level+1, variables(i)%attributes )
-            IF ( i < nelement )  &
-               WRITE( debug_output_unit, TRIM( format1 ) // ')' ) separation_string
-         ENDDO
-
-      END SUBROUTINE print_variables
-
-END SUBROUTINE dom_database_debug_output
 
 !--------------------------------------------------------------------------------------------------!
@@ -662,14 +282,16 @@
 ! ------------
 !> Define output file.
-!--------------------------------------------------------------------------------------------------!
-FUNCTION dom_def_file( filename, format ) RESULT( return_value )
-
-   CHARACTER(LEN=*), INTENT(IN)           ::  filename  !< name of file to be created
-   CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  format    !< format of file to be created
+!> Example call:
+!>   status = dom_def_file( 'my_output_file_name', 'binary' )
+!--------------------------------------------------------------------------------------------------!
+FUNCTION dom_def_file( file_name, file_format ) RESULT( return_value )
+
+   CHARACTER(LEN=*), INTENT(IN) ::  file_name    !< name of file to be created
+   CHARACTER(LEN=*), INTENT(IN) ::  file_format  !< format of file to be created
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_def_file'  !< name of this routine
 
-   INTEGER(iwp) ::  f             !< loop index
-   INTEGER(iwp) ::  return_value  !< return value
+   INTEGER ::  f             !< loop index
+   INTEGER ::  return_value  !< return value
 
    TYPE(file_type), DIMENSION(:), ALLOCATABLE ::  files_tmp  !< temporary file list
@@ -678,21 +300,21 @@
    return_value = 0
 
-   CALL internal_message( 'debug', routine_name // ': define file "' // TRIM( filename ) // '"' )
+   CALL internal_message( 'debug', routine_name // ': define file "' // TRIM( file_name ) // '"' )
 
    !-- Allocate file list or extend it by 1
    IF ( .NOT. ALLOCATED( files ) ) THEN
 
-      nf = 1
-      ALLOCATE( files(nf) )
+      nfiles = 1
+      ALLOCATE( files(nfiles) )
 
    ELSE
 
-      nf = SIZE( files )
+      nfiles = SIZE( files )
       !-- Check if file already exists
-      DO  f = 1, nf
-         IF ( files(f)%name == TRIM( filename ) )  THEN
+      DO  f = 1, nfiles
+         IF ( files(f)%name == TRIM( file_name ) )  THEN
             return_value = 1
-            CALL internal_message( 'error', routine_name // ': file "' // TRIM( filename ) // &
-                                            '" already exists' )
+            CALL internal_message( 'error', routine_name // &
+                    ': file "' // TRIM( file_name ) // '" already exists' )
             EXIT
          ENDIF
@@ -701,10 +323,10 @@
       !-- Extend file list
       IF ( return_value == 0 )  THEN
-         ALLOCATE( files_tmp(nf) )
+         ALLOCATE( files_tmp(nfiles) )
          files_tmp = files
          DEALLOCATE( files )
-         nf = nf + 1
-         ALLOCATE( files(nf) )
-         files(:nf-1) = files_tmp
+         nfiles = nfiles + 1
+         ALLOCATE( files(nfiles) )
+         files(:nfiles-1) = files_tmp
          DEALLOCATE( files_tmp )
       ENDIF
@@ -714,10 +336,6 @@
    !-- Add new file to database
    IF ( return_value == 0 )  THEN
-      files(nf)%name = TRIM( filename )
-      IF ( PRESENT( format ) )  THEN
-         files(nf)%format = TRIM( format )
-      ELSE
-         files(nf)%format = TRIM( output_file_format )
-      ENDIF
+      files(nfiles)%name = TRIM( file_name )
+      files(nfiles)%format = TRIM( file_format )
    ENDIF
 
@@ -727,51 +345,71 @@
 ! Description:
 ! ------------
-!> Define dimension of type integer.
+!> Define dimension.
+!> Dimensions can either be limited (a lower and upper bound is given) or unlimited (only a lower
+!> bound is given). Also, instead of providing all values of the dimension, a single value can be
+!> given which is then used to fill the entire dimension.
+!> An optional mask can be given to mask limited dimensions.
+!> Example call:
+!>   - fixed dimension with 100 entries (values known):
+!>       status = dom_def_dim( file_name='my_output_file_name', dimension_name='my_dimension', &
+!>                             output_type='real32', bounds=(/1,100/), &
+!>                             values_real32=my_dim(1:100), mask=my_dim_mask(1:100) )
+!>   - fixed dimension with 50 entries (values not yet known):
+!>       status = dom_def_dim( file_name='my_output_file_name', dimension_name='my_dimension', &
+!>                             output_type='int32', bounds=(/0,49/), &
+!>                             values_int32=(/fill_value/) )
+!>   - masked dimension with 75 entries:
+!>       status = dom_def_dim( file_name='my_output_file_name', dimension_name='my_dimension', &
+!>                             output_type='real64', bounds=(/101,175/), &
+!>                             values_real64=my_dim(1:75), mask=my_dim_mask(1:75) )
+!>   - unlimited dimension:
+!>       status = dom_def_dim( file_name='my_output_file_name', dimension_name='my_dimension', &
+!>                             output_type='real32', bounds=(/1/), &
+!>                             values_real32=(/fill_value/) )
 !>
 !> @todo Convert given values into selected output_type.
 !--------------------------------------------------------------------------------------------------!
-FUNCTION dom_def_dim( filename, name, output_type, bounds,                   &
+FUNCTION dom_def_dim( file_name, dimension_name, output_type, bounds,        &
                       values_int8, values_int16, values_int32, values_intwp, &
                       values_real32, values_real64, values_realwp,           &
                       mask ) RESULT( return_value )
 
-   CHARACTER(LEN=*), INTENT(IN) ::  filename     !< name of file
-   CHARACTER(LEN=*), INTENT(IN) ::  name         !< name of dimension
-   CHARACTER(LEN=*), INTENT(IN) ::  output_type  !< data type of dimension variable in output file
+   CHARACTER(LEN=*), INTENT(IN) ::  file_name       !< name of file
+   CHARACTER(LEN=*), INTENT(IN) ::  dimension_name  !< name of dimension
+   CHARACTER(LEN=*), INTENT(IN) ::  output_type     !< data type of dimension variable in output file
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_def_dim'  !< name of this routine
 
-   INTEGER(iwp) ::  d             !< loop index
-   INTEGER(iwp) ::  f             !< loop index
-   INTEGER(iwp) ::  i             !< loop index
-   INTEGER(iwp) ::  j             !< loop index
-   INTEGER(iwp) ::  ndim          !< number of dimensions in file
-   INTEGER(iwp) ::  return_value  !< return value
-
-   INTEGER(iwp), DIMENSION(:), INTENT(IN) ::  bounds  !< lower and upper bound of dimension variable
-
-   INTEGER(KIND=1), DIMENSION(:), INTENT(IN), OPTIONAL ::  values_int8   !< values of dimension
-   INTEGER(KIND=2), DIMENSION(:), INTENT(IN), OPTIONAL ::  values_int16  !< values of dimension
-   INTEGER(KIND=4), DIMENSION(:), INTENT(IN), OPTIONAL ::  values_int32  !< values of dimension
-   INTEGER(iwp),    DIMENSION(:), INTENT(IN), OPTIONAL ::  values_intwp  !< values of dimension
-
-   LOGICAL, DIMENSION(:), INTENT(IN), OPTIONAL ::  mask  !< mask of dimesion
-
-   REAL(KIND=4), DIMENSION(:), INTENT(IN), OPTIONAL ::  values_real32  !< values of dimension
-   REAL(KIND=8), DIMENSION(:), INTENT(IN), OPTIONAL ::  values_real64  !< values of dimension
-   REAL(wp),     DIMENSION(:), INTENT(IN), OPTIONAL ::  values_realwp  !< values of dimension
-
-   TYPE(dimension_type) ::  dimension  !< new dimension
-
-   TYPE(dimension_type), DIMENSION(:), ALLOCATABLE ::  dims_tmp  !< temporary dimension list
+   INTEGER ::  d             !< loop index
+   INTEGER ::  f             !< loop index
+   INTEGER ::  i             !< loop index
+   INTEGER ::  j             !< loop index
+   INTEGER ::  ndims         !< number of dimensions in file
+   INTEGER ::  return_value  !< return value
+
+   INTEGER,         DIMENSION(:), INTENT(IN)           ::  bounds         !< lower and upper bound of dimension variable
+   INTEGER(KIND=1), DIMENSION(:), INTENT(IN), OPTIONAL ::  values_int8    !< values of dimension
+   INTEGER(KIND=2), DIMENSION(:), INTENT(IN), OPTIONAL ::  values_int16   !< values of dimension
+   INTEGER(KIND=4), DIMENSION(:), INTENT(IN), OPTIONAL ::  values_int32   !< values of dimension
+   INTEGER(iwp),    DIMENSION(:), INTENT(IN), OPTIONAL ::  values_intwp   !< values of dimension
+
+   LOGICAL,         DIMENSION(:), INTENT(IN), OPTIONAL ::  mask           !< mask of dimesion
+
+   REAL(KIND=4),    DIMENSION(:), INTENT(IN), OPTIONAL ::  values_real32  !< values of dimension
+   REAL(KIND=8),    DIMENSION(:), INTENT(IN), OPTIONAL ::  values_real64  !< values of dimension
+   REAL(wp),        DIMENSION(:), INTENT(IN), OPTIONAL ::  values_realwp  !< values of dimension
+
+   TYPE(dimension_type)                            ::  dimension       !< new dimension
+   TYPE(dimension_type), DIMENSION(:), ALLOCATABLE ::  dimensions_tmp  !< temporary dimension list
 
 
    return_value = 0
 
-   CALL internal_message( 'debug', routine_name //                  &
-                          ': define dimension "' // TRIM( name ) // &
-                          '" in file "' // TRIM( filename ) // '"' )
-
-   dimension%name      = TRIM( name )
+   CALL internal_message( 'debug', routine_name //                    &
+                          ': define dimension ' //                    &
+                          '(dimension "' // TRIM( dimension_name ) // &
+                          '", file "' // TRIM( file_name ) // '")' )
+
+   dimension%name      = TRIM( dimension_name )
    dimension%data_type = TRIM( output_type )
 
@@ -783,11 +421,12 @@
       !-- Set length to -1 as indicator.
       dimension%bounds(:) = bounds(1)
-      dimension%length    = -1_iwp
+      dimension%length    = -1
 
       IF ( PRESENT( mask ) )  THEN
          return_value = 1
-         CALL internal_message( 'error', routine_name //                              &
-                                         ': unlimited dimension "' // TRIM( name ) // &
-                                         '" in file "' // TRIM( filename ) // '" cannot be masked' )
+         CALL internal_message( 'error', routine_name //                      &
+                                ': unlimited dimensions cannot be masked ' // &
+                                '(dimension "' // TRIM( dimension_name ) //   &
+                                '", file "' // TRIM( file_name ) // '")!' )
       ENDIF
 
@@ -803,5 +442,5 @@
             dimension%values_int8 = values_int8
          ELSEIF ( SIZE( values_int8 ) == 1 )  THEN
-            dimension%values_int8(:) = values_int8
+            dimension%values_int8(:) = values_int8(1)
          ELSE
             return_value = 2
@@ -812,5 +451,5 @@
             dimension%values_int16 = values_int16
          ELSEIF ( SIZE( values_int16 ) == 1 )  THEN
-            dimension%values_int16(:) = values_int16
+            dimension%values_int16(:) = values_int16(1)
          ELSE
             return_value = 2
@@ -821,5 +460,5 @@
             dimension%values_int32 = values_int32
          ELSEIF ( SIZE( values_int32 ) == 1 )  THEN
-            dimension%values_int32(:) = values_int32
+            dimension%values_int32(:) = values_int32(1)
          ELSE
             return_value = 2
@@ -830,5 +469,5 @@
             dimension%values_intwp = values_intwp
          ELSEIF ( SIZE( values_intwp ) == 1 )  THEN
-            dimension%values_intwp(:) = values_intwp
+            dimension%values_intwp(:) = values_intwp(1)
          ELSE
             return_value = 2
@@ -839,5 +478,5 @@
             dimension%values_real32 = values_real32
          ELSEIF ( SIZE( values_real32 ) == 1 )  THEN
-            dimension%values_real32(:) = values_real32
+            dimension%values_real32(:) = values_real32(1)
          ELSE
             return_value = 2
@@ -848,5 +487,5 @@
             dimension%values_real64 = values_real64
          ELSEIF ( SIZE( values_real64 ) == 1 )  THEN
-            dimension%values_real64(:) = values_real64
+            dimension%values_real64(:) = values_real64(1)
          ELSE
             return_value = 2
@@ -857,5 +496,5 @@
             dimension%values_realwp = values_realwp
          ELSEIF ( SIZE( values_realwp ) == 1 )  THEN
-            dimension%values_realwp(:) = values_realwp
+            dimension%values_realwp(:) = values_realwp(1)
          ELSE
             return_value = 2
@@ -863,13 +502,16 @@
       ELSE
          return_value = 1
-         CALL internal_message( 'error', routine_name // ': ' // &
-                                         TRIM( name ) // ': no values given' )
+         CALL internal_message( 'error', routine_name //                    &
+                                ': no values given ' //                     &
+                                '(dimension "' // TRIM( dimension_name ) // &
+                                '", file "' // TRIM( file_name ) // '")!' )
       ENDIF
 
       IF ( return_value == 2 )  THEN
          return_value = 1
-         CALL internal_message( 'error', routine_name //                   &
-                                         ': dimension ' // TRIM( name ) // &
-                                         ': number of values and given bounds do not match' )
+         CALL internal_message( 'error', routine_name //                               &
+                                ': number of values and given bounds do not match ' // &
+                                '(dimension "' // TRIM( dimension_name ) //            &
+                                '", file "' // TRIM( file_name ) // '")!' )
       ENDIF
 
@@ -877,107 +519,118 @@
       IF ( PRESENT( mask )  .AND.  return_value == 0 )  THEN
 
-         dimension%is_masked = .TRUE.
-
          IF ( dimension%length == SIZE( mask ) )  THEN
 
-            dimension%length_mask = COUNT( mask )
-
-            ALLOCATE( dimension%mask(dimension%bounds(1):dimension%bounds(2)) )
-            ALLOCATE( dimension%masked_indices(0:dimension%length_mask-1) )
-
-            dimension%mask = mask
-
-            !-- Save masked positions and masked values
-            IF ( ALLOCATED( dimension%values_int8 ) )  THEN
-
-               ALLOCATE( dimension%masked_values_int8(0:dimension%length_mask-1) )
-               j = 0
-               DO  i = 0, dimension%length-1
-                  IF ( dimension%mask(i) )  THEN
-                     dimension%masked_values_int8(j) = dimension%values_int8(i)
-                     dimension%masked_indices(j) = i
-                     j = j + 1
-                  ENDIF
-               ENDDO
-
-            ELSEIF ( ALLOCATED( dimension%values_int16 ) )  THEN
-
-               ALLOCATE( dimension%masked_values_int16(0:dimension%length_mask-1) )
-               j = 0
-               DO  i = 0, dimension%length-1
-                  IF ( dimension%mask(i) )  THEN
-                     dimension%masked_values_int16(j) = dimension%values_int16(i)
-                     dimension%masked_indices(j) = i
-                     j = j + 1
-                  ENDIF
-               ENDDO
-
-            ELSEIF ( ALLOCATED( dimension%values_int32 ) )  THEN
-
-               ALLOCATE( dimension%masked_values_int32(0:dimension%length_mask-1) )
-               j = 0
-               DO  i = 0, dimension%length-1
-                  IF ( dimension%mask(i) )  THEN
-                     dimension%masked_values_int32(j) = dimension%values_int32(i)
-                     dimension%masked_indices(j) = i
-                     j = j + 1
-                  ENDIF
-               ENDDO
-
-            ELSEIF ( ALLOCATED( dimension%values_intwp ) )  THEN
-
-               ALLOCATE( dimension%masked_values_intwp(0:dimension%length_mask-1) )
-               j = 0
-               DO  i = 0, dimension%length-1
-                  IF ( dimension%mask(i) )  THEN
-                     dimension%masked_values_intwp(j) = dimension%values_intwp(i)
-                     dimension%masked_indices(j) = i
-                     j = j + 1
-                  ENDIF
-               ENDDO
-
-            ELSEIF ( ALLOCATED( dimension%values_real32 ) )  THEN
-
-               ALLOCATE( dimension%masked_values_real32(0:dimension%length_mask-1) )
-               j = 0
-               DO  i = 0, dimension%length-1
-                  IF ( dimension%mask(i) )  THEN
-                     dimension%masked_values_real32(j) = dimension%values_real32(i)
-                     dimension%masked_indices(j) = i
-                     j = j + 1
-                  ENDIF
-               ENDDO
-
-            ELSEIF ( ALLOCATED(dimension%values_real64) )  THEN
-
-               ALLOCATE( dimension%masked_values_real64(0:dimension%length_mask-1) )
-               j = 0
-               DO  i = 0, dimension%length-1
-                  IF ( dimension%mask(i) )  THEN
-                     dimension%masked_values_real64(j) = dimension%values_real64(i)
-                     dimension%masked_indices(j) = i
-                     j = j + 1
-                  ENDIF
-               ENDDO
-
-            ELSEIF ( ALLOCATED(dimension%values_realwp) )  THEN
-
-               ALLOCATE( dimension%masked_values_realwp(0:dimension%length_mask-1) )
-               j = 0
-               DO  i = dimension%bounds(1), dimension%bounds(2)   !> @todo change loop also for other data types
-                  IF ( dimension%mask(i) )  THEN
-                     dimension%masked_values_realwp(j) = dimension%values_realwp(i)
-                     dimension%masked_indices(j) = i
-                     j = j + 1
-                  ENDIF
-               ENDDO
-
-            ENDIF
+            IF ( ALL( mask ) )  THEN
+
+               CALL internal_message( 'debug', routine_name //                              &
+                                      ': mask contains only TRUE values. Ignoring mask ' // &
+                                      '(dimension "' // TRIM( dimension_name ) //           &
+                                      '", file "' // TRIM( file_name ) // '")!' )
+
+            ELSE
+
+               dimension%is_masked = .TRUE.
+               dimension%length_mask = COUNT( mask )
+
+               ALLOCATE( dimension%mask(dimension%bounds(1):dimension%bounds(2)) )
+               ALLOCATE( dimension%masked_indices(0:dimension%length_mask-1) )
+
+               dimension%mask = mask
+
+               !-- Save masked positions and masked values
+               IF ( ALLOCATED( dimension%values_int8 ) )  THEN
+
+                  ALLOCATE( dimension%masked_values_int8(0:dimension%length_mask-1) )
+                  j = 0
+                  DO  i = dimension%bounds(1), dimension%bounds(2)
+                     IF ( dimension%mask(i) )  THEN
+                        dimension%masked_values_int8(j) = dimension%values_int8(i)
+                        dimension%masked_indices(j) = i
+                        j = j + 1
+                     ENDIF
+                  ENDDO
+
+               ELSEIF ( ALLOCATED( dimension%values_int16 ) )  THEN
+
+                  ALLOCATE( dimension%masked_values_int16(0:dimension%length_mask-1) )
+                  j = 0
+                  DO  i = dimension%bounds(1), dimension%bounds(2)
+                     IF ( dimension%mask(i) )  THEN
+                        dimension%masked_values_int16(j) = dimension%values_int16(i)
+                        dimension%masked_indices(j) = i
+                        j = j + 1
+                     ENDIF
+                  ENDDO
+
+               ELSEIF ( ALLOCATED( dimension%values_int32 ) )  THEN
+
+                  ALLOCATE( dimension%masked_values_int32(0:dimension%length_mask-1) )
+                  j = 0
+                  DO  i =dimension%bounds(1), dimension%bounds(2)
+                     IF ( dimension%mask(i) )  THEN
+                        dimension%masked_values_int32(j) = dimension%values_int32(i)
+                        dimension%masked_indices(j) = i
+                        j = j + 1
+                     ENDIF
+                  ENDDO
+
+               ELSEIF ( ALLOCATED( dimension%values_intwp ) )  THEN
+
+                  ALLOCATE( dimension%masked_values_intwp(0:dimension%length_mask-1) )
+                  j = 0
+                  DO  i = dimension%bounds(1), dimension%bounds(2)
+                     IF ( dimension%mask(i) )  THEN
+                        dimension%masked_values_intwp(j) = dimension%values_intwp(i)
+                        dimension%masked_indices(j) = i
+                        j = j + 1
+                     ENDIF
+                  ENDDO
+
+               ELSEIF ( ALLOCATED( dimension%values_real32 ) )  THEN
+
+                  ALLOCATE( dimension%masked_values_real32(0:dimension%length_mask-1) )
+                  j = 0
+                  DO  i = dimension%bounds(1), dimension%bounds(2)
+                     IF ( dimension%mask(i) )  THEN
+                        dimension%masked_values_real32(j) = dimension%values_real32(i)
+                        dimension%masked_indices(j) = i
+                        j = j + 1
+                     ENDIF
+                  ENDDO
+
+               ELSEIF ( ALLOCATED(dimension%values_real64) )  THEN
+
+                  ALLOCATE( dimension%masked_values_real64(0:dimension%length_mask-1) )
+                  j = 0
+                  DO  i = dimension%bounds(1), dimension%bounds(2)
+                     IF ( dimension%mask(i) )  THEN
+                        dimension%masked_values_real64(j) = dimension%values_real64(i)
+                        dimension%masked_indices(j) = i
+                        j = j + 1
+                     ENDIF
+                  ENDDO
+
+               ELSEIF ( ALLOCATED(dimension%values_realwp) )  THEN
+
+                  ALLOCATE( dimension%masked_values_realwp(0:dimension%length_mask-1) )
+                  j = 0
+                  DO  i = dimension%bounds(1), dimension%bounds(2)
+                     IF ( dimension%mask(i) )  THEN
+                        dimension%masked_values_realwp(j) = dimension%values_realwp(i)
+                        dimension%masked_indices(j) = i
+                        j = j + 1
+                     ENDIF
+                  ENDDO
+
+               ENDIF
+
+            ENDIF  ! if not all mask = true
 
          ELSE
             return_value = 1
-            CALL internal_message( 'error', routine_name //                   &
-                                            ': dimension ' // TRIM( name ) // &
-                                            ': size of mask and given bounds do not match' )
+            CALL internal_message( 'error', routine_name //                           &
+                                   ': size of mask and given bounds do not match ' // &
+                                   '(dimension "' // TRIM( dimension_name ) //        &
+                                   '", file "' // TRIM( file_name ) // '")!' )
          ENDIF
 
@@ -989,7 +642,6 @@
       CALL internal_message( 'error', routine_name //                                       &
                              ': at least one but no more than two bounds must be given ' // &
-                             '(dimension "' // TRIM( name ) //                              &
-                             '", file "' // TRIM( filename ) //                             &
-                             '")!' )
+                             '(dimension "' // TRIM( dimension_name ) //                    &
+                             '", file "' // TRIM( file_name ) // '")!' )
 
    ENDIF
@@ -998,20 +650,22 @@
    IF ( return_value == 0 )  THEN
 
-      DO  f = 1, nf
-
-         IF ( TRIM( filename ) == files(f)%name )  THEN
+      DO  f = 1, nfiles
+
+         IF ( TRIM( file_name ) == 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 dimension definition allowed!' )
+               CALL internal_message( 'error', routine_name //                      &
+                                      ': file already initialized. ' //             &
+                                      'No further dimension definition allowed ' // &
+                                      '(dimension "' // TRIM( dimension_name ) //   &
+                                      '", file "' // TRIM( file_name ) // '")!' )
                EXIT
 
             ELSEIF ( .NOT. ALLOCATED( files(f)%dimensions ) )  THEN
 
-               ndim = 1
-               ALLOCATE( files(f)%dimensions(ndim) )
+               ndims = 1
+               ALLOCATE( files(f)%dimensions(ndims) )
 
             ELSE
@@ -1022,10 +676,9 @@
                      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.' )
+                        CALL internal_message( 'error', routine_name //                    &
+                                ': file already has a variable of this name defined. ' //  &
+                                'Defining a dimension of the same name is not allowed ' // &
+                                '(dimension "' // TRIM( dimension_name ) //                &
+                                '", file "' // TRIM( file_name ) // '")!' )
                         EXIT
                      ENDIF
@@ -1035,13 +688,13 @@
                IF ( return_value == 0 )  THEN
                   !-- Check if dimension already exists in file
-                  ndim = SIZE( files(f)%dimensions )
-
-                  DO  d = 1, ndim
+                  ndims = SIZE( files(f)%dimensions )
+
+                  DO  d = 1, ndims
                      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 ) // '"' )
+                        CALL internal_message( 'error', routine_name //     &
+                                ': dimension already exists in file ' //    &
+                                '(dimension "' // TRIM( dimension_name ) // &
+                                '", file "' // TRIM( file_name ) // '")!' )
                         EXIT
                      ENDIF
@@ -1050,11 +703,11 @@
                   !-- Extend dimension list
                   IF ( return_value == 0 )  THEN
-                     ALLOCATE( dims_tmp(ndim) )
-                     dims_tmp = files(f)%dimensions
+                     ALLOCATE( dimensions_tmp(ndims) )
+                     dimensions_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 )
+                     ndims = ndims + 1
+                     ALLOCATE( files(f)%dimensions(ndims) )
+                     files(f)%dimensions(:ndims-1) = dimensions_tmp
+                     DEALLOCATE( dimensions_tmp )
                   ENDIF
                ENDIF
@@ -1063,5 +716,5 @@
 
             !-- Add new dimension to database
-            IF ( return_value == 0 )  files(f)%dimensions(ndim) = dimension
+            IF ( return_value == 0 )  files(f)%dimensions(ndims) = dimension
 
             EXIT
@@ -1070,9 +723,9 @@
       ENDDO
 
-      IF ( f > nf )  THEN
+      IF ( f > nfiles )  THEN
          return_value = 1
-         CALL internal_message( 'error', routine_name //                           &
-                                ': file not found (dimension "' // TRIM( name ) // &
-                                '", file "' // TRIM( filename ) // '")!' )
+         CALL internal_message( 'error', routine_name //                                     &
+                                ': file not found (dimension "' // TRIM( dimension_name ) // &
+                                '", file "' // TRIM( file_name ) // '")!' )
       ENDIF
 
@@ -1085,7 +738,10 @@
 ! ------------
 !> Add variable to database.
+!> If a variable is identical for each MPI rank, the optional argument 'is_global' should be set to
+!> TRUE. This flags the variable to be a global variable and is later only written once by the
+!> master output rank.
 !> Example call:
-!>   dom_def_var( filename =  'DATA_OUTPUT_3D', &
-!>                name = 'u', &
+!>   dom_def_var( file_name =  'my_output_file_name', &
+!>                variable_name = 'u', &
 !>                dimension_names = (/'x   ', 'y   ', 'z   ', 'time'/), &
 !>                output_type = 'real32' )
@@ -1103,10 +759,10 @@
 !>          ALLOCATE( u(<z>,<y>,<x>) )
 !--------------------------------------------------------------------------------------------------!
-FUNCTION dom_def_var( filename, name, dimension_names, output_type, is_global ) &
+FUNCTION dom_def_var( file_name, variable_name, dimension_names, output_type, is_global ) &
             RESULT( return_value )
 
-   CHARACTER(LEN=*), INTENT(IN) ::  filename     !< name of file
-   CHARACTER(LEN=*), INTENT(IN) ::  name         !< name of variable
-   CHARACTER(LEN=*), INTENT(IN) ::  output_type  !< data type of variable
+   CHARACTER(LEN=*), INTENT(IN) ::  file_name      !< name of file
+   CHARACTER(LEN=*), INTENT(IN) ::  variable_name  !< name of variable
+   CHARACTER(LEN=*), INTENT(IN) ::  output_type    !< data type of variable
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_def_var'  !< name of this routine
@@ -1114,25 +770,25 @@
    CHARACTER(LEN=*), DIMENSION(:), INTENT(IN) ::  dimension_names  !< list of dimension names
 
-   INTEGER(iwp) ::  d             !< loop index
-   INTEGER(iwp) ::  f             !< loop index
-   INTEGER(iwp) ::  i             !< loop index
-   INTEGER(iwp) ::  nvar          !< number of variables in file
-   INTEGER(iwp) ::  return_value  !< return value
-
-   LOGICAL                       ::  found = .FALSE.  !< true if requested dimension is defined in file
-   LOGICAL, INTENT(IN), OPTIONAL ::  is_global        !< true if variable is global (same on all PE)
-
-   TYPE(variable_type) ::  variable  !< new variable
-
-   TYPE(variable_type), DIMENSION(:), ALLOCATABLE ::  vars_tmp  !< temporary variable list
+   INTEGER ::  d             !< loop index
+   INTEGER ::  f             !< loop index
+   INTEGER ::  i             !< loop index
+   INTEGER ::  nvars         !< number of variables in file
+   INTEGER ::  return_value  !< return value
+
+   LOGICAL                       ::  found      !< true if requested dimension is defined in file
+   LOGICAL, INTENT(IN), OPTIONAL ::  is_global  !< true if variable is global (same on all PE)
+
+   TYPE(variable_type)                            ::  variable       !< new variable
+   TYPE(variable_type), DIMENSION(:), ALLOCATABLE ::  variables_tmp  !< temporary variable list
 
 
    return_value = 0
-
-   CALL internal_message( 'debug', routine_name //                  &
-                          ': define variable "' // TRIM( name ) // &
-                          '" in file "' // TRIM( filename ) // '"' )
-
-   variable%name = TRIM( name )
+   found = .FALSE.
+
+   CALL internal_message( 'debug', routine_name //                                     &
+                          ': define variable (variable "' // TRIM( variable_name ) //  &
+                          '", file "' // TRIM( file_name ) // '")' )
+
+   variable%name = TRIM( variable_name )
 
    ALLOCATE( variable%dimension_names(SIZE( dimension_names )) )
@@ -1150,13 +806,15 @@
 
    !-- Add variable to database
-   DO  f = 1, nf
-
-      IF ( TRIM( filename ) == files(f)%name )  THEN
+   DO  f = 1, nfiles
+
+      IF ( TRIM( file_name ) == 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 variable definition allowed!' )
+            CALL internal_message( 'error', routine_name //                                  &
+                    ': file already initialized. No further variable definition allowed ' // &
+                    '(variable "' // TRIM( variable_name ) //                                &
+                    '", file "' // TRIM( file_name ) // '")!' )
             EXIT
 
@@ -1167,9 +825,9 @@
                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.' )
+                  CALL internal_message( 'error', routine_name //                    &
+                          ': file already has a dimension of this name defined. ' // &
+                          'Defining a variable of the same name is not allowed ' //  &
+                          '(variable "' // TRIM( variable_name ) //                  &
+                          '", file "' // TRIM( file_name ) // '")!' )
                   EXIT
                ENDIF
@@ -1188,11 +846,9 @@
                   IF ( .NOT. found )  THEN
                      return_value = 1
-                     CALL internal_message( 'error',                                            &
-                                            routine_name //                                     &
-                                            ': required dimension "' //                         &
-                                            TRIM( variable%dimension_names(i) ) //              &
-                                            '" for variable "' // TRIM( name ) //               &
-                                            '" is not defined in file "' // TRIM( filename ) // &
-                                            '"!' )
+                     CALL internal_message( 'error', routine_name //                            &
+                             ': required dimension "'//  TRIM( variable%dimension_names(i) ) // &
+                             '" for variable is not defined ' //                                &
+                             '(variable "' // TRIM( variable_name ) //                          &
+                             '", file "' // TRIM( file_name ) // '")!' )
                      EXIT
                   ENDIF
@@ -1203,8 +859,8 @@
 
             return_value = 1
-            CALL internal_message( 'error', routine_name //                        &
-                                   ': cannot define variable "' // TRIM( name ) // &
-                                   '" in file "' // TRIM( filename ) //            &
-                                   '" because no dimensions defined in file.' )
+            CALL internal_message( 'error', routine_name //                      &
+                    ': no dimensions defined in file. Cannot define variable '// &
+                    '(variable "' // TRIM( variable_name ) //                    &
+                    '", file "' // TRIM( file_name ) // '")!' )
 
          ENDIF
@@ -1215,17 +871,17 @@
             IF ( .NOT. ALLOCATED( files(f)%variables ) )  THEN
 
-               nvar = 1
-               ALLOCATE( files(f)%variables(nvar) )
+               nvars = 1
+               ALLOCATE( files(f)%variables(nvars) )
 
             ELSE
 
-               nvar = SIZE( files(f)%variables )
-               DO  i = 1, nvar
+               nvars = SIZE( files(f)%variables )
+               DO  i = 1, nvars
                   IF ( files(f)%variables(i)%name == variable%name )  THEN
                      return_value = 1
-                     CALL internal_message( 'error', routine_name //          &
-                                            ': variable "' // TRIM( name ) // &
-                                            '" already exists in file "' //   &
-                                            TRIM( filename ) // '"!' )
+                     CALL internal_message( 'error', routine_name //   &
+                             ': variable already exists '//            &
+                             '(variable "' // TRIM( variable_name ) // &
+                             '", file "' // TRIM( file_name ) // '")!' )
                      EXIT
                   ENDIF
@@ -1234,11 +890,11 @@
                IF ( return_value == 0 )  THEN
                   !-- Extend variable list
-                  ALLOCATE( vars_tmp(nvar) )
-                  vars_tmp = files(f)%variables
+                  ALLOCATE( variables_tmp(nvars) )
+                  variables_tmp = files(f)%variables
                   DEALLOCATE( files(f)%variables )
-                  nvar = nvar + 1
-                  ALLOCATE( files(f)%variables(nvar) )
-                  files(f)%variables(:nvar-1) = vars_tmp
-                  DEALLOCATE( vars_tmp )
+                  nvars = nvars + 1
+                  ALLOCATE( files(f)%variables(nvars) )
+                  files(f)%variables(:nvars-1) = variables_tmp
+                  DEALLOCATE( variables_tmp )
                ENDIF
 
@@ -1246,5 +902,5 @@
 
             !-- Add new variable to database
-            IF ( return_value == 0 )  files(f)%variables(nvar) = variable
+            IF ( return_value == 0 )  files(f)%variables(nvars) = variable
 
          ENDIF
@@ -1256,9 +912,9 @@
    ENDDO
 
-   IF ( f > nf )  THEN
+   IF ( f > nfiles )  THEN
       return_value = 1
-      CALL internal_message( 'error', routine_name //                           &
-                             ': file not found (variable "' // TRIM( name ) //  &
-                             '", file "' // TRIM( filename ) // '")!' )
+      CALL internal_message( 'error', routine_name //                                   &
+                             ': file not found (variable "' // TRIM( variable_name ) // &
+                             '", file "' // TRIM( file_name ) // '")!' )
    ENDIF
 
@@ -1269,15 +925,37 @@
 ! ------------
 !> Create attribute with value of type character.
-!--------------------------------------------------------------------------------------------------!
-FUNCTION dom_def_att_char( filename, variable, name, value, append ) RESULT( return_value )
-
-   CHARACTER(LEN=*), INTENT(IN)           ::  filename  !< name of file
-   CHARACTER(LEN=*), INTENT(IN)           ::  name      !< name of attribute
-   CHARACTER(LEN=*), INTENT(IN)           ::  value     !< attribute value
-   CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  variable  !< name of variable
+!> If the optional argument 'variable_name' is given, the attribute is added to the respective
+!> variable or dimension of that name. Otherwise, the attribute is added as a global attribute to
+!> the file itself.
+!> If an attribute of similar name already exists, it is updated (overwritten) with the new value.
+!> If the optional argument 'append' is set TRUE, the value of an already existing attribute of
+!> similar name is appended by the new value instead of overwritten.
+!> Example call:
+!>   - define a global file attribute:
+!>      dom_def_att( file_name='my_output_file_name', &
+!>                   attribute_name='my_attribute', &
+!>                   value='This is the attribute value' )
+!>   - define a variable attribute:
+!>      dom_def_att( file_name='my_output_file_name', &
+!>                   variable_name='my_variable', &
+!>                   attribute_name='my_attribute', &
+!>                   value='This is the attribute value' )
+!>   - append an attribute:
+!>      dom_def_att( file_name='my_output_file_name', &
+!>                   attribute_name='my_attribute', &
+!>                   value=' and this part was appended', append=.TRUE. )
+!--------------------------------------------------------------------------------------------------!
+FUNCTION dom_def_att_char( file_name, variable_name, attribute_name, value, append ) &
+            RESULT( return_value )
+
+   CHARACTER(LEN=*),      INTENT(IN)           ::  file_name               !< name of file
+   CHARACTER(LEN=*),      INTENT(IN)           ::  attribute_name          !< name of attribute
+   CHARACTER(LEN=*),      INTENT(IN)           ::  value                   !< attribute value
+   CHARACTER(LEN=*),      INTENT(IN), OPTIONAL ::  variable_name           !< name of variable
+   CHARACTER(LEN=charlen)                      ::  variable_name_internal  !< internal copy of variable_name
 
    ! CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_def_att_char'  !< name of routine
 
-   INTEGER(iwp) ::  return_value  !< return value
+   INTEGER ::  return_value  !< return value
 
    LOGICAL                       ::  append_internal  !< same as 'append'
@@ -1295,15 +973,17 @@
    ENDIF
 
-   attribute%name       = TRIM( name )
+   attribute%name       = TRIM( attribute_name )
    attribute%data_type  = 'char'
    attribute%value_char = TRIM( value )
 
-   IF ( PRESENT( variable ) )  THEN
-      return_value = dom_def_att_save( TRIM( filename ), TRIM( variable ), &
-                                       attribute=attribute, append=append_internal )
+   IF ( PRESENT( variable_name ) )  THEN
+      variable_name_internal = TRIM( variable_name )
    ELSE
-      return_value = dom_def_att_save( TRIM( filename ), &
-                                       attribute=attribute, append=append_internal )
+      variable_name_internal = ''
    ENDIF
+
+   return_value = save_attribute_in_database( file_name=TRIM( file_name ), &
+                     variable_name=TRIM( variable_name_internal ),         &
+                     attribute=attribute, append=append_internal )
 
 END FUNCTION dom_def_att_char
@@ -1313,10 +993,26 @@
 ! ------------
 !> Create attribute with value of type int8.
-!--------------------------------------------------------------------------------------------------!
-FUNCTION dom_def_att_int8( filename, variable, name, value, append ) RESULT( return_value )
-
-   CHARACTER(LEN=*), INTENT(IN)           ::  filename  !< name of file
-   CHARACTER(LEN=*), INTENT(IN)           ::  name      !< name of attribute
-   CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  variable  !< name of variable
+!> If the optional argument 'variable_name' is given, the attribute is added to the respective
+!> variable or dimension of that name. Otherwise, the attribute is added as a global attribute to
+!> the file itself.
+!> Numerical attributes cannot be appended, only updated (append=.TRUE. will cause an error).
+!> Example call:
+!>   - define a global file attribute:
+!>      dom_def_att( file_name='my_output_file_name', &
+!>                   attribute_name='my_attribute', &
+!>                   value=0_1 )
+!>   - define a variable attribute:
+!>      dom_def_att( file_name='my_output_file_name', &
+!>                   variable_name='my_variable', &
+!>                   attribute_name='my_attribute', &
+!>                   value=1_1 )
+!--------------------------------------------------------------------------------------------------!
+FUNCTION dom_def_att_int8( file_name, variable_name, attribute_name, value, append ) &
+            RESULT( return_value )
+
+   CHARACTER(LEN=*),      INTENT(IN)           ::  file_name               !< name of file
+   CHARACTER(LEN=*),      INTENT(IN)           ::  attribute_name          !< name of attribute
+   CHARACTER(LEN=*),      INTENT(IN), OPTIONAL ::  variable_name           !< name of variable
+   CHARACTER(LEN=charlen)                      ::  variable_name_internal  !< internal copy of variable_name
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_def_att_int8'  !< name of routine
@@ -1324,5 +1020,5 @@
    INTEGER(KIND=1), INTENT(IN) ::  value  !< attribute value
 
-   INTEGER(iwp) ::  return_value  !< return value
+   INTEGER ::  return_value  !< return value
 
    LOGICAL                       ::  append_internal  !< same as 'append'
@@ -1333,12 +1029,19 @@
 
    return_value = 0
+
+   IF ( PRESENT( variable_name ) )  THEN
+      variable_name_internal = TRIM( variable_name )
+   ELSE
+      variable_name_internal = ''
+   ENDIF
 
    IF ( PRESENT( append ) )  THEN
       IF ( append )  THEN
          return_value = 1
-         CALL internal_message( 'error',                           &
-                                routine_name //                    &
-                                ': attribute "' // TRIM( name ) // &
-                                '": append of numeric attribute not possible.' )
+         CALL internal_message( 'error', routine_name //                             &
+                                ': numeric attribute cannot be appended ' //         &
+                                '(attribute "' // TRIM( attribute_name ) //          &
+                                '", variable "' // TRIM( variable_name_internal ) // &
+                                '", file "' // TRIM( file_name ) // '")!' )
       ENDIF
    ENDIF
@@ -1347,15 +1050,11 @@
       append_internal = .FALSE.
 
-      attribute%name       = TRIM( name )
+      attribute%name       = TRIM( attribute_name )
       attribute%data_type  = 'int8'
       attribute%value_int8 = value
 
-      IF ( PRESENT( variable ) )  THEN
-         return_value = dom_def_att_save( TRIM( filename ), TRIM( variable ), &
-                                              attribute=attribute, append=append_internal )
-      ELSE
-         return_value = dom_def_att_save( TRIM( filename ), &
-                                              attribute=attribute, append=append_internal )
-      ENDIF
+      return_value = save_attribute_in_database( file_name=TRIM( file_name ), &
+                        variable_name=TRIM( variable_name_internal ),         &
+                        attribute=attribute, append=append_internal )
    ENDIF
 
@@ -1366,10 +1065,26 @@
 ! ------------
 !> Create attribute with value of type int16.
-!--------------------------------------------------------------------------------------------------!
-FUNCTION dom_def_att_int16( filename, variable, name, value, append ) RESULT( return_value )
-
-   CHARACTER(LEN=*), INTENT(IN)           ::  filename  !< name of file
-   CHARACTER(LEN=*), INTENT(IN)           ::  name      !< name of attribute
-   CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  variable  !< name of variable
+!> If the optional argument 'variable_name' is given, the attribute is added to the respective
+!> variable or dimension of that name. Otherwise, the attribute is added as a global attribute to
+!> the file itself.
+!> Numerical attributes cannot be appended, only updated (append=.TRUE. will cause an error).
+!> Example call:
+!>   - define a global file attribute:
+!>      dom_def_att( file_name='my_output_file_name', &
+!>                   attribute_name='my_attribute', &
+!>                   value=0_2 )
+!>   - define a variable attribute:
+!>      dom_def_att( file_name='my_output_file_name', &
+!>                   variable_name='my_variable', &
+!>                   attribute_name='my_attribute', &
+!>                   value=1_2 )
+!--------------------------------------------------------------------------------------------------!
+FUNCTION dom_def_att_int16( file_name, variable_name, attribute_name, value, append ) &
+            RESULT( return_value )
+
+   CHARACTER(LEN=*),      INTENT(IN)           ::  file_name               !< name of file
+   CHARACTER(LEN=*),      INTENT(IN)           ::  attribute_name          !< name of attribute
+   CHARACTER(LEN=*),      INTENT(IN), OPTIONAL ::  variable_name           !< name of variable
+   CHARACTER(LEN=charlen)                      ::  variable_name_internal  !< internal copy of variable_name
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_def_att_int16'  !< name of routine
@@ -1377,5 +1092,5 @@
    INTEGER(KIND=2), INTENT(IN) ::  value  !< attribute value
 
-   INTEGER(iwp) ::  return_value  !< return value
+   INTEGER ::  return_value  !< return value
 
    LOGICAL                       ::  append_internal  !< same as 'append'
@@ -1386,12 +1101,19 @@
 
    return_value = 0
+
+   IF ( PRESENT( variable_name ) )  THEN
+      variable_name_internal = TRIM( variable_name )
+   ELSE
+      variable_name_internal = ''
+   ENDIF
 
    IF ( PRESENT( append ) )  THEN
       IF ( append )  THEN
          return_value = 1
-         CALL internal_message( 'error',                           &
-                                routine_name //                    &
-                                ': attribute "' // TRIM( name ) // &
-                                '": append of numeric attribute not possible.' )
+         CALL internal_message( 'error', routine_name //                             &
+                                ': numeric attribute cannot be appended ' //         &
+                                '(attribute "' // TRIM( attribute_name ) //          &
+                                '", variable "' // TRIM( variable_name_internal ) // &
+                                '", file "' // TRIM( file_name ) // '")!' )
       ENDIF
    ENDIF
@@ -1400,15 +1122,11 @@
       append_internal = .FALSE.
 
-      attribute%name        = TRIM( name )
+      attribute%name        = TRIM( attribute_name )
       attribute%data_type   = 'int16'
       attribute%value_int16 = value
 
-      IF ( PRESENT( variable ) )  THEN
-         return_value = dom_def_att_save( TRIM( filename ), TRIM( variable ), &
-                                               attribute=attribute, append=append_internal )
-      ELSE
-         return_value = dom_def_att_save( TRIM( filename ), &
-                                               attribute=attribute, append=append_internal )
-      ENDIF
+      return_value = save_attribute_in_database( file_name=TRIM( file_name ), &
+                        variable_name=TRIM( variable_name_internal ),         &
+                        attribute=attribute, append=append_internal )
    ENDIF
 
@@ -1419,10 +1137,26 @@
 ! ------------
 !> Create attribute with value of type int32.
-!--------------------------------------------------------------------------------------------------!
-FUNCTION dom_def_att_int32( filename, variable, name, value, append ) RESULT( return_value )
-
-   CHARACTER(LEN=*), INTENT(IN)           ::  filename  !< name of file
-   CHARACTER(LEN=*), INTENT(IN)           ::  name      !< name of attribute
-   CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  variable  !< name of variable
+!> If the optional argument 'variable_name' is given, the attribute is added to the respective
+!> variable or dimension of that name. Otherwise, the attribute is added as a global attribute to
+!> the file itself.
+!> Numerical attributes cannot be appended, only updated (append=.TRUE. will cause an error).
+!> Example call:
+!>   - define a global file attribute:
+!>      dom_def_att( file_name='my_output_file_name', &
+!>                   attribute_name='my_attribute', &
+!>                   value=0_4 )
+!>   - define a variable attribute:
+!>      dom_def_att( file_name='my_output_file_name', &
+!>                   variable_name='my_variable', &
+!>                   attribute_name='my_attribute', &
+!>                   value=1_4 )
+!--------------------------------------------------------------------------------------------------!
+FUNCTION dom_def_att_int32( file_name, variable_name, attribute_name, value, append ) &
+            RESULT( return_value )
+
+   CHARACTER(LEN=*),      INTENT(IN)           ::  file_name               !< name of file
+   CHARACTER(LEN=*),      INTENT(IN)           ::  attribute_name          !< name of attribute
+   CHARACTER(LEN=*),      INTENT(IN), OPTIONAL ::  variable_name           !< name of variable
+   CHARACTER(LEN=charlen)                      ::  variable_name_internal  !< internal copy of variable_name
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_def_att_int32'  !< name of routine
@@ -1430,5 +1164,5 @@
    INTEGER(KIND=4), INTENT(IN) ::  value  !< attribute value
 
-   INTEGER(iwp) ::  return_value  !< return value
+   INTEGER ::  return_value  !< return value
 
    LOGICAL                       ::  append_internal  !< same as 'append'
@@ -1439,12 +1173,19 @@
 
    return_value = 0
+
+   IF ( PRESENT( variable_name ) )  THEN
+      variable_name_internal = TRIM( variable_name )
+   ELSE
+      variable_name_internal = ''
+   ENDIF
 
    IF ( PRESENT( append ) )  THEN
       IF ( append )  THEN
          return_value = 1
-         CALL internal_message( 'error',                           &
-                                routine_name //                    &
-                                ': attribute "' // TRIM( name ) // &
-                                '": append of numeric attribute not possible.' )
+         CALL internal_message( 'error', routine_name //                             &
+                                ': numeric attribute cannot be appended ' //         &
+                                '(attribute "' // TRIM( attribute_name ) //          &
+                                '", variable "' // TRIM( variable_name_internal ) // &
+                                '", file "' // TRIM( file_name ) // '")!' )
       ENDIF
    ENDIF
@@ -1453,15 +1194,11 @@
       append_internal = .FALSE.
 
-      attribute%name        = TRIM( name )
+      attribute%name        = TRIM( attribute_name )
       attribute%data_type   = 'int32'
       attribute%value_int32 = value
 
-      IF ( PRESENT( variable ) )  THEN
-         return_value = dom_def_att_save( TRIM( filename ), TRIM( variable ), &
-                                               attribute=attribute, append=append_internal )
-      ELSE
-         return_value = dom_def_att_save( TRIM( filename ), &
-                                               attribute=attribute, append=append_internal )
-      ENDIF
+      return_value = save_attribute_in_database( file_name=TRIM( file_name ), &
+                        variable_name=TRIM( variable_name_internal ),         &
+                        attribute=attribute, append=append_internal )
    ENDIF
 
@@ -1472,14 +1209,30 @@
 ! ------------
 !> Create attribute with value of type real32.
-!--------------------------------------------------------------------------------------------------!
-FUNCTION dom_def_att_real32( filename, variable, name, value, append ) RESULT( return_value )
-
-   CHARACTER(LEN=*), INTENT(IN)           ::  filename  !< name of file
-   CHARACTER(LEN=*), INTENT(IN)           ::  name      !< name of attribute
-   CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  variable  !< name of variable
+!> If the optional argument 'variable_name' is given, the attribute is added to the respective
+!> variable or dimension of that name. Otherwise, the attribute is added as a global attribute to
+!> the file itself.
+!> Numerical attributes cannot be appended, only updated (append=.TRUE. will cause an error).
+!> Example call:
+!>   - define a global file attribute:
+!>      dom_def_att( file_name='my_output_file_name', &
+!>                   attribute_name='my_attribute', &
+!>                   value=1.0_4 )
+!>   - define a variable attribute:
+!>      dom_def_att( file_name='my_output_file_name', &
+!>                   variable_name='my_variable', &
+!>                   attribute_name='my_attribute', &
+!>                   value=1.0_4 )
+!--------------------------------------------------------------------------------------------------!
+FUNCTION dom_def_att_real32( file_name, variable_name, attribute_name, value, append ) &
+            RESULT( return_value )
+
+   CHARACTER(LEN=*),      INTENT(IN)           ::  file_name               !< name of file
+   CHARACTER(LEN=*),      INTENT(IN)           ::  attribute_name          !< name of attribute
+   CHARACTER(LEN=*),      INTENT(IN), OPTIONAL ::  variable_name           !< name of variable
+   CHARACTER(LEN=charlen)                      ::  variable_name_internal  !< internal copy of variable_name
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_def_att_real32'  !< name of routine
 
-   INTEGER(iwp) ::  return_value  !< return value
+   INTEGER ::  return_value  !< return value
 
    LOGICAL                       ::  append_internal  !< same as 'append'
@@ -1492,12 +1245,19 @@
 
    return_value = 0
+
+   IF ( PRESENT( variable_name ) )  THEN
+      variable_name_internal = TRIM( variable_name )
+   ELSE
+      variable_name_internal = ''
+   ENDIF
 
    IF ( PRESENT( append ) )  THEN
       IF ( append )  THEN
          return_value = 1
-         CALL internal_message( 'error',                           &
-                                routine_name //                    &
-                                ': attribute "' // TRIM( name ) // &
-                                '": append of numeric attribute not possible.' )
+         CALL internal_message( 'error', routine_name //                             &
+                                ': numeric attribute cannot be appended ' //         &
+                                '(attribute "' // TRIM( attribute_name ) //          &
+                                '", variable "' // TRIM( variable_name_internal ) // &
+                                '", file "' // TRIM( file_name ) // '")!' )
       ENDIF
    ENDIF
@@ -1506,15 +1266,11 @@
       append_internal = .FALSE.
 
-      attribute%name         = TRIM( name )
+      attribute%name         = TRIM( attribute_name )
       attribute%data_type    = 'real32'
       attribute%value_real32 = value
 
-      IF ( PRESENT( variable ) )  THEN
-         return_value = dom_def_att_save( TRIM( filename ), TRIM( variable ), &
-                                                attribute=attribute, append=append_internal )
-      ELSE
-         return_value = dom_def_att_save( TRIM( filename ), &
-                                                attribute=attribute, append=append_internal )
-      ENDIF
+      return_value = save_attribute_in_database( file_name=TRIM( file_name ), &
+                        variable_name=TRIM( variable_name_internal ),         &
+                        attribute=attribute, append=append_internal )
    ENDIF
 
@@ -1525,14 +1281,30 @@
 ! ------------
 !> Create attribute with value of type real64.
-!--------------------------------------------------------------------------------------------------!
-FUNCTION dom_def_att_real64( filename, variable, name, value, append ) RESULT( return_value )
-
-   CHARACTER(LEN=*), INTENT(IN)           ::  filename  !< name of file
-   CHARACTER(LEN=*), INTENT(IN)           ::  name      !< name of attribute
-   CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  variable  !< name of variable
+!> If the optional argument 'variable_name' is given, the attribute is added to the respective
+!> variable or dimension of that name. Otherwise, the attribute is added as a global attribute to
+!> the file itself.
+!> Numerical attributes cannot be appended, only updated (append=.TRUE. will cause an error).
+!> Example call:
+!>   - define a global file attribute:
+!>      dom_def_att( file_name='my_output_file_name', &
+!>                   attribute_name='my_attribute', &
+!>                   value=0.0_8 )
+!>   - define a variable attribute:
+!>      dom_def_att( file_name='my_output_file_name', &
+!>                   variable_name='my_variable', &
+!>                   attribute_name='my_attribute', &
+!>                   value=1.0_8 )
+!--------------------------------------------------------------------------------------------------!
+FUNCTION dom_def_att_real64( file_name, variable_name, attribute_name, value, append ) &
+            RESULT( return_value )
+
+   CHARACTER(LEN=*),      INTENT(IN)           ::  file_name               !< name of file
+   CHARACTER(LEN=*),      INTENT(IN)           ::  attribute_name          !< name of attribute
+   CHARACTER(LEN=*),      INTENT(IN), OPTIONAL ::  variable_name           !< name of variable
+   CHARACTER(LEN=charlen)                      ::  variable_name_internal  !< internal copy of variable_name
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_def_att_real64'  !< name of routine
 
-   INTEGER(iwp) ::  return_value  !< return value
+   INTEGER ::  return_value  !< return value
 
    LOGICAL                       ::  append_internal  !< same as 'append'
@@ -1545,12 +1317,19 @@
 
    return_value = 0
+
+   IF ( PRESENT( variable_name ) )  THEN
+      variable_name_internal = TRIM( variable_name )
+   ELSE
+      variable_name_internal = ''
+   ENDIF
 
    IF ( PRESENT( append ) )  THEN
       IF ( append )  THEN
          return_value = 1
-         CALL internal_message( 'error',                           &
-                                routine_name //                    &
-                                ': attribute "' // TRIM( name ) // &
-                                '": append of numeric attribute not possible.' )
+         CALL internal_message( 'error', routine_name //                             &
+                                ': numeric attribute cannot be appended ' //         &
+                                '(attribute "' // TRIM( attribute_name ) //          &
+                                '", variable "' // TRIM( variable_name_internal ) // &
+                                '", file "' // TRIM( file_name ) // '")!' )
       ENDIF
    ENDIF
@@ -1559,18 +1338,1156 @@
       append_internal = .FALSE.
 
-      attribute%name         = TRIM( name )
+      attribute%name         = TRIM( attribute_name )
       attribute%data_type    = 'real64'
       attribute%value_real64 = value
 
-      IF ( PRESENT( variable ) )  THEN
-         return_value = dom_def_att_save( TRIM( filename ), TRIM( variable ), &
-                                                attribute=attribute, append=append_internal )
+      return_value = save_attribute_in_database( file_name=TRIM( file_name ), &
+                        variable_name=TRIM( variable_name_internal ),         &
+                        attribute=attribute, append=append_internal )
+   ENDIF
+
+END FUNCTION dom_def_att_real64
+
+!--------------------------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> End output definition.
+!> The database is cleared from unused files and dimensions. Then, the output files are initialized
+!> and prepared for writing output values to them. The saved values of the dimensions are written
+!> to the files.
+!--------------------------------------------------------------------------------------------------!
+FUNCTION dom_def_end() RESULT( return_value )
+
+   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_def_end'  !< name of routine
+
+   INTEGER ::  d             !< loop index
+   INTEGER ::  f             !< loop index
+   INTEGER ::  return_value  !< return value
+
+   INTEGER(KIND=1), DIMENSION(:), ALLOCATABLE, TARGET ::  values_int8           !< target array for dimension values
+   INTEGER(KIND=2), DIMENSION(:), ALLOCATABLE, TARGET ::  values_int16          !< target array for dimension values
+   INTEGER(KIND=4), DIMENSION(:), ALLOCATABLE, TARGET ::  values_int32          !< target array for dimension values
+   INTEGER(iwp),    DIMENSION(:), ALLOCATABLE, TARGET ::  values_intwp          !< target array for dimension values
+   
+   INTEGER(KIND=1), DIMENSION(:), POINTER, CONTIGUOUS ::  values_int8_pointer   !< pointer to target array
+   INTEGER(KIND=2), DIMENSION(:), POINTER, CONTIGUOUS ::  values_int16_pointer  !< pointer to target array
+   INTEGER(KIND=4), DIMENSION(:), POINTER, CONTIGUOUS ::  values_int32_pointer  !< pointer to target array
+   INTEGER(iwp),    DIMENSION(:), POINTER, CONTIGUOUS ::  values_intwp_pointer  !< pointer to target array
+
+   REAL(KIND=4), DIMENSION(:), ALLOCATABLE, TARGET ::  values_real32            !< target array for dimension values
+   REAL(KIND=8), DIMENSION(:), ALLOCATABLE, TARGET ::  values_real64            !< target array for dimension values
+   REAL(wp),     DIMENSION(:), ALLOCATABLE, TARGET ::  values_realwp            !< target array for dimension values
+
+   REAL(KIND=4), DIMENSION(:), POINTER, CONTIGUOUS ::  values_real32_pointer    !< pointer to target array
+   REAL(KIND=8), DIMENSION(:), POINTER, CONTIGUOUS ::  values_real64_pointer    !< pointer to target array
+   REAL(wp),     DIMENSION(:), POINTER, CONTIGUOUS ::  values_realwp_pointer    !< pointer to target array
+
+
+   return_value = 0
+   CALL internal_message( 'debug', routine_name // ': start' )
+
+   !-- Clear database from empty files and unused dimensions
+   IF ( nfiles > 0 )  return_value = cleanup_database()
+
+   IF ( return_value == 0 )  THEN
+      DO  f = 1, nfiles
+
+         !-- 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, &
+                                return_value=return_value )
+
+         !-- Initialize file header:
+         !-- define dimensions and variables and write attributes
+         IF ( return_value == 0 )  &
+            CALL init_file_header( files(f), return_value=return_value )
+
+         !-- End file definition
+         IF ( return_value == 0 )  &
+            CALL stop_file_header_definition( 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
+                  ALLOCATE( values_int8(files(f)%dimensions(d)%bounds(1): &
+                                        files(f)%dimensions(d)%bounds(2)) )
+                  values_int8 = files(f)%dimensions(d)%values_int8
+                  values_int8_pointer => values_int8
+                  return_value = dom_write_var( files(f)%name, files(f)%dimensions(d)%name, &
+                                    bounds_start=(/ files(f)%dimensions(d)%bounds(1) /),    &
+                                    bounds_end  =(/ files(f)%dimensions(d)%bounds(2) /),    &
+                                    values_int8_1d=values_int8_pointer )
+                  DEALLOCATE( values_int8 )
+               ELSEIF ( ALLOCATED( files(f)%dimensions(d)%values_int16 ) )  THEN
+                  ALLOCATE( values_int16(files(f)%dimensions(d)%bounds(1): &
+                                         files(f)%dimensions(d)%bounds(2)) )
+                  values_int16 = files(f)%dimensions(d)%values_int16
+                  values_int16_pointer => values_int16
+                  return_value = dom_write_var( files(f)%name, files(f)%dimensions(d)%name, &
+                                    bounds_start=(/ files(f)%dimensions(d)%bounds(1) /),    &
+                                    bounds_end  =(/ files(f)%dimensions(d)%bounds(2) /),    &
+                                    values_int16_1d=values_int16_pointer )
+                  DEALLOCATE( values_int16 )
+               ELSEIF ( ALLOCATED( files(f)%dimensions(d)%values_int32 ) )  THEN
+                  ALLOCATE( values_int32(files(f)%dimensions(d)%bounds(1): &
+                                         files(f)%dimensions(d)%bounds(2)) )
+                  values_int32 = files(f)%dimensions(d)%values_int32
+                  values_int32_pointer => values_int32
+                  return_value = dom_write_var( files(f)%name, files(f)%dimensions(d)%name, &
+                                    bounds_start=(/ files(f)%dimensions(d)%bounds(1) /),    &
+                                    bounds_end  =(/ files(f)%dimensions(d)%bounds(2) /),    &
+                                    values_int32_1d=values_int32_pointer )
+                  DEALLOCATE( values_int32 )
+               ELSEIF ( ALLOCATED( files(f)%dimensions(d)%values_intwp ) )  THEN
+                  ALLOCATE( values_intwp(files(f)%dimensions(d)%bounds(1): &
+                                         files(f)%dimensions(d)%bounds(2)) )
+                  values_intwp = files(f)%dimensions(d)%values_intwp
+                  values_intwp_pointer => values_intwp
+                  return_value = dom_write_var( files(f)%name, files(f)%dimensions(d)%name, &
+                                    bounds_start=(/ files(f)%dimensions(d)%bounds(1) /),    &
+                                    bounds_end  =(/ files(f)%dimensions(d)%bounds(2) /),    &
+                                    values_intwp_1d=values_intwp_pointer )
+                  DEALLOCATE( values_intwp )
+               ELSEIF ( ALLOCATED( files(f)%dimensions(d)%values_real32 ) )  THEN
+                  ALLOCATE( values_real32(files(f)%dimensions(d)%bounds(1): &
+                                          files(f)%dimensions(d)%bounds(2)) )
+                  values_real32 = files(f)%dimensions(d)%values_real32
+                  values_real32_pointer => values_real32
+                  return_value = dom_write_var( files(f)%name, files(f)%dimensions(d)%name, &
+                                    bounds_start=(/ files(f)%dimensions(d)%bounds(1) /),    &
+                                    bounds_end  =(/ files(f)%dimensions(d)%bounds(2) /),    &
+                                    values_real32_1d=values_real32_pointer )
+                  DEALLOCATE( values_real32 )
+               ELSEIF ( ALLOCATED( files(f)%dimensions(d)%values_real64 ) )  THEN
+                  ALLOCATE( values_real64(files(f)%dimensions(d)%bounds(1): &
+                                          files(f)%dimensions(d)%bounds(2)) )
+                  values_real64 = files(f)%dimensions(d)%values_real64
+                  values_real64_pointer => values_real64
+                  return_value = dom_write_var( files(f)%name, files(f)%dimensions(d)%name, &
+                                    bounds_start=(/ files(f)%dimensions(d)%bounds(1) /),    &
+                                    bounds_end  =(/ files(f)%dimensions(d)%bounds(2) /),    &
+                                    values_real64_1d=values_real64_pointer )
+                  DEALLOCATE( values_real64 )
+               ELSEIF ( ALLOCATED( files(f)%dimensions(d)%values_realwp ) )  THEN
+                  ALLOCATE( values_realwp(files(f)%dimensions(d)%bounds(1): &
+                                          files(f)%dimensions(d)%bounds(2)) )
+                  values_realwp = files(f)%dimensions(d)%values_realwp
+                  values_realwp_pointer => values_realwp
+                  return_value = dom_write_var( files(f)%name, files(f)%dimensions(d)%name, &
+                                    bounds_start=(/ files(f)%dimensions(d)%bounds(1) /),    &
+                                    bounds_end  =(/ files(f)%dimensions(d)%bounds(2) /),    &
+                                    values_realwp_1d=values_realwp_pointer )
+                  DEALLOCATE( values_realwp )
+               ENDIF
+               IF ( return_value /= 0 )  EXIT
+            ENDDO
+
+         ENDIF
+
+         IF ( return_value /= 0 )  EXIT
+
+      ENDDO
+   ENDIF
+
+   CALL internal_message( 'debug', routine_name // ': finished' )
+
+END FUNCTION dom_def_end
+
+!--------------------------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Write variable to file.
+!> Example call:
+!>   dom_write_var( file_name = 'my_output_file_name', &
+!>                  name = 'u', &
+!>                  bounds_start = (/nxl, nys, nzb, time_step/), &
+!>                  bounds_end = (/nxr, nyn, nzt, time_step/), &
+!>                  values_real64_3d = u )
+!> @note The order of dimension bounds must match to the order of dimensions given in call
+!>       'dom_def_var'. I.e., the corresponding variable definition should be like:
+!>          dom_def_var( file_name =  'my_output_file_name', &
+!>                       name = 'u', &
+!>                       dimension_names = (/'x   ', 'y   ', 'z   ', 'time'/), &
+!>                       output_type = <desired-output-type> )
+!> @note The values given do not need to be of the same data type as was defined in the
+!>       corresponding 'dom_def_var' call. If the output format 'netcdf' was chosen, the values are
+!>       automatically converted to the data type given during the definition. If 'binary' was
+!>       chosen, the values are written to file as given in the 'dom_write_var' call.
+!--------------------------------------------------------------------------------------------------!
+FUNCTION dom_write_var( file_name, variable_name, bounds_start, bounds_end,         &
+            values_int8_0d,   values_int8_1d,   values_int8_2d,   values_int8_3d,   &
+            values_int16_0d,  values_int16_1d,  values_int16_2d,  values_int16_3d,  &
+            values_int32_0d,  values_int32_1d,  values_int32_2d,  values_int32_3d,  &
+            values_intwp_0d,  values_intwp_1d,  values_intwp_2d,  values_intwp_3d,  &
+            values_real32_0d, values_real32_1d, values_real32_2d, values_real32_3d, &
+            values_real64_0d, values_real64_1d, values_real64_2d, values_real64_3d, &
+            values_realwp_0d, values_realwp_1d, values_realwp_2d, values_realwp_3d  &
+            ) RESULT( return_value )
+
+   CHARACTER(LEN=charlen)            ::  file_format    !< file format chosen for file
+   CHARACTER(LEN=*),      INTENT(IN) ::  file_name      !< name of file
+   CHARACTER(LEN=*),      INTENT(IN) ::  variable_name  !< name of variable
+
+   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_write_var'  !< name of routine
+
+   INTEGER ::  file_id              !< file ID
+   INTEGER ::  i                    !< loop index
+   INTEGER ::  j                    !< loop index
+   INTEGER ::  k                    !< loop index
+   INTEGER ::  output_return_value  !< return value of a called output routine
+   INTEGER ::  return_value         !< return value
+   INTEGER ::  variable_id          !< variable ID
+
+   INTEGER, DIMENSION(:),   INTENT(IN)  ::  bounds_end             !< end index per dimension of variable
+   INTEGER, DIMENSION(:),   INTENT(IN)  ::  bounds_start           !< start index per dimension of variable
+   INTEGER, DIMENSION(:),   ALLOCATABLE ::  bounds_origin          !< first index of each dimension
+   INTEGER, DIMENSION(:),   ALLOCATABLE ::  bounds_start_internal  !< start index per dim. for output after masking
+   INTEGER, DIMENSION(:),   ALLOCATABLE ::  value_counts           !< count of indices to be written per dimension
+   INTEGER, DIMENSION(:,:), ALLOCATABLE ::  masked_indices         !< list containing all output indices along a dimension
+
+   LOGICAL ::  do_output  !< true if any data lies within given range of masked dimension
+   LOGICAL ::  is_global  !< true if variable is global
+
+   INTEGER(KIND=1), POINTER, INTENT(IN), OPTIONAL                   ::  values_int8_0d             !< output variable
+   INTEGER(KIND=2), POINTER, INTENT(IN), OPTIONAL                   ::  values_int16_0d            !< output variable
+   INTEGER(KIND=4), POINTER, INTENT(IN), OPTIONAL                   ::  values_int32_0d            !< output variable
+   INTEGER(iwp),    POINTER, INTENT(IN), OPTIONAL                   ::  values_intwp_0d            !< output variable
+   INTEGER(KIND=1), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_int8_1d             !< output variable
+   INTEGER(KIND=2), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_int16_1d            !< output variable
+   INTEGER(KIND=4), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_int32_1d            !< output variable
+   INTEGER(iwp),    POINTER, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_intwp_1d            !< output variable
+   INTEGER(KIND=1), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_int8_2d             !< output variable
+   INTEGER(KIND=2), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_int16_2d            !< output variable
+   INTEGER(KIND=4), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_int32_2d            !< output variable
+   INTEGER(iwp),    POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_intwp_2d            !< output variable
+   INTEGER(KIND=1), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_int8_3d             !< output variable
+   INTEGER(KIND=2), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_int16_3d            !< output variable
+   INTEGER(KIND=4), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_int32_3d            !< output variable
+   INTEGER(iwp),    POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_intwp_3d            !< output variable
+
+   INTEGER(KIND=1), TARGET, ALLOCATABLE, DIMENSION(:)               ::  values_int8_1d_resorted    !< resorted output variable
+   INTEGER(KIND=2), TARGET, ALLOCATABLE, DIMENSION(:)               ::  values_int16_1d_resorted   !< resorted output variable
+   INTEGER(KIND=4), TARGET, ALLOCATABLE, DIMENSION(:)               ::  values_int32_1d_resorted   !< resorted output variable
+   INTEGER(iwp),    TARGET, ALLOCATABLE, DIMENSION(:)               ::  values_intwp_1d_resorted   !< resorted output variable
+   INTEGER(KIND=1), TARGET, ALLOCATABLE, DIMENSION(:,:)             ::  values_int8_2d_resorted    !< resorted output variable
+   INTEGER(KIND=2), TARGET, ALLOCATABLE, DIMENSION(:,:)             ::  values_int16_2d_resorted   !< resorted output variable
+   INTEGER(KIND=4), TARGET, ALLOCATABLE, DIMENSION(:,:)             ::  values_int32_2d_resorted   !< resorted output variable
+   INTEGER(iwp),    TARGET, ALLOCATABLE, DIMENSION(:,:)             ::  values_intwp_2d_resorted   !< resorted output variable
+   INTEGER(KIND=1), TARGET, ALLOCATABLE, DIMENSION(:,:,:)           ::  values_int8_3d_resorted    !< resorted output variable
+   INTEGER(KIND=2), TARGET, ALLOCATABLE, DIMENSION(:,:,:)           ::  values_int16_3d_resorted   !< resorted output variable
+   INTEGER(KIND=4), TARGET, ALLOCATABLE, DIMENSION(:,:,:)           ::  values_int32_3d_resorted   !< resorted output variable
+   INTEGER(iwp),    TARGET, ALLOCATABLE, DIMENSION(:,:,:)           ::  values_intwp_3d_resorted   !< resorted output variable
+
+   INTEGER(KIND=1), POINTER                                         ::  values_int8_0d_pointer     !< pointer to resortet array
+   INTEGER(KIND=2), POINTER                                         ::  values_int16_0d_pointer    !< pointer to resortet array
+   INTEGER(KIND=4), POINTER                                         ::  values_int32_0d_pointer    !< pointer to resortet array
+   INTEGER(iwp),    POINTER                                         ::  values_intwp_0d_pointer    !< pointer to resortet array
+   INTEGER(KIND=1), POINTER, CONTIGUOUS, DIMENSION(:)               ::  values_int8_1d_pointer     !< pointer to resortet array
+   INTEGER(KIND=2), POINTER, CONTIGUOUS, DIMENSION(:)               ::  values_int16_1d_pointer    !< pointer to resortet array
+   INTEGER(KIND=4), POINTER, CONTIGUOUS, DIMENSION(:)               ::  values_int32_1d_pointer    !< pointer to resortet array
+   INTEGER(iwp),    POINTER, CONTIGUOUS, DIMENSION(:)               ::  values_intwp_1d_pointer    !< pointer to resortet array
+   INTEGER(KIND=1), POINTER, CONTIGUOUS, DIMENSION(:,:)             ::  values_int8_2d_pointer     !< pointer to resortet array
+   INTEGER(KIND=2), POINTER, CONTIGUOUS, DIMENSION(:,:)             ::  values_int16_2d_pointer    !< pointer to resortet array
+   INTEGER(KIND=4), POINTER, CONTIGUOUS, DIMENSION(:,:)             ::  values_int32_2d_pointer    !< pointer to resortet array
+   INTEGER(iwp),    POINTER, CONTIGUOUS, DIMENSION(:,:)             ::  values_intwp_2d_pointer    !< pointer to resortet array
+   INTEGER(KIND=1), POINTER, CONTIGUOUS, DIMENSION(:,:,:)           ::  values_int8_3d_pointer     !< pointer to resortet array
+   INTEGER(KIND=2), POINTER, CONTIGUOUS, DIMENSION(:,:,:)           ::  values_int16_3d_pointer    !< pointer to resortet array
+   INTEGER(KIND=4), POINTER, CONTIGUOUS, DIMENSION(:,:,:)           ::  values_int32_3d_pointer    !< pointer to resortet array
+   INTEGER(iwp),    POINTER, CONTIGUOUS, DIMENSION(:,:,:)           ::  values_intwp_3d_pointer    !< pointer to resortet array
+
+   REAL(KIND=4), POINTER, INTENT(IN), OPTIONAL                      ::  values_real32_0d           !< output variable
+   REAL(KIND=8), POINTER, INTENT(IN), OPTIONAL                      ::  values_real64_0d           !< output variable
+   REAL(wp),     POINTER, INTENT(IN), OPTIONAL                      ::  values_realwp_0d           !< output variable
+   REAL(KIND=4), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:)        ::  values_real32_1d           !< output variable
+   REAL(KIND=8), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:)        ::  values_real64_1d           !< output variable
+   REAL(wp),     POINTER, INTENT(IN), OPTIONAL, DIMENSION(:)        ::  values_realwp_1d           !< output variable
+   REAL(KIND=4), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:)      ::  values_real32_2d           !< output variable
+   REAL(KIND=8), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:)      ::  values_real64_2d           !< output variable
+   REAL(wp),     POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:)      ::  values_realwp_2d           !< output variable
+   REAL(KIND=4), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:,:)    ::  values_real32_3d           !< output variable
+   REAL(KIND=8), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:,:)    ::  values_real64_3d           !< output variable
+   REAL(wp),     POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:,:)    ::  values_realwp_3d           !< output variable
+
+   REAL(KIND=4), TARGET, ALLOCATABLE, DIMENSION(:)                  ::  values_real32_1d_resorted  !< resorted output variable
+   REAL(KIND=8), TARGET, ALLOCATABLE, DIMENSION(:)                  ::  values_real64_1d_resorted  !< resorted output variable
+   REAL(wp),     TARGET, ALLOCATABLE, DIMENSION(:)                  ::  values_realwp_1d_resorted  !< resorted output variable
+   REAL(KIND=4), TARGET, ALLOCATABLE, DIMENSION(:,:)                ::  values_real32_2d_resorted  !< resorted output variable
+   REAL(KIND=8), TARGET, ALLOCATABLE, DIMENSION(:,:)                ::  values_real64_2d_resorted  !< resorted output variable
+   REAL(wp),     TARGET, ALLOCATABLE, DIMENSION(:,:)                ::  values_realwp_2d_resorted  !< resorted output variable
+   REAL(KIND=4), TARGET, ALLOCATABLE, DIMENSION(:,:,:)              ::  values_real32_3d_resorted  !< resorted output variable
+   REAL(KIND=8), TARGET, ALLOCATABLE, DIMENSION(:,:,:)              ::  values_real64_3d_resorted  !< resorted output variable
+   REAL(wp),     TARGET, ALLOCATABLE, DIMENSION(:,:,:)              ::  values_realwp_3d_resorted  !< resorted output variable
+
+   REAL(KIND=4), POINTER                                            ::  values_real32_0d_pointer   !< pointer to resortet array
+   REAL(KIND=8), POINTER                                            ::  values_real64_0d_pointer   !< pointer to resortet array
+   REAL(wp),     POINTER                                            ::  values_realwp_0d_pointer   !< pointer to resortet array
+   REAL(KIND=4), POINTER, CONTIGUOUS, DIMENSION(:)                  ::  values_real32_1d_pointer   !< pointer to resortet array
+   REAL(KIND=8), POINTER, CONTIGUOUS, DIMENSION(:)                  ::  values_real64_1d_pointer   !< pointer to resortet array
+   REAL(wp),     POINTER, CONTIGUOUS, DIMENSION(:)                  ::  values_realwp_1d_pointer   !< pointer to resortet array
+   REAL(KIND=4), POINTER, CONTIGUOUS, DIMENSION(:,:)                ::  values_real32_2d_pointer   !< pointer to resortet array
+   REAL(KIND=8), POINTER, CONTIGUOUS, DIMENSION(:,:)                ::  values_real64_2d_pointer   !< pointer to resortet array
+   REAL(wp),     POINTER, CONTIGUOUS, DIMENSION(:,:)                ::  values_realwp_2d_pointer   !< pointer to resortet array
+   REAL(KIND=4), POINTER, CONTIGUOUS, DIMENSION(:,:,:)              ::  values_real32_3d_pointer   !< pointer to resortet array
+   REAL(KIND=8), POINTER, CONTIGUOUS, DIMENSION(:,:,:)              ::  values_real64_3d_pointer   !< pointer to resortet array
+   REAL(wp),     POINTER, CONTIGUOUS, DIMENSION(:,:,:)              ::  values_realwp_3d_pointer   !< pointer to resortet array
+
+   TYPE(dimension_type), DIMENSION(:), ALLOCATABLE ::  dimension_list  !< list of used dimensions of variable
+
+
+   return_value = 0
+   output_return_value = 0
+
+   CALL internal_message( 'debug', routine_name // ': write ' // TRIM( variable_name ) // &
+                          ' into file ' // TRIM( file_name ) )
+
+   !-- Search for variable within file
+   CALL find_var_in_file( file_name, variable_name, file_format, file_id, variable_id, &
+                          is_global, dimension_list, return_value=return_value  )
+
+   IF ( return_value == 0 )  THEN
+
+      !-- Check if the correct amount of variable bounds were given
+      IF ( SIZE( bounds_start ) /= SIZE( dimension_list )  .OR.  &
+           SIZE( bounds_end ) /= SIZE( dimension_list ) )  THEN
+         return_value = 1
+         CALL internal_message( 'error', routine_name //                  &
+                                ': number bounds do not match with ' //   &
+                                'number of dimensions of variable ' //    &
+                                '(variable "' // TRIM( variable_name ) // &
+                                '", file "' // TRIM( file_name ) // '")!' )
+      ENDIF
+
+   ENDIF
+
+   IF ( return_value == 0 )  THEN
+
+      !-- Save starting index (lower bounds) of each dimension
+      ALLOCATE( bounds_origin(SIZE( dimension_list )) )
+      ALLOCATE( bounds_start_internal(SIZE( dimension_list )) )
+      ALLOCATE( value_counts(SIZE( dimension_list )) )
+
+      WRITE( temp_string, * ) bounds_start
+      CALL internal_message( 'debug', routine_name //                    &
+                             ': file "' // TRIM( file_name ) //          &
+                             '", variable "' // TRIM( variable_name ) // &
+                             '", bounds_start =' // TRIM( temp_string ) )
+      WRITE( temp_string, * ) bounds_end
+      CALL internal_message( 'debug', routine_name //                    &
+                             ': file "' // TRIM( file_name ) //          &
+                             '", variable "' // TRIM( variable_name ) // &
+                             '", bounds_end =' // TRIM( temp_string ) )
+
+      !-- Get bounds for masking
+      CALL get_masked_indices_and_masked_dimension_bounds( dimension_list,                  &
+              bounds_start, bounds_end, bounds_start_internal, value_counts, bounds_origin, &
+              masked_indices )
+
+      do_output = .NOT. ANY( value_counts == 0 )
+
+      WRITE( temp_string, * ) bounds_start_internal
+      CALL internal_message( 'debug', routine_name //                    &
+                             ': file "' // TRIM( file_name ) //          &
+                             '", variable "' // TRIM( variable_name ) // &
+                             '", bounds_start_internal =' // TRIM( temp_string ) )
+      WRITE( temp_string, * ) value_counts
+      CALL internal_message( 'debug', routine_name //                    &
+                             ': file "' // TRIM( file_name ) //          &
+                             '", variable "' // TRIM( variable_name ) // &
+                             '", value_counts =' // TRIM( temp_string ) )
+
+      !-- Mask and resort variable
+      !-- 8bit integer output
+      IF ( PRESENT( values_int8_0d ) )  THEN
+         values_int8_0d_pointer => values_int8_0d
+      ELSEIF ( PRESENT( values_int8_1d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_int8_1d_resorted(0:value_counts(1)-1) )
+            !$OMP PARALLEL PRIVATE (i)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               values_int8_1d_resorted(i) = values_int8_1d(masked_indices(1,i))
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_int8_1d_resorted(1) )
+            values_int8_1d_resorted = 0_1
+         ENDIF
+         values_int8_1d_pointer => values_int8_1d_resorted
+      ELSEIF ( PRESENT( values_int8_2d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_int8_2d_resorted(0:value_counts(1)-1, &
+                                              0:value_counts(2)-1) )
+            !$OMP PARALLEL PRIVATE (i,j)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               DO  j = 0, value_counts(2) - 1
+                  values_int8_2d_resorted(i,j) = values_int8_2d(masked_indices(2,j), &
+                                                                masked_indices(1,i)  )
+               ENDDO
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_int8_2d_resorted(1,1) )
+            values_int8_2d_resorted = 0_1
+         ENDIF
+         values_int8_2d_pointer => values_int8_2d_resorted
+      ELSEIF ( PRESENT( values_int8_3d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_int8_3d_resorted(0:value_counts(1)-1, &
+                                              0:value_counts(2)-1, &
+                                              0:value_counts(3)-1) )
+            !$OMP PARALLEL PRIVATE (i,j,k)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               DO  j = 0, value_counts(2) - 1
+                  DO  k = 0, value_counts(3) - 1
+                     values_int8_3d_resorted(i,j,k) = values_int8_3d(masked_indices(3,k), &
+                                                                     masked_indices(2,j), &
+                                                                     masked_indices(1,i)  )
+                  ENDDO
+               ENDDO
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_int8_3d_resorted(1,1,1) )
+            values_int8_3d_resorted = 0_1
+         ENDIF
+         values_int8_3d_pointer => values_int8_3d_resorted
+
+      !-- 16bit integer output
+      ELSEIF ( PRESENT( values_int16_0d ) )  THEN
+         values_int16_0d_pointer => values_int16_0d
+      ELSEIF ( PRESENT( values_int16_1d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_int16_1d_resorted(0:value_counts(1)-1) )
+            !$OMP PARALLEL PRIVATE (i)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               values_int16_1d_resorted(i) = values_int16_1d(masked_indices(1,i))
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_int16_1d_resorted(1) )
+            values_int16_1d_resorted = 0_1
+         ENDIF
+         values_int16_1d_pointer => values_int16_1d_resorted
+      ELSEIF ( PRESENT( values_int16_2d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_int16_2d_resorted(0:value_counts(1)-1, &
+                                               0:value_counts(2)-1) )
+            !$OMP PARALLEL PRIVATE (i,j)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               DO  j = 0, value_counts(2) - 1
+                  values_int16_2d_resorted(i,j) = values_int16_2d(masked_indices(2,j), &
+                                                                  masked_indices(1,i))
+               ENDDO
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_int16_2d_resorted(1,1) )
+            values_int16_2d_resorted = 0_1
+         ENDIF
+         values_int16_2d_pointer => values_int16_2d_resorted
+      ELSEIF ( PRESENT( values_int16_3d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_int16_3d_resorted(0:value_counts(1)-1, &
+                                               0:value_counts(2)-1, &
+                                               0:value_counts(3)-1) )
+            !$OMP PARALLEL PRIVATE (i,j,k)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               DO  j = 0, value_counts(2) - 1
+                  DO  k = 0, value_counts(3) - 1
+                     values_int16_3d_resorted(i,j,k) = values_int16_3d(masked_indices(3,k), &
+                                                                       masked_indices(2,j), &
+                                                                       masked_indices(1,i)  )
+                  ENDDO
+               ENDDO
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_int16_3d_resorted(1,1,1) )
+            values_int16_3d_resorted = 0_1
+         ENDIF
+         values_int16_3d_pointer => values_int16_3d_resorted
+
+      !-- 32bit integer output
+      ELSEIF ( PRESENT( values_int32_0d ) )  THEN
+         values_int32_0d_pointer => values_int32_0d
+      ELSEIF ( PRESENT( values_int32_1d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_int32_1d_resorted(0:value_counts(1)-1) )
+            !$OMP PARALLEL PRIVATE (i)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               values_int32_1d_resorted(i) = values_int32_1d(masked_indices(1,i))
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_int32_1d_resorted(1) )
+            values_int32_1d_resorted = 0_1
+         ENDIF
+         values_int32_1d_pointer => values_int32_1d_resorted
+      ELSEIF ( PRESENT( values_int32_2d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_int32_2d_resorted(0:value_counts(1)-1, &
+                                               0:value_counts(2)-1) )
+            !$OMP PARALLEL PRIVATE (i,j)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               DO  j = 0, value_counts(2) - 1
+                  values_int32_2d_resorted(i,j) = values_int32_2d(masked_indices(2,j), &
+                                                                  masked_indices(1,i)  )
+               ENDDO
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_int32_2d_resorted(1,1) )
+            values_int32_2d_resorted = 0_1
+         ENDIF
+         values_int32_2d_pointer => values_int32_2d_resorted
+      ELSEIF ( PRESENT( values_int32_3d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_int32_3d_resorted(0:value_counts(1)-1, &
+                                               0:value_counts(2)-1, &
+                                               0:value_counts(3)-1) )
+            !$OMP PARALLEL PRIVATE (i,j,k)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               DO  j = 0, value_counts(2) - 1
+                  DO  k = 0, value_counts(3) - 1
+                     values_int32_3d_resorted(i,j,k) = values_int32_3d(masked_indices(3,k), &
+                                                                       masked_indices(2,j), &
+                                                                       masked_indices(1,i)  )
+                  ENDDO
+               ENDDO
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_int32_3d_resorted(1,1,1) )
+            values_int32_3d_resorted = 0_1
+         ENDIF
+         values_int32_3d_pointer => values_int32_3d_resorted
+
+      !-- working-precision integer output
+      ELSEIF ( PRESENT( values_intwp_0d ) )  THEN
+         values_intwp_0d_pointer => values_intwp_0d
+      ELSEIF ( PRESENT( values_intwp_1d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_intwp_1d_resorted(0:value_counts(1)-1) )
+            !$OMP PARALLEL PRIVATE (i)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               values_intwp_1d_resorted(i) = values_intwp_1d(masked_indices(1,i))
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_intwp_1d_resorted(1) )
+            values_intwp_1d_resorted = 0_1
+         ENDIF
+         values_intwp_1d_pointer => values_intwp_1d_resorted
+      ELSEIF ( PRESENT( values_intwp_2d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_intwp_2d_resorted(0:value_counts(1)-1, &
+                                               0:value_counts(2)-1) )
+            !$OMP PARALLEL PRIVATE (i,j)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               DO  j = 0, value_counts(2) - 1
+                  values_intwp_2d_resorted(i,j) = values_intwp_2d(masked_indices(2,j), &
+                                                                  masked_indices(1,i)  )
+               ENDDO
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_intwp_2d_resorted(1,1) )
+            values_intwp_2d_resorted = 0_1
+         ENDIF
+         values_intwp_2d_pointer => values_intwp_2d_resorted
+      ELSEIF ( PRESENT( values_intwp_3d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_intwp_3d_resorted(0:value_counts(1)-1, &
+                                               0:value_counts(2)-1, &
+                                               0:value_counts(3)-1) )
+            !$OMP PARALLEL PRIVATE (i,j,k)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               DO  j = 0, value_counts(2) - 1
+                  DO  k = 0, value_counts(3) - 1
+                     values_intwp_3d_resorted(i,j,k) = values_intwp_3d(masked_indices(3,k), &
+                                                                       masked_indices(2,j), &
+                                                                       masked_indices(1,i)  )
+                  ENDDO
+               ENDDO
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_intwp_3d_resorted(1,1,1) )
+            values_intwp_3d_resorted = 0_1
+         ENDIF
+         values_intwp_3d_pointer => values_intwp_3d_resorted
+
+      !-- 32bit real output
+      ELSEIF ( PRESENT( values_real32_0d ) )  THEN
+         values_real32_0d_pointer => values_real32_0d
+      ELSEIF ( PRESENT( values_real32_1d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_real32_1d_resorted(0:value_counts(1)-1) )
+            !$OMP PARALLEL PRIVATE (i)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               values_real32_1d_resorted(i) = values_real32_1d(masked_indices(1,i))
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_real32_1d_resorted(1) )
+            values_real32_1d_resorted = 0_1
+         ENDIF
+         values_real32_1d_pointer => values_real32_1d_resorted
+      ELSEIF ( PRESENT( values_real32_2d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_real32_2d_resorted(0:value_counts(1)-1, &
+                                                0:value_counts(2)-1) )
+            !$OMP PARALLEL PRIVATE (i,j)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               DO  j = 0, value_counts(2) - 1
+                  values_real32_2d_resorted(i,j) = values_real32_2d(masked_indices(2,j), &
+                                                                    masked_indices(1,i)  )
+               ENDDO
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_real32_2d_resorted(1,1) )
+            values_real32_2d_resorted = 0_1
+         ENDIF
+         values_real32_2d_pointer => values_real32_2d_resorted
+      ELSEIF ( PRESENT( values_real32_3d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_real32_3d_resorted(0:value_counts(1)-1, &
+                                                0:value_counts(2)-1, &
+                                                0:value_counts(3)-1) )
+            !$OMP PARALLEL PRIVATE (i,j,k)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               DO  j = 0, value_counts(2) - 1
+                  DO  k = 0, value_counts(3) - 1
+                     values_real32_3d_resorted(i,j,k) = values_real32_3d(masked_indices(3,k), &
+                                                                         masked_indices(2,j), &
+                                                                         masked_indices(1,i)  )
+                  ENDDO
+               ENDDO
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_real32_3d_resorted(1,1,1) )
+            values_real32_3d_resorted = 0_1
+         ENDIF
+         values_real32_3d_pointer => values_real32_3d_resorted
+
+      !-- 64bit real output
+      ELSEIF ( PRESENT( values_real64_0d ) )  THEN
+         values_real64_0d_pointer => values_real64_0d
+      ELSEIF ( PRESENT( values_real64_1d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_real64_1d_resorted(0:value_counts(1)-1) )
+            !$OMP PARALLEL PRIVATE (i)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               values_real64_1d_resorted(i) = values_real64_1d(masked_indices(1,i))
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_real64_1d_resorted(1) )
+            values_real64_1d_resorted = 0_1
+         ENDIF
+         values_real64_1d_pointer => values_real64_1d_resorted
+      ELSEIF ( PRESENT( values_real64_2d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_real64_2d_resorted(0:value_counts(1)-1, &
+                                                0:value_counts(2)-1) )
+            !$OMP PARALLEL PRIVATE (i,j)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               DO  j = 0, value_counts(2) - 1
+                  values_real64_2d_resorted(i,j) = values_real64_2d(masked_indices(2,j), &
+                                                                    masked_indices(1,i)  )
+               ENDDO
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_real64_2d_resorted(1,1) )
+            values_real64_2d_resorted = 0_1
+         ENDIF
+         values_real64_2d_pointer => values_real64_2d_resorted
+      ELSEIF ( PRESENT( values_real64_3d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_real64_3d_resorted(0:value_counts(1)-1, &
+                                                0:value_counts(2)-1, &
+                                                0:value_counts(3)-1) )
+            !$OMP PARALLEL PRIVATE (i,j,k)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               DO  j = 0, value_counts(2) - 1
+                  DO  k = 0, value_counts(3) - 1
+                     values_real64_3d_resorted(i,j,k) = values_real64_3d(masked_indices(3,k), &
+                                                                         masked_indices(2,j), &
+                                                                         masked_indices(1,i)  )
+                  ENDDO
+               ENDDO
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_real64_3d_resorted(1,1,1) )
+            values_real64_3d_resorted = 0_1
+         ENDIF
+         values_real64_3d_pointer => values_real64_3d_resorted
+
+      !-- working-precision real output
+      ELSEIF ( PRESENT( values_realwp_0d ) )  THEN
+         values_realwp_0d_pointer => values_realwp_0d
+      ELSEIF ( PRESENT( values_realwp_1d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_realwp_1d_resorted(0:value_counts(1)-1) )
+            !$OMP PARALLEL PRIVATE (i)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               values_realwp_1d_resorted(i) = values_realwp_1d(masked_indices(1,i))
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_realwp_1d_resorted(1) )
+            values_realwp_1d_resorted = 0_1
+         ENDIF
+         values_realwp_1d_pointer => values_realwp_1d_resorted
+      ELSEIF ( PRESENT( values_realwp_2d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_realwp_2d_resorted(0:value_counts(1)-1, &
+                                                0:value_counts(2)-1) )
+            !$OMP PARALLEL PRIVATE (i,j)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               DO  j = 0, value_counts(2) - 1
+                  values_realwp_2d_resorted(i,j) = values_realwp_2d(masked_indices(2,j), &
+                                                                    masked_indices(1,i)  )
+               ENDDO
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_realwp_2d_resorted(1,1) )
+            values_realwp_2d_resorted = 0_1
+         ENDIF
+         values_realwp_2d_pointer => values_realwp_2d_resorted
+      ELSEIF ( PRESENT( values_realwp_3d ) )  THEN
+         IF ( do_output ) THEN
+            ALLOCATE( values_realwp_3d_resorted(0:value_counts(1)-1, &
+                                                0:value_counts(2)-1, &
+                                                0:value_counts(3)-1) )
+            !$OMP PARALLEL PRIVATE (i,j,k)
+            !$OMP DO
+            DO  i = 0, value_counts(1) - 1
+               DO  j = 0, value_counts(2) - 1
+                  DO  k = 0, value_counts(3) - 1
+                     values_realwp_3d_resorted(i,j,k) = values_realwp_3d(masked_indices(3,k), &
+                                                                         masked_indices(2,j), &
+                                                                         masked_indices(1,i)  )
+                  ENDDO
+               ENDDO
+            ENDDO
+            !$OMP END PARALLEL
+         ELSE
+            ALLOCATE( values_realwp_3d_resorted(1,1,1) )
+            values_realwp_3d_resorted = 0_1
+         ENDIF
+         values_realwp_3d_pointer => values_realwp_3d_resorted
+
       ELSE
-         return_value = dom_def_att_save( TRIM( filename ), &
-                                                attribute=attribute, append=append_internal )
+         return_value = 1
+         CALL internal_message( 'error', routine_name //                  &
+                                ': no output values given ' //            &
+                                '(variable "' // TRIM( variable_name ) // &
+                                '", file "' // TRIM( file_name ) // '")!'  )
       ENDIF
+
+      DEALLOCATE( masked_indices )
+
+   ENDIF  ! Check for error
+
+   IF ( return_value == 0 )  THEN
+
+      !-- Write variable into file
+      SELECT CASE ( TRIM( file_format ) )
+
+         CASE ( 'binary' )
+            !-- 8bit integer output
+            IF ( PRESENT( values_int8_0d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int8_0d=values_int8_0d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int8_1d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int8_1d=values_int8_1d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int8_2d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int8_2d=values_int8_2d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int8_3d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int8_3d=values_int8_3d_pointer, return_value=output_return_value )
+            !-- 16bit integer output
+            ELSEIF ( PRESENT( values_int16_0d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int16_0d=values_int16_0d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int16_1d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int16_1d=values_int16_1d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int16_2d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int16_2d=values_int16_2d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int16_3d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int16_3d=values_int16_3d_pointer, return_value=output_return_value )
+            !-- 32bit integer output
+            ELSEIF ( PRESENT( values_int32_0d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int32_0d=values_int32_0d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int32_1d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int32_1d=values_int32_1d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int32_2d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int32_2d=values_int32_2d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int32_3d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int32_3d=values_int32_3d_pointer, return_value=output_return_value )
+            !-- working-precision integer output
+            ELSEIF ( PRESENT( values_intwp_0d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_intwp_0d=values_intwp_0d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_intwp_1d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_intwp_1d=values_intwp_1d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_intwp_2d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_intwp_2d=values_intwp_2d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_intwp_3d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_intwp_3d=values_intwp_3d_pointer, return_value=output_return_value )
+            !-- 32bit real output
+            ELSEIF ( PRESENT( values_real32_0d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_real32_0d=values_real32_0d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_real32_1d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_real32_1d=values_real32_1d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_real32_2d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_real32_2d=values_real32_2d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_real32_3d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_real32_3d=values_real32_3d_pointer, return_value=output_return_value )
+            !-- 64bit real output
+            ELSEIF ( PRESENT( values_real64_0d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_real64_0d=values_real64_0d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_real64_1d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_real64_1d=values_real64_1d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_real64_2d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_real64_2d=values_real64_2d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_real64_3d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_real64_3d=values_real64_3d_pointer, return_value=output_return_value )
+            !-- working-precision real output
+            ELSEIF ( PRESENT( values_realwp_0d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_realwp_0d=values_realwp_0d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_realwp_1d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_realwp_1d=values_realwp_1d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_realwp_2d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_realwp_2d=values_realwp_2d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_realwp_3d ) )  THEN
+               CALL binary_write_variable( file_id, variable_id,                      &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_realwp_3d=values_realwp_3d_pointer, return_value=output_return_value )
+            ELSE
+               return_value = 1
+               CALL internal_message( 'error', routine_name //                          &
+                                      ': output_type not supported by file format "' // &
+                                      TRIM( file_format ) // '" ' //                    &
+                                      '(variable "' // TRIM( variable_name ) //         &
+                                      '", file "' // TRIM( file_name ) // '")!' )
+            ENDIF
+
+         CASE ( 'netcdf4-parallel', 'netcdf4-serial' )
+            !-- 8bit integer output
+            IF ( PRESENT( values_int8_0d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int8_0d=values_int8_0d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int8_1d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int8_1d=values_int8_1d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int8_2d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int8_2d=values_int8_2d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int8_3d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int8_3d=values_int8_3d_pointer, return_value=output_return_value )
+            !-- 16bit integer output
+            ELSEIF ( PRESENT( values_int16_0d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int16_0d=values_int16_0d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int16_1d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int16_1d=values_int16_1d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int16_2d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int16_2d=values_int16_2d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int16_3d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int16_3d=values_int16_3d_pointer, return_value=output_return_value )
+            !-- 32bit integer output
+            ELSEIF ( PRESENT( values_int32_0d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int32_0d=values_int32_0d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int32_1d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int32_1d=values_int32_1d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int32_2d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int32_2d=values_int32_2d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_int32_3d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_int32_3d=values_int32_3d_pointer, return_value=output_return_value )
+            !-- working-precision integer output
+            ELSEIF ( PRESENT( values_intwp_0d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_intwp_0d=values_intwp_0d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_intwp_1d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_intwp_1d=values_intwp_1d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_intwp_2d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_intwp_2d=values_intwp_2d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_intwp_3d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_intwp_3d=values_intwp_3d_pointer, return_value=output_return_value )
+            !-- 32bit real output
+            ELSEIF ( PRESENT( values_real32_0d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_real32_0d=values_real32_0d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_real32_1d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_real32_1d=values_real32_1d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_real32_2d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_real32_2d=values_real32_2d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_real32_3d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_real32_3d=values_real32_3d_pointer, return_value=output_return_value )
+            !-- 64bit real output
+            ELSEIF ( PRESENT( values_real64_0d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_real64_0d=values_real64_0d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_real64_1d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_real64_1d=values_real64_1d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_real64_2d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_real64_2d=values_real64_2d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_real64_3d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_real64_3d=values_real64_3d_pointer, return_value=output_return_value )
+            !-- working-precision real output
+            ELSEIF ( PRESENT( values_realwp_0d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_realwp_0d=values_realwp_0d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_realwp_1d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_realwp_1d=values_realwp_1d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_realwp_2d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_realwp_2d=values_realwp_2d_pointer, return_value=output_return_value )
+            ELSEIF ( PRESENT( values_realwp_3d ) )  THEN
+               CALL netcdf4_write_variable( file_id, variable_id,                     &
+                       bounds_start_internal, value_counts, bounds_origin, is_global, &
+                       values_realwp_3d=values_realwp_3d_pointer, return_value=output_return_value )
+            ELSE
+               return_value = 1
+               CALL internal_message( 'error', routine_name //                          &
+                                      ': output_type not supported by file format "' // &
+                                      TRIM( file_format ) // '" ' //                    &
+                                      '(variable "' // TRIM( variable_name ) //         &
+                                      '", file "' // TRIM( file_name ) // '")!' )
+            ENDIF
+
+         CASE DEFAULT
+            return_value = 1
+            CALL internal_message( 'error', routine_name //                    &
+                                   ': file format "' // TRIM( file_format ) // &
+                                   '" not supported ' //                       &
+                                   '(variable "' // TRIM( variable_name ) //   &
+                                   '", file "' // TRIM( file_name ) // '")!' )
+
+      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 ' //      &
+                                '(variable "' // TRIM( variable_name ) // &
+                                '", file "' // TRIM( file_name ) // '")!' )
+      ENDIF
+
    ENDIF
 
-END FUNCTION dom_def_att_real64
+END FUNCTION dom_write_var
+
+!--------------------------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Finalize output.
+!> All necessary steps are carried out to close all output files. If a file could not be closed,
+!> this is noted in the error message.
+!>
+!> @bug if multiple files failed to be closed, only the last failure is given in the error message.
+!--------------------------------------------------------------------------------------------------!
+FUNCTION dom_finalize_output() RESULT( return_value )
+
+   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_finalize_output'  !< name of routine
+
+   INTEGER ::  f                      !< loop index
+   INTEGER ::  output_return_value    !< return value from called routines
+   INTEGER ::  return_value           !< return value
+   INTEGER ::  return_value_internal  !< error code after closing a single file
+
+
+   return_value = 0
+
+   DO  f = 1, nfiles
+
+      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 ) // '" for file "' // &
+                                   TRIM( files(f)%name ) // '"' )
+         ENDIF
+
+      ENDIF
+
+   ENDDO
+
+END FUNCTION dom_finalize_output
+
+!--------------------------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Return the last created error message.
+!--------------------------------------------------------------------------------------------------!
+FUNCTION dom_get_error_message() RESULT( error_message )
+
+   CHARACTER(LEN=800) ::  error_message  !< return error message to main program
+
+
+   error_message = TRIM( internal_error_message )
+
+   error_message = TRIM( error_message ) // TRIM( binary_get_error_message() )
+   
+   error_message = TRIM( error_message ) // TRIM( netcdf4_get_error_message() )
+   
+   internal_error_message = ''
+
+END FUNCTION dom_get_error_message
 
 !--------------------------------------------------------------------------------------------------!
@@ -1581,16 +2498,17 @@
 !> @todo Try to combine similar code parts and shorten routine.
 !--------------------------------------------------------------------------------------------------!
-FUNCTION dom_def_att_save( filename, variable_name, attribute, append ) RESULT( return_value )
-
-   CHARACTER(LEN=*), INTENT(IN) ::  filename                 !< name of file
-   CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  variable_name  !< name of variable
-
-   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_def_att_save'  !< name of routine
-
-   INTEGER(iwp) ::  a             !< loop index
-   INTEGER(iwp) ::  d             !< loop index
-   INTEGER(iwp) ::  f             !< loop index
-   INTEGER(iwp) ::  natt          !< number of attributes
-   INTEGER(iwp) ::  return_value  !< return value
+FUNCTION save_attribute_in_database( file_name, variable_name, attribute, append ) &
+            RESULT( return_value )
+
+   CHARACTER(LEN=*), INTENT(IN) ::  file_name      !< name of file
+   CHARACTER(LEN=*), INTENT(IN) ::  variable_name  !< name of variable
+
+   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'save_attribute_in_database'  !< name of routine
+
+   INTEGER ::  a             !< loop index
+   INTEGER ::  d             !< loop index
+   INTEGER ::  f             !< loop index
+   INTEGER ::  natts         !< number of attributes
+   INTEGER ::  return_value  !< return value
 
    LOGICAL             ::  found   !< true if variable or dimension of name 'variable_name' found
@@ -1605,22 +2523,16 @@
    found = .FALSE.
 
-   IF ( PRESENT( variable_name ) )  THEN
-      CALL internal_message( 'debug', routine_name //                            &
-                             ': define attribute "' // TRIM( attribute%name ) // &
-                             '" of variable "' // TRIM( variable_name ) //       &
-                             '" in file "' // TRIM( filename ) // '"' )
-   ELSE
-      CALL internal_message( 'debug', routine_name //                            &
-                             ': define attribute "' // TRIM( attribute%name ) // &
-                             '" in file "' // TRIM( filename ) // '"' )
-   ENDIF
-
-   DO  f = 1, nf
-
-      IF ( TRIM( filename ) == files(f)%name )  THEN
+   CALL internal_message( 'debug', routine_name //                            &
+                          ': define attribute "' // TRIM( attribute%name ) // &
+                          '" of variable "' // TRIM( variable_name ) //       &
+                          '" in file "' // TRIM( file_name ) // '"' )
+
+   DO  f = 1, nfiles
+
+      IF ( TRIM( file_name ) == files(f)%name )  THEN
 
          IF ( files(f)%is_init )  THEN
             return_value = 1
-            CALL internal_message( 'error', routine_name // ': file "' // TRIM( filename ) // &
+            CALL internal_message( 'error', routine_name // ': file "' // TRIM( file_name ) // &
                     '" is already initialized. No further attribute definition allowed!' )
             EXIT
@@ -1628,15 +2540,15 @@
 
          !-- Add attribute to file
-         IF ( .NOT. PRESENT( variable_name ) )  THEN
+         IF ( TRIM( variable_name ) == '' )  THEN
 
             !-- Initialize first file attribute
             IF ( .NOT. ALLOCATED( files(f)%attributes ) )  THEN
-               natt = 1
-               ALLOCATE( files(f)%attributes(natt) )
+               natts = 1
+               ALLOCATE( files(f)%attributes(natts) )
             ELSE
-               natt = SIZE( files(f)%attributes )
+               natts = SIZE( files(f)%attributes )
 
                !-- Check if attribute already exists
-               DO  a = 1, natt
+               DO  a = 1, natts
                   IF ( files(f)%attributes(a)%name == attribute%name )  THEN
                      IF ( append )  THEN
@@ -1655,10 +2567,10 @@
                !-- Extend attribute list by 1
                IF ( .NOT. found )  THEN
-                  ALLOCATE( atts_tmp(natt) )
+                  ALLOCATE( atts_tmp(natts) )
                   atts_tmp = files(f)%attributes
                   DEALLOCATE( files(f)%attributes )
-                  natt = natt + 1
-                  ALLOCATE( files(f)%attributes(natt) )
-                  files(f)%attributes(:natt-1) = atts_tmp
+                  natts = natts + 1
+                  ALLOCATE( files(f)%attributes(natts) )
+                  files(f)%attributes(:natts-1) = atts_tmp
                   DEALLOCATE( atts_tmp )
                ENDIF
@@ -1667,5 +2579,5 @@
             !-- Save new attribute to the end of the attribute list
             IF ( .NOT. found )  THEN
-               files(f)%attributes(natt) = attribute
+               files(f)%attributes(natts) = attribute
                found = .TRUE.
             ENDIF
@@ -1684,11 +2596,11 @@
                      IF ( .NOT. ALLOCATED( files(f)%dimensions(d)%attributes ) )  THEN
                         !-- Initialize first attribute
-                        natt = 1
-                        ALLOCATE( files(f)%dimensions(d)%attributes(natt) )
+                        natts = 1
+                        ALLOCATE( files(f)%dimensions(d)%attributes(natts) )
                      ELSE
-                        natt = SIZE( files(f)%dimensions(d)%attributes )
+                        natts = SIZE( files(f)%dimensions(d)%attributes )
 
                         !-- Check if attribute already exists
-                        DO  a = 1, natt
+                        DO  a = 1, natts
                            IF ( files(f)%dimensions(d)%attributes(a)%name == attribute%name ) &
                            THEN
@@ -1709,10 +2621,10 @@
                         !-- Extend attribute list
                         IF ( .NOT. found )  THEN
-                           ALLOCATE( atts_tmp(natt) )
+                           ALLOCATE( atts_tmp(natts) )
                            atts_tmp = files(f)%dimensions(d)%attributes
                            DEALLOCATE( files(f)%dimensions(d)%attributes )
-                           natt = natt + 1
-                           ALLOCATE( files(f)%dimensions(d)%attributes(natt) )
-                           files(f)%dimensions(d)%attributes(:natt-1) = atts_tmp
+                           natts = natts + 1
+                           ALLOCATE( files(f)%dimensions(d)%attributes(natts) )
+                           files(f)%dimensions(d)%attributes(:natts-1) = atts_tmp
                            DEALLOCATE( atts_tmp )
                         ENDIF
@@ -1721,5 +2633,5 @@
                      !-- Add new attribute to database
                      IF ( .NOT. found )  THEN
-                        files(f)%dimensions(d)%attributes(natt) = attribute
+                        files(f)%dimensions(d)%attributes(natts) = attribute
                         found = .TRUE.
                      ENDIF
@@ -1742,11 +2654,11 @@
                      IF ( .NOT. ALLOCATED( files(f)%variables(d)%attributes ) )  THEN
                         !-- Initialize first attribute
-                        natt = 1
-                        ALLOCATE( files(f)%variables(d)%attributes(natt) )
+                        natts = 1
+                        ALLOCATE( files(f)%variables(d)%attributes(natts) )
                      ELSE
-                        natt = SIZE( files(f)%variables(d)%attributes )
+                        natts = SIZE( files(f)%variables(d)%attributes )
 
                         !-- Check if attribute already exists
-                        DO  a = 1, natt
+                        DO  a = 1, natts
                            IF ( files(f)%variables(d)%attributes(a)%name == attribute%name )  &
                            THEN
@@ -1767,10 +2679,10 @@
                         !-- Extend attribute list
                         IF ( .NOT. found )  THEN
-                           ALLOCATE( atts_tmp(natt) )
+                           ALLOCATE( atts_tmp(natts) )
                            atts_tmp = files(f)%variables(d)%attributes
                            DEALLOCATE( files(f)%variables(d)%attributes )
-                           natt = natt + 1
-                           ALLOCATE( files(f)%variables(d)%attributes(natt) )
-                           files(f)%variables(d)%attributes(:natt-1) = atts_tmp
+                           natts = natts + 1
+                           ALLOCATE( files(f)%variables(d)%attributes(natts) )
+                           files(f)%variables(d)%attributes(:natts-1) = atts_tmp
                            DEALLOCATE( atts_tmp )
                         ENDIF
@@ -1780,5 +2692,5 @@
                      !-- Add new attribute to database
                      IF ( .NOT. found )  THEN
-                        files(f)%variables(d)%attributes(natt) = attribute
+                        files(f)%variables(d)%attributes(natts) = attribute
                         found = .TRUE.
                      ENDIF
@@ -1798,12 +2710,12 @@
                        ': requested dimension/variable "' // TRIM( variable_name ) // &
                        '" for attribute "' // TRIM( attribute%name ) //               &
-                       '" does not exist in file "' // TRIM( filename ) // '"' )
+                       '" does not exist in file "' // TRIM( file_name ) // '"' )
             ENDIF
 
             EXIT
 
-         ENDIF  ! variable_name present
-
-      ENDIF  ! check filename
+         ENDIF  ! variable_name not empty
+
+      ENDIF  ! check file_name
 
    ENDDO  ! loop over files
@@ -1813,166 +2725,10 @@
       CALL internal_message( 'error',                                         &
                              routine_name //                                  &
-                             ': requested file "' // TRIM( filename ) //      &
+                             ': requested file "' // TRIM( file_name ) //     &
                              '" for attribute "' // TRIM( attribute%name ) // &
                              '" does not exist' )
    ENDIF
 
-END FUNCTION dom_def_att_save
-
-!--------------------------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Start with output: clear database from unused files/dimensions, initialize
-!> files and write dimension values to files.
-!--------------------------------------------------------------------------------------------------!
-FUNCTION dom_start_output() RESULT( return_value )
-
-   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_start_output'  !< name of routine
-
-   INTEGER(iwp) ::  d             !< loop index
-   INTEGER(iwp) ::  f             !< loop index
-   INTEGER(iwp) ::  return_value  !< return value
-
-   INTEGER(KIND=1), DIMENSION(:), ALLOCATABLE, TARGET ::  values_int8          !< target array for dimension values
-   INTEGER(KIND=1), DIMENSION(:), POINTER, CONTIGUOUS ::  values_int8_pointer  !< pointer to target array
-
-   INTEGER(KIND=2), DIMENSION(:), ALLOCATABLE, TARGET ::  values_int16          !< target array for dimension values
-   INTEGER(KIND=2), DIMENSION(:), POINTER, CONTIGUOUS ::  values_int16_pointer  !< pointer to target array
-
-   INTEGER(KIND=4), DIMENSION(:), ALLOCATABLE, TARGET ::  values_int32          !< target array for dimension values
-   INTEGER(KIND=4), DIMENSION(:), POINTER, CONTIGUOUS ::  values_int32_pointer  !< pointer to target array
-
-   INTEGER(iwp), DIMENSION(:), ALLOCATABLE, TARGET ::  values_intwp          !< target array for dimension values
-   INTEGER(iwp), DIMENSION(:), POINTER, CONTIGUOUS ::  values_intwp_pointer  !< pointer to target array
-
-   REAL(KIND=4), DIMENSION(:), ALLOCATABLE, TARGET ::  values_real32          !< target array for dimension values
-   REAL(KIND=4), DIMENSION(:), POINTER, CONTIGUOUS ::  values_real32_pointer  !< pointer to target array
-
-   REAL(KIND=8), DIMENSION(:), ALLOCATABLE, TARGET ::  values_real64          !< target array for dimension values
-   REAL(KIND=8), DIMENSION(:), POINTER, CONTIGUOUS ::  values_real64_pointer  !< pointer to target array
-
-   REAL(wp), DIMENSION(:), ALLOCATABLE, TARGET ::  values_realwp          !< target array for dimension values
-   REAL(wp), DIMENSION(:), POINTER, CONTIGUOUS ::  values_realwp_pointer  !< pointer to target array
-
-
-   return_value = 0
-   CALL internal_message( 'debug', routine_name // ': start' )
-
-   !-- Clear database from empty files and unused dimensions
-   IF ( nf > 0 )  return_value = cleanup_database()
-
-   IF ( return_value == 0 )  THEN
-      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, &
-                                return_value=return_value )
-
-         !-- Initialize file header:
-         !-- define dimensions and variables and write attributes
-         IF ( return_value == 0 )  &
-            CALL dom_init_file_header( files(f), return_value=return_value )
-
-         !-- End file definition
-         IF ( return_value == 0 )  &
-            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
-                  ALLOCATE( values_int8(files(f)%dimensions(d)%bounds(1): &
-                                        files(f)%dimensions(d)%bounds(2)) )
-                  values_int8 = files(f)%dimensions(d)%values_int8
-                  values_int8_pointer => values_int8
-                  return_value = dom_write_var( files(f)%name, files(f)%dimensions(d)%name, &
-                                    bounds_start=(/ files(f)%dimensions(d)%bounds(1) /),    &
-                                    bounds_end  =(/ files(f)%dimensions(d)%bounds(2) /),    &
-                                    var_int8_1d=values_int8_pointer )
-                  DEALLOCATE( values_int8 )
-               ELSEIF ( ALLOCATED( files(f)%dimensions(d)%values_int16 ) )  THEN
-                  ALLOCATE( values_int16(files(f)%dimensions(d)%bounds(1): &
-                                         files(f)%dimensions(d)%bounds(2)) )
-                  values_int16 = files(f)%dimensions(d)%values_int16
-                  values_int16_pointer => values_int16
-                  return_value = dom_write_var( files(f)%name, files(f)%dimensions(d)%name, &
-                                    bounds_start=(/ files(f)%dimensions(d)%bounds(1) /),    &
-                                    bounds_end  =(/ files(f)%dimensions(d)%bounds(2) /),    &
-                                    var_int16_1d=values_int16_pointer )
-                  DEALLOCATE( values_int16 )
-               ELSEIF ( ALLOCATED( files(f)%dimensions(d)%values_int32 ) )  THEN
-                  ALLOCATE( values_int32(files(f)%dimensions(d)%bounds(1): &
-                                         files(f)%dimensions(d)%bounds(2)) )
-                  values_int32 = files(f)%dimensions(d)%values_int32
-                  values_int32_pointer => values_int32
-                  return_value = dom_write_var( files(f)%name, files(f)%dimensions(d)%name, &
-                                    bounds_start=(/ files(f)%dimensions(d)%bounds(1) /),    &
-                                    bounds_end  =(/ files(f)%dimensions(d)%bounds(2) /),    &
-                                    var_int32_1d=values_int32_pointer )
-                  DEALLOCATE( values_int32 )
-               ELSEIF ( ALLOCATED( files(f)%dimensions(d)%values_intwp ) )  THEN
-                  ALLOCATE( values_intwp(files(f)%dimensions(d)%bounds(1): &
-                                         files(f)%dimensions(d)%bounds(2)) )
-                  values_intwp = files(f)%dimensions(d)%values_intwp
-                  values_intwp_pointer => values_intwp
-                  return_value = dom_write_var( files(f)%name, files(f)%dimensions(d)%name, &
-                                    bounds_start=(/ files(f)%dimensions(d)%bounds(1) /),    &
-                                    bounds_end  =(/ files(f)%dimensions(d)%bounds(2) /),    &
-                                    var_intwp_1d=values_intwp_pointer )
-                  DEALLOCATE( values_intwp )
-               ELSEIF ( ALLOCATED( files(f)%dimensions(d)%values_real32 ) )  THEN
-                  ALLOCATE( values_real32(files(f)%dimensions(d)%bounds(1): &
-                                          files(f)%dimensions(d)%bounds(2)) )
-                  values_real32 = files(f)%dimensions(d)%values_real32
-                  values_real32_pointer => values_real32
-                  return_value = dom_write_var( files(f)%name, files(f)%dimensions(d)%name, &
-                                    bounds_start=(/ files(f)%dimensions(d)%bounds(1) /),    &
-                                    bounds_end  =(/ files(f)%dimensions(d)%bounds(2) /),    &
-                                    var_real32_1d=values_real32_pointer )
-                  DEALLOCATE( values_real32 )
-               ELSEIF ( ALLOCATED( files(f)%dimensions(d)%values_real64 ) )  THEN
-                  ALLOCATE( values_real64(files(f)%dimensions(d)%bounds(1): &
-                                          files(f)%dimensions(d)%bounds(2)) )
-                  values_real64 = files(f)%dimensions(d)%values_real64
-                  values_real64_pointer => values_real64
-                  return_value = dom_write_var( files(f)%name, files(f)%dimensions(d)%name, &
-                                    bounds_start=(/ files(f)%dimensions(d)%bounds(1) /),    &
-                                    bounds_end  =(/ files(f)%dimensions(d)%bounds(2) /),    &
-                                    var_real64_1d=values_real64_pointer )
-                  DEALLOCATE( values_real64 )
-               ELSEIF ( ALLOCATED( files(f)%dimensions(d)%values_realwp ) )  THEN
-                  ALLOCATE( values_realwp(files(f)%dimensions(d)%bounds(1): &
-                                          files(f)%dimensions(d)%bounds(2)) )
-                  values_realwp = files(f)%dimensions(d)%values_realwp
-                  values_realwp_pointer => values_realwp
-                  return_value = dom_write_var( files(f)%name, files(f)%dimensions(d)%name, &
-                                    bounds_start=(/ files(f)%dimensions(d)%bounds(1) /),    &
-                                    bounds_end  =(/ files(f)%dimensions(d)%bounds(2) /),    &
-                                    var_realwp_1d=values_realwp_pointer )
-                  DEALLOCATE( values_realwp )
-               ENDIF
-               IF ( return_value /= 0 )  EXIT
-            ENDDO
-
-         ENDIF
-
-         IF ( return_value /= 0 )  EXIT
-
-      ENDDO
-   ENDIF
-
-   CALL internal_message( 'debug', routine_name // ': finished' )
-
-END FUNCTION dom_start_output
+END FUNCTION save_attribute_in_database
 
 !--------------------------------------------------------------------------------------------------!
@@ -1986,15 +2742,15 @@
    ! CHARACTER(LEN=*), PARAMETER ::  routine_name = 'cleanup_database'  !< name of routine
 
-   INTEGER(iwp) ::  d             !< loop index
-   INTEGER(iwp) ::  f             !< loop index
-   INTEGER(iwp) ::  i             !< loop index
-   INTEGER(iwp) ::  ndim          !< number of dimensions in a file
-   INTEGER(iwp) ::  ndim_used     !< number of used dimensions in a file
-   INTEGER(iwp) ::  nf_used       !< number of used files
-   INTEGER(iwp) ::  nvar          !< number of variables in a file
-   INTEGER(iwp) ::  return_value  !< return value
-
-   LOGICAL, DIMENSION(1:nf)           ::  file_is_used       !< true if file contains variables
-   LOGICAL, DIMENSION(:), ALLOCATABLE ::  dimension_is_used  !< true if dimension is used by any variable
+   INTEGER ::  d             !< loop index
+   INTEGER ::  f             !< loop index
+   INTEGER ::  i             !< loop index
+   INTEGER ::  ndims         !< number of dimensions in a file
+   INTEGER ::  ndims_used    !< number of used dimensions in a file
+   INTEGER ::  nfiles_used   !< number of used files
+   INTEGER ::  nvars         !< number of variables in a file
+   INTEGER ::  return_value  !< return value
+
+   LOGICAL, DIMENSION(1:nfiles)             ::  file_is_used       !< true if file contains variables
+   LOGICAL, DIMENSION(:),       ALLOCATABLE ::  dimension_is_used  !< true if dimension is used by any variable
 
    TYPE(dimension_type), DIMENSION(:), ALLOCATABLE ::  used_dimensions  !< list of used dimensions
@@ -2007,5 +2763,5 @@
    !-- Flag files which contain output variables as used
    file_is_used(:) = .FALSE.
-   DO  f = 1, nf
+   DO  f = 1, nfiles
       IF ( ALLOCATED( files(f)%variables ) )  THEN
          file_is_used(f) = .TRUE.
@@ -2014,8 +2770,8 @@
 
    !-- Copy flagged files into temporary list
-   nf_used = COUNT( file_is_used )
-   ALLOCATE( used_files(nf_used) )
+   nfiles_used = COUNT( file_is_used )
+   ALLOCATE( used_files(nfiles_used) )
    i = 0
-   DO  f = 1, nf
+   DO  f = 1, nfiles
       IF ( file_is_used(f) )  THEN
          i = i + 1
@@ -2026,11 +2782,11 @@
    !-- Replace file list with list of used files
    DEALLOCATE( files )
-   nf = nf_used
-   ALLOCATE( files(nf) )
+   nfiles = nfiles_used
+   ALLOCATE( files(nfiles) )
    files = used_files
    DEALLOCATE( used_files )
 
    !-- Check every file for unused dimensions
-   DO  f = 1, nf
+   DO  f = 1, nfiles
 
       !-- If a file is already initialized, it was already checked previously
@@ -2038,11 +2794,11 @@
 
       !-- Get number of defined dimensions
-      ndim = SIZE( files(f)%dimensions )
-      ALLOCATE( dimension_is_used(ndim) )
+      ndims = SIZE( files(f)%dimensions )
+      ALLOCATE( dimension_is_used(ndims) )
 
       !-- Go through all variables and flag all used dimensions
-      nvar = SIZE( files(f)%variables )
-      DO  d = 1, ndim
-         DO  i = 1, nvar
+      nvars = SIZE( files(f)%variables )
+      DO  d = 1, ndims
+         DO  i = 1, nvars
             dimension_is_used(d) = &
                ANY( files(f)%dimensions(d)%name == files(f)%variables(i)%dimension_names )
@@ -2052,8 +2808,8 @@
 
       !-- Copy used dimensions to temporary list
-      ndim_used = COUNT( dimension_is_used )
-      ALLOCATE( used_dimensions(ndim_used) )
+      ndims_used = COUNT( dimension_is_used )
+      ALLOCATE( used_dimensions(ndims_used) )
       i = 0
-      DO  d = 1, ndim
+      DO  d = 1, ndims
          IF ( dimension_is_used(d) )  THEN
             i = i + 1
@@ -2064,6 +2820,6 @@
       !-- Replace dimension list with list of used dimensions
       DEALLOCATE( files(f)%dimensions )
-      ndim = ndim_used
-      ALLOCATE( files(f)%dimensions(ndim) )
+      ndims = ndims_used
+      ALLOCATE( files(f)%dimensions(ndims) )
       files(f)%dimensions = used_dimensions
       DEALLOCATE( used_dimensions )
@@ -2079,14 +2835,14 @@
 !> Open requested output file.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE open_output_file( file_format, filename, file_id, return_value )
+SUBROUTINE open_output_file( file_format, file_name, file_id, return_value )
 
    CHARACTER(LEN=*), INTENT(IN) ::  file_format  !< file format chosen for file
-   CHARACTER(LEN=*), INTENT(IN) ::  filename     !< name of file to be checked
+   CHARACTER(LEN=*), INTENT(IN) ::  file_name    !< name of file to be checked
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'open_output_file'  !< name of routine
 
-   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
+   INTEGER, INTENT(OUT) ::  file_id              !< file ID
+   INTEGER              ::  output_return_value  !< return value of a called output routine
+   INTEGER, INTENT(OUT) ::  return_value         !< return value
 
 
@@ -2097,11 +2853,11 @@
 
       CASE ( 'binary' )
-         CALL binary_open_file( 'binary', filename, file_id, output_return_value )
+         CALL binary_open_file( 'binary', file_name, file_id, output_return_value )
 
       CASE ( 'netcdf4-serial' )
-         CALL netcdf4_open_file( 'serial', filename, file_id, output_return_value )
+         CALL netcdf4_open_file( 'serial', file_name, file_id, output_return_value )
 
       CASE ( 'netcdf4-parallel' )
-         CALL netcdf4_open_file( 'parallel', filename, file_id, output_return_value )
+         CALL netcdf4_open_file( 'parallel', file_name, file_id, output_return_value )
 
       CASE DEFAULT
@@ -2113,10 +2869,10 @@
       return_value = output_return_value
       CALL internal_message( 'error', routine_name // &
-                             ': error while opening file "' // TRIM( filename ) // '"' )
+                             ': error while opening file "' // TRIM( file_name ) // '"' )
    ELSEIF ( return_value /= 0 )  THEN
-      CALL internal_message( 'error', routine_name //                              &
-                                      ': file "' // TRIM( filename ) //            &
-                                      '": file format "' // TRIM( file_format ) // &
-                                      '" not supported' )
+      CALL internal_message( 'error', routine_name //                     &
+                             ': file "' // TRIM( file_name ) //           &
+                             '": file format "' // TRIM( file_format ) // &
+                             '" not supported' )
    ENDIF
 
@@ -2126,13 +2882,13 @@
 ! Description:
 ! ------------
-!> Define attributes, dimensions and variables.
-!--------------------------------------------------------------------------------------------------!
-SUBROUTINE dom_init_file_header( file, return_value )
-
-   ! CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_init_file_header'  !< name of routine
-
-   INTEGER(iwp)              ::  a             !< loop index
-   INTEGER(iwp)              ::  d             !< loop index
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
+!> Initialize attributes, dimensions and variables in a file.
+!--------------------------------------------------------------------------------------------------!
+SUBROUTINE init_file_header( file, return_value )
+
+   ! CHARACTER(LEN=*), PARAMETER ::  routine_name = 'init_file_header'  !< name of routine
+
+   INTEGER              ::  a             !< loop index
+   INTEGER              ::  d             !< loop index
+   INTEGER, INTENT(OUT) ::  return_value  !< return value
 
    TYPE(file_type), INTENT(INOUT) ::  file  !< initialize header of this file
@@ -2144,5 +2900,6 @@
    IF ( ALLOCATED( file%attributes ) )  THEN
       DO  a = 1, SIZE( file%attributes )
-         return_value = write_attribute( file%format, file%id, file%name, var_id=no_var_id, &
+         return_value = write_attribute( file%format, file%id, file%name,     &
+                                         variable_id=no_id, variable_name='', &
                                          attribute=file%attributes(a) )
          IF ( return_value /= 0 )  EXIT
@@ -2158,16 +2915,16 @@
 
             !-- Initialize non-masked dimension
-            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 )
+            CALL init_file_dimension( file%format, file%id, file%name,       &
+                    file%dimensions(d)%id, file%dimensions(d)%name,          &
+                    file%dimensions(d)%data_type, file%dimensions(d)%length, &
+                    file%dimensions(d)%variable_id, return_value )
 
          ELSE
 
             !-- Initialize masked dimension
-            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 )
+            CALL init_file_dimension( file%format, file%id, file%name,            &
+                    file%dimensions(d)%id, file%dimensions(d)%name,               &
+                    file%dimensions(d)%data_type, file%dimensions(d)%length_mask, &
+                    file%dimensions(d)%variable_id, return_value )
 
          ENDIF
@@ -2177,6 +2934,6 @@
             DO  a = 1, SIZE( file%dimensions(d)%attributes )
                return_value = write_attribute( file%format, file%id, file%name, &
-                                 var_id=file%dimensions(d)%var_id,              &
-                                 var_name=file%dimensions(d)%name,              &
+                                 variable_id=file%dimensions(d)%variable_id,    &
+                                 variable_name=file%dimensions(d)%name,         &
                                  attribute=file%dimensions(d)%attributes(a) )
                IF ( return_value /= 0 )  EXIT
@@ -2190,5 +2947,6 @@
       !-- Save dimension IDs for variables wihtin database
       IF ( return_value == 0 )  &
-         CALL collect_dimesion_ids_for_variables( file%variables, file%dimensions, return_value )
+         CALL collect_dimesion_ids_for_variables( file%name, file%variables, file%dimensions, &
+                                                  return_value )
 
       !-- Initialize file variables
@@ -2205,6 +2963,6 @@
                DO  a = 1, SIZE( file%variables(d)%attributes )
                   return_value = write_attribute( file%format, file%id, file%name, &
-                                    var_id=file%variables(d)%id,                   &
-                                    var_name=file%variables(d)%name,               &
+                                    variable_id=file%variables(d)%id,              &
+                                    variable_name=file%variables(d)%name,          &
                                     attribute=file%variables(d)%attributes(a) )
                   IF ( return_value /= 0 )  EXIT
@@ -2219,5 +2977,130 @@
    ENDIF
 
-END SUBROUTINE dom_init_file_header
+END SUBROUTINE init_file_header
+
+!--------------------------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Initialize dimension in file.
+!--------------------------------------------------------------------------------------------------!
+SUBROUTINE init_file_dimension( file_format, file_id, file_name,              &
+              dimension_id, dimension_name, dimension_type, dimension_length, &
+              variable_id, return_value )
+
+   CHARACTER(LEN=*), INTENT(IN) ::  dimension_name  !< name of dimension
+   CHARACTER(LEN=*), INTENT(IN) ::  dimension_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, INTENT(OUT) ::  dimension_id         !< dimension ID
+   INTEGER, INTENT(IN)  ::  dimension_length     !< length of dimension
+   INTEGER, INTENT(IN)  ::  file_id              !< file ID
+   INTEGER              ::  output_return_value  !< return value of a called output routine
+   INTEGER, INTENT(OUT) ::  return_value         !< return value
+   INTEGER, INTENT(OUT) ::  variable_id          !< associated variable ID
+
+
+   return_value = 0
+   output_return_value = 0
+
+   temp_string = '(file "' // TRIM( file_name ) // &
+                 '", dimension "' // TRIM( dimension_name ) // '")'
+
+   SELECT CASE ( TRIM( file_format ) )
+
+      CASE ( 'binary' )
+         CALL binary_init_dimension( 'binary', file_id, dimension_id, variable_id, &
+                 dimension_name, dimension_type, dimension_length,                 &
+                 return_value=output_return_value )
+
+      CASE ( 'netcdf4-serial' )
+         CALL netcdf4_init_dimension( 'serial', file_id, dimension_id, variable_id, &
+                 dimension_name, dimension_type, dimension_length,                  &
+                 return_value=output_return_value )
+
+      CASE ( 'netcdf4-parallel' )
+         CALL netcdf4_init_dimension( 'parallel', file_id, dimension_id, variable_id, &
+                 dimension_name, dimension_type, dimension_length,                    &
+                 return_value=output_return_value )
+
+      CASE DEFAULT
+         return_value = 1
+         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
+
+!--------------------------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Initialize variable.
+!--------------------------------------------------------------------------------------------------!
+SUBROUTINE init_file_variable( file_format, file_id, file_name,        &
+                               variable_id, variable_name, variable_type, dimension_ids, &
+                               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) ::  variable_name  !< name of variable
+   CHARACTER(LEN=*), INTENT(IN) ::  variable_type  !< data type of variable
+
+   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'init_file_variable'  !< file format chosen for file
+
+   INTEGER, INTENT(IN)  ::  file_id              !< file ID
+   INTEGER              ::  output_return_value  !< return value of a called output routine
+   INTEGER, INTENT(OUT) ::  return_value         !< return value
+   INTEGER, INTENT(OUT) ::  variable_id          !< variable ID
+
+   INTEGER, DIMENSION(:), INTENT(IN) ::  dimension_ids  !< list of dimension IDs used by variable
+
+   LOGICAL, INTENT(IN)  ::  is_global  !< true if variable is global
+
+
+   return_value = 0
+   output_return_value = 0
+
+   temp_string = '(file "' // TRIM( file_name ) // &
+                 '", variable "' // TRIM( variable_name ) // '")'
+
+   SELECT CASE ( TRIM( file_format ) )
+
+      CASE ( 'binary' )
+         CALL binary_init_variable( 'binary', file_id, variable_id, variable_name, &
+                 variable_type, dimension_ids, is_global, return_value=output_return_value )
+
+      CASE ( 'netcdf4-serial' )
+         CALL netcdf4_init_variable( 'serial', file_id, variable_id, variable_name, &
+                 variable_type, dimension_ids, is_global, return_value=output_return_value )
+
+      CASE ( 'netcdf4-parallel' )
+         CALL netcdf4_init_variable( 'parallel', file_id, variable_id, variable_name, &
+                 variable_type, dimension_ids, is_global, return_value=output_return_value )
+
+      CASE DEFAULT
+         return_value = 1
+         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
 
 !--------------------------------------------------------------------------------------------------!
@@ -2226,16 +3109,17 @@
 !> Write attribute to file.
 !--------------------------------------------------------------------------------------------------!
-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
+FUNCTION write_attribute( file_format, file_id, file_name,        &
+                          variable_id, variable_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) ::  variable_name  !< variable name
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'write_attribute'  !< file format chosen for file
 
-   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
+   INTEGER, INTENT(IN) ::  file_id              !< file ID
+   INTEGER             ::  return_value         !< return value
+   INTEGER             ::  output_return_value  !< return value of a called output routine
+   INTEGER, INTENT(IN) ::  variable_id          !< variable ID
 
    TYPE(attribute_type), INTENT(IN) ::  attribute  !< attribute to be written
@@ -2246,12 +3130,7 @@
 
    !-- 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
+   temp_string = '(file "' // TRIM( file_name ) //           &
+                 '", variable "' // TRIM( variable_name ) // &
+                 '", attribute "' // TRIM( attribute%name ) // '")'
 
    !-- Write attribute to file
@@ -2263,31 +3142,31 @@
 
             CASE( 'char' )
-               CALL binary_write_attribute( file_id=file_id, var_id=var_id,          &
-                       att_name=attribute%name, att_value_char=attribute%value_char, &
+               CALL binary_write_attribute( file_id=file_id, variable_id=variable_id,  &
+                       attribute_name=attribute%name, value_char=attribute%value_char, &
                        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, &
+               CALL binary_write_attribute( file_id=file_id, variable_id=variable_id,  &
+                       attribute_name=attribute%name, value_int8=attribute%value_int8, &
                        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, &
+               CALL binary_write_attribute( file_id=file_id, variable_id=variable_id,    &
+                       attribute_name=attribute%name, value_int16=attribute%value_int16, &
                        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, &
+               CALL binary_write_attribute( file_id=file_id, variable_id=variable_id,    &
+                       attribute_name=attribute%name, value_int32=attribute%value_int32, &
                        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, &
+               CALL binary_write_attribute( file_id=file_id, variable_id=variable_id,      &
+                       attribute_name=attribute%name, value_real32=attribute%value_real32, &
                        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, &
+               CALL binary_write_attribute( file_id=file_id, variable_id=variable_id,      &
+                       attribute_name=attribute%name, value_real64=attribute%value_real64, &
                        return_value=output_return_value )
 
@@ -2307,31 +3186,31 @@
 
             CASE( 'char' )
-               CALL netcdf4_write_attribute( file_id=file_id, var_id=var_id,  &
-                       att_name=attribute%name, att_value_char=attribute%value_char, &
+               CALL netcdf4_write_attribute( file_id=file_id, variable_id=variable_id, &
+                       attribute_name=attribute%name, value_char=attribute%value_char, &
                        return_value=output_return_value )
 
             CASE( 'int8' )
-               CALL netcdf4_write_attribute( file_id=file_id, var_id=var_id,  &
-                       att_name=attribute%name, att_value_int8=attribute%value_int8, &
+               CALL netcdf4_write_attribute( file_id=file_id, variable_id=variable_id, &
+                       attribute_name=attribute%name, value_int8=attribute%value_int8, &
                        return_value=output_return_value )
 
             CASE( 'int16' )
-               CALL netcdf4_write_attribute( file_id=file_id, var_id=var_id,    &
-                       att_name=attribute%name, att_value_int16=attribute%value_int16, &
+               CALL netcdf4_write_attribute( file_id=file_id, variable_id=variable_id,   &
+                       attribute_name=attribute%name, value_int16=attribute%value_int16, &
                        return_value=output_return_value )
 
             CASE( 'int32' )
-               CALL netcdf4_write_attribute( file_id=file_id, var_id=var_id,    &
-                       att_name=attribute%name, att_value_int32=attribute%value_int32, &
+               CALL netcdf4_write_attribute( file_id=file_id, variable_id=variable_id,   &
+                       attribute_name=attribute%name, value_int32=attribute%value_int32, &
                        return_value=output_return_value )
 
             CASE( 'real32' )
-               CALL netcdf4_write_attribute( file_id=file_id, var_id=var_id,      &
-                       att_name=attribute%name, att_value_real32=attribute%value_real32, &
+               CALL netcdf4_write_attribute( file_id=file_id, variable_id=variable_id,     &
+                       attribute_name=attribute%name, value_real32=attribute%value_real32, &
                        return_value=output_return_value )
 
             CASE( 'real64' )
-               CALL netcdf4_write_attribute( file_id=file_id, var_id=var_id,      &
-                       att_name=attribute%name, att_value_real64=attribute%value_real64, &
+               CALL netcdf4_write_attribute( file_id=file_id, variable_id=variable_id,     &
+                       attribute_name=attribute%name, value_real64=attribute%value_real64, &
                        return_value=output_return_value )
 
@@ -2348,68 +3227,7 @@
       CASE DEFAULT
          return_value = 1
-         CALL internal_message( 'error',        &
-                                routine_name // &
+         CALL internal_message( 'error', routine_name //                                &
                                 ': 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
-
-!--------------------------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Initialize dimension in file.
-!--------------------------------------------------------------------------------------------------!
-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)              ::  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( 'binary', file_id, dim_id, var_id, &
-                 dim_name, dim_type, dim_length, return_value=output_return_value )
-
-      CASE ( 'netcdf4-serial' )
-         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_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
-         CALL internal_message( 'error', routine_name //                    &
-                                ': file format "' // TRIM( file_format ) // &
-                                '" not supported ' // TRIM( temp_string ) )
 
    END SELECT
@@ -2418,8 +3236,8 @@
       return_value = output_return_value
       CALL internal_message( 'error', routine_name // &
-                             ': error while defining dimension ' // TRIM( temp_string ) )
+                             ': error while writing attribute ' // TRIM( temp_string ) )
    ENDIF
 
-END SUBROUTINE init_file_dimension
+END FUNCTION write_attribute
 
 !--------------------------------------------------------------------------------------------------!
@@ -2428,14 +3246,16 @@
 !> Get dimension IDs and save them to variables.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE collect_dimesion_ids_for_variables( variables, dimensions, return_value )
+SUBROUTINE collect_dimesion_ids_for_variables( file_name, variables, dimensions, return_value )
+
+   CHARACTER(LEN=*), INTENT(IN) ::  file_name !< name of file
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'collect_dimesion_ids_for_variables'  !< file format chosen for file
 
-   INTEGER(iwp) ::  d             !< loop index
-   INTEGER(iwp) ::  i             !< loop index
-   INTEGER(iwp) ::  j             !< loop index
-   INTEGER(iwp) ::  ndim          !< number of dimensions
-   INTEGER(iwp) ::  nvar          !< number of variables
-   INTEGER(iwp) ::  return_value  !< return value
+   INTEGER              ::  d             !< loop index
+   INTEGER              ::  i             !< loop index
+   INTEGER              ::  j             !< loop index
+   INTEGER              ::  ndims         !< number of dimensions
+   INTEGER              ::  nvars         !< number of variables
+   INTEGER, INTENT(OUT) ::  return_value  !< return value
 
    LOGICAL ::  found  !< true if dimension required by variable was found in dimension list
@@ -2447,11 +3267,11 @@
 
    return_value  = 0
-   ndim = SIZE( dimensions )
-   nvar = SIZE( variables )
-
-   DO  i = 1, nvar
+   ndims = SIZE( dimensions )
+   nvars = SIZE( variables )
+
+   DO  i = 1, nvars
       DO  j = 1, SIZE( variables(i)%dimension_names )
          found = .FALSE.
-         DO  d = 1, ndim
+         DO  d = 1, ndims
             IF ( variables(i)%dimension_names(j) == dimensions(d)%name )  THEN
                variables(i)%dimension_ids(j) = dimensions(d)%id
@@ -2462,8 +3282,8 @@
          IF ( .NOT. found )  THEN
             return_value = 1
-            CALL internal_message( 'error',                                                 &
-                    routine_name // ': variable "' // TRIM( variables(i)%name ) //          &
-                    '": required dimension "' // TRIM( variables(i)%dimension_names(j) ) // &
-                    '" is undefined' )
+            CALL internal_message( 'error', routine_name //                                &
+                    ': required dimension "' // TRIM( variables(i)%dimension_names(j) ) // &
+                    '" is undefined (variable "' // TRIM( variables(i)%name ) //           &
+                    '", file "' // TRIM( file_name ) // '")!' )
             EXIT
          ENDIF
@@ -2477,25 +3297,18 @@
 ! Description:
 ! ------------
-!> Initialize variable.
-!--------------------------------------------------------------------------------------------------!
-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
+!> Leave file definition/initialization.
+!>
+!> @todo Do we need an MPI barrier at the end?
+!--------------------------------------------------------------------------------------------------!
+SUBROUTINE stop_file_header_definition( 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=*), INTENT(IN) ::  var_name     !< name of variable
-   CHARACTER(LEN=*), INTENT(IN) ::  var_type     !< data type of variable
-
-   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'init_file_variable'  !< file format chosen for file
-
-   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
+
+   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'stop_file_header_definition'  !< name of routine
+
+   INTEGER, INTENT(IN)  ::  file_id              !< file id
+   INTEGER              ::  output_return_value  !< return value of a called output routine
+   INTEGER, INTENT(OUT) ::  return_value         !< return value
 
 
@@ -2503,20 +3316,13 @@
    output_return_value = 0
 
-   temp_string = '(file "' // TRIM( file_name ) // &
-                 '", variable "' // TRIM( var_name ) // '")'
+   temp_string = '(file "' // TRIM( file_name ) // '")'
 
    SELECT CASE ( TRIM( file_format ) )
 
       CASE ( 'binary' )
-         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_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_init_variable( 'parallel', file_id, var_id, var_name, var_type, &
-                                     var_dim_id, is_global, return_value=output_return_value )
+         CALL binary_stop_file_header_definition( file_id, output_return_value )
+
+      CASE ( 'netcdf4-parallel', 'netcdf4-serial' )
+         CALL netcdf4_stop_file_header_definition( file_id, output_return_value )
 
       CASE DEFAULT
@@ -2530,53 +3336,5 @@
    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
-
-!--------------------------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Finalize file definition/initialization.
-!>
-!> @todo Do we need an MPI barrier at the end?
-!--------------------------------------------------------------------------------------------------!
-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)              ::  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, output_return_value )
-
-      CASE ( 'netcdf4-parallel', 'netcdf4-serial' )
-         CALL netcdf4_init_end( file_id, output_return_value )
-
-      CASE DEFAULT
-         return_value = 1
-         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 // &
+      CALL internal_message( 'error', routine_name //                          &
                              ': error while leaving file-definition state ' // &
                              TRIM( temp_string ) )
@@ -2585,944 +3343,28 @@
    ! CALL MPI_Barrier( MPI_COMM_WORLD, return_value )
 
-END SUBROUTINE dom_init_end
+END SUBROUTINE stop_file_header_definition
 
 !--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> Write variable to file.
-!> Example call:
-!>   dom_write_var( file_format = 'binary', &
-!>                  filename = 'DATA_OUTPUT_3D', &
-!>                  name = 'u', &
-!>                  var_real64_3d = u, &
-!>                  bounds_start = (/nxl, nys, nzb, time_step/), &
-!>                  bounds_end = (/nxr, nyn, nzt, time_step/)  )
-!> @note The order of dimension bounds must match to the order of dimensions given in call
-!>       'dom_def_var'. I.e., the corresponding variable definition should be like:
-!>          dom_def_var( filename =  'DATA_OUTPUT_3D', &
-!>                       name = 'u', &
-!>                       dimension_names = (/'x   ', 'y   ', 'z   ', 'time'/), &
-!>                       output_type = <desired-output-type> )
-!--------------------------------------------------------------------------------------------------!
-FUNCTION dom_write_var( filename, name, bounds_start, bounds_end,       &
-            var_int8_0d,   var_int8_1d,   var_int8_2d,   var_int8_3d,   &
-            var_int16_0d,  var_int16_1d,  var_int16_2d,  var_int16_3d,  &
-            var_int32_0d,  var_int32_1d,  var_int32_2d,  var_int32_3d,  &
-            var_intwp_0d,  var_intwp_1d,  var_intwp_2d,  var_intwp_3d,  &
-            var_real32_0d, var_real32_1d, var_real32_2d, var_real32_3d, &
-            var_real64_0d, var_real64_1d, var_real64_2d, var_real64_3d, &
-            var_realwp_0d, var_realwp_1d, var_realwp_2d, var_realwp_3d  &
-            ) RESULT( return_value )
-
-   CHARACTER(LEN=charlen)       ::  file_format  !< file format chosen for file
-   CHARACTER(LEN=*), INTENT(IN) ::  filename     !< name of file
-   CHARACTER(LEN=*), INTENT(IN) ::  name         !< name of variable
-
-   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_write_var'  !< name of routine
-
-   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 per dimension of variable
-   INTEGER(iwp), DIMENSION(:),   INTENT(IN)  ::  bounds_start           !< start index per dimension of variable
-   INTEGER(iwp), DIMENSION(:),   ALLOCATABLE ::  bounds_origin          !< first index of each dimension
-   INTEGER(iwp), DIMENSION(:),   ALLOCATABLE ::  bounds_start_internal  !< start index per dim. for output after masking
-   INTEGER(iwp), DIMENSION(:),   ALLOCATABLE ::  value_counts           !< count of indices to be written per dimension
-   INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  masked_indices         !< list containing all output indices along a dimension
-
-   LOGICAL ::  do_output  !< true if any data lies within given range of masked dimension
-   LOGICAL ::  is_global  !< true if variable is global
-
-   INTEGER(KIND=1), POINTER, INTENT(IN), OPTIONAL                   ::  var_int8_0d  !< output variable
-   INTEGER(KIND=1), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_int8_1d  !< output variable
-   INTEGER(KIND=1), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_int8_2d  !< output variable
-   INTEGER(KIND=1), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_int8_3d  !< output variable
-
-   INTEGER(KIND=1), TARGET, ALLOCATABLE, DIMENSION(:)     ::  var_int8_1d_resorted  !< resorted output variable
-   INTEGER(KIND=1), TARGET, ALLOCATABLE, DIMENSION(:,:)   ::  var_int8_2d_resorted  !< resorted output variable
-   INTEGER(KIND=1), TARGET, ALLOCATABLE, DIMENSION(:,:,:) ::  var_int8_3d_resorted  !< resorted output variable
-
-   INTEGER(KIND=1), POINTER                               ::  var_int8_0d_pointer  !< output variable
-   INTEGER(KIND=1), POINTER, CONTIGUOUS, DIMENSION(:)     ::  var_int8_1d_pointer  !< output variable
-   INTEGER(KIND=1), POINTER, CONTIGUOUS, DIMENSION(:,:)   ::  var_int8_2d_pointer  !< output variable
-   INTEGER(KIND=1), POINTER, CONTIGUOUS, DIMENSION(:,:,:) ::  var_int8_3d_pointer  !< output variable
-
-   INTEGER(KIND=2), POINTER, INTENT(IN), OPTIONAL                   ::  var_int16_0d  !< output variable
-   INTEGER(KIND=2), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_int16_1d  !< output variable
-   INTEGER(KIND=2), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_int16_2d  !< output variable
-   INTEGER(KIND=2), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_int16_3d  !< output variable
-
-   INTEGER(KIND=2), TARGET, ALLOCATABLE, DIMENSION(:)     ::  var_int16_1d_resorted  !< resorted output variable
-   INTEGER(KIND=2), TARGET, ALLOCATABLE, DIMENSION(:,:)   ::  var_int16_2d_resorted  !< resorted output variable
-   INTEGER(KIND=2), TARGET, ALLOCATABLE, DIMENSION(:,:,:) ::  var_int16_3d_resorted  !< resorted output variable
-
-   INTEGER(KIND=2), POINTER                               ::  var_int16_0d_pointer  !< output variable
-   INTEGER(KIND=2), POINTER, CONTIGUOUS, DIMENSION(:)     ::  var_int16_1d_pointer  !< output variable
-   INTEGER(KIND=2), POINTER, CONTIGUOUS, DIMENSION(:,:)   ::  var_int16_2d_pointer  !< output variable
-   INTEGER(KIND=2), POINTER, CONTIGUOUS, DIMENSION(:,:,:) ::  var_int16_3d_pointer  !< output variable
-
-   INTEGER(KIND=4), POINTER, INTENT(IN), OPTIONAL                   ::  var_int32_0d  !< output variable
-   INTEGER(KIND=4), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_int32_1d  !< output variable
-   INTEGER(KIND=4), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_int32_2d  !< output variable
-   INTEGER(KIND=4), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_int32_3d  !< output variable
-
-   INTEGER(KIND=4), TARGET, ALLOCATABLE, DIMENSION(:)     ::  var_int32_1d_resorted  !< resorted output variable
-   INTEGER(KIND=4), TARGET, ALLOCATABLE, DIMENSION(:,:)   ::  var_int32_2d_resorted  !< resorted output variable
-   INTEGER(KIND=4), TARGET, ALLOCATABLE, DIMENSION(:,:,:) ::  var_int32_3d_resorted  !< resorted output variable
-
-   INTEGER(KIND=4), POINTER                               ::  var_int32_0d_pointer  !< output variable
-   INTEGER(KIND=4), POINTER, CONTIGUOUS, DIMENSION(:)     ::  var_int32_1d_pointer  !< output variable
-   INTEGER(KIND=4), POINTER, CONTIGUOUS, DIMENSION(:,:)   ::  var_int32_2d_pointer  !< output variable
-   INTEGER(KIND=4), POINTER, CONTIGUOUS, DIMENSION(:,:,:) ::  var_int32_3d_pointer  !< output variable
-
-   INTEGER(iwp), POINTER, INTENT(IN), OPTIONAL                   ::  var_intwp_0d  !< output variable
-   INTEGER(iwp), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_intwp_1d  !< output variable
-   INTEGER(iwp), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_intwp_2d  !< output variable
-   INTEGER(iwp), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_intwp_3d  !< output variable
-
-   INTEGER(iwp), TARGET, ALLOCATABLE, DIMENSION(:)     ::  var_intwp_1d_resorted  !< resorted output variable
-   INTEGER(iwp), TARGET, ALLOCATABLE, DIMENSION(:,:)   ::  var_intwp_2d_resorted  !< resorted output variable
-   INTEGER(iwp), TARGET, ALLOCATABLE, DIMENSION(:,:,:) ::  var_intwp_3d_resorted  !< resorted output variable
-
-   INTEGER(iwp), POINTER                               ::  var_intwp_0d_pointer  !< output variable
-   INTEGER(iwp), POINTER, CONTIGUOUS, DIMENSION(:)     ::  var_intwp_1d_pointer  !< output variable
-   INTEGER(iwp), POINTER, CONTIGUOUS, DIMENSION(:,:)   ::  var_intwp_2d_pointer  !< output variable
-   INTEGER(iwp), POINTER, CONTIGUOUS, DIMENSION(:,:,:) ::  var_intwp_3d_pointer  !< output variable
-
-   REAL(KIND=4), POINTER, INTENT(IN), OPTIONAL                   ::  var_real32_0d  !< output variable
-   REAL(KIND=4), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_real32_1d  !< output variable
-   REAL(KIND=4), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_real32_2d  !< output variable
-   REAL(KIND=4), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_real32_3d  !< output variable
-
-   REAL(KIND=4), TARGET, ALLOCATABLE, DIMENSION(:)     ::  var_real32_1d_resorted  !< resorted output variable
-   REAL(KIND=4), TARGET, ALLOCATABLE, DIMENSION(:,:)   ::  var_real32_2d_resorted  !< resorted output variable
-   REAL(KIND=4), TARGET, ALLOCATABLE, DIMENSION(:,:,:) ::  var_real32_3d_resorted  !< resorted output variable
-
-   REAL(KIND=4), POINTER                               ::  var_real32_0d_pointer  !< output variable
-   REAL(KIND=4), POINTER, CONTIGUOUS, DIMENSION(:)     ::  var_real32_1d_pointer  !< output variable
-   REAL(KIND=4), POINTER, CONTIGUOUS, DIMENSION(:,:)   ::  var_real32_2d_pointer  !< output variable
-   REAL(KIND=4), POINTER, CONTIGUOUS, DIMENSION(:,:,:) ::  var_real32_3d_pointer  !< output variable
-
-   REAL(KIND=8), POINTER, INTENT(IN), OPTIONAL                   ::  var_real64_0d  !< output variable
-   REAL(KIND=8), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_real64_1d  !< output variable
-   REAL(KIND=8), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_real64_2d  !< output variable
-   REAL(KIND=8), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_real64_3d  !< output variable
-
-   REAL(KIND=8), TARGET, ALLOCATABLE, DIMENSION(:)     ::  var_real64_1d_resorted  !< resorted output variable
-   REAL(KIND=8), TARGET, ALLOCATABLE, DIMENSION(:,:)   ::  var_real64_2d_resorted  !< resorted output variable
-   REAL(KIND=8), TARGET, ALLOCATABLE, DIMENSION(:,:,:) ::  var_real64_3d_resorted  !< resorted output variable
-
-   REAL(KIND=8), POINTER                               ::  var_real64_0d_pointer  !< output variable
-   REAL(KIND=8), POINTER, CONTIGUOUS, DIMENSION(:)     ::  var_real64_1d_pointer  !< output variable
-   REAL(KIND=8), POINTER, CONTIGUOUS, DIMENSION(:,:)   ::  var_real64_2d_pointer  !< output variable
-   REAL(KIND=8), POINTER, CONTIGUOUS, DIMENSION(:,:,:) ::  var_real64_3d_pointer  !< output variable
-
-   REAL(wp), POINTER, INTENT(IN), OPTIONAL                   ::  var_realwp_0d  !< output variable
-   REAL(wp), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_realwp_1d  !< output variable
-   REAL(wp), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_realwp_2d  !< output variable
-   REAL(wp), POINTER, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_realwp_3d  !< output variable
-
-   REAL(wp), TARGET, ALLOCATABLE, DIMENSION(:)     ::  var_realwp_1d_resorted  !< resorted output variable
-   REAL(wp), TARGET, ALLOCATABLE, DIMENSION(:,:)   ::  var_realwp_2d_resorted  !< resorted output variable
-   REAL(wp), TARGET, ALLOCATABLE, DIMENSION(:,:,:) ::  var_realwp_3d_resorted  !< resorted output variable
-
-   REAL(wp), POINTER                               ::  var_realwp_0d_pointer  !< output variable
-   REAL(wp), POINTER, CONTIGUOUS, DIMENSION(:)     ::  var_realwp_1d_pointer  !< output variable
-   REAL(wp), POINTER, CONTIGUOUS, DIMENSION(:,:)   ::  var_realwp_2d_pointer  !< output variable
-   REAL(wp), POINTER, CONTIGUOUS, DIMENSION(:,:,:) ::  var_realwp_3d_pointer  !< output variable
-
-   TYPE(dimension_type), DIMENSION(:), ALLOCATABLE ::  dimension_list  !< list of used dimensions of variable
-
-
-   return_value = 0
-   output_return_value = 0
-
-   CALL internal_message( 'debug', routine_name // ': write ' // TRIM( name ) // &
-                                   ' into file ' // TRIM( filename ) )
-
-   !-- Search for variable within file
-   CALL find_var_in_file( filename, name, file_format, file_id, var_id, &
-                      is_global, dimension_list, return_value=return_value  )
-
-   IF ( return_value == 0 )  THEN
-
-      !-- Check if the correct amount of variable bounds were given
-      IF ( SIZE( bounds_start ) /= SIZE( dimension_list )  .OR.  &
-           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' )
-      ENDIF
-
-   ENDIF
-
-
-   IF ( return_value == 0 )  THEN
-
-      !-- Save starting index (lower bounds) of each dimension
-      ALLOCATE( bounds_origin(SIZE( dimension_list )) )
-      ALLOCATE( bounds_start_internal(SIZE( dimension_list )) )
-      ALLOCATE( value_counts(SIZE( dimension_list )) )
-
-      WRITE( temp_string, * ) bounds_start
-      CALL internal_message( 'debug', routine_name //                     &
-                                      ': file "' // TRIM( filename ) //   &
-                                       '": variable "' // TRIM( name ) // &
-                                       '": bounds_start =' // TRIM( temp_string ) )
-      WRITE( temp_string, * ) bounds_end
-      CALL internal_message( 'debug', routine_name //                     &
-                                      ': file "' // TRIM( filename ) //   &
-                                       '": variable "' // TRIM( name ) // &
-                                       '": bounds_end =' // TRIM( temp_string ) )
-
-      !-- Get bounds for masking
-      CALL get_masked_indices_and_masked_dimension_bounds( dimension_list,                  &
-              bounds_start, bounds_end, bounds_start_internal, value_counts, bounds_origin, &
-              masked_indices )
-
-      do_output = .NOT. ANY( value_counts == 0 )
-
-      WRITE( temp_string, * ) bounds_start_internal
-      CALL internal_message( 'debug', routine_name //                     &
-                                      ': file "' // TRIM( filename ) //   &
-                                       '": variable "' // TRIM( name ) // &
-                                       '": bounds_start_internal =' // TRIM( temp_string ) )
-      WRITE( temp_string, * ) value_counts
-      CALL internal_message( 'debug', routine_name //                     &
-                                      ': file "' // TRIM( filename ) //   &
-                                       '": variable "' // TRIM( name ) // &
-                                       '": value_counts =' // TRIM( temp_string ) )
-
-      !-- Mask and resort variable
-      !-- 8bit integer output
-      IF ( PRESENT( var_int8_0d ) )  THEN
-         var_int8_0d_pointer => var_int8_0d
-      ELSEIF ( PRESENT( var_int8_1d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_int8_1d_resorted(0:value_counts(1)-1) )
-            !$OMP PARALLEL PRIVATE (i)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               var_int8_1d_resorted(i) = var_int8_1d(masked_indices(1,i))
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_int8_1d_resorted(1) )
-            var_int8_1d_resorted = 0_1
-         ENDIF
-         var_int8_1d_pointer => var_int8_1d_resorted
-      ELSEIF ( PRESENT( var_int8_2d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_int8_2d_resorted(0:value_counts(1)-1, &
-                                           0:value_counts(2)-1) )
-            !$OMP PARALLEL PRIVATE (i,j)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               DO  j = 0, value_counts(2) - 1
-                  var_int8_2d_resorted(i,j) = var_int8_2d(masked_indices(2,j), &
-                                                          masked_indices(1,i)  )
-               ENDDO
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_int8_2d_resorted(1,1) )
-            var_int8_2d_resorted = 0_1
-         ENDIF
-         var_int8_2d_pointer => var_int8_2d_resorted
-      ELSEIF ( PRESENT( var_int8_3d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_int8_3d_resorted(0:value_counts(1)-1, &
-                                           0:value_counts(2)-1, &
-                                           0:value_counts(3)-1) )
-            !$OMP PARALLEL PRIVATE (i,j,k)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               DO  j = 0, value_counts(2) - 1
-                  DO  k = 0, value_counts(3) - 1
-                     var_int8_3d_resorted(i,j,k) = var_int8_3d(masked_indices(3,k), &
-                                                               masked_indices(2,j), &
-                                                               masked_indices(1,i)  )
-                  ENDDO
-               ENDDO
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_int8_3d_resorted(1,1,1) )
-            var_int8_3d_resorted = 0_1
-         ENDIF
-         var_int8_3d_pointer => var_int8_3d_resorted
-
-      !-- 16bit integer output
-      ELSEIF ( PRESENT( var_int16_0d ) )  THEN
-         var_int16_0d_pointer => var_int16_0d
-      ELSEIF ( PRESENT( var_int16_1d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_int16_1d_resorted(0:value_counts(1)-1) )
-            !$OMP PARALLEL PRIVATE (i)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               var_int16_1d_resorted(i) = var_int16_1d(masked_indices(1,i))
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_int16_1d_resorted(1) )
-            var_int16_1d_resorted = 0_1
-         ENDIF
-         var_int16_1d_pointer => var_int16_1d_resorted
-      ELSEIF ( PRESENT( var_int16_2d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_int16_2d_resorted(0:value_counts(1)-1, &
-                                            0:value_counts(2)-1) )
-            !$OMP PARALLEL PRIVATE (i,j)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               DO  j = 0, value_counts(2) - 1
-                  var_int16_2d_resorted(i,j) = var_int16_2d(masked_indices(2,j), &
-                                                            masked_indices(1,i))
-               ENDDO
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_int16_2d_resorted(1,1) )
-            var_int16_2d_resorted = 0_1
-         ENDIF
-         var_int16_2d_pointer => var_int16_2d_resorted
-      ELSEIF ( PRESENT( var_int16_3d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_int16_3d_resorted(0:value_counts(1)-1, &
-                                            0:value_counts(2)-1, &
-                                            0:value_counts(3)-1) )
-            !$OMP PARALLEL PRIVATE (i,j,k)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               DO  j = 0, value_counts(2) - 1
-                  DO  k = 0, value_counts(3) - 1
-                     var_int16_3d_resorted(i,j,k) = var_int16_3d(masked_indices(3,k), &
-                                                                 masked_indices(2,j), &
-                                                                 masked_indices(1,i)  )
-                  ENDDO
-               ENDDO
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_int16_3d_resorted(1,1,1) )
-            var_int16_3d_resorted = 0_1
-         ENDIF
-         var_int16_3d_pointer => var_int16_3d_resorted
-
-      !-- 32bit integer output
-      ELSEIF ( PRESENT( var_int32_0d ) )  THEN
-         var_int32_0d_pointer => var_int32_0d
-      ELSEIF ( PRESENT( var_int32_1d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_int32_1d_resorted(0:value_counts(1)-1) )
-            !$OMP PARALLEL PRIVATE (i)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               var_int32_1d_resorted(i) = var_int32_1d(masked_indices(1,i))
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_int32_1d_resorted(1) )
-            var_int32_1d_resorted = 0_1
-         ENDIF
-         var_int32_1d_pointer => var_int32_1d_resorted
-      ELSEIF ( PRESENT( var_int32_2d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_int32_2d_resorted(0:value_counts(1)-1, &
-                                            0:value_counts(2)-1) )
-            !$OMP PARALLEL PRIVATE (i,j)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               DO  j = 0, value_counts(2) - 1
-                  var_int32_2d_resorted(i,j) = var_int32_2d(masked_indices(2,j), &
-                                                            masked_indices(1,i)  )
-               ENDDO
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_int32_2d_resorted(1,1) )
-            var_int32_2d_resorted = 0_1
-         ENDIF
-         var_int32_2d_pointer => var_int32_2d_resorted
-      ELSEIF ( PRESENT( var_int32_3d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_int32_3d_resorted(0:value_counts(1)-1, &
-                                            0:value_counts(2)-1, &
-                                            0:value_counts(3)-1) )
-            !$OMP PARALLEL PRIVATE (i,j,k)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               DO  j = 0, value_counts(2) - 1
-                  DO  k = 0, value_counts(3) - 1
-                     var_int32_3d_resorted(i,j,k) = var_int32_3d(masked_indices(3,k), &
-                                                                 masked_indices(2,j), &
-                                                                 masked_indices(1,i)  )
-                  ENDDO
-               ENDDO
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_int32_3d_resorted(1,1,1) )
-            var_int32_3d_resorted = 0_1
-         ENDIF
-         var_int32_3d_pointer => var_int32_3d_resorted
-
-      !-- working-precision integer output
-      ELSEIF ( PRESENT( var_intwp_0d ) )  THEN
-         var_intwp_0d_pointer => var_intwp_0d
-      ELSEIF ( PRESENT( var_intwp_1d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_intwp_1d_resorted(0:value_counts(1)-1) )
-            !$OMP PARALLEL PRIVATE (i)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               var_intwp_1d_resorted(i) = var_intwp_1d(masked_indices(1,i))
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_intwp_1d_resorted(1) )
-            var_intwp_1d_resorted = 0_1
-         ENDIF
-         var_intwp_1d_pointer => var_intwp_1d_resorted
-      ELSEIF ( PRESENT( var_intwp_2d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_intwp_2d_resorted(0:value_counts(1)-1, &
-                                            0:value_counts(2)-1) )
-            !$OMP PARALLEL PRIVATE (i,j)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               DO  j = 0, value_counts(2) - 1
-                  var_intwp_2d_resorted(i,j) = var_intwp_2d(masked_indices(2,j), &
-                                                            masked_indices(1,i)  )
-               ENDDO
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_intwp_2d_resorted(1,1) )
-            var_intwp_2d_resorted = 0_1
-         ENDIF
-         var_intwp_2d_pointer => var_intwp_2d_resorted
-      ELSEIF ( PRESENT( var_intwp_3d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_intwp_3d_resorted(0:value_counts(1)-1, &
-                                            0:value_counts(2)-1, &
-                                            0:value_counts(3)-1) )
-            !$OMP PARALLEL PRIVATE (i,j,k)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               DO  j = 0, value_counts(2) - 1
-                  DO  k = 0, value_counts(3) - 1
-                     var_intwp_3d_resorted(i,j,k) = var_intwp_3d(masked_indices(3,k), &
-                                                                 masked_indices(2,j), &
-                                                                 masked_indices(1,i)  )
-                  ENDDO
-               ENDDO
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_intwp_3d_resorted(1,1,1) )
-            var_intwp_3d_resorted = 0_1
-         ENDIF
-         var_intwp_3d_pointer => var_intwp_3d_resorted
-
-      !-- 32bit real output
-      ELSEIF ( PRESENT( var_real32_0d ) )  THEN
-         var_real32_0d_pointer => var_real32_0d
-      ELSEIF ( PRESENT( var_real32_1d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_real32_1d_resorted(0:value_counts(1)-1) )
-            !$OMP PARALLEL PRIVATE (i)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               var_real32_1d_resorted(i) = var_real32_1d(masked_indices(1,i))
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_real32_1d_resorted(1) )
-            var_real32_1d_resorted = 0_1
-         ENDIF
-         var_real32_1d_pointer => var_real32_1d_resorted
-      ELSEIF ( PRESENT( var_real32_2d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_real32_2d_resorted(0:value_counts(1)-1, &
-                                             0:value_counts(2)-1) )
-            !$OMP PARALLEL PRIVATE (i,j)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               DO  j = 0, value_counts(2) - 1
-                  var_real32_2d_resorted(i,j) = var_real32_2d(masked_indices(2,j), &
-                                                              masked_indices(1,i)  )
-               ENDDO
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_real32_2d_resorted(1,1) )
-            var_real32_2d_resorted = 0_1
-         ENDIF
-         var_real32_2d_pointer => var_real32_2d_resorted
-      ELSEIF ( PRESENT( var_real32_3d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_real32_3d_resorted(0:value_counts(1)-1, &
-                                             0:value_counts(2)-1, &
-                                             0:value_counts(3)-1) )
-            !$OMP PARALLEL PRIVATE (i,j,k)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               DO  j = 0, value_counts(2) - 1
-                  DO  k = 0, value_counts(3) - 1
-                     var_real32_3d_resorted(i,j,k) = var_real32_3d(masked_indices(3,k), &
-                                                                   masked_indices(2,j), &
-                                                                   masked_indices(1,i)  )
-                  ENDDO
-               ENDDO
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_real32_3d_resorted(1,1,1) )
-            var_real32_3d_resorted = 0_1
-         ENDIF
-         var_real32_3d_pointer => var_real32_3d_resorted
-
-      !-- 64bit real output
-      ELSEIF ( PRESENT( var_real64_0d ) )  THEN
-         var_real64_0d_pointer => var_real64_0d
-      ELSEIF ( PRESENT( var_real64_1d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_real64_1d_resorted(0:value_counts(1)-1) )
-            !$OMP PARALLEL PRIVATE (i)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               var_real64_1d_resorted(i) = var_real64_1d(masked_indices(1,i))
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_real64_1d_resorted(1) )
-            var_real64_1d_resorted = 0_1
-         ENDIF
-         var_real64_1d_pointer => var_real64_1d_resorted
-      ELSEIF ( PRESENT( var_real64_2d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_real64_2d_resorted(0:value_counts(1)-1, &
-                                             0:value_counts(2)-1) )
-            !$OMP PARALLEL PRIVATE (i,j)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               DO  j = 0, value_counts(2) - 1
-                  var_real64_2d_resorted(i,j) = var_real64_2d(masked_indices(2,j), &
-                                                              masked_indices(1,i)  )
-               ENDDO
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_real64_2d_resorted(1,1) )
-            var_real64_2d_resorted = 0_1
-         ENDIF
-         var_real64_2d_pointer => var_real64_2d_resorted
-      ELSEIF ( PRESENT( var_real64_3d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_real64_3d_resorted(0:value_counts(1)-1, &
-                                             0:value_counts(2)-1, &
-                                             0:value_counts(3)-1) )
-            !$OMP PARALLEL PRIVATE (i,j,k)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               DO  j = 0, value_counts(2) - 1
-                  DO  k = 0, value_counts(3) - 1
-                     var_real64_3d_resorted(i,j,k) = var_real64_3d(masked_indices(3,k), &
-                                                                   masked_indices(2,j), &
-                                                                   masked_indices(1,i)  )
-                  ENDDO
-               ENDDO
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_real64_3d_resorted(1,1,1) )
-            var_real64_3d_resorted = 0_1
-         ENDIF
-         var_real64_3d_pointer => var_real64_3d_resorted
-
-      !-- working-precision real output
-      ELSEIF ( PRESENT( var_realwp_0d ) )  THEN
-         var_realwp_0d_pointer => var_realwp_0d
-      ELSEIF ( PRESENT( var_realwp_1d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_realwp_1d_resorted(0:value_counts(1)-1) )
-            !$OMP PARALLEL PRIVATE (i)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               var_realwp_1d_resorted(i) = var_realwp_1d(masked_indices(1,i))
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_realwp_1d_resorted(1) )
-            var_realwp_1d_resorted = 0_1
-         ENDIF
-         var_realwp_1d_pointer => var_realwp_1d_resorted
-      ELSEIF ( PRESENT( var_realwp_2d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_realwp_2d_resorted(0:value_counts(1)-1, &
-                                             0:value_counts(2)-1) )
-            !$OMP PARALLEL PRIVATE (i,j)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               DO  j = 0, value_counts(2) - 1
-                  var_realwp_2d_resorted(i,j) = var_realwp_2d(masked_indices(2,j), &
-                                                              masked_indices(1,i)  )
-               ENDDO
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_realwp_2d_resorted(1,1) )
-            var_realwp_2d_resorted = 0_1
-         ENDIF
-         var_realwp_2d_pointer => var_realwp_2d_resorted
-      ELSEIF ( PRESENT( var_realwp_3d ) )  THEN
-         IF ( do_output ) THEN
-            ALLOCATE( var_realwp_3d_resorted(0:value_counts(1)-1, &
-                                             0:value_counts(2)-1, &
-                                             0:value_counts(3)-1) )
-            !$OMP PARALLEL PRIVATE (i,j,k)
-            !$OMP DO
-            DO  i = 0, value_counts(1) - 1
-               DO  j = 0, value_counts(2) - 1
-                  DO  k = 0, value_counts(3) - 1
-                     var_realwp_3d_resorted(i,j,k) = var_realwp_3d(masked_indices(3,k), &
-                                                                   masked_indices(2,j), &
-                                                                   masked_indices(1,i)  )
-                  ENDDO
-               ENDDO
-            ENDDO
-            !$OMP END PARALLEL
-         ELSE
-            ALLOCATE( var_realwp_3d_resorted(1,1,1) )
-            var_realwp_3d_resorted = 0_1
-         ENDIF
-         var_realwp_3d_pointer => var_realwp_3d_resorted
-
-      ELSE
-         return_value = 1
-         CALL internal_message( 'error', routine_name //                      &
-                                         ': variable "' // TRIM( name ) //    &
-                                         '" in file "' // TRIM( filename ) // &
-                                         '": no values given to output' )
-      ENDIF
-
-      DEALLOCATE( masked_indices )
-
-   ENDIF  ! Check for error
-
-   IF ( return_value == 0 )  THEN
-
-      !-- Write variable into file
-      SELECT CASE ( TRIM( file_format ) )
-
-         CASE ( 'binary' )
-            !-- 8bit integer output
-            IF ( PRESENT( var_int8_0d ) )  THEN
-               CALL binary_write_variable( file_id, var_id,                           &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, 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 binary_write_variable( file_id, var_id,                           &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, 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 binary_write_variable( file_id, var_id,                           &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, is_global, &
-                       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_internal, value_counts, bounds_origin, 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', 'netcdf4-serial' )
-            !-- 8bit integer output
-            IF ( PRESENT( var_int8_0d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_int8_0d=var_int8_0d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_int8_1d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_int8_1d=var_int8_1d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_int8_2d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_int8_2d=var_int8_2d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_int8_3d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, 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_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_int16_0d=var_int16_0d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_int16_1d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_int16_1d=var_int16_1d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_int16_2d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_int16_2d=var_int16_2d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_int16_3d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, 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_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_int32_0d=var_int32_0d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_int32_1d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_int32_1d=var_int32_1d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_int32_2d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_int32_2d=var_int32_2d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_int32_3d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, 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_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_intwp_0d=var_intwp_0d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_intwp_1d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_intwp_1d=var_intwp_1d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_intwp_2d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_intwp_2d=var_intwp_2d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_intwp_3d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, 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_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_real32_0d=var_real32_0d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_real32_1d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_real32_1d=var_real32_1d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_real32_2d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_real32_2d=var_real32_2d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_real32_3d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, 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_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_real64_0d=var_real64_0d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_real64_1d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_real64_1d=var_real64_1d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_real64_2d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_real64_2d=var_real64_2d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_real64_3d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, 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_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_realwp_0d=var_realwp_0d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_realwp_1d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_realwp_1d=var_realwp_1d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_realwp_2d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, is_global, &
-                       var_realwp_2d=var_realwp_2d_pointer, return_value=output_return_value )
-            ELSEIF ( PRESENT( var_realwp_3d ) )  THEN
-               CALL netcdf4_write_variable( file_id, var_id,                          &
-                       bounds_start_internal, value_counts, bounds_origin, 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 DEFAULT
-            return_value = 1
-            CALL internal_message( 'error', routine_name //                              &
-                                            ': file "' // TRIM( filename ) //            &
-                                            '": file format "' // TRIM( file_format ) // &
-                                            '" not supported' )
-
-      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
-
-END FUNCTION dom_write_var
-
-!--------------------------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Find a requested variable 'var_name' and its used dimensions in requested file 'filename'.
-!--------------------------------------------------------------------------------------------------!
-SUBROUTINE find_var_in_file( filename, var_name, file_format, file_id, var_id, &
+!> Find a requested variable 'variable_name' and its used dimensions in requested file 'file_name'.
+!--------------------------------------------------------------------------------------------------!
+SUBROUTINE find_var_in_file( file_name, variable_name, file_format, file_id, variable_id, &
                              is_global, dimensions, return_value )
 
-   CHARACTER(LEN=charlen), INTENT(OUT) ::  file_format  !< file format chosen for file
-   CHARACTER(LEN=*),       INTENT(IN)  ::  filename     !< name of file
-   CHARACTER(LEN=*),       INTENT(IN)  ::  var_name     !< name of variable
+   CHARACTER(LEN=charlen), INTENT(OUT) ::  file_format    !< file format chosen for file
+   CHARACTER(LEN=*),       INTENT(IN)  ::  file_name      !< name of file
+   CHARACTER(LEN=*),       INTENT(IN)  ::  variable_name  !< name of variable
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'find_var_in_file'  !< name of routine
 
-   INTEGER(iwp)              ::  d             !< loop index
-   INTEGER(iwp)              ::  dd            !< loop index
-   INTEGER(iwp)              ::  f             !< loop index
-   INTEGER(iwp), INTENT(OUT) ::  file_id       !< file ID
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
-   INTEGER(iwp), INTENT(OUT) ::  var_id        !< variable ID
-
-   INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  dim_ids  !< list of dimension IDs used by variable
+   INTEGER              ::  d             !< loop index
+   INTEGER              ::  dd            !< loop index
+   INTEGER              ::  f             !< loop index
+   INTEGER, INTENT(OUT) ::  file_id       !< file ID
+   INTEGER, INTENT(OUT) ::  return_value  !< return value
+   INTEGER, INTENT(OUT) ::  variable_id   !< variable ID
+
+   INTEGER, DIMENSION(:), ALLOCATABLE ::  dimension_ids  !< list of dimension IDs used by variable
 
    LOGICAL              ::  found      !< true if requested variable found in requested file
@@ -3532,17 +3374,17 @@
 
 
-   return_value   = 0
+   return_value = 0
    found = .FALSE.
 
-   DO  f = 1, nf
-      IF ( TRIM( filename ) == TRIM( files(f)%name ) )  THEN
+   DO  f = 1, nfiles
+      IF ( TRIM( file_name ) == 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.' )
+            CALL internal_message( 'error', routine_name //                     &
+                                   ': file not initialized. ' //                &
+                                   'Writing variable to file is impossible ' // &
+                                   '(variable "' // TRIM( variable_name ) //    &
+                                   '", file "' // TRIM( file_name ) // '")!' )
             EXIT
          ENDIF
@@ -3553,13 +3395,13 @@
          !-- Search for variable in file
          DO  d = 1, SIZE( files(f)%variables )
-            IF ( TRIM( var_name ) == TRIM( files(f)%variables(d)%name ) )  THEN
-
-               var_id    = files(f)%variables(d)%id
+            IF ( TRIM( variable_name ) == TRIM( files(f)%variables(d)%name ) )  THEN
+
+               variable_id    = files(f)%variables(d)%id
                is_global = files(f)%variables(d)%is_global
 
-               ALLOCATE( dim_ids(SIZE( files(f)%variables(d)%dimension_ids )) )
+               ALLOCATE( dimension_ids(SIZE( files(f)%variables(d)%dimension_ids )) )
                ALLOCATE( dimensions(SIZE( files(f)%variables(d)%dimension_ids )) )
 
-               dim_ids = files(f)%variables(d)%dimension_ids
+               dimension_ids = files(f)%variables(d)%dimension_ids
 
                found = .TRUE.
@@ -3573,6 +3415,6 @@
             !-- Get list of dimensions used by variable
             DO  d = 1, SIZE( files(f)%dimensions )
-               DO  dd = 1, SIZE( dim_ids )
-                  IF ( dim_ids(dd) == files(f)%dimensions(d)%id )  THEN
+               DO  dd = 1, SIZE( dimension_ids )
+                  IF ( dimension_ids(dd) == files(f)%dimensions(d)%id )  THEN
                      dimensions(dd) = files(f)%dimensions(d)
                      EXIT
@@ -3585,7 +3427,7 @@
             !-- If variable was not found, search for a dimension instead
             DO  d = 1, SIZE( files(f)%dimensions )
-               IF ( TRIM( var_name ) == TRIM( files(f)%dimensions(d)%name ) )  THEN
-
-                  var_id    = files(f)%dimensions(d)%var_id
+               IF ( TRIM( variable_name ) == TRIM( files(f)%dimensions(d)%name ) )  THEN
+
+                  variable_id    = files(f)%dimensions(d)%variable_id
                   is_global = .TRUE.
 
@@ -3605,7 +3447,8 @@
          IF ( .NOT. found )  THEN
             return_value = 1
-            CALL internal_message( 'error', routine_name //                       &
-                                            ': variable "' // TRIM( var_name ) // &
-                                            '" not found in file "' // TRIM( filename ) // '"' )
+            CALL internal_message( 'error', routine_name //                  &
+                                   ': variable not found in file ' //        &
+                                   '(variable "' // TRIM( variable_name ) // &
+                                   '", file "' // TRIM( file_name ) // '")!' )
          ENDIF
 
@@ -3617,8 +3460,8 @@
    IF ( .NOT. found  .AND.  return_value == 0 )  THEN
       return_value = 1
-      CALL internal_message( 'error', routine_name //                           &
-                                      ': file "' // TRIM( filename ) //         &
-                                      '" for variable "' // TRIM( var_name ) // &
-                                      '" not found' )
+      CALL internal_message( 'error', routine_name //                  &
+                             ': file not found ' //                    &
+                             '(variable "' // TRIM( variable_name ) // &
+                             '", file "' // TRIM( file_name ) // '")!' )
    ENDIF
 
@@ -3641,14 +3484,14 @@
    ! CHARACTER(LEN=*), PARAMETER ::  routine_name = 'get_masked_indices_and_masked_dimension_bounds'  !< name of routine
 
-   INTEGER(iwp) ::  d  !< loop index
-   INTEGER(iwp) ::  i  !< loop index
-
-   INTEGER(iwp), DIMENSION(:), INTENT(IN)  ::  bounds_end           !< upper bonuds to be searched in
-   INTEGER(iwp), DIMENSION(:), INTENT(OUT) ::  bounds_masked_start  !< lower bounds of masked dimensions within given bounds
-   INTEGER(iwp), DIMENSION(:), INTENT(OUT) ::  bounds_origin        !< first index of each dimension, 0 if dimension is masked
-   INTEGER(iwp), DIMENSION(:), INTENT(IN)  ::  bounds_start         !< lower bounds to be searched in
-   INTEGER(iwp), DIMENSION(:), INTENT(OUT) ::  value_counts         !< count of indices per dimension to be output
-
-   INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE, INTENT(OUT) ::  masked_indices  !< masked indices within given bounds
+   INTEGER ::  d  !< loop index
+   INTEGER ::  i  !< loop index
+
+   INTEGER, DIMENSION(:), INTENT(IN)  ::  bounds_end           !< upper bonuds to be searched in
+   INTEGER, DIMENSION(:), INTENT(OUT) ::  bounds_masked_start  !< lower bounds of masked dimensions within given bounds
+   INTEGER, DIMENSION(:), INTENT(OUT) ::  bounds_origin        !< first index of each dimension, 0 if dimension is masked
+   INTEGER, DIMENSION(:), INTENT(IN)  ::  bounds_start         !< lower bounds to be searched in
+   INTEGER, DIMENSION(:), INTENT(OUT) ::  value_counts         !< count of indices per dimension to be output
+
+   INTEGER, DIMENSION(:,:), ALLOCATABLE, INTENT(OUT) ::  masked_indices  !< masked indices within given bounds
 
    TYPE(dimension_type), DIMENSION(:), INTENT(IN) ::  dimensions  !< dimensions to be searched for masked indices
@@ -3656,5 +3499,5 @@
 
    ALLOCATE( masked_indices(SIZE( dimensions ),0:MAXVAL( bounds_end - bounds_start + 1 )) )
-   masked_indices = -HUGE( 0_iwp )
+   masked_indices = -HUGE( 0 )
 
    !-- Check for masking and update lower and upper bounds if masked
@@ -3665,5 +3508,5 @@
          bounds_origin(d) = 0
 
-         bounds_masked_start(d) = -HUGE( 0_iwp )
+         bounds_masked_start(d) = -HUGE( 0 )
 
          !-- Find number of masked values within given variable bounds
@@ -3681,5 +3524,5 @@
 
                !-- Save bounds of mask within given bounds
-               IF ( bounds_masked_start(d) == -HUGE( 0_iwp ) )  bounds_masked_start(d) = i
+               IF ( bounds_masked_start(d) == -HUGE( 0 ) )  bounds_masked_start(d) = i
 
             ENDIF
@@ -3690,6 +3533,6 @@
          IF ( value_counts(d) == 0 )  THEN
             bounds_origin(:) = 0
-            bounds_masked_start(:) = 0_iwp
-            value_counts(:) = 0_iwp
+            bounds_masked_start(:) = 0
+            value_counts(:) = 0
             EXIT
          ENDIF
@@ -3715,101 +3558,430 @@
 ! Description:
 ! ------------
-!> Finalize output.
-!--------------------------------------------------------------------------------------------------!
-FUNCTION dom_finalize_output() RESULT( return_value )
-
-   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_finalize_output'  !< name of routine
-
-   INTEGER(iwp) ::  return_value           !< return value
-   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
-
-
-   return_value = 0
-
-   DO  f = 1, nf
-
-      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
-
-END FUNCTION dom_finalize_output
+!> Message routine writing debug information into the debug file
+!> or creating the error message string.
+!--------------------------------------------------------------------------------------------------!
+SUBROUTINE internal_message( level, string )
+
+   CHARACTER(LEN=*), INTENT(IN) ::  level   !< message importance level
+   CHARACTER(LEN=*), INTENT(IN) ::  string  !< message string
+
+
+   IF ( TRIM( level ) == 'error' )  THEN
+
+      WRITE( internal_error_message, '(A,A)' ) 'DOM ERROR: ', string
+
+   ELSEIF ( TRIM( level ) == 'debug'  .AND.  print_debug_output )  THEN
+
+      WRITE( debug_output_unit, '(A,A)' ) 'DOM DEBUG: ', string
+      FLUSH( debug_output_unit )
+
+   ENDIF
+
+END SUBROUTINE internal_message
 
 !--------------------------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> Message routine writing debug information into the debug file
-!> or creating the error message string.
-!--------------------------------------------------------------------------------------------------!
-SUBROUTINE internal_message( level, string )
-
-   CHARACTER(LEN=*), INTENT(IN) ::  level   !< message importance level
-   CHARACTER(LEN=*), INTENT(IN) ::  string  !< message string
-
-
-   IF ( TRIM( level ) == 'error' )  THEN
-
-      WRITE( internal_error_message, '(A,A)' ) 'DOM ERROR: ', string
-
-   ELSEIF ( TRIM( level ) == 'debug'  .AND.  print_debug_output )  THEN
-
-      WRITE( debug_output_unit, '(A,A)' ) 'DOM DEBUG: ', string
-      FLUSH( debug_output_unit )
+!> Print contents of the created database to debug_output_unit. This routine can be called at any
+!> stage after the call to 'dom_init'. Multiple calls are possible.
+!--------------------------------------------------------------------------------------------------!
+SUBROUTINE dom_database_debug_output
+
+   CHARACTER(LEN=*), PARAMETER ::  separation_string = '---'                   !< string separating blocks in output
+   CHARACTER(LEN=50)           ::  write_format1                               !< format for write statements
+   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'dom_database_debug_output'  !< name of this routine
+
+   INTEGER            ::  f                       !< loop index
+   INTEGER, PARAMETER ::  indent_depth = 3        !< space per indentation
+   INTEGER            ::  indent_level            !< indentation level
+   INTEGER, PARAMETER ::  max_keyname_length = 6  !< length of longest key name
+   INTEGER            ::  natts                   !< number of attributes
+   INTEGER            ::  ndims                   !< number of dimensions
+   INTEGER            ::  nvars                   !< number of variables
+
+
+   CALL internal_message( 'debug', routine_name // ': write database to debug output' )
+
+   WRITE( debug_output_unit, '(A)' ) 'DOM database:'
+   WRITE( debug_output_unit, '(A)' ) REPEAT( separation_string // ' ', 4 )
+
+   IF ( .NOT. ALLOCATED( files ) .OR. nfiles == 0 )  THEN
+
+      WRITE( debug_output_unit, '(A)' ) 'database is empty'
+
+   ELSE
+
+      indent_level = 1
+      WRITE( write_format1, '(A,I3,A,I3,A)' ) '(T', indent_level * indent_depth + 1, ',A,T',  &
+                                        indent_level * indent_depth + 1 + max_keyname_length, &
+                                        ',(": ")'
+
+      DO  f = 1, nfiles
+
+         natts = 0
+         ndims = 0
+         nvars = 0
+         IF ( ALLOCATED( files(f)%attributes ) ) natts = SIZE( files(f)%attributes )
+         IF ( ALLOCATED( files(f)%dimensions ) ) ndims = SIZE( files(f)%dimensions )
+         IF ( ALLOCATED( files(f)%variables  ) ) nvars = SIZE( files(f)%variables  )
+
+         WRITE( debug_output_unit, '(A)' ) 'file:'
+         WRITE( debug_output_unit, TRIM( write_format1 ) // ',A)' ) 'name', TRIM( files(f)%name )
+         WRITE( debug_output_unit, TRIM( write_format1 ) // ',A)' ) 'format', TRIM(files(f)%format)
+         WRITE( debug_output_unit, TRIM( write_format1 ) // ',I7)' ) 'id', files(f)%id
+         WRITE( debug_output_unit, TRIM( write_format1 ) // ',L1)' ) 'is init', files(f)%is_init
+         WRITE( debug_output_unit, TRIM( write_format1 ) // ',I7)' ) '#atts', natts
+         WRITE( debug_output_unit, TRIM( write_format1 ) // ',I7)' ) '#dims', ndims
+         WRITE( debug_output_unit, TRIM( write_format1 ) // ',I7)' ) '#vars', nvars
+
+         IF ( natts /= 0 )  CALL print_attributes( indent_level, files(f)%attributes )
+         IF ( ndims /= 0 )  CALL print_dimensions( indent_level, files(f)%dimensions )
+         IF ( nvars /= 0 )  CALL print_variables( indent_level, files(f)%variables )
+
+         WRITE( debug_output_unit, '(/A/)' ) REPEAT( separation_string // ' ', 4 )
+
+      ENDDO
 
    ENDIF
 
-END SUBROUTINE internal_message
-
-!--------------------------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Return the last created error message.
-!--------------------------------------------------------------------------------------------------!
-SUBROUTINE dom_get_error_message( error_message )
-
-   CHARACTER(LEN=800), INTENT(OUT) ::  error_message         !< return error message to main program
-   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
-
-END SUBROUTINE dom_get_error_message
+   CONTAINS
+
+      !--------------------------------------------------------------------------------------------!
+      ! Description:
+      ! ------------
+      !> Print list of attributes.
+      !--------------------------------------------------------------------------------------------!
+      SUBROUTINE print_attributes( indent_level, attributes )
+
+         CHARACTER(LEN=50) ::  write_format1  !< format for write statements
+         CHARACTER(LEN=50) ::  write_format2  !< format for write statements
+
+         INTEGER             ::  i                       !< loop index
+         INTEGER, INTENT(IN) ::  indent_level            !< indentation level
+         INTEGER, PARAMETER  ::  max_keyname_length = 6  !< length of longest key name
+         INTEGER             ::  nelement                !< number of elements to print
+
+         TYPE(attribute_type), DIMENSION(:), INTENT(IN) ::  attributes  !< list of attributes
+
+
+         WRITE( write_format1, '(A,I3,A)' ) '(T', indent_level * indent_depth + 1, ',A'
+         WRITE( write_format2, '(A,I3,A,I3,A)' ) &
+            '(T', ( indent_level + 1 ) * indent_depth + 1, ',A,T', &
+            ( indent_level + 1 ) * indent_depth + 1 + max_keyname_length, ',(": ")'
+
+         WRITE( debug_output_unit, TRIM( write_format1 ) // ',A)' ) &
+            REPEAT( separation_string // ' ', 4 )
+         WRITE( debug_output_unit, TRIM( write_format1 ) // ')' ) 'attributes:'
+
+         nelement = SIZE( attributes )
+         DO  i = 1, nelement
+            WRITE( debug_output_unit, TRIM( write_format2 ) // ',A)' ) &
+               'name', TRIM( attributes(i)%name )
+            WRITE( debug_output_unit, TRIM( write_format2 ) // ',A)' ) &
+               'type', TRIM( attributes(i)%data_type )
+
+            IF ( TRIM( attributes(i)%data_type ) == 'char' )  THEN
+               WRITE( debug_output_unit, TRIM( write_format2 ) // ',A)' ) &
+                  'value', TRIM( attributes(i)%value_char )
+            ELSEIF ( TRIM( attributes(i)%data_type ) == 'int8' )  THEN
+               WRITE( debug_output_unit, TRIM( write_format2 ) // ',I4)' ) &
+                  'value', attributes(i)%value_int8
+            ELSEIF ( TRIM( attributes(i)%data_type ) == 'int16' )  THEN
+               WRITE( debug_output_unit, TRIM( write_format2 ) // ',I6)' ) &
+                  'value', attributes(i)%value_int16
+            ELSEIF ( TRIM( attributes(i)%data_type ) == 'int32' )  THEN
+               WRITE( debug_output_unit, TRIM( write_format2 ) // ',I11)' ) &
+                  'value', attributes(i)%value_int32
+            ELSEIF ( TRIM( attributes(i)%data_type ) == 'real32' )  THEN
+               WRITE( debug_output_unit, TRIM( write_format2 ) // ',E14.7)' ) &
+                  'value', attributes(i)%value_real32
+            ELSEIF (  TRIM(attributes(i)%data_type) == 'real64' )  THEN
+               WRITE( debug_output_unit, TRIM( write_format2 ) // ',E22.15)' ) &
+                  'value', attributes(i)%value_real64
+            ENDIF
+            IF ( i < nelement )  &
+               WRITE( debug_output_unit, TRIM( write_format1 ) // ')' ) separation_string
+         ENDDO
+
+      END SUBROUTINE print_attributes
+
+      !--------------------------------------------------------------------------------------------!
+      ! Description:
+      ! ------------
+      !> Print list of dimensions.
+      !--------------------------------------------------------------------------------------------!
+      SUBROUTINE print_dimensions( indent_level, dimensions )
+
+         CHARACTER(LEN=50) ::  write_format1  !< format for write statements
+         CHARACTER(LEN=50) ::  write_format2  !< format for write statements
+
+         INTEGER             ::  i                        !< loop index
+         INTEGER, INTENT(IN) ::  indent_level             !< indentation level
+         INTEGER             ::  j                        !< loop index
+         INTEGER, PARAMETER  ::  max_keyname_length = 15  !< length of longest key name
+         INTEGER             ::  nelement                 !< number of elements to print
+
+         LOGICAL ::  is_masked  !< true if dimension is masked
+
+         TYPE(dimension_type), DIMENSION(:), INTENT(IN) ::  dimensions  !< list of dimensions
+
+
+         WRITE( write_format1, '(A,I3,A)' ) '(T', indent_level * indent_depth + 1, ',A'
+         WRITE( write_format2, '(A,I3,A,I3,A)' ) &
+            '(T', ( indent_level + 1 ) * indent_depth + 1, ',A,T', &
+            ( indent_level + 1 ) * indent_depth + 1 + max_keyname_length, ',(": ")'
+
+         WRITE( debug_output_unit, TRIM( write_format1 ) // ',A)' ) &
+            REPEAT( separation_string // ' ', 4 )
+         WRITE( debug_output_unit, TRIM( write_format1 ) // ')' ) 'dimensions:'
+
+         nelement = SIZE( dimensions )
+         DO  i = 1, nelement
+            is_masked = dimensions(i)%is_masked
+
+            !-- Print general information
+            WRITE( debug_output_unit, TRIM( write_format2 ) // ',A)' ) &
+               'name', TRIM( dimensions(i)%name )
+            WRITE( debug_output_unit, TRIM( write_format2 ) // ',A)' ) &
+               'type', TRIM( dimensions(i)%data_type )
+            WRITE( debug_output_unit, TRIM( write_format2 ) // ',I7)' ) &
+               'id', dimensions(i)%id
+            WRITE( debug_output_unit, TRIM( write_format2 ) // ',I7)' ) &
+               'length', dimensions(i)%length
+            WRITE( debug_output_unit, TRIM( write_format2 ) // ',I7,A,I7)' ) &
+               'bounds', dimensions(i)%bounds(1), ',', dimensions(i)%bounds(2)
+            WRITE( debug_output_unit, TRIM( write_format2 ) // ',L1)' ) &
+               'is masked', dimensions(i)%is_masked
+
+            !-- Print information about mask
+            IF ( is_masked )  THEN
+               WRITE( debug_output_unit, TRIM( write_format2 ) // ',I7)' ) &
+                  'masked length', dimensions(i)%length_mask
+
+               WRITE( debug_output_unit, TRIM( write_format2 ) // ',L1)', ADVANCE='no' ) &
+                  'mask', dimensions(i)%mask(dimensions(i)%bounds(1))
+               DO  j = dimensions(i)%bounds(1)+1, dimensions(i)%bounds(2)
+                  WRITE( debug_output_unit, '(A,L1)', ADVANCE='no' ) ',', dimensions(i)%mask(j)
+               ENDDO
+               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+
+               WRITE( debug_output_unit, TRIM( write_format2 ) // ',I6)', ADVANCE='no' ) &
+                  'masked indices', dimensions(i)%masked_indices(0)
+               DO  j = 1, dimensions(i)%length_mask-1
+                  WRITE( debug_output_unit, '(A,I6)', ADVANCE='no' ) &
+                     ',', dimensions(i)%masked_indices(j)
+               ENDDO
+               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+            ENDIF
+
+            !-- Print saved values
+            IF ( ALLOCATED( dimensions(i)%values_int8 ) )  THEN
+
+               WRITE( debug_output_unit, TRIM( write_format2 ) // ',I4)', ADVANCE='no' ) &
+                  'values', dimensions(i)%values_int8(dimensions(i)%bounds(1))
+               DO  j = dimensions(i)%bounds(1)+1, dimensions(i)%bounds(2)
+                  WRITE( debug_output_unit, '(A,I4)', ADVANCE='no' ) &
+                     ',', dimensions(i)%values_int8(j)
+               ENDDO
+               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+               IF ( is_masked )  THEN
+                  WRITE( debug_output_unit, TRIM( write_format2 ) // ',I4)', ADVANCE='no' ) &
+                     'masked values', dimensions(i)%masked_values_int8(0)
+                  DO  j = 1, dimensions(i)%length_mask-1
+                     WRITE( debug_output_unit, '(A,I4)', ADVANCE='no' ) &
+                        ',', dimensions(i)%masked_values_int8(j)
+                  ENDDO
+                  WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+               ENDIF
+
+            ELSEIF ( ALLOCATED( dimensions(i)%values_int16 ) )  THEN
+
+               WRITE( debug_output_unit, TRIM( write_format2 ) // ',I6)', ADVANCE='no' ) &
+                  'values', dimensions(i)%values_int16(dimensions(i)%bounds(1))
+               DO  j = dimensions(i)%bounds(1)+1, dimensions(i)%bounds(2)
+                  WRITE( debug_output_unit, '(A,I6)', ADVANCE='no' ) &
+                     ',', dimensions(i)%values_int16(j)
+               ENDDO
+               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+               IF ( is_masked )  THEN
+                  WRITE( debug_output_unit, TRIM( write_format2 ) // ',I6)', ADVANCE='no' ) &
+                     'masked values', dimensions(i)%masked_values_int16(0)
+                  DO  j = 1, dimensions(i)%length_mask-1
+                     WRITE( debug_output_unit, '(A,I6)', ADVANCE='no' ) &
+                        ',', dimensions(i)%masked_values_int16(j)
+                  ENDDO
+                  WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+               ENDIF
+
+            ELSEIF ( ALLOCATED( dimensions(i)%values_int32 ) )  THEN
+
+               WRITE( debug_output_unit, TRIM( write_format2 ) // ',I11)', ADVANCE='no' ) &
+                  'values', dimensions(i)%values_int32(dimensions(i)%bounds(1))
+               DO  j = dimensions(i)%bounds(1)+1, dimensions(i)%bounds(2)
+                  WRITE( debug_output_unit, '(A,I11)', ADVANCE='no' ) &
+                     ',', dimensions(i)%values_int32(j)
+               ENDDO
+               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+               IF ( is_masked )  THEN
+                  WRITE( debug_output_unit, TRIM( write_format2 ) // ',I11)', ADVANCE='no' ) &
+                     'masked values', dimensions(i)%masked_values_int32(0)
+                  DO  j = 1, dimensions(i)%length_mask-1
+                     WRITE( debug_output_unit, '(A,I11)', ADVANCE='no' ) &
+                        ',', dimensions(i)%masked_values_int32(j)
+                  ENDDO
+                  WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+               ENDIF
+
+            ELSEIF ( ALLOCATED( dimensions(i)%values_intwp ) )  THEN
+
+               WRITE( debug_output_unit, TRIM( write_format2 ) // ',I11)', ADVANCE='no' ) &
+                  'values', dimensions(i)%values_intwp(dimensions(i)%bounds(1))
+               DO  j = dimensions(i)%bounds(1)+1, dimensions(i)%bounds(2)
+                  WRITE( debug_output_unit, '(A,I11)', ADVANCE='no' ) &
+                     ',', dimensions(i)%values_intwp(j)
+               ENDDO
+               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+               IF ( is_masked )  THEN
+                  WRITE( debug_output_unit, TRIM( write_format2 ) // ',I11)', ADVANCE='no' ) &
+                     'masked values', dimensions(i)%masked_values_intwp(0)
+                  DO  j = 1, dimensions(i)%length_mask-1
+                     WRITE( debug_output_unit, '(A,I11)', ADVANCE='no' ) &
+                        ',', dimensions(i)%masked_values_intwp(j)
+                  ENDDO
+                  WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+               ENDIF
+
+            ELSEIF ( ALLOCATED( dimensions(i)%values_real32 ) )  THEN
+
+               WRITE( debug_output_unit, TRIM( write_format2 ) // ',E14.7)', ADVANCE='no' ) &
+                  'values', dimensions(i)%values_real32(dimensions(i)%bounds(1))
+               DO  j = dimensions(i)%bounds(1)+1, dimensions(i)%bounds(2)
+                  WRITE( debug_output_unit, '(A,E14.7)', ADVANCE='no' ) &
+                     ',', dimensions(i)%values_real32(j)
+               ENDDO
+               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+               IF ( is_masked )  THEN
+                  WRITE( debug_output_unit, TRIM( write_format2 ) // ',E14.7)', ADVANCE='no' ) &
+                     'masked values', dimensions(i)%masked_values_real32(0)
+                  DO  j = 1, dimensions(i)%length_mask-1
+                     WRITE( debug_output_unit, '(A,E14.7)', ADVANCE='no' ) &
+                        ',', dimensions(i)%masked_values_real32(j)
+                  ENDDO
+                  WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+               ENDIF
+
+            ELSEIF ( ALLOCATED( dimensions(i)%values_real64 ) )  THEN
+
+               WRITE( debug_output_unit, TRIM( write_format2 ) // ',E22.15)', ADVANCE='no' ) &
+                  'values', dimensions(i)%values_real64(dimensions(i)%bounds(1))
+               DO  j = dimensions(i)%bounds(1)+1, dimensions(i)%bounds(2)
+                  WRITE( debug_output_unit, '(A,E22.15)', ADVANCE='no' ) &
+                     ',', dimensions(i)%values_real64(j)
+               ENDDO
+               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+               IF ( is_masked )  THEN
+                  WRITE( debug_output_unit, TRIM( write_format2 ) // ',E22.15)', ADVANCE='no' ) &
+                     'masked values', dimensions(i)%masked_values_real64(0)
+                  DO  j = 1, dimensions(i)%length_mask-1
+                     WRITE( debug_output_unit, '(A,E22.15)', ADVANCE='no' ) &
+                        ',', dimensions(i)%masked_values_real64(j)
+                  ENDDO
+                  WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+               ENDIF
+
+            ELSEIF ( ALLOCATED( dimensions(i)%values_realwp ) )  THEN
+
+               WRITE( debug_output_unit, TRIM( write_format2 ) // ',E22.15)', ADVANCE='no' ) &
+                  'values', dimensions(i)%values_realwp(dimensions(i)%bounds(1))
+               DO  j = dimensions(i)%bounds(1)+1, dimensions(i)%bounds(2)
+                  WRITE( debug_output_unit, '(A,E22.15)', ADVANCE='no' ) &
+                     ',', dimensions(i)%values_realwp(j)
+               ENDDO
+               WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+               IF ( is_masked )  THEN
+                  WRITE( debug_output_unit, TRIM( write_format2 ) // ',E22.15)', ADVANCE='no' ) &
+                     'masked values', dimensions(i)%masked_values_realwp(0)
+                  DO  j = 1, dimensions(i)%length_mask-1
+                     WRITE( debug_output_unit, '(A,E22.15)', ADVANCE='no' ) &
+                        ',', dimensions(i)%masked_values_realwp(j)
+                  ENDDO
+                  WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+               ENDIF
+
+            ENDIF
+
+            IF ( ALLOCATED( dimensions(i)%attributes ) )  &
+               CALL print_attributes( indent_level+1, dimensions(i)%attributes )
+
+            IF ( i < nelement )  &
+               WRITE( debug_output_unit, TRIM( write_format1 ) // ')' ) separation_string
+         ENDDO
+
+      END SUBROUTINE print_dimensions
+
+      !--------------------------------------------------------------------------------------------!
+      ! Description:
+      ! ------------
+      !> Print list of variables.
+      !--------------------------------------------------------------------------------------------!
+      SUBROUTINE print_variables( indent_level, variables )
+
+         CHARACTER(LEN=50) ::  write_format1  !< format for write statements
+         CHARACTER(LEN=50) ::  write_format2  !< format for write statements
+
+         INTEGER             ::  i                        !< loop index
+         INTEGER, INTENT(IN) ::  indent_level             !< indentation level
+         INTEGER             ::  j                        !< loop index
+         INTEGER, PARAMETER  ::  max_keyname_length = 16  !< length of longest key name
+         INTEGER             ::  nelement                 !< number of elements to print
+
+         TYPE(variable_type), DIMENSION(:), INTENT(IN) ::  variables  !< list of variables
+
+
+         WRITE( write_format1, '(A,I3,A)' ) '(T', indent_level * indent_depth + 1, ',A'
+         WRITE( write_format2, '(A,I3,A,I3,A)' ) &
+            '(T', ( indent_level + 1 ) * indent_depth + 1, ',A,T', &
+            ( indent_level + 1 ) * indent_depth + 1 + max_keyname_length, ',(": ")'
+
+         WRITE( debug_output_unit, TRIM( write_format1 ) // ',A)' ) &
+            REPEAT( separation_string // ' ', 4 )
+         WRITE( debug_output_unit, TRIM( write_format1 ) // ')' ) 'variables:'
+
+         nelement = SIZE( variables )
+         DO  i = 1, nelement
+            WRITE( debug_output_unit, TRIM( write_format2 ) // ',A)' ) &
+               'name', TRIM( variables(i)%name )
+            WRITE( debug_output_unit, TRIM( write_format2 ) // ',A)' ) &
+               'type', TRIM( variables(i)%data_type )
+            WRITE( debug_output_unit, TRIM( write_format2 ) // ',I7)' ) &
+               'id', variables(i)%id
+            WRITE( debug_output_unit, TRIM( write_format2 ) // ',L1)' ) &
+               'is global', variables(i)%is_global
+
+            WRITE( debug_output_unit, TRIM( write_format2 ) // ',A)', ADVANCE='no' ) &
+               'dimension names', TRIM( variables(i)%dimension_names(1) )
+            DO  j = 2, SIZE( variables(i)%dimension_names )
+               WRITE( debug_output_unit, '(A,A)', ADVANCE='no' ) &
+                  ',', TRIM( variables(i)%dimension_names(j) )
+            ENDDO
+            WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+
+            WRITE( debug_output_unit, TRIM( write_format2 ) // ',I7)', ADVANCE='no' ) &
+               'dimension ids', variables(i)%dimension_ids(1)
+            DO  j = 2, SIZE( variables(i)%dimension_names )
+               WRITE( debug_output_unit, '(A,I7)', ADVANCE='no' ) &
+                  ',', variables(i)%dimension_ids(j)
+            ENDDO
+            WRITE( debug_output_unit, '(A)' )  ''  ! write line-end
+
+            IF ( ALLOCATED( variables(i)%attributes ) )  &
+               CALL print_attributes( indent_level+1, variables(i)%attributes )
+            IF ( i < nelement )  &
+               WRITE( debug_output_unit, TRIM( write_format1 ) // ')' ) separation_string
+         ENDDO
+
+      END SUBROUTINE print_variables
+
+END SUBROUTINE dom_database_debug_output
 
 END MODULE data_output_module
Index: /palm/trunk/SOURCE/data_output_netcdf4_module.f90
===================================================================
--- /palm/trunk/SOURCE/data_output_netcdf4_module.f90	(revision 4140)
+++ /palm/trunk/SOURCE/data_output_netcdf4_module.f90	(revision 4141)
@@ -62,8 +62,8 @@
    CHARACTER(LEN=*), PARAMETER ::  mode_serial   = 'serial'    !< string selecting netcdf4 serial mode
 
-   INTEGER(iwp) ::  debug_output_unit       !< Fortran Unit Number of the debug-output file
-   INTEGER(iwp) ::  global_id_in_file = -1  !< value of global ID within a file
-   INTEGER      ::  master_rank             !< master rank for tasks to be executed by single PE only
-   INTEGER      ::  output_group_comm       !< MPI communicator addressing all MPI ranks which participate in output
+   INTEGER ::  debug_output_unit       !< Fortran Unit Number of the debug-output file
+   INTEGER ::  global_id_in_file = -1  !< value of global ID within a file
+   INTEGER ::  master_rank             !< master rank for tasks to be executed by single PE only
+   INTEGER ::  output_group_comm       !< MPI communicator addressing all MPI ranks which participate in output
 
    LOGICAL ::  print_debug_output = .FALSE.  !< if true, debug output is printed
@@ -93,7 +93,7 @@
    END INTERFACE netcdf4_write_attribute
 
-   INTERFACE netcdf4_init_end
-      MODULE PROCEDURE netcdf4_init_end
-   END INTERFACE netcdf4_init_end
+   INTERFACE netcdf4_stop_file_header_definition
+      MODULE PROCEDURE netcdf4_stop_file_header_definition
+   END INTERFACE netcdf4_stop_file_header_definition
 
    INTERFACE netcdf4_write_variable
@@ -113,5 +113,5 @@
       netcdf4_get_error_message, &
       netcdf4_init_dimension, &
-      netcdf4_init_end, &
+      netcdf4_stop_file_header_definition, &
       netcdf4_init_module, &
       netcdf4_init_variable, &
@@ -136,8 +136,8 @@
                                                                  !> must be unique for each output group
 
-   INTEGER(iwp), INTENT(IN) ::  dom_global_id              !< global id within a file defined by DOM
-   INTEGER,      INTENT(IN) ::  master_output_rank         !< MPI rank executing tasks which must be executed by a single PE
-   INTEGER,      INTENT(IN) ::  mpi_comm_of_output_group   !< MPI communicator specifying the rank group participating in output
-   INTEGER(iwp), INTENT(IN) ::  program_debug_output_unit  !< file unit number for debug output
+   INTEGER, INTENT(IN) ::  dom_global_id              !< global id within a file defined by DOM
+   INTEGER, INTENT(IN) ::  master_output_rank         !< MPI rank executing tasks which must be executed by a single PE
+   INTEGER, INTENT(IN) ::  mpi_comm_of_output_group   !< MPI communicator specifying the rank group participating in output
+   INTEGER, INTENT(IN) ::  program_debug_output_unit  !< file unit number for debug output
 
    LOGICAL, INTENT(IN) ::  debug_output  !< if true, debug output is printed
@@ -160,15 +160,15 @@
 !> Open netcdf file.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE netcdf4_open_file( mode, filename, file_id, return_value )
-
-   CHARACTER(LEN=*), INTENT(IN) ::  filename  !< name of file
-   CHARACTER(LEN=*), INTENT(IN) ::  mode      !< operation mode (either parallel or serial)
+SUBROUTINE netcdf4_open_file( mode, file_name, file_id, return_value )
+
+   CHARACTER(LEN=*), INTENT(IN) ::  file_name  !< name of file
+   CHARACTER(LEN=*), INTENT(IN) ::  mode       !< operation mode (either parallel or serial)
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'netcdf4_open_file'  !< name of this routine
 
-   INTEGER(iwp), INTENT(OUT) ::  file_id       !< file ID
-   INTEGER                   ::  my_rank       !< MPI rank of processor
-   INTEGER(iwp)              ::  nc_stat       !< netcdf return value
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
+   INTEGER, INTENT(OUT) ::  file_id       !< file ID
+   INTEGER              ::  my_rank       !< MPI rank of processor
+   INTEGER              ::  nc_stat       !< netcdf return value
+   INTEGER, INTENT(OUT) ::  return_value  !< return value
 
 
@@ -177,5 +177,5 @@
 
    !-- Open new file
-   CALL internal_message( 'debug', routine_name // ': create file "' // TRIM( filename ) // '"' )
+   CALL internal_message( 'debug', routine_name // ': create file "' // TRIM( file_name ) // '"' )
 
    IF ( TRIM( mode ) == mode_serial )  THEN
@@ -200,6 +200,6 @@
 
       IF ( return_value == 0 )  &
-         nc_stat = NF90_CREATE( TRIM( filename ) // TRIM( file_suffix ), &
-                                IOR( NF90_NOCLOBBER, NF90_NETCDF4 ), &
+         nc_stat = NF90_CREATE( TRIM( file_name ) // TRIM( file_suffix ), &
+                                IOR( NF90_NOCLOBBER, NF90_NETCDF4 ),      &
                                 file_id )
 #else
@@ -214,5 +214,5 @@
 
 #if defined( __parallel ) && defined( __netcdf4 ) && defined( __netcdf4_parallel )
-      nc_stat = NF90_CREATE( TRIM( filename ) // TRIM( file_suffix ),                &
+      nc_stat = NF90_CREATE( TRIM( file_name ) // TRIM( file_suffix ),               &
                              IOR( NF90_NOCLOBBER, IOR( NF90_NETCDF4, NF90_MPIIO ) ), &
                              file_id, COMM = output_group_comm, INFO = MPI_INFO_NULL )
@@ -229,5 +229,5 @@
       nc_stat = 0
       return_value = 1
-      CALL internal_message( 'error', routine_name // ': selected mode "' // &
+      CALL internal_message( 'error', routine_name // ': selected mode "' //  &
                                       TRIM( mode ) // '" must be either "' // &
                                       mode_serial // '" or "' // mode_parallel // '"' )
@@ -237,6 +237,7 @@
    IF ( nc_stat /= NF90_NOERR  .AND.  return_value == 0 )  THEN
       return_value = 1
-      CALL internal_message( 'error', routine_name // ': NetCDF error while opening file "' // &
-                                      TRIM( filename ) // '": ' // NF90_STRERROR( nc_stat ) )
+      CALL internal_message( 'error', routine_name //                 &
+                             ': NetCDF error while opening file "' // &
+                             TRIM( file_name ) // '": ' // NF90_STRERROR( nc_stat ) )
    ENDIF
 #endif
@@ -249,26 +250,26 @@
 !> Write attribute to netcdf file.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE netcdf4_write_attribute( file_id, var_id, att_name, att_value_char, &
-                 att_value_int8, att_value_int16, att_value_int32,             &
-                 att_value_real32, att_value_real64, return_value )
-
-   CHARACTER(LEN=*), INTENT(IN)           ::  att_name        !< name of attribute
-   CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  att_value_char  !< value of attribute
+SUBROUTINE netcdf4_write_attribute( file_id, variable_id, attribute_name, &
+                 value_char, value_int8, value_int16, value_int32,        &
+                 value_real32, value_real64, return_value )
+
+   CHARACTER(LEN=*), INTENT(IN)           ::  attribute_name  !< name of attribute
+   CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  value_char      !< value of attribute
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'netcdf4_write_attribute'  !< name of this routine
 
-   INTEGER(iwp) ::  nc_stat    !< netcdf return value
-   INTEGER(iwp) ::  target_id  !< ID of target which gets attribute (either global or var_id)
-
-   INTEGER(iwp), INTENT(IN)  ::  file_id       !< file ID
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
-   INTEGER(iwp), INTENT(IN)  ::  var_id        !< variable ID
-
-   INTEGER(KIND=1), INTENT(IN), OPTIONAL ::  att_value_int8   !< value of attribute
-   INTEGER(KIND=2), INTENT(IN), OPTIONAL ::  att_value_int16  !< value of attribute
-   INTEGER(KIND=4), INTENT(IN), OPTIONAL ::  att_value_int32  !< value of attribute
-
-   REAL(KIND=4), INTENT(IN), OPTIONAL ::  att_value_real32  !< value of attribute
-   REAL(KIND=8), INTENT(IN), OPTIONAL ::  att_value_real64  !< value of attribute
+   INTEGER ::  nc_stat    !< netcdf return value
+   INTEGER ::  target_id  !< ID of target which gets attribute (either global or variable_id)
+
+   INTEGER, INTENT(IN)  ::  file_id       !< file ID
+   INTEGER, INTENT(OUT) ::  return_value  !< return value
+   INTEGER, INTENT(IN)  ::  variable_id   !< variable ID
+
+   INTEGER(KIND=1), INTENT(IN), OPTIONAL ::  value_int8   !< value of attribute
+   INTEGER(KIND=2), INTENT(IN), OPTIONAL ::  value_int16  !< value of attribute
+   INTEGER(KIND=4), INTENT(IN), OPTIONAL ::  value_int32  !< value of attribute
+
+   REAL(KIND=4), INTENT(IN), OPTIONAL ::  value_real32  !< value of attribute
+   REAL(KIND=8), INTENT(IN), OPTIONAL ::  value_real64  !< value of attribute
 
 
@@ -276,29 +277,29 @@
    return_value = 0
 
-   IF ( var_id == global_id_in_file )  THEN
+   IF ( variable_id == global_id_in_file )  THEN
       target_id = NF90_GLOBAL
    ELSE
-      target_id = var_id
-   ENDIF
-
-   CALL internal_message( 'debug', &
-                          routine_name // ': write attribute "' // TRIM( att_name ) // '"' )
-
-   IF ( PRESENT( att_value_char ) )  THEN
-      nc_stat = NF90_PUT_ATT( file_id, target_id, TRIM( att_name ), TRIM( att_value_char ) )
-   ELSEIF ( PRESENT( att_value_int8 ) )  THEN
-      nc_stat = NF90_PUT_ATT( file_id, target_id, TRIM( att_name ), att_value_int8 )
-   ELSEIF ( PRESENT( att_value_int16 ) )  THEN
-      nc_stat = NF90_PUT_ATT( file_id, target_id, TRIM( att_name ), att_value_int16 )
-   ELSEIF ( PRESENT( att_value_int32 ) )  THEN
-      nc_stat = NF90_PUT_ATT( file_id, target_id, TRIM( att_name ), att_value_int32 )
-   ELSEIF ( PRESENT( att_value_real32 ) )  THEN
-      nc_stat = NF90_PUT_ATT( file_id, target_id, TRIM( att_name ), att_value_real32 )
-   ELSEIF ( PRESENT( att_value_real64 ) )  THEN
-      nc_stat = NF90_PUT_ATT( file_id, target_id, TRIM( att_name ), att_value_real64 )
+      target_id = variable_id
+   ENDIF
+
+   CALL internal_message( 'debug', routine_name // &
+                          ': write attribute "' // TRIM( attribute_name ) // '"' )
+
+   IF ( PRESENT( value_char ) )  THEN
+      nc_stat = NF90_PUT_ATT( file_id, target_id, TRIM( attribute_name ), TRIM( value_char ) )
+   ELSEIF ( PRESENT( value_int8 ) )  THEN
+      nc_stat = NF90_PUT_ATT( file_id, target_id, TRIM( attribute_name ), value_int8 )
+   ELSEIF ( PRESENT( value_int16 ) )  THEN
+      nc_stat = NF90_PUT_ATT( file_id, target_id, TRIM( attribute_name ), value_int16 )
+   ELSEIF ( PRESENT( value_int32 ) )  THEN
+      nc_stat = NF90_PUT_ATT( file_id, target_id, TRIM( attribute_name ), value_int32 )
+   ELSEIF ( PRESENT( value_real32 ) )  THEN
+      nc_stat = NF90_PUT_ATT( file_id, target_id, TRIM( attribute_name ), value_real32 )
+   ELSEIF ( PRESENT( value_real64 ) )  THEN
+      nc_stat = NF90_PUT_ATT( file_id, target_id, TRIM( attribute_name ), value_real64 )
    ELSE
       return_value = 1
-      CALL internal_message( 'error', TRIM( routine_name ) // &
-                                      ': attribute "' // TRIM( att_name ) // '": no value given' )
+      CALL internal_message( 'error', routine_name // &
+                             ': no value given for attribute "' // TRIM( attribute_name ) // '"' )
    ENDIF
 
@@ -306,7 +307,7 @@
       IF ( nc_stat /= NF90_NOERR )  THEN
          return_value = 1
-         CALL internal_message( 'error',                                       &
-                 routine_name // ': NetCDF error while writing attribute "' // &
-                 TRIM( att_name ) // '": ' // NF90_STRERROR( nc_stat ) )
+         CALL internal_message( 'error', routine_name //                      &
+                                ': NetCDF error while writing attribute "' // &
+                                TRIM( attribute_name ) // '": ' // NF90_STRERROR( nc_stat ) )
       ENDIF
    ENDIF
@@ -322,55 +323,55 @@
 !> Initialize dimension.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE netcdf4_init_dimension( mode, file_id, 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) ::  mode      !< operation mode (either parallel or serial)
+SUBROUTINE netcdf4_init_dimension( mode, file_id, dimension_id, variable_id, &
+              dimension_name, dimension_type, dimension_length, return_value )
+
+   CHARACTER(LEN=*), INTENT(IN) ::  dimension_name  !< name of dimension
+   CHARACTER(LEN=*), INTENT(IN) ::  dimension_type  !< data type of dimension
+   CHARACTER(LEN=*), INTENT(IN) ::  mode            !< operation mode (either parallel or serial)
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'netcdf4_init_dimension'  !< name of this routine
 
-   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)              ::  nc_dim_length  !< length of dimension
-   INTEGER(iwp)              ::  nc_stat        !< netcdf return value
-   INTEGER(iwp), INTENT(OUT) ::  return_value   !< return value
-   INTEGER(iwp), INTENT(OUT) ::  var_id         !< variable ID
+   INTEGER, INTENT(OUT) ::  dimension_id         !< dimension ID
+   INTEGER, INTENT(IN)  ::  dimension_length     !< length of dimension
+   INTEGER, INTENT(IN)  ::  file_id              !< file ID
+   INTEGER              ::  nc_dimension_length  !< length of dimension
+   INTEGER              ::  nc_stat              !< netcdf return value
+   INTEGER, INTENT(OUT) ::  return_value         !< return value
+   INTEGER, INTENT(OUT) ::  variable_id          !< variable ID
 
 
 #if defined( __netcdf4 )
    return_value = 0
-   var_id = -1
-
-   CALL internal_message( 'debug', &
-                          routine_name // ': init dimension "' // TRIM( dim_name ) // '"' )
+   variable_id = -1
+
+   CALL internal_message( 'debug', routine_name // &
+                          ': init dimension "' // TRIM( dimension_name ) // '"' )
 
    !-- Check if dimension is unlimited
-   IF ( dim_length < 0 )  THEN
-      nc_dim_length = NF90_UNLIMITED
+   IF ( dimension_length < 0 )  THEN
+      nc_dimension_length = NF90_UNLIMITED
    ELSE
-      nc_dim_length = dim_length
+      nc_dimension_length = dimension_length
    ENDIF
 
    !-- Define dimension in file
-   nc_stat = NF90_DEF_DIM( file_id, dim_name, nc_dim_length, dim_id )
+   nc_stat = NF90_DEF_DIM( file_id, dimension_name, nc_dimension_length, dimension_id )
 
    IF ( nc_stat == NF90_NOERR )  THEN
 
       !-- Define variable holding dimension values in file
-      CALL netcdf4_init_variable( mode, file_id, var_id, dim_name, dim_type, (/dim_id/), &
-                                           is_global=.TRUE., return_value=return_value )
+      CALL netcdf4_init_variable( mode, file_id, variable_id, dimension_name, dimension_type, &
+                                  (/ dimension_id /), is_global=.TRUE., return_value=return_value )
 
    ELSE
       return_value = 1
-      CALL internal_message( 'error', routine_name //                                    &
-                                      ': NetCDF error while initializing dimension "' // &
-                                      TRIM( dim_name ) // '": ' // NF90_STRERROR( nc_stat ) )
+      CALL internal_message( 'error', routine_name //                           &
+                             ': NetCDF error while initializing dimension "' // &
+                             TRIM( dimension_name ) // '": ' // NF90_STRERROR( nc_stat ) )
    ENDIF
 #else
    return_value = 1
-   var_id = -1
-   dim_id = -1
+   variable_id = -1
+   dimension_id = -1
 #endif
 
@@ -382,20 +383,20 @@
 !> Initialize variable.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE netcdf4_init_variable( mode, file_id, var_id, var_name, var_type, var_dim_ids, &
-                                  is_global, return_value )
-
-   CHARACTER(LEN=*), INTENT(IN) ::  mode      !< operation mode (either parallel or serial)
-   CHARACTER(LEN=*), INTENT(IN) ::  var_name  !< name of variable
-   CHARACTER(LEN=*), INTENT(IN) ::  var_type  !< data type of variable
+SUBROUTINE netcdf4_init_variable( mode, file_id, variable_id, variable_name, variable_type, &
+                                  dimension_ids, is_global, return_value )
+
+   CHARACTER(LEN=*), INTENT(IN) ::  mode           !< operation mode (either parallel or serial)
+   CHARACTER(LEN=*), INTENT(IN) ::  variable_name  !< name of variable
+   CHARACTER(LEN=*), INTENT(IN) ::  variable_type  !< data type of variable
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'netcdf4_init_variable'  !< name of this routine
 
-   INTEGER(iwp), INTENT(IN)  ::  file_id       !< file ID
-   INTEGER(iwp)              ::  nc_stat       !< netcdf return value
-   INTEGER(iwp)              ::  nc_var_type   !< netcdf data type
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
-   INTEGER(iwp), INTENT(OUT) ::  var_id        !< variable ID
-
-   INTEGER(iwp), DIMENSION(:), INTENT(IN) ::  var_dim_ids  !< list of dimension IDs used by variable
+   INTEGER, INTENT(IN)  ::  file_id           !< file ID
+   INTEGER              ::  nc_stat           !< netcdf return value
+   INTEGER              ::  nc_variable_type  !< netcdf data type
+   INTEGER, INTENT(OUT) ::  return_value      !< return value
+   INTEGER, INTENT(OUT) ::  variable_id       !< variable ID
+
+   INTEGER, DIMENSION(:), INTENT(IN) ::  dimension_ids  !< list of dimension IDs used by variable
 
    LOGICAL, INTENT(IN)  ::  is_global  !< true if variable is global (same on all PE)
@@ -406,13 +407,14 @@
 
    WRITE( temp_string, * ) is_global
-   CALL internal_message( 'debug', routine_name // ': init variable "' // TRIM( var_name ) // &
-                                   '" ( is_global = ' // TRIM( temp_string ) // ')' )
-
-   nc_var_type = get_netcdf_data_type( var_type )
-
-   IF ( nc_var_type /= -1_iwp )  THEN
+   CALL internal_message( 'debug', routine_name //                        &
+                          ': init variable "' // TRIM( variable_name ) // &
+                          '" ( is_global = ' // TRIM( temp_string ) // ')' )
+
+   nc_variable_type = get_netcdf_data_type( variable_type )
+
+   IF ( nc_variable_type /= -1 )  THEN
 
       !-- Define variable in file
-      nc_stat = NF90_DEF_VAR( file_id, var_name, nc_var_type, var_dim_ids, var_id )
+      nc_stat = NF90_DEF_VAR( file_id, variable_name, nc_variable_type, dimension_ids, variable_id )
 
 #if defined( __netcdf4_parallel )
@@ -420,16 +422,16 @@
       IF ( nc_stat == NF90_NOERR  .AND.  TRIM( mode ) == mode_parallel )  THEN
          IF ( is_global )  THEN
-            nc_stat = NF90_VAR_PAR_ACCESS( file_id, var_id, NF90_INDEPENDENT )
+            nc_stat = NF90_VAR_PAR_ACCESS( file_id, variable_id, NF90_INDEPENDENT )
          ELSE
-            nc_stat = NF90_VAR_PAR_ACCESS( file_id, var_id, NF90_COLLECTIVE )
+            nc_stat = NF90_VAR_PAR_ACCESS( file_id, variable_id, NF90_COLLECTIVE )
          ENDIF
       ENDIF
 #endif
 
-      IF ( nc_stat /= NF90_NOERR)  THEN
+      IF ( nc_stat /= NF90_NOERR )  THEN
          return_value = 1
-         CALL internal_message( 'error', routine_name //                                   &
-                                         ': NetCDF error while initializing variable "' // &
-                                         TRIM( var_name ) // '": ' // NF90_STRERROR( nc_stat ) )
+         CALL internal_message( 'error', routine_name //                          &
+                                ': NetCDF error while initializing variable "' // &
+                                TRIM( variable_name ) // '": ' // NF90_STRERROR( nc_stat ) )
       ENDIF
 
@@ -440,5 +442,5 @@
 #else
    return_value = 1
-   var_id = -1
+   variable_id = -1
 #endif
 
@@ -450,12 +452,12 @@
 !> Leave file definition state.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE netcdf4_init_end( file_id, return_value )
-
-   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'netcdf4_init_end'  !< name of this routine
-
-   INTEGER(iwp), INTENT(IN)  ::  file_id       !< file ID
-   INTEGER(iwp)              ::  nc_stat       !< netcdf return value
-   INTEGER(iwp)              ::  old_mode      !< previous netcdf fill mode
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
+SUBROUTINE netcdf4_stop_file_header_definition( file_id, return_value )
+
+   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'netcdf4_stop_file_header_definition'  !< name of this routine
+
+   INTEGER, INTENT(IN)  ::  file_id        !< file ID
+   INTEGER              ::  nc_stat        !< netcdf return value
+   INTEGER              ::  old_fill_mode  !< previous netcdf fill mode
+   INTEGER, INTENT(OUT) ::  return_value   !< return value
 
 
@@ -464,10 +466,9 @@
 
    WRITE( temp_string, * ) file_id
-   CALL internal_message( 'debug',        &
-                          routine_name // &
+   CALL internal_message( 'debug', routine_name // &
                           ': finalize file definition (file_id=' // TRIM( temp_string ) // ')' )
 
    !-- Set general no fill, otherwise the performance drops significantly
-   nc_stat = NF90_SET_FILL( file_id, NF90_NOFILL, old_mode )
+   nc_stat = NF90_SET_FILL( file_id, NF90_NOFILL, old_fill_mode )
 
    IF ( nc_stat == NF90_NOERR )  THEN
@@ -477,6 +478,6 @@
    IF ( nc_stat /= NF90_NOERR )  THEN
       return_value = 1
-      CALL internal_message( 'error', routine_name // ': NetCDF error: ' // &
-                                      NF90_STRERROR( nc_stat ) )
+      CALL internal_message( 'error', routine_name // &
+                             ': NetCDF error: ' // NF90_STRERROR( nc_stat ) )
    ENDIF
 #else
@@ -484,5 +485,5 @@
 #endif
 
-END SUBROUTINE netcdf4_init_end
+END SUBROUTINE netcdf4_stop_file_header_definition
 
 !--------------------------------------------------------------------------------------------------!
@@ -491,68 +492,63 @@
 !> Write variable of different kind into netcdf file.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE netcdf4_write_variable(                                        &
-              file_id, var_id, bounds_start, value_counts, bounds_origin, &
-              is_global,                                                  &
-              var_int8_0d,   var_int8_1d,   var_int8_2d,   var_int8_3d,   &
-              var_int16_0d,  var_int16_1d,  var_int16_2d,  var_int16_3d,  &
-              var_int32_0d,  var_int32_1d,  var_int32_2d,  var_int32_3d,  &
-              var_intwp_0d,  var_intwp_1d,  var_intwp_2d,  var_intwp_3d,  &
-              var_real32_0d, var_real32_1d, var_real32_2d, var_real32_3d, &
-              var_real64_0d, var_real64_1d, var_real64_2d, var_real64_3d, &
-              var_realwp_0d, var_realwp_1d, var_realwp_2d, var_realwp_3d, &
+SUBROUTINE netcdf4_write_variable(                                                    &
+              file_id, variable_id, bounds_start, value_counts, bounds_origin,        &
+              is_global,                                                              &
+              values_int8_0d,   values_int8_1d,   values_int8_2d,   values_int8_3d,   &
+              values_int16_0d,  values_int16_1d,  values_int16_2d,  values_int16_3d,  &
+              values_int32_0d,  values_int32_1d,  values_int32_2d,  values_int32_3d,  &
+              values_intwp_0d,  values_intwp_1d,  values_intwp_2d,  values_intwp_3d,  &
+              values_real32_0d, values_real32_1d, values_real32_2d, values_real32_3d, &
+              values_real64_0d, values_real64_1d, values_real64_2d, values_real64_3d, &
+              values_realwp_0d, values_realwp_1d, values_realwp_2d, values_realwp_3d, &
               return_value )
 
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'netcdf4_write_variable'  !< name of this routine
 
-   INTEGER(iwp)              ::  d             !< loop index
-   INTEGER(iwp), INTENT(IN)  ::  file_id       !< file ID
-   INTEGER                   ::  my_rank       !< MPI rank of processor
-   INTEGER(iwp)              ::  nc_stat       !< netcdf return value
-   INTEGER(iwp)              ::  ndim          !< number of dimensions of variable in file
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
-   INTEGER(iwp), INTENT(IN)  ::  var_id        !< variable ID
-
-   INTEGER(iwp), DIMENSION(:), INTENT(IN)  ::  bounds_origin  !< starting index of each dimension
-   INTEGER(iwp), DIMENSION(:), INTENT(IN)  ::  bounds_start   !< starting index of variable
-   INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  dim_ids        !< IDs of dimensions of variable in file
-   INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  dim_lengths    !< length of dimensions of variable in file
-   INTEGER(iwp), DIMENSION(:), INTENT(IN)  ::  value_counts   !< count of values along each dimension to be written
-
-   INTEGER(KIND=1), POINTER, INTENT(IN), OPTIONAL                               ::  var_int8_0d  !< output variable
-   INTEGER(KIND=1), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_int8_1d  !< output variable
-   INTEGER(KIND=1), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_int8_2d  !< output variable
-   INTEGER(KIND=1), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_int8_3d  !< output variable
-
-   INTEGER(KIND=2), POINTER, INTENT(IN), OPTIONAL                               ::  var_int16_0d  !< output variable
-   INTEGER(KIND=2), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_int16_1d  !< output variable
-   INTEGER(KIND=2), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_int16_2d  !< output variable
-   INTEGER(KIND=2), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_int16_3d  !< output variable
-
-   INTEGER(KIND=4), POINTER, INTENT(IN), OPTIONAL                               ::  var_int32_0d  !< output variable
-   INTEGER(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_int32_1d  !< output variable
-   INTEGER(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_int32_2d  !< output variable
-   INTEGER(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_int32_3d  !< output variable
-
-   INTEGER(iwp), POINTER, INTENT(IN), OPTIONAL                               ::  var_intwp_0d  !< output variable
-   INTEGER(iwp), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_intwp_1d  !< output variable
-   INTEGER(iwp), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_intwp_2d  !< output variable
-   INTEGER(iwp), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_intwp_3d  !< output variable
+   INTEGER              ::  d             !< loop index
+   INTEGER, INTENT(IN)  ::  file_id       !< file ID
+   INTEGER              ::  my_rank       !< MPI rank of processor
+   INTEGER              ::  nc_stat       !< netcdf return value
+   INTEGER              ::  ndims         !< number of dimensions of variable in file
+   INTEGER, INTENT(OUT) ::  return_value  !< return value
+   INTEGER, INTENT(IN)  ::  variable_id   !< variable ID
+
+   INTEGER, DIMENSION(:),              INTENT(IN)  ::  bounds_origin      !< starting index of each dimension
+   INTEGER, DIMENSION(:),              INTENT(IN)  ::  bounds_start       !< starting index of variable
+   INTEGER, DIMENSION(:), ALLOCATABLE              ::  dimension_ids      !< IDs of dimensions of variable in file
+   INTEGER, DIMENSION(:), ALLOCATABLE              ::  dimension_lengths  !< length of dimensions of variable in file
+   INTEGER, DIMENSION(:),              INTENT(IN)  ::  value_counts       !< count of values along each dimension to be written
+
+   INTEGER(KIND=1), POINTER,             INTENT(IN), OPTIONAL                   ::  values_int8_0d   !< output variable
+   INTEGER(KIND=2), POINTER,             INTENT(IN), OPTIONAL                   ::  values_int16_0d  !< output variable
+   INTEGER(KIND=4), POINTER,             INTENT(IN), OPTIONAL                   ::  values_int32_0d  !< output variable
+   INTEGER(iwp),    POINTER,             INTENT(IN), OPTIONAL                   ::  values_intwp_0d  !< output variable
+   INTEGER(KIND=1), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_int8_1d   !< output variable
+   INTEGER(KIND=2), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_int16_1d  !< output variable
+   INTEGER(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_int32_1d  !< output variable
+   INTEGER(iwp),    POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_intwp_1d  !< output variable
+   INTEGER(KIND=1), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_int8_2d   !< output variable
+   INTEGER(KIND=2), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_int16_2d  !< output variable
+   INTEGER(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_int32_2d  !< output variable
+   INTEGER(iwp),    POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_intwp_2d  !< output variable
+   INTEGER(KIND=1), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_int8_3d   !< output variable
+   INTEGER(KIND=2), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_int16_3d  !< output variable
+   INTEGER(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_int32_3d  !< output variable
+   INTEGER(iwp),    POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_intwp_3d  !< output variable
 
    LOGICAL, INTENT(IN) ::  is_global  !< true if variable is global (same on all PE)
 
-   REAL(KIND=4), POINTER, INTENT(IN), OPTIONAL                               ::  var_real32_0d  !< output variable
-   REAL(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_real32_1d  !< output variable
-   REAL(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_real32_2d  !< output variable
-   REAL(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_real32_3d  !< output variable
-
-   REAL(KIND=8), POINTER, INTENT(IN), OPTIONAL                               ::  var_real64_0d  !< output variable
-   REAL(KIND=8), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_real64_1d  !< output variable
-   REAL(KIND=8), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_real64_2d  !< output variable
-   REAL(KIND=8), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_real64_3d  !< output variable
-
-   REAL(wp), POINTER, INTENT(IN), OPTIONAL                               ::  var_realwp_0d  !< output variable
-   REAL(wp), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  var_realwp_1d  !< output variable
-   REAL(wp), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  var_realwp_2d  !< output variable
-   REAL(wp), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  var_realwp_3d  !< output variable
+   REAL(KIND=4), POINTER,             INTENT(IN), OPTIONAL                   ::  values_real32_0d  !< output variable
+   REAL(KIND=8), POINTER,             INTENT(IN), OPTIONAL                   ::  values_real64_0d  !< output variable
+   REAL(wp),     POINTER,             INTENT(IN), OPTIONAL                   ::  values_realwp_0d  !< output variable
+   REAL(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_real32_1d  !< output variable
+   REAL(KIND=8), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_real64_1d  !< output variable
+   REAL(wp),     POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:)     ::  values_realwp_1d  !< output variable
+   REAL(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_real32_2d  !< output variable
+   REAL(KIND=8), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_real64_2d  !< output variable
+   REAL(wp),     POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:)   ::  values_realwp_2d  !< output variable
+   REAL(KIND=4), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_real32_3d  !< output variable
+   REAL(KIND=8), POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_real64_3d  !< output variable
+   REAL(wp),     POINTER, CONTIGUOUS, INTENT(IN), OPTIONAL, DIMENSION(:,:,:) ::  values_realwp_3d  !< output variable
 
 
@@ -571,126 +567,126 @@
    IF ( return_value == 0  .AND.  ( .NOT. is_global  .OR.  my_rank == master_rank ) )  THEN
 
-      WRITE( temp_string, * ) var_id
+      WRITE( temp_string, * ) variable_id
       CALL internal_message( 'debug', routine_name // ': write variable ' // TRIM( temp_string ) )
 
-      ndim = SIZE( bounds_start )
+      ndims = SIZE( bounds_start )
 
       !-- 8bit integer output
-      IF ( PRESENT( var_int8_0d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, (/ var_int8_0d /),       &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_int8_1d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_int8_1d,             &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_int8_2d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_int8_2d,             &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_int8_3d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_int8_3d,             &
+      IF ( PRESENT( values_int8_0d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, (/ values_int8_0d /), &
+                                 start = bounds_start - bounds_origin + 1,   &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_int8_1d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_int8_1d,     &
+                                 start = bounds_start - bounds_origin + 1, &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_int8_2d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_int8_2d,     &
+                                 start = bounds_start - bounds_origin + 1, &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_int8_3d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_int8_3d,     &
                                  start = bounds_start - bounds_origin + 1, &
                                  count = value_counts )
       !-- 16bit integer output
-      ELSEIF ( PRESENT( var_int16_0d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, (/ var_int16_0d /),      &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_int16_1d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_int16_1d,            &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_int16_2d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_int16_2d,            &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_int16_3d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_int16_3d,            &
+      ELSEIF ( PRESENT( values_int16_0d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, (/ values_int16_0d /), &
+                                 start = bounds_start - bounds_origin + 1,    &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_int16_1d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_int16_1d,    &
+                                 start = bounds_start - bounds_origin + 1, &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_int16_2d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_int16_2d,    &
+                                 start = bounds_start - bounds_origin + 1, &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_int16_3d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_int16_3d,    &
                                  start = bounds_start - bounds_origin + 1, &
                                  count = value_counts )
       !-- 32bit integer output
-      ELSEIF ( PRESENT( var_int32_0d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, (/ var_int32_0d /),      &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_int32_1d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_int32_1d,            &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_int32_2d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_int32_2d,            &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_int32_3d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_int32_3d,            &
+      ELSEIF ( PRESENT( values_int32_0d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, (/ values_int32_0d /),  &
+                                 start = bounds_start - bounds_origin + 1,     &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_int32_1d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_int32_1d,    &
+                                 start = bounds_start - bounds_origin + 1, &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_int32_2d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_int32_2d,    &
+                                 start = bounds_start - bounds_origin + 1, &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_int32_3d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_int32_3d,    &
                                  start = bounds_start - bounds_origin + 1, &
                                  count = value_counts )
       !-- working-precision integer output
-      ELSEIF ( PRESENT( var_intwp_0d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, (/ var_intwp_0d /),      &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_intwp_1d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_intwp_1d,            &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_intwp_2d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_intwp_2d,            &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_intwp_3d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_intwp_3d,            &
+      ELSEIF ( PRESENT( values_intwp_0d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, (/ values_intwp_0d /),  &
+                                 start = bounds_start - bounds_origin + 1,     &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_intwp_1d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_intwp_1d,    &
+                                 start = bounds_start - bounds_origin + 1, &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_intwp_2d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_intwp_2d,    &
+                                 start = bounds_start - bounds_origin + 1, &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_intwp_3d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_intwp_3d,    &
                                  start = bounds_start - bounds_origin + 1, &
                                  count = value_counts )
       !-- 32bit real output
-      ELSEIF ( PRESENT( var_real32_0d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, (/ var_real32_0d /),     &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_real32_1d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_real32_1d,           &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_real32_2d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_real32_2d,           &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_real32_3d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_real32_3d,           &
+      ELSEIF ( PRESENT( values_real32_0d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, (/ values_real32_0d /), &
+                                 start = bounds_start - bounds_origin + 1,     &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_real32_1d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_real32_1d,   &
+                                 start = bounds_start - bounds_origin + 1, &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_real32_2d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_real32_2d,   &
+                                 start = bounds_start - bounds_origin + 1, &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_real32_3d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_real32_3d,   &
                                  start = bounds_start - bounds_origin + 1, &
                                  count = value_counts )
       !-- 64bit real output
-      ELSEIF ( PRESENT( var_real64_0d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, (/ var_real64_0d /),     &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_real64_1d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_real64_1d,           &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_real64_2d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_real64_2d,           &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_real64_3d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_real64_3d,           &
+      ELSEIF ( PRESENT( values_real64_0d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, (/ values_real64_0d /), &
+                                 start = bounds_start - bounds_origin + 1,     &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_real64_1d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_real64_1d,   &
+                                 start = bounds_start - bounds_origin + 1, &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_real64_2d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_real64_2d,   &
+                                 start = bounds_start - bounds_origin + 1, &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_real64_3d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_real64_3d,   &
                                  start = bounds_start - bounds_origin + 1, &
                                  count = value_counts )
       !-- working-precision real output
-      ELSEIF ( PRESENT( var_realwp_0d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, (/ var_realwp_0d /),     &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_realwp_1d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_realwp_1d,           &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_realwp_2d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_realwp_2d,           &
-                                 start = bounds_start - bounds_origin + 1, &
-                                 count = value_counts )
-      ELSEIF ( PRESENT( var_realwp_3d ) )  THEN
-         nc_stat = NF90_PUT_VAR( file_id, var_id, var_realwp_3d,           &
+      ELSEIF ( PRESENT( values_realwp_0d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, (/ values_realwp_0d /), &
+                                 start = bounds_start - bounds_origin + 1,     &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_realwp_1d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_realwp_1d,   &
+                                 start = bounds_start - bounds_origin + 1, &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_realwp_2d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_realwp_2d,   &
+                                 start = bounds_start - bounds_origin + 1, &
+                                 count = value_counts )
+      ELSEIF ( PRESENT( values_realwp_3d ) )  THEN
+         nc_stat = NF90_PUT_VAR( file_id, variable_id, values_realwp_3d,   &
                                  start = bounds_start - bounds_origin + 1, &
                                  count = value_counts )
@@ -698,8 +694,7 @@
          return_value = 1
          nc_stat = NF90_NOERR
-         WRITE( temp_string, '(": variable_id=",I6,", file_id=",I6)' ) var_id, file_id
+         WRITE( temp_string, '(": variable_id=",I6,", file_id=",I6)' ) variable_id, file_id
          CALL internal_message( 'error', routine_name // &
-                                TRIM( temp_string ) //   &
-                                ': no output values given' )
+                                ': no output values given ' // TRIM( temp_string ) )
       ENDIF
 
@@ -713,12 +708,13 @@
             WRITE( temp_string, * )  NF90_STRERROR( nc_stat )
 
-            ALLOCATE( dim_ids(ndim) )
-            ALLOCATE( dim_lengths(ndim) )
-
-            nc_stat = NF90_INQUIRE_VARIABLE( file_id, var_id, dimids=dim_ids )
+            ALLOCATE( dimension_ids(ndims) )
+            ALLOCATE( dimension_lengths(ndims) )
+
+            nc_stat = NF90_INQUIRE_VARIABLE( file_id, variable_id, dimids=dimension_ids )
 
             d = 1
-            DO WHILE ( d <= ndim .AND. nc_stat == NF90_NOERR )
-               nc_stat = NF90_INQUIRE_DIMENSION( file_id, dim_ids(d), len=dim_lengths(d) )
+            DO WHILE ( d <= ndims .AND. nc_stat == NF90_NOERR )
+               nc_stat = NF90_INQUIRE_DIMENSION( file_id, dimension_ids(d), &
+                                                 LEN=dimension_lengths(d) )
                d = d + 1
             ENDDO
@@ -728,11 +724,10 @@
                   'start=', bounds_start, ', count=', value_counts, ', origin=', bounds_origin
                CALL internal_message( 'error', routine_name //     &
-                                      ': error while writing: ' // &
-                                      TRIM( temp_string ) )
+                                      ': error while writing: ' // TRIM( temp_string ) )
             ELSE
                !-- Error occured during NF90_INQUIRE_VARIABLE or NF90_INQUIRE_DIMENSION
                CALL internal_message( 'error', routine_name //            &
                                       ': error while accessing file: ' // &
-                                      NF90_STRERROR( nc_stat ) )
+                                       NF90_STRERROR( nc_stat ) )
             ENDIF
 
@@ -740,6 +735,5 @@
             !-- Other NetCDF error
             CALL internal_message( 'error', routine_name //     &
-                                   ': error while writing: ' // &
-                                   NF90_STRERROR( nc_stat ) )
+                                   ': error while writing: ' // NF90_STRERROR( nc_stat ) )
          ENDIF
       ENDIF
@@ -761,7 +755,7 @@
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'netcdf4_finalize'  !< name of routine
 
-   INTEGER(iwp), INTENT(IN)  ::  file_id       !< file ID
-   INTEGER(iwp)              ::  nc_stat       !< netcdf return value
-   INTEGER(iwp), INTENT(OUT) ::  return_value  !< return value
+   INTEGER, INTENT(IN)  ::  file_id       !< file ID
+   INTEGER              ::  nc_stat       !< netcdf return value
+   INTEGER, INTENT(OUT) ::  return_value  !< return value
 
 
@@ -769,5 +763,5 @@
    WRITE( temp_string, * ) file_id
    CALL internal_message( 'debug', routine_name // &
-                                   ': close file (file_id=' // TRIM( temp_string ) // ')' )
+                          ': close file (file_id=' // TRIM( temp_string ) // ')' )
 
    nc_stat = NF90_CLOSE( file_id )
@@ -777,5 +771,5 @@
       return_value = 1
       CALL internal_message( 'error', routine_name // &
-                                      ': NetCDF error: ' // NF90_STRERROR( nc_stat ) )
+                             ': NetCDF error: ' // NF90_STRERROR( nc_stat ) )
    ENDIF
 #else
@@ -796,5 +790,5 @@
    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'get_netcdf_data_type'  !< name of this routine
 
-   INTEGER(iwp) ::  return_value  !< netcdf data type
+   INTEGER ::  return_value  !< netcdf data type
 
 
@@ -823,6 +817,6 @@
       CASE DEFAULT
          CALL internal_message( 'error', routine_name // &
-                                         ': data type unknown (' // TRIM( data_type ) // ')' )
-         return_value = -1_iwp
+                                ': data type unknown (' // TRIM( data_type ) // ')' )
+         return_value = -1
 
    END SELECT
@@ -860,12 +854,14 @@
 !> Return the last created error message.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE netcdf4_get_error_message( error_message )
-
-   CHARACTER(LEN=800), INTENT(OUT) ::  error_message  !< return error message to main program
-
-
-   error_message = internal_error_message
-
-END SUBROUTINE netcdf4_get_error_message
+FUNCTION netcdf4_get_error_message() RESULT( error_message )
+
+   CHARACTER(LEN=800) ::  error_message  !< return error message to main program
+
+
+   error_message = TRIM( internal_error_message )
+
+   internal_error_message = ''
+
+END FUNCTION netcdf4_get_error_message
 
 
Index: /palm/trunk/UTIL/binary_to_netcdf.f90
===================================================================
--- /palm/trunk/UTIL/binary_to_netcdf.f90	(revision 4140)
+++ /palm/trunk/UTIL/binary_to_netcdf.f90	(revision 4141)
@@ -49,6 +49,6 @@
 
    !-- Set kinds to be used as defaults
-   INTEGER, PARAMETER ::   wp = 8  !< default real kind
-   INTEGER, PARAMETER ::  iwp = 4  !< default integer kind
+   INTEGER, PARAMETER ::  iwp = 4  !< default integer kind for output-variable values
+   INTEGER, PARAMETER ::  wp  = 8  !< default real kind for output-variable values
 
    INTEGER, PARAMETER ::  charlen_internal = 1000  !< length of strings within this program
@@ -59,5 +59,5 @@
       CHARACTER(LEN=charlen_internal) ::  name          !< name of attribute
       CHARACTER(LEN=charlen_internal) ::  value_char    !< character value
-      INTEGER(iwp)                    ::  var_id        !< id of variable to which the attribute belongs to
+      INTEGER                         ::  variable_id   !< id of variable to which the attribute belongs to
       INTEGER(KIND=1)                 ::  value_int8    !< 8bit integer value
       INTEGER(KIND=2)                 ::  value_int16   !< 16bit integer value
@@ -70,13 +70,13 @@
       CHARACTER(LEN=charlen_internal) ::  data_type  !< data type of dimension
       CHARACTER(LEN=charlen_internal) ::  name       !< dimension name
-      INTEGER(iwp)                    ::  id         !< dimension id within file
-      INTEGER(iwp)                    ::  length     !< length of dimension
+      INTEGER                         ::  id         !< dimension id within file
+      INTEGER                         ::  length     !< length of dimension
    END TYPE dimension_type
 
    TYPE variable_type
-      CHARACTER(LEN=charlen_internal) ::  data_type  !< data type of variable
-      CHARACTER(LEN=charlen_internal) ::  name       !< variable name
-      INTEGER(iwp)                    ::  id         !< variable id within file
-      INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  dimension_ids  !< list of dimension ids used by variable
+      CHARACTER(LEN=charlen_internal)    ::  data_type      !< data type of variable
+      CHARACTER(LEN=charlen_internal)    ::  name           !< variable name
+      INTEGER                            ::  id             !< variable id within file
+      INTEGER, DIMENSION(:), ALLOCATABLE ::  dimension_ids  !< list of dimension ids used by variable
    END TYPE variable_type
 
@@ -92,20 +92,20 @@
       config_file_list_name = 'BINARY_CONFIG_LIST'  !< file containing list of binary config files of each output group
 
-   INTEGER(iwp) ::  charlen            !< length of characters (strings) in binary file
-   INTEGER(iwp) ::  dom_global_id      !< global ID within a single file defined by DOM
-   INTEGER      ::  dom_master_rank    !< master MPI rank in DOM (rank which wrote additional information in DOM)
-   INTEGER      ::  dom_nrank          !< number of MPI ranks used by DOM
-   INTEGER(iwp) ::  file_index         !< loop index to loop over files
-   INTEGER      ::  group              !< loop index to loop over groups
-   INTEGER(iwp) ::  nc_file_id         !< ID of netcdf output file
-   INTEGER(iwp) ::  nfiles             !< number of output files defined in config file
-   INTEGER      ::  ngroup             !< number of output-file groups
-   INTEGER      ::  return_value       !< return value
-   INTEGER      ::  your_return_value  !< returned value of called routine
+   INTEGER ::  charlen            !< length of characters (strings) in binary file
+   INTEGER ::  dom_global_id      !< global ID within a single file defined by DOM
+   INTEGER ::  dom_master_rank    !< master MPI rank in DOM (rank which wrote additional information in DOM)
+   INTEGER ::  dom_nranks         !< number of MPI ranks used by DOM
+   INTEGER ::  file_index         !< loop index to loop over files
+   INTEGER ::  group              !< loop index to loop over groups
+   INTEGER ::  nc_file_id         !< ID of netcdf output file
+   INTEGER ::  nfiles             !< number of output files defined in config file
+   INTEGER ::  ngroups            !< number of output-file groups
+   INTEGER ::  return_value       !< return value
+   INTEGER ::  your_return_value  !< returned value of called routine
 
    INTEGER(KIND=1) ::  dummy_int8   !< dummy variable used for reading
    INTEGER(KIND=2) ::  dummy_int16  !< dummy variable used for reading
    INTEGER(KIND=4) ::  dummy_int32  !< dummy variable used for reading
-   INTEGER(iwp)    ::  dummy_intwp  !< dummy variable used for reading
+   INTEGER         ::  dummy_int    !< dummy variable used for reading
 
    INTEGER, PARAMETER ::  bin_file_unit = 12          !< Fortran unit of binary file
@@ -113,9 +113,9 @@
    INTEGER, PARAMETER ::  config_file_list_unit = 10  !< Fortran unit of file containing config-file list
 
-   INTEGER, DIMENSION(:), ALLOCATABLE ::  dim_id_netcdf  !< mapped dimension id within NetCDF file:
-                                                         !> dimension_list(i)%id and dim_id_netcdf(dimension_list(i)%id)
+   INTEGER, DIMENSION(:), ALLOCATABLE ::  dimension_id_netcdf  !< mapped dimension id within NetCDF file:
+                                                         !> dimension_list(i)%id and dimension_id_netcdf(dimension_list(i)%id)
                                                          !> reference the same dimension
-   INTEGER, DIMENSION(:), ALLOCATABLE ::  var_id_netcdf  !< mapped variable id within NetCDF file:
-                                                         !> variable_list(i)%id and var_id_netcdf(variable_list(i)%id)
+   INTEGER, DIMENSION(:), ALLOCATABLE ::  variable_id_netcdf  !< mapped variable id within NetCDF file:
+                                                         !> variable_list(i)%id and variable_id_netcdf(variable_list(i)%id)
                                                          !> reference the same variable
 
@@ -139,5 +139,5 @@
 
       !-- Go through each group of output files (all marked by same file suffix)
-      DO  group = 1, ngroup
+      DO  group = 1, ngroups
 
          CALL internal_message( 'info', 'Start converting ' // TRIM( group_names(group) ) // &
@@ -160,5 +160,6 @@
 
                IF ( your_return_value == 0 )  THEN
-                  CALL convert_data_to_netcdf( TRIM( filename_list(file_index) ), your_return_value )
+                  CALL convert_data_to_netcdf( TRIM( filename_list(file_index) ), &
+                                               your_return_value )
                ELSE
                   return_value = your_return_value
@@ -228,17 +229,17 @@
 
       !-- Count the configuration files
-      ngroup = 0
+      ngroups = 0
       DO WHILE ( io_stat == 0 )
          READ( config_file_list_unit, '(A)', IOSTAT=io_stat )  file_name
-         IF ( io_stat == 0 )  ngroup = ngroup + 1
+         IF ( io_stat == 0 )  ngroups = ngroups + 1
       ENDDO
       REWIND( config_file_list_unit )
 
-      IF ( ngroup /= 0 )  THEN
-
-         ALLOCATE( group_names(ngroup) )
+      IF ( ngroups /= 0 )  THEN
+
+         ALLOCATE( group_names(ngroups) )
 
          !-- Extract the group names
-         DO  i = 1, ngroup
+         DO  i = 1, ngroups
             READ( config_file_list_unit, '(A)', IOSTAT=io_stat )  file_name
             IF ( INDEX( TRIM( file_name ), config_file_name_base ) == 1 )  THEN
@@ -284,5 +285,5 @@
    CHARACTER(LEN=charlen_internal), DIMENSION(:), ALLOCATABLE ::  filename_list_tmp  !< temporary list of file names
 
-   INTEGER(iwp)         ::  filename_prefix_length  !< length of string containing the filname prefix
+   INTEGER              ::  filename_prefix_length  !< length of string containing the filname prefix
    INTEGER              ::  io_stat                 !< status of Fortran I/O operations
    INTEGER, INTENT(OUT) ::  return_value            !< return value of routine
@@ -298,15 +299,15 @@
    IF ( io_stat /= 0 )  THEN
       return_value = 1
-      CALL internal_message( 'error', &
-              routine_name // ': error while opening configuration file "' // &
-              TRIM( config_file_name ) // '"' )
+      CALL internal_message( 'error', routine_name // &
+                             ': error while opening configuration file "' // &
+                             TRIM( config_file_name ) // '"' )
    ENDIF
 
    IF ( return_value == 0 )  THEN
 
-      READ( config_file_unit ) dom_nrank
-
-      IF ( dom_nrank > 1000000 )  THEN
-         dom_nrank = 1000000
+      READ( config_file_unit ) dom_nranks
+
+      IF ( dom_nranks > 1000000 )  THEN
+         dom_nranks = 1000000
          CALL internal_message( 'info', routine_name // &
                  ': number of MPI ranks used in PALM is greater than the maximum ' // &
@@ -357,5 +358,5 @@
             return_value = 1
             CALL internal_message( 'error', routine_name // &
-                                            ': error while reading file names from config' )
+                                   ': error while reading file names from config' )
             EXIT
          ENDIF
@@ -377,5 +378,5 @@
 
    CHARACTER(LEN=2*charlen)             ::  bin_filename       !< name of binary file which to read
-   CHARACTER(LEN=*        ), INTENT(IN) ::  bin_filename_body  !< body of binary filename which to read
+   CHARACTER(LEN=*),         INTENT(IN) ::  bin_filename_body  !< body of binary filename which to read
    CHARACTER(LEN=charlen  )             ::  read_string        !< string read from file
 
@@ -387,5 +388,5 @@
    INTEGER              ::  n_dimensions       !< number of dimensions in file
    INTEGER              ::  n_variables        !< number of variables in file
-   INTEGER(iwp)         ::  var_ndim           !< number of dimensions of a variable
+   INTEGER              ::  variable_ndims     !< number of dimensions of a variable
    INTEGER, INTENT(OUT) ::  return_value       !< return value
 
@@ -408,7 +409,7 @@
    IF ( io_stat == 0 )  THEN
 
-      READ( bin_file_unit ) dummy_intwp
-      READ( bin_file_unit ) dummy_intwp
-      READ( bin_file_unit ) read_string
+      READ( bin_file_unit ) dummy_int    ! charlen
+      READ( bin_file_unit ) dummy_int    ! file_id
+      READ( bin_file_unit ) read_string  ! filename
 
    ELSE
@@ -475,7 +476,8 @@
             READ( bin_file_unit ) read_string
             variable_list(n_variables)%data_type = read_string
-            READ( bin_file_unit ) var_ndim
-            ALLOCATE( variable_list(n_variables)%dimension_ids(1:var_ndim) )
-            READ( bin_file_unit )  ( variable_list(n_variables)%dimension_ids(i), i = 1, var_ndim )
+            READ( bin_file_unit ) variable_ndims
+            ALLOCATE( variable_list(n_variables)%dimension_ids(1:variable_ndims) )
+            READ( bin_file_unit ) &
+               ( variable_list(n_variables)%dimension_ids(i), i = 1, variable_ndims )
 
          CASE ( 'attribute' )
@@ -496,5 +498,5 @@
 
             !-- Read attribute
-            READ( bin_file_unit ) attribute_list(n_attributes)%var_id
+            READ( bin_file_unit ) attribute_list(n_attributes)%variable_id
             READ( bin_file_unit ) read_string
             attribute_list(n_attributes)%name = read_string
@@ -559,12 +561,12 @@
    CHARACTER(LEN=*), PARAMETER  ::  routine_name = 'define_netcdf_files'  !< routine name
 
-   INTEGER              ::  i              !< loop index
-   INTEGER              ::  j              !< loop index
-   INTEGER              ::  nc_data_type   !< netcdf data type of output variable
-   INTEGER              ::  nc_dim_length  !< length of dimension in netcdf file
-   INTEGER              ::  nc_stat        !< return value of Netcdf calls
-   INTEGER, INTENT(OUT) ::  return_value   !< return value
-
-   INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  var_dim_id  !< list of dimension ids of a variable
+   INTEGER              ::  i                    !< loop index
+   INTEGER              ::  j                    !< loop index
+   INTEGER              ::  nc_data_type         !< netcdf data type of output variable
+   INTEGER              ::  nc_dimension_length  !< length of dimension in netcdf file
+   INTEGER              ::  nc_stat              !< return value of Netcdf calls
+   INTEGER, INTENT(OUT) ::  return_value         !< return value
+
+   INTEGER, DIMENSION(:), ALLOCATABLE ::  dimension_ids  !< list of dimension ids of a variable
 
 
@@ -581,16 +583,16 @@
 
       !-- Define dimensions in NetCDF file
-      ALLOCATE( dim_id_netcdf(1:MAXVAL(dimension_list(:)%id)) )
+      ALLOCATE( dimension_id_netcdf(1:MAXVAL(dimension_list(:)%id)) )
 
       DO  i = 1, SIZE( dimension_list )
 
          IF ( dimension_list(i)%length < 0 )  THEN
-            nc_dim_length = NF90_UNLIMITED
+            nc_dimension_length = NF90_UNLIMITED
          ELSE
-            nc_dim_length = dimension_list(i)%length
+            nc_dimension_length = dimension_list(i)%length
          ENDIF
 
-         nc_stat =  NF90_DEF_DIM( nc_file_id, dimension_list(i)%name, nc_dim_length, &
-                                  dim_id_netcdf(dimension_list(i)%id) )
+         nc_stat =  NF90_DEF_DIM( nc_file_id, dimension_list(i)%name, nc_dimension_length, &
+                                  dimension_id_netcdf(dimension_list(i)%id) )
 
          IF ( nc_stat /= NF90_NOERR )  THEN
@@ -609,11 +611,12 @@
 
       !-- Create vector to map variable IDs from binary file to those within netcdf file
-      ALLOCATE( var_id_netcdf(MIN( MINVAL(attribute_list(:)%var_id),   &
-                                   MINVAL(variable_list(:)%id) )     : &
-                              MAX( MAXVAL(attribute_list(:)%var_id),   &
-                                   MAXVAL(variable_list(:)%id) )     ) )
+      ALLOCATE( variable_id_netcdf(MIN( MINVAL( attribute_list(:)%variable_id ), &
+                                        MINVAL( variable_list(:)%id ) )          &
+                                   :                                             &
+                                   MAX( MAXVAL( attribute_list(:)%variable_id ), &
+                                        MAXVAL( variable_list(:)%id ) )     ) )
 
       !-- Map global id from binary file to that of the netcdf file
-      var_id_netcdf(dom_global_id) = NF90_GLOBAL
+      variable_id_netcdf(dom_global_id) = NF90_GLOBAL
 
       !-- Define variables in NetCDF file
@@ -651,14 +654,14 @@
          IF ( return_value == 0 )  THEN
 
-            ALLOCATE( var_dim_id(1:SIZE( variable_list(i)%dimension_ids )) )
+            ALLOCATE( dimension_ids(1:SIZE( variable_list(i)%dimension_ids )) )
 
             DO  j = 1, SIZE( variable_list(i)%dimension_ids )
 
-               var_dim_id(j) = dim_id_netcdf(variable_list(i)%dimension_ids(j))
+               dimension_ids(j) = dimension_id_netcdf(variable_list(i)%dimension_ids(j))
 
             ENDDO
 
             nc_stat =  NF90_DEF_VAR( nc_file_id, variable_list(i)%name, nc_data_type, &
-                                     var_dim_id, var_id_netcdf(variable_list(i)%id) )
+                                     dimension_ids, variable_id_netcdf(variable_list(i)%id) )
             IF ( nc_stat /= NF90_NOERR )  THEN
                return_value = 1
@@ -668,5 +671,5 @@
             ENDIF
 
-            DEALLOCATE( var_dim_id )
+            DEALLOCATE( dimension_ids )
 
          ENDIF
@@ -686,42 +689,42 @@
 
             CASE ( 'char' )
-               nc_stat = NF90_PUT_ATT( nc_file_id,                              &
-                                       var_id_netcdf(attribute_list(i)%var_id), &
-                                       TRIM(attribute_list(i)%name),            &
+               nc_stat = NF90_PUT_ATT( nc_file_id,                                        &
+                                       variable_id_netcdf(attribute_list(i)%variable_id), &
+                                       TRIM(attribute_list(i)%name),                      &
                                        TRIM(attribute_list(i)%value_char) )
 
             CASE ( 'int8' )
-               nc_stat = NF90_PUT_ATT( nc_file_id,                              &
-                                       var_id_netcdf(attribute_list(i)%var_id), &
-                                       TRIM(attribute_list(i)%name),            &
+               nc_stat = NF90_PUT_ATT( nc_file_id,                                        &
+                                       variable_id_netcdf(attribute_list(i)%variable_id), &
+                                       TRIM(attribute_list(i)%name),                      &
                                        attribute_list(i)%value_int8 )
 
             CASE ( 'int16' )
-               nc_stat = NF90_PUT_ATT( nc_file_id,                              &
-                                       var_id_netcdf(attribute_list(i)%var_id), &
-                                       TRIM(attribute_list(i)%name),            &
+               nc_stat = NF90_PUT_ATT( nc_file_id,                                        &
+                                       variable_id_netcdf(attribute_list(i)%variable_id), &
+                                       TRIM(attribute_list(i)%name),                      &
                                        attribute_list(i)%value_int16 )
 
             CASE ( 'int32' )
-               nc_stat = NF90_PUT_ATT( nc_file_id,                              &
-                                       var_id_netcdf(attribute_list(i)%var_id), &
-                                       TRIM(attribute_list(i)%name),            &
+               nc_stat = NF90_PUT_ATT( nc_file_id,                                        &
+                                       variable_id_netcdf(attribute_list(i)%variable_id), &
+                                       TRIM(attribute_list(i)%name),                      &
                                        attribute_list(i)%value_int32 )
 
             CASE ( 'real32' )
-               nc_stat = NF90_PUT_ATT( nc_file_id,                              &
-                                       var_id_netcdf(attribute_list(i)%var_id), &
-                                       TRIM(attribute_list(i)%name),            &
+               nc_stat = NF90_PUT_ATT( nc_file_id,                                        &
+                                       variable_id_netcdf(attribute_list(i)%variable_id), &
+                                       TRIM(attribute_list(i)%name),                      &
                                        attribute_list(i)%value_real32 )
 
             CASE ( 'real64' )
-               nc_stat = NF90_PUT_ATT( nc_file_id,                              &
-                                       var_id_netcdf(attribute_list(i)%var_id), &
-                                       TRIM(attribute_list(i)%name),            &
+               nc_stat = NF90_PUT_ATT( nc_file_id,                                        &
+                                       variable_id_netcdf(attribute_list(i)%variable_id), &
+                                       TRIM(attribute_list(i)%name),                      &
                                        attribute_list(i)%value_real64 )
 
             CASE DEFAULT
                return_value = 1
-               CALL internal_message( 'error', routine_name // &
+               CALL internal_message( 'error', routine_name //                   &
                        ': data type "' // TRIM( attribute_list(i)%data_type ) // &
                        '" of attribute "' // TRIM( attribute_list(i)%name ) //   &
@@ -733,6 +736,6 @@
          IF ( nc_stat /= NF90_NOERR )  THEN
             return_value = 1
-            CALL internal_message( 'error', routine_name // &
-                    ': attribute "' // TRIM( attribute_list(i)%name ) //   &
+            CALL internal_message( 'error', routine_name //              &
+                    ': attribute "' // TRIM( attribute_list(i)%name ) // &
                     '": NF90_PUT_ATT error: ' // TRIM( NF90_STRERROR( nc_stat ) ) )
             EXIT
@@ -750,5 +753,5 @@
       return_value = 1
       CALL internal_message( 'error', routine_name // &
-              ': NF90_ENDDEF error: ' // TRIM( NF90_STRERROR( nc_stat ) ) )
+                             ': NF90_ENDDEF error: ' // TRIM( NF90_STRERROR( nc_stat ) ) )
    ENDIF
 
@@ -772,14 +775,14 @@
    INTEGER              ::  i             !< loop file_index
    INTEGER              ::  io_stat       !< status of Fortran I/O operations
-   INTEGER              ::  pe_id         !< loop index for loop over PE files
-   INTEGER              ::  n_dim         !< number of dimensions of a variable
+   INTEGER              ::  rank          !< loop index for loop over rank files
+   INTEGER              ::  n_dimensions  !< number of dimensions of a variable
    INTEGER              ::  nc_stat       !< return value of Netcdf calls
    INTEGER, INTENT(OUT) ::  return_value  !< return value
-   INTEGER(iwp)         ::  var_id        !< variable id read from binary file
-
-   INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  start_positions           !< start position of data per dimension
-   INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  data_count_per_dimension  !< data count of variable per dimension
-   INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  bounds_start              !< lower bounds of variable
-   INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  bounds_origin             !< lower bounds of dimensions in output file
+   INTEGER              ::  variable_id   !< variable id read from binary file
+
+   INTEGER, DIMENSION(:), ALLOCATABLE ::  start_positions           !< start position of data per dimension
+   INTEGER, DIMENSION(:), ALLOCATABLE ::  data_count_per_dimension  !< data count of variable per dimension
+   INTEGER, DIMENSION(:), ALLOCATABLE ::  bounds_start              !< lower bounds of variable
+   INTEGER, DIMENSION(:), ALLOCATABLE ::  bounds_origin             !< lower bounds of dimensions in output file
 
    INTEGER(KIND=1), DIMENSION(:), ALLOCATABLE ::  values_int8   !< variable values
@@ -797,9 +800,9 @@
    return_value = 0
 
-   !-- Open binary files of every possible PE
-   DO  pe_id = 0, dom_nrank - 1
+   !-- Open binary files of every possible MPI rank
+   DO  rank = 0, dom_nranks - 1
 
       WRITE( bin_filename, '(A, I6.6)' ) &
-         TRIM( filename_prefix ) // TRIM( bin_filename_body ) // '_', pe_id
+         TRIM( filename_prefix ) // TRIM( bin_filename_body ) // '_', rank
 
       INQUIRE( FILE=bin_filename, EXIST=file_exists )
@@ -846,17 +849,17 @@
          DO WHILE ( io_stat == 0  .AND.  return_value == 0 )
 
-            READ( bin_file_unit, IOSTAT=io_stat ) var_id
+            READ( bin_file_unit, IOSTAT=io_stat ) variable_id
             IF ( io_stat < 0 )  EXIT  ! End-of-file
 
             DO  i = LBOUND( variable_list, DIM=1 ), UBOUND( variable_list, DIM=1 )
-               IF ( var_id == variable_list(i)%id )  THEN
-                  n_dim = SIZE( variable_list(i)%dimension_ids )
+               IF ( variable_id == variable_list(i)%id )  THEN
+                  n_dimensions = SIZE( variable_list(i)%dimension_ids )
                   variable_name = variable_list(i)%name
 
                   CALL internal_message( 'debug', routine_name // ': read variable "' // &
                                          TRIM( variable_name ) // '"' )
-                  WRITE( temp_string, * ) n_dim
+                  WRITE( temp_string, * ) n_dimensions
                   CALL internal_message( 'debug', routine_name // &
-                                         ':  n_dim = ' // TRIM( temp_string ) )
+                                         ':  n_dimensions = ' // TRIM( temp_string ) )
 
                   EXIT
@@ -864,12 +867,12 @@
             ENDDO
 
-            ALLOCATE( bounds_start(1:n_dim) )
-            ALLOCATE( bounds_origin(1:n_dim) )
-            ALLOCATE( start_positions(1:n_dim) )
-            ALLOCATE( data_count_per_dimension(1:n_dim) )
-
-            READ( bin_file_unit ) ( bounds_start(i), i = 1, n_dim )
-            READ( bin_file_unit ) ( data_count_per_dimension(i), i = 1, n_dim )
-            READ( bin_file_unit ) ( bounds_origin(i), i = 1, n_dim )
+            ALLOCATE( bounds_start(1:n_dimensions) )
+            ALLOCATE( bounds_origin(1:n_dimensions) )
+            ALLOCATE( start_positions(1:n_dimensions) )
+            ALLOCATE( data_count_per_dimension(1:n_dimensions) )
+
+            READ( bin_file_unit ) ( bounds_start(i), i = 1, n_dimensions )
+            READ( bin_file_unit ) ( data_count_per_dimension(i), i = 1, n_dimensions )
+            READ( bin_file_unit ) ( bounds_origin(i), i = 1, n_dimensions )
 
             WRITE( temp_string, * ) bounds_start
@@ -885,5 +888,5 @@
             data_count = 1
 
-            DO  i = 1, n_dim
+            DO  i = 1, n_dimensions
                data_count = data_count * data_count_per_dimension(i)
                start_positions(i) = bounds_start(i) - bounds_origin(i) + 1
@@ -900,5 +903,6 @@
                   READ( bin_file_unit ) ( values_int8(i), i = 1, data_count )
 
-                  nc_stat = NF90_PUT_VAR( nc_file_id, var_id_netcdf(var_id), values_int8, &
+                  nc_stat = NF90_PUT_VAR( nc_file_id, variable_id_netcdf(variable_id), &
+                               values_int8,                                            &
                                start = start_positions, count = data_count_per_dimension )
 
@@ -910,5 +914,6 @@
                   READ( bin_file_unit ) ( values_int16(i), i = 1, data_count )
 
-                  nc_stat = NF90_PUT_VAR( nc_file_id, var_id_netcdf(var_id), values_int16, &
+                  nc_stat = NF90_PUT_VAR( nc_file_id, variable_id_netcdf(variable_id), &
+                               values_int16,                                           &
                                start = start_positions, count = data_count_per_dimension )
 
@@ -920,5 +925,6 @@
                   READ( bin_file_unit ) ( values_int32(i), i = 1, data_count )
 
-                  nc_stat = NF90_PUT_VAR( nc_file_id, var_id_netcdf(var_id), values_int32, &
+                  nc_stat = NF90_PUT_VAR( nc_file_id, variable_id_netcdf(variable_id), &
+                               values_int32,                                           &
                                start = start_positions, count = data_count_per_dimension )
 
@@ -930,5 +936,6 @@
                   READ( bin_file_unit ) ( values_intwp(i), i = 1, data_count )
 
-                  nc_stat = NF90_PUT_VAR( nc_file_id, var_id_netcdf(var_id), values_intwp, &
+                  nc_stat = NF90_PUT_VAR( nc_file_id, variable_id_netcdf(variable_id), &
+                               values_intwp,                                          &
                                start = start_positions, count = data_count_per_dimension )
 
@@ -940,5 +947,6 @@
                   READ( bin_file_unit ) ( values_real32(i), i = 1, data_count )
 
-                  nc_stat = NF90_PUT_VAR( nc_file_id, var_id_netcdf(var_id), values_real32, &
+                  nc_stat = NF90_PUT_VAR( nc_file_id, variable_id_netcdf(variable_id), &
+                               values_real32,                                          &
                                start = start_positions, count = data_count_per_dimension )
 
@@ -950,5 +958,6 @@
                   READ( bin_file_unit ) ( values_real64(i), i = 1, data_count )
 
-                  nc_stat = NF90_PUT_VAR( nc_file_id, var_id_netcdf(var_id), values_real64, &
+                  nc_stat = NF90_PUT_VAR( nc_file_id, variable_id_netcdf(variable_id), &
+                               values_real64,                                          &
                                start = start_positions, count = data_count_per_dimension )
 
@@ -960,5 +969,6 @@
                   READ( bin_file_unit ) ( values_realwp(i), i = 1, data_count )
 
-                  nc_stat = NF90_PUT_VAR( nc_file_id, var_id_netcdf(var_id), values_realwp, &
+                  nc_stat = NF90_PUT_VAR( nc_file_id, variable_id_netcdf(variable_id), &
+                               values_realwp,                                          &
                                start = start_positions, count = data_count_per_dimension )
 
@@ -997,7 +1007,7 @@
 
    !-- Deallocate fields for next file
-   IF ( ALLOCATED( variable_list ) )  DEALLOCATE( variable_list )
-   IF ( ALLOCATED( dim_id_netcdf ) )  DEALLOCATE( dim_id_netcdf )
-   IF ( ALLOCATED( var_id_netcdf ) )  DEALLOCATE( var_id_netcdf )
+   IF ( ALLOCATED( variable_list       ) )  DEALLOCATE( variable_list )
+   IF ( ALLOCATED( dimension_id_netcdf ) )  DEALLOCATE( dimension_id_netcdf )
+   IF ( ALLOCATED( variable_id_netcdf  ) )  DEALLOCATE( variable_id_netcdf )
 
 END SUBROUTINE convert_data_to_netcdf
@@ -1010,6 +1020,6 @@
 SUBROUTINE internal_message( level, string )
 
-   CHARACTER(LEN=*), INTENT(IN) :: level  !< message importance level
-   CHARACTER(LEN=*), INTENT(IN) :: string !< message string
+   CHARACTER(LEN=*), INTENT(IN) :: level   !< message importance level
+   CHARACTER(LEN=*), INTENT(IN) :: string  !< message string
 
    IF ( TRIM( level ) == 'error' )  THEN
