Index: palm/trunk/SOURCE/data_output_binary_module.f90
===================================================================
--- palm/trunk/SOURCE/data_output_binary_module.f90	(revision 4146)
+++ palm/trunk/SOURCE/data_output_binary_module.f90	(revision 4147)
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! corrected indentation according to coding standard
+!
+! 4141 2019-08-05 12:24:51Z gronemeier
 ! Initial revision
 !
@@ -38,95 +41,95 @@
 !> @todo Get iostat value of write statements.
 !--------------------------------------------------------------------------------------------------!
-MODULE data_output_binary_module
-
-   USE kinds
+ MODULE data_output_binary_module
+
+    USE kinds
 
 #if defined( __parallel )
 #if defined( __mpifh )
-   INCLUDE "mpif.h"
+    INCLUDE "mpif.h"
 #else
-   USE MPI
+    USE MPI
 #endif
 #endif
 
-   IMPLICIT NONE
-
-   INTEGER, PARAMETER ::  charlen = 100  !< maximum length of character variables
-
-   CHARACTER(LEN=*), PARAMETER ::  config_file_name = 'BINARY_TO_NETCDF_CONFIG'  !< name of config file
-   CHARACTER(LEN=*), PARAMETER ::  mode_binary = 'binary'                        !< string to select operation mode of module
-   CHARACTER(LEN=*), PARAMETER ::  file_prefix = 'BIN_'                          !< file prefix for binary files
-
-   CHARACTER(LEN=charlen)      ::  file_suffix = ''             !< file suffix added to each file name
-   CHARACTER(LEN=800)          ::  internal_error_message = ''  !< string containing the last error message
-   CHARACTER(LEN=800)          ::  temp_string                  !< dummy string
-
-   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
-   LOGICAL ::  config_file_open = .FALSE.            !< true if config file is opened and not closed
-   LOGICAL ::  print_debug_output = .FALSE.          !< if true, debug output is printed
-
-   SAVE
-
-   PRIVATE
-
-   INTERFACE binary_init_module
-      MODULE PROCEDURE binary_init_module
-   END INTERFACE binary_init_module
-
-   INTERFACE binary_open_file
-      MODULE PROCEDURE binary_open_file
-   END INTERFACE binary_open_file
-
-   INTERFACE binary_init_dimension
-      MODULE PROCEDURE binary_init_dimension
-   END INTERFACE binary_init_dimension
-
-   INTERFACE binary_init_variable
-      MODULE PROCEDURE binary_init_variable
-   END INTERFACE binary_init_variable
-
-   INTERFACE binary_write_attribute
-      MODULE PROCEDURE binary_write_attribute
-   END INTERFACE binary_write_attribute
-
-   INTERFACE binary_stop_file_header_definition
-      MODULE PROCEDURE binary_stop_file_header_definition
-   END INTERFACE binary_stop_file_header_definition
-
-   INTERFACE binary_write_variable
-      MODULE PROCEDURE binary_write_variable
-   END INTERFACE binary_write_variable
-
-   INTERFACE binary_finalize
-      MODULE PROCEDURE binary_finalize
-   END INTERFACE binary_finalize
-
-   INTERFACE binary_get_error_message
-      MODULE PROCEDURE binary_get_error_message
-   END INTERFACE binary_get_error_message
-
-   PUBLIC &
-      binary_finalize, &
-      binary_get_error_message, &
-      binary_init_dimension, &
-      binary_stop_file_header_definition, &
-      binary_init_module, &
-      binary_init_variable, &
-      binary_open_file, &
-      binary_write_attribute, &
-      binary_write_variable
-
-
-CONTAINS
+    IMPLICIT NONE
+
+    INTEGER, PARAMETER ::  charlen = 100  !< maximum length of character variables
+
+    CHARACTER(LEN=*), PARAMETER ::  config_file_name = 'BINARY_TO_NETCDF_CONFIG'  !< name of config file
+    CHARACTER(LEN=*), PARAMETER ::  mode_binary = 'binary'                        !< string to select operation mode of module
+    CHARACTER(LEN=*), PARAMETER ::  file_prefix = 'BIN_'                          !< file prefix for binary files
+
+    CHARACTER(LEN=charlen)      ::  file_suffix = ''             !< file suffix added to each file name
+    CHARACTER(LEN=800)          ::  internal_error_message = ''  !< string containing the last error message
+    CHARACTER(LEN=800)          ::  temp_string                  !< dummy string
+
+    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
+    LOGICAL ::  config_file_open = .FALSE.            !< true if config file is opened and not closed
+    LOGICAL ::  print_debug_output = .FALSE.          !< if true, debug output is printed
+
+    SAVE
+
+    PRIVATE
+
+    INTERFACE binary_init_module
+       MODULE PROCEDURE binary_init_module
+    END INTERFACE binary_init_module
+
+    INTERFACE binary_open_file
+       MODULE PROCEDURE binary_open_file
+    END INTERFACE binary_open_file
+
+    INTERFACE binary_init_dimension
+       MODULE PROCEDURE binary_init_dimension
+    END INTERFACE binary_init_dimension
+
+    INTERFACE binary_init_variable
+       MODULE PROCEDURE binary_init_variable
+    END INTERFACE binary_init_variable
+
+    INTERFACE binary_write_attribute
+       MODULE PROCEDURE binary_write_attribute
+    END INTERFACE binary_write_attribute
+
+    INTERFACE binary_stop_file_header_definition
+       MODULE PROCEDURE binary_stop_file_header_definition
+    END INTERFACE binary_stop_file_header_definition
+
+    INTERFACE binary_write_variable
+       MODULE PROCEDURE binary_write_variable
+    END INTERFACE binary_write_variable
+
+    INTERFACE binary_finalize
+       MODULE PROCEDURE binary_finalize
+    END INTERFACE binary_finalize
+
+    INTERFACE binary_get_error_message
+       MODULE PROCEDURE binary_get_error_message
+    END INTERFACE binary_get_error_message
+
+    PUBLIC &
+       binary_finalize, &
+       binary_get_error_message, &
+       binary_init_dimension, &
+       binary_stop_file_header_definition, &
+       binary_init_module, &
+       binary_init_variable, &
+       binary_open_file, &
+       binary_write_attribute, &
+       binary_write_variable
+
+
+ CONTAINS
 
 
@@ -136,29 +139,29 @@
 !> Initialize data-output module.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE binary_init_module( file_suffix_of_output_group, mpi_comm_of_output_group, &
-                               master_output_rank,                                    &
-                               program_debug_output_unit, debug_output, dom_global_id )
-
-   CHARACTER(LEN=*), INTENT(IN) ::  file_suffix_of_output_group  !> file-name suffix added to each file;
-                                                                 !> must be unique for each output group
-
-   INTEGER, 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
-
-
-   file_suffix = file_suffix_of_output_group
-   output_group_comm = mpi_comm_of_output_group
-   master_rank = master_output_rank
-
-   debug_output_unit = program_debug_output_unit
-   print_debug_output = debug_output
-
-   global_id_in_file = dom_global_id
-
-END SUBROUTINE binary_init_module
+ SUBROUTINE binary_init_module( file_suffix_of_output_group, mpi_comm_of_output_group, &
+                                master_output_rank,                                    &
+                                program_debug_output_unit, debug_output, dom_global_id )
+
+    CHARACTER(LEN=*), INTENT(IN) ::  file_suffix_of_output_group  !> file-name suffix added to each file;
+                                                                  !> must be unique for each output group
+
+    INTEGER, 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
+
+
+    file_suffix = file_suffix_of_output_group
+    output_group_comm = mpi_comm_of_output_group
+    master_rank = master_output_rank
+
+    debug_output_unit = program_debug_output_unit
+    print_debug_output = debug_output
+
+    global_id_in_file = dom_global_id
+
+ END SUBROUTINE binary_init_module
 
 !--------------------------------------------------------------------------------------------------!
@@ -167,155 +170,155 @@
 !> Open binary file.
 !--------------------------------------------------------------------------------------------------!
-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) ::  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
-
-   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'binary_open_file'  !< name of this routine
-
-   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
-
-
-   return_value = 0
+ 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) ::  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
+
+    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'binary_open_file'  !< name of this routine
+
+    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
+
+
+    return_value = 0
 
 #if defined( __parallel )
-   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
-      WRITE( my_rank_char, '("_",I6.6)' )  my_rank
-   ELSE
-      CALL internal_message( 'error', routine_name // ': MPI error' )
-   ENDIF
+    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
+       WRITE( my_rank_char, '("_",I6.6)' )  my_rank
+    ELSE
+       CALL internal_message( 'error', routine_name // ': MPI error' )
+    ENDIF
 #else
-   nranks = 1
-   my_rank = master_rank
-   WRITE( my_rank_char, '("_",I6.6)' )  my_rank
+    nranks = 1
+    my_rank = master_rank
+    WRITE( my_rank_char, '("_",I6.6)' )  my_rank
 #endif
-
-   !-- Check mode (not required, added for compatibility reasons)
-   IF ( TRIM( mode ) == mode_binary )  CONTINUE
-
-   !-- Open binary config file for combining script
-   IF ( return_value == 0  .AND.  binary_open_file_first_call )  THEN
-
-      binary_open_file_first_call = .FALSE.
-      config_file_unit = binary_file_lowest_unit
-
-      IF ( my_rank == master_rank )  THEN
-
-         !-- Remove any pre-existing file
-         INQUIRE( FILE=TRIM( config_file_name ) // TRIM( file_suffix ), &
-                  EXIST=file_exists )
-
-         IF ( file_exists )  THEN
-            CALL internal_message( 'debug', routine_name //     &
-                                   ': Remove existing file ' // &
-                                   TRIM( config_file_name ) // TRIM( file_suffix ) )
-            !> @note Fortran2008 feature 'EXECUTE_COMMAND_LINE' not yet supported by
-            !>       PGI 18.10 compiler. Hence, non-standard 'SYSTEM' call must be used
-            ! CALL EXECUTE_COMMAND_LINE(                                                &
-            !         COMMAND='rm ' // TRIM( config_file_name ) // TRIM( file_suffix ), &
-            !         WAIT=.TRUE., EXITSTAT=return_value )
-            CALL SYSTEM( 'rm ' // TRIM( config_file_name ) // TRIM( file_suffix ) )
-         ENDIF
-
-         OPEN( config_file_unit, FILE=TRIM( config_file_name ) // TRIM( file_suffix ), &
-               FORM='UNFORMATTED', STATUS='NEW', IOSTAT=return_value )
-
-         IF ( return_value == 0 )  THEN
-
-            config_file_open = .TRUE.
-
-            !-- Write some general information to config file
-            WRITE( config_file_unit )  nranks
-            WRITE( config_file_unit )  master_rank
-            WRITE( config_file_unit )  LEN( file_prefix )
-            WRITE( config_file_unit )  file_prefix
-            WRITE( config_file_unit )  charlen
-            WRITE( config_file_unit )  global_id_in_file
-
-         ELSE
-
-            return_value = 1
-            CALL internal_message( 'error', routine_name // ': could not create config' )
-
-         ENDIF
-
-      ENDIF
-
-      next_available_unit = binary_file_lowest_unit + 1
-
-   ENDIF
-
-   !-- Initialize output file: open, write header, initialize variable/dimension IDs
-   IF ( return_value == 0 )  THEN
-
-      bin_filename = file_prefix // TRIM( file_name ) // TRIM( file_suffix ) // my_rank_char
-
-      !-- Remove any pre-existing file
-      INQUIRE( FILE=TRIM( bin_filename ), EXIST=file_exists )
-
-      IF ( file_exists )  THEN
-         CALL internal_message( 'debug', routine_name // &
-                                ': 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
-         ! CALL EXECUTE_COMMAND_LINE( COMMAND='rm ' // TRIM( bin_filename ), &
-         !                            WAIT=.TRUE., EXITSTAT=return_value )
-         CALL SYSTEM( 'rm ' // TRIM( bin_filename ) )
-      ENDIF
-
-      !-- Open binary file
-      CALL internal_message( 'debug', routine_name // ': open file ' // TRIM( bin_filename ) )
-      OPEN ( next_available_unit, FILE=TRIM( bin_filename ), &
-             FORM='UNFORMATTED', STATUS='NEW', IOSTAT=return_value )
-
-      IF ( return_value == 0 )  THEN
-
-         !-- Add file_name to config file
-         IF ( my_rank == master_rank )  THEN
-            WRITE( config_file_unit )  file_name
-         ENDIF
-
-         !-- Save file ID and increase next file unit number
-         file_id = next_available_unit
-         next_available_unit = next_available_unit + 1
-
-         !-- Write some meta data to file
-         WRITE ( file_id )  charlen
-         WRITE ( file_id )  file_id
-         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_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_variable_id(binary_file_lowest_unit+1:file_id) )
-         ENDIF
-         files_highest_variable_id(file_id) = 0
-
-      ELSE
-         return_value = 1
-         CALL internal_message( 'error', routine_name // &
-                                ': could not open file "' // TRIM( file_name ) // '"')
-      ENDIF
-
-   ENDIF
-
-END SUBROUTINE binary_open_file
+!
+!-- Check mode (not required, added for compatibility reasons)
+    IF ( TRIM( mode ) == mode_binary )  CONTINUE
+!
+!-- Open binary config file for combining script
+    IF ( return_value == 0  .AND.  binary_open_file_first_call )  THEN
+
+       binary_open_file_first_call = .FALSE.
+       config_file_unit = binary_file_lowest_unit
+
+       IF ( my_rank == master_rank )  THEN
+!
+!--       Remove any pre-existing file
+          INQUIRE( FILE=TRIM( config_file_name ) // TRIM( file_suffix ), &
+                   EXIST=file_exists )
+
+          IF ( file_exists )  THEN
+             CALL internal_message( 'debug', routine_name //     &
+                                    ': Remove existing file ' // &
+                                    TRIM( config_file_name ) // TRIM( file_suffix ) )
+             !> @note Fortran2008 feature 'EXECUTE_COMMAND_LINE' not yet supported by
+             !>       PGI 18.10 compiler. Hence, non-standard 'SYSTEM' call must be used
+             ! CALL EXECUTE_COMMAND_LINE(                                                &
+             !         COMMAND='rm ' // TRIM( config_file_name ) // TRIM( file_suffix ), &
+             !         WAIT=.TRUE., EXITSTAT=return_value )
+             CALL SYSTEM( 'rm ' // TRIM( config_file_name ) // TRIM( file_suffix ) )
+          ENDIF
+
+          OPEN( config_file_unit, FILE=TRIM( config_file_name ) // TRIM( file_suffix ), &
+                FORM='UNFORMATTED', STATUS='NEW', IOSTAT=return_value )
+
+          IF ( return_value == 0 )  THEN
+
+             config_file_open = .TRUE.
+!
+!--          Write some general information to config file
+             WRITE( config_file_unit )  nranks
+             WRITE( config_file_unit )  master_rank
+             WRITE( config_file_unit )  LEN( file_prefix )
+             WRITE( config_file_unit )  file_prefix
+             WRITE( config_file_unit )  charlen
+             WRITE( config_file_unit )  global_id_in_file
+
+          ELSE
+
+             return_value = 1
+             CALL internal_message( 'error', routine_name // ': could not create config' )
+
+          ENDIF
+
+       ENDIF
+
+       next_available_unit = binary_file_lowest_unit + 1
+
+    ENDIF
+!
+!-- Initialize output file: open, write header, initialize variable/dimension IDs
+    IF ( return_value == 0 )  THEN
+
+       bin_filename = file_prefix // TRIM( file_name ) // TRIM( file_suffix ) // my_rank_char
+!
+!--    Remove any pre-existing file
+       INQUIRE( FILE=TRIM( bin_filename ), EXIST=file_exists )
+
+       IF ( file_exists )  THEN
+          CALL internal_message( 'debug', routine_name // &
+                                 ': 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
+          ! CALL EXECUTE_COMMAND_LINE( COMMAND='rm ' // TRIM( bin_filename ), &
+          !                            WAIT=.TRUE., EXITSTAT=return_value )
+          CALL SYSTEM( 'rm ' // TRIM( bin_filename ) )
+       ENDIF
+!
+!--    Open binary file
+       CALL internal_message( 'debug', routine_name // ': open file ' // TRIM( bin_filename ) )
+       OPEN ( next_available_unit, FILE=TRIM( bin_filename ), &
+              FORM='UNFORMATTED', STATUS='NEW', IOSTAT=return_value )
+
+       IF ( return_value == 0 )  THEN
+!
+!--       Add file_name to config file
+          IF ( my_rank == master_rank )  THEN
+             WRITE( config_file_unit )  file_name
+          ENDIF
+!
+!--       Save file ID and increase next file unit number
+          file_id = next_available_unit
+          next_available_unit = next_available_unit + 1
+!
+!--       Write some meta data to file
+          WRITE ( file_id )  charlen
+          WRITE ( file_id )  file_id
+          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_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_variable_id(binary_file_lowest_unit+1:file_id) )
+          ENDIF
+          files_highest_variable_id(file_id) = 0
+
+       ELSE
+          return_value = 1
+          CALL internal_message( 'error', routine_name // &
+                                 ': could not open file "' // TRIM( file_name ) // '"')
+       ENDIF
+
+    ENDIF
+
+ END SUBROUTINE binary_open_file
 
 !--------------------------------------------------------------------------------------------------!
@@ -324,70 +327,70 @@
 !> Write attribute to file.
 !--------------------------------------------------------------------------------------------------!
-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)           ::  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
-
-
-   return_value = 0
-
-   CALL internal_message( 'debug', TRIM( routine_name ) // &
-                          ': write attribute ' // TRIM( attribute_name ) )
-
-   !-- Write attribute to file
-   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 ) // &
-                             ': no value given for attribute "' // TRIM( attribute_name ) // '"' )
-   ENDIF
-
-END SUBROUTINE binary_write_attribute
+ 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)           ::  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
+
+
+    return_value = 0
+
+    CALL internal_message( 'debug', TRIM( routine_name ) // &
+                           ': write attribute ' // TRIM( attribute_name ) )
+!
+!-- Write attribute to file
+    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 ) // &
+                              ': no value given for attribute "' // TRIM( attribute_name ) // '"' )
+    ENDIF
+
+ END SUBROUTINE binary_write_attribute
 
 !--------------------------------------------------------------------------------------------------!
@@ -397,49 +400,49 @@
 !> 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, 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( dimension_name ) )
-
-   !-- Check mode (not required, added for compatibility reasons only)
-   IF ( TRIM( mode ) == mode_binary )  CONTINUE
-
-   !-- Assign dimension ID
-   dimension_id = files_highest_variable_id( file_id ) + 1
-   files_highest_variable_id( file_id ) = dimension_id
-
-   !-- Define dimension in file
-   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, 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( dimension_name ) // '"' )
-   ENDIF
-
-END SUBROUTINE binary_init_dimension
+ 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, 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( dimension_name ) )
+!
+!-- Check mode (not required, added for compatibility reasons only)
+    IF ( TRIM( mode ) == mode_binary )  CONTINUE
+!
+!-- Assign dimension ID
+    dimension_id = files_highest_variable_id( file_id ) + 1
+    files_highest_variable_id( file_id ) = dimension_id
+!
+!-- Define dimension in file
+    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, 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( dimension_name ) // '"' )
+    ENDIF
+
+ END SUBROUTINE binary_init_dimension
 
 !--------------------------------------------------------------------------------------------------!
@@ -448,47 +451,47 @@
 !> Initialize variable. Write information of variable into file header.
 !--------------------------------------------------------------------------------------------------!
-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, 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)
-
-
-   return_value = 0
-
-   CALL internal_message( 'debug', routine_name // ': init variable ' // TRIM( variable_name ) )
-
-   !-- Check mode (not required, added for compatibility reasons only)
-   IF ( TRIM( mode ) == mode_binary )  CONTINUE
-
-   !-- Check if variable is global (not required, added for compatibility reasons only)
-   IF ( is_global )  CONTINUE
-
-   !-- Assign variable ID
-   variable_id = files_highest_variable_id( file_id ) + 1
-   files_highest_variable_id( file_id ) = variable_id
-
-   !-- Write variable information in file
-   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
+ 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, 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)
+
+
+    return_value = 0
+
+    CALL internal_message( 'debug', routine_name // ': init variable ' // TRIM( variable_name ) )
+!
+!-- Check mode (not required, added for compatibility reasons only)
+    IF ( TRIM( mode ) == mode_binary )  CONTINUE
+!
+!-- Check if variable is global (not required, added for compatibility reasons only)
+    IF ( is_global )  CONTINUE
+!
+!-- Assign variable ID
+    variable_id = files_highest_variable_id( file_id ) + 1
+    files_highest_variable_id( file_id ) = variable_id
+!
+!-- Write variable information in file
+    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,24 +500,24 @@
 !> Leave file definition state.
 !--------------------------------------------------------------------------------------------------!
-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
-
-
-   return_value = 0
-
-   WRITE( temp_string, * ) file_id
-   CALL internal_message( 'debug', routine_name // &
-                          ': finalize file definition (file_id=' // TRIM( temp_string ) // ')' )
-
-   output_string = '*** end file header ***'
-   WRITE( file_id )  output_string
-
-END SUBROUTINE binary_stop_file_header_definition
+ 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
+
+
+    return_value = 0
+
+    WRITE( temp_string, * ) file_id
+    CALL internal_message( 'debug', routine_name // &
+                           ': finalize file definition (file_id=' // TRIM( temp_string ) // ')' )
+
+    output_string = '*** end file header ***'
+    WRITE( file_id )  output_string
+
+ END SUBROUTINE binary_stop_file_header_definition
 
 !--------------------------------------------------------------------------------------------------!
@@ -523,200 +526,207 @@
 !> Write variable to file.
 !--------------------------------------------------------------------------------------------------!
-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) ::  output_string  !< output string
-
-   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'binary_write_variable'  !< name of this routine
-
-   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                   ::  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)' ) variable_id, file_id
-   CALL internal_message( 'debug', routine_name // TRIM( temp_string ) )
-
-   IF ( is_global )  CONTINUE  ! reqired to prevent compiler warning
-
-   IF ( .NOT. ANY( value_counts == 0 ) )  THEN
-      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( 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( 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( 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( 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( 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( 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( 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
-         CALL internal_message( 'error', routine_name // ': no values given' )
-      ENDIF
-
-   ENDIF
-
-END SUBROUTINE binary_write_variable
+ 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) ::  output_string  !< output string
+
+    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'binary_write_variable'  !< name of this routine
+
+    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                   ::  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)' ) variable_id, file_id
+    CALL internal_message( 'debug', routine_name // TRIM( temp_string ) )
+
+    IF ( is_global )  CONTINUE  ! reqired to prevent compiler warning
+
+    IF ( .NOT. ANY( value_counts == 0 ) )  THEN
+       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( 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( 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( 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( 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( 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( 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( 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
+          CALL internal_message( 'error', routine_name // ': no values given' )
+       ENDIF
+
+    ENDIF
+
+ END SUBROUTINE binary_write_variable
 
 !--------------------------------------------------------------------------------------------------!
@@ -725,49 +735,49 @@
 !> Close opened files.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE binary_finalize( file_id, return_value )
-
-   CHARACTER(LEN=charlen) ::  output_string  !< output string
-
-   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'binary_finalize'  !< name of this routine
-
-   INTEGER, INTENT(IN)  ::  file_id       !< file ID
-   INTEGER, INTENT(OUT) ::  return_value  !< return value
-
-
-   IF ( config_file_open )  THEN
-
-      output_string = '*** end config file ***'
-      WRITE( config_file_unit )  output_string
-
-      CLOSE( config_file_unit, IOSTAT=return_value )
-
-      IF ( return_value /= 0 )  THEN
-         CALL internal_message( 'error', routine_name // ': cannot close configuration file' )
-      ELSE
-         config_file_open = .FALSE.
-      ENDIF
-
-   ELSE
-
-      return_value = 0
-
-   ENDIF
-
-   IF ( return_value == 0 )  THEN
-
-      WRITE( temp_string, * ) file_id
-      CALL internal_message( 'debug', routine_name // &
-                             ': 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 // &
-                                ': cannot close file (file_id=' // TRIM( temp_string ) // ')' )
-      ENDIF
-
-   ENDIF
-
-END SUBROUTINE binary_finalize
+ SUBROUTINE binary_finalize( file_id, return_value )
+
+    CHARACTER(LEN=charlen) ::  output_string  !< output string
+
+    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'binary_finalize'  !< name of this routine
+
+    INTEGER, INTENT(IN)  ::  file_id       !< file ID
+    INTEGER, INTENT(OUT) ::  return_value  !< return value
+
+
+    IF ( config_file_open )  THEN
+
+       output_string = '*** end config file ***'
+       WRITE( config_file_unit )  output_string
+
+       CLOSE( config_file_unit, IOSTAT=return_value )
+
+       IF ( return_value /= 0 )  THEN
+          CALL internal_message( 'error', routine_name // ': cannot close configuration file' )
+       ELSE
+          config_file_open = .FALSE.
+       ENDIF
+
+    ELSE
+
+       return_value = 0
+
+    ENDIF
+
+    IF ( return_value == 0 )  THEN
+
+       WRITE( temp_string, * ) file_id
+       CALL internal_message( 'debug', routine_name // &
+                              ': 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 // &
+                                 ': cannot close file (file_id=' // TRIM( temp_string ) // ')' )
+       ENDIF
+
+    ENDIF
+
+ END SUBROUTINE binary_finalize
 
 !--------------------------------------------------------------------------------------------------!
@@ -777,22 +787,22 @@
 !> 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)' ) ': ', 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
+ 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)' ) ': ', 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
 
 !--------------------------------------------------------------------------------------------------!
@@ -801,14 +811,14 @@
 !> Return the last created 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
+ 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 4146)
+++ palm/trunk/SOURCE/data_output_module.f90	(revision 4147)
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! corrected indentation according to coding standard
+!
+! 4141 2019-08-05 12:24:51Z gronemeier
 ! Initial revision
 !
@@ -59,180 +62,180 @@
 !> @todo Convert variable if type of given values do not fit specified type.
 !--------------------------------------------------------------------------------------------------!
-MODULE data_output_module
-
-   USE kinds
-
-   USE data_output_netcdf4_module, &
-      ONLY: netcdf4_init_dimension, &
-            netcdf4_get_error_message, &
-            netcdf4_stop_file_header_definition, &
-            netcdf4_init_module, &
-            netcdf4_init_variable, &
-            netcdf4_finalize, &
-            netcdf4_open_file, &
-            netcdf4_write_attribute, &
-            netcdf4_write_variable
-
-   USE data_output_binary_module, &
-      ONLY: binary_finalize, &
-            binary_get_error_message, &
-            binary_init_dimension, &
-            binary_stop_file_header_definition, &
-            binary_init_module, &
-            binary_init_variable, &
-            binary_open_file, &
-            binary_write_attribute, &
-            binary_write_variable
-
-   IMPLICIT NONE
-
-   INTEGER, PARAMETER ::  charlen = 100  !< maximum length of character variables
-   INTEGER, PARAMETER ::  no_id = -1     !< default ID if no ID was assigned
-
-   TYPE attribute_type
-      CHARACTER(LEN=charlen) ::  data_type = ''  !< data type
-      CHARACTER(LEN=charlen) ::  name            !< attribute name
-      CHARACTER(LEN=charlen) ::  value_char      !< attribute value if character
-      INTEGER(KIND=1)        ::  value_int8      !< attribute value if 8bit integer
-      INTEGER(KIND=2)        ::  value_int16     !< attribute value if 16bit integer
-      INTEGER(KIND=4)        ::  value_int32     !< attribute value if 32bit integer
-      REAL(KIND=4)           ::  value_real32    !< attribute value if 32bit real
-      REAL(KIND=8)           ::  value_real64    !< attribute value if 64bit real
-   END TYPE attribute_type
-
-   TYPE variable_type
-      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                                    ::  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
-      INTEGER(KIND=4), DIMENSION(:), ALLOCATABLE ::  masked_values_int32   !< masked dimension values if 32bit integer
-      INTEGER(iwp),    DIMENSION(:), ALLOCATABLE ::  masked_values_intwp   !< masked dimension values if working-precision int
-      INTEGER(KIND=1), DIMENSION(:), ALLOCATABLE ::  values_int8           !< dimension values if 16bit integer
-      INTEGER(KIND=2), DIMENSION(:), ALLOCATABLE ::  values_int16          !< dimension values if 16bit integer
-      INTEGER(KIND=4), DIMENSION(:), ALLOCATABLE ::  values_int32          !< dimension values if 32bit integer
-      INTEGER(iwp),    DIMENSION(:), ALLOCATABLE ::  values_intwp          !< dimension values if working-precision integer
-      LOGICAL,         DIMENSION(:), ALLOCATABLE ::  mask                  !< mask
-      REAL(KIND=4),    DIMENSION(:), ALLOCATABLE ::  masked_values_real32  !< masked dimension values if 32bit real
-      REAL(KIND=8),    DIMENSION(:), ALLOCATABLE ::  masked_values_real64  !< masked dimension values if 64bit real
-      REAL(wp),        DIMENSION(:), ALLOCATABLE ::  masked_values_realwp  !< masked dimension values if working-precision real
-      REAL(KIND=4),    DIMENSION(:), ALLOCATABLE ::  values_real32         !< dimension values if 32bit real
-      REAL(KIND=8),    DIMENSION(:), ALLOCATABLE ::  values_real64         !< dimension values if 64bit real
-      REAL(wp),        DIMENSION(:), ALLOCATABLE ::  values_realwp         !< dimension values if working-precision real
-      TYPE(attribute_type), DIMENSION(:), ALLOCATABLE ::  attributes       !< list of attributes
-   END TYPE dimension_type
-
-   TYPE file_type
-      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_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
-
-   TYPE(file_type), DIMENSION(:), ALLOCATABLE ::  files  !< file list
-
-   SAVE
-
-   PRIVATE
-
-   !> Initialize the data-output module
-   INTERFACE dom_init
-      MODULE PROCEDURE dom_init
-   END INTERFACE dom_init
-
-   !> Add files to database
-   INTERFACE dom_def_file
-      MODULE PROCEDURE dom_def_file
-   END INTERFACE dom_def_file
-
-   !> Add dimensions to database
-   INTERFACE dom_def_dim
-      MODULE PROCEDURE dom_def_dim
-   END INTERFACE dom_def_dim
-
-   !> Add variables to database
-   INTERFACE dom_def_var
-      MODULE PROCEDURE dom_def_var
-   END INTERFACE dom_def_var
-
-   !> Add attributes to database
-   INTERFACE dom_def_att
-      MODULE PROCEDURE dom_def_att_char
-      MODULE PROCEDURE dom_def_att_int8
-      MODULE PROCEDURE dom_def_att_int16
-      MODULE PROCEDURE dom_def_att_int32
-      MODULE PROCEDURE dom_def_att_real32
-      MODULE PROCEDURE dom_def_att_real64
-   END INTERFACE dom_def_att
-
-   !> Prepare for output: evaluate database and create files
-   INTERFACE dom_def_end
-      MODULE PROCEDURE dom_def_end
-   END INTERFACE dom_def_end
-
-   !> Write variables to file
-   INTERFACE dom_write_var
-      MODULE PROCEDURE dom_write_var
-   END INTERFACE dom_write_var
-
-   !> Last actions required for output befor termination
-   INTERFACE dom_finalize_output
-      MODULE PROCEDURE dom_finalize_output
-   END INTERFACE dom_finalize_output
-
-   !> Return error message
-   INTERFACE dom_get_error_message
-      MODULE PROCEDURE dom_get_error_message
-   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_init, &
-      dom_def_file, &
-      dom_def_dim, &
-      dom_def_var, &
-      dom_def_att, &
-      dom_def_end, &
-      dom_write_var, &
-      dom_finalize_output, &
-      dom_get_error_message, &
-      dom_database_debug_output
-
-CONTAINS
+ MODULE data_output_module
+
+    USE kinds
+
+    USE data_output_netcdf4_module, &
+       ONLY: netcdf4_init_dimension, &
+             netcdf4_get_error_message, &
+             netcdf4_stop_file_header_definition, &
+             netcdf4_init_module, &
+             netcdf4_init_variable, &
+             netcdf4_finalize, &
+             netcdf4_open_file, &
+             netcdf4_write_attribute, &
+             netcdf4_write_variable
+
+    USE data_output_binary_module, &
+       ONLY: binary_finalize, &
+             binary_get_error_message, &
+             binary_init_dimension, &
+             binary_stop_file_header_definition, &
+             binary_init_module, &
+             binary_init_variable, &
+             binary_open_file, &
+             binary_write_attribute, &
+             binary_write_variable
+
+    IMPLICIT NONE
+
+    INTEGER, PARAMETER ::  charlen = 100  !< maximum length of character variables
+    INTEGER, PARAMETER ::  no_id = -1     !< default ID if no ID was assigned
+
+    TYPE attribute_type
+       CHARACTER(LEN=charlen) ::  data_type = ''  !< data type
+       CHARACTER(LEN=charlen) ::  name            !< attribute name
+       CHARACTER(LEN=charlen) ::  value_char      !< attribute value if character
+       INTEGER(KIND=1)        ::  value_int8      !< attribute value if 8bit integer
+       INTEGER(KIND=2)        ::  value_int16     !< attribute value if 16bit integer
+       INTEGER(KIND=4)        ::  value_int32     !< attribute value if 32bit integer
+       REAL(KIND=4)           ::  value_real32    !< attribute value if 32bit real
+       REAL(KIND=8)           ::  value_real64    !< attribute value if 64bit real
+    END TYPE attribute_type
+
+    TYPE variable_type
+       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                                    ::  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
+       INTEGER(KIND=4), DIMENSION(:), ALLOCATABLE ::  masked_values_int32   !< masked dimension values if 32bit integer
+       INTEGER(iwp),    DIMENSION(:), ALLOCATABLE ::  masked_values_intwp   !< masked dimension values if working-precision int
+       INTEGER(KIND=1), DIMENSION(:), ALLOCATABLE ::  values_int8           !< dimension values if 16bit integer
+       INTEGER(KIND=2), DIMENSION(:), ALLOCATABLE ::  values_int16          !< dimension values if 16bit integer
+       INTEGER(KIND=4), DIMENSION(:), ALLOCATABLE ::  values_int32          !< dimension values if 32bit integer
+       INTEGER(iwp),    DIMENSION(:), ALLOCATABLE ::  values_intwp          !< dimension values if working-precision integer
+       LOGICAL,         DIMENSION(:), ALLOCATABLE ::  mask                  !< mask
+       REAL(KIND=4),    DIMENSION(:), ALLOCATABLE ::  masked_values_real32  !< masked dimension values if 32bit real
+       REAL(KIND=8),    DIMENSION(:), ALLOCATABLE ::  masked_values_real64  !< masked dimension values if 64bit real
+       REAL(wp),        DIMENSION(:), ALLOCATABLE ::  masked_values_realwp  !< masked dimension values if working-precision real
+       REAL(KIND=4),    DIMENSION(:), ALLOCATABLE ::  values_real32         !< dimension values if 32bit real
+       REAL(KIND=8),    DIMENSION(:), ALLOCATABLE ::  values_real64         !< dimension values if 64bit real
+       REAL(wp),        DIMENSION(:), ALLOCATABLE ::  values_realwp         !< dimension values if working-precision real
+       TYPE(attribute_type), DIMENSION(:), ALLOCATABLE ::  attributes       !< list of attributes
+    END TYPE dimension_type
+
+    TYPE file_type
+       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_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
+
+    TYPE(file_type), DIMENSION(:), ALLOCATABLE ::  files  !< file list
+
+    SAVE
+
+    PRIVATE
+
+    !> Initialize the data-output module
+    INTERFACE dom_init
+       MODULE PROCEDURE dom_init
+    END INTERFACE dom_init
+
+    !> Add files to database
+    INTERFACE dom_def_file
+       MODULE PROCEDURE dom_def_file
+    END INTERFACE dom_def_file
+
+    !> Add dimensions to database
+    INTERFACE dom_def_dim
+       MODULE PROCEDURE dom_def_dim
+    END INTERFACE dom_def_dim
+
+    !> Add variables to database
+    INTERFACE dom_def_var
+       MODULE PROCEDURE dom_def_var
+    END INTERFACE dom_def_var
+
+    !> Add attributes to database
+    INTERFACE dom_def_att
+       MODULE PROCEDURE dom_def_att_char
+       MODULE PROCEDURE dom_def_att_int8
+       MODULE PROCEDURE dom_def_att_int16
+       MODULE PROCEDURE dom_def_att_int32
+       MODULE PROCEDURE dom_def_att_real32
+       MODULE PROCEDURE dom_def_att_real64
+    END INTERFACE dom_def_att
+
+    !> Prepare for output: evaluate database and create files
+    INTERFACE dom_def_end
+       MODULE PROCEDURE dom_def_end
+    END INTERFACE dom_def_end
+
+    !> Write variables to file
+    INTERFACE dom_write_var
+       MODULE PROCEDURE dom_write_var
+    END INTERFACE dom_write_var
+
+    !> Last actions required for output befor termination
+    INTERFACE dom_finalize_output
+       MODULE PROCEDURE dom_finalize_output
+    END INTERFACE dom_finalize_output
+
+    !> Return error message
+    INTERFACE dom_get_error_message
+       MODULE PROCEDURE dom_get_error_message
+    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_init, &
+       dom_def_file, &
+       dom_def_dim, &
+       dom_def_var, &
+       dom_def_att, &
+       dom_def_end, &
+       dom_write_var, &
+       dom_finalize_output, &
+       dom_get_error_message, &
+       dom_database_debug_output
+
+ CONTAINS
 
 
@@ -247,34 +250,34 @@
 !> 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, &
-                     program_debug_output_unit, debug_output )
-
-   CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  file_suffix_of_output_group  !< file-name suffix added to each file;
-                                                                           !> must be unique for each output group
-
-   INTEGER, INTENT(IN), OPTIONAL ::  master_output_rank         !< MPI rank executing tasks which must
-                                                                !> be executed by a single PE only
-   INTEGER, INTENT(IN)           ::  mpi_comm_of_output_group   !< MPI communicator specifying the MPI group
-                                                                !> which participate in the output
-   INTEGER, INTENT(IN)           ::  program_debug_output_unit  !< file unit number for debug output
-
-   LOGICAL, INTENT(IN)           ::  debug_output               !< if true, debug output is printed
-
-
-   IF ( PRESENT( file_suffix_of_output_group ) )  output_file_suffix = file_suffix_of_output_group
-   IF ( PRESENT( master_output_rank ) )  master_rank = master_output_rank
-
-   output_group_comm = mpi_comm_of_output_group
-
-   debug_output_unit = program_debug_output_unit
-   print_debug_output = debug_output
-
-   CALL binary_init_module( output_file_suffix, output_group_comm, master_rank, &
-                            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_id )
-
-END SUBROUTINE dom_init
+ SUBROUTINE dom_init( file_suffix_of_output_group, mpi_comm_of_output_group, master_output_rank, &
+                      program_debug_output_unit, debug_output )
+
+    CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  file_suffix_of_output_group  !< file-name suffix added to each file;
+                                                                            !> must be unique for each output group
+
+    INTEGER, INTENT(IN), OPTIONAL ::  master_output_rank         !< MPI rank executing tasks which must
+                                                                 !> be executed by a single PE only
+    INTEGER, INTENT(IN)           ::  mpi_comm_of_output_group   !< MPI communicator specifying the MPI group
+                                                                 !> which participate in the output
+    INTEGER, INTENT(IN)           ::  program_debug_output_unit  !< file unit number for debug output
+
+    LOGICAL, INTENT(IN)           ::  debug_output               !< if true, debug output is printed
+
+
+    IF ( PRESENT( file_suffix_of_output_group ) )  output_file_suffix = file_suffix_of_output_group
+    IF ( PRESENT( master_output_rank ) )  master_rank = master_output_rank
+
+    output_group_comm = mpi_comm_of_output_group
+
+    debug_output_unit = program_debug_output_unit
+    print_debug_output = debug_output
+
+    CALL binary_init_module( output_file_suffix, output_group_comm, master_rank, &
+                             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_id )
+
+ END SUBROUTINE dom_init
 
 !--------------------------------------------------------------------------------------------------!
@@ -285,60 +288,61 @@
 !>   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 ::  f             !< loop index
-   INTEGER ::  return_value  !< return value
-
-   TYPE(file_type), DIMENSION(:), ALLOCATABLE ::  files_tmp  !< temporary file list
-
-
-   return_value = 0
-
-   CALL internal_message( 'debug', routine_name // ': define file "' // TRIM( file_name ) // '"' )
-
-   !-- Allocate file list or extend it by 1
-   IF ( .NOT. ALLOCATED( files ) ) THEN
-
-      nfiles = 1
-      ALLOCATE( files(nfiles) )
-
-   ELSE
-
-      nfiles = SIZE( files )
-      !-- Check if file already exists
-      DO  f = 1, nfiles
-         IF ( files(f)%name == TRIM( file_name ) )  THEN
-            return_value = 1
-            CALL internal_message( 'error', routine_name // &
-                    ': file "' // TRIM( file_name ) // '" already exists' )
-            EXIT
-         ENDIF
-      ENDDO
-
-      !-- Extend file list
-      IF ( return_value == 0 )  THEN
-         ALLOCATE( files_tmp(nfiles) )
-         files_tmp = files
-         DEALLOCATE( files )
-         nfiles = nfiles + 1
-         ALLOCATE( files(nfiles) )
-         files(:nfiles-1) = files_tmp
-         DEALLOCATE( files_tmp )
-      ENDIF
-
-   ENDIF
-
-   !-- Add new file to database
-   IF ( return_value == 0 )  THEN
-      files(nfiles)%name = TRIM( file_name )
-      files(nfiles)%format = TRIM( file_format )
-   ENDIF
-
-END FUNCTION dom_def_file
+ 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 ::  f             !< loop index
+    INTEGER ::  return_value  !< return value
+
+    TYPE(file_type), DIMENSION(:), ALLOCATABLE ::  files_tmp  !< temporary file list
+
+
+    return_value = 0
+
+    CALL internal_message( 'debug', routine_name // ': define file "' // TRIM( file_name ) // '"' )
+!
+!-- Allocate file list or extend it by 1
+    IF ( .NOT. ALLOCATED( files ) ) THEN
+
+       nfiles = 1
+       ALLOCATE( files(nfiles) )
+
+    ELSE
+
+       nfiles = SIZE( files )
+!
+!--    Check if file already exists
+       DO  f = 1, nfiles
+          IF ( files(f)%name == TRIM( file_name ) )  THEN
+             return_value = 1
+             CALL internal_message( 'error', routine_name // &
+                     ': file "' // TRIM( file_name ) // '" already exists' )
+             EXIT
+          ENDIF
+       ENDDO
+!
+!--    Extend file list
+       IF ( return_value == 0 )  THEN
+          ALLOCATE( files_tmp(nfiles) )
+          files_tmp = files
+          DEALLOCATE( files )
+          nfiles = nfiles + 1
+          ALLOCATE( files(nfiles) )
+          files(:nfiles-1) = files_tmp
+          DEALLOCATE( files_tmp )
+       ENDIF
+
+    ENDIF
+!
+!-- Add new file to database
+    IF ( return_value == 0 )  THEN
+       files(nfiles)%name = TRIM( file_name )
+       files(nfiles)%format = TRIM( file_format )
+    ENDIF
+
+ END FUNCTION dom_def_file
 
 !--------------------------------------------------------------------------------------------------!
@@ -370,367 +374,368 @@
 !> @todo Convert given values into selected output_type.
 !--------------------------------------------------------------------------------------------------!
-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) ::  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 ::  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 ' //                    &
-                          '(dimension "' // TRIM( dimension_name ) // &
-                          '", file "' // TRIM( file_name ) // '")' )
-
-   dimension%name      = TRIM( dimension_name )
-   dimension%data_type = TRIM( output_type )
-
-   !-- Check dimension bounds and allocate dimension according to bounds
-   IF ( SIZE( bounds ) == 1 )  THEN
-
-      !-- Dimension has only lower bound, which means it changes its size
-      !-- during simulation.
-      !-- Set length to -1 as indicator.
-      dimension%bounds(:) = bounds(1)
-      dimension%length    = -1
-
-      IF ( PRESENT( mask ) )  THEN
-         return_value = 1
-         CALL internal_message( 'error', routine_name //                      &
-                                ': unlimited dimensions cannot be masked ' // &
-                                '(dimension "' // TRIM( dimension_name ) //   &
-                                '", file "' // TRIM( file_name ) // '")!' )
-      ENDIF
-
-   ELSEIF ( SIZE( bounds ) == 2 )  THEN
-
-      dimension%bounds = bounds
-      dimension%length = bounds(2) - bounds(1) + 1
-
-      !-- Save dimension values
-      IF ( PRESENT( values_int8 ) )  THEN
-         ALLOCATE( dimension%values_int8(dimension%bounds(1):dimension%bounds(2)) )
-         IF ( SIZE( values_int8 ) == dimension%length )  THEN
-            dimension%values_int8 = values_int8
-         ELSEIF ( SIZE( values_int8 ) == 1 )  THEN
-            dimension%values_int8(:) = values_int8(1)
-         ELSE
-            return_value = 2
-         ENDIF
-      ELSEIF( PRESENT( values_int16 ) )  THEN
-         ALLOCATE( dimension%values_int16(dimension%bounds(1):dimension%bounds(2)) )
-         IF ( SIZE( values_int16 ) == dimension%length )  THEN
-            dimension%values_int16 = values_int16
-         ELSEIF ( SIZE( values_int16 ) == 1 )  THEN
-            dimension%values_int16(:) = values_int16(1)
-         ELSE
-            return_value = 2
-         ENDIF
-      ELSEIF( PRESENT( values_int32 ) )  THEN
-         ALLOCATE( dimension%values_int32(dimension%bounds(1):dimension%bounds(2)) )
-         IF ( SIZE( values_int32 ) == dimension%length )  THEN
-            dimension%values_int32 = values_int32
-         ELSEIF ( SIZE( values_int32 ) == 1 )  THEN
-            dimension%values_int32(:) = values_int32(1)
-         ELSE
-            return_value = 2
-         ENDIF
-      ELSEIF( PRESENT( values_intwp ) )  THEN
-         ALLOCATE( dimension%values_intwp(dimension%bounds(1):dimension%bounds(2)) )
-         IF ( SIZE( values_intwp ) == dimension%length )  THEN
-            dimension%values_intwp = values_intwp
-         ELSEIF ( SIZE( values_intwp ) == 1 )  THEN
-            dimension%values_intwp(:) = values_intwp(1)
-         ELSE
-            return_value = 2
-         ENDIF
-      ELSEIF( PRESENT( values_real32 ) )  THEN
-         ALLOCATE( dimension%values_real32(dimension%bounds(1):dimension%bounds(2)) )
-         IF ( SIZE( values_real32 ) == dimension%length )  THEN
-            dimension%values_real32 = values_real32
-         ELSEIF ( SIZE( values_real32 ) == 1 )  THEN
-            dimension%values_real32(:) = values_real32(1)
-         ELSE
-            return_value = 2
-         ENDIF
-      ELSEIF( PRESENT( values_real64 ) )  THEN
-         ALLOCATE( dimension%values_real64(dimension%bounds(1):dimension%bounds(2)) )
-         IF ( SIZE( values_real64 ) == dimension%length )  THEN
-            dimension%values_real64 = values_real64
-         ELSEIF ( SIZE( values_real64 ) == 1 )  THEN
-            dimension%values_real64(:) = values_real64(1)
-         ELSE
-            return_value = 2
-         ENDIF
-      ELSEIF( PRESENT( values_realwp ) )  THEN
-         ALLOCATE( dimension%values_realwp(dimension%bounds(1):dimension%bounds(2)) )
-         IF ( SIZE( values_realwp ) == dimension%length )  THEN
-            dimension%values_realwp = values_realwp
-         ELSEIF ( SIZE( values_realwp ) == 1 )  THEN
-            dimension%values_realwp(:) = values_realwp(1)
-         ELSE
-            return_value = 2
-         ENDIF
-      ELSE
-         return_value = 1
-         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 //                               &
-                                ': number of values and given bounds do not match ' // &
-                                '(dimension "' // TRIM( dimension_name ) //            &
-                                '", file "' // TRIM( file_name ) // '")!' )
-      ENDIF
-
-      !-- Initialize mask
-      IF ( PRESENT( mask )  .AND.  return_value == 0 )  THEN
-
-         IF ( dimension%length == SIZE( mask ) )  THEN
-
-            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 //                           &
-                                   ': size of mask and given bounds do not match ' // &
-                                   '(dimension "' // TRIM( dimension_name ) //        &
-                                   '", file "' // TRIM( file_name ) // '")!' )
-         ENDIF
-
-      ENDIF
-
-   ELSE
-
-      return_value = 1
-      CALL internal_message( 'error', routine_name //                                       &
-                             ': at least one but no more than two bounds must be given ' // &
-                             '(dimension "' // TRIM( dimension_name ) //                    &
-                             '", file "' // TRIM( file_name ) // '")!' )
-
-   ENDIF
-
-   !-- Add dimension to database
-   IF ( return_value == 0 )  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 already initialized. ' //             &
-                                      'No further dimension definition allowed ' // &
-                                      '(dimension "' // TRIM( dimension_name ) //   &
-                                      '", file "' // TRIM( file_name ) // '")!' )
-               EXIT
-
-            ELSEIF ( .NOT. ALLOCATED( files(f)%dimensions ) )  THEN
-
-               ndims = 1
-               ALLOCATE( files(f)%dimensions(ndims) )
-
-            ELSE
-
-               !-- Check if any variable of the same name as the new dimension is already defined
-               IF ( ALLOCATED( files(f)%variables ) )  THEN
-                  DO  i = 1, SIZE( files(f)%variables )
-                     IF ( files(f)%variables(i)%name == dimension%name )  THEN
-                        return_value = 1
-                        CALL internal_message( 'error', routine_name //                    &
-                                ': file 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
-                  ENDDO
-               ENDIF
-
-               IF ( return_value == 0 )  THEN
-                  !-- Check if dimension already exists in file
-                  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 already exists in file ' //    &
-                                '(dimension "' // TRIM( dimension_name ) // &
-                                '", file "' // TRIM( file_name ) // '")!' )
-                        EXIT
-                     ENDIF
-                  ENDDO
-
-                  !-- Extend dimension list
-                  IF ( return_value == 0 )  THEN
-                     ALLOCATE( dimensions_tmp(ndims) )
-                     dimensions_tmp = files(f)%dimensions
-                     DEALLOCATE( files(f)%dimensions )
-                     ndims = ndims + 1
-                     ALLOCATE( files(f)%dimensions(ndims) )
-                     files(f)%dimensions(:ndims-1) = dimensions_tmp
-                     DEALLOCATE( dimensions_tmp )
-                  ENDIF
-               ENDIF
-
-            ENDIF
-
-            !-- Add new dimension to database
-            IF ( return_value == 0 )  files(f)%dimensions(ndims) = dimension
-
-            EXIT
-
-         ENDIF
-      ENDDO
-
-      IF ( f > nfiles )  THEN
-         return_value = 1
-         CALL internal_message( 'error', routine_name //                                     &
-                                ': file not found (dimension "' // TRIM( dimension_name ) // &
-                                '", file "' // TRIM( file_name ) // '")!' )
-      ENDIF
-
-   ENDIF
-
-END FUNCTION dom_def_dim
+ 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) ::  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 ::  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 ' //                    &
+                           '(dimension "' // TRIM( dimension_name ) // &
+                           '", file "' // TRIM( file_name ) // '")' )
+
+    dimension%name      = TRIM( dimension_name )
+    dimension%data_type = TRIM( output_type )
+!
+!-- Check dimension bounds and allocate dimension according to bounds
+    IF ( SIZE( bounds ) == 1 )  THEN
+!
+!--    Dimension has only lower bound, which means it changes its size
+!--    during simulation.
+!--    Set length to -1 as indicator.
+       dimension%bounds(:) = bounds(1)
+       dimension%length    = -1
+
+       IF ( PRESENT( mask ) )  THEN
+          return_value = 1
+          CALL internal_message( 'error', routine_name //                      &
+                                 ': unlimited dimensions cannot be masked ' // &
+                                 '(dimension "' // TRIM( dimension_name ) //   &
+                                 '", file "' // TRIM( file_name ) // '")!' )
+       ENDIF
+
+    ELSEIF ( SIZE( bounds ) == 2 )  THEN
+
+       dimension%bounds = bounds
+       dimension%length = bounds(2) - bounds(1) + 1
+!
+!--    Save dimension values
+       IF ( PRESENT( values_int8 ) )  THEN
+          ALLOCATE( dimension%values_int8(dimension%bounds(1):dimension%bounds(2)) )
+          IF ( SIZE( values_int8 ) == dimension%length )  THEN
+             dimension%values_int8 = values_int8
+          ELSEIF ( SIZE( values_int8 ) == 1 )  THEN
+             dimension%values_int8(:) = values_int8(1)
+          ELSE
+             return_value = 2
+          ENDIF
+       ELSEIF( PRESENT( values_int16 ) )  THEN
+          ALLOCATE( dimension%values_int16(dimension%bounds(1):dimension%bounds(2)) )
+          IF ( SIZE( values_int16 ) == dimension%length )  THEN
+             dimension%values_int16 = values_int16
+          ELSEIF ( SIZE( values_int16 ) == 1 )  THEN
+             dimension%values_int16(:) = values_int16(1)
+          ELSE
+             return_value = 2
+          ENDIF
+       ELSEIF( PRESENT( values_int32 ) )  THEN
+          ALLOCATE( dimension%values_int32(dimension%bounds(1):dimension%bounds(2)) )
+          IF ( SIZE( values_int32 ) == dimension%length )  THEN
+             dimension%values_int32 = values_int32
+          ELSEIF ( SIZE( values_int32 ) == 1 )  THEN
+             dimension%values_int32(:) = values_int32(1)
+          ELSE
+             return_value = 2
+          ENDIF
+       ELSEIF( PRESENT( values_intwp ) )  THEN
+          ALLOCATE( dimension%values_intwp(dimension%bounds(1):dimension%bounds(2)) )
+          IF ( SIZE( values_intwp ) == dimension%length )  THEN
+             dimension%values_intwp = values_intwp
+          ELSEIF ( SIZE( values_intwp ) == 1 )  THEN
+             dimension%values_intwp(:) = values_intwp(1)
+          ELSE
+             return_value = 2
+          ENDIF
+       ELSEIF( PRESENT( values_real32 ) )  THEN
+          ALLOCATE( dimension%values_real32(dimension%bounds(1):dimension%bounds(2)) )
+          IF ( SIZE( values_real32 ) == dimension%length )  THEN
+             dimension%values_real32 = values_real32
+          ELSEIF ( SIZE( values_real32 ) == 1 )  THEN
+             dimension%values_real32(:) = values_real32(1)
+          ELSE
+             return_value = 2
+          ENDIF
+       ELSEIF( PRESENT( values_real64 ) )  THEN
+          ALLOCATE( dimension%values_real64(dimension%bounds(1):dimension%bounds(2)) )
+          IF ( SIZE( values_real64 ) == dimension%length )  THEN
+             dimension%values_real64 = values_real64
+          ELSEIF ( SIZE( values_real64 ) == 1 )  THEN
+             dimension%values_real64(:) = values_real64(1)
+          ELSE
+             return_value = 2
+          ENDIF
+       ELSEIF( PRESENT( values_realwp ) )  THEN
+          ALLOCATE( dimension%values_realwp(dimension%bounds(1):dimension%bounds(2)) )
+          IF ( SIZE( values_realwp ) == dimension%length )  THEN
+             dimension%values_realwp = values_realwp
+          ELSEIF ( SIZE( values_realwp ) == 1 )  THEN
+             dimension%values_realwp(:) = values_realwp(1)
+          ELSE
+             return_value = 2
+          ENDIF
+       ELSE
+          return_value = 1
+          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 //                               &
+                                 ': number of values and given bounds do not match ' // &
+                                 '(dimension "' // TRIM( dimension_name ) //            &
+                                 '", file "' // TRIM( file_name ) // '")!' )
+       ENDIF
+!
+!--    Initialize mask
+       IF ( PRESENT( mask )  .AND.  return_value == 0 )  THEN
+
+          IF ( dimension%length == SIZE( mask ) )  THEN
+
+             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 //                           &
+                                    ': size of mask and given bounds do not match ' // &
+                                    '(dimension "' // TRIM( dimension_name ) //        &
+                                    '", file "' // TRIM( file_name ) // '")!' )
+          ENDIF
+
+       ENDIF
+
+    ELSE
+
+       return_value = 1
+       CALL internal_message( 'error', routine_name //                                       &
+                              ': at least one but no more than two bounds must be given ' // &
+                              '(dimension "' // TRIM( dimension_name ) //                    &
+                              '", file "' // TRIM( file_name ) // '")!' )
+
+    ENDIF
+!
+!-- Add dimension to database
+    IF ( return_value == 0 )  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 already initialized. ' //             &
+                                       'No further dimension definition allowed ' // &
+                                       '(dimension "' // TRIM( dimension_name ) //   &
+                                       '", file "' // TRIM( file_name ) // '")!' )
+                EXIT
+
+             ELSEIF ( .NOT. ALLOCATED( files(f)%dimensions ) )  THEN
+
+                ndims = 1
+                ALLOCATE( files(f)%dimensions(ndims) )
+
+             ELSE
+!
+!--             Check if any variable of the same name as the new dimension is already defined
+                IF ( ALLOCATED( files(f)%variables ) )  THEN
+                   DO  i = 1, SIZE( files(f)%variables )
+                      IF ( files(f)%variables(i)%name == dimension%name )  THEN
+                         return_value = 1
+                         CALL internal_message( 'error', routine_name //                    &
+                                 ': file 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
+                   ENDDO
+                ENDIF
+
+                IF ( return_value == 0 )  THEN
+!
+!--                Check if dimension already exists in file
+                   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 already exists in file ' //    &
+                                 '(dimension "' // TRIM( dimension_name ) // &
+                                 '", file "' // TRIM( file_name ) // '")!' )
+                         EXIT
+                      ENDIF
+                   ENDDO
+!
+!--                Extend dimension list
+                   IF ( return_value == 0 )  THEN
+                      ALLOCATE( dimensions_tmp(ndims) )
+                      dimensions_tmp = files(f)%dimensions
+                      DEALLOCATE( files(f)%dimensions )
+                      ndims = ndims + 1
+                      ALLOCATE( files(f)%dimensions(ndims) )
+                      files(f)%dimensions(:ndims-1) = dimensions_tmp
+                      DEALLOCATE( dimensions_tmp )
+                   ENDIF
+                ENDIF
+
+             ENDIF
+!
+!--          Add new dimension to database
+             IF ( return_value == 0 )  files(f)%dimensions(ndims) = dimension
+
+             EXIT
+
+          ENDIF
+       ENDDO
+
+       IF ( f > nfiles )  THEN
+          return_value = 1
+          CALL internal_message( 'error', routine_name //                                     &
+                                 ': file not found (dimension "' // TRIM( dimension_name ) // &
+                                 '", file "' // TRIM( file_name ) // '")!' )
+       ENDIF
+
+    ENDIF
+
+ END FUNCTION dom_def_dim
 
 !--------------------------------------------------------------------------------------------------!
@@ -759,165 +764,166 @@
 !>          ALLOCATE( u(<z>,<y>,<x>) )
 !--------------------------------------------------------------------------------------------------!
-FUNCTION dom_def_var( file_name, variable_name, dimension_names, output_type, is_global ) &
-            RESULT( return_value )
-
-   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
-
-   CHARACTER(LEN=*), DIMENSION(:), INTENT(IN) ::  dimension_names  !< list of dimension names
-
-   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
-   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 )) )
-   ALLOCATE( variable%dimension_ids(SIZE( dimension_names )) )
-
-   variable%dimension_names = dimension_names
-   variable%dimension_ids = -1
-   variable%data_type = TRIM( output_type )
-
-   IF ( PRESENT( is_global ) )  THEN
-      variable%is_global = is_global
-   ELSE
-      variable%is_global = .FALSE.
-   ENDIF
-
-   !-- Add variable to database
-   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 already initialized. No further variable definition allowed ' // &
-                    '(variable "' // TRIM( variable_name ) //                                &
-                    '", file "' // TRIM( file_name ) // '")!' )
-            EXIT
-
-         ELSEIF ( ALLOCATED( files(f)%dimensions ) )  THEN
-
-            !-- Check if any dimension of the same name as the new variable is already defined
-            DO  d = 1, SIZE( files(f)%dimensions )
-               IF ( files(f)%dimensions(d)%name == variable%name )  THEN
-                  return_value = 1
-                  CALL internal_message( 'error', routine_name //                    &
-                          ': file 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
-            ENDDO
-
-            !-- Check if dimensions assigned to variable are defined within file
-            IF ( return_value == 0 )  THEN
-               DO  i = 1, SIZE( variable%dimension_names )
-                  found = .FALSE.
-                  DO  d = 1, SIZE( files(f)%dimensions )
-                     IF ( files(f)%dimensions(d)%name == variable%dimension_names(i) )  THEN
-                        found = .TRUE.
-                        EXIT
-                     ENDIF
-                  ENDDO
-                  IF ( .NOT. found )  THEN
-                     return_value = 1
-                     CALL internal_message( 'error', routine_name //                            &
-                             ': required dimension "'//  TRIM( variable%dimension_names(i) ) // &
-                             '" for variable is not defined ' //                                &
-                             '(variable "' // TRIM( variable_name ) //                          &
-                             '", file "' // TRIM( file_name ) // '")!' )
-                     EXIT
-                  ENDIF
-               ENDDO
-            ENDIF
-
-         ELSE
-
-            return_value = 1
-            CALL internal_message( 'error', routine_name //                      &
-                    ': no dimensions defined in file. Cannot define variable '// &
-                    '(variable "' // TRIM( variable_name ) //                    &
-                    '", file "' // TRIM( file_name ) // '")!' )
-
-         ENDIF
-
-         IF ( return_value == 0 )  THEN
-
-            !-- Check if variable already exists
-            IF ( .NOT. ALLOCATED( files(f)%variables ) )  THEN
-
-               nvars = 1
-               ALLOCATE( files(f)%variables(nvars) )
-
-            ELSE
-
-               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 already exists '//            &
-                             '(variable "' // TRIM( variable_name ) // &
-                             '", file "' // TRIM( file_name ) // '")!' )
-                     EXIT
-                  ENDIF
-               ENDDO
-
-               IF ( return_value == 0 )  THEN
-                  !-- Extend variable list
-                  ALLOCATE( variables_tmp(nvars) )
-                  variables_tmp = files(f)%variables
-                  DEALLOCATE( files(f)%variables )
-                  nvars = nvars + 1
-                  ALLOCATE( files(f)%variables(nvars) )
-                  files(f)%variables(:nvars-1) = variables_tmp
-                  DEALLOCATE( variables_tmp )
-               ENDIF
-
-            ENDIF
-
-            !-- Add new variable to database
-            IF ( return_value == 0 )  files(f)%variables(nvars) = variable
-
-         ENDIF
-
-         EXIT
-
-      ENDIF
-
-   ENDDO
-
-   IF ( f > nfiles )  THEN
-      return_value = 1
-      CALL internal_message( 'error', routine_name //                                   &
-                             ': file not found (variable "' // TRIM( variable_name ) // &
-                             '", file "' // TRIM( file_name ) // '")!' )
-   ENDIF
-
-END FUNCTION dom_def_var
+ FUNCTION dom_def_var( file_name, variable_name, dimension_names, output_type, is_global ) &
+             RESULT( return_value )
+
+    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
+
+    CHARACTER(LEN=*), DIMENSION(:), INTENT(IN) ::  dimension_names  !< list of dimension names
+
+    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
+    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 )) )
+    ALLOCATE( variable%dimension_ids(SIZE( dimension_names )) )
+
+    variable%dimension_names = dimension_names
+    variable%dimension_ids = -1
+    variable%data_type = TRIM( output_type )
+
+    IF ( PRESENT( is_global ) )  THEN
+       variable%is_global = is_global
+    ELSE
+       variable%is_global = .FALSE.
+    ENDIF
+!
+!-- Add variable to database
+    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 already initialized. No further variable definition allowed ' // &
+                     '(variable "' // TRIM( variable_name ) //                                &
+                     '", file "' // TRIM( file_name ) // '")!' )
+             EXIT
+
+          ELSEIF ( ALLOCATED( files(f)%dimensions ) )  THEN
+!
+!--          Check if any dimension of the same name as the new variable is already defined
+             DO  d = 1, SIZE( files(f)%dimensions )
+                IF ( files(f)%dimensions(d)%name == variable%name )  THEN
+                   return_value = 1
+                   CALL internal_message( 'error', routine_name //                    &
+                           ': file 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
+             ENDDO
+!
+!--          Check if dimensions assigned to variable are defined within file
+             IF ( return_value == 0 )  THEN
+                DO  i = 1, SIZE( variable%dimension_names )
+                   found = .FALSE.
+                   DO  d = 1, SIZE( files(f)%dimensions )
+                      IF ( files(f)%dimensions(d)%name == variable%dimension_names(i) )  THEN
+                         found = .TRUE.
+                         EXIT
+                      ENDIF
+                   ENDDO
+                   IF ( .NOT. found )  THEN
+                      return_value = 1
+                      CALL internal_message( 'error', routine_name //                            &
+                              ': required dimension "'//  TRIM( variable%dimension_names(i) ) // &
+                              '" for variable is not defined ' //                                &
+                              '(variable "' // TRIM( variable_name ) //                          &
+                              '", file "' // TRIM( file_name ) // '")!' )
+                      EXIT
+                   ENDIF
+                ENDDO
+             ENDIF
+
+          ELSE
+
+             return_value = 1
+             CALL internal_message( 'error', routine_name //                      &
+                     ': no dimensions defined in file. Cannot define variable '// &
+                     '(variable "' // TRIM( variable_name ) //                    &
+                     '", file "' // TRIM( file_name ) // '")!' )
+
+          ENDIF
+
+          IF ( return_value == 0 )  THEN
+!
+!--          Check if variable already exists
+             IF ( .NOT. ALLOCATED( files(f)%variables ) )  THEN
+
+                nvars = 1
+                ALLOCATE( files(f)%variables(nvars) )
+
+             ELSE
+
+                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 already exists '//            &
+                              '(variable "' // TRIM( variable_name ) // &
+                              '", file "' // TRIM( file_name ) // '")!' )
+                      EXIT
+                   ENDIF
+                ENDDO
+
+                IF ( return_value == 0 )  THEN
+!
+!--                Extend variable list
+                   ALLOCATE( variables_tmp(nvars) )
+                   variables_tmp = files(f)%variables
+                   DEALLOCATE( files(f)%variables )
+                   nvars = nvars + 1
+                   ALLOCATE( files(f)%variables(nvars) )
+                   files(f)%variables(:nvars-1) = variables_tmp
+                   DEALLOCATE( variables_tmp )
+                ENDIF
+
+             ENDIF
+!
+!--          Add new variable to database
+             IF ( return_value == 0 )  files(f)%variables(nvars) = variable
+
+          ENDIF
+
+          EXIT
+
+       ENDIF
+
+    ENDDO
+
+    IF ( f > nfiles )  THEN
+       return_value = 1
+       CALL internal_message( 'error', routine_name //                                   &
+                              ': file not found (variable "' // TRIM( variable_name ) // &
+                              '", file "' // TRIM( file_name ) // '")!' )
+    ENDIF
+
+ END FUNCTION dom_def_var
 
 !--------------------------------------------------------------------------------------------------!
@@ -946,46 +952,46 @@
 !>                   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 ::  return_value  !< return value
-
-   LOGICAL                       ::  append_internal  !< same as 'append'
-   LOGICAL, INTENT(IN), OPTIONAL ::  append           !< if true, append value to existing value
-
-   TYPE(attribute_type) ::  attribute  !< new attribute
-
-
-   return_value = 0
-
-   IF ( PRESENT( append ) )  THEN
-      append_internal = append
-   ELSE
-      append_internal = .FALSE.
-   ENDIF
-
-   attribute%name       = TRIM( attribute_name )
-   attribute%data_type  = 'char'
-   attribute%value_char = TRIM( value )
-
-   IF ( PRESENT( variable_name ) )  THEN
-      variable_name_internal = TRIM( variable_name )
-   ELSE
-      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
+ 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 ::  return_value  !< return value
+
+    LOGICAL                       ::  append_internal  !< same as 'append'
+    LOGICAL, INTENT(IN), OPTIONAL ::  append           !< if true, append value to existing value
+
+    TYPE(attribute_type) ::  attribute  !< new attribute
+
+
+    return_value = 0
+
+    IF ( PRESENT( append ) )  THEN
+       append_internal = append
+    ELSE
+       append_internal = .FALSE.
+    ENDIF
+
+    attribute%name       = TRIM( attribute_name )
+    attribute%data_type  = 'char'
+    attribute%value_char = TRIM( value )
+
+    IF ( PRESENT( variable_name ) )  THEN
+       variable_name_internal = TRIM( variable_name )
+    ELSE
+       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
 
 !--------------------------------------------------------------------------------------------------!
@@ -1008,56 +1014,56 @@
 !>                   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
-
-   INTEGER(KIND=1), INTENT(IN) ::  value  !< attribute value
-
-   INTEGER ::  return_value  !< return value
-
-   LOGICAL                       ::  append_internal  !< same as 'append'
-   LOGICAL, INTENT(IN), OPTIONAL ::  append           !< if true, append value to existing value
-
-   TYPE(attribute_type) ::  attribute  !< new attribute
-
-
-   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 //                             &
-                                ': numeric attribute cannot be appended ' //         &
-                                '(attribute "' // TRIM( attribute_name ) //          &
-                                '", variable "' // TRIM( variable_name_internal ) // &
-                                '", file "' // TRIM( file_name ) // '")!' )
-      ENDIF
-   ENDIF
-
-   IF ( return_value == 0 )  THEN
-      append_internal = .FALSE.
-
-      attribute%name       = TRIM( attribute_name )
-      attribute%data_type  = 'int8'
-      attribute%value_int8 = value
-
-      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_int8
+ 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
+
+    INTEGER(KIND=1), INTENT(IN) ::  value  !< attribute value
+
+    INTEGER ::  return_value  !< return value
+
+    LOGICAL                       ::  append_internal  !< same as 'append'
+    LOGICAL, INTENT(IN), OPTIONAL ::  append           !< if true, append value to existing value
+
+    TYPE(attribute_type) ::  attribute  !< new attribute
+
+
+    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 //                             &
+                                 ': numeric attribute cannot be appended ' //         &
+                                 '(attribute "' // TRIM( attribute_name ) //          &
+                                 '", variable "' // TRIM( variable_name_internal ) // &
+                                 '", file "' // TRIM( file_name ) // '")!' )
+       ENDIF
+    ENDIF
+
+    IF ( return_value == 0 )  THEN
+       append_internal = .FALSE.
+
+       attribute%name       = TRIM( attribute_name )
+       attribute%data_type  = 'int8'
+       attribute%value_int8 = value
+
+       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_int8
 
 !--------------------------------------------------------------------------------------------------!
@@ -1080,56 +1086,56 @@
 !>                   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
-
-   INTEGER(KIND=2), INTENT(IN) ::  value  !< attribute value
-
-   INTEGER ::  return_value  !< return value
-
-   LOGICAL                       ::  append_internal  !< same as 'append'
-   LOGICAL, INTENT(IN), OPTIONAL ::  append           !< if true, append value to existing value
-
-   TYPE(attribute_type) ::  attribute  !< new attribute
-
-
-   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 //                             &
-                                ': numeric attribute cannot be appended ' //         &
-                                '(attribute "' // TRIM( attribute_name ) //          &
-                                '", variable "' // TRIM( variable_name_internal ) // &
-                                '", file "' // TRIM( file_name ) // '")!' )
-      ENDIF
-   ENDIF
-
-   IF ( return_value == 0 )  THEN
-      append_internal = .FALSE.
-
-      attribute%name        = TRIM( attribute_name )
-      attribute%data_type   = 'int16'
-      attribute%value_int16 = value
-
-      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_int16
+ 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
+
+    INTEGER(KIND=2), INTENT(IN) ::  value  !< attribute value
+
+    INTEGER ::  return_value  !< return value
+
+    LOGICAL                       ::  append_internal  !< same as 'append'
+    LOGICAL, INTENT(IN), OPTIONAL ::  append           !< if true, append value to existing value
+
+    TYPE(attribute_type) ::  attribute  !< new attribute
+
+
+    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 //                             &
+                                 ': numeric attribute cannot be appended ' //         &
+                                 '(attribute "' // TRIM( attribute_name ) //          &
+                                 '", variable "' // TRIM( variable_name_internal ) // &
+                                 '", file "' // TRIM( file_name ) // '")!' )
+       ENDIF
+    ENDIF
+
+    IF ( return_value == 0 )  THEN
+       append_internal = .FALSE.
+
+       attribute%name        = TRIM( attribute_name )
+       attribute%data_type   = 'int16'
+       attribute%value_int16 = value
+
+       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_int16
 
 !--------------------------------------------------------------------------------------------------!
@@ -1152,56 +1158,56 @@
 !>                   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
-
-   INTEGER(KIND=4), INTENT(IN) ::  value  !< attribute value
-
-   INTEGER ::  return_value  !< return value
-
-   LOGICAL                       ::  append_internal  !< same as 'append'
-   LOGICAL, INTENT(IN), OPTIONAL ::  append           !< if true, append value to existing value
-
-   TYPE(attribute_type) ::  attribute  !< new attribute
-
-
-   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 //                             &
-                                ': numeric attribute cannot be appended ' //         &
-                                '(attribute "' // TRIM( attribute_name ) //          &
-                                '", variable "' // TRIM( variable_name_internal ) // &
-                                '", file "' // TRIM( file_name ) // '")!' )
-      ENDIF
-   ENDIF
-
-   IF ( return_value == 0 )  THEN
-      append_internal = .FALSE.
-
-      attribute%name        = TRIM( attribute_name )
-      attribute%data_type   = 'int32'
-      attribute%value_int32 = value
-
-      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_int32
+ 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
+
+    INTEGER(KIND=4), INTENT(IN) ::  value  !< attribute value
+
+    INTEGER ::  return_value  !< return value
+
+    LOGICAL                       ::  append_internal  !< same as 'append'
+    LOGICAL, INTENT(IN), OPTIONAL ::  append           !< if true, append value to existing value
+
+    TYPE(attribute_type) ::  attribute  !< new attribute
+
+
+    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 //                             &
+                                 ': numeric attribute cannot be appended ' //         &
+                                 '(attribute "' // TRIM( attribute_name ) //          &
+                                 '", variable "' // TRIM( variable_name_internal ) // &
+                                 '", file "' // TRIM( file_name ) // '")!' )
+       ENDIF
+    ENDIF
+
+    IF ( return_value == 0 )  THEN
+       append_internal = .FALSE.
+
+       attribute%name        = TRIM( attribute_name )
+       attribute%data_type   = 'int32'
+       attribute%value_int32 = value
+
+       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_int32
 
 !--------------------------------------------------------------------------------------------------!
@@ -1224,56 +1230,56 @@
 !>                   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 ::  return_value  !< return value
-
-   LOGICAL                       ::  append_internal  !< same as 'append'
-   LOGICAL, INTENT(IN), OPTIONAL ::  append           !< if true, append value to existing value
-
-   REAL(KIND=4), INTENT(IN) ::  value  !< attribute value
-
-   TYPE(attribute_type) ::  attribute  !< new attribute
-
-
-   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 //                             &
-                                ': numeric attribute cannot be appended ' //         &
-                                '(attribute "' // TRIM( attribute_name ) //          &
-                                '", variable "' // TRIM( variable_name_internal ) // &
-                                '", file "' // TRIM( file_name ) // '")!' )
-      ENDIF
-   ENDIF
-
-   IF ( return_value == 0 )  THEN
-      append_internal = .FALSE.
-
-      attribute%name         = TRIM( attribute_name )
-      attribute%data_type    = 'real32'
-      attribute%value_real32 = value
-
-      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_real32
+ 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 ::  return_value  !< return value
+
+    LOGICAL                       ::  append_internal  !< same as 'append'
+    LOGICAL, INTENT(IN), OPTIONAL ::  append           !< if true, append value to existing value
+
+    REAL(KIND=4), INTENT(IN) ::  value  !< attribute value
+
+    TYPE(attribute_type) ::  attribute  !< new attribute
+
+
+    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 //                             &
+                                 ': numeric attribute cannot be appended ' //         &
+                                 '(attribute "' // TRIM( attribute_name ) //          &
+                                 '", variable "' // TRIM( variable_name_internal ) // &
+                                 '", file "' // TRIM( file_name ) // '")!' )
+       ENDIF
+    ENDIF
+
+    IF ( return_value == 0 )  THEN
+       append_internal = .FALSE.
+
+       attribute%name         = TRIM( attribute_name )
+       attribute%data_type    = 'real32'
+       attribute%value_real32 = value
+
+       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_real32
 
 !--------------------------------------------------------------------------------------------------!
@@ -1296,56 +1302,56 @@
 !>                   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 ::  return_value  !< return value
-
-   LOGICAL                       ::  append_internal  !< same as 'append'
-   LOGICAL, INTENT(IN), OPTIONAL ::  append           !< if true, append value to existing value
-
-   REAL(KIND=8), INTENT(IN) ::  value  !< attribute value
-
-   TYPE(attribute_type) ::  attribute  !< new attribute
-
-
-   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 //                             &
-                                ': numeric attribute cannot be appended ' //         &
-                                '(attribute "' // TRIM( attribute_name ) //          &
-                                '", variable "' // TRIM( variable_name_internal ) // &
-                                '", file "' // TRIM( file_name ) // '")!' )
-      ENDIF
-   ENDIF
-
-   IF ( return_value == 0 )  THEN
-      append_internal = .FALSE.
-
-      attribute%name         = TRIM( attribute_name )
-      attribute%data_type    = 'real64'
-      attribute%value_real64 = value
-
-      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
+ 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 ::  return_value  !< return value
+
+    LOGICAL                       ::  append_internal  !< same as 'append'
+    LOGICAL, INTENT(IN), OPTIONAL ::  append           !< if true, append value to existing value
+
+    REAL(KIND=8), INTENT(IN) ::  value  !< attribute value
+
+    TYPE(attribute_type) ::  attribute  !< new attribute
+
+
+    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 //                             &
+                                 ': numeric attribute cannot be appended ' //         &
+                                 '(attribute "' // TRIM( attribute_name ) //          &
+                                 '", variable "' // TRIM( variable_name_internal ) // &
+                                 '", file "' // TRIM( file_name ) // '")!' )
+       ENDIF
+    ENDIF
+
+    IF ( return_value == 0 )  THEN
+       append_internal = .FALSE.
+
+       attribute%name         = TRIM( attribute_name )
+       attribute%data_type    = 'real64'
+       attribute%value_real64 = value
+
+       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
 
 !--------------------------------------------------------------------------------------------------!
@@ -1357,151 +1363,151 @@
 !> 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): &
+ 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_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): &
+                   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_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): &
+                   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_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): &
+                   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_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
+                   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
 
 !--------------------------------------------------------------------------------------------------!
@@ -1526,888 +1532,902 @@
 !>       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, &
+ 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_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, &
+             !$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_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, &
+             !$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_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, &
+             !$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_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, &
+             !$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_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, &
+             !$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_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, &
+             !$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_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, &
+             !$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_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 = 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_write_var
+             !$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 = 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_write_var
 
 !--------------------------------------------------------------------------------------------------!
@@ -2420,54 +2440,54 @@
 !> @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
+ 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
 
 !--------------------------------------------------------------------------------------------------!
@@ -2476,18 +2496,18 @@
 !> 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
+ 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
 
 !--------------------------------------------------------------------------------------------------!
@@ -2498,237 +2518,244 @@
 !> @todo Try to combine similar code parts and shorten routine.
 !--------------------------------------------------------------------------------------------------!
-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
-   LOGICAL, INTENT(IN) ::  append  !< if true, append value to existing value
-
-   TYPE(attribute_type), INTENT(IN) ::  attribute  !< new attribute
-
-   TYPE(attribute_type), DIMENSION(:), ALLOCATABLE ::  atts_tmp  !< temporary attribute list
-
-
-   return_value = 0
-   found = .FALSE.
-
-   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( file_name ) // &
-                    '" is already initialized. No further attribute definition allowed!' )
-            EXIT
-         ENDIF
-
-         !-- Add attribute to file
-         IF ( TRIM( variable_name ) == '' )  THEN
-
-            !-- Initialize first file attribute
-            IF ( .NOT. ALLOCATED( files(f)%attributes ) )  THEN
-               natts = 1
-               ALLOCATE( files(f)%attributes(natts) )
-            ELSE
-               natts = SIZE( files(f)%attributes )
-
-               !-- Check if attribute already exists
-               DO  a = 1, natts
-                  IF ( files(f)%attributes(a)%name == attribute%name )  THEN
-                     IF ( append )  THEN
-                        !-- Append existing string attribute
-                        files(f)%attributes(a)%value_char =             &
-                           TRIM( files(f)%attributes(a)%value_char ) // &
-                           TRIM( attribute%value_char )
-                     ELSE
-                        files(f)%attributes(a) = attribute
-                     ENDIF
-                     found = .TRUE.
-                     EXIT
-                  ENDIF
-               ENDDO
-
-               !-- Extend attribute list by 1
-               IF ( .NOT. found )  THEN
-                  ALLOCATE( atts_tmp(natts) )
-                  atts_tmp = files(f)%attributes
-                  DEALLOCATE( files(f)%attributes )
-                  natts = natts + 1
-                  ALLOCATE( files(f)%attributes(natts) )
-                  files(f)%attributes(:natts-1) = atts_tmp
-                  DEALLOCATE( atts_tmp )
-               ENDIF
-            ENDIF
-
-            !-- Save new attribute to the end of the attribute list
-            IF ( .NOT. found )  THEN
-               files(f)%attributes(natts) = attribute
-               found = .TRUE.
-            ENDIF
-
-            EXIT
-
-         ELSE
-
-            !-- Add attribute to dimension
-            IF ( ALLOCATED( files(f)%dimensions ) )  THEN
-
-               DO  d = 1, SIZE( files(f)%dimensions )
-
-                  IF ( files(f)%dimensions(d)%name == TRIM( variable_name ) )  THEN
-
-                     IF ( .NOT. ALLOCATED( files(f)%dimensions(d)%attributes ) )  THEN
-                        !-- Initialize first attribute
-                        natts = 1
-                        ALLOCATE( files(f)%dimensions(d)%attributes(natts) )
-                     ELSE
-                        natts = SIZE( files(f)%dimensions(d)%attributes )
-
-                        !-- Check if attribute already exists
-                        DO  a = 1, natts
-                           IF ( files(f)%dimensions(d)%attributes(a)%name == attribute%name ) &
-                           THEN
-                              IF ( append )  THEN
-                                 !-- Append existing character attribute
-                                 files(f)%dimensions(d)%attributes(a)%value_char =             &
-                                    TRIM( files(f)%dimensions(d)%attributes(a)%value_char ) // &
-                                    TRIM( attribute%value_char )
-                              ELSE
-                                 !-- Update existing attribute
-                                 files(f)%dimensions(d)%attributes(a) = attribute
-                              ENDIF
-                              found = .TRUE.
-                              EXIT
-                           ENDIF
-                        ENDDO
-
-                        !-- Extend attribute list
-                        IF ( .NOT. found )  THEN
-                           ALLOCATE( atts_tmp(natts) )
-                           atts_tmp = files(f)%dimensions(d)%attributes
-                           DEALLOCATE( files(f)%dimensions(d)%attributes )
-                           natts = natts + 1
-                           ALLOCATE( files(f)%dimensions(d)%attributes(natts) )
-                           files(f)%dimensions(d)%attributes(:natts-1) = atts_tmp
-                           DEALLOCATE( atts_tmp )
-                        ENDIF
-                     ENDIF
-
-                     !-- Add new attribute to database
-                     IF ( .NOT. found )  THEN
-                        files(f)%dimensions(d)%attributes(natts) = attribute
-                        found = .TRUE.
-                     ENDIF
-
-                     EXIT
-
-                  ENDIF  ! dimension found
-
-               ENDDO  ! loop over dimensions
-
-            ENDIF  ! dimensions exist in file
-
-            !-- Add attribute to variable
-            IF ( .NOT. found  .AND.  ALLOCATED( files(f)%variables) )  THEN
-
-               DO  d = 1, SIZE( files(f)%variables )
-
-                  IF ( files(f)%variables(d)%name == TRIM( variable_name ) )  THEN
-
-                     IF ( .NOT. ALLOCATED( files(f)%variables(d)%attributes ) )  THEN
-                        !-- Initialize first attribute
-                        natts = 1
-                        ALLOCATE( files(f)%variables(d)%attributes(natts) )
-                     ELSE
-                        natts = SIZE( files(f)%variables(d)%attributes )
-
-                        !-- Check if attribute already exists
-                        DO  a = 1, natts
-                           IF ( files(f)%variables(d)%attributes(a)%name == attribute%name )  &
-                           THEN
-                              IF ( append )  THEN
-                                 !-- Append existing character attribute
-                                 files(f)%variables(d)%attributes(a)%value_char =             &
-                                    TRIM( files(f)%variables(d)%attributes(a)%value_char ) // &
-                                    TRIM( attribute%value_char )
-                              ELSE
-                                 !-- Update existing attribute
-                                 files(f)%variables(d)%attributes(a) = attribute
-                              ENDIF
-                              found = .TRUE.
-                              EXIT
-                           ENDIF
-                        ENDDO
-
-                        !-- Extend attribute list
-                        IF ( .NOT. found )  THEN
-                           ALLOCATE( atts_tmp(natts) )
-                           atts_tmp = files(f)%variables(d)%attributes
-                           DEALLOCATE( files(f)%variables(d)%attributes )
-                           natts = natts + 1
-                           ALLOCATE( files(f)%variables(d)%attributes(natts) )
-                           files(f)%variables(d)%attributes(:natts-1) = atts_tmp
-                           DEALLOCATE( atts_tmp )
-                        ENDIF
-
-                     ENDIF
-
-                     !-- Add new attribute to database
-                     IF ( .NOT. found )  THEN
-                        files(f)%variables(d)%attributes(natts) = attribute
-                        found = .TRUE.
-                     ENDIF
-
-                     EXIT
-
-                  ENDIF  ! variable found
-
-               ENDDO  ! loop over variables
-
-            ENDIF  ! variables exist in file
-
-            IF ( .NOT. found )  THEN
-               return_value = 1
-               CALL internal_message( 'error',                                        &
-                       routine_name //                                                &
-                       ': requested dimension/variable "' // TRIM( variable_name ) // &
-                       '" for attribute "' // TRIM( attribute%name ) //               &
-                       '" does not exist in file "' // TRIM( file_name ) // '"' )
-            ENDIF
-
-            EXIT
-
-         ENDIF  ! variable_name not empty
-
-      ENDIF  ! check file_name
-
-   ENDDO  ! loop over files
-
-   IF ( .NOT. found  .AND.  return_value == 0 )  THEN
-      return_value = 1
-      CALL internal_message( 'error',                                         &
-                             routine_name //                                  &
-                             ': requested file "' // TRIM( file_name ) //     &
-                             '" for attribute "' // TRIM( attribute%name ) // &
-                             '" does not exist' )
-   ENDIF
-
-END FUNCTION save_attribute_in_database
+ 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
+    LOGICAL, INTENT(IN) ::  append  !< if true, append value to existing value
+
+    TYPE(attribute_type), INTENT(IN) ::  attribute  !< new attribute
+
+    TYPE(attribute_type), DIMENSION(:), ALLOCATABLE ::  atts_tmp  !< temporary attribute list
+
+
+    return_value = 0
+    found = .FALSE.
+
+    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( file_name ) // &
+                     '" is already initialized. No further attribute definition allowed!' )
+             EXIT
+          ENDIF
+!
+!--       Add attribute to file
+          IF ( TRIM( variable_name ) == '' )  THEN
+!
+!--          Initialize first file attribute
+             IF ( .NOT. ALLOCATED( files(f)%attributes ) )  THEN
+                natts = 1
+                ALLOCATE( files(f)%attributes(natts) )
+             ELSE
+                natts = SIZE( files(f)%attributes )
+!
+!--             Check if attribute already exists
+                DO  a = 1, natts
+                   IF ( files(f)%attributes(a)%name == attribute%name )  THEN
+                      IF ( append )  THEN
+!
+!--                      Append existing string attribute
+                         files(f)%attributes(a)%value_char =             &
+                            TRIM( files(f)%attributes(a)%value_char ) // &
+                            TRIM( attribute%value_char )
+                      ELSE
+                         files(f)%attributes(a) = attribute
+                      ENDIF
+                      found = .TRUE.
+                      EXIT
+                   ENDIF
+                ENDDO
+!
+!--             Extend attribute list by 1
+                IF ( .NOT. found )  THEN
+                   ALLOCATE( atts_tmp(natts) )
+                   atts_tmp = files(f)%attributes
+                   DEALLOCATE( files(f)%attributes )
+                   natts = natts + 1
+                   ALLOCATE( files(f)%attributes(natts) )
+                   files(f)%attributes(:natts-1) = atts_tmp
+                   DEALLOCATE( atts_tmp )
+                ENDIF
+             ENDIF
+!
+!--          Save new attribute to the end of the attribute list
+             IF ( .NOT. found )  THEN
+                files(f)%attributes(natts) = attribute
+                found = .TRUE.
+             ENDIF
+
+             EXIT
+
+          ELSE
+!
+!--          Add attribute to dimension
+             IF ( ALLOCATED( files(f)%dimensions ) )  THEN
+
+                DO  d = 1, SIZE( files(f)%dimensions )
+
+                   IF ( files(f)%dimensions(d)%name == TRIM( variable_name ) )  THEN
+
+                      IF ( .NOT. ALLOCATED( files(f)%dimensions(d)%attributes ) )  THEN
+!
+!--                      Initialize first attribute
+                         natts = 1
+                         ALLOCATE( files(f)%dimensions(d)%attributes(natts) )
+                      ELSE
+                         natts = SIZE( files(f)%dimensions(d)%attributes )
+!
+!--                      Check if attribute already exists
+                         DO  a = 1, natts
+                            IF ( files(f)%dimensions(d)%attributes(a)%name == attribute%name ) &
+                            THEN
+                               IF ( append )  THEN
+!
+!--                               Append existing character attribute
+                                  files(f)%dimensions(d)%attributes(a)%value_char =             &
+                                     TRIM( files(f)%dimensions(d)%attributes(a)%value_char ) // &
+                                     TRIM( attribute%value_char )
+                               ELSE
+!
+!--                               Update existing attribute
+                                  files(f)%dimensions(d)%attributes(a) = attribute
+                               ENDIF
+                               found = .TRUE.
+                               EXIT
+                            ENDIF
+                         ENDDO
+!
+!--                      Extend attribute list
+                         IF ( .NOT. found )  THEN
+                            ALLOCATE( atts_tmp(natts) )
+                            atts_tmp = files(f)%dimensions(d)%attributes
+                            DEALLOCATE( files(f)%dimensions(d)%attributes )
+                            natts = natts + 1
+                            ALLOCATE( files(f)%dimensions(d)%attributes(natts) )
+                            files(f)%dimensions(d)%attributes(:natts-1) = atts_tmp
+                            DEALLOCATE( atts_tmp )
+                         ENDIF
+                      ENDIF
+!
+!--                   Add new attribute to database
+                      IF ( .NOT. found )  THEN
+                         files(f)%dimensions(d)%attributes(natts) = attribute
+                         found = .TRUE.
+                      ENDIF
+
+                      EXIT
+
+                   ENDIF  ! dimension found
+
+                ENDDO  ! loop over dimensions
+
+             ENDIF  ! dimensions exist in file
+!
+!--          Add attribute to variable
+             IF ( .NOT. found  .AND.  ALLOCATED( files(f)%variables) )  THEN
+
+                DO  d = 1, SIZE( files(f)%variables )
+
+                   IF ( files(f)%variables(d)%name == TRIM( variable_name ) )  THEN
+
+                      IF ( .NOT. ALLOCATED( files(f)%variables(d)%attributes ) )  THEN
+!
+!--                      Initialize first attribute
+                         natts = 1
+                         ALLOCATE( files(f)%variables(d)%attributes(natts) )
+                      ELSE
+                         natts = SIZE( files(f)%variables(d)%attributes )
+!
+!--                      Check if attribute already exists
+                         DO  a = 1, natts
+                            IF ( files(f)%variables(d)%attributes(a)%name == attribute%name )  &
+                            THEN
+                               IF ( append )  THEN
+!
+!--                               Append existing character attribute
+                                  files(f)%variables(d)%attributes(a)%value_char =             &
+                                     TRIM( files(f)%variables(d)%attributes(a)%value_char ) // &
+                                     TRIM( attribute%value_char )
+                               ELSE
+!
+!--                               Update existing attribute
+                                  files(f)%variables(d)%attributes(a) = attribute
+                               ENDIF
+                               found = .TRUE.
+                               EXIT
+                            ENDIF
+                         ENDDO
+!
+!--                      Extend attribute list
+                         IF ( .NOT. found )  THEN
+                            ALLOCATE( atts_tmp(natts) )
+                            atts_tmp = files(f)%variables(d)%attributes
+                            DEALLOCATE( files(f)%variables(d)%attributes )
+                            natts = natts + 1
+                            ALLOCATE( files(f)%variables(d)%attributes(natts) )
+                            files(f)%variables(d)%attributes(:natts-1) = atts_tmp
+                            DEALLOCATE( atts_tmp )
+                         ENDIF
+
+                      ENDIF
+!
+!--                   Add new attribute to database
+                      IF ( .NOT. found )  THEN
+                         files(f)%variables(d)%attributes(natts) = attribute
+                         found = .TRUE.
+                      ENDIF
+
+                      EXIT
+
+                   ENDIF  ! variable found
+
+                ENDDO  ! loop over variables
+
+             ENDIF  ! variables exist in file
+
+             IF ( .NOT. found )  THEN
+                return_value = 1
+                CALL internal_message( 'error',                                        &
+                        routine_name //                                                &
+                        ': requested dimension/variable "' // TRIM( variable_name ) // &
+                        '" for attribute "' // TRIM( attribute%name ) //               &
+                        '" does not exist in file "' // TRIM( file_name ) // '"' )
+             ENDIF
+
+             EXIT
+
+          ENDIF  ! variable_name not empty
+
+       ENDIF  ! check file_name
+
+    ENDDO  ! loop over files
+
+    IF ( .NOT. found  .AND.  return_value == 0 )  THEN
+       return_value = 1
+       CALL internal_message( 'error',                                         &
+                              routine_name //                                  &
+                              ': requested file "' // TRIM( file_name ) //     &
+                              '" for attribute "' // TRIM( attribute%name ) // &
+                              '" does not exist' )
+    ENDIF
+
+ END FUNCTION save_attribute_in_database
 
 !--------------------------------------------------------------------------------------------------!
@@ -2738,95 +2765,95 @@
 !> without variables).
 !--------------------------------------------------------------------------------------------------!
-FUNCTION cleanup_database() RESULT( return_value )
-
-   ! CHARACTER(LEN=*), PARAMETER ::  routine_name = 'cleanup_database'  !< name of routine
-
-   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
-
-   TYPE(file_type), DIMENSION(:), ALLOCATABLE ::  used_files  !< list of used files
-
-
-   return_value = 0
-
-   !-- Flag files which contain output variables as used
-   file_is_used(:) = .FALSE.
-   DO  f = 1, nfiles
-      IF ( ALLOCATED( files(f)%variables ) )  THEN
-         file_is_used(f) = .TRUE.
-      ENDIF
-   ENDDO
-
-   !-- Copy flagged files into temporary list
-   nfiles_used = COUNT( file_is_used )
-   ALLOCATE( used_files(nfiles_used) )
-   i = 0
-   DO  f = 1, nfiles
-      IF ( file_is_used(f) )  THEN
-         i = i + 1
-         used_files(i) = files(f)
-      ENDIF
-   ENDDO
-
-   !-- Replace file list with list of used files
-   DEALLOCATE( files )
-   nfiles = nfiles_used
-   ALLOCATE( files(nfiles) )
-   files = used_files
-   DEALLOCATE( used_files )
-
-   !-- Check every file for unused dimensions
-   DO  f = 1, nfiles
-
-      !-- If a file is already initialized, it was already checked previously
-      IF ( files(f)%is_init )  CYCLE
-
-      !-- Get number of defined dimensions
-      ndims = SIZE( files(f)%dimensions )
-      ALLOCATE( dimension_is_used(ndims) )
-
-      !-- Go through all variables and flag all used dimensions
-      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 )
-            IF ( dimension_is_used(d) )  EXIT
-         ENDDO
-      ENDDO
-
-      !-- Copy used dimensions to temporary list
-      ndims_used = COUNT( dimension_is_used )
-      ALLOCATE( used_dimensions(ndims_used) )
-      i = 0
-      DO  d = 1, ndims
-         IF ( dimension_is_used(d) )  THEN
-            i = i + 1
-            used_dimensions(i) = files(f)%dimensions(d)
-         ENDIF
-      ENDDO
-
-      !-- Replace dimension list with list of used dimensions
-      DEALLOCATE( files(f)%dimensions )
-      ndims = ndims_used
-      ALLOCATE( files(f)%dimensions(ndims) )
-      files(f)%dimensions = used_dimensions
-      DEALLOCATE( used_dimensions )
-      DEALLOCATE( dimension_is_used )
-
-   ENDDO
-
-END FUNCTION cleanup_database
+ FUNCTION cleanup_database() RESULT( return_value )
+
+    ! CHARACTER(LEN=*), PARAMETER ::  routine_name = 'cleanup_database'  !< name of routine
+
+    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
+
+    TYPE(file_type), DIMENSION(:), ALLOCATABLE ::  used_files  !< list of used files
+
+
+    return_value = 0
+!
+!-- Flag files which contain output variables as used
+    file_is_used(:) = .FALSE.
+    DO  f = 1, nfiles
+       IF ( ALLOCATED( files(f)%variables ) )  THEN
+          file_is_used(f) = .TRUE.
+       ENDIF
+    ENDDO
+!
+!-- Copy flagged files into temporary list
+    nfiles_used = COUNT( file_is_used )
+    ALLOCATE( used_files(nfiles_used) )
+    i = 0
+    DO  f = 1, nfiles
+       IF ( file_is_used(f) )  THEN
+          i = i + 1
+          used_files(i) = files(f)
+       ENDIF
+    ENDDO
+!
+!-- Replace file list with list of used files
+    DEALLOCATE( files )
+    nfiles = nfiles_used
+    ALLOCATE( files(nfiles) )
+    files = used_files
+    DEALLOCATE( used_files )
+!
+!-- Check every file for unused dimensions
+    DO  f = 1, nfiles
+!
+!--    If a file is already initialized, it was already checked previously
+       IF ( files(f)%is_init )  CYCLE
+!
+!--    Get number of defined dimensions
+       ndims = SIZE( files(f)%dimensions )
+       ALLOCATE( dimension_is_used(ndims) )
+!
+!--    Go through all variables and flag all used dimensions
+       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 )
+             IF ( dimension_is_used(d) )  EXIT
+          ENDDO
+       ENDDO
+!
+!--    Copy used dimensions to temporary list
+       ndims_used = COUNT( dimension_is_used )
+       ALLOCATE( used_dimensions(ndims_used) )
+       i = 0
+       DO  d = 1, ndims
+          IF ( dimension_is_used(d) )  THEN
+             i = i + 1
+             used_dimensions(i) = files(f)%dimensions(d)
+          ENDIF
+       ENDDO
+!
+!--    Replace dimension list with list of used dimensions
+       DEALLOCATE( files(f)%dimensions )
+       ndims = ndims_used
+       ALLOCATE( files(f)%dimensions(ndims) )
+       files(f)%dimensions = used_dimensions
+       DEALLOCATE( used_dimensions )
+       DEALLOCATE( dimension_is_used )
+
+    ENDDO
+
+ END FUNCTION cleanup_database
 
 !--------------------------------------------------------------------------------------------------!
@@ -2835,47 +2862,47 @@
 !> Open requested output file.
 !--------------------------------------------------------------------------------------------------!
-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) ::  file_name    !< name of file to be checked
-
-   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'open_output_file'  !< name of routine
-
-   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
-
-
-   return_value = 0
-   output_return_value = 0
-
-   SELECT CASE ( TRIM( file_format ) )
-
-      CASE ( 'binary' )
-         CALL binary_open_file( 'binary', file_name, file_id, output_return_value )
-
-      CASE ( 'netcdf4-serial' )
-         CALL netcdf4_open_file( 'serial', file_name, file_id, output_return_value )
-
-      CASE ( 'netcdf4-parallel' )
-         CALL netcdf4_open_file( 'parallel', file_name, file_id, output_return_value )
-
-      CASE DEFAULT
-         return_value = 1
-
-   END SELECT
-
-   IF ( output_return_value /= 0 )  THEN
-      return_value = output_return_value
-      CALL internal_message( 'error', routine_name // &
-                             ': error while opening file "' // TRIM( file_name ) // '"' )
-   ELSEIF ( return_value /= 0 )  THEN
-      CALL internal_message( 'error', routine_name //                     &
-                             ': file "' // TRIM( file_name ) //           &
-                             '": file format "' // TRIM( file_format ) // &
-                             '" not supported' )
-   ENDIF
-
-END SUBROUTINE open_output_file
+ 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) ::  file_name    !< name of file to be checked
+
+    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'open_output_file'  !< name of routine
+
+    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
+
+
+    return_value = 0
+    output_return_value = 0
+
+    SELECT CASE ( TRIM( file_format ) )
+
+       CASE ( 'binary' )
+          CALL binary_open_file( 'binary', file_name, file_id, output_return_value )
+
+       CASE ( 'netcdf4-serial' )
+          CALL netcdf4_open_file( 'serial', file_name, file_id, output_return_value )
+
+       CASE ( 'netcdf4-parallel' )
+          CALL netcdf4_open_file( 'parallel', file_name, file_id, output_return_value )
+
+       CASE DEFAULT
+          return_value = 1
+
+    END SELECT
+
+    IF ( output_return_value /= 0 )  THEN
+       return_value = output_return_value
+       CALL internal_message( 'error', routine_name // &
+                              ': error while opening file "' // TRIM( file_name ) // '"' )
+    ELSEIF ( return_value /= 0 )  THEN
+       CALL internal_message( 'error', routine_name //                     &
+                              ': file "' // TRIM( file_name ) //           &
+                              '": file format "' // TRIM( file_format ) // &
+                              '" not supported' )
+    ENDIF
+
+ END SUBROUTINE open_output_file
 
 !--------------------------------------------------------------------------------------------------!
@@ -2884,98 +2911,100 @@
 !> 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
-
-
-   return_value  = 0
-
-   !-- Write file attributes
-   IF ( ALLOCATED( file%attributes ) )  THEN
-      DO  a = 1, SIZE( file%attributes )
-         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
-      ENDDO
-   ENDIF
-
-   IF ( return_value == 0 )  THEN
-
-      !-- Initialize file dimensions
-      DO  d = 1, SIZE( file%dimensions )
-
-         IF ( .NOT. file%dimensions(d)%is_masked )  THEN
-
-            !-- Initialize non-masked dimension
-            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)%name,               &
-                    file%dimensions(d)%data_type, file%dimensions(d)%length_mask, &
-                    file%dimensions(d)%variable_id, return_value )
-
-         ENDIF
-
-         IF ( return_value == 0  .AND.  ALLOCATED( file%dimensions(d)%attributes ) )  THEN
-            !-- Write dimension attributes
-            DO  a = 1, SIZE( file%dimensions(d)%attributes )
-               return_value = write_attribute( file%format, file%id, file%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
-            ENDDO
-         ENDIF
-
-         IF ( return_value /= 0 )  EXIT
-
-      ENDDO
-
-      !-- Save dimension IDs for variables wihtin database
-      IF ( return_value == 0 )  &
-         CALL collect_dimesion_ids_for_variables( file%name, file%variables, file%dimensions, &
-                                                  return_value )
-
-      !-- Initialize file variables
-      IF ( return_value == 0 )  THEN
-         DO  d = 1, SIZE( file%variables )
-
-            CALL init_file_variable( file%format, file%id, file%name,                          &
-                    file%variables(d)%id, file%variables(d)%name, file%variables(d)%data_type, &
-                    file%variables(d)%dimension_ids,                                           &
-                    file%variables(d)%is_global, return_value )
-
-            IF ( return_value == 0  .AND.  ALLOCATED( file%variables(d)%attributes ) )  THEN
-               !-- Write variable attributes
-               DO  a = 1, SIZE( file%variables(d)%attributes )
-                  return_value = write_attribute( file%format, file%id, file%name, &
-                                    variable_id=file%variables(d)%id,              &
-                                    variable_name=file%variables(d)%name,          &
-                                    attribute=file%variables(d)%attributes(a) )
-                  IF ( return_value /= 0 )  EXIT
-               ENDDO
-            ENDIF
-
-            IF ( return_value /= 0 )  EXIT
-
-         ENDDO
-      ENDIF
-
-   ENDIF
-
-END SUBROUTINE init_file_header
+ 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
+
+
+    return_value  = 0
+!
+!-- Write file attributes
+    IF ( ALLOCATED( file%attributes ) )  THEN
+       DO  a = 1, SIZE( file%attributes )
+          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
+       ENDDO
+    ENDIF
+
+    IF ( return_value == 0 )  THEN
+!
+!--    Initialize file dimensions
+       DO  d = 1, SIZE( file%dimensions )
+
+          IF ( .NOT. file%dimensions(d)%is_masked )  THEN
+!
+!--          Initialize non-masked dimension
+             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)%name,               &
+                     file%dimensions(d)%data_type, file%dimensions(d)%length_mask, &
+                     file%dimensions(d)%variable_id, return_value )
+
+          ENDIF
+
+          IF ( return_value == 0  .AND.  ALLOCATED( file%dimensions(d)%attributes ) )  THEN
+!
+!--          Write dimension attributes
+             DO  a = 1, SIZE( file%dimensions(d)%attributes )
+                return_value = write_attribute( file%format, file%id, file%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
+             ENDDO
+          ENDIF
+
+          IF ( return_value /= 0 )  EXIT
+
+       ENDDO
+!
+!--    Save dimension IDs for variables wihtin database
+       IF ( return_value == 0 )  &
+          CALL collect_dimesion_ids_for_variables( file%name, file%variables, file%dimensions, &
+                                                   return_value )
+!
+!--    Initialize file variables
+       IF ( return_value == 0 )  THEN
+          DO  d = 1, SIZE( file%variables )
+
+             CALL init_file_variable( file%format, file%id, file%name,                          &
+                     file%variables(d)%id, file%variables(d)%name, file%variables(d)%data_type, &
+                     file%variables(d)%dimension_ids,                                           &
+                     file%variables(d)%is_global, return_value )
+
+             IF ( return_value == 0  .AND.  ALLOCATED( file%variables(d)%attributes ) )  THEN
+!
+!--             Write variable attributes
+                DO  a = 1, SIZE( file%variables(d)%attributes )
+                   return_value = write_attribute( file%format, file%id, file%name, &
+                                     variable_id=file%variables(d)%id,              &
+                                     variable_name=file%variables(d)%name,          &
+                                     attribute=file%variables(d)%attributes(a) )
+                   IF ( return_value /= 0 )  EXIT
+                ENDDO
+             ENDIF
+
+             IF ( return_value /= 0 )  EXIT
+
+          ENDDO
+       ENDIF
+
+    ENDIF
+
+ END SUBROUTINE init_file_header
 
 !--------------------------------------------------------------------------------------------------!
@@ -2984,61 +3013,61 @@
 !> 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
+ 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
 
 !--------------------------------------------------------------------------------------------------!
@@ -3047,60 +3076,60 @@
 !> 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
+ 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
 
 !--------------------------------------------------------------------------------------------------!
@@ -3109,135 +3138,135 @@
 !> Write attribute to file.
 !--------------------------------------------------------------------------------------------------!
-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, 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
-
-
-   return_value = 0
-   output_return_value = 0
-
-   !-- Prepare for possible error message
-   temp_string = '(file "' // TRIM( file_name ) //           &
-                 '", variable "' // TRIM( variable_name ) // &
-                 '", attribute "' // TRIM( attribute%name ) // '")'
-
-   !-- Write attribute to file
-   SELECT CASE ( TRIM( file_format ) )
-
-      CASE ( 'binary' )
-
-         SELECT CASE ( TRIM( attribute%data_type ) )
-
-            CASE( '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, 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, 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, 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, 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, variable_id=variable_id,      &
-                       attribute_name=attribute%name, value_real64=attribute%value_real64, &
-                       return_value=output_return_value )
-
-            CASE DEFAULT
-               return_value = 1
-               CALL internal_message( 'error', routine_name //                     &
-                                      ': file format "' // TRIM( file_format ) //  &
-                                      '" does not support attribute data type "'// &
-                                      TRIM( attribute%data_type ) //               &
-                                      '" ' // TRIM( temp_string ) )
-
-         END SELECT
-
-      CASE ( 'netcdf4-parallel', 'netcdf4-serial' )
-
-         SELECT CASE ( TRIM( attribute%data_type ) )
-
-            CASE( '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, 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, 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, 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, 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, variable_id=variable_id,     &
-                       attribute_name=attribute%name, value_real64=attribute%value_real64, &
-                       return_value=output_return_value )
-
-            CASE DEFAULT
-               return_value = 1
-               CALL internal_message( 'error', routine_name //                     &
-                                      ': file format "' // TRIM( file_format ) //  &
-                                      '" does not support attribute data type "'// &
-                                      TRIM( attribute%data_type ) //               &
-                                      '" ' // TRIM( temp_string ) )
-
-         END SELECT
-
-      CASE DEFAULT
-         return_value = 1
-         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
+ 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, 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
+
+
+    return_value = 0
+    output_return_value = 0
+!
+!-- Prepare for possible error message
+    temp_string = '(file "' // TRIM( file_name ) //           &
+                  '", variable "' // TRIM( variable_name ) // &
+                  '", attribute "' // TRIM( attribute%name ) // '")'
+!
+!-- Write attribute to file
+    SELECT CASE ( TRIM( file_format ) )
+
+       CASE ( 'binary' )
+
+          SELECT CASE ( TRIM( attribute%data_type ) )
+
+             CASE( '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, 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, 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, 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, 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, variable_id=variable_id,      &
+                        attribute_name=attribute%name, value_real64=attribute%value_real64, &
+                        return_value=output_return_value )
+
+             CASE DEFAULT
+                return_value = 1
+                CALL internal_message( 'error', routine_name //                     &
+                                       ': file format "' // TRIM( file_format ) //  &
+                                       '" does not support attribute data type "'// &
+                                       TRIM( attribute%data_type ) //               &
+                                       '" ' // TRIM( temp_string ) )
+
+          END SELECT
+
+       CASE ( 'netcdf4-parallel', 'netcdf4-serial' )
+
+          SELECT CASE ( TRIM( attribute%data_type ) )
+
+             CASE( '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, 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, 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, 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, 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, variable_id=variable_id,     &
+                        attribute_name=attribute%name, value_real64=attribute%value_real64, &
+                        return_value=output_return_value )
+
+             CASE DEFAULT
+                return_value = 1
+                CALL internal_message( 'error', routine_name //                     &
+                                       ': file format "' // TRIM( file_format ) //  &
+                                       '" does not support attribute data type "'// &
+                                       TRIM( attribute%data_type ) //               &
+                                       '" ' // TRIM( temp_string ) )
+
+          END SELECT
+
+       CASE DEFAULT
+          return_value = 1
+          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
 
 !--------------------------------------------------------------------------------------------------!
@@ -3246,51 +3275,51 @@
 !> Get dimension IDs and save them to variables.
 !--------------------------------------------------------------------------------------------------!
-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              ::  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
-
-   TYPE(dimension_type), DIMENSION(:), INTENT(IN) ::  dimensions  !< list of dimensions in file
-
-   TYPE(variable_type), DIMENSION(:), INTENT(INOUT) ::  variables  !< list of variables in file
-
-
-   return_value  = 0
-   ndims = SIZE( dimensions )
-   nvars = SIZE( variables )
-
-   DO  i = 1, nvars
-      DO  j = 1, SIZE( variables(i)%dimension_names )
-         found = .FALSE.
-         DO  d = 1, ndims
-            IF ( variables(i)%dimension_names(j) == dimensions(d)%name )  THEN
-               variables(i)%dimension_ids(j) = dimensions(d)%id
-               found = .TRUE.
-               EXIT
-            ENDIF
-         ENDDO
-         IF ( .NOT. found )  THEN
-            return_value = 1
-            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
-      ENDDO
-      IF ( .NOT. found )  EXIT
-   ENDDO
-
-END SUBROUTINE collect_dimesion_ids_for_variables
+ 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              ::  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
+
+    TYPE(dimension_type), DIMENSION(:), INTENT(IN) ::  dimensions  !< list of dimensions in file
+
+    TYPE(variable_type), DIMENSION(:), INTENT(INOUT) ::  variables  !< list of variables in file
+
+
+    return_value  = 0
+    ndims = SIZE( dimensions )
+    nvars = SIZE( variables )
+
+    DO  i = 1, nvars
+       DO  j = 1, SIZE( variables(i)%dimension_names )
+          found = .FALSE.
+          DO  d = 1, ndims
+             IF ( variables(i)%dimension_names(j) == dimensions(d)%name )  THEN
+                variables(i)%dimension_ids(j) = dimensions(d)%id
+                found = .TRUE.
+                EXIT
+             ENDIF
+          ENDDO
+          IF ( .NOT. found )  THEN
+             return_value = 1
+             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
+       ENDDO
+       IF ( .NOT. found )  EXIT
+    ENDDO
+
+ END SUBROUTINE collect_dimesion_ids_for_variables
 
 !--------------------------------------------------------------------------------------------------!
@@ -3301,47 +3330,47 @@
 !> @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=*), 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
-
-
-   return_value = 0
-   output_return_value = 0
-
-   temp_string = '(file "' // TRIM( file_name ) // '")'
-
-   SELECT CASE ( TRIM( file_format ) )
-
-      CASE ( 'binary' )
-         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
-         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 leaving file-definition state ' // &
-                             TRIM( temp_string ) )
-   ENDIF
-
-   ! CALL MPI_Barrier( MPI_COMM_WORLD, return_value )
-
-END SUBROUTINE stop_file_header_definition
+ 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=*), 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
+
+
+    return_value = 0
+    output_return_value = 0
+
+    temp_string = '(file "' // TRIM( file_name ) // '")'
+
+    SELECT CASE ( TRIM( file_format ) )
+
+       CASE ( 'binary' )
+          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
+          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 leaving file-definition state ' // &
+                              TRIM( temp_string ) )
+    ENDIF
+
+    ! CALL MPI_Barrier( MPI_COMM_WORLD, return_value )
+
+ END SUBROUTINE stop_file_header_definition
 
 !--------------------------------------------------------------------------------------------------!
@@ -3350,121 +3379,121 @@
 !> 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)  ::  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              ::  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
-   LOGICAL, INTENT(OUT) ::  is_global  !< true if variable is global
-
-   TYPE(dimension_type), DIMENSION(:), ALLOCATABLE, INTENT(OUT) ::  dimensions  !< list of dimensions used by variable
-
-
-   return_value = 0
-   found = .FALSE.
-
-   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 not initialized. ' //                &
-                                   'Writing variable to file is impossible ' // &
-                                   '(variable "' // TRIM( variable_name ) //    &
-                                   '", file "' // TRIM( file_name ) // '")!' )
-            EXIT
-         ENDIF
-
-         file_id     = files(f)%id
-         file_format = files(f)%format
-
-         !-- Search for variable in file
-         DO  d = 1, SIZE( files(f)%variables )
-            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( dimension_ids(SIZE( files(f)%variables(d)%dimension_ids )) )
-               ALLOCATE( dimensions(SIZE( files(f)%variables(d)%dimension_ids )) )
-
-               dimension_ids = files(f)%variables(d)%dimension_ids
-
-               found = .TRUE.
-               EXIT
-
-            ENDIF
-         ENDDO
-
-         IF ( found )  THEN
-
-            !-- Get list of dimensions used by variable
-            DO  d = 1, SIZE( files(f)%dimensions )
-               DO  dd = 1, SIZE( dimension_ids )
-                  IF ( dimension_ids(dd) == files(f)%dimensions(d)%id )  THEN
-                     dimensions(dd) = files(f)%dimensions(d)
-                     EXIT
-                  ENDIF
-               ENDDO
-            ENDDO
-
-         ELSE
-
-            !-- If variable was not found, search for a dimension instead
-            DO  d = 1, SIZE( files(f)%dimensions )
-               IF ( TRIM( variable_name ) == TRIM( files(f)%dimensions(d)%name ) )  THEN
-
-                  variable_id    = files(f)%dimensions(d)%variable_id
-                  is_global = .TRUE.
-
-                  ALLOCATE( dimensions(1) )
-
-                  dimensions(1) = files(f)%dimensions(d)
-
-                  found = .TRUE.
-                  EXIT
-
-               ENDIF
-            ENDDO
-
-         ENDIF
-
-         !-- If variable was not found in requested file, return an error
-         IF ( .NOT. found )  THEN
-            return_value = 1
-            CALL internal_message( 'error', routine_name //                  &
-                                   ': variable not found in file ' //        &
-                                   '(variable "' // TRIM( variable_name ) // &
-                                   '", file "' // TRIM( file_name ) // '")!' )
-         ENDIF
-
-         EXIT
-
-      ENDIF  ! file found
-   ENDDO  ! loop over files
-
-   IF ( .NOT. found  .AND.  return_value == 0 )  THEN
-      return_value = 1
-      CALL internal_message( 'error', routine_name //                  &
-                             ': file not found ' //                    &
-                             '(variable "' // TRIM( variable_name ) // &
-                             '", file "' // TRIM( file_name ) // '")!' )
-   ENDIF
-
-END SUBROUTINE find_var_in_file
+ 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)  ::  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              ::  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
+    LOGICAL, INTENT(OUT) ::  is_global  !< true if variable is global
+
+    TYPE(dimension_type), DIMENSION(:), ALLOCATABLE, INTENT(OUT) ::  dimensions  !< list of dimensions used by variable
+
+
+    return_value = 0
+    found = .FALSE.
+
+    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 not initialized. ' //                &
+                                    'Writing variable to file is impossible ' // &
+                                    '(variable "' // TRIM( variable_name ) //    &
+                                    '", file "' // TRIM( file_name ) // '")!' )
+             EXIT
+          ENDIF
+
+          file_id     = files(f)%id
+          file_format = files(f)%format
+!
+!--       Search for variable in file
+          DO  d = 1, SIZE( files(f)%variables )
+             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( dimension_ids(SIZE( files(f)%variables(d)%dimension_ids )) )
+                ALLOCATE( dimensions(SIZE( files(f)%variables(d)%dimension_ids )) )
+
+                dimension_ids = files(f)%variables(d)%dimension_ids
+
+                found = .TRUE.
+                EXIT
+
+             ENDIF
+          ENDDO
+
+          IF ( found )  THEN
+!
+!--          Get list of dimensions used by variable
+             DO  d = 1, SIZE( files(f)%dimensions )
+                DO  dd = 1, SIZE( dimension_ids )
+                   IF ( dimension_ids(dd) == files(f)%dimensions(d)%id )  THEN
+                      dimensions(dd) = files(f)%dimensions(d)
+                      EXIT
+                   ENDIF
+                ENDDO
+             ENDDO
+
+          ELSE
+!
+!--          If variable was not found, search for a dimension instead
+             DO  d = 1, SIZE( files(f)%dimensions )
+                IF ( TRIM( variable_name ) == TRIM( files(f)%dimensions(d)%name ) )  THEN
+
+                   variable_id    = files(f)%dimensions(d)%variable_id
+                   is_global = .TRUE.
+
+                   ALLOCATE( dimensions(1) )
+
+                   dimensions(1) = files(f)%dimensions(d)
+
+                   found = .TRUE.
+                   EXIT
+
+                ENDIF
+             ENDDO
+
+          ENDIF
+!
+!--       If variable was not found in requested file, return an error
+          IF ( .NOT. found )  THEN
+             return_value = 1
+             CALL internal_message( 'error', routine_name //                  &
+                                    ': variable not found in file ' //        &
+                                    '(variable "' // TRIM( variable_name ) // &
+                                    '", file "' // TRIM( file_name ) // '")!' )
+          ENDIF
+
+          EXIT
+
+       ENDIF  ! file found
+    ENDDO  ! loop over files
+
+    IF ( .NOT. found  .AND.  return_value == 0 )  THEN
+       return_value = 1
+       CALL internal_message( 'error', routine_name //                  &
+                              ': file not found ' //                    &
+                              '(variable "' // TRIM( variable_name ) // &
+                              '", file "' // TRIM( file_name ) // '")!' )
+    ENDIF
+
+ END SUBROUTINE find_var_in_file
 
 !--------------------------------------------------------------------------------------------------!
@@ -3478,80 +3507,80 @@
 !> starts and origins are set to zero for all dimensions.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE get_masked_indices_and_masked_dimension_bounds(                             &
-              dimensions, bounds_start, bounds_end, bounds_masked_start, value_counts, &
-              bounds_origin, masked_indices )
-
-   ! CHARACTER(LEN=*), PARAMETER ::  routine_name = 'get_masked_indices_and_masked_dimension_bounds'  !< name of routine
-
-   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
-
-
-   ALLOCATE( masked_indices(SIZE( dimensions ),0:MAXVAL( bounds_end - bounds_start + 1 )) )
-   masked_indices = -HUGE( 0 )
-
-   !-- Check for masking and update lower and upper bounds if masked
-   DO  d = 1, SIZE( dimensions )
-
-      IF ( dimensions(d)%is_masked )  THEN
-
-         bounds_origin(d) = 0
-
-         bounds_masked_start(d) = -HUGE( 0 )
-
-         !-- Find number of masked values within given variable bounds
-         value_counts(d) = 0
-         DO  i = LBOUND( dimensions(d)%masked_indices, DIM=1 ), &
-                 UBOUND( dimensions(d)%masked_indices, DIM=1 )
-
-            !-- Is masked index within given bounds?
-            IF ( dimensions(d)%masked_indices(i) >= bounds_start(d)  .AND.  &
-                 dimensions(d)%masked_indices(i) <= bounds_end(d)           )  THEN
-
-               !-- Save masked index
-               masked_indices(d,value_counts(d)) = dimensions(d)%masked_indices(i)
-               value_counts(d) = value_counts(d) + 1
-
-               !-- Save bounds of mask within given bounds
-               IF ( bounds_masked_start(d) == -HUGE( 0 ) )  bounds_masked_start(d) = i
-
-            ENDIF
-
-         ENDDO
-
-         !-- Set masked bounds to zero if no masked index lies within bounds
-         IF ( value_counts(d) == 0 )  THEN
-            bounds_origin(:) = 0
-            bounds_masked_start(:) = 0
-            value_counts(:) = 0
-            EXIT
-         ENDIF
-
-      ELSE
-
-         !-- If dimension is not masked, save all indices within bounds for output
-         bounds_origin(d) = dimensions(d)%bounds(1)
-         bounds_masked_start(d) = bounds_start(d)
-         value_counts(d) = bounds_end(d) - bounds_start(d) + 1
-
-         DO  i = 0, value_counts(d) - 1
-            masked_indices(d,i) = bounds_start(d) + i
-         ENDDO
-
-      ENDIF
-
-   ENDDO
-
-END SUBROUTINE get_masked_indices_and_masked_dimension_bounds
+ SUBROUTINE get_masked_indices_and_masked_dimension_bounds(                             &
+               dimensions, bounds_start, bounds_end, bounds_masked_start, value_counts, &
+               bounds_origin, masked_indices )
+
+    ! CHARACTER(LEN=*), PARAMETER ::  routine_name = 'get_masked_indices_and_masked_dimension_bounds'  !< name of routine
+
+    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
+
+
+    ALLOCATE( masked_indices(SIZE( dimensions ),0:MAXVAL( bounds_end - bounds_start + 1 )) )
+    masked_indices = -HUGE( 0 )
+!
+!-- Check for masking and update lower and upper bounds if masked
+    DO  d = 1, SIZE( dimensions )
+
+       IF ( dimensions(d)%is_masked )  THEN
+
+          bounds_origin(d) = 0
+
+          bounds_masked_start(d) = -HUGE( 0 )
+!
+!--       Find number of masked values within given variable bounds
+          value_counts(d) = 0
+          DO  i = LBOUND( dimensions(d)%masked_indices, DIM=1 ), &
+                  UBOUND( dimensions(d)%masked_indices, DIM=1 )
+!
+!--          Is masked index within given bounds?
+             IF ( dimensions(d)%masked_indices(i) >= bounds_start(d)  .AND.  &
+                  dimensions(d)%masked_indices(i) <= bounds_end(d)           )  THEN
+!
+!--             Save masked index
+                masked_indices(d,value_counts(d)) = dimensions(d)%masked_indices(i)
+                value_counts(d) = value_counts(d) + 1
+!
+!--             Save bounds of mask within given bounds
+                IF ( bounds_masked_start(d) == -HUGE( 0 ) )  bounds_masked_start(d) = i
+
+             ENDIF
+
+          ENDDO
+!
+!--       Set masked bounds to zero if no masked index lies within bounds
+          IF ( value_counts(d) == 0 )  THEN
+             bounds_origin(:) = 0
+             bounds_masked_start(:) = 0
+             value_counts(:) = 0
+             EXIT
+          ENDIF
+
+       ELSE
+!
+!--       If dimension is not masked, save all indices within bounds for output
+          bounds_origin(d) = dimensions(d)%bounds(1)
+          bounds_masked_start(d) = bounds_start(d)
+          value_counts(d) = bounds_end(d) - bounds_start(d) + 1
+
+          DO  i = 0, value_counts(d) - 1
+             masked_indices(d,i) = bounds_start(d) + i
+          ENDDO
+
+       ENDIF
+
+    ENDDO
+
+ END SUBROUTINE get_masked_indices_and_masked_dimension_bounds
 
 !--------------------------------------------------------------------------------------------------!
@@ -3561,22 +3590,22 @@
 !> 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
+ 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
 
 !--------------------------------------------------------------------------------------------------!
@@ -3586,402 +3615,402 @@
 !> 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
-
-   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
+ 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
+
+    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 4146)
+++ palm/trunk/SOURCE/data_output_netcdf4_module.f90	(revision 4147)
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! corrected indentation according to coding standard
+!
+! 4141 2019-08-05 12:24:51Z gronemeier
 ! Initial revision
 !
@@ -37,89 +40,89 @@
 !> This is either done in parallel mode via parallel NetCDF4 I/O or in serial mode only by PE0.
 !--------------------------------------------------------------------------------------------------!
-MODULE data_output_netcdf4_module
-
-   USE kinds
+ MODULE data_output_netcdf4_module
+
+    USE kinds
 
 #if defined( __parallel )
 #if defined( __mpifh )
-   INCLUDE "mpif.h"
-#else
-   USE MPI
-#endif
-#endif
-
-#if defined( __netcdf4 )
-   USE NETCDF
-#endif
-
-   IMPLICIT NONE
-
-   CHARACTER(LEN=800) ::  internal_error_message = ''  !< string containing the last error message
-   CHARACTER(LEN=100) ::  file_suffix = ''             !< file suffix added to each file name
-   CHARACTER(LEN=800) ::  temp_string                  !< dummy string
-
-   CHARACTER(LEN=*), PARAMETER ::  mode_parallel = 'parallel'  !< string selecting netcdf4 parallel mode
-   CHARACTER(LEN=*), PARAMETER ::  mode_serial   = 'serial'    !< string selecting netcdf4 serial mode
-
-   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
-
-   SAVE
-
-   PRIVATE
-
-   INTERFACE netcdf4_init_module
-      MODULE PROCEDURE netcdf4_init_module
-   END INTERFACE netcdf4_init_module
-
-   INTERFACE netcdf4_open_file
-      MODULE PROCEDURE netcdf4_open_file
-   END INTERFACE netcdf4_open_file
-
-   INTERFACE netcdf4_init_dimension
-      MODULE PROCEDURE netcdf4_init_dimension
-   END INTERFACE netcdf4_init_dimension
-
-   INTERFACE netcdf4_init_variable
-      MODULE PROCEDURE netcdf4_init_variable
-   END INTERFACE netcdf4_init_variable
-
-   INTERFACE netcdf4_write_attribute
-      MODULE PROCEDURE netcdf4_write_attribute
-   END INTERFACE netcdf4_write_attribute
-
-   INTERFACE netcdf4_stop_file_header_definition
-      MODULE PROCEDURE netcdf4_stop_file_header_definition
-   END INTERFACE netcdf4_stop_file_header_definition
-
-   INTERFACE netcdf4_write_variable
-      MODULE PROCEDURE netcdf4_write_variable
-   END INTERFACE netcdf4_write_variable
-
-   INTERFACE netcdf4_finalize
-      MODULE PROCEDURE netcdf4_finalize
-   END INTERFACE netcdf4_finalize
-
-   INTERFACE netcdf4_get_error_message
-      MODULE PROCEDURE netcdf4_get_error_message
-   END INTERFACE netcdf4_get_error_message
-
-   PUBLIC &
-      netcdf4_finalize, &
-      netcdf4_get_error_message, &
-      netcdf4_init_dimension, &
-      netcdf4_stop_file_header_definition, &
-      netcdf4_init_module, &
-      netcdf4_init_variable, &
-      netcdf4_open_file, &
-      netcdf4_write_attribute, &
-      netcdf4_write_variable
-
-
-CONTAINS
+    INCLUDE "mpif.h"
+#else
+    USE MPI
+#endif
+#endif
+
+#if defined( __netcdf4 )
+    USE NETCDF
+#endif
+
+    IMPLICIT NONE
+
+    CHARACTER(LEN=800) ::  internal_error_message = ''  !< string containing the last error message
+    CHARACTER(LEN=100) ::  file_suffix = ''             !< file suffix added to each file name
+    CHARACTER(LEN=800) ::  temp_string                  !< dummy string
+
+    CHARACTER(LEN=*), PARAMETER ::  mode_parallel = 'parallel'  !< string selecting netcdf4 parallel mode
+    CHARACTER(LEN=*), PARAMETER ::  mode_serial   = 'serial'    !< string selecting netcdf4 serial mode
+
+    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
+
+    SAVE
+
+    PRIVATE
+
+    INTERFACE netcdf4_init_module
+       MODULE PROCEDURE netcdf4_init_module
+    END INTERFACE netcdf4_init_module
+
+    INTERFACE netcdf4_open_file
+       MODULE PROCEDURE netcdf4_open_file
+    END INTERFACE netcdf4_open_file
+
+    INTERFACE netcdf4_init_dimension
+       MODULE PROCEDURE netcdf4_init_dimension
+    END INTERFACE netcdf4_init_dimension
+
+    INTERFACE netcdf4_init_variable
+       MODULE PROCEDURE netcdf4_init_variable
+    END INTERFACE netcdf4_init_variable
+
+    INTERFACE netcdf4_write_attribute
+       MODULE PROCEDURE netcdf4_write_attribute
+    END INTERFACE netcdf4_write_attribute
+
+    INTERFACE netcdf4_stop_file_header_definition
+       MODULE PROCEDURE netcdf4_stop_file_header_definition
+    END INTERFACE netcdf4_stop_file_header_definition
+
+    INTERFACE netcdf4_write_variable
+       MODULE PROCEDURE netcdf4_write_variable
+    END INTERFACE netcdf4_write_variable
+
+    INTERFACE netcdf4_finalize
+       MODULE PROCEDURE netcdf4_finalize
+    END INTERFACE netcdf4_finalize
+
+    INTERFACE netcdf4_get_error_message
+       MODULE PROCEDURE netcdf4_get_error_message
+    END INTERFACE netcdf4_get_error_message
+
+    PUBLIC &
+       netcdf4_finalize, &
+       netcdf4_get_error_message, &
+       netcdf4_init_dimension, &
+       netcdf4_stop_file_header_definition, &
+       netcdf4_init_module, &
+       netcdf4_init_variable, &
+       netcdf4_open_file, &
+       netcdf4_write_attribute, &
+       netcdf4_write_variable
+
+
+ CONTAINS
 
 
@@ -129,29 +132,29 @@
 !> Initialize data-output module.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE netcdf4_init_module( file_suffix_of_output_group, mpi_comm_of_output_group, &
-                                master_output_rank,                                    &
-                                program_debug_output_unit, debug_output, dom_global_id )
-
-   CHARACTER(LEN=*), INTENT(IN) ::  file_suffix_of_output_group  !> file-name suffix added to each file;
-                                                                 !> must be unique for each output group
-
-   INTEGER, 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
-
-
-   file_suffix = file_suffix_of_output_group
-   output_group_comm = mpi_comm_of_output_group
-   master_rank = master_output_rank
-
-   debug_output_unit = program_debug_output_unit
-   print_debug_output = debug_output
-
-   global_id_in_file = dom_global_id
-
-END SUBROUTINE netcdf4_init_module
+ SUBROUTINE netcdf4_init_module( file_suffix_of_output_group, mpi_comm_of_output_group, &
+                                 master_output_rank,                                    &
+                                 program_debug_output_unit, debug_output, dom_global_id )
+
+    CHARACTER(LEN=*), INTENT(IN) ::  file_suffix_of_output_group  !> file-name suffix added to each file;
+                                                                  !> must be unique for each output group
+
+    INTEGER, 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
+
+
+    file_suffix = file_suffix_of_output_group
+    output_group_comm = mpi_comm_of_output_group
+    master_rank = master_output_rank
+
+    debug_output_unit = program_debug_output_unit
+    print_debug_output = debug_output
+
+    global_id_in_file = dom_global_id
+
+ END SUBROUTINE netcdf4_init_module
 
 !--------------------------------------------------------------------------------------------------!
@@ -160,88 +163,88 @@
 !> Open netcdf file.
 !--------------------------------------------------------------------------------------------------!
-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, 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
-
-
-   return_value = 0
-   file_id = -1
-
-   !-- Open new file
-   CALL internal_message( 'debug', routine_name // ': create file "' // TRIM( file_name ) // '"' )
-
-   IF ( TRIM( mode ) == mode_serial )  THEN
+ 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, 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
+
+
+    return_value = 0
+    file_id = -1
+!
+!-- Open new file
+    CALL internal_message( 'debug', routine_name // ': create file "' // TRIM( file_name ) // '"' )
+
+    IF ( TRIM( mode ) == mode_serial )  THEN
 
 #if defined( __netcdf4 )
 #if defined( __parallel )
-      CALL MPI_COMM_RANK( output_group_comm, my_rank, return_value )
-      IF ( return_value /= 0 )  THEN
-         CALL internal_message( 'error', routine_name // ': MPI_COMM_RANK error' )
-      ENDIF
-      IF ( my_rank /= master_rank )  THEN
-         return_value = 1
-         CALL internal_message( 'error', routine_name // &
-                                ': trying to define a NetCDF file in serial mode by an MPI ' // &
-                                'rank other than the master output rank. Serial NetCDF ' // &
-                                'files can only be defined by the master output rank!' )
-      ENDIF
-#else
-      my_rank = master_rank
-      return_value = 0
-#endif
-
-      IF ( return_value == 0 )  &
-         nc_stat = NF90_CREATE( TRIM( file_name ) // TRIM( file_suffix ), &
-                                IOR( NF90_NOCLOBBER, NF90_NETCDF4 ),      &
-                                file_id )
-#else
-      nc_stat = 0
-      return_value = 1
-      CALL internal_message( 'error', routine_name //                               &
-                             ': pre-processor directive "__netcdf4" not given. ' // &
-                             'Using NetCDF4 output not possible' )
-#endif
-
-   ELSEIF ( TRIM( mode ) == mode_parallel )  THEN
+       CALL MPI_COMM_RANK( output_group_comm, my_rank, return_value )
+       IF ( return_value /= 0 )  THEN
+          CALL internal_message( 'error', routine_name // ': MPI_COMM_RANK error' )
+       ENDIF
+       IF ( my_rank /= master_rank )  THEN
+          return_value = 1
+          CALL internal_message( 'error', routine_name // &
+                                 ': trying to define a NetCDF file in serial mode by an MPI ' // &
+                                 'rank other than the master output rank. Serial NetCDF ' // &
+                                 'files can only be defined by the master output rank!' )
+       ENDIF
+#else
+       my_rank = master_rank
+       return_value = 0
+#endif
+
+       IF ( return_value == 0 )  &
+          nc_stat = NF90_CREATE( TRIM( file_name ) // TRIM( file_suffix ), &
+                                 IOR( NF90_NOCLOBBER, NF90_NETCDF4 ),      &
+                                 file_id )
+#else
+       nc_stat = 0
+       return_value = 1
+       CALL internal_message( 'error', routine_name //                               &
+                              ': pre-processor directive "__netcdf4" not given. ' // &
+                              'Using NetCDF4 output not possible' )
+#endif
+
+    ELSEIF ( TRIM( mode ) == mode_parallel )  THEN
 
 #if defined( __parallel ) && defined( __netcdf4 ) && defined( __netcdf4_parallel )
-      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 )
-#else
-      nc_stat = 0
-      return_value = 1
-      CALL internal_message( 'error', routine_name //                                 &
-                             ': pre-processor directives "__parallel" and/or ' //     &
-                             '"__netcdf4" and/or "__netcdf4_parallel" not given. ' // &
-                             'Using parallel NetCDF4 output not possible' )
-#endif
-
-   ELSE
-      nc_stat = 0
-      return_value = 1
-      CALL internal_message( 'error', routine_name // ': selected mode "' //  &
-                                      TRIM( mode ) // '" must be either "' // &
-                                      mode_serial // '" or "' // mode_parallel // '"' )
-   ENDIF
-
-#if defined( __netcdf4 )
-   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( file_name ) // '": ' // NF90_STRERROR( nc_stat ) )
-   ENDIF
-#endif
-
-END SUBROUTINE netcdf4_open_file
+       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 )
+#else
+       nc_stat = 0
+       return_value = 1
+       CALL internal_message( 'error', routine_name //                                 &
+                              ': pre-processor directives "__parallel" and/or ' //     &
+                              '"__netcdf4" and/or "__netcdf4_parallel" not given. ' // &
+                              'Using parallel NetCDF4 output not possible' )
+#endif
+
+    ELSE
+       nc_stat = 0
+       return_value = 1
+       CALL internal_message( 'error', routine_name // ': selected mode "' //  &
+                                       TRIM( mode ) // '" must be either "' // &
+                                       mode_serial // '" or "' // mode_parallel // '"' )
+    ENDIF
+
+#if defined( __netcdf4 )
+    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( file_name ) // '": ' // NF90_STRERROR( nc_stat ) )
+    ENDIF
+#endif
+
+ END SUBROUTINE netcdf4_open_file
 
 !--------------------------------------------------------------------------------------------------!
@@ -250,71 +253,71 @@
 !> Write attribute to netcdf file.
 !--------------------------------------------------------------------------------------------------!
-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 ::  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
-
-
-#if defined( __netcdf4 )
-   return_value = 0
-
-   IF ( variable_id == global_id_in_file )  THEN
-      target_id = NF90_GLOBAL
-   ELSE
-      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', routine_name // &
-                             ': no value given for attribute "' // TRIM( attribute_name ) // '"' )
-   ENDIF
-
-   IF ( return_value == 0 )  THEN
-      IF ( nc_stat /= NF90_NOERR )  THEN
-         return_value = 1
-         CALL internal_message( 'error', routine_name //                      &
-                                ': NetCDF error while writing attribute "' // &
-                                TRIM( attribute_name ) // '": ' // NF90_STRERROR( nc_stat ) )
-      ENDIF
-   ENDIF
-#else
-   return_value = 1
-#endif
-
-END SUBROUTINE netcdf4_write_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 ::  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
+
+
+#if defined( __netcdf4 )
+    return_value = 0
+
+    IF ( variable_id == global_id_in_file )  THEN
+       target_id = NF90_GLOBAL
+    ELSE
+       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', routine_name // &
+                              ': no value given for attribute "' // TRIM( attribute_name ) // '"' )
+    ENDIF
+
+    IF ( return_value == 0 )  THEN
+       IF ( nc_stat /= NF90_NOERR )  THEN
+          return_value = 1
+          CALL internal_message( 'error', routine_name //                      &
+                                 ': NetCDF error while writing attribute "' // &
+                                 TRIM( attribute_name ) // '": ' // NF90_STRERROR( nc_stat ) )
+       ENDIF
+    ENDIF
+#else
+    return_value = 1
+#endif
+
+ END SUBROUTINE netcdf4_write_attribute
 
 !--------------------------------------------------------------------------------------------------!
@@ -323,58 +326,58 @@
 !> Initialize dimension.
 !--------------------------------------------------------------------------------------------------!
-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, 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
-   variable_id = -1
-
-   CALL internal_message( 'debug', routine_name // &
-                          ': init dimension "' // TRIM( dimension_name ) // '"' )
-
-   !-- Check if dimension is unlimited
-   IF ( dimension_length < 0 )  THEN
-      nc_dimension_length = NF90_UNLIMITED
-   ELSE
-      nc_dimension_length = dimension_length
-   ENDIF
-
-   !-- Define dimension in file
-   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, 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( dimension_name ) // '": ' // NF90_STRERROR( nc_stat ) )
-   ENDIF
-#else
-   return_value = 1
-   variable_id = -1
-   dimension_id = -1
-#endif
-
-END SUBROUTINE netcdf4_init_dimension
+ 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, 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
+    variable_id = -1
+
+    CALL internal_message( 'debug', routine_name // &
+                           ': init dimension "' // TRIM( dimension_name ) // '"' )
+!
+!-- Check if dimension is unlimited
+    IF ( dimension_length < 0 )  THEN
+       nc_dimension_length = NF90_UNLIMITED
+    ELSE
+       nc_dimension_length = dimension_length
+    ENDIF
+!
+!-- Define dimension in file
+    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, 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( dimension_name ) // '": ' // NF90_STRERROR( nc_stat ) )
+    ENDIF
+#else
+    return_value = 1
+    variable_id = -1
+    dimension_id = -1
+#endif
+
+ END SUBROUTINE netcdf4_init_dimension
 
 !--------------------------------------------------------------------------------------------------!
@@ -383,67 +386,68 @@
 !> Initialize 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, 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)
-
-
-#if defined( __netcdf4 )
-   return_value = 0
-
-   WRITE( temp_string, * ) is_global
-   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, variable_name, nc_variable_type, dimension_ids, variable_id )
+ 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, 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)
+
+
+#if defined( __netcdf4 )
+    return_value = 0
+
+    WRITE( temp_string, * ) is_global
+    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, variable_name, nc_variable_type, dimension_ids, variable_id )
 
 #if defined( __netcdf4_parallel )
-      !-- Define how variable can be accessed by PEs in parallel netcdf file
-      IF ( nc_stat == NF90_NOERR  .AND.  TRIM( mode ) == mode_parallel )  THEN
-         IF ( is_global )  THEN
-            nc_stat = NF90_VAR_PAR_ACCESS( file_id, variable_id, NF90_INDEPENDENT )
-         ELSE
-            nc_stat = NF90_VAR_PAR_ACCESS( file_id, variable_id, NF90_COLLECTIVE )
-         ENDIF
-      ENDIF
-#endif
-
-      IF ( nc_stat /= NF90_NOERR )  THEN
-         return_value = 1
-         CALL internal_message( 'error', routine_name //                          &
-                                ': NetCDF error while initializing variable "' // &
-                                TRIM( variable_name ) // '": ' // NF90_STRERROR( nc_stat ) )
-      ENDIF
-
-   ELSE
-      return_value = 1
-   ENDIF
-
-#else
-   return_value = 1
-   variable_id = -1
-#endif
-
-END SUBROUTINE netcdf4_init_variable
+!
+!--    Define how variable can be accessed by PEs in parallel netcdf file
+       IF ( nc_stat == NF90_NOERR  .AND.  TRIM( mode ) == mode_parallel )  THEN
+          IF ( is_global )  THEN
+             nc_stat = NF90_VAR_PAR_ACCESS( file_id, variable_id, NF90_INDEPENDENT )
+          ELSE
+             nc_stat = NF90_VAR_PAR_ACCESS( file_id, variable_id, NF90_COLLECTIVE )
+          ENDIF
+       ENDIF
+#endif
+
+       IF ( nc_stat /= NF90_NOERR )  THEN
+          return_value = 1
+          CALL internal_message( 'error', routine_name //                          &
+                                 ': NetCDF error while initializing variable "' // &
+                                 TRIM( variable_name ) // '": ' // NF90_STRERROR( nc_stat ) )
+       ENDIF
+
+    ELSE
+       return_value = 1
+    ENDIF
+
+#else
+    return_value = 1
+    variable_id = -1
+#endif
+
+ END SUBROUTINE netcdf4_init_variable
 
 !--------------------------------------------------------------------------------------------------!
@@ -452,38 +456,38 @@
 !> Leave file definition state.
 !--------------------------------------------------------------------------------------------------!
-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
-
-
-#if defined( __netcdf4 )
-   return_value = 0
-
-   WRITE( temp_string, * ) file_id
-   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_fill_mode )
-
-   IF ( nc_stat == NF90_NOERR )  THEN
-      nc_stat = NF90_ENDDEF( file_id )
-   ENDIF
-
-   IF ( nc_stat /= NF90_NOERR )  THEN
-      return_value = 1
-      CALL internal_message( 'error', routine_name // &
-                             ': NetCDF error: ' // NF90_STRERROR( nc_stat ) )
-   ENDIF
-#else
-   return_value = 1
-#endif
-
-END SUBROUTINE netcdf4_stop_file_header_definition
+ 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
+
+
+#if defined( __netcdf4 )
+    return_value = 0
+
+    WRITE( temp_string, * ) file_id
+    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_fill_mode )
+
+    IF ( nc_stat == NF90_NOERR )  THEN
+       nc_stat = NF90_ENDDEF( file_id )
+    ENDIF
+
+    IF ( nc_stat /= NF90_NOERR )  THEN
+       return_value = 1
+       CALL internal_message( 'error', routine_name // &
+                              ': NetCDF error: ' // NF90_STRERROR( nc_stat ) )
+    ENDIF
+#else
+    return_value = 1
+#endif
+
+ END SUBROUTINE netcdf4_stop_file_header_definition
 
 !--------------------------------------------------------------------------------------------------!
@@ -492,63 +496,63 @@
 !> Write variable of different kind into netcdf file.
 !--------------------------------------------------------------------------------------------------!
-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              ::  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                   ::  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
+ 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              ::  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                   ::  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
 
 
@@ -556,193 +560,201 @@
 
 #if defined( __parallel )
-   CALL MPI_COMM_RANK( output_group_comm, my_rank, return_value )
-   IF ( return_value /= 0 )  THEN
-      CALL internal_message( 'error', routine_name // ': MPI_COMM_RANK error' )
-   ENDIF
-#else
-   my_rank = master_rank
-   return_value = 0
-#endif
-
-   IF ( return_value == 0  .AND.  ( .NOT. is_global  .OR.  my_rank == master_rank ) )  THEN
-
-      WRITE( temp_string, * ) variable_id
-      CALL internal_message( 'debug', routine_name // ': write variable ' // TRIM( temp_string ) )
-
-      ndims = SIZE( bounds_start )
-
-      !-- 8bit integer output
-      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( 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( 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( 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( 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( 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( 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 )
-      ELSE
-         return_value = 1
-         nc_stat = NF90_NOERR
-         WRITE( temp_string, '(": variable_id=",I6,", file_id=",I6)' ) variable_id, file_id
-         CALL internal_message( 'error', routine_name // &
-                                ': no output values given ' // TRIM( temp_string ) )
-      ENDIF
-
-      !-- Check for errors
-      IF ( nc_stat /= NF90_NOERR )  THEN
-         return_value = 1
-
-         IF ( nc_stat == NF90_EEDGE .OR. nc_stat == NF90_EINVALCOORDS )  THEN
-
-            !-- If given bounds exceed dimension bounds, get information of bounds in file
-            WRITE( temp_string, * )  NF90_STRERROR( nc_stat )
-
-            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 <= ndims .AND. nc_stat == NF90_NOERR )
-               nc_stat = NF90_INQUIRE_DIMENSION( file_id, dimension_ids(d), &
-                                                 LEN=dimension_lengths(d) )
-               d = d + 1
-            ENDDO
-
-            IF ( nc_stat == NF90_NOERR )  THEN
-               WRITE( temp_string, * )  TRIM( temp_string ) // '; given variable bounds: ' //  &
-                  'start=', bounds_start, ', count=', value_counts, ', origin=', bounds_origin
-               CALL internal_message( 'error', routine_name //     &
-                                      ': 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 ) )
-            ENDIF
-
-         ELSE
-            !-- Other NetCDF error
-            CALL internal_message( 'error', routine_name //     &
-                                   ': error while writing: ' // NF90_STRERROR( nc_stat ) )
-         ENDIF
-      ENDIF
-
-   ENDIF
-#else
-   return_value = 1
-#endif
-
-END SUBROUTINE netcdf4_write_variable
+    CALL MPI_COMM_RANK( output_group_comm, my_rank, return_value )
+    IF ( return_value /= 0 )  THEN
+       CALL internal_message( 'error', routine_name // ': MPI_COMM_RANK error' )
+    ENDIF
+#else
+    my_rank = master_rank
+    return_value = 0
+#endif
+
+    IF ( return_value == 0  .AND.  ( .NOT. is_global  .OR.  my_rank == master_rank ) )  THEN
+
+       WRITE( temp_string, * ) variable_id
+       CALL internal_message( 'debug', routine_name // ': write variable ' // TRIM( temp_string ) )
+
+       ndims = SIZE( bounds_start )
+!
+!--    8bit integer output
+       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( 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( 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( 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( 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( 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( 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 )
+       ELSE
+          return_value = 1
+          nc_stat = NF90_NOERR
+          WRITE( temp_string, '(": variable_id=",I6,", file_id=",I6)' ) variable_id, file_id
+          CALL internal_message( 'error', routine_name // &
+                                 ': no output values given ' // TRIM( temp_string ) )
+       ENDIF
+!
+!--    Check for errors
+       IF ( nc_stat /= NF90_NOERR )  THEN
+          return_value = 1
+
+          IF ( nc_stat == NF90_EEDGE .OR. nc_stat == NF90_EINVALCOORDS )  THEN
+!
+!--          If given bounds exceed dimension bounds, get information of bounds in file
+             WRITE( temp_string, * )  NF90_STRERROR( nc_stat )
+
+             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 <= ndims .AND. nc_stat == NF90_NOERR )
+                nc_stat = NF90_INQUIRE_DIMENSION( file_id, dimension_ids(d), &
+                                                  LEN=dimension_lengths(d) )
+                d = d + 1
+             ENDDO
+
+             IF ( nc_stat == NF90_NOERR )  THEN
+                WRITE( temp_string, * )  TRIM( temp_string ) // '; given variable bounds: ' //  &
+                   'start=', bounds_start, ', count=', value_counts, ', origin=', bounds_origin
+                CALL internal_message( 'error', routine_name //     &
+                                       ': 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 ) )
+             ENDIF
+
+          ELSE
+!
+!--          Other NetCDF error
+             CALL internal_message( 'error', routine_name //     &
+                                    ': error while writing: ' // NF90_STRERROR( nc_stat ) )
+          ENDIF
+       ENDIF
+
+    ENDIF
+#else
+    return_value = 1
+#endif
+
+ END SUBROUTINE netcdf4_write_variable
 
 !--------------------------------------------------------------------------------------------------!
@@ -751,31 +763,31 @@
 !> Close netcdf file.
 !--------------------------------------------------------------------------------------------------!
-SUBROUTINE netcdf4_finalize( file_id, return_value )
-
-   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'netcdf4_finalize'  !< name of routine
-
-   INTEGER, INTENT(IN)  ::  file_id       !< file ID
-   INTEGER              ::  nc_stat       !< netcdf return value
-   INTEGER, INTENT(OUT) ::  return_value  !< return value
-
-
-#if defined( __netcdf4 )
-   WRITE( temp_string, * ) file_id
-   CALL internal_message( 'debug', routine_name // &
-                          ': close file (file_id=' // TRIM( temp_string ) // ')' )
-
-   nc_stat = NF90_CLOSE( file_id )
-   IF ( nc_stat == NF90_NOERR )  THEN
-      return_value = 0
-   ELSE
-      return_value = 1
-      CALL internal_message( 'error', routine_name // &
-                             ': NetCDF error: ' // NF90_STRERROR( nc_stat ) )
-   ENDIF
-#else
-   return_value = 1
-#endif
-
-END SUBROUTINE netcdf4_finalize
+ SUBROUTINE netcdf4_finalize( file_id, return_value )
+
+    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'netcdf4_finalize'  !< name of routine
+
+    INTEGER, INTENT(IN)  ::  file_id       !< file ID
+    INTEGER              ::  nc_stat       !< netcdf return value
+    INTEGER, INTENT(OUT) ::  return_value  !< return value
+
+
+#if defined( __netcdf4 )
+    WRITE( temp_string, * ) file_id
+    CALL internal_message( 'debug', routine_name // &
+                           ': close file (file_id=' // TRIM( temp_string ) // ')' )
+
+    nc_stat = NF90_CLOSE( file_id )
+    IF ( nc_stat == NF90_NOERR )  THEN
+       return_value = 0
+    ELSE
+       return_value = 1
+       CALL internal_message( 'error', routine_name // &
+                              ': NetCDF error: ' // NF90_STRERROR( nc_stat ) )
+    ENDIF
+#else
+    return_value = 1
+#endif
+
+ END SUBROUTINE netcdf4_finalize
 
 !--------------------------------------------------------------------------------------------------!
@@ -784,43 +796,43 @@
 !> Convert data_type string into netcdf data type value.
 !--------------------------------------------------------------------------------------------------!
-FUNCTION get_netcdf_data_type( data_type ) RESULT( return_value )
-
-   CHARACTER(LEN=*), INTENT(IN) ::  data_type  !< requested data type
-
-   CHARACTER(LEN=*), PARAMETER ::  routine_name = 'get_netcdf_data_type'  !< name of this routine
-
-   INTEGER ::  return_value  !< netcdf data type
-
-
-   SELECT CASE ( TRIM( data_type ) )
-
-#if defined( __netcdf4 )
-      CASE ( 'char' )
-         return_value = NF90_CHAR
-
-      CASE ( 'int8' )
-         return_value = NF90_BYTE
-
-      CASE ( 'int16' )
-         return_value = NF90_SHORT
-
-      CASE ( 'int32' )
-         return_value = NF90_INT
-
-      CASE ( 'real32' )
-         return_value = NF90_FLOAT
-
-      CASE ( 'real64' )
-         return_value = NF90_DOUBLE
-#endif
-
-      CASE DEFAULT
-         CALL internal_message( 'error', routine_name // &
-                                ': data type unknown (' // TRIM( data_type ) // ')' )
-         return_value = -1
-
-   END SELECT
-
-END FUNCTION get_netcdf_data_type
+ FUNCTION get_netcdf_data_type( data_type ) RESULT( return_value )
+
+    CHARACTER(LEN=*), INTENT(IN) ::  data_type  !< requested data type
+
+    CHARACTER(LEN=*), PARAMETER ::  routine_name = 'get_netcdf_data_type'  !< name of this routine
+
+    INTEGER ::  return_value  !< netcdf data type
+
+
+    SELECT CASE ( TRIM( data_type ) )
+
+#if defined( __netcdf4 )
+       CASE ( 'char' )
+          return_value = NF90_CHAR
+
+       CASE ( 'int8' )
+          return_value = NF90_BYTE
+
+       CASE ( 'int16' )
+          return_value = NF90_SHORT
+
+       CASE ( 'int32' )
+          return_value = NF90_INT
+
+       CASE ( 'real32' )
+          return_value = NF90_FLOAT
+
+       CASE ( 'real64' )
+          return_value = NF90_DOUBLE
+#endif
+
+       CASE DEFAULT
+          CALL internal_message( 'error', routine_name // &
+                                 ': data type unknown (' // TRIM( data_type ) // ')' )
+          return_value = -1
+
+    END SELECT
+
+ END FUNCTION get_netcdf_data_type
 
 !--------------------------------------------------------------------------------------------------!
@@ -830,22 +842,22 @@
 !> 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)' ) ': ', 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
+ 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)' ) ': ', 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
 
 !--------------------------------------------------------------------------------------------------!
@@ -854,15 +866,15 @@
 !> Return the last created 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
-
-
-END MODULE data_output_netcdf4_module
+ 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
+
+
+ END MODULE data_output_netcdf4_module
