Index: /palm/trunk/UTIL/Makefile_utilities
===================================================================
--- /palm/trunk/UTIL/Makefile_utilities	(revision 3865)
+++ /palm/trunk/UTIL/Makefile_utilities	(revision 3866)
@@ -23,4 +23,8 @@
 # -----------------
 # $Id$
+# Use PALM's kinds module in inifor
+# 
+# 
+# 3795 2019-03-15 09:40:05Z eckhard
 # Upated inifor build dependencies
 # 
@@ -141,4 +145,6 @@
 	inifor_defs.o \
 	inifor_util.o
+inifor_defs.o: \
+	mod_kinds.o
 inifor_grid.o: \
 	inifor_control.o \
Index: /palm/trunk/UTIL/inifor/src/inifor.f90
===================================================================
--- /palm/trunk/UTIL/inifor/src/inifor.f90	(revision 3865)
+++ /palm/trunk/UTIL/inifor/src/inifor.f90	(revision 3866)
@@ -26,4 +26,11 @@
 ! -----------------
 ! $Id$
+! Use PALM's working precision
+! Show error message if compiled without netCDF support
+! Renamed run_control -> log_runtime
+! Improved coding style added comments
+! 
+! 
+! 3785 2019-03-06 10:41:14Z eckhard
 ! Average geostrophic wind components on coarse COSMO levels instead of fine PALM levels
 ! Remove --debug netCDF output of internal pressure profiles
@@ -81,4 +88,5 @@
 !------------------------------------------------------------------------------!
  PROGRAM inifor
+
 #if defined ( __netcdf )
 
@@ -86,17 +94,43 @@
     USE inifor_defs
     USE inifor_grid,                                                           &
-        ONLY:  setup_parameters, setup_grids, setup_variable_tables,           &
-               setup_io_groups, fini_grids, fini_variables, fini_io_groups,    &
-               fini_file_lists, preprocess, origin_lon, origin_lat,            &
-               output_file, io_group_list, output_var_table,                   &
-               cosmo_grid, palm_grid, nx, ny, nz, p0, cfg, f3,                 &
-               averaging_width_ns, averaging_width_ew, phi_n, lambda_n,        &
-               lam_centre, phi_centre
+        ONLY:  averaging_width_ns,                                             &
+               averaging_width_ew,                                             &
+               cfg,                                                            &    
+               cosmo_grid,                                                     &
+               f3,                                                             &
+               fini_grids,                                                     &
+               fini_io_groups,                                                 &
+               fini_variables,                                                 &
+               fini_file_lists,                                                &
+               io_group_list,                                                  &
+               lam_centre,                                                     &
+               lambda_n,                                                       &
+               nx, ny, nz,                                                     &
+               origin_lat,                                                     &
+               origin_lon,                                                     &
+               output_file,                                                    &
+               output_var_table,                                               &
+               p0,                                                             &
+               phi_centre,                                                     &
+               phi_n,                                                          &
+               preprocess,                                                     &
+               palm_grid,                                                      &
+               setup_grids,                                                    &
+               setup_parameters,                                               &
+               setup_variable_tables,                                          &
+               setup_io_groups
     USE inifor_io
     USE inifor_transform,                                                      &
-        ONLY:  average_pressure_perturbation, average_profile, interpolate_1d, &
-               interpolate_1d_arr, interpolate_2d, interpolate_3d,             &
-               interp_average_profile, geostrophic_winds, extrapolate_density, &
-               extrapolate_pressure, get_surface_pressure
+        ONLY:  average_pressure_perturbation,                                  &
+               average_profile,                                                & 
+               extrapolate_density,                                            &
+               extrapolate_pressure,                                           & 
+               geostrophic_winds,                                              &
+               get_surface_pressure,                                           &
+               interp_average_profile,                                         &
+               interpolate_1d,                                                 &
+               interpolate_1d_arr,                                             & 
+               interpolate_2d,                                                 &
+               interpolate_3d
     USE inifor_types
     
@@ -107,21 +141,21 @@
     INTEGER ::  iter   !< loop index for time steps
 
-    REAL(dp), ALLOCATABLE, DIMENSION(:,:,:)     ::  output_arr !< array buffer for interpolated quantities
-    REAL(dp), ALLOCATABLE, DIMENSION(:), TARGET ::  rho_centre !< density profile of the centre averaging domain
-    REAL(dp), ALLOCATABLE, DIMENSION(:), TARGET ::  ug_cosmo   !< profile of the geostrophic wind in x direction on COSMO levels
-    REAL(dp), ALLOCATABLE, DIMENSION(:), TARGET ::  vg_cosmo   !< profile of the geostrophic wind in y direction on COSMO levels
-    REAL(dp), ALLOCATABLE, DIMENSION(:), TARGET ::  ug_palm    !< profile of the geostrophic wind in x direction interpolated onto PALM levels
-    REAL(dp), ALLOCATABLE, DIMENSION(:), TARGET ::  vg_palm    !< profile of the geostrophic wind in y direction interpolated onto PALM levels
-    REAL(dp), ALLOCATABLE, DIMENSION(:), TARGET ::  rho_north  !< density profile of the northern averaging domain
-    REAL(dp), ALLOCATABLE, DIMENSION(:), TARGET ::  rho_south  !< density profile of the southern averaging domain
-    REAL(dp), ALLOCATABLE, DIMENSION(:), TARGET ::  rho_east   !< density profile of the eastern averaging domain
-    REAL(dp), ALLOCATABLE, DIMENSION(:), TARGET ::  rho_west   !< density profile of the western averaging domain
-    REAL(dp), ALLOCATABLE, DIMENSION(:), TARGET ::  p_north    !< pressure profile of the northern averaging domain
-    REAL(dp), ALLOCATABLE, DIMENSION(:), TARGET ::  p_south    !< pressure profile of the southern averaging domain
-    REAL(dp), ALLOCATABLE, DIMENSION(:), TARGET ::  p_east     !< pressure profile of the eastern averaging domain
-    REAL(dp), ALLOCATABLE, DIMENSION(:), TARGET ::  p_west     !< pressure profile of the western averaging domain
-
-    REAL(dp), POINTER, DIMENSION(:) ::  internal_arr !< pointer to the currently processed internal array (density, pressure)
-    REAL(dp), POINTER, DIMENSION(:) ::  ug_vg_palm   !< pointer to the currently processed geostrophic wind component
+    REAL(wp), ALLOCATABLE, DIMENSION(:,:,:)     ::  output_arr !< array buffer for interpolated quantities
+    REAL(wp), ALLOCATABLE, DIMENSION(:), TARGET ::  rho_centre !< density profile of the centre averaging domain
+    REAL(wp), ALLOCATABLE, DIMENSION(:), TARGET ::  ug_cosmo   !< profile of the geostrophic wind in x direction on COSMO levels
+    REAL(wp), ALLOCATABLE, DIMENSION(:), TARGET ::  vg_cosmo   !< profile of the geostrophic wind in y direction on COSMO levels
+    REAL(wp), ALLOCATABLE, DIMENSION(:), TARGET ::  ug_palm    !< profile of the geostrophic wind in x direction interpolated onto PALM levels
+    REAL(wp), ALLOCATABLE, DIMENSION(:), TARGET ::  vg_palm    !< profile of the geostrophic wind in y direction interpolated onto PALM levels
+    REAL(wp), ALLOCATABLE, DIMENSION(:), TARGET ::  rho_north  !< density profile of the northern averaging domain
+    REAL(wp), ALLOCATABLE, DIMENSION(:), TARGET ::  rho_south  !< density profile of the southern averaging domain
+    REAL(wp), ALLOCATABLE, DIMENSION(:), TARGET ::  rho_east   !< density profile of the eastern averaging domain
+    REAL(wp), ALLOCATABLE, DIMENSION(:), TARGET ::  rho_west   !< density profile of the western averaging domain
+    REAL(wp), ALLOCATABLE, DIMENSION(:), TARGET ::  p_north    !< pressure profile of the northern averaging domain
+    REAL(wp), ALLOCATABLE, DIMENSION(:), TARGET ::  p_south    !< pressure profile of the southern averaging domain
+    REAL(wp), ALLOCATABLE, DIMENSION(:), TARGET ::  p_east     !< pressure profile of the eastern averaging domain
+    REAL(wp), ALLOCATABLE, DIMENSION(:), TARGET ::  p_west     !< pressure profile of the western averaging domain
+
+    REAL(wp), POINTER, DIMENSION(:) ::  internal_arr !< pointer to the currently processed internal array (density, pressure)
+    REAL(wp), POINTER, DIMENSION(:) ::  ug_vg_palm   !< pointer to the currently processed geostrophic wind component
 
     TYPE(nc_var), POINTER        ::  output_var      !< pointer to the currently processed output variable
@@ -138,216 +172,235 @@
 !- Section 1: Initialization
 !------------------------------------------------------------------------------
- CALL run_control('init', 'void')
+    CALL log_runtime( 'init', 'void' )
 
 !
 !-- Initialize INIFOR's parameters from command-line interface and namelists
-    CALL setup_parameters()
+    CALL setup_parameters
 
 !
 !-- Initialize all grids, including interpolation neighbours and weights
-    CALL setup_grids()
- CALL run_control('time', 'init')
+    CALL setup_grids
+    CALL log_runtime( 'time', 'init' )
 
 !
 !-- Initialize the netCDF output file and define dimensions
-    CALL setup_netcdf_dimensions(output_file, palm_grid, cfg % start_date,    &
+    CALL setup_netcdf_dimensions(output_file, palm_grid, cfg%start_date,    &
                                  origin_lon, origin_lat)
- CALL run_control('time', 'write')
+    CALL log_runtime( 'time', 'write' )
 
 !
 !-- Set up the tables containing the input and output variables and set
 !-- the corresponding netCDF dimensions for each output variable
-    CALL setup_variable_tables(cfg % ic_mode)
- CALL run_control('time', 'write')
+    CALL setup_variable_tables( cfg%ic_mode )
+    CALL log_runtime( 'time', 'write' )
 
 !
 !-- Add the output variables to the netCDF output file
-    CALL setup_netcdf_variables(output_file % name, output_var_table)
-
-    CALL setup_io_groups()
- CALL run_control('time', 'init')
-
-!------------------------------------------------------------------------------
-!- Section 2: Main loop
-!------------------------------------------------------------------------------
-    DO igroup = 1, SIZE(io_group_list)
+    CALL setup_netcdf_variables( output_file%name, output_var_table )
+
+    CALL setup_io_groups
+    CALL log_runtime( 'time', 'init' )
+
+!------------------------------------------------------------------------------
+!-- Section 2: Main loop
+!------------------------------------------------------------------------------
+!
+!-- Input and output variables are organized into IO groups. For instance, the
+!-- 'thermodynamics' IO group bundles the input variaebls T, P, QV and the
+!-- output variables p, theta, rho, and qv.
+!-- An IO group bunldes variables that are physically dependent on each other.
+!-- In case of the 'thermodynamics' group, theta = f(P,T), rho = f(P,T,QV).
+    DO  igroup = 1, SIZE( io_group_list )
 
        group => io_group_list(igroup)
-       IF ( group % to_be_processed )  THEN
+       IF ( group%to_be_processed )  THEN
           
-          DO iter = 1, group % nt 
-
-!------------------------------------------------------------------------------
-!- Section 2.1: Read and preprocess input data
-!------------------------------------------------------------------------------
-             CALL read_input_variables(group, iter, input_buffer)
- CALL run_control('time', 'read')
-
-             CALL preprocess(group, input_buffer, cosmo_grid, iter)
- CALL run_control('time', 'comp')
+!--       Loop over all output time steps for the current group.
+          DO  iter = 1, group%nt 
+
+!------------------------------------------------------------------------------
+!-- Section 2.1: Read and preprocess input data
+!------------------------------------------------------------------------------
+             CALL read_input_variables( group, iter, input_buffer )
+             CALL log_runtime( 'time', 'read' )
+
+!--          Carry out all required physical conversion of the input variables
+!--          of the current IO group on the input (COSMO) grid. For instance,
+!--          horizontal velocities are rotated to the PALM coordinate system and
+!--          potential temperature is computed from the absolute temperature and
+!--          pressure.
+             CALL preprocess( group, input_buffer, cosmo_grid, iter )
+             CALL log_runtime( 'time', 'comp' )
 
              !TODO: move this assertion into 'preprocess'.
-             IF ( .NOT. ALL(input_buffer(:) % is_preprocessed .AND. .TRUE.) )  THEN
-                message = "Input buffers for group '" // TRIM(group % kind) // &
-                   "' could not be preprocessed sucessfully."
-                CALL inifor_abort('main loop', message)
+             IF ( .NOT. ALL(input_buffer(:)%is_preprocessed .AND. .TRUE.) )  THEN
+                message = "Input buffers for group '" // TRIM( group%kind ) // &
+                          "' could not be preprocessed sucessfully."
+                CALL inifor_abort( 'main loop', message )
              ENDIF
 
 !------------------------------------------------------------------------------
-!- Section 2.2: Interpolate each output variable of the group
-!------------------------------------------------------------------------------
-             DO ivar = 1, group % nv
-
-                output_var => group % out_vars( ivar )
-
-                IF ( output_var % to_be_processed .AND.                        &
-                     iter .LE. output_var % nt )  THEN
-
-                   message = "Processing '" // TRIM(output_var % name) //      &
-                             "' (" // TRIM(output_var % kind) //               &
-                             "), iteration " // TRIM(str(iter)) //" of " //    &
-                             TRIM(str(output_var % nt))
-                   CALL report('main loop', message)
-
-                   SELECT CASE( TRIM(output_var % task) )
-
-                   CASE( 'interpolate_2d' ) 
-                   
-                      SELECT CASE( TRIM(output_var % kind) )
-                       
-                      CASE( 'init soil' )
-
-                         ALLOCATE( output_arr( 0:output_var % grid % nx,       &
-                                               0:output_var % grid % ny,       &
-                                               SIZE(output_var % grid % depths) ) )
-
-                      CASE ( 'surface forcing' )
-
-                         ALLOCATE( output_arr( 0:output_var % grid % nx,       &
-                                               0:output_var % grid % ny, 1 ) )
-
-                      CASE DEFAULT
-
-                          message = "'" // TRIM(output_var % kind) // "' is not a soil variable"
-                          CALL inifor_abort("main loop", message)
-
-                      END SELECT
- CALL run_control('time', 'alloc')
-
-                      CALL interpolate_2d(input_buffer(output_var % input_id) % array(:,:,:), &
-                              output_arr(:,:,:), output_var % intermediate_grid, output_var)
- CALL run_control('time', 'comp')
-
-
-                   CASE ( 'interpolate_3d' )
-
-                      ALLOCATE( output_arr( 0:output_var % grid % nx,          &
-                                            0:output_var % grid % ny,          &
-                                            1:output_var % grid % nz ) )
-
- CALL run_control('time', 'alloc')
-                      CALL interpolate_3d(                                     &
-                         input_buffer(output_var % input_id) % array(:,:,:),   &
-                         output_arr(:,:,:),                                    &
-                         output_var % intermediate_grid,                       &
-                         output_var % grid)
- CALL run_control('time', 'comp')
-
-                   CASE ( 'average profile' )
-
-                      ALLOCATE( output_arr( 0:output_var % grid % nx,          &
-                                            0:output_var % grid % ny,          &
-                                            1:output_var % grid % nz ) )
- CALL run_control('time', 'alloc')
+!-- Section 2.2: Interpolate each output variable of the group
+!------------------------------------------------------------------------------
+             DO  ivar = 1, group%nv
+
+                output_var => group%out_vars(ivar)
+
+                IF ( output_var%to_be_processed .AND.                          &
+                     iter .LE. output_var%nt )  THEN
+
+                   message = "Processing '" // TRIM( output_var%name ) //      &
+                             "' (" // TRIM( output_var%kind ) //               &
+                             "), iteration " // TRIM( str( iter ) ) //" of " //&
+                             TRIM( str( output_var%nt ) )
+                   CALL report( 'main loop', message )
+
+                   SELECT CASE( TRIM( output_var%task ) )
+
+!--                   2D horizontal interpolation
+                      CASE( 'interpolate_2d' ) 
                       
-                      CALL interp_average_profile(                             &
-                         input_buffer(output_var % input_id) % array(:,:,:),   &
-                         output_arr(0,0,:),                                    &
-                         output_var % averaging_grid)
-
-                      IF ( TRIM(output_var % name) ==                          &
-                           'surface_forcing_surface_pressure' )  THEN
-
-                         IF ( cfg % p0_is_set )  THEN
-                            output_arr(0,0,1) = p0
-                         ELSE
-                            CALL get_surface_pressure(                         &
-                               output_arr(0,0,:), rho_centre,                  &
-                               output_var % averaging_grid)
+                         SELECT CASE( TRIM( output_var%kind ) )
+                          
+                         CASE( 'init soil' )
+   
+                            ALLOCATE( output_arr(0:output_var%grid%nx,            &
+                                                 0:output_var%grid%ny,            &
+                                                 SIZE( output_var%grid%depths )) )
+   
+                         CASE ( 'surface forcing' )
+   
+                            ALLOCATE( output_arr(0:output_var%grid%nx,            &
+                                                 0:output_var%grid%ny, 1) )
+   
+                         CASE DEFAULT
+   
+                             message = "'" // TRIM( output_var%kind ) // "' is not a soil variable"
+                             CALL inifor_abort( "main loop", message )
+   
+                         END SELECT
+                         CALL log_runtime( 'time', 'alloc' )
+   
+                         CALL interpolate_2d( input_buffer(output_var%input_id)%array(:,:,:), &
+                                 output_arr(:,:,:), output_var%intermediate_grid, output_var )
+                         CALL log_runtime( 'time', 'comp' )
+   
+   
+!--                   Interpolation in 3D, used for atmospheric initial and
+!--                   boundary conditions.
+                      CASE ( 'interpolate_3d' )
+   
+                         ALLOCATE( output_arr(0:output_var%grid % nx,           &
+                                              0:output_var%grid % ny,           &
+                                              1:output_var%grid % nz) )
+   
+                         CALL log_runtime( 'time', 'alloc' )
+                         CALL interpolate_3d(                                     &
+                            input_buffer(output_var%input_id)%array(:,:,:),       &
+                            output_arr(:,:,:),                                    &
+                            output_var%intermediate_grid,                         &
+                            output_var%grid)
+                         CALL log_runtime( 'time', 'comp' )
+   
+!--                   Compute initial avaerage profiles (if --init-mode profile
+!--                   is used) 
+                      CASE ( 'average profile' )
+   
+                         ALLOCATE( output_arr(0:output_var%grid%nx,               &
+                                              0:output_var%grid%ny,               &
+                                              1:output_var%grid%nz) )
+                         CALL log_runtime( 'time', 'alloc' )
+                         
+                         CALL interp_average_profile(                             &
+                            input_buffer(output_var%input_id)%array(:,:,:),     &
+                            output_arr(0,0,:),                                    &
+                            output_var%averaging_grid )
+   
+                         IF ( TRIM( output_var%name ) ==                          &
+                              'surface_forcing_surface_pressure' )  THEN
+   
+                            IF ( cfg%p0_is_set )  THEN
+                               output_arr(0,0,1) = p0
+                            ELSE
+                               CALL get_surface_pressure(                         &
+                                  output_arr(0,0,:), rho_centre,                  &
+                                  output_var%averaging_grid )
+                            ENDIF
+   
                          ENDIF
-
-                      ENDIF
- CALL run_control('time', 'comp')
-
-                   CASE ( 'internal profile' )
-
-                      message = "Averaging of internal profile for variable '" //&
-                         TRIM(output_var % name) // "' is not supported."
-
-                      SELECT CASE (TRIM(output_var % name))
-
-                      CASE('internal_density_centre')
-                         ALLOCATE( rho_centre( 1:cosmo_grid % nz) )
-                         internal_arr => rho_centre
-
-                      CASE('internal_density_north')
-                         ALLOCATE( rho_north( 1:cosmo_grid % nz) )
-                         internal_arr => rho_north
-
-                      CASE('internal_density_south')
-                         ALLOCATE( rho_south( 1:cosmo_grid % nz) )
-                         internal_arr => rho_south
-
-                      CASE('internal_density_east')
-                         ALLOCATE( rho_east( 1:cosmo_grid % nz) )
-                         internal_arr => rho_east
-
-                      CASE('internal_density_west')
-                         ALLOCATE( rho_west( 1:cosmo_grid % nz) )
-                         internal_arr => rho_west
-
-                      CASE('internal_pressure_north')
-                         ALLOCATE( p_north( 1:cosmo_grid % nz) )
-                         internal_arr => p_north
-
-                      CASE('internal_pressure_south')
-                         ALLOCATE( p_south( 1:cosmo_grid % nz) )
-                         internal_arr => p_south
-
-                      CASE('internal_pressure_east')
-                         ALLOCATE( p_east( 1:cosmo_grid % nz) )
-                         internal_arr => p_east
-
-                      CASE('internal_pressure_west')
-                         ALLOCATE( p_west( 1:cosmo_grid % nz) )
-                         internal_arr => p_west
-
-                      CASE DEFAULT
-                         CALL inifor_abort('main loop', message)
-
-                      END SELECT
- CALL run_control('time', 'alloc')
-
-
-                      SELECT CASE( TRIM( output_var % name ) )
-
-                      CASE( 'internal_pressure_north',                         &
-                            'internal_pressure_south',                         &
-                            'internal_pressure_east',                          &
-                            'internal_pressure_west' )
-
-                         CALL average_pressure_perturbation(                   &
-                            input_buffer(output_var % input_id) % array(:,:,:),&
-                            internal_arr(:),                                   &
-                            cosmo_grid, output_var % averaging_grid            &
-                         )
-
-                      CASE DEFAULT
-
-                         CALL average_profile(                                 &
-                            input_buffer(output_var % input_id) % array(:,:,:),&
-                            internal_arr(:),                                   &
-                            output_var % averaging_grid                        &
-                         )
+                         CALL log_runtime( 'time', 'comp' )
+   
+!--                   Compute internal profiles, required for differentiation of
+!--                   geostrophic wind
+                      CASE ( 'internal profile' )
+   
+                         message = "Averaging of internal profile for variable '" //&
+                            TRIM( output_var%name ) // "' is not supported."
+   
+                         SELECT CASE ( TRIM( output_var%name ) )
+   
+                         CASE( 'internal_density_centre' )
+                            ALLOCATE( rho_centre(1:cosmo_grid%nz) )
+                            internal_arr => rho_centre
+   
+                         CASE( 'internal_density_north' )
+                            ALLOCATE( rho_north(1:cosmo_grid%nz) )
+                            internal_arr => rho_north
+   
+                         CASE( 'internal_density_south' )
+                            ALLOCATE( rho_south(1:cosmo_grid%nz) )
+                            internal_arr => rho_south
+   
+                         CASE( 'internal_density_east' )
+                            ALLOCATE( rho_east(1:cosmo_grid%nz) )
+                            internal_arr => rho_east
+   
+                         CASE( 'internal_density_west' )
+                            ALLOCATE( rho_west(1:cosmo_grid%nz) )
+                            internal_arr => rho_west
+   
+                         CASE( 'internal_pressure_north' )
+                            ALLOCATE( p_north(1:cosmo_grid%nz) )
+                            internal_arr => p_north
+   
+                         CASE( 'internal_pressure_south' )
+                            ALLOCATE( p_south(1:cosmo_grid%nz) )
+                            internal_arr => p_south
+   
+                         CASE( 'internal_pressure_east' )
+                            ALLOCATE( p_east(1:cosmo_grid%nz) )
+                            internal_arr => p_east
+   
+                         CASE( 'internal_pressure_west' )
+                            ALLOCATE( p_west(1:cosmo_grid%nz) )
+                            internal_arr => p_west
+   
+                         CASE DEFAULT
+                            CALL inifor_abort( 'main loop', message )
+   
+                         END SELECT
+                         CALL log_runtime( 'time', 'alloc' )
+   
+   
+                         SELECT CASE( TRIM( output_var%name ) )
+   
+                         CASE( 'internal_pressure_north',                         &
+                               'internal_pressure_south',                         &
+                               'internal_pressure_east',                          &
+                               'internal_pressure_west' )
+   
+                            CALL average_pressure_perturbation(                   &
+                               input_buffer(output_var%input_id) % array(:,:,:),&
+                               internal_arr(:),                                   &
+                               cosmo_grid, output_var%averaging_grid            &
+                            )
+   
+                         CASE DEFAULT
+   
+                            CALL average_profile(                                 &
+                               input_buffer(output_var%input_id) % array(:,:,:),&
+                               internal_arr(:),                                   &
+                               output_var%averaging_grid                        &
+                            )
 
                       END SELECT
@@ -360,8 +413,8 @@
 !--                   (requires definiton of COSMO levels in netCDF output.)
                       !IF (.TRUE.)  THEN
-                      !   ALLOCATE( output_arr(1,1,1:output_var % grid % nz) )
+                      !   ALLOCATE( output_arr(1,1,1:output_var%grid % nz) )
                       !   output_arr(1,1,:) = internal_arr(:)
                       !ENDIF
- CALL run_control('time', 'comp')
+                      CALL log_runtime( 'time', 'comp' )
 
 !
@@ -372,12 +425,12 @@
 
                       IF (.NOT. ug_vg_have_been_computed )  THEN
-                         ALLOCATE( ug_palm(output_var % grid % nz) )
-                         ALLOCATE( vg_palm(output_var % grid % nz) )
-                         ALLOCATE( ug_cosmo(cosmo_grid % nz) )
-                         ALLOCATE( vg_cosmo(cosmo_grid % nz) )
-
-                         IF ( cfg % ug_defined_by_user )  THEN
-                            ug_palm = cfg % ug
-                            vg_palm = cfg % vg
+                         ALLOCATE( ug_palm(output_var%grid%nz) )
+                         ALLOCATE( vg_palm(output_var%grid%nz) )
+                         ALLOCATE( ug_cosmo(cosmo_grid%nz) )
+                         ALLOCATE( vg_cosmo(cosmo_grid%nz) )
+
+                         IF ( cfg%ug_defined_by_user )  THEN
+                            ug_palm = cfg%ug
+                            vg_palm = cfg%vg
                          ELSE
                             CALL geostrophic_winds( p_north, p_south, p_east,  &
@@ -390,8 +443,8 @@
 
                             CALL interpolate_1d( ug_cosmo, ug_palm,             &
-                                                 output_var % grid )
+                                                 output_var%grid )
 
                             CALL interpolate_1d( vg_cosmo, vg_palm,             &
-                                                 output_var % grid )
+                                                 output_var%grid )
                          ENDIF
 
@@ -402,51 +455,40 @@
 !
 !--                   Select output array of current geostrophic wind component
-                      SELECT CASE(TRIM(output_var % name))
-                      CASE ('ls_forcing_ug')
+                      SELECT CASE( TRIM( output_var%name ) )
+                      CASE ( 'ls_forcing_ug' )
                          ug_vg_palm => ug_palm
-                      CASE ('ls_forcing_vg')
+                      CASE ( 'ls_forcing_vg' )
                          ug_vg_palm => vg_palm
                       END SELECT
 
-                      ALLOCATE( output_arr(1,1,output_var % grid % nz) )
+                      ALLOCATE( output_arr(1,1,output_var%grid%nz) )
                       output_arr(1,1,:) = ug_vg_palm(:)
 
-                   CASE ( 'average scalar' )
-
-                      ALLOCATE( output_arr(1,1,1) )
- CALL run_control('time', 'alloc')
-                      output_arr(1,1,1) = p0
- CALL run_control('time', 'comp')
-
+!--                User defined constant profiles
                    CASE ( 'set profile' )
                       
-                      ALLOCATE( output_arr( 1, 1, 1:nz ) )
- CALL run_control('time', 'alloc')
-
-                      SELECT CASE (TRIM(output_var % name))
-
-                      CASE('nudging_tau')
+                      ALLOCATE( output_arr(1,1,1:nz) )
+                      CALL log_runtime( 'time', 'alloc' )
+
+                      SELECT CASE ( TRIM( output_var%name ) )
+
+                      CASE ( 'nudging_tau' )
                           output_arr(1, 1, :) = NUDGING_TAU
 
                       CASE DEFAULT
-                          message = "'" // TRIM(output_var % name) //          &
-                             "' is not a valid '" // TRIM(output_var % kind) //&
+                          message = "'" // TRIM( output_var%name ) //          &
+                             "' is not a valid '" // TRIM( output_var%kind ) //&
                              "' variable kind."
-                          CALL inifor_abort('main loop', message)
+                          CALL inifor_abort( 'main loop', message )
                       END SELECT
- CALL run_control('time', 'comp')
-
-                   CASE('average large-scale profile')
-                      message = "Averaging of large-scale forcing profiles " //&
-                                "has not been implemented, yet."
-                      CALL inifor_abort('main loop', message)
+                      CALL log_runtime( 'time', 'comp' )
 
                    CASE DEFAULT
-                      message = "Processing task '" // TRIM(output_var % task) //&
+                      message = "Processing task '" // TRIM( output_var%task ) //&
                                "' not recognized."
-                      CALL inifor_abort('', message)
+                      CALL inifor_abort( '', message )
 
                    END SELECT
- CALL run_control('time', 'comp')
+                   CALL log_runtime( 'time', 'comp' )
 
 !------------------------------------------------------------------------------
@@ -458,16 +500,16 @@
 !--                defined on averaged COSMO levels instead of PALM levels
 !--                (requires definiton of COSMO levels in netCDF output.)
-                   !IF (.NOT. output_var % is_internal .OR. debugging_output)  THEN
-
-                   IF (.NOT. output_var % is_internal)  THEN
-                      message = "Writing variable '" // TRIM(output_var%name) // "'."
-                      CALL report('main loop', message)
-                      CALL update_output(output_var, output_arr, iter,         &
-                                         output_file, cfg)
- CALL run_control('time', 'write')
+                   !IF (.NOT. output_var%is_internal .OR. debugging_output)  THEN
+
+                   IF ( .NOT. output_var%is_internal )  THEN
+                      message = "Writing variable '" // TRIM( output_var%name ) // "'."
+                      CALL report( 'main loop', message )
+                      CALL update_output( output_var, output_arr, iter,        &
+                                          output_file, cfg )
+                      CALL log_runtime( 'time', 'write' )
                    ENDIF
 
-                   IF (ALLOCATED(output_arr))  DEALLOCATE(output_arr)
- CALL run_control('time', 'alloc')
+                   IF ( ALLOCATED( output_arr ) )  DEALLOCATE( output_arr )
+                   CALL log_runtime( 'time', 'alloc' )
 
                 ENDIF
@@ -478,5 +520,5 @@
 
              ug_vg_have_been_computed = .FALSE.
-             IF ( group % kind == 'thermodynamics' )  THEN
+             IF ( group%kind == 'thermodynamics' )  THEN
                 DEALLOCATE( rho_centre )
                 DEALLOCATE( ug_palm )
@@ -484,5 +526,5 @@
                 DEALLOCATE( ug_cosmo )
                 DEALLOCATE( vg_cosmo )
-                IF ( .NOT. cfg % ug_defined_by_user )  THEN
+                IF ( .NOT. cfg%ug_defined_by_user )  THEN
                    DEALLOCATE( rho_north )
                    DEALLOCATE( rho_south )
@@ -499,11 +541,11 @@
 !--          Keep input buffer around for averaged (radiation) and 
 !--          accumulated COSMO quantities (precipitation).
-             IF ( group % kind == 'running average' .OR. &
-                  group % kind == 'accumulated' )  THEN
+             IF ( group%kind == 'running average' .OR. &
+                  group%kind == 'accumulated' )  THEN
              ELSE
-                CALL report('main loop', 'Deallocating input buffer', cfg % debug)
-                DEALLOCATE(input_buffer)
+                CALL report( 'main loop', 'Deallocating input buffer', cfg%debug )
+                DEALLOCATE( input_buffer )
              ENDIF
- CALL run_control('time', 'alloc')
+             CALL log_runtime( 'time', 'alloc' )
 
 !
@@ -511,22 +553,22 @@
           ENDDO
 
-          IF (ALLOCATED(input_buffer))  THEN
-             CALL report('main loop', 'Deallocating input buffer', cfg % debug)
-             DEALLOCATE(input_buffer)
+          IF ( ALLOCATED( input_buffer ) )  THEN
+             CALL report( 'main loop', 'Deallocating input buffer', cfg%debug )
+             DEALLOCATE( input_buffer )
           ENDIF
- CALL run_control('time', 'alloc')
+          CALL log_runtime( 'time', 'alloc' )
 
        ELSE 
 
-          message = "Skipping IO group " // TRIM(str(igroup)) // " '" // TRIM(group % kind) // "'"
-          IF ( ALLOCATED(group % in_var_list) )  THEN
-              message = TRIM(message) // " with input variable '" //           &
-              TRIM(group % in_var_list(1) % name) // "'."
+          message = "Skipping IO group " // TRIM( str( igroup ) ) // " '" // TRIM( group%kind ) // "'"
+          IF ( ALLOCATED( group%in_var_list ) )  THEN
+              message = TRIM( message ) // " with input variable '" //         &
+              TRIM( group%in_var_list(1)%name ) // "'."
           ENDIF
 
-          CALL report('main loop', message, cfg % debug)
-
-!
-!--    IO group % to_be_processed conditional
+          CALL report( 'main loop', message, cfg%debug )
+
+!
+!--    IO group%to_be_processed conditional
        ENDIF
 
@@ -538,16 +580,27 @@
 !- Section 3: Clean up.
 !------------------------------------------------------------------------------
-    CALL fini_file_lists()
-    CALL fini_io_groups()
-    CALL fini_variables()
-    !CALL fini_grids()
- CALL run_control('time', 'alloc')
- CALL run_control('report', 'void')
-
-    message = "Finished writing dynamic driver '" // TRIM(output_file % name) // &
+    CALL fini_file_lists
+    CALL fini_io_groups
+    CALL fini_variables
+    !CALL fini_grids
+    CALL log_runtime( 'time', 'alloc' )
+    CALL log_runtime( 'report', 'void' )
+
+    message = "Finished writing dynamic driver '" // TRIM( output_file%name ) // &
               "' successfully."
-    CALL report('main loop', message)
-
-
+    CALL report( 'main loop', message )
+    CALL close_log
+
+#else
+
+    USE inifor_control
+    IMPLICIT NONE
+    
+    message = "INIFOR was compiled without netCDF support, which is required for it to run. "  //     &
+              "To use INIFOR, recompile PALM with netCDF support by adding the -D__netcdf " //        &
+              "precompiler flag to your .palm.config file."
+    CALL inifor_abort( 'main loop', message )
+ 
 #endif
+
  END PROGRAM inifor
Index: /palm/trunk/UTIL/inifor/src/inifor_control.f90
===================================================================
--- /palm/trunk/UTIL/inifor/src/inifor_control.f90	(revision 3865)
+++ /palm/trunk/UTIL/inifor/src/inifor_control.f90	(revision 3866)
@@ -26,4 +26,11 @@
 ! -----------------
 ! $Id$
+! Use PALM's working precision
+! Renamed run_control -> log_runtime
+! Open log file only once
+! Improved coding style
+! 
+! 
+! 3785 2019-03-06 10:41:14Z eckhard
 ! Added message buffer for displaying tips to rectify encountered errors
 ! 
@@ -66,10 +73,8 @@
 !> feedback to the terminal and a log file.
 !------------------------------------------------------------------------------!
-#if defined ( __netcdf )
  MODULE inifor_control
 
     USE inifor_defs,                                                           &
-        ONLY:  LNAME, dp, VERSION, COPYRIGHT
-
+        ONLY:  COPYRIGHT, LNAME, LOG_FILE_NAME, VERSION, wp
     USE inifor_util,                                                           &
         ONLY:  real_to_str, real_to_str_f
@@ -79,4 +84,5 @@
     CHARACTER (LEN=5000) ::  message = '' !< log message buffer
     CHARACTER (LEN=5000) ::  tip     = '' !< optional log message buffer for tips on how to rectify encountered errors
+    INTEGER, SAVE        ::  u            !< Fortran file unit for the log file
 
  CONTAINS
@@ -94,36 +100,30 @@
 !> to it.
 !------------------------------------------------------------------------------!
-    SUBROUTINE report(routine, message, debug)
-
-       CHARACTER(LEN=*), INTENT(IN)  ::  routine !< name of calling subroutine of function
-       CHARACTER(LEN=*), INTENT(IN)  ::  message !< log message
-       LOGICAL, OPTIONAL, INTENT(IN) ::  debug   !< flag the current message as debugging message
-
-       INTEGER                       ::  u                     !< Fortran file unit for the log file
-       LOGICAL, SAVE                 ::  is_first_run = .TRUE. !< control flag for file opening mode
-       LOGICAL                       ::  suppress_message      !< control falg for additional debugging log
-
-
-       IF ( is_first_run )  THEN
-          OPEN( NEWUNIT=u, FILE='inifor.log', STATUS='replace' )
-          is_first_run = .FALSE.
-       ELSE
-          OPEN( NEWUNIT=u, FILE='inifor.log', POSITION='append', STATUS='old' )
-       ENDIF
-          
-
-       suppress_message = .FALSE.
-       IF ( PRESENT(debug) )  THEN
-          IF ( .NOT. debug )  suppress_message = .TRUE.
-       ENDIF
-
-       IF ( .NOT. suppress_message )  THEN
-          PRINT *, "inifor: " // TRIM(message) // "  [ " // TRIM(routine) // " ]"
-          WRITE(u, *)  TRIM(message) // "  [ " // TRIM(routine) // " ]"
-       ENDIF
-
-       CLOSE(u)
-
-    END SUBROUTINE report
+ SUBROUTINE report(routine, message, debug)
+
+    CHARACTER(LEN=*), INTENT(IN)  ::  routine !< name of calling subroutine of function
+    CHARACTER(LEN=*), INTENT(IN)  ::  message !< log message
+    LOGICAL, OPTIONAL, INTENT(IN) ::  debug   !< flag the current message as debugging message
+
+    LOGICAL, SAVE                 ::  is_first_run = .TRUE. !< control flag for file opening mode
+    LOGICAL                       ::  suppress_message      !< control falg for additional debugging log
+
+    IF ( is_first_run )  THEN
+       OPEN( NEWUNIT=u, FILE=LOG_FILE_NAME, STATUS='replace' )
+       is_first_run = .FALSE.
+    ENDIF
+       
+
+    suppress_message = .FALSE.
+    IF ( PRESENT(debug) )  THEN
+       IF ( .NOT. debug )  suppress_message = .TRUE.
+    ENDIF
+
+    IF ( .NOT. suppress_message )  THEN
+       PRINT *, "inifor: " // TRIM(message) // "  [ " // TRIM(routine) // " ]"
+       WRITE(u, *)  TRIM(message) // "  [ " // TRIM(routine) // " ]"
+    ENDIF
+
+ END SUBROUTINE report
 
 
@@ -138,12 +138,12 @@
 !> continue.
 !------------------------------------------------------------------------------!
-    SUBROUTINE warn(routine, message)
-
-       CHARACTER(LEN=*), INTENT(IN) ::  routine !< name of calling subroutine or function
-       CHARACTER(LEN=*), INTENT(IN) ::  message !< log message
-
-       CALL report(routine, "WARNING: " // TRIM(message))
-
-    END SUBROUTINE warn
+ SUBROUTINE warn(routine, message)
+
+    CHARACTER(LEN=*), INTENT(IN) ::  routine !< name of calling subroutine or function
+    CHARACTER(LEN=*), INTENT(IN) ::  message !< log message
+
+    CALL report(routine, "WARNING: " // TRIM(message))
+
+ END SUBROUTINE warn
 
 
@@ -158,13 +158,21 @@
 !> INIFOR from continueing.
 !------------------------------------------------------------------------------!
-    SUBROUTINE inifor_abort(routine, message)
-
-       CHARACTER(LEN=*), INTENT(IN) ::  routine !< name of calling subroutine or function
-       CHARACTER(LEN=*), INTENT(IN) ::  message !< log message
-
-       CALL report(routine, "ERROR: " // TRIM(message) // " Stopping.")
-       STOP
-
-    END SUBROUTINE inifor_abort
+ SUBROUTINE inifor_abort(routine, message)
+
+    CHARACTER(LEN=*), INTENT(IN) ::  routine !< name of calling subroutine or function
+    CHARACTER(LEN=*), INTENT(IN) ::  message !< log message
+
+    CALL report(routine, "ERROR: " // TRIM(message) // " Stopping.")
+    CALL close_log
+    STOP
+
+ END SUBROUTINE inifor_abort
+
+
+ SUBROUTINE close_log()
+
+    CLOSE(u)
+
+ END SUBROUTINE close_log
 
 
@@ -175,98 +183,96 @@
 !> print_version() prints the INIFOR version number and copyright notice.
 !------------------------------------------------------------------------------!
-    SUBROUTINE print_version()
-       PRINT *, "INIFOR " // VERSION
-       PRINT *, COPYRIGHT
-    END SUBROUTINE print_version
-
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> 
-!> run_control() measures the run times of various parts of INIFOR and
+ SUBROUTINE print_version()
+    PRINT *, "INIFOR " // VERSION
+    PRINT *, COPYRIGHT
+ END SUBROUTINE print_version
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> 
+!> log_runtime() measures the run times of various parts of INIFOR and
 !> accumulates them in timing budgets.
 !------------------------------------------------------------------------------!
-    SUBROUTINE run_control(mode, budget)
-
-       CHARACTER(LEN=*), INTENT(IN) ::  mode   !< name of the calling mode
-       CHARACTER(LEN=*), INTENT(IN) ::  budget !< name of the timing budget
-
-       REAL(dp), SAVE ::  t0               !< begin of timing interval
-       REAL(dp), SAVE ::  t1               !< end of timing interval
-       REAL(dp), SAVE ::  t_comp  = 0.0_dp !< computation timing budget
-       REAL(dp), SAVE ::  t_alloc = 0.0_dp !< allocation timing budget
-       REAL(dp), SAVE ::  t_init  = 0.0_dp !< initialization timing budget
-       REAL(dp), SAVE ::  t_read  = 0.0_dp !< reading timing budget
-       REAL(dp), SAVE ::  t_total = 0.0_dp !< total time
-       REAL(dp), SAVE ::  t_write = 0.0_dp !< writing timing budget
-
-       CHARACTER(LEN=*), PARAMETER  ::  fmt='(F6.2)' !< floating-point output format
-
-
-       SELECT CASE(TRIM(mode))
-
-       CASE('init')
-          CALL CPU_TIME(t0)
-
-       CASE('time')
-
-          CALL CPU_TIME(t1)
-
-          SELECT CASE(TRIM(budget))
-
-             CASE('alloc')
-                t_alloc = t_alloc + t1 - t0
-
-             CASE('init')
-                t_init = t_init + t1 - t0
-
-             CASE('read')
-                t_read = t_read + t1 - t0
-
-             CASE('write')
-                t_write = t_write + t1 - t0
-
-             CASE('comp')
-                t_comp = t_comp + t1 - t0
-
-             CASE DEFAULT
-                CALL inifor_abort('run_control', "Time Budget '" // TRIM(mode) // "' is not supported.")
-
-          END SELECT
-
-          t0 = t1
-
-       CASE('report')
-           t_total = t_init + t_read + t_write + t_comp
-
-           CALL report('run_control', " *** CPU time ***")
-
-           CALL report('run_control', "Initialization: " // real_to_str(t_init)  // &
-                       " s (" // TRIM(real_to_str(100*t_init/t_total, fmt))      // " %)")
-
-           CALL report('run_control', "(De-)Allocation:" // real_to_str(t_alloc)  // &
-                       " s (" // TRIM(real_to_str(100*t_alloc/t_total, fmt))      // " %)")
-
-           CALL report('run_control', "Reading data:   " // real_to_str(t_read)  // &
-                       " s (" // TRIM(real_to_str(100*t_read/t_total, fmt))      // " %)")
-
-           CALL report('run_control', "Writing data:   " // real_to_str(t_write) // &
-                       " s (" // TRIM(real_to_str(100*t_write/t_total, fmt))     // " %)")
-
-           CALL report('run_control', "Computation:    " // real_to_str(t_comp)  // &
-                       " s (" // TRIM(real_to_str(100*t_comp/t_total, fmt))      // " %)")
-
-           CALL report('run_control', "Total:          " // real_to_str(t_total) // &
-                       " s (" // TRIM(real_to_str(100*t_total/t_total, fmt))     // " %)")
-
-       CASE DEFAULT
-          CALL inifor_abort('run_control', "Mode '" // TRIM(mode) // "' is not supported.")
+ SUBROUTINE log_runtime(mode, budget)
+
+    CHARACTER(LEN=*), INTENT(IN) ::  mode   !< name of the calling mode
+    CHARACTER(LEN=*), INTENT(IN) ::  budget !< name of the timing budget
+
+    REAL(wp), SAVE ::  t0               !< begin of timing interval
+    REAL(wp), SAVE ::  t1               !< end of timing interval
+    REAL(wp), SAVE ::  t_comp  = 0.0_wp !< computation timing budget
+    REAL(wp), SAVE ::  t_alloc = 0.0_wp !< allocation timing budget
+    REAL(wp), SAVE ::  t_init  = 0.0_wp !< initialization timing budget
+    REAL(wp), SAVE ::  t_read  = 0.0_wp !< reading timing budget
+    REAL(wp), SAVE ::  t_total = 0.0_wp !< total time
+    REAL(wp), SAVE ::  t_write = 0.0_wp !< writing timing budget
+
+    CHARACTER(LEN=*), PARAMETER  ::  fmt='(F6.2)' !< floating-point output format
+
+
+    SELECT CASE(TRIM(mode))
+
+    CASE('init')
+       CALL CPU_TIME(t0)
+
+    CASE('time')
+
+       CALL CPU_TIME(t1)
+
+       SELECT CASE(TRIM(budget))
+
+          CASE('alloc')
+             t_alloc = t_alloc + t1 - t0
+
+          CASE('init')
+             t_init = t_init + t1 - t0
+
+          CASE('read')
+             t_read = t_read + t1 - t0
+
+          CASE('write')
+             t_write = t_write + t1 - t0
+
+          CASE('comp')
+             t_comp = t_comp + t1 - t0
+
+          CASE DEFAULT
+             CALL inifor_abort('log_runtime', "Time Budget '" // TRIM(mode) // "' is not supported.")
 
        END SELECT
 
-    END SUBROUTINE run_control
+       t0 = t1
+
+    CASE('report')
+        t_total = t_init + t_read + t_write + t_comp
+
+        CALL report('log_runtime', " *** CPU time ***")
+
+        CALL report('log_runtime', "Initialization:  " // TRIM( real_to_str( t_init ) ) // &
+                    " s  (" // TRIM( real_to_str( 100 * t_init / t_total, fmt ) ) // " %)" )
+
+        CALL report('log_runtime', "(De-)Allocation: " // TRIM( real_to_str( t_alloc ) ) // &
+                    " s  (" // TRIM( real_to_str( 100 * t_alloc / t_total, fmt ) ) // " %)" )
+
+        CALL report('log_runtime', "Reading data:    " // TRIM( real_to_str( t_read ) )  // &
+                    " s  (" // TRIM( real_to_str( 100 * t_read / t_total, fmt ) ) // " %)" )
+
+        CALL report('log_runtime', "Writing data:    " // TRIM( real_to_str( t_write ) ) // &
+                    " s  (" // TRIM( real_to_str( 100 * t_write / t_total, fmt ) ) // " %)" )
+
+        CALL report('log_runtime', "Computation:     " // TRIM( real_to_str( t_comp ) )  // &
+                    " s  (" // TRIM( real_to_str( 100 * t_comp / t_total, fmt) ) // " %)" )
+
+        CALL report('log_runtime', "Total:           " // TRIM( real_to_str( t_total ) ) // &
+                    " s  (" // TRIM( real_to_str( 100 * t_total / t_total, fmt ) ) // " %)")
+
+    CASE DEFAULT
+       CALL inifor_abort('log_runtime', "Mode '" // TRIM(mode) // "' is not supported.")
+
+    END SELECT
+
+ END SUBROUTINE log_runtime
 
  END MODULE inifor_control
-#endif
-
Index: /palm/trunk/UTIL/inifor/src/inifor_defs.f90
===================================================================
--- /palm/trunk/UTIL/inifor/src/inifor_defs.f90	(revision 3865)
+++ /palm/trunk/UTIL/inifor/src/inifor_defs.f90	(revision 3866)
@@ -26,4 +26,9 @@
 ! -----------------
 ! $Id$
+! Added parameter for INIFOR's log file name
+! Use PALM's working precision
+! 
+! 
+! 3801 2019-03-15 17:14:25Z eckhard
 ! Defined netCDF variable names for COSMO grid
 ! Bumped version number
@@ -94,14 +99,13 @@
 !> The defs module provides global constants used in INIFOR.
 !------------------------------------------------------------------------------!
-#if defined ( __netcdf )
  MODULE inifor_defs
  
+ USE kinds,                                                                    &
+     ONLY :  wp, iwp
+
  IMPLICIT NONE
 
 ! 
 !-- Parameters for type definitions
- INTEGER, PARAMETER  ::  dp    = 8   !< double precision (8 bytes = 64 bits)
- INTEGER, PARAMETER  ::  sp    = 4   !< single precision (4 bytes = 32 bits)
- INTEGER, PARAMETER  ::  hp    = 2   !< half precision (2 bytes = 16 bits)
  INTEGER, PARAMETER  ::  PATH  = 140 !< length of file path strings
  INTEGER, PARAMETER  ::  LNAME = 150 !< length of long name strings
@@ -111,31 +115,31 @@
 ! 
 !-- Trigonomentry
- REAL(dp), PARAMETER ::  PI = 3.14159265358979323846264338_dp !< Ratio of a circle's circumference to its diamter [-]
- REAL(dp), PARAMETER ::  TO_RADIANS = PI / 180.0_dp           !< Conversion factor from degrees to radiant [-]
- REAL(dp), PARAMETER ::  TO_DEGREES = 180.0_dp / PI           !< Conversion factor from radians to degrees [-]
+ REAL(wp), PARAMETER ::  PI = 3.14159265358979323846264338_wp !< Ratio of a circle's circumference to its diamter [-]
+ REAL(wp), PARAMETER ::  TO_RADIANS = PI / 180.0_wp           !< Conversion factor from degrees to radiant [-]
+ REAL(wp), PARAMETER ::  TO_DEGREES = 180.0_wp / PI           !< Conversion factor from radians to degrees [-]
 
 ! 
 !-- COSMO parameters
  INTEGER, PARAMETER  ::  WATER_ID = 9                !< Integer corresponding to the water soil type in COSMO-DE [-]
- REAL(dp), PARAMETER ::  EARTH_RADIUS = 6371229.0_dp !< Earth radius used in COSMO-DE [m]
- REAL(dp), PARAMETER ::  P_SL = 1e5_dp               !< Reference pressure for computation of COSMO-DE's basic state pressure [Pa]
- REAL(dp), PARAMETER ::  T_SL = 288.15_dp            !< Reference temperature for computation of COSMO-DE's basic state pressure [K]
- REAL(dp), PARAMETER ::  BETA = 42.0_dp              !< logarithmic lapse rate, dT / d ln(p), for computation of COSMO-DE's basic
+ REAL(wp), PARAMETER ::  EARTH_RADIUS = 6371229.0_wp !< Earth radius used in COSMO-DE [m]
+ REAL(wp), PARAMETER ::  P_SL = 1e5_wp               !< Reference pressure for computation of COSMO-DE's basic state pressure [Pa]
+ REAL(wp), PARAMETER ::  T_SL = 288.15_wp            !< Reference temperature for computation of COSMO-DE's basic state pressure [K]
+ REAL(wp), PARAMETER ::  BETA = 42.0_wp              !< logarithmic lapse rate, dT / d ln(p), for computation of COSMO-DE's basic
                                                      !< state pressure [K]
- REAL(dp), PARAMETER ::  RD   = 287.05_dp            !< specific gas constant of dry air, used in computation of COSMO-DE's basic
+ REAL(wp), PARAMETER ::  RD   = 287.05_wp            !< specific gas constant of dry air, used in computation of COSMO-DE's basic
                                                      !< state [J/kg/K]
- REAL(dp), PARAMETER ::  RV   = 461.51_dp            !< specific gas constant of water vapor [J/kg/K]
- REAL(dp), PARAMETER ::  G    = 9.80665_dp           !< acceleration of Earth's gravity, used in computation of COSMO-DE's basic
+ REAL(wp), PARAMETER ::  RV   = 461.51_wp            !< specific gas constant of water vapor [J/kg/K]
+ REAL(wp), PARAMETER ::  G    = 9.80665_wp           !< acceleration of Earth's gravity, used in computation of COSMO-DE's basic
                                                      !< state [m/s/s]
- REAL(dp), PARAMETER ::  RHO_L = 1e3_dp              !< density of liquid water, used to convert W_SO from [kg/m^2] to [m^3/m^3],
+ REAL(wp), PARAMETER ::  RHO_L = 1e3_wp              !< density of liquid water, used to convert W_SO from [kg/m^2] to [m^3/m^3],
                                                      !< in [kg/m^3]
- REAL(dp), PARAMETER ::  HECTO = 100_dp              !< unit conversion factor from hPa to Pa
+ REAL(wp), PARAMETER ::  HECTO = 100_wp              !< unit conversion factor from hPa to Pa
 
 !
 !-- PALM-4U parameters
- REAL(dp), PARAMETER ::  OMEGA   = 7.29e-5_dp !< angular velocity of Earth's rotation [s^-1]
- REAL(dp), PARAMETER ::  P_REF   = 1e5_dp     !< Reference pressure for potential temperature [Pa]
- REAL(dp), PARAMETER ::  RD_PALM = 287.0_dp   !< specific gas constant of dry air, used in computation of PALM-4U's potential temperature [J/kg/K]
- REAL(dp), PARAMETER ::  CP_PALM = 1005.0_dp  !< heat capacity of dry air at constant pressure, used in computation of PALM-4U's potential temperature [J/kg/K]
+ REAL(wp), PARAMETER ::  OMEGA   = 7.29e-5_wp !< angular velocity of Earth's rotation [s^-1]
+ REAL(wp), PARAMETER ::  P_REF   = 1e5_wp     !< Reference pressure for potential temperature [Pa]
+ REAL(wp), PARAMETER ::  RD_PALM = 287.0_wp   !< specific gas constant of dry air, used in computation of PALM-4U's potential temperature [J/kg/K]
+ REAL(wp), PARAMETER ::  CP_PALM = 1005.0_wp  !< heat capacity of dry air at constant pressure, used in computation of PALM-4U's potential temperature [J/kg/K]
 
 ! 
@@ -154,9 +158,9 @@
                                                               !< water cells [-]
  INTEGER, PARAMETER          ::  FORCING_STEP = 1             !< Number of hours between forcing time steps [h]
- REAL(dp), PARAMETER         ::  NUDGING_TAU = 21600.0_dp     !< Nudging relaxation time scale [s]
- CHARACTER(LEN=*), PARAMETER ::  VERSION = '1.4.8'            !< INIFOR version number
+ REAL(wp), PARAMETER         ::  NUDGING_TAU = 21600.0_wp     !< Nudging relaxation time scale [s]
  CHARACTER(LEN=*), PARAMETER ::  COPYRIGHT = 'Copyright 2017-2019 Leibniz Universitaet Hannover' // &
-     ACHAR( 10 ) // ' Copyright 2017-2019 Deutscher Wetterdienst Offenbach' !< Copyright notice
-
+    ACHAR( 10 ) // ' Copyright 2017-2019 Deutscher Wetterdienst Offenbach' !< Copyright notice
+ CHARACTER(LEN=*), PARAMETER ::  LOG_FILE_NAME = 'inifor.log' !< Name of INIFOR's log file
+ CHARACTER(LEN=*), PARAMETER ::  VERSION = '1.4.9rc'          !< INIFOR version number
+ 
  END MODULE inifor_defs
-#endif
Index: /palm/trunk/UTIL/inifor/src/inifor_grid.f90
===================================================================
--- /palm/trunk/UTIL/inifor/src/inifor_grid.f90	(revision 3865)
+++ /palm/trunk/UTIL/inifor/src/inifor_grid.f90	(revision 3866)
@@ -26,4 +26,10 @@
 ! -----------------
 ! $Id$
+! Use PALM's working precision
+! Catch errors while reading namelists
+! Improved coding style
+!
+! 
+! 3802 2019-03-17 13:33:42Z raasch
 ! unused variable removed
 ! 
@@ -131,8 +137,8 @@
     USE inifor_control
     USE inifor_defs,                                                           &
-        ONLY:  DATE, EARTH_RADIUS, TO_RADIANS, TO_DEGREES, PI, dp, hp, sp,     &
+        ONLY:  DATE, EARTH_RADIUS, TO_RADIANS, TO_DEGREES, PI,                 &
                SNAME, LNAME, PATH, FORCING_STEP, WATER_ID, FILL_ITERATIONS,    &
                BETA, P_SL, T_SL, BETA, RD, RV, G, P_REF, RD_PALM, CP_PALM,     &
-               RHO_L, OMEGA, HECTO
+               RHO_L, OMEGA, HECTO, wp, iwp
     USE inifor_io,                                                             &
         ONLY:  get_cosmo_grid, get_netcdf_attribute, get_netcdf_dim_vector,    &
@@ -140,5 +146,5 @@
                get_input_file_list, validate_config
     USE inifor_transform,                                                      &
-        ONLY:  average_2d, rotate_to_cosmo, find_horizontal_neighbours,&
+        ONLY:  average_2d, rotate_to_cosmo, find_horizontal_neighbours,        &
                compute_horizontal_interp_weights,                              &
                find_vertical_neighbours_and_weights_interp,                    &
@@ -156,81 +162,82 @@
     SAVE
     
-    REAL(dp) ::  averaging_angle   = 0.0_dp       !< latitudal and longitudal width of averaging regions [rad]
-    REAL(dp) ::  averaging_width_ns = 0.0_dp       !< longitudal width of averaging regions [m]
-    REAL(dp) ::  averaging_width_ew = 0.0_dp       !< latitudal width of averaging regions [m]
-    REAL(dp) ::  phi_equat         = 0.0_dp       !< latitude of rotated equator of COSMO-DE grid [rad]
-    REAL(dp) ::  phi_n             = 0.0_dp       !< latitude of rotated pole of COSMO-DE grid [rad]
-    REAL(dp) ::  lambda_n          = 0.0_dp       !< longitude of rotaded pole of COSMO-DE grid [rad]
-    REAL(dp) ::  phi_c             = 0.0_dp       !< rotated-grid latitude of the center of the PALM domain [rad]
-    REAL(dp) ::  lambda_c          = 0.0_dp       !< rotated-grid longitude of the centre of the PALM domain [rad]
-    REAL(dp) ::  phi_cn            = 0.0_dp       !< latitude of the rotated pole relative to the COSMO-DE grid [rad]
-    REAL(dp) ::  lambda_cn         = 0.0_dp       !< longitude of the rotated pole relative to the COSMO-DE grid [rad]
-    REAL(dp) ::  lam_centre        = 0.0_dp       !< longitude of the PLAM domain centre in the source (COSMO rotated-pole) system [rad]
-    REAL(dp) ::  phi_centre        = 0.0_dp       !< latitude of the PLAM domain centre in the source (COSMO rotated-pole) system [rad]
-    REAL(dp) ::  lam_east          = 0.0_dp       !< longitude of the east central-averaging-domain boundary in the source (COSMO rotated-pole) system [rad]
-    REAL(dp) ::  lam_west          = 0.0_dp       !< longitude of the west central-averaging-domain boundary in the source (COSMO rotated-pole) system [rad]
-    REAL(dp) ::  phi_north         = 0.0_dp       !< latitude of the north central-averaging-domain boundary in the source (COSMO rotated-pole) system [rad]
-    REAL(dp) ::  phi_south         = 0.0_dp       !< latitude of the south central-averaging-domain boundary in the source (COSMO rotated-pole) system [rad]
-    REAL(dp) ::  gam               = 0.0_dp       !< angle for working around phirot2phi/rlarot2rla bug
-    REAL(dp) ::  dx                = 0.0_dp       !< PALM-4U grid spacing in x direction [m]
-    REAL(dp) ::  dy                = 0.0_dp       !< PALM-4U grid spacing in y direction [m]
-    REAL(dp) ::  dz(10)            = -1.0_dp      !< PALM-4U grid spacing in z direction [m]
-    REAL(dp) ::  dz_max            = 1000.0_dp    !< maximum vertical grid spacing [m]
-    REAL(dp) ::  dz_stretch_factor = 1.08_dp      !< factor for vertical grid stretching [m]
-    REAL(dp) ::  dz_stretch_level  = -9999999.9_dp!< height above which the vertical grid will be stretched [m]
-    REAL(dp) ::  dz_stretch_level_start(9) = -9999999.9_dp !< namelist parameter
-    REAL(dp) ::  dz_stretch_level_end(9) = 9999999.9_dp !< namelist parameter
-    REAL(dp) ::  dz_stretch_factor_array(9) = 1.08_dp !< namelist parameter
-    REAL(dp) ::  dxi               = 0.0_dp       !< inverse PALM-4U grid spacing in x direction [m^-1]
-    REAL(dp) ::  dyi               = 0.0_dp       !< inverse PALM-4U grid spacing in y direction [m^-1]
-    REAL(dp) ::  dzi               = 0.0_dp       !< inverse PALM-4U grid spacing in z direction [m^-1]
-    REAL(dp) ::  f3                = 0.0_dp       !< Coriolis parameter
-    REAL(dp) ::  lx                = 0.0_dp       !< PALM-4U domain size in x direction [m]
-    REAL(dp) ::  ly                = 0.0_dp       !< PALM-4U domain size in y direction [m]
-    REAL(dp) ::  p0                = 0.0_dp       !< PALM-4U surface pressure, at z0 [Pa]
-    REAL(dp) ::  x0                = 0.0_dp       !< x coordinate of PALM-4U Earth tangent [m] 
-    REAL(dp) ::  y0                = 0.0_dp       !< y coordinate of PALM-4U Earth tangent [m] 
-    REAL(dp) ::  z0                = 0.0_dp       !< Elevation of the PALM-4U domain above sea level [m]
-    REAL(dp) ::  z_top             = 0.0_dp       !< height of the scalar top boundary [m]
-    REAL(dp) ::  zw_top            = 0.0_dp       !< height of the vertical velocity top boundary [m]
-    REAL(dp) ::  lonmin_cosmo      = 0.0_dp       !< Minimunm longitude of COSMO-DE's rotated-pole grid [COSMO rotated-pole rad]
-    REAL(dp) ::  lonmax_cosmo      = 0.0_dp       !< Maximum longitude of COSMO-DE's rotated-pole grid [COSMO rotated-pole rad]
-    REAL(dp) ::  latmin_cosmo      = 0.0_dp       !< Minimunm latitude of COSMO-DE's rotated-pole grid [COSMO rotated-pole rad]
-    REAL(dp) ::  latmax_cosmo      = 0.0_dp       !< Maximum latitude of COSMO-DE's rotated-pole grid [COSMO rotated-pole rad]
-    REAL(dp) ::  lonmin_palm       = 0.0_dp       !< Minimunm longitude of PALM grid [COSMO rotated-pole rad]
-    REAL(dp) ::  lonmax_palm       = 0.0_dp       !< Maximum longitude of PALM grid [COSMO rotated-pole rad]
-    REAL(dp) ::  latmin_palm       = 0.0_dp       !< Minimunm latitude of PALM grid [COSMO rotated-pole rad]
-    REAL(dp) ::  latmax_palm       = 0.0_dp       !< Maximum latitude of PALM grid [COSMO rotated-pole rad]
-    REAL(dp) ::  lonmin_tot        = 0.0_dp       !< Minimunm longitude of required COSMO data [COSMO rotated-pole rad]
-    REAL(dp) ::  lonmax_tot        = 0.0_dp       !< Maximum longitude of required COSMO data [COSMO rotated-pole rad]
-    REAL(dp) ::  latmin_tot        = 0.0_dp       !< Minimunm latitude of required COSMO data [COSMO rotated-pole rad]
-    REAL(dp) ::  latmax_tot        = 0.0_dp       !< Maximum latitude of required COSMO data [COSMO rotated-pole rad]
-    REAL(dp) ::  latitude          = 0.0_dp       !< geographical latitude of the PALM-4U origin, from inipar namelist [deg]
-    REAL(dp) ::  longitude         = 0.0_dp       !< geographical longitude of the PALM-4U origin, from inipar namelist [deg]
-    REAL(dp) ::  origin_lat        = 0.0_dp       !< geographical latitude of the PALM-4U origin, from static driver netCDF file [deg]
-    REAL(dp) ::  origin_lon        = 0.0_dp       !< geographical longitude of the PALM-4U origin, from static driver netCDF file [deg]
-    REAL(dp) ::  rotation_angle    = 0.0_dp       !< clockwise angle the PALM-4U north is rotated away from geographical north [deg]
-    REAL(dp) ::  end_time          = 0.0_dp       !< PALM-4U simulation time [s]
-
-    REAL(dp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  hhl             !< heights of half layers (cell faces) above sea level in COSMO-DE, read in from external file
-    REAL(dp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  hfl             !< heights of full layers (cell centres) above sea level in COSMO-DE, computed as arithmetic average of hhl
-    REAL(dp), DIMENSION(:), ALLOCATABLE, TARGET     ::  depths          !< COSMO-DE's TERRA-ML soil layer depths
-    REAL(dp), DIMENSION(:), ALLOCATABLE, TARGET     ::  d_depth         !< COSMO-DE's TERRA-ML soil layer thicknesses
-    REAL(dp), DIMENSION(:), ALLOCATABLE, TARGET     ::  d_depth_rho_inv !< inverted soil water mass
-    REAL(dp), DIMENSION(:), ALLOCATABLE, TARGET     ::  rlon            !< longitudes of COSMO-DE's rotated-pole grid
-    REAL(dp), DIMENSION(:), ALLOCATABLE, TARGET     ::  rlat            !< latitudes of COSMO-DE's rotated-pole grid
-    REAL(dp), DIMENSION(:), ALLOCATABLE, TARGET     ::  time            !< output times 
-    REAL(dp), DIMENSION(:), ALLOCATABLE, TARGET     ::  x               !< base palm grid x coordinate vector pointed to by grid_definitions
-    REAL(dp), DIMENSION(:), ALLOCATABLE, TARGET     ::  xu              !< base palm grid xu coordinate vector pointed to by grid_definitions
-    REAL(dp), DIMENSION(:), ALLOCATABLE, TARGET     ::  y               !< base palm grid y coordinate vector pointed to by grid_definitions
-    REAL(dp), DIMENSION(:), ALLOCATABLE, TARGET     ::  yv              !< base palm grid yv coordinate vector pointed to by grid_definitions
-    REAL(dp), DIMENSION(:), ALLOCATABLE, TARGET     ::  z_column        !< base palm grid z coordinate vector including the top boundary coordinate (entire column)
-    REAL(dp), DIMENSION(:), ALLOCATABLE, TARGET     ::  zw_column       !< base palm grid zw coordinate vector including the top boundary coordinate (entire column)
-    REAL(dp), DIMENSION(:), ALLOCATABLE, TARGET     ::  z               !< base palm grid z coordinate vector pointed to by grid_definitions
-    REAL(dp), DIMENSION(:), ALLOCATABLE, TARGET     ::  zw              !< base palm grid zw coordinate vector pointed to by grid_definitions
-
-    INTEGER(hp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  soiltyp      !< COSMO-DE soil type map
+    REAL(wp) ::  averaging_angle   = 0.0_wp       !< latitudal and longitudal width of averaging regions [rad]
+    REAL(wp) ::  averaging_width_ns = 0.0_wp       !< longitudal width of averaging regions [m]
+    REAL(wp) ::  averaging_width_ew = 0.0_wp       !< latitudal width of averaging regions [m]
+    REAL(wp) ::  phi_equat         = 0.0_wp       !< latitude of rotated equator of COSMO-DE grid [rad]
+    REAL(wp) ::  phi_n             = 0.0_wp       !< latitude of rotated pole of COSMO-DE grid [rad]
+    REAL(wp) ::  lambda_n          = 0.0_wp       !< longitude of rotaded pole of COSMO-DE grid [rad]
+    REAL(wp) ::  phi_c             = 0.0_wp       !< rotated-grid latitude of the center of the PALM domain [rad]
+    REAL(wp) ::  lambda_c          = 0.0_wp       !< rotated-grid longitude of the centre of the PALM domain [rad]
+    REAL(wp) ::  phi_cn            = 0.0_wp       !< latitude of the rotated pole relative to the COSMO-DE grid [rad]
+    REAL(wp) ::  lambda_cn         = 0.0_wp       !< longitude of the rotated pole relative to the COSMO-DE grid [rad]
+    REAL(wp) ::  lam_centre        = 0.0_wp       !< longitude of the PLAM domain centre in the source (COSMO rotated-pole) system [rad]
+    REAL(wp) ::  phi_centre        = 0.0_wp       !< latitude of the PLAM domain centre in the source (COSMO rotated-pole) system [rad]
+    REAL(wp) ::  lam_east          = 0.0_wp       !< longitude of the east central-averaging-domain boundary in the source (COSMO rotated-pole) system [rad]
+    REAL(wp) ::  lam_west          = 0.0_wp       !< longitude of the west central-averaging-domain boundary in the source (COSMO rotated-pole) system [rad]
+    REAL(wp) ::  phi_north         = 0.0_wp       !< latitude of the north central-averaging-domain boundary in the source (COSMO rotated-pole) system [rad]
+    REAL(wp) ::  phi_south         = 0.0_wp       !< latitude of the south central-averaging-domain boundary in the source (COSMO rotated-pole) system [rad]
+    REAL(wp) ::  gam               = 0.0_wp       !< angle for working around phirot2phi/rlarot2rla bug
+    REAL(wp) ::  dx                = 0.0_wp       !< PALM-4U grid spacing in x direction [m]
+    REAL(wp) ::  dy                = 0.0_wp       !< PALM-4U grid spacing in y direction [m]
+    REAL(wp) ::  dz(10)            = -1.0_wp      !< PALM-4U grid spacing in z direction [m]
+    REAL(wp) ::  dz_max            = 1000.0_wp    !< maximum vertical grid spacing [m]
+    REAL(wp) ::  dz_stretch_factor = 1.08_wp      !< factor for vertical grid stretching [m]
+    REAL(wp) ::  dz_stretch_level  = -9999999.9_wp!< height above which the vertical grid will be stretched [m]
+    REAL(wp) ::  dz_stretch_level_start(9) = -9999999.9_wp !< namelist parameter
+    REAL(wp) ::  dz_stretch_level_end(9) = 9999999.9_wp !< namelist parameter
+    REAL(wp) ::  dz_stretch_factor_array(9) = 1.08_wp !< namelist parameter
+    REAL(wp) ::  dxi               = 0.0_wp       !< inverse PALM-4U grid spacing in x direction [m^-1]
+    REAL(wp) ::  dyi               = 0.0_wp       !< inverse PALM-4U grid spacing in y direction [m^-1]
+    REAL(wp) ::  dzi               = 0.0_wp       !< inverse PALM-4U grid spacing in z direction [m^-1]
+    REAL(wp) ::  f3                = 0.0_wp       !< Coriolis parameter
+    REAL(wp) ::  lx                = 0.0_wp       !< PALM-4U domain size in x direction [m]
+    REAL(wp) ::  ly                = 0.0_wp       !< PALM-4U domain size in y direction [m]
+    REAL(wp) ::  p0                = 0.0_wp       !< PALM-4U surface pressure, at z0 [Pa]
+    REAL(wp) ::  x0                = 0.0_wp       !< x coordinate of PALM-4U Earth tangent [m] 
+    REAL(wp) ::  y0                = 0.0_wp       !< y coordinate of PALM-4U Earth tangent [m] 
+    REAL(wp) ::  z0                = 0.0_wp       !< Elevation of the PALM-4U domain above sea level [m]
+    REAL(wp) ::  z_top             = 0.0_wp       !< height of the scalar top boundary [m]
+    REAL(wp) ::  zw_top            = 0.0_wp       !< height of the vertical velocity top boundary [m]
+    REAL(wp) ::  lonmin_cosmo      = 0.0_wp       !< Minimunm longitude of COSMO-DE's rotated-pole grid [COSMO rotated-pole rad]
+    REAL(wp) ::  lonmax_cosmo      = 0.0_wp       !< Maximum longitude of COSMO-DE's rotated-pole grid [COSMO rotated-pole rad]
+    REAL(wp) ::  latmin_cosmo      = 0.0_wp       !< Minimunm latitude of COSMO-DE's rotated-pole grid [COSMO rotated-pole rad]
+    REAL(wp) ::  latmax_cosmo      = 0.0_wp       !< Maximum latitude of COSMO-DE's rotated-pole grid [COSMO rotated-pole rad]
+    REAL(wp) ::  lonmin_palm       = 0.0_wp       !< Minimunm longitude of PALM grid [COSMO rotated-pole rad]
+    REAL(wp) ::  lonmax_palm       = 0.0_wp       !< Maximum longitude of PALM grid [COSMO rotated-pole rad]
+    REAL(wp) ::  latmin_palm       = 0.0_wp       !< Minimunm latitude of PALM grid [COSMO rotated-pole rad]
+    REAL(wp) ::  latmax_palm       = 0.0_wp       !< Maximum latitude of PALM grid [COSMO rotated-pole rad]
+    REAL(wp) ::  lonmin_tot        = 0.0_wp       !< Minimunm longitude of required COSMO data [COSMO rotated-pole rad]
+    REAL(wp) ::  lonmax_tot        = 0.0_wp       !< Maximum longitude of required COSMO data [COSMO rotated-pole rad]
+    REAL(wp) ::  latmin_tot        = 0.0_wp       !< Minimunm latitude of required COSMO data [COSMO rotated-pole rad]
+    REAL(wp) ::  latmax_tot        = 0.0_wp       !< Maximum latitude of required COSMO data [COSMO rotated-pole rad]
+    REAL(wp) ::  latitude          = 0.0_wp       !< geographical latitude of the PALM-4U origin, from inipar namelist [deg]
+    REAL(wp) ::  longitude         = 0.0_wp       !< geographical longitude of the PALM-4U origin, from inipar namelist [deg]
+    REAL(wp) ::  origin_lat        = 0.0_wp       !< geographical latitude of the PALM-4U origin, from static driver netCDF file [deg]
+    REAL(wp) ::  origin_lon        = 0.0_wp       !< geographical longitude of the PALM-4U origin, from static driver netCDF file [deg]
+    REAL(wp) ::  rotation_angle    = 0.0_wp       !< clockwise angle the PALM-4U north is rotated away from geographical north [deg]
+    REAL(wp) ::  end_time          = 0.0_wp       !< PALM-4U simulation time [s]
+
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  hhl             !< heights of half layers (cell faces) above sea level in COSMO-DE, read in from external file
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  hfl             !< heights of full layers (cell centres) above sea level in COSMO-DE, computed as arithmetic average of hhl
+    REAL(wp), DIMENSION(:), ALLOCATABLE, TARGET     ::  depths          !< COSMO-DE's TERRA-ML soil layer depths
+    REAL(wp), DIMENSION(:), ALLOCATABLE, TARGET     ::  d_depth         !< COSMO-DE's TERRA-ML soil layer thicknesses
+    REAL(wp), DIMENSION(:), ALLOCATABLE, TARGET     ::  d_depth_rho_inv !< inverted soil water mass
+    REAL(wp), DIMENSION(:), ALLOCATABLE, TARGET     ::  rlon            !< longitudes of COSMO-DE's rotated-pole grid
+    REAL(wp), DIMENSION(:), ALLOCATABLE, TARGET     ::  rlat            !< latitudes of COSMO-DE's rotated-pole grid
+    REAL(wp), DIMENSION(:), ALLOCATABLE, TARGET     ::  time            !< output times 
+    REAL(wp), DIMENSION(:), ALLOCATABLE, TARGET     ::  x               !< base palm grid x coordinate vector pointed to by grid_definitions
+    REAL(wp), DIMENSION(:), ALLOCATABLE, TARGET     ::  xu              !< base palm grid xu coordinate vector pointed to by grid_definitions
+    REAL(wp), DIMENSION(:), ALLOCATABLE, TARGET     ::  y               !< base palm grid y coordinate vector pointed to by grid_definitions
+    REAL(wp), DIMENSION(:), ALLOCATABLE, TARGET     ::  yv              !< base palm grid yv coordinate vector pointed to by grid_definitions
+    REAL(wp), DIMENSION(:), ALLOCATABLE, TARGET     ::  z_column        !< base palm grid z coordinate vector including the top boundary coordinate (entire column)
+    REAL(wp), DIMENSION(:), ALLOCATABLE, TARGET     ::  zw_column       !< base palm grid zw coordinate vector including the top boundary coordinate (entire column)
+    REAL(wp), DIMENSION(:), ALLOCATABLE, TARGET     ::  z               !< base palm grid z coordinate vector pointed to by grid_definitions
+    REAL(wp), DIMENSION(:), ALLOCATABLE, TARGET     ::  zw              !< base palm grid zw coordinate vector pointed to by grid_definitions
+
+    INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  soiltyp     !< COSMO-DE soil type map
     INTEGER ::  dz_stretch_level_end_index(9)               !< vertical grid level index until which the vertical grid spacing is stretched
     INTEGER ::  dz_stretch_level_start_index(9)             !< vertical grid level index above which the vertical grid spacing is stretched
+    INTEGER ::  iostat !< return status of READ statement
     INTEGER ::  nt    !< number of output time steps
     INTEGER ::  nx    !< number of PALM-4U grid points in x direction
@@ -355,5 +362,5 @@
 !> interplation grids later in setup_grids().
 !------------------------------------------------------------------------------!
-    SUBROUTINE setup_parameters()
+ SUBROUTINE setup_parameters()
 
 !
@@ -361,44 +368,44 @@
 ! Section 1: Define default parameters
 !------------------------------------------------------------------------------
-       cfg % start_date = '2013072100'
-       end_hour = 2
-       start_hour_soil = -2
-       start_hour_soilmoisture = - (4 * 7 * 24) - 2
-
-!
-!--    Defaultmain centre (_c) of the PALM-4U grid in the geographical system (_g)
-       origin_lat = 52.325079_dp * TO_RADIANS ! south-west of Berlin, origin used for the Dec 2017 showcase simulation
-       origin_lon = 13.082744_dp * TO_RADIANS
-       cfg % z0 = 35.0_dp
-
-!
-!--    Default atmospheric parameters
-       cfg % ug = 0.0_dp
-       cfg % vg = 0.0_dp
-       cfg % p0 = P_SL
-
-!
-!--    Parameters for file names
-       start_hour_flow = 0
-       start_hour_soil = 0
-       start_hour_radiation = 0
-       start_hour_soilmoisture = start_hour_flow - 2
-       end_hour_soilmoisture = start_hour_flow
-       step_hour = FORCING_STEP
-
-       input_prefix = 'laf'
-       cfg % flow_prefix = input_prefix
-       cfg % input_prefix = input_prefix
-       cfg % soil_prefix = input_prefix
-       cfg % radiation_prefix = input_prefix
-       cfg % soilmoisture_prefix  = input_prefix
-
-       flow_suffix = '-flow'
-       soil_suffix = '-soil'
-       radiation_suffix = '-rad'
-       soilmoisture_suffix = '-soilmoisture'
-
-       cfg % debug = .FALSE.
-       cfg % averaging_angle = 2.0_dp
+    cfg%start_date = '2013072100'
+    end_hour = 2
+    start_hour_soil = -2
+    start_hour_soilmoisture = - (4 * 7 * 24) - 2
+
+!
+!-- Defaultmain centre (_c) of the PALM-4U grid in the geographical system (_g)
+    origin_lat = 52.325079_wp * TO_RADIANS ! south-west of Berlin, origin used for the Dec 2017 showcase simulation
+    origin_lon = 13.082744_wp * TO_RADIANS
+    cfg%z0 = 35.0_wp
+
+!
+!-- Default atmospheric parameters
+    cfg%ug = 0.0_wp
+    cfg%vg = 0.0_wp
+    cfg%p0 = P_SL
+
+!
+!-- Parameters for file names
+    start_hour_flow = 0
+    start_hour_soil = 0
+    start_hour_radiation = 0
+    start_hour_soilmoisture = start_hour_flow - 2
+    end_hour_soilmoisture = start_hour_flow
+    step_hour = FORCING_STEP
+
+    input_prefix = 'laf'
+    cfg%flow_prefix = input_prefix
+    cfg%input_prefix = input_prefix
+    cfg%soil_prefix = input_prefix
+    cfg%radiation_prefix = input_prefix
+    cfg%soilmoisture_prefix  = input_prefix
+
+    flow_suffix = '-flow'
+    soil_suffix = '-soil'
+    radiation_suffix = '-rad'
+    soilmoisture_suffix = '-soilmoisture'
+
+    cfg%debug = .FALSE.
+    cfg%averaging_angle = 2.0_wp
 !
 !------------------------------------------------------------------------------
@@ -407,88 +414,102 @@
 
 !
-!--    Set default paths and modes
-       cfg % input_path         = './'
-       cfg % hhl_file           = ''
-       cfg % soiltyp_file       = ''
-       cfg % namelist_file      = './namelist'
-       cfg % static_driver_file = ''
-       cfg % output_file = './palm-4u-input.nc'
-       cfg % ic_mode = 'volume'
-       cfg % bc_mode = 'real'
-       cfg % averaging_mode = 'level'
-
-!
-!--    Overwrite defaults with user configuration
-       CALL parse_command_line_arguments( cfg )
-       CALL report('main_loop', 'Running INIFOR version ' // VERSION)
-
-       flow_prefix = TRIM(cfg % input_prefix)
-       radiation_prefix = TRIM(cfg % input_prefix)
-       soil_prefix = TRIM(cfg % input_prefix)
-       soilmoisture_prefix = TRIM(cfg % input_prefix)
-       IF (cfg % flow_prefix_is_set)  flow_prefix = TRIM(cfg % flow_prefix)
-       IF (cfg % radiation_prefix_is_set)  radiation_prefix = TRIM(cfg % radiation_prefix)
-       IF (cfg % soil_prefix_is_set)  soil_prefix = TRIM(cfg % soil_prefix)
-       IF (cfg % soilmoisture_prefix_is_set)  soilmoisture_prefix = TRIM(cfg % soilmoisture_prefix)
-
-       output_file % name = cfg % output_file
-       z0 = cfg % z0
-       p0 = cfg % p0
-
-       init_variables_required = .TRUE.
-       boundary_variables_required = TRIM( cfg % bc_mode ) == 'real'
-       ls_forcing_variables_required = TRIM( cfg % bc_mode ) == 'ideal'
-       surface_forcing_required = .FALSE.
-
-       IF ( ls_forcing_variables_required )  THEN
-          message = "Averaging of large-scale forcing profiles " //            &
-                    "has not been implemented, yet."
-          CALL inifor_abort('setup_parameters', message)
-       ENDIF
-
-!
-!--    Set default file paths, if not specified by user.
-       CALL normalize_path(cfg % input_path)
-       IF (TRIM(cfg % hhl_file) == '')  cfg % hhl_file = TRIM(cfg % input_path) // 'hhl.nc'
-       IF (TRIM(cfg % soiltyp_file) == '')  cfg % soiltyp_file = TRIM(cfg % input_path) // 'soil.nc'
-
-       CALL validate_config( cfg ) 
-
-       CALL report('setup_parameters', "initialization mode: " // TRIM(cfg % ic_mode))
-       CALL report('setup_parameters', "       forcing mode: " // TRIM(cfg % bc_mode))
-       CALL report('setup_parameters', "     averaging mode: " // TRIM(cfg % averaging_mode))
-       CALL report('setup_parameters', "    averaging angle: " // real_to_str(cfg % averaging_angle))
-       CALL report('setup_parameters', "          data path: " // TRIM(cfg % input_path))
-       CALL report('setup_parameters', "           hhl file: " // TRIM(cfg % hhl_file))
-       CALL report('setup_parameters', "       soiltyp file: " // TRIM(cfg % soiltyp_file))
-       CALL report('setup_parameters', "      namelist file: " // TRIM(cfg % namelist_file))
-       CALL report('setup_parameters', "   output data file: " // TRIM(output_file % name))
-       IF (cfg % debug )  CALL report('setup_parameters', "     debugging mode: enabled")
-
- CALL run_control('time', 'init')
- !
- !--   Read in namelist parameters
-       OPEN(10, FILE=cfg % namelist_file)
-       READ(10, NML=inipar) ! nx, ny, nz, dx, dy, dz
-       READ(10, NML=d3par)  ! end_time
+!-- Set default paths and modes
+    cfg%input_path         = './'
+    cfg%hhl_file           = ''
+    cfg%soiltyp_file       = ''
+    cfg%namelist_file      = './namelist'
+    cfg%static_driver_file = ''
+    cfg%output_file = './palm-4u-input.nc'
+    cfg%ic_mode = 'volume'
+    cfg%bc_mode = 'real'
+    cfg%averaging_mode = 'level'
+
+!
+!-- Overwrite defaults with user configuration
+    CALL parse_command_line_arguments( cfg )
+    CALL report('main_loop', 'Running INIFOR version ' // VERSION)
+
+    flow_prefix = TRIM(cfg%input_prefix)
+    radiation_prefix = TRIM(cfg%input_prefix)
+    soil_prefix = TRIM(cfg%input_prefix)
+    soilmoisture_prefix = TRIM(cfg%input_prefix)
+    IF (cfg%flow_prefix_is_set)  flow_prefix = TRIM(cfg%flow_prefix)
+    IF (cfg%radiation_prefix_is_set)  radiation_prefix = TRIM(cfg%radiation_prefix)
+    IF (cfg%soil_prefix_is_set)  soil_prefix = TRIM(cfg%soil_prefix)
+    IF (cfg%soilmoisture_prefix_is_set)  soilmoisture_prefix = TRIM(cfg%soilmoisture_prefix)
+
+    output_file%name = cfg%output_file
+    z0 = cfg%z0
+    p0 = cfg%p0
+
+    init_variables_required = .TRUE.
+    boundary_variables_required = TRIM( cfg%bc_mode ) == 'real'
+    ls_forcing_variables_required = TRIM( cfg%bc_mode ) == 'ideal'
+    surface_forcing_required = .FALSE.
+
+    IF ( ls_forcing_variables_required )  THEN
+       message = "Averaging of large-scale forcing profiles " //            &
+                 "has not been implemented, yet."
+       CALL inifor_abort('setup_parameters', message)
+    ENDIF
+
+!
+!-- Set default file paths, if not specified by user.
+    CALL normalize_path(cfg%input_path)
+    IF (TRIM(cfg%hhl_file) == '')  cfg%hhl_file = TRIM(cfg%input_path) // 'hhl.nc'
+    IF (TRIM(cfg%soiltyp_file) == '')  cfg%soiltyp_file = TRIM(cfg%input_path) // 'soil.nc'
+
+    CALL validate_config( cfg ) 
+
+    CALL report('setup_parameters', "initialization mode: " // TRIM(cfg%ic_mode))
+    CALL report('setup_parameters', "       forcing mode: " // TRIM(cfg%bc_mode))
+    CALL report('setup_parameters', "     averaging mode: " // TRIM(cfg%averaging_mode))
+    CALL report('setup_parameters', "    averaging angle: " // real_to_str(cfg%averaging_angle))
+    CALL report('setup_parameters', "          data path: " // TRIM(cfg%input_path))
+    CALL report('setup_parameters', "           hhl file: " // TRIM(cfg%hhl_file))
+    CALL report('setup_parameters', "       soiltyp file: " // TRIM(cfg%soiltyp_file))
+    CALL report('setup_parameters', "      namelist file: " // TRIM(cfg%namelist_file))
+    CALL report('setup_parameters', "   output data file: " // TRIM(output_file%name))
+    IF (cfg%debug )  CALL report('setup_parameters', "     debugging mode: enabled")
+
+    CALL log_runtime('time', 'init')
+!
+!-- Read in namelist parameters
+    OPEN(10, FILE=cfg%namelist_file, STATUS='old')
+    READ(10, NML=inipar, IOSTAT=iostat) ! nx, ny, nz, dx, dy, dz    
+    IF ( iostat > 0 )  THEN      
+       message = "Failed to read namelist 'inipar' from file '" //             &
+                 TRIM( cfg%namelist_file ) // "'. "
+       CALL inifor_abort( 'setup_parameters', message )
        CLOSE(10)
- CALL run_control('time', 'read')
-
-       end_hour = CEILING( end_time / 3600.0 * step_hour )
-
-!
-!--    Generate input file lists
-       CALL get_input_file_list(                                               &
-          cfg % start_date, start_hour_flow, end_hour, step_hour,              &
-          cfg % input_path, flow_prefix, flow_suffix, flow_files)
-       CALL get_input_file_list(                                               &
-          cfg % start_date, start_hour_soil, end_hour, step_hour,              &
-          cfg % input_path, soil_prefix, soil_suffix, soil_files)
-       CALL get_input_file_list(                                               &
-          cfg % start_date, start_hour_radiation, end_hour, step_hour,         &
-          cfg % input_path, radiation_prefix, radiation_suffix, radiation_files, nocheck=.TRUE.)
-       CALL get_input_file_list(                                               &
-          cfg % start_date, start_hour_soilmoisture, end_hour_soilmoisture, step_hour, &
-          cfg % input_path, soilmoisture_prefix, soilmoisture_suffix, soil_moisture_files, nocheck=.TRUE.)
+    ENDIF
+
+    READ(10, NML=d3par, IOSTAT=iostat)  ! end_time
+    IF ( iostat > 0 )  THEN
+       message = "Failed to read namelist 'd3par' from file '" //              &
+                 TRIM( cfg%namelist_file ) // "'. "
+       CALL inifor_abort( 'setup_parameters', message )
+       CLOSE(10)
+    ENDIF
+    CLOSE(10)
+    
+    CALL log_runtime('time', 'read')
+
+    end_hour = CEILING( end_time / 3600.0 * step_hour )
+
+!
+!-- Generate input file lists
+    CALL get_input_file_list(                                               &
+       cfg%start_date, start_hour_flow, end_hour, step_hour,              &
+       cfg%input_path, flow_prefix, flow_suffix, flow_files)
+    CALL get_input_file_list(                                               &
+       cfg%start_date, start_hour_soil, end_hour, step_hour,              &
+       cfg%input_path, soil_prefix, soil_suffix, soil_files)
+    CALL get_input_file_list(                                               &
+       cfg%start_date, start_hour_radiation, end_hour, step_hour,         &
+       cfg%input_path, radiation_prefix, radiation_suffix, radiation_files, nocheck=.TRUE.)
+    CALL get_input_file_list(                                               &
+       cfg%start_date, start_hour_soilmoisture, end_hour_soilmoisture, step_hour, &
+       cfg%input_path, soilmoisture_prefix, soilmoisture_suffix, soil_moisture_files, nocheck=.TRUE.)
 
 !
@@ -506,42 +527,42 @@
 
 
- CALL run_control('time', 'init')
-!
-!--    Read COSMO soil type map
-       cosmo_var % name = 'SOILTYP'
-       CALL get_netcdf_variable(cfg % soiltyp_file, cosmo_var, soiltyp)
-
-       message = 'Reading PALM-4U origin from'
-       IF (TRIM(cfg % static_driver_file) .NE. '')  THEN
-
-          origin_lon = get_netcdf_attribute(cfg % static_driver_file, 'origin_lon')
-          origin_lat = get_netcdf_attribute(cfg % static_driver_file, 'origin_lat')
-
-          message = TRIM(message) // " static driver file '"                   &
-                                  // TRIM(cfg % static_driver_file) // "'"
-
-
-       ELSE
-
-          origin_lon = longitude
-          origin_lat = latitude
-
-          message = TRIM(message) // " namlist file '"                         &
-                                  // TRIM(cfg % namelist_file) // "'"
-
-       ENDIF
-       origin_lon = origin_lon * TO_RADIANS
-       origin_lat = origin_lat * TO_RADIANS
-
-       CALL report('setup_parameters', message)
-
- CALL run_control('time', 'read')
-
-       CALL get_cosmo_grid( cfg, soil_files(1), rlon, rlat, hhl, hfl, depths,  &
-                            d_depth, d_depth_rho_inv, phi_n, lambda_n,         &
-                            phi_equat,                                         &
-                            lonmin_cosmo, lonmax_cosmo,                        &
-                            latmin_cosmo, latmax_cosmo,                        &
-                            nlon, nlat, nlev, ndepths )
+    CALL log_runtime('time', 'init')
+!
+!-- Read COSMO soil type map
+    cosmo_var%name = 'SOILTYP'
+    CALL get_netcdf_variable(cfg%soiltyp_file, cosmo_var, soiltyp)
+
+    message = 'Reading PALM-4U origin from'
+    IF (TRIM(cfg%static_driver_file) .NE. '')  THEN
+
+       origin_lon = get_netcdf_attribute(cfg%static_driver_file, 'origin_lon')
+       origin_lat = get_netcdf_attribute(cfg%static_driver_file, 'origin_lat')
+
+       message = TRIM(message) // " static driver file '"                   &
+                               // TRIM(cfg%static_driver_file) // "'"
+
+
+    ELSE
+
+       origin_lon = longitude
+       origin_lat = latitude
+
+       message = TRIM(message) // " namlist file '"                         &
+                               // TRIM(cfg%namelist_file) // "'"
+
+    ENDIF
+    origin_lon = origin_lon * TO_RADIANS
+    origin_lat = origin_lat * TO_RADIANS
+
+    CALL report('setup_parameters', message)
+
+    CALL log_runtime('time', 'read')
+
+    CALL get_cosmo_grid( cfg, soil_files(1), rlon, rlat, hhl, hfl, depths,  &
+                         d_depth, d_depth_rho_inv, phi_n, lambda_n,         &
+                         phi_equat,                                         &
+                         lonmin_cosmo, lonmax_cosmo,                        &
+                         latmin_cosmo, latmax_cosmo,                        &
+                         nlon, nlat, nlev, ndepths )
 
 
@@ -550,71 +571,71 @@
 !------------------------------------------------------------------------------
 !
-!--    PALM-4U domain extents
-       lx = (nx+1) * dx
-       ly = (ny+1) * dy
-       
-!
-!--    PALM-4U point of Earth tangency
-       x0 = 0.0_dp
-       y0 = 0.0_dp
-
-!
-!--    time vector
-       nt = CEILING(end_time / (step_hour * 3600.0_dp)) + 1
-       ALLOCATE( time(nt) )
-       CALL linspace(0.0_dp, 3600.0_dp * (nt-1), time)
-       output_file % time => time
- CALL run_control('time', 'init')
-
-!
-!--    Convert the PALM-4U origin coordinates to COSMO's rotated-pole grid
-       phi_c    = TO_RADIANS *                                                 &
-                  phi2phirot( origin_lat * TO_DEGREES, origin_lon * TO_DEGREES,&
-                              phi_n * TO_DEGREES, lambda_n * TO_DEGREES )
-       lambda_c = TO_RADIANS *                                                 &
-                  rla2rlarot( origin_lat * TO_DEGREES, origin_lon * TO_DEGREES,&
-                              phi_n * TO_DEGREES, lambda_n * TO_DEGREES,     &
-                              0.0_dp )
-
-!
-!--    Set gamma according to whether PALM domain is in the northern or southern
-!--    hemisphere of the COSMO rotated-pole system. Gamma assumes either the
-!--    value 0 or PI and is needed to work around around a bug in the
-!--    rotated-pole coordinate transformations.
-       gam = gamma_from_hemisphere(origin_lat, phi_equat)
-
-!
-!--    Compute the north pole of the rotated-pole grid centred at the PALM-4U
-!--    domain centre. The resulting (phi_cn, lambda_cn) are coordinates in
-!--    COSMO-DE's rotated-pole grid.
-       phi_cn    = phic_to_phin(phi_c) 
-       lambda_cn = lamc_to_lamn(phi_c, lambda_c) 
-
-       message =   "PALM-4U origin:" // NEW_LINE('') // &
-          "           lon (lambda) = " // &
-          TRIM(real_to_str_f(origin_lon * TO_DEGREES)) // " deg"// NEW_LINE(' ') //&
-          "           lat (phi   ) = " // &
-          TRIM(real_to_str_f(origin_lat * TO_DEGREES)) // " deg (geographical)" // NEW_LINE(' ') //&
-          "           lon (lambda) = " // &
-          TRIM(real_to_str_f(lambda_c * TO_DEGREES)) // " deg" // NEW_LINE(' ') // &
-          "           lat (phi   ) = " // &
-          TRIM(real_to_str_f(phi_c * TO_DEGREES)) // " deg (COSMO-DE rotated-pole)"
-      CALL report ('setup_parameters', message)
-
-       message = "North pole of the rotated COSMO-DE system:" // NEW_LINE(' ') // &
-          "           lon (lambda) = " // &
-          TRIM(real_to_str_f(lambda_n * TO_DEGREES)) // " deg" // NEW_LINE(' ') //&
-          "           lat (phi   ) = " // &
-          TRIM(real_to_str_f(phi_n * TO_DEGREES)) // " deg (geographical)"
-       CALL report ('setup_parameters', message)
-          
-       message = "North pole of the rotated palm system:" // NEW_LINE(' ') // &
-          "           lon (lambda) = " // &
-          TRIM(real_to_str_f(lambda_cn * TO_DEGREES)) // " deg" // NEW_LINE(' ') // &
-          "           lat (phi   ) = " // &
-          TRIM(real_to_str_f(phi_cn * TO_DEGREES)) // " deg (COSMO-DE rotated-pole)"
-       CALL report ('setup_parameters', message)
-
- CALL run_control('time', 'comp')
+!-- PALM-4U domain extents
+    lx = (nx+1) * dx
+    ly = (ny+1) * dy
+    
+!
+!-- PALM-4U point of Earth tangency
+    x0 = 0.0_wp
+    y0 = 0.0_wp
+
+!
+!-- time vector
+    nt = CEILING(end_time / (step_hour * 3600.0_wp)) + 1
+    ALLOCATE( time(nt) )
+    CALL linspace(0.0_wp, 3600.0_wp * (nt-1), time)
+    output_file%time => time
+    CALL log_runtime('time', 'init')
+
+!
+!-- Convert the PALM-4U origin coordinates to COSMO's rotated-pole grid
+    phi_c    = TO_RADIANS *                                                 &
+               phi2phirot( origin_lat * TO_DEGREES, origin_lon * TO_DEGREES,&
+                           phi_n * TO_DEGREES, lambda_n * TO_DEGREES )
+    lambda_c = TO_RADIANS *                                                 &
+               rla2rlarot( origin_lat * TO_DEGREES, origin_lon * TO_DEGREES,&
+                           phi_n * TO_DEGREES, lambda_n * TO_DEGREES,     &
+                           0.0_wp )
+
+!
+!-- Set gamma according to whether PALM domain is in the northern or southern
+!-- hemisphere of the COSMO rotated-pole system. Gamma assumes either the
+!-- value 0 or PI and is needed to work around around a bug in the
+!-- rotated-pole coordinate transformations.
+    gam = gamma_from_hemisphere(origin_lat, phi_equat)
+
+!
+!-- Compute the north pole of the rotated-pole grid centred at the PALM-4U
+!-- domain centre. The resulting (phi_cn, lambda_cn) are coordinates in
+!-- COSMO-DE's rotated-pole grid.
+    phi_cn    = phic_to_phin(phi_c) 
+    lambda_cn = lamc_to_lamn(phi_c, lambda_c) 
+
+    message =   "PALM-4U origin:" // NEW_LINE('') // &
+       "           lon (lambda) = " // &
+       TRIM(real_to_str_f(origin_lon * TO_DEGREES)) // " deg"// NEW_LINE(' ') //&
+       "           lat (phi   ) = " // &
+       TRIM(real_to_str_f(origin_lat * TO_DEGREES)) // " deg (geographical)" // NEW_LINE(' ') //&
+       "           lon (lambda) = " // &
+       TRIM(real_to_str_f(lambda_c * TO_DEGREES)) // " deg" // NEW_LINE(' ') // &
+       "           lat (phi   ) = " // &
+       TRIM(real_to_str_f(phi_c * TO_DEGREES)) // " deg (COSMO-DE rotated-pole)"
+    CALL report ('setup_parameters', message)
+
+    message = "North pole of the rotated COSMO-DE system:" // NEW_LINE(' ') // &
+       "           lon (lambda) = " // &
+       TRIM(real_to_str_f(lambda_n * TO_DEGREES)) // " deg" // NEW_LINE(' ') //&
+       "           lat (phi   ) = " // &
+       TRIM(real_to_str_f(phi_n * TO_DEGREES)) // " deg (geographical)"
+    CALL report ('setup_parameters', message)
+       
+    message = "North pole of the rotated palm system:" // NEW_LINE(' ') // &
+       "           lon (lambda) = " // &
+       TRIM(real_to_str_f(lambda_cn * TO_DEGREES)) // " deg" // NEW_LINE(' ') // &
+       "           lat (phi   ) = " // &
+       TRIM(real_to_str_f(phi_cn * TO_DEGREES)) // " deg (COSMO-DE rotated-pole)"
+    CALL report ('setup_parameters', message)
+
+    CALL log_runtime('time', 'comp')
 
 !------------------------------------------------------------------------------
@@ -625,6 +646,6 @@
 !-- Compute coordiantes of the PALM centre in the source (COSMO) system
     phi_centre = phirot2phi(                                                   &
-       phirot = project(0.5_dp*ly, y0, EARTH_RADIUS) * TO_DEGREES,             &
-       rlarot = project(0.5_dp*lx, x0, EARTH_RADIUS) * TO_DEGREES,             &
+       phirot = project(0.5_wp*ly, y0, EARTH_RADIUS) * TO_DEGREES,             &
+       rlarot = project(0.5_wp*lx, x0, EARTH_RADIUS) * TO_DEGREES,             &
        polphi = phi_cn * TO_DEGREES,                                           &
        polgam = gam * TO_DEGREES                                               &
@@ -632,6 +653,6 @@
 
     lam_centre = rlarot2rla(                                                   &
-       phirot = project(0.5_dp*ly, y0, EARTH_RADIUS) * TO_DEGREES,             &
-       rlarot = project(0.5_dp*lx, x0, EARTH_RADIUS) * TO_DEGREES,             &
+       phirot = project(0.5_wp*ly, y0, EARTH_RADIUS) * TO_DEGREES,             &
+       rlarot = project(0.5_wp*lx, x0, EARTH_RADIUS) * TO_DEGREES,             &
        polphi = phi_cn * TO_DEGREES, pollam = lambda_cn * TO_DEGREES,          &
        polgam = gam * TO_DEGREES                                               &
@@ -647,9 +668,9 @@
 !
 !-- Compute boundaries of the central averaging box
-    averaging_angle = cfg % averaging_angle * TO_RADIANS
-    lam_east = lam_centre + 0.5_dp * averaging_angle
-    lam_west = lam_centre - 0.5_dp * averaging_angle
-    phi_north = phi_centre + 0.5_dp * averaging_angle
-    phi_south = phi_centre - 0.5_dp * averaging_angle
+    averaging_angle = cfg%averaging_angle * TO_RADIANS
+    lam_east = lam_centre + 0.5_wp * averaging_angle
+    lam_west = lam_centre - 0.5_wp * averaging_angle
+    phi_north = phi_centre + 0.5_wp * averaging_angle
+    phi_south = phi_centre - 0.5_wp * averaging_angle
     averaging_width_ew = averaging_angle * COS(phi_centre) * EARTH_RADIUS
     averaging_width_ns = averaging_angle * EARTH_RADIUS
@@ -674,5 +695,5 @@
 !
 !-- Coriolis parameter
-    f3 = 2.0_dp * OMEGA * SIN(                                                 &
+    f3 = 2.0_wp * OMEGA * SIN(                                                 &
        TO_RADIANS*phirot2phi( phi_centre * TO_DEGREES, lam_centre * TO_DEGREES,&
                               phi_n * TO_DEGREES,                              &
@@ -680,5 +701,5 @@
     )
 
-    END SUBROUTINE setup_parameters
+ END SUBROUTINE setup_parameters
 
 
@@ -689,6 +710,6 @@
 !> coordinates and interpolation weights
 !------------------------------------------------------------------------------!
-    SUBROUTINE setup_grids()
-       CHARACTER ::  interp_mode
+ SUBROUTINE setup_grids()
+    CHARACTER ::  interp_mode
 
 !------------------------------------------------------------------------------
@@ -696,28 +717,28 @@
 !------------------------------------------------------------------------------
 !
-!--    palm x y z, we allocate the column to nz+1 in order to include the top
-!--    scalar boundary. The interpolation grids will be associated with
-!--    a shorter column that omits the top element.
-       ALLOCATE( x(0:nx), y(0:ny), z(1:nz), z_column(1:nz+1) )
-       CALL linspace(0.5_dp * dx, lx - 0.5_dp * dx, x)
-       CALL linspace(0.5_dp * dy, ly - 0.5_dp * dy, y)
-       CALL stretched_z(z_column, dz, dz_max=dz_max,                           &
-                        dz_stretch_factor=dz_stretch_factor,                   &
-                        dz_stretch_level=dz_stretch_level,                     &
-                        dz_stretch_level_start=dz_stretch_level_start,         &
-                        dz_stretch_level_end=dz_stretch_level_end,             &
-                        dz_stretch_factor_array=dz_stretch_factor_array)
-       z(1:nz) = z_column(1:nz)
-       z_top = z_column(nz+1)
-
-!
-!--    palm xu yv zw, compared to the scalar grid, velocity coordinates
-!--    contain one element less.
-       ALLOCATE( xu(1:nx),  yv(1:ny), zw(1:nz-1), zw_column(1:nz))
-       CALL linspace(dx, lx - dx, xu)
-       CALL linspace(dy, ly - dy, yv)
-       CALL midpoints(z_column, zw_column)
-       zw(1:nz-1) = zw_column(1:nz-1)
-       zw_top     = zw_column(nz)
+!-- palm x y z, we allocate the column to nz+1 in order to include the top
+!-- scalar boundary. The interpolation grids will be associated with
+!-- a shorter column that omits the top element.
+    ALLOCATE( x(0:nx), y(0:ny), z(1:nz), z_column(1:nz+1) )
+    CALL linspace(0.5_wp * dx, lx - 0.5_wp * dx, x)
+    CALL linspace(0.5_wp * dy, ly - 0.5_wp * dy, y)
+    CALL stretched_z(z_column, dz, dz_max=dz_max,                           &
+                     dz_stretch_factor=dz_stretch_factor,                   &
+                     dz_stretch_level=dz_stretch_level,                     &
+                     dz_stretch_level_start=dz_stretch_level_start,         &
+                     dz_stretch_level_end=dz_stretch_level_end,             &
+                     dz_stretch_factor_array=dz_stretch_factor_array)
+    z(1:nz) = z_column(1:nz)
+    z_top = z_column(nz+1)
+
+!
+!-- palm xu yv zw, compared to the scalar grid, velocity coordinates
+!-- contain one element less.
+    ALLOCATE( xu(1:nx),  yv(1:ny), zw(1:nz-1), zw_column(1:nz))
+    CALL linspace(dx, lx - dx, xu)
+    CALL linspace(dy, ly - dy, yv)
+    CALL midpoints(z_column, zw_column)
+    zw(1:nz-1) = zw_column(1:nz-1)
+    zw_top     = zw_column(nz)
 
 
@@ -725,313 +746,313 @@
 ! Section 1: Define initialization and boundary grids
 !------------------------------------------------------------------------------
-       CALL init_grid_definition('palm', grid=palm_grid,                       &
-               xmin=0.0_dp, xmax=lx,                                           &
-               ymin=0.0_dp, ymax=ly,                                           &
-               x0=x0, y0=y0, z0=z0,                                            &
-               nx=nx, ny=ny, nz=nz, z=z, zw=zw, ic_mode=cfg % ic_mode)
-
-!
-!--    Subtracting 1 because arrays will be allocated with nlon + 1 elements.
-       CALL init_grid_definition('cosmo-de', grid=cosmo_grid,                  &
-               xmin=lonmin_cosmo, xmax=lonmax_cosmo,                           &
-               ymin=latmin_cosmo, ymax=latmax_cosmo,                           &
-               x0=x0, y0=y0, z0=0.0_dp,                                        &
-               nx=nlon-1, ny=nlat-1, nz=nlev-1)
-
-!
-!--    Define intermediate grid. This is the same as palm_grid except with a
-!--    much coarser vertical grid. The vertical levels are interpolated in each
-!--    PALM column from COSMO's secondary levels. The main levels are then
-!--    computed as the averages of the bounding secondary levels.
-       CALL init_grid_definition('palm intermediate', grid=palm_intermediate,  &
-               xmin=0.0_dp, xmax=lx,                                           &
-               ymin=0.0_dp, ymax=ly,                                           &
-               x0=x0, y0=y0, z0=z0,                                            &
-               nx=nx, ny=ny, nz=nlev-2)
-
-       CALL init_grid_definition('boundary', grid=u_initial_grid,              &
+    CALL init_grid_definition('palm', grid=palm_grid,                       &
+            xmin=0.0_wp, xmax=lx,                                           &
+            ymin=0.0_wp, ymax=ly,                                           &
+            x0=x0, y0=y0, z0=z0,                                            &
+            nx=nx, ny=ny, nz=nz, z=z, zw=zw, ic_mode=cfg%ic_mode)
+
+!
+!-- Subtracting 1 because arrays will be allocated with nlon + 1 elements.
+    CALL init_grid_definition('cosmo-de', grid=cosmo_grid,                  &
+            xmin=lonmin_cosmo, xmax=lonmax_cosmo,                           &
+            ymin=latmin_cosmo, ymax=latmax_cosmo,                           &
+            x0=x0, y0=y0, z0=0.0_wp,                                        &
+            nx=nlon-1, ny=nlat-1, nz=nlev-1)
+
+!
+!-- Define intermediate grid. This is the same as palm_grid except with a
+!-- much coarser vertical grid. The vertical levels are interpolated in each
+!-- PALM column from COSMO's secondary levels. The main levels are then
+!-- computed as the averages of the bounding secondary levels.
+    CALL init_grid_definition('palm intermediate', grid=palm_intermediate,  &
+            xmin=0.0_wp, xmax=lx,                                           &
+            ymin=0.0_wp, ymax=ly,                                           &
+            x0=x0, y0=y0, z0=z0,                                            &
+            nx=nx, ny=ny, nz=nlev-2)
+
+    CALL init_grid_definition('boundary', grid=u_initial_grid,              &
+            xmin = dx, xmax = lx - dx,                                      &
+            ymin = 0.5_wp * dy, ymax = ly - 0.5_wp * dy,                    &
+            x0=x0, y0=y0, z0 = z0,                                          &
+            nx = nx-1, ny = ny, nz = nz,                                    &
+            z=z, ic_mode=cfg%ic_mode)
+
+    CALL init_grid_definition('boundary', grid=v_initial_grid,              &
+            xmin = 0.5_wp * dx, xmax = lx - 0.5_wp * dx,                    &
+            ymin = dy, ymax = ly - dy,                                      &
+            x0=x0, y0=y0, z0 = z0,                                          &
+            nx = nx, ny = ny-1, nz = nz,                                    &
+            z=z, ic_mode=cfg%ic_mode)
+
+    CALL init_grid_definition('boundary', grid=w_initial_grid,              &
+            xmin = 0.5_wp * dx, xmax = lx - 0.5_wp * dx,                    &
+            ymin = 0.5_wp * dy, ymax = ly - 0.5_wp * dy,                    &
+            x0=x0, y0=y0, z0 = z0,                                          &
+            nx = nx, ny = ny, nz = nz-1,                                    &
+            z=zw, ic_mode=cfg%ic_mode)
+
+    CALL init_grid_definition('boundary intermediate', grid=u_initial_intermediate,      &
+            xmin = dx, xmax = lx - dx,                                      &
+            ymin = 0.5_wp * dy, ymax = ly - 0.5_wp * dy,                    &
+            x0=x0, y0=y0, z0 = z0,                                          &
+            nx = nx-1, ny = ny, nz = nlev - 2)
+
+    CALL init_grid_definition('boundary intermediate', grid=v_initial_intermediate,      &
+            xmin = 0.5_wp * dx, xmax = lx - 0.5_wp * dx,                    &
+            ymin = dy, ymax = ly - dy,                                      &
+            x0=x0, y0=y0, z0 = z0,                                          &
+            nx = nx, ny = ny-1, nz = nlev - 2)
+
+    CALL init_grid_definition('boundary intermediate', grid=w_initial_intermediate,      &
+            xmin = 0.5_wp * dx, xmax = lx - 0.5_wp * dx,                    &
+            ymin = 0.5_wp * dy, ymax = ly - 0.5_wp * dy,                    &
+            x0=x0, y0=y0, z0 = z0,                                          &
+            nx = nx, ny = ny, nz = nlev - 1)
+
+    IF (boundary_variables_required)  THEN
+!
+!------------------------------------------------------------------------------
+! Section 2: Define PALM-4U boundary grids
+!------------------------------------------------------------------------------
+       CALL init_grid_definition('boundary', grid=scalars_east_grid,           &
+               xmin = lx + 0.5_wp * dx, xmax = lx + 0.5_wp * dx,               &
+               ymin =  0.5_wp * dy, ymax = ly - 0.5_wp * dy,                   &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = 0, ny = ny, nz = nz, z=z)
+
+       CALL init_grid_definition('boundary', grid=scalars_west_grid,           &
+               xmin = -0.5_wp * dx, xmax = -0.5_wp * dx,                       &
+               ymin =  0.5_wp * dy, ymax = ly - 0.5_wp * dy,                   &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = 0, ny = ny, nz = nz, z=z)
+
+       CALL init_grid_definition('boundary', grid=scalars_north_grid,          &
+               xmin = 0.5_wp * dx, xmax = lx - 0.5_wp * dx,                    &
+               ymin = ly + 0.5_wp * dy, ymax = ly + 0.5_wp * dy,               &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx, ny = 0, nz = nz, z=z)
+
+       CALL init_grid_definition('boundary', grid=scalars_south_grid,          &
+               xmin =  0.5_wp * dx, xmax = lx - 0.5_wp * dx,                   &
+               ymin = -0.5_wp * dy, ymax = -0.5_wp * dy,                       &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx, ny = 0, nz = nz, z=z)
+
+       CALL init_grid_definition('boundary', grid=scalars_top_grid,            &
+               xmin =  0.5_wp * dx, xmax = lx - 0.5_wp * dx,                   &
+               ymin =  0.5_wp * dy, ymax = ly - 0.5_wp * dy,                   &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx, ny = ny, nz = 1, z=(/z_top/))
+
+       CALL init_grid_definition('boundary', grid=u_east_grid,                 &
+               xmin = lx, xmax = lx,                                           &
+               ymin = 0.5_wp * dy, ymax = ly - 0.5_wp * dy,                    &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = 0, ny = ny, nz = nz, z=z)
+
+       CALL init_grid_definition('boundary', grid=u_west_grid,                 &
+               xmin = 0.0_wp, xmax = 0.0_wp,                                   &
+               ymin = 0.5_wp * dy, ymax = ly - 0.5_wp * dy,                    &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = 0, ny = ny, nz = nz, z=z)
+
+       CALL init_grid_definition('boundary', grid=u_north_grid,                &
                xmin = dx, xmax = lx - dx,                                      &
-               ymin = 0.5_dp * dy, ymax = ly - 0.5_dp * dy,                    &
+               ymin = ly + 0.5_wp * dy, ymax = ly + 0.5_wp * dy,               &
                x0=x0, y0=y0, z0 = z0,                                          &
-               nx = nx-1, ny = ny, nz = nz,                                    &
-               z=z, ic_mode=cfg % ic_mode)
-
-       CALL init_grid_definition('boundary', grid=v_initial_grid,              &
-               xmin = 0.5_dp * dx, xmax = lx - 0.5_dp * dx,                    &
+               nx = nx-1, ny = 0, nz = nz, z=z)
+    
+       CALL init_grid_definition('boundary', grid=u_south_grid,                &
+               xmin = dx, xmax = lx - dx,                                      &
+               ymin = -0.5_wp * dy, ymax = -0.5_wp * dy,                       &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx-1, ny = 0, nz = nz, z=z)
+
+       CALL init_grid_definition('boundary', grid=u_top_grid,                  &
+               xmin = dx, xmax = lx - dx,                                      &
+               ymin = 0.5_wp * dy, ymax = ly - 0.5_wp * dy,                    &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx-1, ny = ny, nz = 1, z=(/z_top/))
+
+       CALL init_grid_definition('boundary', grid=v_east_grid,                 &
+               xmin = lx + 0.5_wp * dx, xmax = lx + 0.5_wp * dx,               &
                ymin = dy, ymax = ly - dy,                                      &
                x0=x0, y0=y0, z0 = z0,                                          &
-               nx = nx, ny = ny-1, nz = nz,                                    &
-               z=z, ic_mode=cfg % ic_mode)
-
-       CALL init_grid_definition('boundary', grid=w_initial_grid,              &
-               xmin = 0.5_dp * dx, xmax = lx - 0.5_dp * dx,                    &
-               ymin = 0.5_dp * dy, ymax = ly - 0.5_dp * dy,                    &
+               nx = 0, ny = ny-1, nz = nz, z=z)
+
+       CALL init_grid_definition('boundary', grid=v_west_grid,                 &
+               xmin = -0.5_wp * dx, xmax = -0.5_wp * dx,                       &
+               ymin = dy, ymax = ly - dy,                                      &
                x0=x0, y0=y0, z0 = z0,                                          &
-               nx = nx, ny = ny, nz = nz-1,                                    &
-               z=zw, ic_mode=cfg % ic_mode)
-
-       CALL init_grid_definition('boundary intermediate', grid=u_initial_intermediate,      &
+               nx = 0, ny = ny-1, nz = nz, z=z)
+
+       CALL init_grid_definition('boundary', grid=v_north_grid,                &
+               xmin = 0.5_wp * dx, xmax = lx - 0.5_wp * dx,                    &
+               ymin = ly, ymax = ly,                                           &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx, ny = 0, nz = nz, z=z)
+
+       CALL init_grid_definition('boundary', grid=v_south_grid,                &
+               xmin = 0.5_wp * dx, xmax = lx - 0.5_wp * dx,                    &
+               ymin = 0.0_wp, ymax = 0.0_wp,                                   &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx, ny = 0, nz = nz, z=z)
+
+       CALL init_grid_definition('boundary', grid=v_top_grid,                  &
+               xmin = 0.5_wp * dx, xmax = lx - 0.5_wp * dx,                    &
+               ymin = dy, ymax = ly - dy,                                      &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx, ny = ny-1, nz = 1, z=(/z_top/))
+
+       CALL init_grid_definition('boundary', grid=w_east_grid,                 &
+               xmin = lx + 0.5_wp * dx, xmax = lx + 0.5_wp * dx,               &
+               ymin =  0.5_wp * dy, ymax = ly - 0.5_wp * dy,                   &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = 0, ny = ny, nz = nz - 1, z=zw)
+
+       CALL init_grid_definition('boundary', grid=w_west_grid,                 &
+               xmin = -0.5_wp * dx, xmax = -0.5_wp * dx,                       &
+               ymin = 0.5_wp * dy, ymax = ly - 0.5_wp * dy,                    &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = 0, ny = ny, nz = nz - 1, z=zw)
+
+       CALL init_grid_definition('boundary', grid=w_north_grid,                &
+               xmin = 0.5_wp * dx, xmax = lx - 0.5_wp * dx,                    &
+               ymin = ly + 0.5_wp * dy, ymax = ly + 0.5_wp * dy,               &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx, ny = 0, nz = nz - 1, z=zw)
+
+       CALL init_grid_definition('boundary', grid=w_south_grid,                &
+               xmin =  0.5_wp * dx, xmax = lx - 0.5_wp * dx,                   &
+               ymin = -0.5_wp * dy, ymax = -0.5_wp * dy,                       &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx, ny = 0, nz = nz - 1, z=zw)
+
+       CALL init_grid_definition('boundary', grid=w_top_grid,                  &
+               xmin = 0.5_wp * dx, xmax = lx - 0.5_wp * dx,                    &
+               ymin = 0.5_wp * dy, ymax = ly - 0.5_wp * dy,                    &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx, ny = ny, nz = 1, z=(/zw_top/))
+
+       CALL init_grid_definition('boundary intermediate', grid=scalars_east_intermediate,   &
+               xmin = lx + 0.5_wp * dx, xmax = lx + 0.5_wp * dx,               &
+               ymin =  0.5_wp * dy, ymax = ly - 0.5_wp * dy,                   &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = 0, ny = ny, nz = nlev - 2)
+
+       CALL init_grid_definition('boundary intermediate', grid=scalars_west_intermediate,   &
+               xmin = -0.5_wp * dx, xmax = -0.5_wp * dx,                       &
+               ymin =  0.5_wp * dy, ymax = ly - 0.5_wp * dy,                   &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = 0, ny = ny, nz = nlev - 2)
+
+       CALL init_grid_definition('boundary intermediate', grid=scalars_north_intermediate,  &
+               xmin = 0.5_wp * dx, xmax = lx - 0.5_wp * dx,                    &
+               ymin = ly + 0.5_wp * dy, ymax = ly + 0.5_wp * dy,               &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx, ny = 0, nz = nlev - 2)
+
+       CALL init_grid_definition('boundary intermediate', grid=scalars_south_intermediate,  &
+               xmin =  0.5_wp * dx, xmax = lx - 0.5_wp * dx,                   &
+               ymin = -0.5_wp * dy, ymax = -0.5_wp * dy,                       &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx, ny = 0, nz = nlev - 2)
+
+       CALL init_grid_definition('boundary intermediate', grid=scalars_top_intermediate,    &
+               xmin =  0.5_wp * dx, xmax = lx - 0.5_wp * dx,                   &
+               ymin =  0.5_wp * dy, ymax = ly - 0.5_wp * dy,                   &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx, ny = ny, nz = nlev - 2)
+
+       CALL init_grid_definition('boundary intermediate', grid=u_east_intermediate,         &
+               xmin = lx, xmax = lx,                                           &
+               ymin = 0.5_wp * dy, ymax = ly - 0.5_wp * dy,                    &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = 0, ny = ny, nz = nlev - 2)
+
+       CALL init_grid_definition('boundary intermediate', grid=u_west_intermediate,         &
+               xmin = 0.0_wp, xmax = 0.0_wp,                                   &
+               ymin = 0.5_wp * dy, ymax = ly - 0.5_wp * dy,                    &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = 0, ny = ny, nz = nlev - 2)
+
+       CALL init_grid_definition('boundary intermediate', grid=u_north_intermediate,        &
                xmin = dx, xmax = lx - dx,                                      &
-               ymin = 0.5_dp * dy, ymax = ly - 0.5_dp * dy,                    &
+               ymin = ly + 0.5_wp * dy, ymax = ly + 0.5_wp * dy,               &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx-1, ny = 0, nz = nlev - 2)
+
+       CALL init_grid_definition('boundary intermediate', grid=u_south_intermediate,        &
+               xmin = dx, xmax = lx - dx,                                      &
+               ymin = -0.5_wp * dy, ymax = -0.5_wp * dy,                       &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx-1, ny = 0, nz = nlev - 2)
+
+       CALL init_grid_definition('boundary intermediate', grid=u_top_intermediate,          &
+               xmin = dx, xmax = lx - dx,                                      &
+               ymin = 0.5_wp * dy, ymax = ly - 0.5_wp * dy,                    &
                x0=x0, y0=y0, z0 = z0,                                          &
                nx = nx-1, ny = ny, nz = nlev - 2)
 
-       CALL init_grid_definition('boundary intermediate', grid=v_initial_intermediate,      &
-               xmin = 0.5_dp * dx, xmax = lx - 0.5_dp * dx,                    &
+       CALL init_grid_definition('boundary intermediate', grid=v_east_intermediate,         &
+               xmin = lx + 0.5_wp * dx, xmax = lx + 0.5_wp * dx,               &
+               ymin = dy, ymax = ly - dy,                                      &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = 0, ny = ny-1, nz = nlev - 2)
+
+       CALL init_grid_definition('boundary intermediate', grid=v_west_intermediate,         &
+               xmin = -0.5_wp * dx, xmax = -0.5_wp * dx,                       &
+               ymin = dy, ymax = ly - dy,                                      &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = 0, ny = ny-1, nz = nlev - 2)
+
+       CALL init_grid_definition('boundary intermediate', grid=v_north_intermediate,        &
+               xmin = 0.5_wp * dx, xmax = lx - 0.5_wp * dx,                    &
+               ymin = ly, ymax = ly,                                           &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx, ny = 0, nz = nlev - 2)
+
+       CALL init_grid_definition('boundary intermediate', grid=v_south_intermediate,        &
+               xmin = 0.5_wp * dx, xmax = lx - 0.5_wp * dx,                    &
+               ymin = 0.0_wp, ymax = 0.0_wp,                                   &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx, ny = 0, nz = nlev - 2)
+
+       CALL init_grid_definition('boundary intermediate', grid=v_top_intermediate,          &
+               xmin = 0.5_wp * dx, xmax = lx - 0.5_wp * dx,                    &
                ymin = dy, ymax = ly - dy,                                      &
                x0=x0, y0=y0, z0 = z0,                                          &
                nx = nx, ny = ny-1, nz = nlev - 2)
 
-       CALL init_grid_definition('boundary intermediate', grid=w_initial_intermediate,      &
-               xmin = 0.5_dp * dx, xmax = lx - 0.5_dp * dx,                    &
-               ymin = 0.5_dp * dy, ymax = ly - 0.5_dp * dy,                    &
+       CALL init_grid_definition('boundary intermediate', grid=w_east_intermediate,         &
+               xmin = lx + 0.5_wp * dx, xmax = lx + 0.5_wp * dx,               &
+               ymin =  0.5_wp * dy, ymax = ly - 0.5_wp * dy,                   &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = 0, ny = ny, nz = nlev - 1)
+
+       CALL init_grid_definition('boundary intermediate', grid=w_west_intermediate,         &
+               xmin = -0.5_wp * dx, xmax = -0.5_wp * dx,                       &
+               ymin =  0.5_wp * dy, ymax = ly - 0.5_wp * dy,                   &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = 0, ny = ny, nz = nlev - 1)
+
+       CALL init_grid_definition('boundary intermediate', grid=w_north_intermediate,        &
+               xmin = 0.5_wp * dx, xmax = lx - 0.5_wp * dx,                    &
+               ymin = ly + 0.5_wp * dy, ymax = ly + 0.5_wp * dy,               &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx, ny = 0, nz = nlev - 1)
+
+       CALL init_grid_definition('boundary intermediate', grid=w_south_intermediate,        &
+               xmin =  0.5_wp * dx, xmax = lx - 0.5_wp * dx,                   &
+               ymin = -0.5_wp * dy, ymax = -0.5_wp * dy,                       &
+               x0=x0, y0=y0, z0 = z0,                                          &
+               nx = nx, ny = 0, nz = nlev - 1)
+
+       CALL init_grid_definition('boundary intermediate', grid=w_top_intermediate,          &
+               xmin =  0.5_wp * dx, xmax = lx - 0.5_wp * dx,                   &
+               ymin =  0.5_wp * dy, ymax = ly - 0.5_wp * dy,                   &
                x0=x0, y0=y0, z0 = z0,                                          &
                nx = nx, ny = ny, nz = nlev - 1)
-
-      IF (boundary_variables_required)  THEN
-!
-!------------------------------------------------------------------------------
-! Section 2: Define PALM-4U boundary grids
-!------------------------------------------------------------------------------
-          CALL init_grid_definition('boundary', grid=scalars_east_grid,           &
-                  xmin = lx + 0.5_dp * dx, xmax = lx + 0.5_dp * dx,               &
-                  ymin =  0.5_dp * dy, ymax = ly - 0.5_dp * dy,                   &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = 0, ny = ny, nz = nz, z=z)
-
-          CALL init_grid_definition('boundary', grid=scalars_west_grid,           &
-                  xmin = -0.5_dp * dx, xmax = -0.5_dp * dx,                       &
-                  ymin =  0.5_dp * dy, ymax = ly - 0.5_dp * dy,                   &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = 0, ny = ny, nz = nz, z=z)
-
-          CALL init_grid_definition('boundary', grid=scalars_north_grid,          &
-                  xmin = 0.5_dp * dx, xmax = lx - 0.5_dp * dx,                    &
-                  ymin = ly + 0.5_dp * dy, ymax = ly + 0.5_dp * dy,               &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = 0, nz = nz, z=z)
-
-          CALL init_grid_definition('boundary', grid=scalars_south_grid,          &
-                  xmin =  0.5_dp * dx, xmax = lx - 0.5_dp * dx,                   &
-                  ymin = -0.5_dp * dy, ymax = -0.5_dp * dy,                       &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = 0, nz = nz, z=z)
-
-          CALL init_grid_definition('boundary', grid=scalars_top_grid,            &
-                  xmin =  0.5_dp * dx, xmax = lx - 0.5_dp * dx,                   &
-                  ymin =  0.5_dp * dy, ymax = ly - 0.5_dp * dy,                   &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = ny, nz = 1, z=(/z_top/))
-
-          CALL init_grid_definition('boundary', grid=u_east_grid,                 &
-                  xmin = lx, xmax = lx,                                           &
-                  ymin = 0.5_dp * dy, ymax = ly - 0.5_dp * dy,                    &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = 0, ny = ny, nz = nz, z=z)
-
-          CALL init_grid_definition('boundary', grid=u_west_grid,                 &
-                  xmin = 0.0_dp, xmax = 0.0_dp,                                   &
-                  ymin = 0.5_dp * dy, ymax = ly - 0.5_dp * dy,                    &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = 0, ny = ny, nz = nz, z=z)
-
-          CALL init_grid_definition('boundary', grid=u_north_grid,                &
-                  xmin = dx, xmax = lx - dx,                                      &
-                  ymin = ly + 0.5_dp * dy, ymax = ly + 0.5_dp * dy,               &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx-1, ny = 0, nz = nz, z=z)
-    
-          CALL init_grid_definition('boundary', grid=u_south_grid,                &
-                  xmin = dx, xmax = lx - dx,                                      &
-                  ymin = -0.5_dp * dy, ymax = -0.5_dp * dy,                       &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx-1, ny = 0, nz = nz, z=z)
-
-          CALL init_grid_definition('boundary', grid=u_top_grid,                  &
-                  xmin = dx, xmax = lx - dx,                                      &
-                  ymin = 0.5_dp * dy, ymax = ly - 0.5_dp * dy,                    &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx-1, ny = ny, nz = 1, z=(/z_top/))
-
-          CALL init_grid_definition('boundary', grid=v_east_grid,                 &
-                  xmin = lx + 0.5_dp * dx, xmax = lx + 0.5_dp * dx,               &
-                  ymin = dy, ymax = ly - dy,                                      &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = 0, ny = ny-1, nz = nz, z=z)
-
-          CALL init_grid_definition('boundary', grid=v_west_grid,                 &
-                  xmin = -0.5_dp * dx, xmax = -0.5_dp * dx,                       &
-                  ymin = dy, ymax = ly - dy,                                      &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = 0, ny = ny-1, nz = nz, z=z)
-
-          CALL init_grid_definition('boundary', grid=v_north_grid,                &
-                  xmin = 0.5_dp * dx, xmax = lx - 0.5_dp * dx,                    &
-                  ymin = ly, ymax = ly,                                           &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = 0, nz = nz, z=z)
-
-          CALL init_grid_definition('boundary', grid=v_south_grid,                &
-                  xmin = 0.5_dp * dx, xmax = lx - 0.5_dp * dx,                    &
-                  ymin = 0.0_dp, ymax = 0.0_dp,                                   &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = 0, nz = nz, z=z)
-
-          CALL init_grid_definition('boundary', grid=v_top_grid,                  &
-                  xmin = 0.5_dp * dx, xmax = lx - 0.5_dp * dx,                    &
-                  ymin = dy, ymax = ly - dy,                                      &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = ny-1, nz = 1, z=(/z_top/))
-
-          CALL init_grid_definition('boundary', grid=w_east_grid,                 &
-                  xmin = lx + 0.5_dp * dx, xmax = lx + 0.5_dp * dx,               &
-                  ymin =  0.5_dp * dy, ymax = ly - 0.5_dp * dy,                   &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = 0, ny = ny, nz = nz - 1, z=zw)
-
-          CALL init_grid_definition('boundary', grid=w_west_grid,                 &
-                  xmin = -0.5_dp * dx, xmax = -0.5_dp * dx,                       &
-                  ymin = 0.5_dp * dy, ymax = ly - 0.5_dp * dy,                    &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = 0, ny = ny, nz = nz - 1, z=zw)
-
-          CALL init_grid_definition('boundary', grid=w_north_grid,                &
-                  xmin = 0.5_dp * dx, xmax = lx - 0.5_dp * dx,                    &
-                  ymin = ly + 0.5_dp * dy, ymax = ly + 0.5_dp * dy,               &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = 0, nz = nz - 1, z=zw)
-
-          CALL init_grid_definition('boundary', grid=w_south_grid,                &
-                  xmin =  0.5_dp * dx, xmax = lx - 0.5_dp * dx,                   &
-                  ymin = -0.5_dp * dy, ymax = -0.5_dp * dy,                       &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = 0, nz = nz - 1, z=zw)
-
-          CALL init_grid_definition('boundary', grid=w_top_grid,                  &
-                  xmin = 0.5_dp * dx, xmax = lx - 0.5_dp * dx,                    &
-                  ymin = 0.5_dp * dy, ymax = ly - 0.5_dp * dy,                    &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = ny, nz = 1, z=(/zw_top/))
-
-          CALL init_grid_definition('boundary intermediate', grid=scalars_east_intermediate,   &
-                  xmin = lx + 0.5_dp * dx, xmax = lx + 0.5_dp * dx,               &
-                  ymin =  0.5_dp * dy, ymax = ly - 0.5_dp * dy,                   &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = 0, ny = ny, nz = nlev - 2)
-
-          CALL init_grid_definition('boundary intermediate', grid=scalars_west_intermediate,   &
-                  xmin = -0.5_dp * dx, xmax = -0.5_dp * dx,                       &
-                  ymin =  0.5_dp * dy, ymax = ly - 0.5_dp * dy,                   &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = 0, ny = ny, nz = nlev - 2)
-
-          CALL init_grid_definition('boundary intermediate', grid=scalars_north_intermediate,  &
-                  xmin = 0.5_dp * dx, xmax = lx - 0.5_dp * dx,                    &
-                  ymin = ly + 0.5_dp * dy, ymax = ly + 0.5_dp * dy,               &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = 0, nz = nlev - 2)
-
-          CALL init_grid_definition('boundary intermediate', grid=scalars_south_intermediate,  &
-                  xmin =  0.5_dp * dx, xmax = lx - 0.5_dp * dx,                   &
-                  ymin = -0.5_dp * dy, ymax = -0.5_dp * dy,                       &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = 0, nz = nlev - 2)
-
-          CALL init_grid_definition('boundary intermediate', grid=scalars_top_intermediate,    &
-                  xmin =  0.5_dp * dx, xmax = lx - 0.5_dp * dx,                   &
-                  ymin =  0.5_dp * dy, ymax = ly - 0.5_dp * dy,                   &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = ny, nz = nlev - 2)
-
-          CALL init_grid_definition('boundary intermediate', grid=u_east_intermediate,         &
-                  xmin = lx, xmax = lx,                                           &
-                  ymin = 0.5_dp * dy, ymax = ly - 0.5_dp * dy,                    &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = 0, ny = ny, nz = nlev - 2)
-
-          CALL init_grid_definition('boundary intermediate', grid=u_west_intermediate,         &
-                  xmin = 0.0_dp, xmax = 0.0_dp,                                   &
-                  ymin = 0.5_dp * dy, ymax = ly - 0.5_dp * dy,                    &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = 0, ny = ny, nz = nlev - 2)
-
-          CALL init_grid_definition('boundary intermediate', grid=u_north_intermediate,        &
-                  xmin = dx, xmax = lx - dx,                                      &
-                  ymin = ly + 0.5_dp * dy, ymax = ly + 0.5_dp * dy,               &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx-1, ny = 0, nz = nlev - 2)
-
-          CALL init_grid_definition('boundary intermediate', grid=u_south_intermediate,        &
-                  xmin = dx, xmax = lx - dx,                                      &
-                  ymin = -0.5_dp * dy, ymax = -0.5_dp * dy,                       &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx-1, ny = 0, nz = nlev - 2)
-
-          CALL init_grid_definition('boundary intermediate', grid=u_top_intermediate,          &
-                  xmin = dx, xmax = lx - dx,                                      &
-                  ymin = 0.5_dp * dy, ymax = ly - 0.5_dp * dy,                    &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx-1, ny = ny, nz = nlev - 2)
-
-          CALL init_grid_definition('boundary intermediate', grid=v_east_intermediate,         &
-                  xmin = lx + 0.5_dp * dx, xmax = lx + 0.5_dp * dx,               &
-                  ymin = dy, ymax = ly - dy,                                      &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = 0, ny = ny-1, nz = nlev - 2)
-
-          CALL init_grid_definition('boundary intermediate', grid=v_west_intermediate,         &
-                  xmin = -0.5_dp * dx, xmax = -0.5_dp * dx,                       &
-                  ymin = dy, ymax = ly - dy,                                      &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = 0, ny = ny-1, nz = nlev - 2)
-
-          CALL init_grid_definition('boundary intermediate', grid=v_north_intermediate,        &
-                  xmin = 0.5_dp * dx, xmax = lx - 0.5_dp * dx,                    &
-                  ymin = ly, ymax = ly,                                           &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = 0, nz = nlev - 2)
-
-          CALL init_grid_definition('boundary intermediate', grid=v_south_intermediate,        &
-                  xmin = 0.5_dp * dx, xmax = lx - 0.5_dp * dx,                    &
-                  ymin = 0.0_dp, ymax = 0.0_dp,                                   &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = 0, nz = nlev - 2)
-
-          CALL init_grid_definition('boundary intermediate', grid=v_top_intermediate,          &
-                  xmin = 0.5_dp * dx, xmax = lx - 0.5_dp * dx,                    &
-                  ymin = dy, ymax = ly - dy,                                      &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = ny-1, nz = nlev - 2)
-
-          CALL init_grid_definition('boundary intermediate', grid=w_east_intermediate,         &
-                  xmin = lx + 0.5_dp * dx, xmax = lx + 0.5_dp * dx,               &
-                  ymin =  0.5_dp * dy, ymax = ly - 0.5_dp * dy,                   &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = 0, ny = ny, nz = nlev - 1)
-
-          CALL init_grid_definition('boundary intermediate', grid=w_west_intermediate,         &
-                  xmin = -0.5_dp * dx, xmax = -0.5_dp * dx,                       &
-                  ymin =  0.5_dp * dy, ymax = ly - 0.5_dp * dy,                   &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = 0, ny = ny, nz = nlev - 1)
-
-          CALL init_grid_definition('boundary intermediate', grid=w_north_intermediate,        &
-                  xmin = 0.5_dp * dx, xmax = lx - 0.5_dp * dx,                    &
-                  ymin = ly + 0.5_dp * dy, ymax = ly + 0.5_dp * dy,               &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = 0, nz = nlev - 1)
-
-          CALL init_grid_definition('boundary intermediate', grid=w_south_intermediate,        &
-                  xmin =  0.5_dp * dx, xmax = lx - 0.5_dp * dx,                   &
-                  ymin = -0.5_dp * dy, ymax = -0.5_dp * dy,                       &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = 0, nz = nlev - 1)
-
-          CALL init_grid_definition('boundary intermediate', grid=w_top_intermediate,          &
-                  xmin =  0.5_dp * dx, xmax = lx - 0.5_dp * dx,                   &
-                  ymin =  0.5_dp * dy, ymax = ly - 0.5_dp * dy,                   &
-                  x0=x0, y0=y0, z0 = z0,                                          &
-                  nx = nx, ny = ny, nz = nlev - 1)
-       ENDIF
+    ENDIF
 
 !                                                                              
@@ -1040,60 +1061,60 @@
 !------------------------------------------------------------------------------
 
-       lonmin_palm = MINVAL(palm_intermediate % clon)
-       lonmax_palm = MAXVAL(palm_intermediate % clon)
-       latmin_palm = MINVAL(palm_intermediate % clat)
-       latmax_palm = MAXVAL(palm_intermediate % clat)
-
-       CALL init_averaging_grid(averaged_initial_scalar_profile, cosmo_grid,   &
-               x = 0.5_dp * lx, y = 0.5_dp * ly, z = z, z0 = z0,               &
-               lonmin = lonmin_palm, lonmax = lonmax_palm,                     &
-               latmin = latmin_palm, latmax = latmax_palm,                     &
-               kind='scalar', name='averaged initial scalar')
-
-       CALL init_averaging_grid(averaged_initial_w_profile, cosmo_grid,        &
-               x = 0.5_dp * lx, y = 0.5_dp * ly, z = zw, z0 = z0,              &
-               lonmin = lonmin_palm, lonmax = lonmax_palm,                     &
-               latmin = latmin_palm, latmax = latmax_palm,                     &
-               kind='w', name='averaged initial w')
-
-       CALL init_averaging_grid(averaged_scalar_profile, cosmo_grid,           &
-               x = 0.5_dp * lx, y = 0.5_dp * ly, z = z, z0 = z0,               &
-               lonmin = lam_west, lonmax = lam_east,                           &
-               latmin = phi_south, latmax = phi_north,                         &
-               kind='scalar', name='centre geostrophic scalar')
-
-       CALL init_averaging_grid(averaged_w_profile, cosmo_grid,                &
-               x = 0.5_dp * lx, y = 0.5_dp * ly, z = zw, z0 = z0,              &
-               lonmin = lam_west, lonmax = lam_east,                           &
-               latmin = phi_south, latmax = phi_north,                         &
-               kind='w', name='centre geostrophic w')
-
-       CALL init_averaging_grid(south_averaged_scalar_profile, cosmo_grid,     &
-               x = 0.5_dp * lx, y = 0.5_dp * ly, z = z, z0 = z0,               &
-               lonmin = lam_west, lonmax = lam_east,                           &
-               latmin = phi_centre - averaging_angle,                          &
-               latmax = phi_centre,                                            &
-               kind='scalar', name='south geostrophic scalar')
-
-       CALL init_averaging_grid(north_averaged_scalar_profile, cosmo_grid,     &
-               x = 0.5_dp * lx, y = 0.5_dp * ly, z = z, z0 = z0,               &
-               lonmin = lam_west, lonmax = lam_east,                           &
-               latmin = phi_centre,                                            &
-               latmax = phi_centre + averaging_angle,                          &
-               kind='scalar', name='north geostrophic scalar')
-
-       CALL init_averaging_grid(west_averaged_scalar_profile, cosmo_grid,      &
-               x = 0.5_dp * lx, y = 0.5_dp * ly, z = z, z0 = z0,               &
-               lonmin = lam_centre - averaging_angle,                          &
-               lonmax = lam_centre,                                            &
-               latmin = phi_south, latmax = phi_north,                         &
-               kind='scalar', name='west geostrophic scalar')
-
-       CALL init_averaging_grid(east_averaged_scalar_profile, cosmo_grid,      &
-               x = 0.5_dp * lx, y = 0.5_dp * ly, z = z, z0 = z0,               &
-               lonmin = lam_centre,                                            &
-               lonmax = lam_centre + averaging_angle,                          &
-               latmin = phi_south, latmax = phi_north,                         &
-               kind='scalar', name='east geostrophic scalar')
+    lonmin_palm = MINVAL(palm_intermediate%clon)
+    lonmax_palm = MAXVAL(palm_intermediate%clon)
+    latmin_palm = MINVAL(palm_intermediate%clat)
+    latmax_palm = MAXVAL(palm_intermediate%clat)
+
+    CALL init_averaging_grid(averaged_initial_scalar_profile, cosmo_grid,   &
+            x = 0.5_wp * lx, y = 0.5_wp * ly, z = z, z0 = z0,               &
+            lonmin = lonmin_palm, lonmax = lonmax_palm,                     &
+            latmin = latmin_palm, latmax = latmax_palm,                     &
+            kind='scalar', name='averaged initial scalar')
+
+    CALL init_averaging_grid(averaged_initial_w_profile, cosmo_grid,        &
+            x = 0.5_wp * lx, y = 0.5_wp * ly, z = zw, z0 = z0,              &
+            lonmin = lonmin_palm, lonmax = lonmax_palm,                     &
+            latmin = latmin_palm, latmax = latmax_palm,                     &
+            kind='w', name='averaged initial w')
+
+    CALL init_averaging_grid(averaged_scalar_profile, cosmo_grid,           &
+            x = 0.5_wp * lx, y = 0.5_wp * ly, z = z, z0 = z0,               &
+            lonmin = lam_west, lonmax = lam_east,                           &
+            latmin = phi_south, latmax = phi_north,                         &
+            kind='scalar', name='centre geostrophic scalar')
+
+    CALL init_averaging_grid(averaged_w_profile, cosmo_grid,                &
+            x = 0.5_wp * lx, y = 0.5_wp * ly, z = zw, z0 = z0,              &
+            lonmin = lam_west, lonmax = lam_east,                           &
+            latmin = phi_south, latmax = phi_north,                         &
+            kind='w', name='centre geostrophic w')
+
+    CALL init_averaging_grid(south_averaged_scalar_profile, cosmo_grid,     &
+            x = 0.5_wp * lx, y = 0.5_wp * ly, z = z, z0 = z0,               &
+            lonmin = lam_west, lonmax = lam_east,                           &
+            latmin = phi_centre - averaging_angle,                          &
+            latmax = phi_centre,                                            &
+            kind='scalar', name='south geostrophic scalar')
+
+    CALL init_averaging_grid(north_averaged_scalar_profile, cosmo_grid,     &
+            x = 0.5_wp * lx, y = 0.5_wp * ly, z = z, z0 = z0,               &
+            lonmin = lam_west, lonmax = lam_east,                           &
+            latmin = phi_centre,                                            &
+            latmax = phi_centre + averaging_angle,                          &
+            kind='scalar', name='north geostrophic scalar')
+
+    CALL init_averaging_grid(west_averaged_scalar_profile, cosmo_grid,      &
+            x = 0.5_wp * lx, y = 0.5_wp * ly, z = z, z0 = z0,               &
+            lonmin = lam_centre - averaging_angle,                          &
+            lonmax = lam_centre,                                            &
+            latmin = phi_south, latmax = phi_north,                         &
+            kind='scalar', name='west geostrophic scalar')
+
+    CALL init_averaging_grid(east_averaged_scalar_profile, cosmo_grid,      &
+            x = 0.5_wp * lx, y = 0.5_wp * ly, z = z, z0 = z0,               &
+            lonmin = lam_centre,                                            &
+            lonmax = lam_centre + averaging_angle,                          &
+            latmin = phi_south, latmax = phi_north,                         &
+            kind='scalar', name='east geostrophic scalar')
 
 
@@ -1102,50 +1123,49 @@
 ! Section 4: Precompute neighbours and weights for interpolation              
 !------------------------------------------------------------------------------
-       interp_mode = 's'
-       CALL setup_interpolation(cosmo_grid, palm_grid, palm_intermediate, interp_mode, ic_mode=cfg % ic_mode)
-       IF (boundary_variables_required)  THEN
-          CALL setup_interpolation(cosmo_grid, scalars_east_grid, scalars_east_intermediate, interp_mode)
-          CALL setup_interpolation(cosmo_grid, scalars_west_grid, scalars_west_intermediate, interp_mode)
-          CALL setup_interpolation(cosmo_grid, scalars_north_grid, scalars_north_intermediate, interp_mode)
-          CALL setup_interpolation(cosmo_grid, scalars_south_grid, scalars_south_intermediate, interp_mode)
-          CALL setup_interpolation(cosmo_grid, scalars_top_grid, scalars_top_intermediate, interp_mode)
-       ENDIF
-
-       interp_mode = 'u'
-       CALL setup_interpolation(cosmo_grid, u_initial_grid, u_initial_intermediate, interp_mode, ic_mode=cfg % ic_mode)
-       IF (boundary_variables_required)  THEN
-          CALL setup_interpolation(cosmo_grid, u_east_grid, u_east_intermediate, interp_mode)
-          CALL setup_interpolation(cosmo_grid, u_west_grid, u_west_intermediate, interp_mode)
-          CALL setup_interpolation(cosmo_grid, u_north_grid, u_north_intermediate, interp_mode)
-          CALL setup_interpolation(cosmo_grid, u_south_grid, u_south_intermediate, interp_mode)
-          CALL setup_interpolation(cosmo_grid, u_top_grid, u_top_intermediate, interp_mode)
-       ENDIF
-
-       interp_mode = 'v'
-       CALL setup_interpolation(cosmo_grid, v_initial_grid, v_initial_intermediate, interp_mode, ic_mode=cfg % ic_mode)
-       IF (boundary_variables_required)  THEN
-          CALL setup_interpolation(cosmo_grid, v_east_grid, v_east_intermediate, interp_mode)
-          CALL setup_interpolation(cosmo_grid, v_west_grid, v_west_intermediate, interp_mode)
-          CALL setup_interpolation(cosmo_grid, v_north_grid, v_north_intermediate, interp_mode)
-          CALL setup_interpolation(cosmo_grid, v_south_grid, v_south_intermediate, interp_mode)
-          CALL setup_interpolation(cosmo_grid, v_top_grid, v_top_intermediate, interp_mode)
-       ENDIF
-
-       interp_mode = 'w'
-       CALL setup_interpolation(cosmo_grid, w_initial_grid, w_initial_intermediate, interp_mode, ic_mode=cfg % ic_mode)
-       IF (boundary_variables_required)  THEN
-          CALL setup_interpolation(cosmo_grid, w_east_grid, w_east_intermediate, interp_mode)
-          CALL setup_interpolation(cosmo_grid, w_west_grid, w_west_intermediate, interp_mode)
-          CALL setup_interpolation(cosmo_grid, w_north_grid, w_north_intermediate, interp_mode)
-          CALL setup_interpolation(cosmo_grid, w_south_grid, w_south_intermediate, interp_mode)
-          CALL setup_interpolation(cosmo_grid, w_top_grid, w_top_intermediate, interp_mode)
-       ENDIF
-
-       IF (TRIM(cfg % ic_mode) == 'profile')  THEN
-           !TODO: remove this conditional if not needed. 
-       ENDIF
-        
-
-    END SUBROUTINE setup_grids
+    interp_mode = 's'
+    CALL setup_interpolation(cosmo_grid, palm_grid, palm_intermediate, interp_mode, ic_mode=cfg%ic_mode)
+    IF (boundary_variables_required)  THEN
+       CALL setup_interpolation(cosmo_grid, scalars_east_grid, scalars_east_intermediate, interp_mode)
+       CALL setup_interpolation(cosmo_grid, scalars_west_grid, scalars_west_intermediate, interp_mode)
+       CALL setup_interpolation(cosmo_grid, scalars_north_grid, scalars_north_intermediate, interp_mode)
+       CALL setup_interpolation(cosmo_grid, scalars_south_grid, scalars_south_intermediate, interp_mode)
+       CALL setup_interpolation(cosmo_grid, scalars_top_grid, scalars_top_intermediate, interp_mode)
+    ENDIF
+
+    interp_mode = 'u'
+    CALL setup_interpolation(cosmo_grid, u_initial_grid, u_initial_intermediate, interp_mode, ic_mode=cfg%ic_mode)
+    IF (boundary_variables_required)  THEN
+       CALL setup_interpolation(cosmo_grid, u_east_grid, u_east_intermediate, interp_mode)
+       CALL setup_interpolation(cosmo_grid, u_west_grid, u_west_intermediate, interp_mode)
+       CALL setup_interpolation(cosmo_grid, u_north_grid, u_north_intermediate, interp_mode)
+       CALL setup_interpolation(cosmo_grid, u_south_grid, u_south_intermediate, interp_mode)
+       CALL setup_interpolation(cosmo_grid, u_top_grid, u_top_intermediate, interp_mode)
+    ENDIF
+
+    interp_mode = 'v'
+    CALL setup_interpolation(cosmo_grid, v_initial_grid, v_initial_intermediate, interp_mode, ic_mode=cfg%ic_mode)
+    IF (boundary_variables_required)  THEN
+       CALL setup_interpolation(cosmo_grid, v_east_grid, v_east_intermediate, interp_mode)
+       CALL setup_interpolation(cosmo_grid, v_west_grid, v_west_intermediate, interp_mode)
+       CALL setup_interpolation(cosmo_grid, v_north_grid, v_north_intermediate, interp_mode)
+       CALL setup_interpolation(cosmo_grid, v_south_grid, v_south_intermediate, interp_mode)
+       CALL setup_interpolation(cosmo_grid, v_top_grid, v_top_intermediate, interp_mode)
+    ENDIF
+
+    interp_mode = 'w'
+    CALL setup_interpolation(cosmo_grid, w_initial_grid, w_initial_intermediate, interp_mode, ic_mode=cfg%ic_mode)
+    IF (boundary_variables_required)  THEN
+       CALL setup_interpolation(cosmo_grid, w_east_grid, w_east_intermediate, interp_mode)
+       CALL setup_interpolation(cosmo_grid, w_west_grid, w_west_intermediate, interp_mode)
+       CALL setup_interpolation(cosmo_grid, w_north_grid, w_north_intermediate, interp_mode)
+       CALL setup_interpolation(cosmo_grid, w_south_grid, w_south_intermediate, interp_mode)
+       CALL setup_interpolation(cosmo_grid, w_top_grid, w_top_intermediate, interp_mode)
+    ENDIF
+
+    IF (TRIM(cfg%ic_mode) == 'profile')  THEN
+        !TODO: remove this conditional if not needed. 
+    ENDIF
+
+ END SUBROUTINE setup_grids
 
 
@@ -1156,15 +1176,15 @@
 !> vertical interpolation.
 !------------------------------------------------------------------------------!
-    SUBROUTINE setup_interpolation(cosmo_grid, grid, intermediate_grid, kind, ic_mode)
-
-       TYPE(grid_definition), INTENT(IN), TARGET    ::  cosmo_grid
-       TYPE(grid_definition), INTENT(INOUT), TARGET ::  grid, intermediate_grid
-       CHARACTER, INTENT(IN)                        ::  kind
-       CHARACTER(LEN=*), INTENT(IN), OPTIONAL       ::  ic_mode
-
-       REAL(dp), DIMENSION(:), POINTER     ::  cosmo_lat, cosmo_lon
-       REAL(dp), DIMENSION(:,:,:), POINTER ::  cosmo_h
-
-       LOGICAL :: setup_volumetric
+ SUBROUTINE setup_interpolation(cosmo_grid, grid, intermediate_grid, kind, ic_mode)
+
+    TYPE(grid_definition), INTENT(IN), TARGET    ::  cosmo_grid
+    TYPE(grid_definition), INTENT(INOUT), TARGET ::  grid, intermediate_grid
+    CHARACTER, INTENT(IN)                        ::  kind
+    CHARACTER(LEN=*), INTENT(IN), OPTIONAL       ::  ic_mode
+
+    REAL(wp), DIMENSION(:), POINTER     ::  cosmo_lat, cosmo_lon
+    REAL(wp), DIMENSION(:,:,:), POINTER ::  cosmo_h
+
+    LOGICAL :: setup_volumetric
 
 !------------------------------------------------------------------------------
@@ -1172,52 +1192,52 @@
 !------------------------------------------------------------------------------
 !
-!--    Select horizontal coordinates according to kind of points (s/w, u, v)
-       SELECT CASE(kind)
-
-!
-!--    scalars
-       CASE('s')
-
-          cosmo_lat => cosmo_grid % lat
-          cosmo_lon => cosmo_grid % lon
-          cosmo_h   => cosmo_grid % hfl
-!
-!--    vertical velocity
-       CASE('w')
-
-          cosmo_lat => cosmo_grid % lat
-          cosmo_lon => cosmo_grid % lon
-          cosmo_h   => cosmo_grid % hhl
-!
-!--    x velocity
-       CASE('u')
-
-          cosmo_lat => cosmo_grid % lat
-          cosmo_lon => cosmo_grid % lonu
-          cosmo_h   => cosmo_grid % hfl
-
-!
-!--    y velocity
-       CASE('v')
-
-          cosmo_lat => cosmo_grid % latv
-          cosmo_lon => cosmo_grid % lon
-          cosmo_h   => cosmo_grid % hfl
-
-       CASE DEFAULT
-
-          message = "Interpolation quantity '" // kind // "' is not supported."
-          CALL inifor_abort('setup_interpolation', message)
-
-       END SELECT
-
-       CALL find_horizontal_neighbours(cosmo_lat, cosmo_lon,                   &
-          intermediate_grid % clat, intermediate_grid % clon,                  &
-          intermediate_grid % ii, intermediate_grid % jj)
-
-       CALL compute_horizontal_interp_weights(cosmo_lat, cosmo_lon,            &
-          intermediate_grid % clat, intermediate_grid % clon,                  &
-          intermediate_grid % ii, intermediate_grid % jj,                      &
-          intermediate_grid % w_horiz)
+!-- Select horizontal coordinates according to kind of points (s/w, u, v)
+    SELECT CASE(kind)
+
+!
+!-- scalars
+    CASE('s')
+
+       cosmo_lat => cosmo_grid%lat
+       cosmo_lon => cosmo_grid%lon
+       cosmo_h   => cosmo_grid%hfl
+!
+!-- vertical velocity
+    CASE('w')
+
+       cosmo_lat => cosmo_grid%lat
+       cosmo_lon => cosmo_grid%lon
+       cosmo_h   => cosmo_grid%hhl
+!
+!-- x velocity
+    CASE('u')
+
+       cosmo_lat => cosmo_grid%lat
+       cosmo_lon => cosmo_grid%lonu
+       cosmo_h   => cosmo_grid%hfl
+
+!
+!-- y velocity
+    CASE('v')
+
+       cosmo_lat => cosmo_grid%latv
+       cosmo_lon => cosmo_grid%lon
+       cosmo_h   => cosmo_grid%hfl
+
+    CASE DEFAULT
+
+       message = "Interpolation quantity '" // kind // "' is not supported."
+       CALL inifor_abort('setup_interpolation', message)
+
+    END SELECT
+
+    CALL find_horizontal_neighbours(cosmo_lat, cosmo_lon,                   &
+       intermediate_grid%clat, intermediate_grid%clon,                  &
+       intermediate_grid%ii, intermediate_grid%jj)
+
+    CALL compute_horizontal_interp_weights(cosmo_lat, cosmo_lon,            &
+       intermediate_grid%clat, intermediate_grid%clon,                  &
+       intermediate_grid%ii, intermediate_grid%jj,                      &
+       intermediate_grid%w_horiz)
 
 !------------------------------------------------------------------------------
@@ -1226,28 +1246,28 @@
 
 !
-!--    If profile initialization is chosen, we--somewhat counterintuitively--
-!--    don't need to compute vertical interpolation weights. At least, we
-!--    don't need them on the intermediate grid, which fills the entire PALM
-!--    domain volume. Instead we need vertical weights for the intermediate
-!--    profile grids, which get computed in setup_averaging().
-       setup_volumetric = .TRUE.
-       IF (PRESENT(ic_mode))  THEN
-          IF (TRIM(ic_mode) == 'profile')  setup_volumetric = .FALSE.
-       ENDIF
-
-       IF (setup_volumetric)  THEN
-          ALLOCATE( intermediate_grid % h(0:intermediate_grid % nx,            &
-                                          0:intermediate_grid % ny,            &
-                                          0:intermediate_grid % nz) ) 
-          intermediate_grid % h(:,:,:) = - EARTH_RADIUS
-
-!
-!--       For w points, use hhl, for scalars use hfl
-!--       compute the full heights for the intermediate grids
-          CALL interpolate_2d(cosmo_h, intermediate_grid % h, intermediate_grid)
-          CALL find_vertical_neighbours_and_weights_interp(grid, intermediate_grid)
-       ENDIF
-       
-    END SUBROUTINE setup_interpolation
+!-- If profile initialization is chosen, we--somewhat counterintuitively--
+!-- don't need to compute vertical interpolation weights. At least, we
+!-- don't need them on the intermediate grid, which fills the entire PALM
+!-- domain volume. Instead we need vertical weights for the intermediate
+!-- profile grids, which get computed in setup_averaging().
+    setup_volumetric = .TRUE.
+    IF (PRESENT(ic_mode))  THEN
+       IF (TRIM(ic_mode) == 'profile')  setup_volumetric = .FALSE.
+    ENDIF
+
+    IF (setup_volumetric)  THEN
+       ALLOCATE( intermediate_grid%h(0:intermediate_grid%nx,            &
+                                     0:intermediate_grid%ny,            &
+                                     0:intermediate_grid%nz) ) 
+       intermediate_grid%h(:,:,:) = - EARTH_RADIUS
+
+!
+!--    For w points, use hhl, for scalars use hfl
+!--    compute the full heights for the intermediate grids
+       CALL interpolate_2d(cosmo_h, intermediate_grid%h, intermediate_grid)
+       CALL find_vertical_neighbours_and_weights_interp(grid, intermediate_grid)
+    ENDIF
+       
+ END SUBROUTINE setup_interpolation
 
 !------------------------------------------------------------------------------!
@@ -1281,220 +1301,220 @@
 !> grid : Grid variable to be initialized.
 !------------------------------------------------------------------------------!
-    SUBROUTINE init_grid_definition(kind, xmin, xmax, ymin, ymax,              &
-                                    x0, y0, z0, nx, ny, nz, z, zw, grid, ic_mode)
-        CHARACTER(LEN=*), INTENT(IN)           ::  kind
-        CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  ic_mode
-        INTEGER, INTENT(IN)                    ::  nx, ny, nz
-        REAL(dp), INTENT(IN)                   ::  xmin, xmax, ymin, ymax
-        REAL(dp), INTENT(IN)                   ::  x0, y0, z0
-        REAL(dp), INTENT(IN), TARGET, OPTIONAL ::  z(:)
-        REAL(dp), INTENT(IN), TARGET, OPTIONAL ::  zw(:)
-        TYPE(grid_definition), INTENT(INOUT)   ::  grid
-
-        grid % nx = nx
-        grid % ny = ny
-        grid % nz = nz
-
-        grid % lx = xmax - xmin
-        grid % ly = ymax - ymin
-
-        grid % x0 = x0
-        grid % y0 = y0
-        grid % z0 = z0
-
-        SELECT CASE( TRIM(kind) )
-
-        CASE('boundary')
-
-           IF (.NOT.PRESENT(z))  THEN
-              message = "z has not been passed but is required for 'boundary' grids"
-              CALL inifor_abort('init_grid_definition', message)
-           ENDIF
-
-           ALLOCATE( grid % x(0:nx) )
-           CALL linspace(xmin, xmax, grid % x)
-
-           ALLOCATE( grid % y(0:ny) )
-           CALL linspace(ymin, ymax, grid % y)
-
-           grid % z => z
-
-!
-!--        Allocate neighbour indices and weights
-           IF (TRIM(ic_mode) .NE. 'profile')  THEN
-              ALLOCATE( grid % kk(0:nx, 0:ny, 1:nz, 2) )
-              grid % kk(:,:,:,:) = -1
-
-              ALLOCATE( grid % w_verti(0:nx, 0:ny, 1:nz, 2) )
-              grid % w_verti(:,:,:,:) = 0.0_dp
-           ENDIF
-        
-        CASE('boundary intermediate')
-
-           ALLOCATE( grid % x(0:nx) )
-           CALL linspace(xmin, xmax, grid % x)
-
-           ALLOCATE( grid % y(0:ny) )
-           CALL linspace(ymin, ymax, grid % y)
-
-           ALLOCATE( grid % clon(0:nx, 0:ny), grid % clat(0:nx, 0:ny)  )
-
-           CALL rotate_to_cosmo(                                               &
-              phir = project( grid % y, y0, EARTH_RADIUS ) ,                   & ! = plate-carree latitude
-              lamr = project( grid % x, x0, EARTH_RADIUS ) ,                   & ! = plate-carree longitude
-              phip = phi_cn, lamp = lambda_cn,                                 &
-              phi  = grid % clat,                                              &
-              lam  = grid % clon,                                              &
-              gam  = gam                                                       &
-           )
-
-!
-!--        Allocate neighbour indices and weights
-           ALLOCATE( grid % ii(0:nx, 0:ny, 4),                                 &
-                     grid % jj(0:nx, 0:ny, 4) )
-           grid % ii(:,:,:)   = -1
-           grid % jj(:,:,:)   = -1
-
-           ALLOCATE( grid % w_horiz(0:nx, 0:ny, 4) )
-           grid % w_horiz(:,:,:)   = 0.0_dp
-        
-!
-!--     This mode initializes a Cartesian PALM-4U grid and adds the
-!--     corresponding latitudes and longitudes of the rotated pole grid.
-        CASE('palm')
-
-           IF (.NOT.PRESENT(z))  THEN
-              message = "z has not been passed but is required for 'palm' grids"
-              CALL inifor_abort('init_grid_definition', message)
-           ENDIF
-
-           IF (.NOT.PRESENT(zw))  THEN
-              message = "zw has not been passed but is required for 'palm' grids"
-              CALL inifor_abort('init_grid_definition', message)
-           ENDIF
-
-           grid % name(1) = 'x and lon'
-           grid % name(2) = 'y and lat'
-           grid % name(3) = 'z'
-
-!
-!--        TODO: Remove use of global dx, dy, dz variables. Consider
-!--        TODO: associating global x,y, and z arrays.
-           ALLOCATE( grid % x(0:nx),   grid % y(0:ny) )
-           ALLOCATE( grid % xu(1:nx),  grid % yv(1:ny) )
-           CALL linspace(xmin + 0.5_dp* dx, xmax - 0.5_dp* dx, grid % x)
-           CALL linspace(ymin + 0.5_dp* dy, ymax - 0.5_dp* dy, grid % y)
-           grid % z => z
-           CALL linspace(xmin +  dx, xmax -  dx, grid % xu)
-           CALL linspace(ymin +  dy, ymax -  dy, grid % yv)
-           grid % zw => zw
-
-           grid % depths => depths
-
-!
-!--        Allocate neighbour indices and weights
-           IF (TRIM(ic_mode) .NE. 'profile')  THEN
-              ALLOCATE( grid % kk(0:nx, 0:ny, 1:nz, 2) )
-              grid % kk(:,:,:,:) = -1
-
-              ALLOCATE( grid % w_verti(0:nx, 0:ny, 1:nz, 2) )
-              grid % w_verti(:,:,:,:) = 0.0_dp
-           ENDIF
-
-        CASE('palm intermediate')
-
-           grid % name(1) = 'x and lon'
-           grid % name(2) = 'y and lat'
-           grid % name(3) = 'interpolated hhl or hfl'
-
-!
-!--        TODO: Remove use of global dx, dy, dz variables. Consider
-!--        TODO: associating global x,y, and z arrays.
-           ALLOCATE( grid % x(0:nx),   grid % y(0:ny) )
-           ALLOCATE( grid % xu(1:nx),  grid % yv(1:ny) )
-           CALL linspace(xmin + 0.5_dp*dx, xmax - 0.5_dp*dx, grid % x)
-           CALL linspace(ymin + 0.5_dp*dy, ymax - 0.5_dp*dy, grid % y)
-           CALL linspace(xmin + dx, xmax - dx, grid % xu)
-           CALL linspace(ymin + dy, ymax - dy, grid % yv)
-
-           grid % depths => depths
-
-!
-!--        Allocate rotated-pole coordinates, clon is for (c)osmo-de (lon)gitude
-           ALLOCATE( grid % clon(0:nx, 0:ny),   grid % clat(0:nx, 0:ny)  )
-           ALLOCATE( grid % clonu(1:nx, 0:ny),  grid % clatu(1:nx, 0:ny) )
-           ALLOCATE( grid % clonv(0:nx, 1:ny),  grid % clatv(0:nx, 1:ny) )
-
-!
-!--        Compute rotated-pole coordinates of...
-!--        ... PALM-4U centres
-           CALL rotate_to_cosmo(                                               &
-              phir = project( grid % y, y0, EARTH_RADIUS ) , & ! = plate-carree latitude
-              lamr = project( grid % x, x0, EARTH_RADIUS ) , & ! = plate-carree longitude
-              phip = phi_cn, lamp = lambda_cn,                                 &
-              phi  = grid % clat,                                              &
-              lam  = grid % clon,                                              &
-              gam  = gam                                                       &
-           )
-
-!
-!--        ... PALM-4U u winds
-           CALL rotate_to_cosmo(                                               &
-              phir = project( grid % y,  y0, EARTH_RADIUS ), & ! = plate-carree latitude
-              lamr = project( grid % xu, x0, EARTH_RADIUS ), & ! = plate-carree longitude
-              phip = phi_cn, lamp = lambda_cn,                                 &
-              phi  = grid % clatu,                                             &
-              lam  = grid % clonu,                                             &
-              gam  = gam                                                       &
-           )
-
-!
-!--        ... PALM-4U v winds
-           CALL rotate_to_cosmo(                                               &
-              phir = project( grid % yv, y0, EARTH_RADIUS ), & ! = plate-carree latitude
-              lamr = project( grid % x,  x0, EARTH_RADIUS ), & ! = plate-carree longitude
-              phip = phi_cn, lamp = lambda_cn,                                 &
-              phi  = grid % clatv,                                             &
-              lam  = grid % clonv,                                             &
-              gam  = gam                                                       &
-           )
-
-!
-!--        Allocate neighbour indices and weights
-           ALLOCATE( grid % ii(0:nx, 0:ny, 4),                                 &
-                     grid % jj(0:nx, 0:ny, 4) )
-           grid % ii(:,:,:)   = -1
-           grid % jj(:,:,:)   = -1
-
-           ALLOCATE( grid % w_horiz(0:nx, 0:ny, 4) )
-           grid % w_horiz(:,:,:)   = 0.0_dp
-
-        CASE('cosmo-de')
-           grid % name(1) = 'rlon'         ! of COMSO-DE cell centres (scalars)
-           grid % name(2) = 'rlat'         ! of COMSO-DE cell centres (scalars)
-           grid % name(3) = 'height'
-
-           ALLOCATE( grid % lon(0:nx),   grid % lat(0:ny)  )
-           ALLOCATE( grid % lonu(0:nx),  grid % latv(0:ny) )
-
-           CALL linspace(xmin, xmax, grid % lon)
-           CALL linspace(ymin, ymax, grid % lat)
-           grid % lonu(:) = grid % lon + 0.5_dp * (grid % lx / grid % nx)
-           grid % latv(:) = grid % lat + 0.5_dp * (grid % ly / grid % ny)
-
-!
-!--        Point to heights of half levels (hhl) and compute heights of full
-!--        levels (hfl) as arithmetic averages
-           grid % hhl => hhl
-           grid % hfl => hfl
-           grid % depths => depths
-
-        CASE DEFAULT
-            message = "Grid kind '" // TRIM(kind) // "' is not recognized."
-            CALL inifor_abort('init_grid_definition', message)
-
-        END SELECT
-
-    END SUBROUTINE init_grid_definition
+ SUBROUTINE init_grid_definition(kind, xmin, xmax, ymin, ymax,              &
+                                 x0, y0, z0, nx, ny, nz, z, zw, grid, ic_mode)
+    CHARACTER(LEN=*), INTENT(IN)           ::  kind
+    CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  ic_mode
+    INTEGER, INTENT(IN)                    ::  nx, ny, nz
+    REAL(wp), INTENT(IN)                   ::  xmin, xmax, ymin, ymax
+    REAL(wp), INTENT(IN)                   ::  x0, y0, z0
+    REAL(wp), INTENT(IN), TARGET, OPTIONAL ::  z(:)
+    REAL(wp), INTENT(IN), TARGET, OPTIONAL ::  zw(:)
+    TYPE(grid_definition), INTENT(INOUT)   ::  grid
+
+    grid%nx = nx
+    grid%ny = ny
+    grid%nz = nz
+
+    grid%lx = xmax - xmin
+    grid%ly = ymax - ymin
+
+    grid%x0 = x0
+    grid%y0 = y0
+    grid%z0 = z0
+
+    SELECT CASE( TRIM(kind) )
+
+       CASE('boundary')
+       
+          IF (.NOT.PRESENT(z))  THEN
+             message = "z has not been passed but is required for 'boundary' grids"
+             CALL inifor_abort('init_grid_definition', message)
+          ENDIF
+       
+          ALLOCATE( grid%x(0:nx) )
+          CALL linspace(xmin, xmax, grid%x)
+       
+          ALLOCATE( grid%y(0:ny) )
+          CALL linspace(ymin, ymax, grid%y)
+       
+          grid%z => z
+       
+!      
+!--       Allocate neighbour indices and weights
+          IF (TRIM(ic_mode) .NE. 'profile')  THEN
+             ALLOCATE( grid%kk(0:nx, 0:ny, 1:nz, 2) )
+             grid%kk(:,:,:,:) = -1
+       
+             ALLOCATE( grid%w_verti(0:nx, 0:ny, 1:nz, 2) )
+             grid%w_verti(:,:,:,:) = 0.0_wp
+          ENDIF
+       
+       CASE('boundary intermediate')
+       
+          ALLOCATE( grid%x(0:nx) )
+          CALL linspace(xmin, xmax, grid%x)
+       
+          ALLOCATE( grid%y(0:ny) )
+          CALL linspace(ymin, ymax, grid%y)
+       
+          ALLOCATE( grid%clon(0:nx, 0:ny), grid%clat(0:nx, 0:ny)  )
+       
+          CALL rotate_to_cosmo(                                               &
+             phir = project( grid%y, y0, EARTH_RADIUS ) ,                   & ! = plate-carree latitude
+             lamr = project( grid%x, x0, EARTH_RADIUS ) ,                   & ! = plate-carree longitude
+             phip = phi_cn, lamp = lambda_cn,                                 &
+             phi  = grid%clat,                                              &
+             lam  = grid%clon,                                              &
+             gam  = gam                                                       &
+          )
+       
+!      
+!--       Allocate neighbour indices and weights
+          ALLOCATE( grid%ii(0:nx, 0:ny, 4),                                 &
+                    grid%jj(0:nx, 0:ny, 4) )
+          grid%ii(:,:,:)   = -1
+          grid%jj(:,:,:)   = -1
+       
+          ALLOCATE( grid%w_horiz(0:nx, 0:ny, 4) )
+          grid%w_horiz(:,:,:)   = 0.0_wp
+       
+!      
+!--    This mode initializes a Cartesian PALM-4U grid and adds the
+!--    corresponding latitudes and longitudes of the rotated pole grid.
+       CASE('palm')
+       
+          IF (.NOT.PRESENT(z))  THEN
+             message = "z has not been passed but is required for 'palm' grids"
+             CALL inifor_abort('init_grid_definition', message)
+          ENDIF
+       
+          IF (.NOT.PRESENT(zw))  THEN
+             message = "zw has not been passed but is required for 'palm' grids"
+             CALL inifor_abort('init_grid_definition', message)
+          ENDIF
+       
+          grid%name(1) = 'x and lon'
+          grid%name(2) = 'y and lat'
+          grid%name(3) = 'z'
+       
+!      
+!--       TODO: Remove use of global dx, dy, dz variables. Consider
+!--       TODO: associating global x,y, and z arrays.
+          ALLOCATE( grid%x(0:nx),   grid%y(0:ny) )
+          ALLOCATE( grid%xu(1:nx),  grid%yv(1:ny) )
+          CALL linspace(xmin + 0.5_wp* dx, xmax - 0.5_wp* dx, grid%x)
+          CALL linspace(ymin + 0.5_wp* dy, ymax - 0.5_wp* dy, grid%y)
+          grid%z => z
+          CALL linspace(xmin +  dx, xmax -  dx, grid%xu)
+          CALL linspace(ymin +  dy, ymax -  dy, grid%yv)
+          grid%zw => zw
+       
+          grid%depths => depths
+       
+!      
+!--       Allocate neighbour indices and weights
+          IF (TRIM(ic_mode) .NE. 'profile')  THEN
+             ALLOCATE( grid%kk(0:nx, 0:ny, 1:nz, 2) )
+             grid%kk(:,:,:,:) = -1
+       
+             ALLOCATE( grid%w_verti(0:nx, 0:ny, 1:nz, 2) )
+             grid%w_verti(:,:,:,:) = 0.0_wp
+          ENDIF
+       
+       CASE('palm intermediate')
+       
+          grid%name(1) = 'x and lon'
+          grid%name(2) = 'y and lat'
+          grid%name(3) = 'interpolated hhl or hfl'
+       
+!      
+!--       TODO: Remove use of global dx, dy, dz variables. Consider
+!--       TODO: associating global x,y, and z arrays.
+          ALLOCATE( grid%x(0:nx),   grid%y(0:ny) )
+          ALLOCATE( grid%xu(1:nx),  grid%yv(1:ny) )
+          CALL linspace(xmin + 0.5_wp*dx, xmax - 0.5_wp*dx, grid%x)
+          CALL linspace(ymin + 0.5_wp*dy, ymax - 0.5_wp*dy, grid%y)
+          CALL linspace(xmin + dx, xmax - dx, grid%xu)
+          CALL linspace(ymin + dy, ymax - dy, grid%yv)
+       
+          grid%depths => depths
+       
+!      
+!--       Allocate rotated-pole coordinates, clon is for (c)osmo-de (lon)gitude
+          ALLOCATE( grid%clon(0:nx, 0:ny),   grid%clat(0:nx, 0:ny)  )
+          ALLOCATE( grid%clonu(1:nx, 0:ny),  grid%clatu(1:nx, 0:ny) )
+          ALLOCATE( grid%clonv(0:nx, 1:ny),  grid%clatv(0:nx, 1:ny) )
+       
+!      
+!--       Compute rotated-pole coordinates of...
+!--       ... PALM-4U centres
+          CALL rotate_to_cosmo(                                               &
+             phir = project( grid%y, y0, EARTH_RADIUS ) , & ! = plate-carree latitude
+             lamr = project( grid%x, x0, EARTH_RADIUS ) , & ! = plate-carree longitude
+             phip = phi_cn, lamp = lambda_cn,                                 &
+             phi  = grid%clat,                                              &
+             lam  = grid%clon,                                              &
+             gam  = gam                                                       &
+          )
+       
+!      
+!--       ... PALM-4U u winds
+          CALL rotate_to_cosmo(                                               &
+             phir = project( grid%y,  y0, EARTH_RADIUS ), & ! = plate-carree latitude
+             lamr = project( grid%xu, x0, EARTH_RADIUS ), & ! = plate-carree longitude
+             phip = phi_cn, lamp = lambda_cn,                                 &
+             phi  = grid%clatu,                                             &
+             lam  = grid%clonu,                                             &
+             gam  = gam                                                       &
+          )
+       
+!      
+!--       ... PALM-4U v winds
+          CALL rotate_to_cosmo(                                               &
+             phir = project( grid%yv, y0, EARTH_RADIUS ), & ! = plate-carree latitude
+             lamr = project( grid%x,  x0, EARTH_RADIUS ), & ! = plate-carree longitude
+             phip = phi_cn, lamp = lambda_cn,                                 &
+             phi  = grid%clatv,                                             &
+             lam  = grid%clonv,                                             &
+             gam  = gam                                                       &
+          )
+       
+!      
+!--       Allocate neighbour indices and weights
+          ALLOCATE( grid%ii(0:nx, 0:ny, 4),                                 &
+                    grid%jj(0:nx, 0:ny, 4) )
+          grid%ii(:,:,:)   = -1
+          grid%jj(:,:,:)   = -1
+       
+          ALLOCATE( grid%w_horiz(0:nx, 0:ny, 4) )
+          grid%w_horiz(:,:,:)   = 0.0_wp
+       
+       CASE('cosmo-de')
+          grid%name(1) = 'rlon'         ! of COMSO-DE cell centres (scalars)
+          grid%name(2) = 'rlat'         ! of COMSO-DE cell centres (scalars)
+          grid%name(3) = 'height'
+       
+          ALLOCATE( grid%lon(0:nx),   grid%lat(0:ny)  )
+          ALLOCATE( grid%lonu(0:nx),  grid%latv(0:ny) )
+       
+          CALL linspace(xmin, xmax, grid%lon)
+          CALL linspace(ymin, ymax, grid%lat)
+          grid%lonu(:) = grid%lon + 0.5_wp * (grid%lx / grid%nx)
+          grid%latv(:) = grid%lat + 0.5_wp * (grid%ly / grid%ny)
+       
+!      
+!--       Point to heights of half levels (hhl) and compute heights of full
+!--       levels (hfl) as arithmetic averages
+          grid%hhl => hhl
+          grid%hfl => hfl
+          grid%depths => depths
+       
+       CASE DEFAULT
+           message = "Grid kind '" // TRIM(kind) // "' is not recognized."
+           CALL inifor_abort('init_grid_definition', message)
+
+    END SELECT
+
+ END SUBROUTINE init_grid_definition
 
 
@@ -1527,148 +1547,147 @@
 !> avg_grid : averagin grid to be initialized
 !------------------------------------------------------------------------------!
-    SUBROUTINE init_averaging_grid(avg_grid, cosmo_grid, x, y, z, z0,          &
-       lonmin, lonmax, latmin, latmax, kind, name)
-
-       TYPE(grid_definition), INTENT(INOUT) ::  avg_grid
-       TYPE(grid_definition), INTENT(IN)    ::  cosmo_grid
-       REAL(dp), INTENT(IN)                 ::  x, y, z0
-       REAL(dp), INTENT(IN), TARGET         ::  z(:)
-       REAL(dp), INTENT(IN)                 ::  lonmin !< lower longitude bound of the averaging grid region [COSMO rotated-pole rad]
-       REAL(dp), INTENT(IN)                 ::  lonmax !< upper longitude bound of the averaging grid region [COSMO rotated-pole rad]
-       REAL(dp), INTENT(IN)                 ::  latmin !< lower latitude bound of the averaging grid region [COSMO rotated-pole rad]
-       REAL(dp), INTENT(IN)                 ::  latmax !< lower latitude bound of the averaging grid region [COSMO rotated-pole rad]
-
-       CHARACTER(LEN=*), INTENT(IN)         ::  kind
-       CHARACTER(LEN=*), INTENT(IN)         ::  name
-
-       LOGICAL                              ::  level_based_averaging
-
-       ALLOCATE( avg_grid % x(1) )
-       ALLOCATE( avg_grid % y(1) )
-       avg_grid % x(1) = x
-       avg_grid % y(1) = y
-       avg_grid % z => z
-       avg_grid % z0 = z0
-
-       avg_grid % nz = SIZE(z, 1)
-
-       ALLOCATE( avg_grid % lon(2) )
-       ALLOCATE( avg_grid % lat(2) )
-       avg_grid % lon(1:2) = (/lonmin, lonmax/)
-       avg_grid % lat(1:2) = (/latmin, latmax/)
-
-       avg_grid % kind = TRIM(kind)
-       avg_grid % name(1) = TRIM(name)
-
-!
-!--    Find and store COSMO columns that fall into the coordinate range
-!--    given by avg_grid % clon, % clat
-       CALL get_cosmo_averaging_region(avg_grid, cosmo_grid)
-
-       ALLOCATE (avg_grid % kkk(avg_grid % n_columns, avg_grid % nz, 2) )
-       ALLOCATE (avg_grid % w(avg_grid % n_columns, avg_grid % nz, 2) )
-!
-!--    Compute average COSMO levels in the averaging region
-       SELECT CASE(avg_grid % kind)
+ SUBROUTINE init_averaging_grid(avg_grid, cosmo_grid, x, y, z, z0,          &
+    lonmin, lonmax, latmin, latmax, kind, name)
+
+    TYPE(grid_definition), INTENT(INOUT) ::  avg_grid
+    TYPE(grid_definition), INTENT(IN)    ::  cosmo_grid
+    REAL(wp), INTENT(IN)                 ::  x, y, z0
+    REAL(wp), INTENT(IN), TARGET         ::  z(:)
+    REAL(wp), INTENT(IN)                 ::  lonmin !< lower longitude bound of the averaging grid region [COSMO rotated-pole rad]
+    REAL(wp), INTENT(IN)                 ::  lonmax !< upper longitude bound of the averaging grid region [COSMO rotated-pole rad]
+    REAL(wp), INTENT(IN)                 ::  latmin !< lower latitude bound of the averaging grid region [COSMO rotated-pole rad]
+    REAL(wp), INTENT(IN)                 ::  latmax !< lower latitude bound of the averaging grid region [COSMO rotated-pole rad]
+
+    CHARACTER(LEN=*), INTENT(IN)         ::  kind
+    CHARACTER(LEN=*), INTENT(IN)         ::  name
+
+    LOGICAL                              ::  level_based_averaging
+
+    ALLOCATE( avg_grid%x(1) )
+    ALLOCATE( avg_grid%y(1) )
+    avg_grid%x(1) = x
+    avg_grid%y(1) = y
+    avg_grid%z => z
+    avg_grid%z0 = z0
+
+    avg_grid%nz = SIZE(z, 1)
+
+    ALLOCATE( avg_grid%lon(2) )
+    ALLOCATE( avg_grid%lat(2) )
+    avg_grid%lon(1:2) = (/lonmin, lonmax/)
+    avg_grid%lat(1:2) = (/latmin, latmax/)
+
+    avg_grid%kind = TRIM(kind)
+    avg_grid%name(1) = TRIM(name)
+
+!
+!-- Find and store COSMO columns that fall into the coordinate range
+!-- given by avg_grid%clon, %clat
+    CALL get_cosmo_averaging_region(avg_grid, cosmo_grid)
+
+    ALLOCATE (avg_grid%kkk(avg_grid%n_columns, avg_grid%nz, 2) )
+    ALLOCATE (avg_grid%w(avg_grid%n_columns, avg_grid%nz, 2) )
+!
+!-- Compute average COSMO levels in the averaging region
+    SELECT CASE(avg_grid%kind)
 
        CASE('scalar', 'u', 'v')
-          avg_grid % cosmo_h => cosmo_grid % hfl
+          avg_grid%cosmo_h => cosmo_grid%hfl
 
        CASE('w')
-          avg_grid % cosmo_h => cosmo_grid % hhl
+          avg_grid%cosmo_h => cosmo_grid%hhl
 
        CASE DEFAULT
-          message = "Averaging grid kind '" // TRIM(avg_grid % kind) // &
+          message = "Averaging grid kind '" // TRIM(avg_grid%kind) // &
                     "' is not supported. Use 'scalar', 'u', or 'v'."
           CALL inifor_abort('get_cosmo_averaging_region', message)
 
-       END SELECT
-
-!
-!--    For level-besed averaging, compute average heights
-       !level_based_averaging = ( TRIM(mode) == 'level' )
-       level_based_averaging = ( TRIM(cfg % averaging_mode) == 'level' )
-       IF (level_based_averaging)  THEN
-          ALLOCATE(avg_grid % h(1,1,SIZE(avg_grid % cosmo_h, 3)) )
+    END SELECT
+
+!
+!-- For level-besed averaging, compute average heights
+    level_based_averaging = ( TRIM(cfg%averaging_mode) == 'level' )
+    IF (level_based_averaging)  THEN
+       ALLOCATE(avg_grid%h(1,1,SIZE(avg_grid%cosmo_h, 3)) )
  
-          CALL average_2d(avg_grid % cosmo_h, avg_grid % h(1,1,:),             &
-                          avg_grid % iii, avg_grid % jjj)
-
-       ENDIF
-
-!
-!--    Compute vertical weights and neighbours
-       CALL find_vertical_neighbours_and_weights_average(                      &
-          avg_grid, level_based_averaging                                      &
-       )
-
-    END SUBROUTINE init_averaging_grid
-
-
-    SUBROUTINE get_cosmo_averaging_region(avg_grid, cosmo_grid)
-       TYPE(grid_definition), INTENT(INOUT)         ::  avg_grid
-       TYPE(grid_definition), TARGET, INTENT(IN)    ::  cosmo_grid
-
-       REAL(dp), DIMENSION(:), POINTER              ::  cosmo_lon, cosmo_lat
-       REAL(dp)                                     ::  dlon, dlat
-
-       INTEGER ::  i, j, imin, imax, jmin, jmax, l, nx, ny
-
-
-       SELECT CASE( TRIM(avg_grid % kind) )
+       CALL average_2d(avg_grid%cosmo_h, avg_grid%h(1,1,:),             &
+                       avg_grid%iii, avg_grid%jjj)
+
+    ENDIF
+
+!
+!-- Compute vertical weights and neighbours
+    CALL find_vertical_neighbours_and_weights_average(                      &
+       avg_grid, level_based_averaging                                      &
+    )
+
+ END SUBROUTINE init_averaging_grid
+
+
+ SUBROUTINE get_cosmo_averaging_region(avg_grid, cosmo_grid)
+    TYPE(grid_definition), INTENT(INOUT)         ::  avg_grid
+    TYPE(grid_definition), TARGET, INTENT(IN)    ::  cosmo_grid
+
+    REAL(wp), DIMENSION(:), POINTER              ::  cosmo_lon, cosmo_lat
+    REAL(wp)                                     ::  dlon, dlat
+
+    INTEGER ::  i, j, imin, imax, jmin, jmax, l, nx, ny
+
+
+    SELECT CASE( TRIM(avg_grid%kind) )
 
        CASE('scalar', 'w')
-          cosmo_lon => cosmo_grid % lon
-          cosmo_lat => cosmo_grid % lat
+          cosmo_lon => cosmo_grid%lon
+          cosmo_lat => cosmo_grid%lat
 
        CASE('u')
-          cosmo_lon => cosmo_grid % lonu
-          cosmo_lat => cosmo_grid % lat
+          cosmo_lon => cosmo_grid%lonu
+          cosmo_lat => cosmo_grid%lat
 
        CASE('v')
-          cosmo_lon => cosmo_grid % lon
-          cosmo_lat => cosmo_grid % latv
+          cosmo_lon => cosmo_grid%lon
+          cosmo_lat => cosmo_grid%latv
 
        CASE DEFAULT
-          message = "Averaging grid kind '" // TRIM(avg_grid % kind) // &
+          message = "Averaging grid kind '" // TRIM(avg_grid%kind) // &
                     "' is not supported. Use 'scalar', 'u', or 'v'."
           CALL inifor_abort('get_cosmo_averaging_region', message)
 
-       END SELECT
-
-!
-!--    FIXME: make dlon, dlat parameters of the grid_defintion type
-       dlon = cosmo_lon(1) - cosmo_lon(0)
-       dlat = cosmo_lat(1) - cosmo_lat(0)
-
-       imin = FLOOR  ( (avg_grid % lon(1) - cosmo_lon(0)) / dlon )
-       imax = CEILING( (avg_grid % lon(2) - cosmo_lon(0)) / dlon )
-
-       jmin = FLOOR  ( (avg_grid % lat(1) - cosmo_lat(0)) / dlat )
-       jmax = CEILING( (avg_grid % lat(2) - cosmo_lat(0)) / dlat )
-       
-       message = "Grid " // TRIM(avg_grid % name(1)) // " averages over " // &
-                 TRIM(str(imin)) // " <= i <= " // TRIM(str(imax)) //          &
-                 " and " //                                                    &
-                 TRIM(str(jmin)) // " <= j <= " // TRIM(str(jmax))
-       CALL report( 'get_cosmo_averaging_region', message )
-
-       nx = imax - imin + 1
-       ny = jmax - jmin + 1
-       avg_grid % n_columns = nx * ny
-
-       ALLOCATE( avg_grid % iii(avg_grid % n_columns),                         &
-                 avg_grid % jjj(avg_grid % n_columns) )
-
-       l = 0
-       DO j = jmin, jmax
-       DO i = imin, imax
-          l = l + 1
-          avg_grid % iii(l) = i
-          avg_grid % jjj(l) = j
-       ENDDO
-       ENDDO
-
-    END SUBROUTINE get_cosmo_averaging_region
+    END SELECT
+
+!
+!-- FIXME: make dlon, dlat parameters of the grid_defintion type
+    dlon = cosmo_lon(1) - cosmo_lon(0)
+    dlat = cosmo_lat(1) - cosmo_lat(0)
+
+    imin = FLOOR  ( (avg_grid%lon(1) - cosmo_lon(0)) / dlon )
+    imax = CEILING( (avg_grid%lon(2) - cosmo_lon(0)) / dlon )
+
+    jmin = FLOOR  ( (avg_grid%lat(1) - cosmo_lat(0)) / dlat )
+    jmax = CEILING( (avg_grid%lat(2) - cosmo_lat(0)) / dlat )
+    
+    message = "Grid " // TRIM(avg_grid%name(1)) // " averages over " // &
+              TRIM(str(imin)) // " <= i <= " // TRIM(str(imax)) //          &
+              " and " //                                                    &
+              TRIM(str(jmin)) // " <= j <= " // TRIM(str(jmax))
+    CALL report( 'get_cosmo_averaging_region', message )
+
+    nx = imax - imin + 1
+    ny = jmax - jmin + 1
+    avg_grid%n_columns = nx * ny
+
+    ALLOCATE( avg_grid%iii(avg_grid%n_columns),                         &
+              avg_grid%jjj(avg_grid%n_columns) )
+
+    l = 0
+    DO  j = jmin, jmax
+    DO  i = imin, imax
+       l = l + 1
+       avg_grid%iii(l) = i
+       avg_grid%jjj(l) = j
+    ENDDO
+    ENDDO
+
+ END SUBROUTINE get_cosmo_averaging_region
 
 
@@ -1683,39 +1702,39 @@
 !> 'modpoints'.
 !------------------------------------------------------------------------------!
-    SUBROUTINE stretched_z(z, dz, dz_max, dz_stretch_factor, dz_stretch_level, &
-                           dz_stretch_level_start, dz_stretch_level_end,       &
-                           dz_stretch_factor_array)
-
-       REAL(dp), DIMENSION(:), INTENT(INOUT) ::  z, dz, dz_stretch_factor_array
-       REAL(dp), DIMENSION(:), INTENT(INOUT) ::  dz_stretch_level_start, dz_stretch_level_end
-       REAL(dp), INTENT(IN) ::  dz_max, dz_stretch_factor, dz_stretch_level
-
-       INTEGER ::  number_stretch_level_start        !< number of user-specified start levels for stretching
-       INTEGER ::  number_stretch_level_end          !< number of user-specified end levels for stretching
-
-       REAL(dp), DIMENSION(:), ALLOCATABLE ::  min_dz_stretch_level_end
-       REAL(dp) ::  dz_level_end, dz_stretched
-
-       INTEGER ::  dz_stretch_level_end_index(9)      !< vertical grid level index until which the vertical grid spacing is stretched
-       INTEGER ::  dz_stretch_level_start_index(9)    !< vertical grid level index above which the vertical grid spacing is stretched
-       INTEGER ::  dz_stretch_level_index = 0
-       INTEGER ::  k, n, number_dz
+ SUBROUTINE stretched_z(z, dz, dz_max, dz_stretch_factor, dz_stretch_level, &
+                        dz_stretch_level_start, dz_stretch_level_end,       &
+                        dz_stretch_factor_array)
+
+    REAL(wp), DIMENSION(:), INTENT(INOUT) ::  z, dz, dz_stretch_factor_array
+    REAL(wp), DIMENSION(:), INTENT(INOUT) ::  dz_stretch_level_start, dz_stretch_level_end
+    REAL(wp), INTENT(IN) ::  dz_max, dz_stretch_factor, dz_stretch_level
+
+    INTEGER ::  number_stretch_level_start        !< number of user-specified start levels for stretching
+    INTEGER ::  number_stretch_level_end          !< number of user-specified end levels for stretching
+
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  min_dz_stretch_level_end
+    REAL(wp) ::  dz_level_end, dz_stretched
+
+    INTEGER ::  dz_stretch_level_end_index(9)      !< vertical grid level index until which the vertical grid spacing is stretched
+    INTEGER ::  dz_stretch_level_start_index(9)    !< vertical grid level index above which the vertical grid spacing is stretched
+    INTEGER ::  dz_stretch_level_index = 0
+    INTEGER ::  k, n, number_dz
 
 !
 !-- Compute height of u-levels from constant grid length and dz stretch factors
-       IF ( dz(1) == -1.0_dp )  THEN
-          message = 'missing dz'
-          CALL inifor_abort( 'stretched_z', message)
-       ELSEIF ( dz(1) <= 0.0_dp )  THEN
-          WRITE( message, * ) 'dz=', dz(1),' <= 0.0'
-          CALL inifor_abort( 'stretched_z', message)
-       ENDIF
+    IF ( dz(1) == -1.0_wp )  THEN
+       message = 'missing dz'
+       CALL inifor_abort( 'stretched_z', message)
+    ELSEIF ( dz(1) <= 0.0_wp )  THEN
+       WRITE( message, * ) 'dz=', dz(1),' <= 0.0'
+       CALL inifor_abort( 'stretched_z', message)
+    ENDIF
 
 !
 !-- Initialize dz_stretch_level_start with the value of dz_stretch_level
 !-- if it was set by the user
-       IF ( dz_stretch_level /= -9999999.9_dp ) THEN
-          dz_stretch_level_start(1) = dz_stretch_level
-       ENDIF
+    IF ( dz_stretch_level /= -9999999.9_wp )  THEN
+       dz_stretch_level_start(1) = dz_stretch_level
+    ENDIF
        
 !
@@ -1727,54 +1746,54 @@
 !-- is used (Attention: The user is not allowed to specify a dz value equal
 !-- to the default of dz_max = 999.0). 
-       number_dz = COUNT( dz /= -1.0_dp .AND. dz /= dz_max )
-       number_stretch_level_start = COUNT( dz_stretch_level_start /=           &
-                                           -9999999.9_dp )
-       number_stretch_level_end = COUNT( dz_stretch_level_end /=               &
-                                         9999999.9_dp )
+    number_dz = COUNT( dz /= -1.0_wp .AND. dz /= dz_max )
+    number_stretch_level_start = COUNT( dz_stretch_level_start /=           &
+                                        -9999999.9_wp )
+    number_stretch_level_end = COUNT( dz_stretch_level_end /=               &
+                                      9999999.9_wp )
 
 !
 !-- The number of specified end levels +1 has to be the same than the number 
 !-- of specified dz values
-       IF ( number_dz /= number_stretch_level_end + 1 ) THEN
-          WRITE( message, * ) 'The number of values for dz = ',                &
-                              number_dz, 'has to be the same than ',           &
-                              'the number of values for ',                     &
-                              'dz_stretch_level_end + 1 = ',                   &
-                              number_stretch_level_end+1
-          CALL inifor_abort( 'stretched_z', message)
-       ENDIF
+    IF ( number_dz /= number_stretch_level_end + 1 )  THEN
+       WRITE( message, * ) 'The number of values for dz = ',                &
+                           number_dz, 'has to be the same than ',           &
+                           'the number of values for ',                     &
+                           'dz_stretch_level_end + 1 = ',                   &
+                           number_stretch_level_end+1
+       CALL inifor_abort( 'stretched_z', message)
+    ENDIF
     
 !
-!--    The number of specified start levels has to be the same or one less than 
-!--    the number of specified dz values
-       IF ( number_dz /= number_stretch_level_start + 1 .AND.                  &
-            number_dz /= number_stretch_level_start ) THEN
-          WRITE( message, * ) 'The number of values for dz = ',         &
-                              number_dz, 'has to be the same or one ', &
-                              'more than& the number of values for ',  &
-                              'dz_stretch_level_start = ',             &
-                              number_stretch_level_start
-          CALL inifor_abort( 'stretched_z', message)
-       ENDIF
+!-- The number of specified start levels has to be the same or one less than 
+!-- the number of specified dz values
+    IF ( number_dz /= number_stretch_level_start + 1 .AND.                  &
+         number_dz /= number_stretch_level_start )  THEN
+       WRITE( message, * ) 'The number of values for dz = ',         &
+                           number_dz, 'has to be the same or one ', &
+                           'more than& the number of values for ',  &
+                           'dz_stretch_level_start = ',             &
+                           number_stretch_level_start
+       CALL inifor_abort( 'stretched_z', message)
+    ENDIF
     
-!--    The number of specified start levels has to be the same or one more than 
-!--    the number of specified end levels
-       IF ( number_stretch_level_start /= number_stretch_level_end + 1 .AND.   &
-            number_stretch_level_start /= number_stretch_level_end ) THEN
-          WRITE( message, * ) 'The number of values for ',              &
-                              'dz_stretch_level_start = ',              &
-                              dz_stretch_level_start, 'has to be the ',&
-                              'same or one more than& the number of ', &
-                              'values for dz_stretch_level_end = ',    &
-                              number_stretch_level_end
-          CALL inifor_abort( 'stretched_z', message)
-       ENDIF
+!-- The number of specified start levels has to be the same or one more than 
+!-- the number of specified end levels
+    IF ( number_stretch_level_start /= number_stretch_level_end + 1 .AND.   &
+         number_stretch_level_start /= number_stretch_level_end )  THEN
+       WRITE( message, * ) 'The number of values for ',              &
+                           'dz_stretch_level_start = ',              &
+                           dz_stretch_level_start, 'has to be the ',&
+                           'same or one more than& the number of ', &
+                           'values for dz_stretch_level_end = ',    &
+                           number_stretch_level_end
+       CALL inifor_abort( 'stretched_z', message)
+    ENDIF
 
 !
 !-- Initialize dz for the free atmosphere with the value of dz_max
-       IF ( dz(number_stretch_level_start+1) == -1.0_dp .AND.                     &
-            number_stretch_level_start /= 0 ) THEN 
-          dz(number_stretch_level_start+1) = dz_max
-       ENDIF
+    IF ( dz(number_stretch_level_start+1) == -1.0_wp .AND.                     &
+         number_stretch_level_start /= 0 )  THEN 
+       dz(number_stretch_level_start+1) = dz_max
+    ENDIF
        
 !
@@ -1782,133 +1801,133 @@
 !-- atmosphere is desired (dz_stretch_level_end was not specified for the 
 !-- free atmosphere)
-       IF ( number_stretch_level_start == number_stretch_level_end + 1 ) THEN 
-          dz_stretch_factor_array(number_stretch_level_start) =                   &
-          dz_stretch_factor
+    IF ( number_stretch_level_start == number_stretch_level_end + 1 )  THEN 
+       dz_stretch_factor_array(number_stretch_level_start) =                   &
+       dz_stretch_factor
+    ENDIF
+
+!-- Allocation of arrays for stretching
+    ALLOCATE( min_dz_stretch_level_end(number_stretch_level_start) )
+
+!
+!-- The stretching region has to be large enough to allow for a smooth
+!-- transition between two different grid spacings
+    DO  n = 1, number_stretch_level_start
+       min_dz_stretch_level_end(n) = dz_stretch_level_start(n) +            &
+                                     4 * MAX( dz(n),dz(n+1) )
+    ENDDO
+
+    IF ( ANY( min_dz_stretch_level_end(1:number_stretch_level_start) >      &
+              dz_stretch_level_end(1:number_stretch_level_start) ) )  THEN
+    !IF ( ANY( min_dz_stretch_level_end >      &
+    !          dz_stretch_level_end ) ) THEN
+          message = 'Each dz_stretch_level_end has to be larger '  // &
+                    'than its corresponding value for ' //            &
+                    'dz_stretch_level_start + 4*MAX(dz(n),dz(n+1)) '//&
+                    'to allow for smooth grid stretching'
+          CALL inifor_abort('stretched_z', message)
+    ENDIF
+    
+!
+!-- Stretching must not be applied within the prandtl_layer 
+!-- (first two grid points). For the default case dz_stretch_level_start 
+!-- is negative. Therefore the absolut value is checked here.
+    IF ( ANY( ABS( dz_stretch_level_start ) < dz(1) * 1.5_wp ) )  THEN
+       WRITE( message, * ) 'Eeach dz_stretch_level_start has to be ',&
+                           'larger than ', dz(1) * 1.5
+          CALL inifor_abort( 'stretched_z', message)
+    ENDIF
+
+!
+!-- The stretching has to start and end on a grid level. Therefore 
+!-- user-specified values have to ''interpolate'' to the next lowest level
+    IF ( number_stretch_level_start /= 0 )  THEN
+       dz_stretch_level_start(1) = INT( (dz_stretch_level_start(1) -        &
+                                         dz(1)/2.0) / dz(1) )               &
+                                   * dz(1) + dz(1)/2.0
+    ENDIF
+    
+    IF ( number_stretch_level_start > 1 )  THEN
+       DO  n = 2, number_stretch_level_start
+          dz_stretch_level_start(n) = INT( dz_stretch_level_start(n) /      &
+                                           dz(n) ) * dz(n)
+       ENDDO
+    ENDIF
+    
+    IF ( number_stretch_level_end /= 0 )  THEN
+       DO  n = 1, number_stretch_level_end
+          dz_stretch_level_end(n) = INT( dz_stretch_level_end(n) /          &
+                                         dz(n+1) ) * dz(n+1)
+       ENDDO
+    ENDIF
+ 
+!
+!-- Determine stretching factor if necessary
+    IF ( number_stretch_level_end >= 1 )  THEN 
+       CALL calculate_stretching_factor( number_stretch_level_end, dz,      & 
+                                         dz_stretch_factor_array,           &   
+                                         dz_stretch_level_end,              &
+                                         dz_stretch_level_start )
+    ENDIF
+
+    z(1) = dz(1) * 0.5_wp
+!
+    dz_stretch_level_index = n
+    dz_stretched = dz(1)
+    DO  k = 2, n
+
+       IF ( dz_stretch_level <= z(k-1)  .AND.  dz_stretched < dz_max )  THEN
+
+          dz_stretched = dz_stretched * dz_stretch_factor
+          dz_stretched = MIN( dz_stretched, dz_max )
+
+          IF ( dz_stretch_level_index == n )  dz_stretch_level_index = k-1
+
        ENDIF
 
-!-- Allocation of arrays for stretching
-       ALLOCATE( min_dz_stretch_level_end(number_stretch_level_start) )
-
-!
-!--    The stretching region has to be large enough to allow for a smooth
-!--    transition between two different grid spacings
-       DO n = 1, number_stretch_level_start
-          min_dz_stretch_level_end(n) = dz_stretch_level_start(n) +            &
-                                        4 * MAX( dz(n),dz(n+1) )
-       ENDDO
-
-       IF ( ANY( min_dz_stretch_level_end(1:number_stretch_level_start) >      &
-                 dz_stretch_level_end(1:number_stretch_level_start) ) ) THEN
-       !IF ( ANY( min_dz_stretch_level_end >      &
-       !          dz_stretch_level_end ) ) THEN
-             message = 'Each dz_stretch_level_end has to be larger '  // &
-                       'than its corresponding value for ' //            &
-                       'dz_stretch_level_start + 4*MAX(dz(n),dz(n+1)) '//&
-                       'to allow for smooth grid stretching'
-             CALL inifor_abort('stretched_z', message)
-       ENDIF
-       
-!
-!--    Stretching must not be applied within the prandtl_layer 
-!--    (first two grid points). For the default case dz_stretch_level_start 
-!--    is negative. Therefore the absolut value is checked here.
-       IF ( ANY( ABS( dz_stretch_level_start ) < dz(1) * 1.5_dp ) ) THEN
-          WRITE( message, * ) 'Eeach dz_stretch_level_start has to be ',&
-                              'larger than ', dz(1) * 1.5
-             CALL inifor_abort( 'stretched_z', message)
-       ENDIF
-
-!
-!--    The stretching has to start and end on a grid level. Therefore 
-!--    user-specified values have to ''interpolate'' to the next lowest level
-       IF ( number_stretch_level_start /= 0 ) THEN
-          dz_stretch_level_start(1) = INT( (dz_stretch_level_start(1) -        &
-                                            dz(1)/2.0) / dz(1) )               &
-                                      * dz(1) + dz(1)/2.0
-       ENDIF
-       
-       IF ( number_stretch_level_start > 1 ) THEN
-          DO n = 2, number_stretch_level_start
-             dz_stretch_level_start(n) = INT( dz_stretch_level_start(n) /      &
-                                              dz(n) ) * dz(n)
-          ENDDO
-       ENDIF
-       
-       IF ( number_stretch_level_end /= 0 ) THEN
-          DO n = 1, number_stretch_level_end
-             dz_stretch_level_end(n) = INT( dz_stretch_level_end(n) /          &
-                                            dz(n+1) ) * dz(n+1)
-          ENDDO
-       ENDIF
- 
-!
-!--    Determine stretching factor if necessary
-       IF ( number_stretch_level_end >= 1 ) THEN 
-          CALL calculate_stretching_factor( number_stretch_level_end, dz,      & 
-                                            dz_stretch_factor_array,           &   
-                                            dz_stretch_level_end,              &
-                                            dz_stretch_level_start )
-       ENDIF
-
-       z(1) = dz(1) * 0.5_dp
-!
-       dz_stretch_level_index = n
-       dz_stretched = dz(1)
-       DO  k = 2, n
-
-          IF ( dz_stretch_level <= z(k-1)  .AND.  dz_stretched < dz_max )  THEN
-
-             dz_stretched = dz_stretched * dz_stretch_factor
-             dz_stretched = MIN( dz_stretched, dz_max )
-
-             IF ( dz_stretch_level_index == n ) dz_stretch_level_index = k-1
-
-          ENDIF
-
-          z(k) = z(k-1) + dz_stretched
-
-       ENDDO
-!--    Determine u and v height levels considering the possibility of grid
-!--    stretching in several heights.
-       n = 1
-       dz_stretch_level_start_index(:) = UBOUND(z, 1)
-       dz_stretch_level_end_index(:) = UBOUND(z, 1)
-       dz_stretched = dz(1)
-
-!--    The default value of dz_stretch_level_start is negative, thus the first
-!--    condition is always true. Hence, the second condition is necessary.
-       DO  k = 2, UBOUND(z, 1)
-          IF ( dz_stretch_level_start(n) <= z(k-1) .AND.                      &
-               dz_stretch_level_start(n) /= -9999999.9_dp ) THEN
-             dz_stretched = dz_stretched * dz_stretch_factor_array(n)
-             
-             IF ( dz(n) > dz(n+1) ) THEN
-                dz_stretched = MAX( dz_stretched, dz(n+1) ) !Restrict dz_stretched to the user-specified (higher) dz
-             ELSE
-                dz_stretched = MIN( dz_stretched, dz(n+1) ) !Restrict dz_stretched to the user-specified (lower) dz
-             ENDIF
-             
-             IF ( dz_stretch_level_start_index(n) == UBOUND(z, 1) )            &
-             dz_stretch_level_start_index(n) = k-1
-             
+       z(k) = z(k-1) + dz_stretched
+
+    ENDDO
+!-- Determine u and v height levels considering the possibility of grid
+!-- stretching in several heights.
+    n = 1
+    dz_stretch_level_start_index(:) = UBOUND(z, 1)
+    dz_stretch_level_end_index(:) = UBOUND(z, 1)
+    dz_stretched = dz(1)
+
+!-- The default value of dz_stretch_level_start is negative, thus the first
+!-- condition is always true. Hence, the second condition is necessary.
+    DO  k = 2, UBOUND(z, 1)
+       IF ( dz_stretch_level_start(n) <= z(k-1) .AND.                      &
+            dz_stretch_level_start(n) /= -9999999.9_wp )  THEN
+          dz_stretched = dz_stretched * dz_stretch_factor_array(n)
+          
+          IF ( dz(n) > dz(n+1) )  THEN
+             dz_stretched = MAX( dz_stretched, dz(n+1) ) !Restrict dz_stretched to the user-specified (higher) dz
+          ELSE
+             dz_stretched = MIN( dz_stretched, dz(n+1) ) !Restrict dz_stretched to the user-specified (lower) dz
           ENDIF
           
-          z(k) = z(k-1) + dz_stretched
+          IF ( dz_stretch_level_start_index(n) == UBOUND(z, 1) )            &
+          dz_stretch_level_start_index(n) = k-1
           
-!
-!--       Make sure that the stretching ends exactly at dz_stretch_level_end 
-          dz_level_end = ABS( z(k) - dz_stretch_level_end(n) ) 
-          
-          IF ( dz_level_end < dz(n+1)/3.0 ) THEN
-             z(k) = dz_stretch_level_end(n)
-             dz_stretched = dz(n+1)
-             dz_stretch_level_end_index(n) = k
-             n = n + 1             
-          ENDIF
-       ENDDO
-
-       DEALLOCATE( min_dz_stretch_level_end )
-
-    END SUBROUTINE stretched_z
+       ENDIF
+       
+       z(k) = z(k-1) + dz_stretched
+       
+!
+!--    Make sure that the stretching ends exactly at dz_stretch_level_end 
+       dz_level_end = ABS( z(k) - dz_stretch_level_end(n) ) 
+       
+       IF ( dz_level_end < dz(n+1)/3.0 )  THEN
+          z(k) = dz_stretch_level_end(n)
+          dz_stretched = dz(n+1)
+          dz_stretch_level_end_index(n) = k
+          n = n + 1             
+       ENDIF
+    ENDDO
+
+    DEALLOCATE( min_dz_stretch_level_end )
+
+ END SUBROUTINE stretched_z
 
 
@@ -1928,7 +1947,7 @@
                                          dz_stretch_level_start )
  
-    REAL(dp), DIMENSION(:), INTENT(IN)    ::  dz
-    REAL(dp), DIMENSION(:), INTENT(INOUT) ::  dz_stretch_factor_array
-    REAL(dp), DIMENSION(:), INTENT(IN)    ::  dz_stretch_level_end, dz_stretch_level_start
+    REAL(wp), DIMENSION(:), INTENT(IN)    ::  dz
+    REAL(wp), DIMENSION(:), INTENT(INOUT) ::  dz_stretch_factor_array
+    REAL(wp), DIMENSION(:), INTENT(IN)    ::  dz_stretch_level_end, dz_stretch_level_start
   
     INTEGER ::  iterations  !< number of iterations until stretch_factor_lower/upper_limit is reached  
@@ -1938,19 +1957,19 @@
     INTEGER, INTENT(IN) ::  number_end !< number of user-specified end levels for stretching
         
-    REAL(dp) ::  delta_l               !< absolute difference between l and l_rounded
-    REAL(dp) ::  delta_stretch_factor  !< absolute difference between stretch_factor_1 and stretch_factor_2
-    REAL(dp) ::  delta_total_new       !< sum of delta_l and delta_stretch_factor for the next iteration (should be as small as possible) 
-    REAL(dp) ::  delta_total_old       !< sum of delta_l and delta_stretch_factor for the last iteration 
-    REAL(dp) ::  distance              !< distance between dz_stretch_level_start and dz_stretch_level_end (stretching region)
-    REAL(dp) ::  l                     !< value that fulfil Eq. (5) in the paper mentioned above together with stretch_factor_1 exactly
-    REAL(dp) ::  numerator             !< numerator of the quotient
-    REAL(dp) ::  stretch_factor_1      !< stretching factor that fulfil Eq. (5) togehter with l exactly
-    REAL(dp) ::  stretch_factor_2      !< stretching factor that fulfil Eq. (6) togehter with l_rounded exactly
+    REAL(wp) ::  delta_l               !< absolute difference between l and l_rounded
+    REAL(wp) ::  delta_stretch_factor  !< absolute difference between stretch_factor_1 and stretch_factor_2
+    REAL(wp) ::  delta_total_new       !< sum of delta_l and delta_stretch_factor for the next iteration (should be as small as possible) 
+    REAL(wp) ::  delta_total_old       !< sum of delta_l and delta_stretch_factor for the last iteration 
+    REAL(wp) ::  distance              !< distance between dz_stretch_level_start and dz_stretch_level_end (stretching region)
+    REAL(wp) ::  l                     !< value that fulfil Eq. (5) in the paper mentioned above together with stretch_factor_1 exactly
+    REAL(wp) ::  numerator             !< numerator of the quotient
+    REAL(wp) ::  stretch_factor_1      !< stretching factor that fulfil Eq. (5) togehter with l exactly
+    REAL(wp) ::  stretch_factor_2      !< stretching factor that fulfil Eq. (6) togehter with l_rounded exactly
     
-    REAL(dp) ::  dz_stretch_factor_array_2(9) = 1.08_dp  !< Array that contains all stretch_factor_2 that belongs to stretch_factor_1 
+    REAL(wp) ::  dz_stretch_factor_array_2(9) = 1.08_wp  !< Array that contains all stretch_factor_2 that belongs to stretch_factor_1 
     
-    REAL(dp), PARAMETER ::  stretch_factor_interval = 1.0E-06  !< interval for sampling possible stretching factors
-    REAL(dp), PARAMETER ::  stretch_factor_lower_limit = 0.88  !< lowest possible stretching factor
-    REAL(dp), PARAMETER ::  stretch_factor_upper_limit = 1.12  !< highest possible stretching factor
+    REAL(wp), PARAMETER ::  stretch_factor_interval = 1.0E-06  !< interval for sampling possible stretching factors
+    REAL(wp), PARAMETER ::  stretch_factor_lower_limit = 0.88  !< lowest possible stretching factor
+    REAL(wp), PARAMETER ::  stretch_factor_upper_limit = 1.12  !< highest possible stretching factor
  
  
@@ -1963,6 +1982,6 @@
        delta_total_old = 1.0
        
-       IF ( dz(n) > dz(n+1) ) THEN
-          DO WHILE ( stretch_factor_1 >= stretch_factor_lower_limit ) 
+       IF ( dz(n) > dz(n+1) )  THEN
+          DO  WHILE ( stretch_factor_1 >= stretch_factor_lower_limit ) 
              
              stretch_factor_1 = 1.0 - iterations * stretch_factor_interval
@@ -1972,5 +1991,5 @@
                          stretch_factor_1 - distance/dz(n)
              
-             IF ( numerator > 0.0 ) THEN
+             IF ( numerator > 0.0 )  THEN
                 l = LOG( numerator ) / LOG( stretch_factor_1 ) - 1.0
                 l_rounded = NINT( l )
@@ -1991,5 +2010,5 @@
 !--                stretch_factor_2 would guarantee that the stretched dz(n) is
 !--                equal to dz(n+1) after l_rounded grid levels.
-             IF (delta_total_new < delta_total_old) THEN
+             IF (delta_total_new < delta_total_old)  THEN
                 dz_stretch_factor_array(n) = stretch_factor_1
                 dz_stretch_factor_array_2(n) = stretch_factor_2
@@ -2001,6 +2020,6 @@
           ENDDO
              
-       ELSEIF ( dz(n) < dz(n+1) ) THEN 
-          DO WHILE ( stretch_factor_1 <= stretch_factor_upper_limit )
+       ELSEIF ( dz(n) < dz(n+1) )  THEN 
+          DO  WHILE ( stretch_factor_1 <= stretch_factor_upper_limit )
                      
              stretch_factor_1 = 1.0 + iterations * stretch_factor_interval
@@ -2027,5 +2046,5 @@
 !--          stretch_factor_2 would guarantee that the stretched dz(n) is
 !--          equal to dz(n+1) after l_rounded grid levels.
-             IF (delta_total_new < delta_total_old) THEN
+             IF (delta_total_new < delta_total_old)  THEN
                 dz_stretch_factor_array(n) = stretch_factor_1
                 dz_stretch_factor_array_2(n) = stretch_factor_2
@@ -2045,6 +2064,6 @@
 !--    interval. If not, print a warning for the user.
        IF ( dz_stretch_factor_array_2(n) < stretch_factor_lower_limit .OR.     & 
-            dz_stretch_factor_array_2(n) > stretch_factor_upper_limit ) THEN
-          WRITE( message, * ) 'stretch_factor_2 = ',                    &
+            dz_stretch_factor_array_2(n) > stretch_factor_upper_limit )  THEN
+          WRITE( message, * ) 'stretch_factor_2 = ',                           &
                                      dz_stretch_factor_array_2(n), ' which is',&
                                      ' responsible for exactly reaching& dz =',&
@@ -2067,16 +2086,16 @@
 !> coordinate vector 'z' and stores it in 'zw'.
 !------------------------------------------------------------------------------!
-    SUBROUTINE midpoints(z, zw)
-
-        REAL(dp), INTENT(IN)  ::  z(0:)
-        REAL(dp), INTENT(OUT) ::  zw(1:)
-
-        INTEGER ::  k
-
-        DO k = 1, UBOUND(zw, 1)
-           zw(k) = 0.5_dp * (z(k-1) + z(k))
-        ENDDO
-
-    END SUBROUTINE midpoints
+ SUBROUTINE midpoints(z, zw)
+
+     REAL(wp), INTENT(IN)  ::  z(0:)
+     REAL(wp), INTENT(OUT) ::  zw(1:)
+
+     INTEGER ::  k
+
+     DO  k = 1, UBOUND(zw, 1)
+        zw(k) = 0.5_wp * (z(k-1) + z(k))
+     ENDDO
+
+ END SUBROUTINE midpoints
 
 !------------------------------------------------------------------------------!
@@ -2085,165 +2104,165 @@
 !> Defines INFOR's IO groups.
 !------------------------------------------------------------------------------!
-    SUBROUTINE setup_io_groups()
-
-       INTEGER ::  ngroups
-
-       ngroups = 16
-       ALLOCATE( io_group_list(ngroups) )
-
-!
-!--    soil temp
-       io_group_list(1) = init_io_group(                                       &
-          in_files = soil_files,                                               &
-          out_vars = output_var_table(1:1),                                    &
-          in_var_list = input_var_table(1:1),                                  &
-          kind = 'soil-temperature'                                            &
-       )
-
-!
-!--    soil water
-       io_group_list(2) = init_io_group(                                       &
-          in_files = soil_files,                                               &
-          out_vars = output_var_table(2:2),                                    &
-          in_var_list = input_var_table(2:2),                                  &
-          kind = 'soil-water'                                                  &
-       )
-
-!
-!--    potential temperature, surface pressure, specific humidity including
-!--    nudging and subsidence, and geostrophic winds ug, vg
-       io_group_list(3) = init_io_group(                                       &
-          in_files = flow_files,                                               &
-          out_vars = [output_var_table(56:64),                                 & ! internal averaged density and pressure profiles
-                      output_var_table(3:8), output_var_table(9:14),           &
-                      output_var_table(42:42), output_var_table(43:44),        &
-                      output_var_table(49:51), output_var_table(52:54)],       &
-          in_var_list = (/input_var_table(3), input_var_table(17),             & ! T, P, QV
-                          input_var_table(4) /),                               &
-          kind = 'thermodynamics',                                             &
-          n_output_quantities = 4                                              & ! P, Theta, Rho, qv
-       )
-
-!
-!--    Moved to therodynamic io_group
-       !io_group_list(4) = init_io_group(                                       &
-       !   in_files = flow_files,                                               &
-       !   out_vars = [output_var_table(9:14), output_var_table(52:54)],        &
-       !   in_var_list = input_var_table(4:4),                                  &
-       !   kind = 'scalar'                                                      &
-       !)
-
-!
-!--    u and v velocity
-       io_group_list(5) = init_io_group(                                       &
-          in_files = flow_files,                                               &
-          out_vars = [output_var_table(15:26), output_var_table(45:46)],       &
-          !out_vars = output_var_table(15:20),                                  &
-          in_var_list = input_var_table(5:6),                                  &
-          !in_var_list = input_var_table(5:5),                                  &
-          kind = 'velocities'                                                  &
-       )
+ SUBROUTINE setup_io_groups()
+
+    INTEGER ::  ngroups
+
+    ngroups = 16
+    ALLOCATE( io_group_list(ngroups) )
+
+!
+!-- soil temp
+    io_group_list(1) = init_io_group(                                       &
+       in_files = soil_files,                                               &
+       out_vars = output_var_table(1:1),                                    &
+       in_var_list = input_var_table(1:1),                                  &
+       kind = 'soil-temperature'                                            &
+    )
+
+!
+!-- soil water
+    io_group_list(2) = init_io_group(                                       &
+       in_files = soil_files,                                               &
+       out_vars = output_var_table(2:2),                                    &
+       in_var_list = input_var_table(2:2),                                  &
+       kind = 'soil-water'                                                  &
+    )
+
+!
+!-- potential temperature, surface pressure, specific humidity including
+!-- nudging and subsidence, and geostrophic winds ug, vg
+    io_group_list(3) = init_io_group(                                       &
+       in_files = flow_files,                                               &
+       out_vars = [output_var_table(56:64),                                 & ! internal averaged density and pressure profiles
+                   output_var_table(3:8), output_var_table(9:14),           &
+                   output_var_table(42:42), output_var_table(43:44),        &
+                   output_var_table(49:51), output_var_table(52:54)],       &
+       in_var_list = (/input_var_table(3), input_var_table(17),             & ! T, P, QV
+                       input_var_table(4) /),                               &
+       kind = 'thermodynamics',                                             &
+       n_output_quantities = 4                                              & ! P, Theta, Rho, qv
+    )
+
+!
+!-- Moved to therodynamic io_group
+    !io_group_list(4) = init_io_group(                                       &
+    !   in_files = flow_files,                                               &
+    !   out_vars = [output_var_table(9:14), output_var_table(52:54)],        &
+    !   in_var_list = input_var_table(4:4),                                  &
+    !   kind = 'scalar'                                                      &
+    !)
+
+!
+!-- u and v velocity
+    io_group_list(5) = init_io_group(                                       &
+       in_files = flow_files,                                               &
+       out_vars = [output_var_table(15:26), output_var_table(45:46)],       &
+       !out_vars = output_var_table(15:20),                                  &
+       in_var_list = input_var_table(5:6),                                  &
+       !in_var_list = input_var_table(5:5),                                  &
+       kind = 'velocities'                                                  &
+    )
    
 !
-!--    v velocity, deprecated!
-       !io_group_list(6) = init_io_group(                                       &
-       !   in_files = flow_files,                                               &
-       !   out_vars = output_var_table(21:26),                                  &
-       !   in_var_list = input_var_table(6:6),                                  &
-       !   kind = 'horizontal velocity'                                         &
-       !)
-       !io_group_list(6) % to_be_processed = .FALSE.
+!-- v velocity, deprecated!
+    !io_group_list(6) = init_io_group(                                       &
+    !   in_files = flow_files,                                               &
+    !   out_vars = output_var_table(21:26),                                  &
+    !   in_var_list = input_var_table(6:6),                                  &
+    !   kind = 'horizontal velocity'                                         &
+    !)
+    !io_group_list(6)%to_be_processed = .FALSE.
    
 !
-!--    w velocity and subsidence and w nudging
-       io_group_list(7) = init_io_group(                                       &
-          in_files = flow_files,                                               &
-          out_vars = [output_var_table(27:32), output_var_table(47:48)],       &
-          in_var_list = input_var_table(7:7),                                  &
-          kind = 'scalar'                                                      &
-       )
-!
-!--    rain
-       io_group_list(8) = init_io_group(                                       &
-          in_files = soil_moisture_files,                                      &
-          out_vars = output_var_table(33:33),                                  &
-          in_var_list = input_var_table(8:8),                                  &
-          kind = 'accumulated'                                                 &
-       )
-       io_group_list(8) % to_be_processed = .FALSE.
-!
-!--    snow
-       io_group_list(9) = init_io_group(                                       &
-          in_files = soil_moisture_files,                                      &
-          out_vars = output_var_table(34:34),                                  &
-          in_var_list = input_var_table(9:9),                                  &
-          kind = 'accumulated'                                                 &
-       )
-       io_group_list(9) % to_be_processed = .FALSE.
-!
-!--    graupel
-       io_group_list(10) = init_io_group(                                      &
-          in_files = soil_moisture_files,                                      &
-          out_vars = output_var_table(35:35),                                  &
-          in_var_list = input_var_table(10:10),                                &
-          kind = 'accumulated'                                                 &
-       )
-       io_group_list(10) % to_be_processed = .FALSE.
-!
-!--    evapotranspiration
-       io_group_list(11) = init_io_group(                                      &
-          in_files = soil_moisture_files,                                      &
-          out_vars = output_var_table(37:37),                                  &
-          in_var_list = input_var_table(11:11),                                &
-          kind = 'accumulated'                                                 &
-       )
-       io_group_list(11) % to_be_processed = .FALSE.
-!
-!--    2m air temperature
-       io_group_list(12) = init_io_group(                                      &
-          in_files = soil_moisture_files,                                      &
-          out_vars = output_var_table(36:36),                                  &
-          in_var_list = input_var_table(12:12),                                &
-          kind = 'surface'                                                     &
-       )
-       io_group_list(12) % to_be_processed = .FALSE.
-!
-!--    incoming diffusive sw flux
-       io_group_list(13) = init_io_group(                                      &
-          in_files = radiation_files,                                          &
-          out_vars = output_var_table(38:38),                                  &
-          in_var_list = input_var_table(13:13),                                &
-          kind = 'running average'                                             &
-       )
-       io_group_list(13) % to_be_processed = .FALSE.
-!
-!--    incoming direct sw flux
-       io_group_list(14) = init_io_group(                                      &
-          in_files = radiation_files,                                          &
-          out_vars = output_var_table(39:39),                                  &
-          in_var_list = input_var_table(14:14),                                &
-          kind = 'running average'                                             &
-       )
-       io_group_list(14) % to_be_processed = .FALSE.
-!
-!--    sw radiation balance
-       io_group_list(15) = init_io_group(                                      &
-          in_files = radiation_files,                                          &
-          out_vars = output_var_table(40:40),                                  &
-          in_var_list = input_var_table(15:15),                                &
-          kind = 'running average'                                             &
-       )
-       io_group_list(15) % to_be_processed = .FALSE.
-!
-!--    lw radiation balance
-       io_group_list(16) = init_io_group(                                      &
-          in_files = radiation_files,                                          &
-          out_vars = output_var_table(41:41),                                  &
-          in_var_list = input_var_table(16:16),                                &
-          kind = 'running average'                                             &
-       )
-       io_group_list(16) % to_be_processed = .FALSE.
-
-    END SUBROUTINE setup_io_groups
+!-- w velocity and subsidence and w nudging
+    io_group_list(7) = init_io_group(                                       &
+       in_files = flow_files,                                               &
+       out_vars = [output_var_table(27:32), output_var_table(47:48)],       &
+       in_var_list = input_var_table(7:7),                                  &
+       kind = 'scalar'                                                      &
+    )
+!
+!-- rain
+    io_group_list(8) = init_io_group(                                       &
+       in_files = soil_moisture_files,                                      &
+       out_vars = output_var_table(33:33),                                  &
+       in_var_list = input_var_table(8:8),                                  &
+       kind = 'accumulated'                                                 &
+    )
+    io_group_list(8)%to_be_processed = .FALSE.
+!
+!-- snow
+    io_group_list(9) = init_io_group(                                       &
+       in_files = soil_moisture_files,                                      &
+       out_vars = output_var_table(34:34),                                  &
+       in_var_list = input_var_table(9:9),                                  &
+       kind = 'accumulated'                                                 &
+    )
+    io_group_list(9)%to_be_processed = .FALSE.
+!
+!-- graupel
+    io_group_list(10) = init_io_group(                                      &
+       in_files = soil_moisture_files,                                      &
+       out_vars = output_var_table(35:35),                                  &
+       in_var_list = input_var_table(10:10),                                &
+       kind = 'accumulated'                                                 &
+    )
+    io_group_list(10)%to_be_processed = .FALSE.
+!
+!-- evapotranspiration
+    io_group_list(11) = init_io_group(                                      &
+       in_files = soil_moisture_files,                                      &
+       out_vars = output_var_table(37:37),                                  &
+       in_var_list = input_var_table(11:11),                                &
+       kind = 'accumulated'                                                 &
+    )
+    io_group_list(11)%to_be_processed = .FALSE.
+!
+!-- 2m air temperature
+    io_group_list(12) = init_io_group(                                      &
+       in_files = soil_moisture_files,                                      &
+       out_vars = output_var_table(36:36),                                  &
+       in_var_list = input_var_table(12:12),                                &
+       kind = 'surface'                                                     &
+    )
+    io_group_list(12)%to_be_processed = .FALSE.
+!
+!-- incoming diffusive sw flux
+    io_group_list(13) = init_io_group(                                      &
+       in_files = radiation_files,                                          &
+       out_vars = output_var_table(38:38),                                  &
+       in_var_list = input_var_table(13:13),                                &
+       kind = 'running average'                                             &
+    )
+    io_group_list(13)%to_be_processed = .FALSE.
+!
+!-- incoming direct sw flux
+    io_group_list(14) = init_io_group(                                      &
+       in_files = radiation_files,                                          &
+       out_vars = output_var_table(39:39),                                  &
+       in_var_list = input_var_table(14:14),                                &
+       kind = 'running average'                                             &
+    )
+    io_group_list(14)%to_be_processed = .FALSE.
+!
+!-- sw radiation balance
+    io_group_list(15) = init_io_group(                                      &
+       in_files = radiation_files,                                          &
+       out_vars = output_var_table(40:40),                                  &
+       in_var_list = input_var_table(15:15),                                &
+       kind = 'running average'                                             &
+    )
+    io_group_list(15)%to_be_processed = .FALSE.
+!
+!-- lw radiation balance
+    io_group_list(16) = init_io_group(                                      &
+       in_files = radiation_files,                                          &
+       out_vars = output_var_table(41:41),                                  &
+       in_var_list = input_var_table(16:16),                                &
+       kind = 'running average'                                             &
+    )
+    io_group_list(16)%to_be_processed = .FALSE.
+
+ END SUBROUTINE setup_io_groups
 
 
@@ -2259,41 +2278,41 @@
 !> on the output quantity Theta.
 !------------------------------------------------------------------------------!
-    FUNCTION init_io_group(in_files, out_vars, in_var_list, kind,              &
-                           n_output_quantities) RESULT(group)
-       CHARACTER(LEN=PATH), INTENT(IN) ::  in_files(:)
-       CHARACTER(LEN=*), INTENT(IN)    ::  kind
-       TYPE(nc_var), INTENT(IN)        ::  out_vars(:)
-       TYPE(nc_var), INTENT(IN)        ::  in_var_list(:)
-       INTEGER, OPTIONAL               ::  n_output_quantities
-
-       TYPE(io_group)                  ::  group
-
-       group % nt = SIZE(in_files)
-       group % nv = SIZE(out_vars)
-       group % n_inputs = SIZE(in_var_list)
-       group % kind = TRIM(kind)
-!
-!--    For the 'thermodynamics' IO group, one quantity more than input variables
-!--    is needed to compute all output variables of the IO group. Concretely, in
-!--    preprocess() the density is computed from T,P or PP,QV in adddition to
-!--    the variables Theta, p, qv. In read_input_variables(),
-!--    n_output_quantities is used to allocate the correct number of input
-!--    buffers.
-       IF ( PRESENT(n_output_quantities) )  THEN
-          group % n_output_quantities = n_output_quantities
-       ELSE
-          group % n_output_quantities = group % n_inputs
-       ENDIF
-
-       ALLOCATE(group % in_var_list(group % n_inputs))
-       ALLOCATE(group % in_files(group % nt))
-       ALLOCATE(group % out_vars(group % nv))
-
-       group % in_var_list = in_var_list
-       group % in_files = in_files
-       group % out_vars = out_vars
-       group % to_be_processed = .TRUE.
-
-    END FUNCTION init_io_group
+ FUNCTION init_io_group(in_files, out_vars, in_var_list, kind,              &
+                        n_output_quantities) RESULT(group)
+    CHARACTER(LEN=PATH), INTENT(IN) ::  in_files(:)
+    CHARACTER(LEN=*), INTENT(IN)    ::  kind
+    TYPE(nc_var), INTENT(IN)        ::  out_vars(:)
+    TYPE(nc_var), INTENT(IN)        ::  in_var_list(:)
+    INTEGER, OPTIONAL               ::  n_output_quantities
+
+    TYPE(io_group)                  ::  group
+
+    group%nt = SIZE(in_files)
+    group%nv = SIZE(out_vars)
+    group%n_inputs = SIZE(in_var_list)
+    group%kind = TRIM(kind)
+!
+!-- For the 'thermodynamics' IO group, one quantity more than input variables
+!-- is needed to compute all output variables of the IO group. Concretely, in
+!-- preprocess() the density is computed from T,P or PP,QV in adddition to
+!-- the variables Theta, p, qv. In read_input_variables(),
+!-- n_output_quantities is used to allocate the correct number of input
+!-- buffers.
+    IF ( PRESENT(n_output_quantities) )  THEN
+       group%n_output_quantities = n_output_quantities
+    ELSE
+       group%n_output_quantities = group%n_inputs
+    ENDIF
+
+    ALLOCATE(group%in_var_list(group%n_inputs))
+    ALLOCATE(group%in_files(group%nt))
+    ALLOCATE(group%out_vars(group%nv))
+
+    group%in_var_list = in_var_list
+    group%in_files = in_files
+    group%out_vars = out_vars
+    group%to_be_processed = .TRUE.
+
+ END FUNCTION init_io_group
 
 
@@ -2303,25 +2322,25 @@
 !> Deallocates all allocated variables.
 !------------------------------------------------------------------------------!
-    SUBROUTINE fini_grids()
-
-       CALL report('fini_grids', 'Deallocating grids', cfg % debug)
-       
-       DEALLOCATE(x, y, z, xu, yv, zw, z_column, zw_column)
-
-       DEALLOCATE(palm_grid%x,  palm_grid%y,  palm_grid%z,                     &
-                  palm_grid%xu, palm_grid%yv, palm_grid%zw,                    &
-                  palm_grid%clon,  palm_grid%clat,                             &
-                  palm_grid%clonu, palm_grid%clatu)
-
-       DEALLOCATE(palm_intermediate%x,  palm_intermediate%y,  palm_intermediate%z, &
-                  palm_intermediate%xu, palm_intermediate%yv, palm_intermediate%zw,&
-                  palm_intermediate%clon,  palm_intermediate%clat,             &  
-                  palm_intermediate%clonu, palm_intermediate%clatu)
-
-       DEALLOCATE(cosmo_grid%lon,  cosmo_grid%lat,                             &
-                  cosmo_grid%lonu, cosmo_grid%latv,                            &
-                  cosmo_grid%hfl)
-
-    END SUBROUTINE fini_grids
+ SUBROUTINE fini_grids()
+
+    CALL report('fini_grids', 'Deallocating grids', cfg%debug)
+    
+    DEALLOCATE(x, y, z, xu, yv, zw, z_column, zw_column)
+
+    DEALLOCATE(palm_grid%x,  palm_grid%y,  palm_grid%z,                        &
+               palm_grid%xu, palm_grid%yv, palm_grid%zw,                       &
+               palm_grid%clon,  palm_grid%clat,                                &
+               palm_grid%clonu, palm_grid%clatu)
+
+    DEALLOCATE(palm_intermediate%x,  palm_intermediate%y,  palm_intermediate%z, &
+               palm_intermediate%xu, palm_intermediate%yv, palm_intermediate%zw,&
+               palm_intermediate%clon,  palm_intermediate%clat,                &  
+               palm_intermediate%clonu, palm_intermediate%clatu)
+
+    DEALLOCATE(cosmo_grid%lon,  cosmo_grid%lat,                                &
+               cosmo_grid%lonu, cosmo_grid%latv,                               &
+               cosmo_grid%hfl)
+
+ END SUBROUTINE fini_grids
 
 
@@ -2331,21 +2350,21 @@
 !> Initializes the variable list.
 !------------------------------------------------------------------------------!
-    SUBROUTINE setup_variable_tables(ic_mode)
-       CHARACTER(LEN=*), INTENT(IN) ::  ic_mode
-       INTEGER                      ::  n_invar = 0  !< number of variables in the input variable table
-       INTEGER                      ::  n_outvar = 0 !< number of variables in the output variable table
-       TYPE(nc_var), POINTER        ::  var
-
-       IF (TRIM(cfg % start_date) == '')  THEN
-          message = 'Simulation start date has not been set.'
-          CALL inifor_abort('setup_variable_tables', message)
-       ENDIF
-
-       nc_source_text = 'COSMO-DE analysis from ' // TRIM(cfg % start_date)
-
-       n_invar = 17
-       n_outvar = 64
-       ALLOCATE( input_var_table(n_invar) )
-       ALLOCATE( output_var_table(n_outvar) )
+ SUBROUTINE setup_variable_tables(ic_mode)
+    CHARACTER(LEN=*), INTENT(IN) ::  ic_mode
+    INTEGER                      ::  n_invar = 0  !< number of variables in the input variable table
+    INTEGER                      ::  n_outvar = 0 !< number of variables in the output variable table
+    TYPE(nc_var), POINTER        ::  var
+
+    IF (TRIM(cfg%start_date) == '')  THEN
+       message = 'Simulation start date has not been set.'
+       CALL inifor_abort('setup_variable_tables', message)
+    ENDIF
+
+    nc_source_text = 'COSMO-DE analysis from ' // TRIM(cfg%start_date)
+
+    n_invar = 17
+    n_outvar = 64
+    ALLOCATE( input_var_table(n_invar) )
+    ALLOCATE( output_var_table(n_outvar) )
 
 !
@@ -2353,88 +2372,88 @@
 !- Section 1: NetCDF input variables
 !------------------------------------------------------------------------------
-       var => input_var_table(1)
-       var % name = 'T_SO'
-       var % to_be_processed = .TRUE.
-       var % is_upside_down = .FALSE.
-
-       var => input_var_table(2)
-       var % name = 'W_SO'
-       var % to_be_processed = .TRUE.
-       var % is_upside_down = .FALSE.
-
-       var => input_var_table(3)
-       var % name = 'T'
-       var % to_be_processed = .TRUE.
-       var % is_upside_down = .TRUE.
-
-       var => input_var_table(4)
-       var % name = 'QV'
-       var % to_be_processed = .TRUE.
-       var % is_upside_down = .TRUE.
-
-       var => input_var_table(5)
-       var % name = 'U'
-       var % to_be_processed = .TRUE.
-       var % is_upside_down = .TRUE.
-
-       var => input_var_table(6)
-       var % name = 'V'
-       var % to_be_processed = .TRUE.
-       var % is_upside_down = .TRUE.
-
-       var => input_var_table(7)
-       var % name = 'W'
-       var % to_be_processed = .TRUE.
-       var % is_upside_down = .TRUE.
-
-       var => input_var_table(8)
-       var % name = 'RAIN_GSP'
-       var % to_be_processed = .TRUE.
-       var % is_upside_down = .FALSE.
-
-       var => input_var_table(9)
-       var % name = 'SNOW_GSP'
-       var % to_be_processed = .TRUE.
-       var % is_upside_down = .FALSE.
-
-       var => input_var_table(10)
-       var % name = 'GRAU_GSP'
-       var % to_be_processed = .TRUE.
-       var % is_upside_down = .FALSE.
-
-       var => input_var_table(11)
-       var % name = 'AEVAP_S'
-       var % to_be_processed = .TRUE.
-       var % is_upside_down = .FALSE.
-
-       var => input_var_table(12)
-       var % name = 'T_2M'
-       var % to_be_processed = .TRUE.
-       var % is_upside_down = .FALSE.
-
-       var => input_var_table(13)
-       var % name = 'ASWDIFD_S'
-       var % to_be_processed = .TRUE.
-       var % is_upside_down = .FALSE.
-
-       var => input_var_table(14)
-       var % name = 'ASWDIR_S'
-       var % to_be_processed = .TRUE.
-       var % is_upside_down = .FALSE.
-
-       var => input_var_table(15)
-       var % name = 'ASOB_S'
-       var % to_be_processed = .TRUE.
-       var % is_upside_down = .FALSE.
-
-       var => input_var_table(16)
-       var % name = 'ATHB_S'
-       var % to_be_processed = .TRUE.
-       var % is_upside_down = .FALSE.
-
-       var => input_var_table(17)
-       var % name = 'P'
-       var % to_be_processed = .TRUE.
-       var % is_upside_down = .TRUE.
+    var => input_var_table(1)
+    var%name = 'T_SO'
+    var%to_be_processed = .TRUE.
+    var%is_upside_down = .FALSE.
+
+    var => input_var_table(2)
+    var%name = 'W_SO'
+    var%to_be_processed = .TRUE.
+    var%is_upside_down = .FALSE.
+
+    var => input_var_table(3)
+    var%name = 'T'
+    var%to_be_processed = .TRUE.
+    var%is_upside_down = .TRUE.
+
+    var => input_var_table(4)
+    var%name = 'QV'
+    var%to_be_processed = .TRUE.
+    var%is_upside_down = .TRUE.
+
+    var => input_var_table(5)
+    var%name = 'U'
+    var%to_be_processed = .TRUE.
+    var%is_upside_down = .TRUE.
+
+    var => input_var_table(6)
+    var%name = 'V'
+    var%to_be_processed = .TRUE.
+    var%is_upside_down = .TRUE.
+
+    var => input_var_table(7)
+    var%name = 'W'
+    var%to_be_processed = .TRUE.
+    var%is_upside_down = .TRUE.
+
+    var => input_var_table(8)
+    var%name = 'RAIN_GSP'
+    var%to_be_processed = .TRUE.
+    var%is_upside_down = .FALSE.
+
+    var => input_var_table(9)
+    var%name = 'SNOW_GSP'
+    var%to_be_processed = .TRUE.
+    var%is_upside_down = .FALSE.
+
+    var => input_var_table(10)
+    var%name = 'GRAU_GSP'
+    var%to_be_processed = .TRUE.
+    var%is_upside_down = .FALSE.
+
+    var => input_var_table(11)
+    var%name = 'AEVAP_S'
+    var%to_be_processed = .TRUE.
+    var%is_upside_down = .FALSE.
+
+    var => input_var_table(12)
+    var%name = 'T_2M'
+    var%to_be_processed = .TRUE.
+    var%is_upside_down = .FALSE.
+
+    var => input_var_table(13)
+    var%name = 'ASWDIFD_S'
+    var%to_be_processed = .TRUE.
+    var%is_upside_down = .FALSE.
+
+    var => input_var_table(14)
+    var%name = 'ASWDIR_S'
+    var%to_be_processed = .TRUE.
+    var%is_upside_down = .FALSE.
+
+    var => input_var_table(15)
+    var%name = 'ASOB_S'
+    var%to_be_processed = .TRUE.
+    var%is_upside_down = .FALSE.
+
+    var => input_var_table(16)
+    var%name = 'ATHB_S'
+    var%to_be_processed = .TRUE.
+    var%is_upside_down = .FALSE.
+
+    var => input_var_table(17)
+    var%name = 'P'
+    var%to_be_processed = .TRUE.
+    var%is_upside_down = .TRUE.
 
 !
@@ -2446,840 +2465,840 @@
 ! Section 2.1: Realistic forcings, i.e. 3D initial and boundary conditions
 !------------------------------------------------------------------------------
-       output_var_table(1) = init_nc_var(                                      &
-          name              = 'init_soil_t',                                   &
-          std_name          = "",                                              &
-          long_name         = "initial soil temperature",                      &
-          units             = "K",                                             &
-          kind              = "init soil",                                     &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = palm_grid,                                       &
-          intermediate_grid = palm_intermediate                                &
-       )
-
-       output_var_table(2) = init_nc_var(                                      &
-          name              = 'init_soil_m',                                   &
-          std_name          = "",                                              &
-          long_name         = "initial soil moisture",                         &
-          units             = "m^3/m^3",                                       &
-          kind              = "init soil",                                     &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = palm_grid,                                       &
-          intermediate_grid = palm_intermediate                                &
-       )
-
-       output_var_table(3) = init_nc_var(                                      &
-          name              = 'init_atmosphere_pt',                            &
-          std_name          = "",                                              &
-          long_name         = "initial potential temperature",                 &
-          units             = "K",                                             &
-          kind              = "init scalar",                                   &
-          input_id          = 1,                                               & ! first in (T, p) IO group
-          output_file       = output_file,                                     &
-          grid              = palm_grid,                                       &
-          intermediate_grid = palm_intermediate,                               &
-          is_profile = (TRIM(ic_mode) == 'profile')                            &
-       )
-       IF (TRIM(ic_mode) == 'profile')  THEN
-          output_var_table(3) % averaging_grid => averaged_initial_scalar_profile
-       ENDIF
-
-       output_var_table(4) = init_nc_var(                                      &
-          name              = 'ls_forcing_left_pt',                            &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for left model boundary for the potential temperature", &
-          units             = "K",                                             &
-          kind              = "left scalar",                                   &
-          input_id          = 1,                                               &
-          grid              = scalars_west_grid,                               &
-          intermediate_grid = scalars_west_intermediate,                       &
-          output_file = output_file                                            &
-       )
-
-       output_var_table(5) = init_nc_var(                                      &
-          name              = 'ls_forcing_right_pt',                           &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for right model boundary for the potential temperature", &
-          units             = "K",                                             &
-          kind              = "right scalar",                                  &
-          input_id          = 1,                                               &
-          grid              = scalars_east_grid,                               &
-          intermediate_grid = scalars_east_intermediate,                       &
-          output_file = output_file                                            &
-       )
-
-       output_var_table(6) = init_nc_var(                                      &
-          name              = 'ls_forcing_north_pt',                           &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for north model boundary for the potential temperature", &
-          units             = "K",                                             &
-          kind              = "north scalar",                                  &
-          input_id          = 1,                                               &
-          grid              = scalars_north_grid,                              &
-          intermediate_grid = scalars_north_intermediate,                      &
-          output_file = output_file                                            &
-       )
-
-       output_var_table(7) = init_nc_var(                                      &
-          name              = 'ls_forcing_south_pt',                           &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for south model boundary for the potential temperature", &
-          units             = "K",                                             &
-          kind              = "south scalar",                                  &
-          input_id          = 1,                                               &
-          grid              = scalars_south_grid,                              &
-          intermediate_grid = scalars_south_intermediate,                      &
-          output_file = output_file                                            &
-       )
-
-       output_var_table(8) = init_nc_var(                                      &
-          name              = 'ls_forcing_top_pt',                             &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for top model boundary for the potential temperature", &
-          units             = "K",                                             &
-          kind              = "top scalar",                                    &
-          input_id          = 1,                                               &
-          grid              = scalars_top_grid,                                &
-          intermediate_grid = scalars_top_intermediate,                        &
-          output_file = output_file                                            &
-       )
-
-       output_var_table(9) = init_nc_var(                                      &
-          name              = 'init_atmosphere_qv',                            &
-          std_name          = "",                                              &
-          long_name         = "initial specific humidity",                     &
-          units             = "kg/kg",                                         &
-          kind              = "init scalar",                                   &
-          input_id          = 3,                                               &
-          output_file       = output_file,                                     &
-          grid              = palm_grid,                                       &
-          intermediate_grid = palm_intermediate,                               &
-          is_profile = (TRIM(ic_mode) == 'profile')                            &
-       )
-       IF (TRIM(ic_mode) == 'profile')  THEN
-          output_var_table(9) % averaging_grid => averaged_initial_scalar_profile
-       ENDIF
-
-       output_var_table(10) = init_nc_var(                                     &
-          name              = 'ls_forcing_left_qv',                            &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for left model boundary for the specific humidity", &
-          units             = "kg/kg",                                         &
-          kind              = "left scalar",                                   &
-          input_id          = 3,                                               &
-          output_file       = output_file,                                     &
-          grid              = scalars_west_grid,                               &
-          intermediate_grid = scalars_west_intermediate                        &
-       )
-
-       output_var_table(11) = init_nc_var(                                     &
-          name              = 'ls_forcing_right_qv',                           &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for right model boundary for the specific humidity", &
-          units             = "kg/kg",                                         &
-          kind              = "right scalar",                                  &
-          input_id          = 3,                                               &
-          output_file       = output_file,                                     &
-          grid              = scalars_east_grid,                               &
-          intermediate_grid = scalars_east_intermediate                        &
-       )
-
-       output_var_table(12) = init_nc_var(                                     &
-          name              = 'ls_forcing_north_qv',                           &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for north model boundary for the specific humidity", &
-          units             = "kg/kg",                                         &
-          kind              = "north scalar",                                  &
-          input_id          = 3,                                               &
-          output_file       = output_file,                                     &
-          grid              = scalars_north_grid,                              &
-          intermediate_grid = scalars_north_intermediate                       &
-       )
-
-       output_var_table(13) = init_nc_var(                                     &
-          name              = 'ls_forcing_south_qv',                           &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for south model boundary for the specific humidity", &
-          units             = "kg/kg",                                         &
-          kind              = "south scalar",                                  &
-          input_id          = 3,                                               &
-          output_file       = output_file,                                     &
-          grid              = scalars_south_grid,                              &
-          intermediate_grid = scalars_south_intermediate                       &
-       )
-
-       output_var_table(14) = init_nc_var(                                     &
-          name              = 'ls_forcing_top_qv',                             &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for top model boundary for the specific humidity", &
-          units             = "kg/kg",                                         &
-          kind              = "top scalar",                                    &
-          input_id          = 3,                                               &
-          output_file       = output_file,                                     &
-          grid              = scalars_top_grid,                                &
-          intermediate_grid = scalars_top_intermediate                         &
-       )
-
-       output_var_table(15) = init_nc_var(                                     &
-          name              = 'init_atmosphere_u',                             &
-          std_name          = "",                                              &
-          long_name         = "initial wind component in x direction",         &
-          units             = "m/s",                                           &
-          kind              = "init u",                                        &
-          input_id          = 1,                                               & ! first in (U, V) I/O group
-          output_file       = output_file,                                     &
-          grid              = u_initial_grid,                                  &
-          intermediate_grid = u_initial_intermediate,                          &
-          is_profile = (TRIM(ic_mode) == 'profile')                            &
-       )
-       IF (TRIM(ic_mode) == 'profile')  THEN
-          output_var_table(15) % averaging_grid => averaged_initial_scalar_profile
-       ENDIF 
-
-       output_var_table(16) = init_nc_var(                                     &
-          name              = 'ls_forcing_left_u',                             &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for left model boundary for the wind component in x direction", &
-          units             = "m/s",                                           &
-          kind              = "left u",                                        &
-          input_id          = 1,                                               & ! first in (U, V) I/O group
-          output_file       = output_file,                                     &
-          grid              = u_west_grid,                                     &
-          intermediate_grid = u_west_intermediate                              &
-       )
-
-       output_var_table(17) = init_nc_var(                                     &
-          name              = 'ls_forcing_right_u',                            &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for right model boundary for the wind component in x direction", &
-          units             = "m/s",                                           &
-          kind              = "right u",                                       &
-          input_id          = 1,                                               & ! first in (U, V) I/O group
-          output_file       = output_file,                                     &
-          grid              = u_east_grid,                                     &
-          intermediate_grid = u_east_intermediate                              &
-       )
-
-       output_var_table(18) = init_nc_var(                                     &
-          name              = 'ls_forcing_north_u',                            &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for north model boundary for the wind component in x direction", &
-          units             = "m/s",                                           &
-          kind              = "north u",                                       &
-          input_id          = 1,                                               & ! first in (U, V) I/O group
-          output_file       = output_file,                                     &
-          grid              = u_north_grid,                                    &
-          intermediate_grid = u_north_intermediate                             &
-       )
-
-       output_var_table(19) = init_nc_var(                                     &
-          name              = 'ls_forcing_south_u',                            &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for south model boundary for the wind component in x direction", &
-          units             = "m/s",                                           &
-          kind              = "south u",                                       &
-          input_id          = 1,                                               & ! first in (U, V) I/O group
-          output_file       = output_file,                                     &
-          grid              = u_south_grid,                                    &
-          intermediate_grid = u_south_intermediate                             &
-       )
-
-       output_var_table(20) = init_nc_var(                                     &
-          name              = 'ls_forcing_top_u',                              &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for top model boundary for the wind component in x direction", &
-          units             = "m/s",                                           &
-          kind              = "top u",                                         &
-          input_id          = 1,                                               & ! first in (U, V) I/O group
-          output_file       = output_file,                                     &
-          grid              = u_top_grid,                                      &
-          intermediate_grid = u_top_intermediate                               &
-       )
-
-       output_var_table(21) = init_nc_var(                                     &
-          name              = 'init_atmosphere_v',                             &
-          std_name          = "",                                              &
-          long_name         = "initial wind component in y direction",         &
-          units             = "m/s",                                           &
-          kind              = "init v",                                        &
-          input_id          = 2,                                               & ! second in (U, V) I/O group
-          output_file       = output_file,                                     &
-          grid              = v_initial_grid,                                  &
-          intermediate_grid = v_initial_intermediate,                          &
-          is_profile = (TRIM(ic_mode) == 'profile')                            &
-       )
-       IF (TRIM(ic_mode) == 'profile')  THEN
-          output_var_table(21) % averaging_grid => averaged_initial_scalar_profile
-       ENDIF
-
-       output_var_table(22) = init_nc_var(                                     &
-          name              = 'ls_forcing_left_v',                             &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for left model boundary for the wind component in y direction", &
-          units             = "m/s",                                           &
-          kind              = "right v",                                       &
-          input_id          = 2,                                               & ! second in (U, V) I/O group
-          output_file       = output_file,                                     &
-          grid              = v_west_grid,                                     &
-          intermediate_grid = v_west_intermediate                              &
-       )
-
-       output_var_table(23) = init_nc_var(                                     &
-          name              = 'ls_forcing_right_v',                            &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for right model boundary for the wind component in y direction", &
-          units             = "m/s",                                           &
-          kind              = "right v",                                       &
-          input_id          = 2,                                               & ! second in (U, V) I/O group
-          output_file       = output_file,                                     &
-          grid              = v_east_grid,                                     &
-          intermediate_grid = v_east_intermediate                              &
-       )
-
-       output_var_table(24) = init_nc_var(                                     &
-          name              = 'ls_forcing_north_v',                            &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for north model boundary for the wind component in y direction", &
-          units             = "m/s",                                           &
-          kind              = "north v",                                       &
-          input_id          = 2,                                               & ! second in (U, V) I/O group
-          output_file       = output_file,                                     &
-          grid              = v_north_grid,                                    &
-          intermediate_grid = v_north_intermediate                             &
-       )
-
-       output_var_table(25) = init_nc_var(                                     &
-          name              = 'ls_forcing_south_v',                            &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for south model boundary for the wind component in y direction", &
-          units             = "m/s",                                           &
-          kind              = "south v",                                       &
-          input_id          = 2,                                               & ! second in (U, V) I/O group
-          output_file       = output_file,                                     &
-          grid              = v_south_grid,                                    &
-          intermediate_grid = v_south_intermediate                             &
-       )
-
-       output_var_table(26) = init_nc_var(                                     &
-          name              = 'ls_forcing_top_v',                              &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for top model boundary for the wind component in y direction", &
-          units             = "m/s",                                           &
-          kind              = "top v",                                         &
-          input_id          = 2,                                               & ! second in (U, V) I/O group
-          output_file       = output_file,                                     &
-          grid              = v_top_grid,                                      &
-          intermediate_grid = v_top_intermediate                               &
-       )
-
-       output_var_table(27) = init_nc_var(                                     &
-          name              = 'init_atmosphere_w',                             &
-          std_name          = "",                                              &
-          long_name         = "initial wind component in z direction",         &
-          units             = "m/s",                                           &
-          kind              = "init w",                                        &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = w_initial_grid,                                  &
-          intermediate_grid = w_initial_intermediate,                          &
-          is_profile = (TRIM(ic_mode) == 'profile')                            &
-       )
-       IF (TRIM(ic_mode) == 'profile')  THEN
-          output_var_table(27) % averaging_grid => averaged_initial_w_profile
-       ENDIF
-
-       output_var_table(28) = init_nc_var(                                     &
-          name              = 'ls_forcing_left_w',                             &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for left model boundary for the wind component in z direction", &
-          units             = "m/s",                                           &
-          kind              = "left w",                                        &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = w_west_grid,                                     &
-          intermediate_grid = w_west_intermediate                              &
-       )
-
-       output_var_table(29) = init_nc_var(                                     &
-          name              = 'ls_forcing_right_w',                            &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for right model boundary for the wind component in z direction", &
-          units             = "m/s",                                           &
-          kind              = "right w",                                       &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = w_east_grid,                                     &
-          intermediate_grid = w_east_intermediate                              &
-       )
-
-       output_var_table(30) = init_nc_var(                                     &
-          name              = 'ls_forcing_north_w',                            &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for north model boundary for the wind component in z direction", &
-          units             = "m/s",                                           &
-          kind              = "north w",                                       &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = w_north_grid,                                    &
-          intermediate_grid = w_north_intermediate                             &
-       )
-
-       output_var_table(31) = init_nc_var(                                     &
-          name              = 'ls_forcing_south_w',                            &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for south model boundary for the wind component in z direction", &
-          units             = "m/s",                                           &
-          kind              = "south w",                                       &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = w_south_grid,                                    &
-          intermediate_grid = w_south_intermediate                             &
-       )
-
-       output_var_table(32) = init_nc_var(                                     &
-          name              = 'ls_forcing_top_w',                              &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing for top model boundary for the wind component in z direction", &
-          units             = "m/s",                                           &
-          kind              = "top w",                                         &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = w_top_grid,                                      &
-          intermediate_grid = w_top_intermediate                               &
-       )
-
-       output_var_table(33) = init_nc_var(                                     &
-          name              = 'ls_forcing_soil_rain',                          &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing rain",                      &
-          units             = "kg/m2",                                         &
-          kind              = "surface forcing",                               &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = palm_grid,                                       &
-          intermediate_grid = palm_intermediate                                &
-       )
-
-       output_var_table(34) = init_nc_var(                                     &
-          name              = 'ls_forcing_soil_snow',                          &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing snow",                      &
-          units             = "kg/m2",                                         &
-          kind              = "surface forcing",                               &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = palm_grid,                                       &
-          intermediate_grid = palm_intermediate                                &
-       )
-
-       output_var_table(35) = init_nc_var(                                     &
-          name              = 'ls_forcing_soil_graupel',                       &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing graupel",                   &
-          units             = "kg/m2",                                         &
-          kind              = "surface forcing",                               &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = palm_grid,                                       &
-          intermediate_grid = palm_intermediate                                &
-       )
-
-       output_var_table(36) = init_nc_var(                                     &
-          name              = 'ls_forcing_soil_t_2m',                          &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing 2m air temperature",        &
-          units             = "kg/m2",                                         &
-          kind              = "surface forcing",                               &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = palm_grid,                                       &
-          intermediate_grid = palm_intermediate                                &
-       )
-
-       output_var_table(37) = init_nc_var(                                     &
-          name              = 'ls_forcing_soil_evap',                          &
-          std_name          = "",                                              &
-          long_name         = "large-scale forcing evapo-transpiration",       &
-          units             = "kg/m2",                                         &
-          kind              = "surface forcing",                               &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = palm_grid,                                       &
-          intermediate_grid = palm_intermediate                                &
-       )
-
-       output_var_table(38) = init_nc_var(                                     &
-          name              = 'rad_swd_dif_0',                                 &
-          std_name          = "",                                              &
-          long_name         = "incoming diffuse shortwave radiative flux at the surface", &
-          units             = "W/m2",                                          &
-          kind              = "surface forcing",                               &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = palm_grid,                                       &
-          intermediate_grid = palm_intermediate                                &
-       )
-
-       output_var_table(39) = init_nc_var(                                     &
-          name              = 'rad_swd_dir_0',                                 &
-          std_name          = "",                                              &
-          long_name         = "incoming direct shortwave radiative flux at the surface", &
-          units             = "W/m2",                                          &
-          kind              = "surface forcing",                               &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = palm_grid,                                       &
-          intermediate_grid = palm_intermediate                                &
-       )
-
-       output_var_table(40) = init_nc_var(                                     &
-          name              = 'rad_sw_bal_0',                                  &
-          std_name          = "",                                              &
-          long_name         = "shortwave radiation balance at the surface",    &
-          units             = "W/m2",                                          &
-          kind              = "surface forcing",                               &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = palm_grid,                                       &
-          intermediate_grid = palm_intermediate                                &
-       )
-
-       output_var_table(41) = init_nc_var(                                     &
-          name              = 'rad_lw_bal_0',                                  &
-          std_name          = "",                                              &
-          long_name         = "longwave radiation balance at the surface",     &
-          units             = "W/m2",                                          &
-          kind              = "surface forcing",                               &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = palm_grid,                                       &
-          intermediate_grid = palm_intermediate                                &
-       )
+    output_var_table(1) = init_nc_var(                                      &
+       name              = 'init_soil_t',                                   &
+       std_name          = "",                                              &
+       long_name         = "initial soil temperature",                      &
+       units             = "K",                                             &
+       kind              = "init soil",                                     &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = palm_grid,                                       &
+       intermediate_grid = palm_intermediate                                &
+    )
+
+    output_var_table(2) = init_nc_var(                                      &
+       name              = 'init_soil_m',                                   &
+       std_name          = "",                                              &
+       long_name         = "initial soil moisture",                         &
+       units             = "m^3/m^3",                                       &
+       kind              = "init soil",                                     &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = palm_grid,                                       &
+       intermediate_grid = palm_intermediate                                &
+    )
+
+    output_var_table(3) = init_nc_var(                                      &
+       name              = 'init_atmosphere_pt',                            &
+       std_name          = "",                                              &
+       long_name         = "initial potential temperature",                 &
+       units             = "K",                                             &
+       kind              = "init scalar",                                   &
+       input_id          = 1,                                               & ! first in (T, p) IO group
+       output_file       = output_file,                                     &
+       grid              = palm_grid,                                       &
+       intermediate_grid = palm_intermediate,                               &
+       is_profile = (TRIM(ic_mode) == 'profile')                            &
+    )
+    IF (TRIM(ic_mode) == 'profile')  THEN
+       output_var_table(3)%averaging_grid => averaged_initial_scalar_profile
+    ENDIF
+
+    output_var_table(4) = init_nc_var(                                      &
+       name              = 'ls_forcing_left_pt',                            &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for left model boundary for the potential temperature", &
+       units             = "K",                                             &
+       kind              = "left scalar",                                   &
+       input_id          = 1,                                               &
+       grid              = scalars_west_grid,                               &
+       intermediate_grid = scalars_west_intermediate,                       &
+       output_file = output_file                                            &
+    )
+
+    output_var_table(5) = init_nc_var(                                      &
+       name              = 'ls_forcing_right_pt',                           &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for right model boundary for the potential temperature", &
+       units             = "K",                                             &
+       kind              = "right scalar",                                  &
+       input_id          = 1,                                               &
+       grid              = scalars_east_grid,                               &
+       intermediate_grid = scalars_east_intermediate,                       &
+       output_file = output_file                                            &
+    )
+
+    output_var_table(6) = init_nc_var(                                      &
+       name              = 'ls_forcing_north_pt',                           &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for north model boundary for the potential temperature", &
+       units             = "K",                                             &
+       kind              = "north scalar",                                  &
+       input_id          = 1,                                               &
+       grid              = scalars_north_grid,                              &
+       intermediate_grid = scalars_north_intermediate,                      &
+       output_file = output_file                                            &
+    )
+
+    output_var_table(7) = init_nc_var(                                      &
+       name              = 'ls_forcing_south_pt',                           &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for south model boundary for the potential temperature", &
+       units             = "K",                                             &
+       kind              = "south scalar",                                  &
+       input_id          = 1,                                               &
+       grid              = scalars_south_grid,                              &
+       intermediate_grid = scalars_south_intermediate,                      &
+       output_file = output_file                                            &
+    )
+
+    output_var_table(8) = init_nc_var(                                      &
+       name              = 'ls_forcing_top_pt',                             &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for top model boundary for the potential temperature", &
+       units             = "K",                                             &
+       kind              = "top scalar",                                    &
+       input_id          = 1,                                               &
+       grid              = scalars_top_grid,                                &
+       intermediate_grid = scalars_top_intermediate,                        &
+       output_file = output_file                                            &
+    )
+
+    output_var_table(9) = init_nc_var(                                      &
+       name              = 'init_atmosphere_qv',                            &
+       std_name          = "",                                              &
+       long_name         = "initial specific humidity",                     &
+       units             = "kg/kg",                                         &
+       kind              = "init scalar",                                   &
+       input_id          = 3,                                               &
+       output_file       = output_file,                                     &
+       grid              = palm_grid,                                       &
+       intermediate_grid = palm_intermediate,                               &
+       is_profile = (TRIM(ic_mode) == 'profile')                            &
+    )
+    IF (TRIM(ic_mode) == 'profile')  THEN
+       output_var_table(9)%averaging_grid => averaged_initial_scalar_profile
+    ENDIF
+
+    output_var_table(10) = init_nc_var(                                     &
+       name              = 'ls_forcing_left_qv',                            &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for left model boundary for the specific humidity", &
+       units             = "kg/kg",                                         &
+       kind              = "left scalar",                                   &
+       input_id          = 3,                                               &
+       output_file       = output_file,                                     &
+       grid              = scalars_west_grid,                               &
+       intermediate_grid = scalars_west_intermediate                        &
+    )
+
+    output_var_table(11) = init_nc_var(                                     &
+       name              = 'ls_forcing_right_qv',                           &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for right model boundary for the specific humidity", &
+       units             = "kg/kg",                                         &
+       kind              = "right scalar",                                  &
+       input_id          = 3,                                               &
+       output_file       = output_file,                                     &
+       grid              = scalars_east_grid,                               &
+       intermediate_grid = scalars_east_intermediate                        &
+    )
+
+    output_var_table(12) = init_nc_var(                                     &
+       name              = 'ls_forcing_north_qv',                           &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for north model boundary for the specific humidity", &
+       units             = "kg/kg",                                         &
+       kind              = "north scalar",                                  &
+       input_id          = 3,                                               &
+       output_file       = output_file,                                     &
+       grid              = scalars_north_grid,                              &
+       intermediate_grid = scalars_north_intermediate                       &
+    )
+
+    output_var_table(13) = init_nc_var(                                     &
+       name              = 'ls_forcing_south_qv',                           &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for south model boundary for the specific humidity", &
+       units             = "kg/kg",                                         &
+       kind              = "south scalar",                                  &
+       input_id          = 3,                                               &
+       output_file       = output_file,                                     &
+       grid              = scalars_south_grid,                              &
+       intermediate_grid = scalars_south_intermediate                       &
+    )
+
+    output_var_table(14) = init_nc_var(                                     &
+       name              = 'ls_forcing_top_qv',                             &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for top model boundary for the specific humidity", &
+       units             = "kg/kg",                                         &
+       kind              = "top scalar",                                    &
+       input_id          = 3,                                               &
+       output_file       = output_file,                                     &
+       grid              = scalars_top_grid,                                &
+       intermediate_grid = scalars_top_intermediate                         &
+    )
+
+    output_var_table(15) = init_nc_var(                                     &
+       name              = 'init_atmosphere_u',                             &
+       std_name          = "",                                              &
+       long_name         = "initial wind component in x direction",         &
+       units             = "m/s",                                           &
+       kind              = "init u",                                        &
+       input_id          = 1,                                               & ! first in (U, V) I/O group
+       output_file       = output_file,                                     &
+       grid              = u_initial_grid,                                  &
+       intermediate_grid = u_initial_intermediate,                          &
+       is_profile = (TRIM(ic_mode) == 'profile')                            &
+    )
+    IF (TRIM(ic_mode) == 'profile')  THEN
+       output_var_table(15)%averaging_grid => averaged_initial_scalar_profile
+    ENDIF 
+
+    output_var_table(16) = init_nc_var(                                     &
+       name              = 'ls_forcing_left_u',                             &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for left model boundary for the wind component in x direction", &
+       units             = "m/s",                                           &
+       kind              = "left u",                                        &
+       input_id          = 1,                                               & ! first in (U, V) I/O group
+       output_file       = output_file,                                     &
+       grid              = u_west_grid,                                     &
+       intermediate_grid = u_west_intermediate                              &
+    )
+
+    output_var_table(17) = init_nc_var(                                     &
+       name              = 'ls_forcing_right_u',                            &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for right model boundary for the wind component in x direction", &
+       units             = "m/s",                                           &
+       kind              = "right u",                                       &
+       input_id          = 1,                                               & ! first in (U, V) I/O group
+       output_file       = output_file,                                     &
+       grid              = u_east_grid,                                     &
+       intermediate_grid = u_east_intermediate                              &
+    )
+
+    output_var_table(18) = init_nc_var(                                     &
+       name              = 'ls_forcing_north_u',                            &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for north model boundary for the wind component in x direction", &
+       units             = "m/s",                                           &
+       kind              = "north u",                                       &
+       input_id          = 1,                                               & ! first in (U, V) I/O group
+       output_file       = output_file,                                     &
+       grid              = u_north_grid,                                    &
+       intermediate_grid = u_north_intermediate                             &
+    )
+
+    output_var_table(19) = init_nc_var(                                     &
+       name              = 'ls_forcing_south_u',                            &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for south model boundary for the wind component in x direction", &
+       units             = "m/s",                                           &
+       kind              = "south u",                                       &
+       input_id          = 1,                                               & ! first in (U, V) I/O group
+       output_file       = output_file,                                     &
+       grid              = u_south_grid,                                    &
+       intermediate_grid = u_south_intermediate                             &
+    )
+
+    output_var_table(20) = init_nc_var(                                     &
+       name              = 'ls_forcing_top_u',                              &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for top model boundary for the wind component in x direction", &
+       units             = "m/s",                                           &
+       kind              = "top u",                                         &
+       input_id          = 1,                                               & ! first in (U, V) I/O group
+       output_file       = output_file,                                     &
+       grid              = u_top_grid,                                      &
+       intermediate_grid = u_top_intermediate                               &
+    )
+
+    output_var_table(21) = init_nc_var(                                     &
+       name              = 'init_atmosphere_v',                             &
+       std_name          = "",                                              &
+       long_name         = "initial wind component in y direction",         &
+       units             = "m/s",                                           &
+       kind              = "init v",                                        &
+       input_id          = 2,                                               & ! second in (U, V) I/O group
+       output_file       = output_file,                                     &
+       grid              = v_initial_grid,                                  &
+       intermediate_grid = v_initial_intermediate,                          &
+       is_profile = (TRIM(ic_mode) == 'profile')                            &
+    )
+    IF (TRIM(ic_mode) == 'profile')  THEN
+       output_var_table(21)%averaging_grid => averaged_initial_scalar_profile
+    ENDIF
+
+    output_var_table(22) = init_nc_var(                                     &
+       name              = 'ls_forcing_left_v',                             &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for left model boundary for the wind component in y direction", &
+       units             = "m/s",                                           &
+       kind              = "right v",                                       &
+       input_id          = 2,                                               & ! second in (U, V) I/O group
+       output_file       = output_file,                                     &
+       grid              = v_west_grid,                                     &
+       intermediate_grid = v_west_intermediate                              &
+    )
+
+    output_var_table(23) = init_nc_var(                                     &
+       name              = 'ls_forcing_right_v',                            &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for right model boundary for the wind component in y direction", &
+       units             = "m/s",                                           &
+       kind              = "right v",                                       &
+       input_id          = 2,                                               & ! second in (U, V) I/O group
+       output_file       = output_file,                                     &
+       grid              = v_east_grid,                                     &
+       intermediate_grid = v_east_intermediate                              &
+    )
+
+    output_var_table(24) = init_nc_var(                                     &
+       name              = 'ls_forcing_north_v',                            &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for north model boundary for the wind component in y direction", &
+       units             = "m/s",                                           &
+       kind              = "north v",                                       &
+       input_id          = 2,                                               & ! second in (U, V) I/O group
+       output_file       = output_file,                                     &
+       grid              = v_north_grid,                                    &
+       intermediate_grid = v_north_intermediate                             &
+    )
+
+    output_var_table(25) = init_nc_var(                                     &
+       name              = 'ls_forcing_south_v',                            &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for south model boundary for the wind component in y direction", &
+       units             = "m/s",                                           &
+       kind              = "south v",                                       &
+       input_id          = 2,                                               & ! second in (U, V) I/O group
+       output_file       = output_file,                                     &
+       grid              = v_south_grid,                                    &
+       intermediate_grid = v_south_intermediate                             &
+    )
+
+    output_var_table(26) = init_nc_var(                                     &
+       name              = 'ls_forcing_top_v',                              &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for top model boundary for the wind component in y direction", &
+       units             = "m/s",                                           &
+       kind              = "top v",                                         &
+       input_id          = 2,                                               & ! second in (U, V) I/O group
+       output_file       = output_file,                                     &
+       grid              = v_top_grid,                                      &
+       intermediate_grid = v_top_intermediate                               &
+    )
+
+    output_var_table(27) = init_nc_var(                                     &
+       name              = 'init_atmosphere_w',                             &
+       std_name          = "",                                              &
+       long_name         = "initial wind component in z direction",         &
+       units             = "m/s",                                           &
+       kind              = "init w",                                        &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = w_initial_grid,                                  &
+       intermediate_grid = w_initial_intermediate,                          &
+       is_profile = (TRIM(ic_mode) == 'profile')                            &
+    )
+    IF (TRIM(ic_mode) == 'profile')  THEN
+       output_var_table(27)%averaging_grid => averaged_initial_w_profile
+    ENDIF
+
+    output_var_table(28) = init_nc_var(                                     &
+       name              = 'ls_forcing_left_w',                             &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for left model boundary for the wind component in z direction", &
+       units             = "m/s",                                           &
+       kind              = "left w",                                        &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = w_west_grid,                                     &
+       intermediate_grid = w_west_intermediate                              &
+    )
+
+    output_var_table(29) = init_nc_var(                                     &
+       name              = 'ls_forcing_right_w',                            &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for right model boundary for the wind component in z direction", &
+       units             = "m/s",                                           &
+       kind              = "right w",                                       &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = w_east_grid,                                     &
+       intermediate_grid = w_east_intermediate                              &
+    )
+
+    output_var_table(30) = init_nc_var(                                     &
+       name              = 'ls_forcing_north_w',                            &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for north model boundary for the wind component in z direction", &
+       units             = "m/s",                                           &
+       kind              = "north w",                                       &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = w_north_grid,                                    &
+       intermediate_grid = w_north_intermediate                             &
+    )
+
+    output_var_table(31) = init_nc_var(                                     &
+       name              = 'ls_forcing_south_w',                            &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for south model boundary for the wind component in z direction", &
+       units             = "m/s",                                           &
+       kind              = "south w",                                       &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = w_south_grid,                                    &
+       intermediate_grid = w_south_intermediate                             &
+    )
+
+    output_var_table(32) = init_nc_var(                                     &
+       name              = 'ls_forcing_top_w',                              &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing for top model boundary for the wind component in z direction", &
+       units             = "m/s",                                           &
+       kind              = "top w",                                         &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = w_top_grid,                                      &
+       intermediate_grid = w_top_intermediate                               &
+    )
+
+    output_var_table(33) = init_nc_var(                                     &
+       name              = 'ls_forcing_soil_rain',                          &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing rain",                      &
+       units             = "kg/m2",                                         &
+       kind              = "surface forcing",                               &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = palm_grid,                                       &
+       intermediate_grid = palm_intermediate                                &
+    )
+
+    output_var_table(34) = init_nc_var(                                     &
+       name              = 'ls_forcing_soil_snow',                          &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing snow",                      &
+       units             = "kg/m2",                                         &
+       kind              = "surface forcing",                               &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = palm_grid,                                       &
+       intermediate_grid = palm_intermediate                                &
+    )
+
+    output_var_table(35) = init_nc_var(                                     &
+       name              = 'ls_forcing_soil_graupel',                       &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing graupel",                   &
+       units             = "kg/m2",                                         &
+       kind              = "surface forcing",                               &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = palm_grid,                                       &
+       intermediate_grid = palm_intermediate                                &
+    )
+
+    output_var_table(36) = init_nc_var(                                     &
+       name              = 'ls_forcing_soil_t_2m',                          &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing 2m air temperature",        &
+       units             = "kg/m2",                                         &
+       kind              = "surface forcing",                               &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = palm_grid,                                       &
+       intermediate_grid = palm_intermediate                                &
+    )
+
+    output_var_table(37) = init_nc_var(                                     &
+       name              = 'ls_forcing_soil_evap',                          &
+       std_name          = "",                                              &
+       long_name         = "large-scale forcing evapo-transpiration",       &
+       units             = "kg/m2",                                         &
+       kind              = "surface forcing",                               &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = palm_grid,                                       &
+       intermediate_grid = palm_intermediate                                &
+    )
+
+    output_var_table(38) = init_nc_var(                                     &
+       name              = 'rad_swd_dif_0',                                 &
+       std_name          = "",                                              &
+       long_name         = "incoming diffuse shortwave radiative flux at the surface", &
+       units             = "W/m2",                                          &
+       kind              = "surface forcing",                               &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = palm_grid,                                       &
+       intermediate_grid = palm_intermediate                                &
+    )
+
+    output_var_table(39) = init_nc_var(                                     &
+       name              = 'rad_swd_dir_0',                                 &
+       std_name          = "",                                              &
+       long_name         = "incoming direct shortwave radiative flux at the surface", &
+       units             = "W/m2",                                          &
+       kind              = "surface forcing",                               &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = palm_grid,                                       &
+       intermediate_grid = palm_intermediate                                &
+    )
+
+    output_var_table(40) = init_nc_var(                                     &
+       name              = 'rad_sw_bal_0',                                  &
+       std_name          = "",                                              &
+       long_name         = "shortwave radiation balance at the surface",    &
+       units             = "W/m2",                                          &
+       kind              = "surface forcing",                               &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = palm_grid,                                       &
+       intermediate_grid = palm_intermediate                                &
+    )
+
+    output_var_table(41) = init_nc_var(                                     &
+       name              = 'rad_lw_bal_0',                                  &
+       std_name          = "",                                              &
+       long_name         = "longwave radiation balance at the surface",     &
+       units             = "W/m2",                                          &
+       kind              = "surface forcing",                               &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = palm_grid,                                       &
+       intermediate_grid = palm_intermediate                                &
+    )
 !
 !------------------------------------------------------------------------------
 ! Section 2.2: Idealized large-scale forcings
 !------------------------------------------------------------------------------
-       output_var_table(42) = init_nc_var(                                     &
-          name              = 'surface_forcing_surface_pressure',              &
-          std_name          = "",                                              &
-          long_name         = "surface pressure",                              &
-          units             = "Pa",                                            &
-          kind              = "time series",                                   &
-          input_id          = 2,                                               & ! second in (T, p) I/O group
-          output_file       = output_file,                                     &
-          grid              = palm_grid,                                       &
-          intermediate_grid = palm_intermediate                                &
-       )
-       output_var_table(42) % averaging_grid => averaged_scalar_profile
-
-       output_var_table(43) = init_nc_var(                                     &
-          name              = 'ls_forcing_ug',                                 &
-          std_name          = "",                                              &
-          long_name         = "geostrophic wind (u component)",                &
-          units             = "m/s",                                           &
-          kind              = "geostrophic",                                   &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = averaged_scalar_profile,                         &
-          intermediate_grid = averaged_scalar_profile                          &
-       )
-
-       output_var_table(44) = init_nc_var(                                     &
-          name              = 'ls_forcing_vg',                                 &
-          std_name          = "",                                              &
-          long_name         = "geostrophic wind (v component)",                &
-          units             = "m/s",                                           &
-          kind              = "geostrophic",                                   &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = averaged_scalar_profile,                         &
-          intermediate_grid = averaged_scalar_profile                          &
-       )
-
-       output_var_table(45) = init_nc_var(                                     &
-          name              = 'nudging_u',                                     &
-          std_name          = "",                                              &
-          long_name         = "wind component in x direction",                 &
-          units             = "m/s",                                           &
-          kind              = "geostrophic",                                   &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = averaged_scalar_profile,                         &
-          intermediate_grid = averaged_scalar_profile                          &
-       )
-       output_var_table(45) % to_be_processed = ls_forcing_variables_required
-
-       output_var_table(46) = init_nc_var(                                     &
-          name              = 'nudging_v',                                     &
-          std_name          = "",                                              &
-          long_name         = "wind component in y direction",                 &
-          units             = "m/s",                                           &
-          kind              = "large-scale scalar forcing",                    &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = averaged_scalar_profile,                         &
-          intermediate_grid = averaged_scalar_profile                          &
-       )
-       output_var_table(46) % to_be_processed = ls_forcing_variables_required
-
-       output_var_table(47) = init_nc_var(                                     &
-          name              = 'ls_forcing_sub_w',                              &
-          std_name          = "",                                              &
-          long_name         = "subsidence velocity of w",                      &
-          units             = "m/s",                                           &
-          kind              = "large-scale w forcing",                         &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = averaged_scalar_profile,                         &
-          intermediate_grid = averaged_scalar_profile                          &
-       )
-       output_var_table(47) % to_be_processed = ls_forcing_variables_required
-
-       output_var_table(48) = init_nc_var(                                     &
-          name              = 'nudging_w',                                     &
-          std_name          = "",                                              &
-          long_name         = "wind component in w direction",                 &
-          units             = "m/s",                                           &
-          kind              = "large-scale w forcing",                         &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = averaged_w_profile,                              &
-          intermediate_grid = averaged_w_profile                               &
-       )
-       output_var_table(48) % to_be_processed = ls_forcing_variables_required
-
-
-       output_var_table(49) = init_nc_var(                                     &
-          name              = 'ls_forcing_adv_pt',                             &
-          std_name          = "",                                              &
-          long_name         = "advection of potential temperature",            &
-          units             = "K/s",                                           &
-          kind              = "large-scale scalar forcing",                    &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = averaged_scalar_profile,                         &
-          intermediate_grid = averaged_scalar_profile                          &
-       )
-       output_var_table(49) % to_be_processed = ls_forcing_variables_required
-
-       output_var_table(50) = init_nc_var(                                     &
-          name              = 'ls_forcing_sub_pt',                             &
-          std_name          = "",                                              &
-          long_name         = "subsidence velocity of potential temperature",  &
-          units             = "K/s",                                           &
-          kind              = "large-scale scalar forcing",                    &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = averaged_scalar_profile,                         &
-          intermediate_grid = averaged_scalar_profile                          &
-       )
-       output_var_table(50) % to_be_processed = ls_forcing_variables_required
-
-       output_var_table(51) = init_nc_var(                                     &
-          name              = 'nudging_pt',                                    &
-          std_name          = "",                                              &
-          long_name         = "potential temperature",                         &
-          units             = "K",                                             &
-          kind              = "large-scale scalar forcing",                    &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = averaged_scalar_profile,                         &
-          intermediate_grid = averaged_scalar_profile                          &
-       )
-       output_var_table(51) % to_be_processed = ls_forcing_variables_required
-
-       output_var_table(52) = init_nc_var(                                     &
-          name              = 'ls_forcing_adv_qv',                             &
-          std_name          = "",                                              &
-          long_name         = "advection of specific humidity",                &
-          units             = "kg/kg/s",                                       &
-          kind              = "large-scale scalar forcing",                    &
-          input_id          = 3,                                               &
-          output_file       = output_file,                                     &
-          grid              = averaged_scalar_profile,                         &
-          intermediate_grid = averaged_scalar_profile                          &
-       )
-       output_var_table(52) % to_be_processed = ls_forcing_variables_required
-
-
-       output_var_table(53) = init_nc_var(                                     &
-          name              = 'ls_forcing_sub_qv',                             &
-          std_name          = "",                                              &
-          long_name         = "subsidence velocity of specific humidity",      &
-          units             = "kg/kg/s",                                       &
-          kind              = "large-scale scalar forcing",                    &
-          input_id          = 3,                                               &
-          output_file       = output_file,                                     &
-          grid              = averaged_scalar_profile,                         &
-          intermediate_grid = averaged_scalar_profile                          &
-       )
-       output_var_table(53) % to_be_processed = ls_forcing_variables_required
-
-       output_var_table(54) = init_nc_var(                                     &
-          name              = 'nudging_qv',                                    &
-          std_name          = "",                                              &
-          long_name         = "specific humidity",                             &
-          units             = "kg/kg",                                         &
-          kind              = "large-scale scalar forcing",                    &
-          input_id          = 3,                                               &
-          output_file       = output_file,                                     &
-          grid              = averaged_scalar_profile,                         &
-          intermediate_grid = averaged_scalar_profile                          &
-       )
-       output_var_table(54) % to_be_processed = ls_forcing_variables_required
-
-       output_var_table(55) = init_nc_var(                                     &
-          name              = 'nudging_tau',                                   &
-          std_name          = "",                                              &
-          long_name         = "nudging relaxation time scale",                 &
-          units             = "s",                                             &
-          kind              = "constant scalar profile",                       &
-          input_id          = 1,                                               &
-          output_file       = output_file,                                     &
-          grid              = averaged_scalar_profile,                         &
-          intermediate_grid = averaged_scalar_profile                          &
-       )
-       output_var_table(55) % to_be_processed = ls_forcing_variables_required
-
-
-       output_var_table(56) = init_nc_var(                                     &
-          name              = 'internal_density_centre',                              &
-          std_name          = "",                                              &
-          long_name         = "",                                              &
-          units             = "",                                              &
-          kind              = "internal profile",                              &
-          input_id          = 4,                                               &
-          output_file       = output_file,                                     &
-          grid              = averaged_scalar_profile,                         &
-          intermediate_grid = averaged_scalar_profile                          &
-       )
-       output_var_table(56) % averaging_grid => averaged_scalar_profile
-
-
-       output_var_table(57) = init_nc_var(                                     &
-          name              = 'internal_density_north',                       &
-          std_name          = "",                                              &
-          long_name         = "",                                              &
-          units             = "",                                              &
-          kind              = "internal profile",                              &
-          input_id          = 4,                                               &
-          output_file       = output_file,                                     &
-          grid              = north_averaged_scalar_profile,                   &
-          intermediate_grid = north_averaged_scalar_profile                    &
-       )
-       output_var_table(57) % averaging_grid => north_averaged_scalar_profile
-       output_var_table(57) % to_be_processed = .NOT. cfg % ug_defined_by_user
-
-
-       output_var_table(58) = init_nc_var(                                     &
-          name              = 'internal_density_south',                       &
-          std_name          = "",                                              &
-          long_name         = "",                                              &
-          units             = "",                                              &
-          kind              = "internal profile",                              &
-          input_id          = 4,                                               &
-          output_file       = output_file,                                     &
-          grid              = south_averaged_scalar_profile,                   &
-          intermediate_grid = south_averaged_scalar_profile                    &
-       )
-       output_var_table(58) % averaging_grid => south_averaged_scalar_profile
-       output_var_table(58) % to_be_processed = .NOT. cfg % ug_defined_by_user
-
-
-       output_var_table(59) = init_nc_var(                                     &
-          name              = 'internal_density_east',                        &
-          std_name          = "",                                              &
-          long_name         = "",                                              &
-          units             = "",                                              &
-          kind              = "internal profile",                              &
-          input_id          = 4,                                               &
-          output_file       = output_file,                                     &
-          grid              = east_averaged_scalar_profile,                    &
-          intermediate_grid = east_averaged_scalar_profile                     &
-       )
-       output_var_table(59) % averaging_grid => east_averaged_scalar_profile
-       output_var_table(59) % to_be_processed = .NOT. cfg % ug_defined_by_user
-
-
-       output_var_table(60) = init_nc_var(                                     &
-          name              = 'internal_density_west',                        &
-          std_name          = "",                                              &
-          long_name         = "",                                              &
-          units             = "",                                              &
-          kind              = "internal profile",                              &
-          input_id          = 4,                                               &
-          output_file       = output_file,                                     &
-          grid              = west_averaged_scalar_profile,                    &
-          intermediate_grid = west_averaged_scalar_profile                     &
-       )
-       output_var_table(60) % averaging_grid => west_averaged_scalar_profile
-       output_var_table(60) % to_be_processed = .NOT. cfg % ug_defined_by_user
-
-       output_var_table(61) = init_nc_var(                                     &
-          name              = 'internal_pressure_north',                       &
-          std_name          = "",                                              &
-          long_name         = "",                                              &
-          units             = "",                                              &
-          kind              = "internal profile",                              &
-          input_id          = 2,                                               &
-          output_file       = output_file,                                     &
-          grid              = north_averaged_scalar_profile,                   &
-          intermediate_grid = north_averaged_scalar_profile                    &
-       )
-       output_var_table(61) % averaging_grid => north_averaged_scalar_profile
-       output_var_table(61) % to_be_processed = .NOT. cfg % ug_defined_by_user
-
-
-       output_var_table(62) = init_nc_var(                                     &
-          name              = 'internal_pressure_south',                       &
-          std_name          = "",                                              &
-          long_name         = "",                                              &
-          units             = "",                                              &
-          kind              = "internal profile",                              &
-          input_id          = 2,                                               &
-          output_file       = output_file,                                     &
-          grid              = south_averaged_scalar_profile,                   &
-          intermediate_grid = south_averaged_scalar_profile                    &
-       )
-       output_var_table(62) % averaging_grid => south_averaged_scalar_profile
-       output_var_table(62) % to_be_processed = .NOT. cfg % ug_defined_by_user
-
-
-       output_var_table(63) = init_nc_var(                                     &
-          name              = 'internal_pressure_east',                        &
-          std_name          = "",                                              &
-          long_name         = "",                                              &
-          units             = "",                                              &
-          kind              = "internal profile",                              &
-          input_id          = 2,                                               &
-          output_file       = output_file,                                     &
-          grid              = east_averaged_scalar_profile,                    &
-          intermediate_grid = east_averaged_scalar_profile                     &
-       )
-       output_var_table(63) % averaging_grid => east_averaged_scalar_profile
-       output_var_table(63) % to_be_processed = .NOT. cfg % ug_defined_by_user
-
-
-       output_var_table(64) = init_nc_var(                                     &
-          name              = 'internal_pressure_west',                        &
-          std_name          = "",                                              &
-          long_name         = "",                                              &
-          units             = "",                                              &
-          kind              = "internal profile",                              &
-          input_id          = 2,                                               &
-          output_file       = output_file,                                     &
-          grid              = west_averaged_scalar_profile,                    &
-          intermediate_grid = west_averaged_scalar_profile                     &
-       )
-       output_var_table(64) % averaging_grid => west_averaged_scalar_profile
-       output_var_table(64) % to_be_processed = .NOT. cfg % ug_defined_by_user
-
-!
-!--    Attributes shared among all variables
-       output_var_table(:) % source = nc_source_text
-
-
-    END SUBROUTINE setup_variable_tables
+    output_var_table(42) = init_nc_var(                                     &
+       name              = 'surface_forcing_surface_pressure',              &
+       std_name          = "",                                              &
+       long_name         = "surface pressure",                              &
+       units             = "Pa",                                            &
+       kind              = "time series",                                   &
+       input_id          = 2,                                               & ! second in (T, p) I/O group
+       output_file       = output_file,                                     &
+       grid              = palm_grid,                                       &
+       intermediate_grid = palm_intermediate                                &
+    )
+    output_var_table(42)%averaging_grid => averaged_scalar_profile
+
+    output_var_table(43) = init_nc_var(                                     &
+       name              = 'ls_forcing_ug',                                 &
+       std_name          = "",                                              &
+       long_name         = "geostrophic wind (u component)",                &
+       units             = "m/s",                                           &
+       kind              = "geostrophic",                                   &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = averaged_scalar_profile,                         &
+       intermediate_grid = averaged_scalar_profile                          &
+    )
+
+    output_var_table(44) = init_nc_var(                                     &
+       name              = 'ls_forcing_vg',                                 &
+       std_name          = "",                                              &
+       long_name         = "geostrophic wind (v component)",                &
+       units             = "m/s",                                           &
+       kind              = "geostrophic",                                   &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = averaged_scalar_profile,                         &
+       intermediate_grid = averaged_scalar_profile                          &
+    )
+
+    output_var_table(45) = init_nc_var(                                     &
+       name              = 'nudging_u',                                     &
+       std_name          = "",                                              &
+       long_name         = "wind component in x direction",                 &
+       units             = "m/s",                                           &
+       kind              = "geostrophic",                                   &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = averaged_scalar_profile,                         &
+       intermediate_grid = averaged_scalar_profile                          &
+    )
+    output_var_table(45)%to_be_processed = ls_forcing_variables_required
+
+    output_var_table(46) = init_nc_var(                                     &
+       name              = 'nudging_v',                                     &
+       std_name          = "",                                              &
+       long_name         = "wind component in y direction",                 &
+       units             = "m/s",                                           &
+       kind              = "large-scale scalar forcing",                    &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = averaged_scalar_profile,                         &
+       intermediate_grid = averaged_scalar_profile                          &
+    )
+    output_var_table(46)%to_be_processed = ls_forcing_variables_required
+
+    output_var_table(47) = init_nc_var(                                     &
+       name              = 'ls_forcing_sub_w',                              &
+       std_name          = "",                                              &
+       long_name         = "subsidence velocity of w",                      &
+       units             = "m/s",                                           &
+       kind              = "large-scale w forcing",                         &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = averaged_scalar_profile,                         &
+       intermediate_grid = averaged_scalar_profile                          &
+    )
+    output_var_table(47)%to_be_processed = ls_forcing_variables_required
+
+    output_var_table(48) = init_nc_var(                                     &
+       name              = 'nudging_w',                                     &
+       std_name          = "",                                              &
+       long_name         = "wind component in w direction",                 &
+       units             = "m/s",                                           &
+       kind              = "large-scale w forcing",                         &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = averaged_w_profile,                              &
+       intermediate_grid = averaged_w_profile                               &
+    )
+    output_var_table(48)%to_be_processed = ls_forcing_variables_required
+
+
+    output_var_table(49) = init_nc_var(                                     &
+       name              = 'ls_forcing_adv_pt',                             &
+       std_name          = "",                                              &
+       long_name         = "advection of potential temperature",            &
+       units             = "K/s",                                           &
+       kind              = "large-scale scalar forcing",                    &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = averaged_scalar_profile,                         &
+       intermediate_grid = averaged_scalar_profile                          &
+    )
+    output_var_table(49)%to_be_processed = ls_forcing_variables_required
+
+    output_var_table(50) = init_nc_var(                                     &
+       name              = 'ls_forcing_sub_pt',                             &
+       std_name          = "",                                              &
+       long_name         = "subsidence velocity of potential temperature",  &
+       units             = "K/s",                                           &
+       kind              = "large-scale scalar forcing",                    &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = averaged_scalar_profile,                         &
+       intermediate_grid = averaged_scalar_profile                          &
+    )
+    output_var_table(50)%to_be_processed = ls_forcing_variables_required
+
+    output_var_table(51) = init_nc_var(                                     &
+       name              = 'nudging_pt',                                    &
+       std_name          = "",                                              &
+       long_name         = "potential temperature",                         &
+       units             = "K",                                             &
+       kind              = "large-scale scalar forcing",                    &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = averaged_scalar_profile,                         &
+       intermediate_grid = averaged_scalar_profile                          &
+    )
+    output_var_table(51)%to_be_processed = ls_forcing_variables_required
+
+    output_var_table(52) = init_nc_var(                                     &
+       name              = 'ls_forcing_adv_qv',                             &
+       std_name          = "",                                              &
+       long_name         = "advection of specific humidity",                &
+       units             = "kg/kg/s",                                       &
+       kind              = "large-scale scalar forcing",                    &
+       input_id          = 3,                                               &
+       output_file       = output_file,                                     &
+       grid              = averaged_scalar_profile,                         &
+       intermediate_grid = averaged_scalar_profile                          &
+    )
+    output_var_table(52)%to_be_processed = ls_forcing_variables_required
+
+
+    output_var_table(53) = init_nc_var(                                     &
+       name              = 'ls_forcing_sub_qv',                             &
+       std_name          = "",                                              &
+       long_name         = "subsidence velocity of specific humidity",      &
+       units             = "kg/kg/s",                                       &
+       kind              = "large-scale scalar forcing",                    &
+       input_id          = 3,                                               &
+       output_file       = output_file,                                     &
+       grid              = averaged_scalar_profile,                         &
+       intermediate_grid = averaged_scalar_profile                          &
+    )
+    output_var_table(53)%to_be_processed = ls_forcing_variables_required
+
+    output_var_table(54) = init_nc_var(                                     &
+       name              = 'nudging_qv',                                    &
+       std_name          = "",                                              &
+       long_name         = "specific humidity",                             &
+       units             = "kg/kg",                                         &
+       kind              = "large-scale scalar forcing",                    &
+       input_id          = 3,                                               &
+       output_file       = output_file,                                     &
+       grid              = averaged_scalar_profile,                         &
+       intermediate_grid = averaged_scalar_profile                          &
+    )
+    output_var_table(54)%to_be_processed = ls_forcing_variables_required
+
+    output_var_table(55) = init_nc_var(                                     &
+       name              = 'nudging_tau',                                   &
+       std_name          = "",                                              &
+       long_name         = "nudging relaxation time scale",                 &
+       units             = "s",                                             &
+       kind              = "constant scalar profile",                       &
+       input_id          = 1,                                               &
+       output_file       = output_file,                                     &
+       grid              = averaged_scalar_profile,                         &
+       intermediate_grid = averaged_scalar_profile                          &
+    )
+    output_var_table(55)%to_be_processed = ls_forcing_variables_required
+
+
+    output_var_table(56) = init_nc_var(                                     &
+       name              = 'internal_density_centre',                              &
+       std_name          = "",                                              &
+       long_name         = "",                                              &
+       units             = "",                                              &
+       kind              = "internal profile",                              &
+       input_id          = 4,                                               &
+       output_file       = output_file,                                     &
+       grid              = averaged_scalar_profile,                         &
+       intermediate_grid = averaged_scalar_profile                          &
+    )
+    output_var_table(56)%averaging_grid => averaged_scalar_profile
+
+
+    output_var_table(57) = init_nc_var(                                     &
+       name              = 'internal_density_north',                       &
+       std_name          = "",                                              &
+       long_name         = "",                                              &
+       units             = "",                                              &
+       kind              = "internal profile",                              &
+       input_id          = 4,                                               &
+       output_file       = output_file,                                     &
+       grid              = north_averaged_scalar_profile,                   &
+       intermediate_grid = north_averaged_scalar_profile                    &
+    )
+    output_var_table(57)%averaging_grid => north_averaged_scalar_profile
+    output_var_table(57)%to_be_processed = .NOT. cfg%ug_defined_by_user
+
+
+    output_var_table(58) = init_nc_var(                                     &
+       name              = 'internal_density_south',                       &
+       std_name          = "",                                              &
+       long_name         = "",                                              &
+       units             = "",                                              &
+       kind              = "internal profile",                              &
+       input_id          = 4,                                               &
+       output_file       = output_file,                                     &
+       grid              = south_averaged_scalar_profile,                   &
+       intermediate_grid = south_averaged_scalar_profile                    &
+    )
+    output_var_table(58)%averaging_grid => south_averaged_scalar_profile
+    output_var_table(58)%to_be_processed = .NOT. cfg%ug_defined_by_user
+
+
+    output_var_table(59) = init_nc_var(                                     &
+       name              = 'internal_density_east',                        &
+       std_name          = "",                                              &
+       long_name         = "",                                              &
+       units             = "",                                              &
+       kind              = "internal profile",                              &
+       input_id          = 4,                                               &
+       output_file       = output_file,                                     &
+       grid              = east_averaged_scalar_profile,                    &
+       intermediate_grid = east_averaged_scalar_profile                     &
+    )
+    output_var_table(59)%averaging_grid => east_averaged_scalar_profile
+    output_var_table(59)%to_be_processed = .NOT. cfg%ug_defined_by_user
+
+
+    output_var_table(60) = init_nc_var(                                     &
+       name              = 'internal_density_west',                        &
+       std_name          = "",                                              &
+       long_name         = "",                                              &
+       units             = "",                                              &
+       kind              = "internal profile",                              &
+       input_id          = 4,                                               &
+       output_file       = output_file,                                     &
+       grid              = west_averaged_scalar_profile,                    &
+       intermediate_grid = west_averaged_scalar_profile                     &
+    )
+    output_var_table(60)%averaging_grid => west_averaged_scalar_profile
+    output_var_table(60)%to_be_processed = .NOT. cfg%ug_defined_by_user
+
+    output_var_table(61) = init_nc_var(                                     &
+       name              = 'internal_pressure_north',                       &
+       std_name          = "",                                              &
+       long_name         = "",                                              &
+       units             = "",                                              &
+       kind              = "internal profile",                              &
+       input_id          = 2,                                               &
+       output_file       = output_file,                                     &
+       grid              = north_averaged_scalar_profile,                   &
+       intermediate_grid = north_averaged_scalar_profile                    &
+    )
+    output_var_table(61)%averaging_grid => north_averaged_scalar_profile
+    output_var_table(61)%to_be_processed = .NOT. cfg%ug_defined_by_user
+
+
+    output_var_table(62) = init_nc_var(                                     &
+       name              = 'internal_pressure_south',                       &
+       std_name          = "",                                              &
+       long_name         = "",                                              &
+       units             = "",                                              &
+       kind              = "internal profile",                              &
+       input_id          = 2,                                               &
+       output_file       = output_file,                                     &
+       grid              = south_averaged_scalar_profile,                   &
+       intermediate_grid = south_averaged_scalar_profile                    &
+    )
+    output_var_table(62)%averaging_grid => south_averaged_scalar_profile
+    output_var_table(62)%to_be_processed = .NOT. cfg%ug_defined_by_user
+
+
+    output_var_table(63) = init_nc_var(                                     &
+       name              = 'internal_pressure_east',                        &
+       std_name          = "",                                              &
+       long_name         = "",                                              &
+       units             = "",                                              &
+       kind              = "internal profile",                              &
+       input_id          = 2,                                               &
+       output_file       = output_file,                                     &
+       grid              = east_averaged_scalar_profile,                    &
+       intermediate_grid = east_averaged_scalar_profile                     &
+    )
+    output_var_table(63)%averaging_grid => east_averaged_scalar_profile
+    output_var_table(63)%to_be_processed = .NOT. cfg%ug_defined_by_user
+
+
+    output_var_table(64) = init_nc_var(                                     &
+       name              = 'internal_pressure_west',                        &
+       std_name          = "",                                              &
+       long_name         = "",                                              &
+       units             = "",                                              &
+       kind              = "internal profile",                              &
+       input_id          = 2,                                               &
+       output_file       = output_file,                                     &
+       grid              = west_averaged_scalar_profile,                    &
+       intermediate_grid = west_averaged_scalar_profile                     &
+    )
+    output_var_table(64)%averaging_grid => west_averaged_scalar_profile
+    output_var_table(64)%to_be_processed = .NOT. cfg%ug_defined_by_user
+
+!
+!-- Attributes shared among all variables
+    output_var_table(:)%source = nc_source_text
+
+
+ END SUBROUTINE setup_variable_tables
 
 
@@ -3291,34 +3310,34 @@
 !> 'lod', as defined by the PALM-4U input data standard.
 !------------------------------------------------------------------------------!
-    FUNCTION init_nc_var(name, std_name, long_name, units, kind, input_id,     &
-                         grid, intermediate_grid, output_file, is_profile)     &
-       RESULT(var)
-
-       CHARACTER(LEN=*), INTENT(IN)      ::  name, std_name, long_name, units, kind
-       INTEGER, INTENT(IN)               ::  input_id
-       TYPE(grid_definition), INTENT(IN), TARGET ::  grid, intermediate_grid
-       TYPE(nc_file), INTENT(IN)         ::  output_file
-       LOGICAL, INTENT(IN), OPTIONAL     ::  is_profile
-
-       CHARACTER(LEN=LNAME)              ::  out_var_kind 
-       TYPE(nc_var)                      ::  var
-
-       out_var_kind = TRIM(kind)
-
-       IF (PRESENT(is_profile))  THEN
-          IF (is_profile)  out_var_kind = TRIM(kind) // ' profile'
-       ENDIF
-
-       var % name              = name
-       var % standard_name     = std_name
-       var % long_name         = long_name
-       var % units             = units
-       var % kind              = TRIM(out_var_kind)
-       var % input_id          = input_id
-       var % nt                = SIZE (output_file % time)
-       var % grid              => grid
-       var % intermediate_grid => intermediate_grid
-
-       SELECT CASE( TRIM(out_var_kind) )
+ FUNCTION init_nc_var(name, std_name, long_name, units, kind, input_id,     &
+                      grid, intermediate_grid, output_file, is_profile)     &
+    RESULT(var)
+
+    CHARACTER(LEN=*), INTENT(IN)      ::  name, std_name, long_name, units, kind
+    INTEGER, INTENT(IN)               ::  input_id
+    TYPE(grid_definition), INTENT(IN), TARGET ::  grid, intermediate_grid
+    TYPE(nc_file), INTENT(IN)         ::  output_file
+    LOGICAL, INTENT(IN), OPTIONAL     ::  is_profile
+
+    CHARACTER(LEN=LNAME)              ::  out_var_kind 
+    TYPE(nc_var)                      ::  var
+
+    out_var_kind = TRIM(kind)
+
+    IF (PRESENT(is_profile))  THEN
+       IF (is_profile)  out_var_kind = TRIM(kind) // ' profile'
+    ENDIF
+
+    var%name              = name
+    var%standard_name     = std_name
+    var%long_name         = long_name
+    var%units             = units
+    var%kind              = TRIM(out_var_kind)
+    var%input_id          = input_id
+    var%nt                = SIZE (output_file%time)
+    var%grid              => grid
+    var%intermediate_grid => intermediate_grid
+
+    SELECT CASE( TRIM(out_var_kind) )
 
 !
@@ -3327,302 +3346,302 @@
 !--    TODO: and pass into init_nc_var.
        CASE( 'init soil' )
-          var % nt              = 1
-          var % lod             = 2
-          var % ndim            = 3
-          var % dimids(1:3)     = output_file % dimids_soil
-          var % dimvarids(1:3)  = output_file % dimvarids_soil
-          var % to_be_processed = init_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "interpolate_2d"
+          var%nt              = 1
+          var%lod             = 2
+          var%ndim            = 3
+          var%dimids(1:3)     = output_file%dimids_soil
+          var%dimvarids(1:3)  = output_file%dimvarids_soil
+          var%to_be_processed = init_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "interpolate_2d"
 
        CASE( 'init scalar' )
-          var % nt              = 1
-          var % lod             = 2
-          var % ndim            = 3
-          var % dimids(1:3)     = output_file % dimids_scl
-          var % dimvarids(1:3)  = output_file % dimvarids_scl
-          var % to_be_processed = init_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "interpolate_3d"
+          var%nt              = 1
+          var%lod             = 2
+          var%ndim            = 3
+          var%dimids(1:3)     = output_file%dimids_scl
+          var%dimvarids(1:3)  = output_file%dimvarids_scl
+          var%to_be_processed = init_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "interpolate_3d"
 
        CASE( 'init u' )
-          var % nt              = 1
-          var % lod             = 2
-          var % ndim            = 3
-          var % dimids(1)       = output_file % dimids_vel(1)
-          var % dimids(2)       = output_file % dimids_scl(2)
-          var % dimids(3)       = output_file % dimids_scl(3)
-          var % dimvarids(1)    = output_file % dimvarids_vel(1)
-          var % dimvarids(2)    = output_file % dimvarids_scl(2)
-          var % dimvarids(3)    = output_file % dimvarids_scl(3)
-          var % to_be_processed = init_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "interpolate_3d"
+          var%nt              = 1
+          var%lod             = 2
+          var%ndim            = 3
+          var%dimids(1)       = output_file%dimids_vel(1)
+          var%dimids(2)       = output_file%dimids_scl(2)
+          var%dimids(3)       = output_file%dimids_scl(3)
+          var%dimvarids(1)    = output_file%dimvarids_vel(1)
+          var%dimvarids(2)    = output_file%dimvarids_scl(2)
+          var%dimvarids(3)    = output_file%dimvarids_scl(3)
+          var%to_be_processed = init_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "interpolate_3d"
 
        CASE( 'init v' )
-          var % nt              = 1
-          var % lod             = 2
-          var % ndim            = 3
-          var % dimids(1)       = output_file % dimids_scl(1)
-          var % dimids(2)       = output_file % dimids_vel(2)
-          var % dimids(3)       = output_file % dimids_scl(3)
-          var % dimvarids(1)    = output_file % dimvarids_scl(1)
-          var % dimvarids(2)    = output_file % dimvarids_vel(2)
-          var % dimvarids(3)    = output_file % dimvarids_scl(3)
-          var % to_be_processed = init_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "interpolate_3d"
+          var%nt              = 1
+          var%lod             = 2
+          var%ndim            = 3
+          var%dimids(1)       = output_file%dimids_scl(1)
+          var%dimids(2)       = output_file%dimids_vel(2)
+          var%dimids(3)       = output_file%dimids_scl(3)
+          var%dimvarids(1)    = output_file%dimvarids_scl(1)
+          var%dimvarids(2)    = output_file%dimvarids_vel(2)
+          var%dimvarids(3)    = output_file%dimvarids_scl(3)
+          var%to_be_processed = init_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "interpolate_3d"
 
        CASE( 'init w' )
-          var % nt              = 1
-          var % lod             = 2
-          var % ndim            = 3
-          var % dimids(1)       = output_file % dimids_scl(1)
-          var % dimids(2)       = output_file % dimids_scl(2)
-          var % dimids(3)       = output_file % dimids_vel(3)
-          var % dimvarids(1)    = output_file % dimvarids_scl(1)
-          var % dimvarids(2)    = output_file % dimvarids_scl(2)
-          var % dimvarids(3)    = output_file % dimvarids_vel(3)
-          var % to_be_processed = init_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "interpolate_3d"
+          var%nt              = 1
+          var%lod             = 2
+          var%ndim            = 3
+          var%dimids(1)       = output_file%dimids_scl(1)
+          var%dimids(2)       = output_file%dimids_scl(2)
+          var%dimids(3)       = output_file%dimids_vel(3)
+          var%dimvarids(1)    = output_file%dimvarids_scl(1)
+          var%dimvarids(2)    = output_file%dimvarids_scl(2)
+          var%dimvarids(3)    = output_file%dimvarids_vel(3)
+          var%to_be_processed = init_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "interpolate_3d"
 
        CASE( 'init scalar profile', 'init u profile', 'init v profile')
-          var % nt              = 1
-          var % lod             = 1
-          var % ndim            = 1
-          var % dimids(1)       = output_file % dimids_scl(3)    !z
-          var % dimvarids(1)    = output_file % dimvarids_scl(3) !z
-          var % to_be_processed = init_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "average profile"
+          var%nt              = 1
+          var%lod             = 1
+          var%ndim            = 1
+          var%dimids(1)       = output_file%dimids_scl(3)    !z
+          var%dimvarids(1)    = output_file%dimvarids_scl(3) !z
+          var%to_be_processed = init_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "average profile"
 
        CASE( 'init w profile')
-          var % nt              = 1
-          var % lod             = 1
-          var % ndim            = 1
-          var % dimids(1)       = output_file % dimids_vel(3)    !z
-          var % dimvarids(1)    = output_file % dimvarids_vel(3) !z
-          var % to_be_processed = init_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "average profile"
+          var%nt              = 1
+          var%lod             = 1
+          var%ndim            = 1
+          var%dimids(1)       = output_file%dimids_vel(3)    !z
+          var%dimvarids(1)    = output_file%dimvarids_vel(3) !z
+          var%to_be_processed = init_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "average profile"
 
        CASE( 'surface forcing' )
-          var % lod             = -1
-          var % ndim            = 3
-          var % dimids(3)       = output_file % dimid_time
-          var % dimids(1:2)     = output_file % dimids_soil(1:2)
-          var % dimvarids(3)    = output_file % dimvarid_time
-          var % dimvarids(1:2)  = output_file % dimvarids_soil(1:2)
-          var % to_be_processed = surface_forcing_required
-          var % is_internal     = .FALSE.
-          var % task            = "interpolate_2d"
+          var%lod             = -1
+          var%ndim            = 3
+          var%dimids(3)       = output_file%dimid_time
+          var%dimids(1:2)     = output_file%dimids_soil(1:2)
+          var%dimvarids(3)    = output_file%dimvarid_time
+          var%dimvarids(1:2)  = output_file%dimvarids_soil(1:2)
+          var%to_be_processed = surface_forcing_required
+          var%is_internal     = .FALSE.
+          var%task            = "interpolate_2d"
 
        CASE( 'left scalar', 'right scalar')
-          var % lod             = -1
-          var % ndim            = 3
-          var % dimids(3)       = output_file % dimid_time
-          var % dimids(1)       = output_file % dimids_scl(2)
-          var % dimids(2)       = output_file % dimids_scl(3)
-          var % dimvarids(3)    = output_file % dimvarid_time
-          var % dimvarids(1)    = output_file % dimvarids_scl(2)
-          var % dimvarids(2)    = output_file % dimvarids_scl(3)
-          var % to_be_processed = boundary_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "interpolate_3d"
+          var%lod             = -1
+          var%ndim            = 3
+          var%dimids(3)       = output_file%dimid_time
+          var%dimids(1)       = output_file%dimids_scl(2)
+          var%dimids(2)       = output_file%dimids_scl(3)
+          var%dimvarids(3)    = output_file%dimvarid_time
+          var%dimvarids(1)    = output_file%dimvarids_scl(2)
+          var%dimvarids(2)    = output_file%dimvarids_scl(3)
+          var%to_be_processed = boundary_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "interpolate_3d"
 
        CASE( 'north scalar', 'south scalar')
-          var % lod             = -1
-          var % ndim            = 3
-          var % dimids(3)       = output_file % dimid_time
-          var % dimids(1)       = output_file % dimids_scl(1)
-          var % dimids(2)       = output_file % dimids_scl(3)
-          var % dimvarids(3)    = output_file % dimvarid_time
-          var % dimvarids(1)    = output_file % dimvarids_scl(1)
-          var % dimvarids(2)    = output_file % dimvarids_scl(3)
-          var % to_be_processed = boundary_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "interpolate_3d"
+          var%lod             = -1
+          var%ndim            = 3
+          var%dimids(3)       = output_file%dimid_time
+          var%dimids(1)       = output_file%dimids_scl(1)
+          var%dimids(2)       = output_file%dimids_scl(3)
+          var%dimvarids(3)    = output_file%dimvarid_time
+          var%dimvarids(1)    = output_file%dimvarids_scl(1)
+          var%dimvarids(2)    = output_file%dimvarids_scl(3)
+          var%to_be_processed = boundary_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "interpolate_3d"
 
        CASE( 'top scalar', 'top w' )
-          var % lod             = -1
-          var % ndim            = 3
-          var % dimids(3)       = output_file % dimid_time
-          var % dimids(1)       = output_file % dimids_scl(1)
-          var % dimids(2)       = output_file % dimids_scl(2)
-          var % dimvarids(3)    = output_file % dimvarid_time
-          var % dimvarids(1)    = output_file % dimvarids_scl(1)
-          var % dimvarids(2)    = output_file % dimvarids_scl(2)
-          var % to_be_processed = boundary_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "interpolate_3d"
+          var%lod             = -1
+          var%ndim            = 3
+          var%dimids(3)       = output_file%dimid_time
+          var%dimids(1)       = output_file%dimids_scl(1)
+          var%dimids(2)       = output_file%dimids_scl(2)
+          var%dimvarids(3)    = output_file%dimvarid_time
+          var%dimvarids(1)    = output_file%dimvarids_scl(1)
+          var%dimvarids(2)    = output_file%dimvarids_scl(2)
+          var%to_be_processed = boundary_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "interpolate_3d"
 
        CASE( 'left u', 'right u' )
-          var % lod             = -1
-          var % ndim            = 3
-          var % dimids(3)       = output_file % dimid_time
-          var % dimids(1)       = output_file % dimids_scl(2)
-          var % dimids(2)       = output_file % dimids_scl(3)
-          var % dimvarids(3)    = output_file % dimvarid_time
-          var % dimvarids(1)    = output_file % dimvarids_scl(2)
-          var % dimvarids(2)    = output_file % dimvarids_scl(3)
-          var % to_be_processed = boundary_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "interpolate_3d"
+          var%lod             = -1
+          var%ndim            = 3
+          var%dimids(3)       = output_file%dimid_time
+          var%dimids(1)       = output_file%dimids_scl(2)
+          var%dimids(2)       = output_file%dimids_scl(3)
+          var%dimvarids(3)    = output_file%dimvarid_time
+          var%dimvarids(1)    = output_file%dimvarids_scl(2)
+          var%dimvarids(2)    = output_file%dimvarids_scl(3)
+          var%to_be_processed = boundary_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "interpolate_3d"
 
        CASE( 'north u', 'south u' )
-          var % lod             = -1
-          var % ndim            = 3
-          var % dimids(3)       = output_file % dimid_time    !t
-          var % dimids(1)       = output_file % dimids_vel(1) !x
-          var % dimids(2)       = output_file % dimids_scl(3) !z
-          var % dimvarids(3)    = output_file % dimvarid_time
-          var % dimvarids(1)    = output_file % dimvarids_vel(1)
-          var % dimvarids(2)    = output_file % dimvarids_scl(3)
-          var % to_be_processed = boundary_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "interpolate_3d"
+          var%lod             = -1
+          var%ndim            = 3
+          var%dimids(3)       = output_file%dimid_time    !t
+          var%dimids(1)       = output_file%dimids_vel(1) !x
+          var%dimids(2)       = output_file%dimids_scl(3) !z
+          var%dimvarids(3)    = output_file%dimvarid_time
+          var%dimvarids(1)    = output_file%dimvarids_vel(1)
+          var%dimvarids(2)    = output_file%dimvarids_scl(3)
+          var%to_be_processed = boundary_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "interpolate_3d"
 
        CASE( 'top u' )
-          var % lod             = -1
-          var % ndim            = 3
-          var % dimids(3)       = output_file % dimid_time    !t
-          var % dimids(1)       = output_file % dimids_vel(1) !x
-          var % dimids(2)       = output_file % dimids_scl(2) !z
-          var % dimvarids(3)    = output_file % dimvarid_time
-          var % dimvarids(1)    = output_file % dimvarids_vel(1)
-          var % dimvarids(2)    = output_file % dimvarids_scl(2)
-          var % to_be_processed = boundary_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "interpolate_3d"
+          var%lod             = -1
+          var%ndim            = 3
+          var%dimids(3)       = output_file%dimid_time    !t
+          var%dimids(1)       = output_file%dimids_vel(1) !x
+          var%dimids(2)       = output_file%dimids_scl(2) !z
+          var%dimvarids(3)    = output_file%dimvarid_time
+          var%dimvarids(1)    = output_file%dimvarids_vel(1)
+          var%dimvarids(2)    = output_file%dimvarids_scl(2)
+          var%to_be_processed = boundary_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "interpolate_3d"
 
        CASE( 'left v', 'right v' )
-          var % lod             = -1
-          var % ndim            = 3
-          var % dimids(3)       = output_file % dimid_time
-          var % dimids(1)       = output_file % dimids_vel(2)
-          var % dimids(2)       = output_file % dimids_scl(3)
-          var % dimvarids(3)    = output_file % dimvarid_time
-          var % dimvarids(1)    = output_file % dimvarids_vel(2)
-          var % dimvarids(2)    = output_file % dimvarids_scl(3)
-          var % to_be_processed = boundary_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "interpolate_3d"
+          var%lod             = -1
+          var%ndim            = 3
+          var%dimids(3)       = output_file%dimid_time
+          var%dimids(1)       = output_file%dimids_vel(2)
+          var%dimids(2)       = output_file%dimids_scl(3)
+          var%dimvarids(3)    = output_file%dimvarid_time
+          var%dimvarids(1)    = output_file%dimvarids_vel(2)
+          var%dimvarids(2)    = output_file%dimvarids_scl(3)
+          var%to_be_processed = boundary_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "interpolate_3d"
 
        CASE( 'north v', 'south v' )
-          var % lod             = -1
-          var % ndim            = 3
-          var % dimids(3)       = output_file % dimid_time    !t
-          var % dimids(1)       = output_file % dimids_scl(1) !x
-          var % dimids(2)       = output_file % dimids_scl(3) !z
-          var % dimvarids(3)    = output_file % dimvarid_time
-          var % dimvarids(1)    = output_file % dimvarids_scl(1)
-          var % dimvarids(2)    = output_file % dimvarids_scl(3)
-          var % to_be_processed = boundary_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "interpolate_3d"
+          var%lod             = -1
+          var%ndim            = 3
+          var%dimids(3)       = output_file%dimid_time    !t
+          var%dimids(1)       = output_file%dimids_scl(1) !x
+          var%dimids(2)       = output_file%dimids_scl(3) !z
+          var%dimvarids(3)    = output_file%dimvarid_time
+          var%dimvarids(1)    = output_file%dimvarids_scl(1)
+          var%dimvarids(2)    = output_file%dimvarids_scl(3)
+          var%to_be_processed = boundary_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "interpolate_3d"
 
        CASE( 'top v' )
-          var % lod             = -1
-          var % ndim            = 3
-          var % dimids(3)       = output_file % dimid_time    !t
-          var % dimids(1)       = output_file % dimids_scl(1) !x
-          var % dimids(2)       = output_file % dimids_vel(2) !z
-          var % dimvarids(3)    = output_file % dimvarid_time
-          var % dimvarids(1)    = output_file % dimvarids_scl(1)
-          var % dimvarids(2)    = output_file % dimvarids_vel(2)
-          var % to_be_processed = boundary_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "interpolate_3d"
+          var%lod             = -1
+          var%ndim            = 3
+          var%dimids(3)       = output_file%dimid_time    !t
+          var%dimids(1)       = output_file%dimids_scl(1) !x
+          var%dimids(2)       = output_file%dimids_vel(2) !z
+          var%dimvarids(3)    = output_file%dimvarid_time
+          var%dimvarids(1)    = output_file%dimvarids_scl(1)
+          var%dimvarids(2)    = output_file%dimvarids_vel(2)
+          var%to_be_processed = boundary_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "interpolate_3d"
 
        CASE( 'left w', 'right w')
-          var % lod             = -1
-          var % ndim            = 3
-          var % dimids(3)       = output_file % dimid_time
-          var % dimids(1)       = output_file % dimids_scl(2)
-          var % dimids(2)       = output_file % dimids_vel(3)
-          var % dimvarids(3)    = output_file % dimvarid_time
-          var % dimvarids(1)    = output_file % dimvarids_scl(2)
-          var % dimvarids(2)    = output_file % dimvarids_vel(3)
-          var % to_be_processed = boundary_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "interpolate_3d"
+          var%lod             = -1
+          var%ndim            = 3
+          var%dimids(3)       = output_file%dimid_time
+          var%dimids(1)       = output_file%dimids_scl(2)
+          var%dimids(2)       = output_file%dimids_vel(3)
+          var%dimvarids(3)    = output_file%dimvarid_time
+          var%dimvarids(1)    = output_file%dimvarids_scl(2)
+          var%dimvarids(2)    = output_file%dimvarids_vel(3)
+          var%to_be_processed = boundary_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "interpolate_3d"
 
        CASE( 'north w', 'south w' )
-          var % lod             = -1
-          var % ndim            = 3
-          var % dimids(3)       = output_file % dimid_time    !t
-          var % dimids(1)       = output_file % dimids_scl(1) !x
-          var % dimids(2)       = output_file % dimids_vel(3) !z
-          var % dimvarids(3)    = output_file % dimvarid_time
-          var % dimvarids(1)    = output_file % dimvarids_scl(1)
-          var % dimvarids(2)    = output_file % dimvarids_vel(3)
-          var % to_be_processed = boundary_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "interpolate_3d"
+          var%lod             = -1
+          var%ndim            = 3
+          var%dimids(3)       = output_file%dimid_time    !t
+          var%dimids(1)       = output_file%dimids_scl(1) !x
+          var%dimids(2)       = output_file%dimids_vel(3) !z
+          var%dimvarids(3)    = output_file%dimvarid_time
+          var%dimvarids(1)    = output_file%dimvarids_scl(1)
+          var%dimvarids(2)    = output_file%dimvarids_vel(3)
+          var%to_be_processed = boundary_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "interpolate_3d"
 
        CASE( 'time series' )
-          var % lod             = 0
-          var % ndim            = 1
-          var % dimids(1)       = output_file % dimid_time    !t
-          var % dimvarids(1)    = output_file % dimvarid_time
-          var % to_be_processed = .TRUE.
-          var % is_internal     = .FALSE.
-          var % task            = "average profile"
+          var%lod             = 0
+          var%ndim            = 1
+          var%dimids(1)       = output_file%dimid_time    !t
+          var%dimvarids(1)    = output_file%dimvarid_time
+          var%to_be_processed = .TRUE.
+          var%is_internal     = .FALSE.
+          var%task            = "average profile"
 
        CASE( 'constant scalar profile' )
-          var % lod             = -1
-          var % ndim            = 2
-          var % dimids(2)       = output_file % dimid_time    !t
-          var % dimids(1)       = output_file % dimids_scl(3) !z
-          var % dimvarids(2)    = output_file % dimvarid_time
-          var % dimvarids(1)    = output_file % dimvarids_scl(3)
-          var % to_be_processed = .TRUE.
-          var % is_internal     = .FALSE.
-          var % task            = "set profile"
+          var%lod             = -1
+          var%ndim            = 2
+          var%dimids(2)       = output_file%dimid_time    !t
+          var%dimids(1)       = output_file%dimids_scl(3) !z
+          var%dimvarids(2)    = output_file%dimvarid_time
+          var%dimvarids(1)    = output_file%dimvarids_scl(3)
+          var%to_be_processed = .TRUE.
+          var%is_internal     = .FALSE.
+          var%task            = "set profile"
 
        CASE( 'large-scale scalar forcing' )
-          var % lod             = -1
-          var % ndim            = 2
-          var % dimids(2)       = output_file % dimid_time    !t
-          var % dimids(1)       = output_file % dimids_scl(3) !z
-          var % dimvarids(2)    = output_file % dimvarid_time
-          var % dimvarids(1)    = output_file % dimvarids_scl(3)
-          var % to_be_processed = ls_forcing_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "average large-scale profile"
+          var%lod             = -1
+          var%ndim            = 2
+          var%dimids(2)       = output_file%dimid_time    !t
+          var%dimids(1)       = output_file%dimids_scl(3) !z
+          var%dimvarids(2)    = output_file%dimvarid_time
+          var%dimvarids(1)    = output_file%dimvarids_scl(3)
+          var%to_be_processed = ls_forcing_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "average large-scale profile"
 
        CASE( 'geostrophic' )
-          var % lod             = -1
-          var % ndim            = 2
-          var % dimids(2)       = output_file % dimid_time    !t
-          var % dimids(1)       = output_file % dimids_scl(3) !z
-          var % dimvarids(2)    = output_file % dimvarid_time
-          var % dimvarids(1)    = output_file % dimvarids_scl(3)
-          var % to_be_processed = .TRUE.
-          var % is_internal     = .FALSE.
-          var % task            = "geostrophic winds"
+          var%lod             = -1
+          var%ndim            = 2
+          var%dimids(2)       = output_file%dimid_time    !t
+          var%dimids(1)       = output_file%dimids_scl(3) !z
+          var%dimvarids(2)    = output_file%dimvarid_time
+          var%dimvarids(1)    = output_file%dimvarids_scl(3)
+          var%to_be_processed = .TRUE.
+          var%is_internal     = .FALSE.
+          var%task            = "geostrophic winds"
 
        CASE( 'large-scale w forcing' )
-          var % lod             = -1
-          var % ndim            = 2
-          var % dimids(2)       = output_file % dimid_time    !t
-          var % dimids(1)       = output_file % dimids_vel(3) !z
-          var % dimvarids(2)    = output_file % dimvarid_time
-          var % dimvarids(1)    = output_file % dimvarids_vel(3)
-          var % to_be_processed = ls_forcing_variables_required
-          var % is_internal     = .FALSE.
-          var % task            = "average large-scale profile"
+          var%lod             = -1
+          var%ndim            = 2
+          var%dimids(2)       = output_file%dimid_time    !t
+          var%dimids(1)       = output_file%dimids_vel(3) !z
+          var%dimvarids(2)    = output_file%dimvarid_time
+          var%dimvarids(1)    = output_file%dimvarids_vel(3)
+          var%to_be_processed = ls_forcing_variables_required
+          var%is_internal     = .FALSE.
+          var%task            = "average large-scale profile"
 
        CASE( 'internal profile' )
-          var % lod             = -1
-          var % ndim            = 2
-          var % dimids(2)       = output_file % dimid_time    !t
-          var % dimids(1)       = output_file % dimids_scl(3) !z
-          var % dimvarids(2)    = output_file % dimvarid_time
-          var % dimvarids(1)    = output_file % dimvarids_scl(3)
-          var % to_be_processed = .TRUE.
-          var % is_internal     = .TRUE.
-          var % task            = "internal profile"
+          var%lod             = -1
+          var%ndim            = 2
+          var%dimids(2)       = output_file%dimid_time    !t
+          var%dimids(1)       = output_file%dimids_scl(3) !z
+          var%dimvarids(2)    = output_file%dimvarid_time
+          var%dimvarids(1)    = output_file%dimvarids_scl(3)
+          var%to_be_processed = .TRUE.
+          var%is_internal     = .TRUE.
+          var%task            = "internal profile"
 
        CASE DEFAULT
@@ -3630,31 +3649,31 @@
            CALL inifor_abort ('init_nc_var', message)
 
-       END SELECT
-
-    END FUNCTION init_nc_var
-
-
-    SUBROUTINE fini_variables()
-
-       CALL report('fini_variables', 'Deallocating variable table', cfg % debug)
-       DEALLOCATE( input_var_table )
-
-    END SUBROUTINE fini_variables
-
-
-    SUBROUTINE fini_io_groups()
-
-       CALL report('fini_io_groups', 'Deallocating IO groups', cfg % debug)
-       DEALLOCATE( io_group_list )
-
-    END SUBROUTINE fini_io_groups
-
-
-    SUBROUTINE fini_file_lists()
-       
-       CALL report('fini_file_lists', 'Deallocating file lists', cfg % debug)
-       DEALLOCATE( flow_files, soil_files, radiation_files, soil_moisture_files )
-
-    END SUBROUTINE fini_file_lists
+    END SELECT
+
+ END FUNCTION init_nc_var
+
+
+ SUBROUTINE fini_variables()
+
+    CALL report('fini_variables', 'Deallocating variable table', cfg%debug)
+    DEALLOCATE( input_var_table )
+
+ END SUBROUTINE fini_variables
+
+
+ SUBROUTINE fini_io_groups()
+
+    CALL report('fini_io_groups', 'Deallocating IO groups', cfg%debug)
+    DEALLOCATE( io_group_list )
+
+ END SUBROUTINE fini_io_groups
+
+
+ SUBROUTINE fini_file_lists()
+    
+    CALL report('fini_file_lists', 'Deallocating file lists', cfg%debug)
+    DEALLOCATE( flow_files, soil_files, radiation_files, soil_moisture_files )
+
+ END SUBROUTINE fini_file_lists
 
 
@@ -3670,20 +3689,20 @@
 !> array will match a COSMO-DE scalar array.
 !------------------------------------------------------------------------------!
-    SUBROUTINE preprocess(group, input_buffer, cosmo_grid, iter)
-
-       TYPE(io_group), INTENT(INOUT), TARGET       ::  group
-       TYPE(container), INTENT(INOUT), ALLOCATABLE ::  input_buffer(:)
-       TYPE(grid_definition), INTENT(IN)           ::  cosmo_grid
-       INTEGER, INTENT(IN)                         ::  iter
-       
-       REAL(dp), ALLOCATABLE                       ::  basic_state_pressure(:)
-       TYPE(container), ALLOCATABLE                ::  preprocess_buffer(:)
-       INTEGER                                     ::  hour, dt
-       INTEGER                                     ::  i, j, k
-       INTEGER                                     ::  nx, ny, nz
-       
-       input_buffer(:) % is_preprocessed = .FALSE.
-        
-       SELECT CASE( group % kind )
+ SUBROUTINE preprocess(group, input_buffer, cosmo_grid, iter)
+
+    TYPE(io_group), INTENT(INOUT), TARGET       ::  group
+    TYPE(container), INTENT(INOUT), ALLOCATABLE ::  input_buffer(:)
+    TYPE(grid_definition), INTENT(IN)           ::  cosmo_grid
+    INTEGER, INTENT(IN)                         ::  iter
+    
+    REAL(wp), ALLOCATABLE                       ::  basic_state_pressure(:)
+    TYPE(container), ALLOCATABLE                ::  preprocess_buffer(:)
+    INTEGER                                     ::  hour, dt
+    INTEGER                                     ::  i, j, k
+    INTEGER                                     ::  nx, ny, nz
+    
+    input_buffer(:)%is_preprocessed = .FALSE.
+     
+    SELECT CASE( group%kind )
        
        CASE( 'velocities' )
@@ -3694,71 +3713,71 @@
 !
 !--       Allocate u and v arrays with scalar dimensions
-          nx = SIZE(input_buffer(1) % array, 1)
-          ny = SIZE(input_buffer(1) % array, 2)
-          nz = SIZE(input_buffer(1) % array, 3)
-          ALLOCATE( preprocess_buffer(1) % array(nx, ny, nz) ) ! u buffer
-          ALLOCATE( preprocess_buffer(2) % array(nx, ny, nz) ) ! v buffer
-
- CALL run_control('time', 'alloc')
+          nx = SIZE(input_buffer(1)%array, 1)
+          ny = SIZE(input_buffer(1)%array, 2)
+          nz = SIZE(input_buffer(1)%array, 3)
+          ALLOCATE( preprocess_buffer(1)%array(nx, ny, nz) ) ! u buffer
+          ALLOCATE( preprocess_buffer(2)%array(nx, ny, nz) ) ! v buffer
+
+          CALL log_runtime('time', 'alloc')
 
 !
 !--       interpolate U and V to centres
-          CALL centre_velocities( u_face = input_buffer(1) % array,            &
-                                  v_face = input_buffer(2) % array,            &
-                                  u_centre = preprocess_buffer(1) % array,     &
-                                  v_centre = preprocess_buffer(2) % array )
+          CALL centre_velocities( u_face = input_buffer(1)%array,            &
+                                  v_face = input_buffer(2)%array,            &
+                                  u_centre = preprocess_buffer(1)%array,     &
+                                  v_centre = preprocess_buffer(2)%array )
           
-          cfg % rotation_method = 'rotated-pole'
-          SELECT CASE(cfg % rotation_method)
-
-          CASE('rotated-pole')
-!
-!--          rotate U and V to PALM-4U orientation and overwrite U and V with
-!--          rotated velocities
-             DO k = 1, nz
-             DO j = 1, ny
-             DO i = 1, nx
-                CALL uv2uvrot( urot = preprocess_buffer(1) % array(i,j,k),     &
-                               vrot = preprocess_buffer(2) % array(i,j,k),     &
-                               rlat = cosmo_grid % lat(j-1),                   &
-                               rlon = cosmo_grid % lon(i-1),                   &
-                               pollat = phi_cn,                                &
-                               pollon = lambda_cn,                             &
-                               u = input_buffer(1) % array(i,j,k),             &
-                               v = input_buffer(2) % array(i,j,k) )
-             ENDDO
-             ENDDO
-             ENDDO
-
-          CASE DEFAULT
-             message = "Rotation method '" // TRIM(cfg % rotation_method) //   &
-                "' not recognized."
-             CALL inifor_abort('preprocess', message)
+          cfg%rotation_method = 'rotated-pole'
+          SELECT CASE(cfg%rotation_method)
+
+             CASE('rotated-pole')
+!            
+!--             rotate U and V to PALM-4U orientation and overwrite U and V with
+!--             rotated velocities
+                DO  k = 1, nz
+                DO  j = 1, ny
+                DO  i = 1, nx
+                   CALL uv2uvrot( urot = preprocess_buffer(1)%array(i,j,k),     &
+                                  vrot = preprocess_buffer(2)%array(i,j,k),     &
+                                  rlat = cosmo_grid%lat(j-1),                   &
+                                  rlon = cosmo_grid%lon(i-1),                   &
+                                  pollat = phi_cn,                                &
+                                  pollon = lambda_cn,                             &
+                                  u = input_buffer(1)%array(i,j,k),             &
+                                  v = input_buffer(2)%array(i,j,k) )
+                ENDDO
+                ENDDO
+                ENDDO
+             
+             CASE DEFAULT
+                message = "Rotation method '" // TRIM(cfg%rotation_method) //   &
+                   "' not recognized."
+                CALL inifor_abort('preprocess', message)
 
           END SELECT
 
-          input_buffer(1) % array(1,:,:) = 0.0_dp
-          input_buffer(2) % array(1,:,:) = 0.0_dp
-          input_buffer(1) % array(:,1,:) = 0.0_dp
-          input_buffer(2) % array(:,1,:) = 0.0_dp
-
-          input_buffer(1:2) % is_preprocessed = .TRUE.
- CALL run_control('time', 'comp')
+          input_buffer(1)%array(1,:,:) = 0.0_wp
+          input_buffer(2)%array(1,:,:) = 0.0_wp
+          input_buffer(1)%array(:,1,:) = 0.0_wp
+          input_buffer(2)%array(:,1,:) = 0.0_wp
+
+          input_buffer(1:2)%is_preprocessed = .TRUE.
+          CALL log_runtime('time', 'comp')
 
           DEALLOCATE( preprocess_buffer )
- CALL run_control('time', 'alloc')
-
-          message = "Input buffers for group '" // TRIM(group % kind) // "'"//&
+          CALL log_runtime('time', 'alloc')
+
+          message = "Input buffers for group '" // TRIM(group%kind) // "'"//&
              " preprocessed sucessfully."
           CALL report('preprocess', message)
        
        CASE( 'thermodynamics' ) ! T, P, QV
-          nx = SIZE(input_buffer(1) % array, 1)
-          ny = SIZE(input_buffer(1) % array, 2)
-          nz = SIZE(input_buffer(1) % array, 3)
+          nx = SIZE(input_buffer(1)%array, 1)
+          ny = SIZE(input_buffer(1)%array, 2)
+          nz = SIZE(input_buffer(1)%array, 3)
 
 !
 !--       Compute absolute pressure if presure perturbation has been read in.
-          IF ( TRIM(group % in_var_list(2) % name) == 'PP' )  THEN
+          IF ( TRIM(group%in_var_list(2)%name) == 'PP' )  THEN
              message = "Absolute pressure, P, not available, " //              &
                        "computing from pressure preturbation PP."
@@ -3766,10 +3785,10 @@
 
              ALLOCATE( basic_state_pressure(1:nz) )
- CALL run_control('time', 'alloc')
-
-             DO j = 1, ny
-             DO i = 1, nx
-
-                CALL get_basic_state(cosmo_grid % hfl(i,j,:), BETA, P_SL, T_SL,&
+             CALL log_runtime('time', 'alloc')
+
+             DO  j = 1, ny
+             DO  i = 1, nx
+
+                CALL get_basic_state(cosmo_grid%hfl(i,j,:), BETA, P_SL, T_SL,&
                                      RD, G, basic_state_pressure)
 
@@ -3777,35 +3796,36 @@
 !--             Overwrite pressure perturbation with absolute pressure. HECTO
 !--             converts pressure perturbation from hPa to Pa.
-                input_buffer (2) % array(i,j,:) =                              &
-                   HECTO * input_buffer (2) % array(i,j,:) +                   &
+                input_buffer (2)%array(i,j,:) =                              &
+                   HECTO * input_buffer (2)%array(i,j,:) +                   &
                    basic_state_pressure(:)
 
              ENDDO
              ENDDO
- CALL run_control('time', 'comp')
+             CALL log_runtime('time', 'comp')
 
              DEALLOCATE( basic_state_pressure )
- CALL run_control('time', 'alloc')
-
-             group % in_var_list(2) % name = 'P'
+             CALL log_runtime('time', 'alloc')
+
+             group%in_var_list(2)%name = 'P'
 
           ENDIF
 !
 !--       mark pressure as preprocessed
-          input_buffer(2) % is_preprocessed = .TRUE.
+          input_buffer(2)%is_preprocessed = .TRUE.
 
 !
 !--       Copy temperature to the last input buffer array
           ALLOCATE(                                                            &
-              input_buffer( group % n_output_quantities ) % array (nx, ny, nz) &
+              input_buffer( group%n_output_quantities )%array (nx, ny, nz) &
           )
-          input_buffer(group % n_output_quantities) % array(:,:,:) =           &
-              input_buffer(1) % array(:,:,:)
+          CALL log_runtime('time', 'alloc')
+          input_buffer(group%n_output_quantities)%array(:,:,:) =           &
+              input_buffer(1)%array(:,:,:)
 
 !
 !--       Convert absolute in place to potential temperature
           CALL potential_temperature(                                          &
-             t = input_buffer(1) % array(:,:,:),                               &
-             p = input_buffer(2) % array(:,:,:),                               &
+             t = input_buffer(1)%array(:,:,:),                               &
+             p = input_buffer(2)%array(:,:,:),                               &
              p_ref = P_REF,                                                    &
              r = RD_PALM,                                                      &
@@ -3815,12 +3835,12 @@
 !
 !--       mark potential temperature as preprocessed
-          input_buffer(1) % is_preprocessed = .TRUE.
+          input_buffer(1)%is_preprocessed = .TRUE.
 
 !
 !--       Convert temperature copy to density
           CALL moist_density(                                                  &
-             t_rho = input_buffer(group % n_output_quantities) % array(:,:,:), &
-             p = input_buffer(2) % array(:,:,:),                               &
-             qv = input_buffer(3) % array(:,:,:),                              &
+             t_rho = input_buffer(group%n_output_quantities)%array(:,:,:), &
+             p = input_buffer(2)%array(:,:,:),                               &
+             qv = input_buffer(3)%array(:,:,:),                              &
              rd = RD,                                                          &
              rv = RV                                                           &
@@ -3829,43 +3849,40 @@
 !
 !--       mark qv as preprocessed
-          input_buffer(3) % is_preprocessed = .TRUE.
+          input_buffer(3)%is_preprocessed = .TRUE.
 
 !
 !--       mark density as preprocessed
-          input_buffer(group % n_output_quantities) % is_preprocessed = .TRUE.
-
-
-          message = "Input buffers for group '" // TRIM(group % kind) // "'"//&
+          input_buffer(group%n_output_quantities)%is_preprocessed = .TRUE.
+
+
+          message = "Input buffers for group '" // TRIM(group%kind) // "'"//&
              " preprocessed sucessfully."
           CALL report('preprocess', message)
- CALL run_control('time', 'comp')
        
        CASE( 'scalar' ) ! S or W
-          input_buffer(:) % is_preprocessed = .TRUE.
- CALL run_control('time', 'comp')
+          input_buffer(:)%is_preprocessed = .TRUE.
 
        CASE( 'soil-temperature' ) ! 
           
-          CALL fill_water_cells(soiltyp, input_buffer(1) % array, &
-                                SIZE(input_buffer(1) % array, 3), &
+          CALL fill_water_cells(soiltyp, input_buffer(1)%array, &
+                                SIZE(input_buffer(1)%array, 3), &
                                 FILL_ITERATIONS)
-          input_buffer(:) % is_preprocessed = .TRUE.
- CALL run_control('time', 'comp')
+          input_buffer(:)%is_preprocessed = .TRUE.
 
        CASE( 'soil-water' ) ! 
 
-          CALL fill_water_cells(soiltyp, input_buffer(1) % array, &
-                                SIZE(input_buffer(1) % array, 3), &
+          CALL fill_water_cells(soiltyp, input_buffer(1)%array, &
+                                SIZE(input_buffer(1)%array, 3), &
                                 FILL_ITERATIONS)
 
-          nx = SIZE(input_buffer(1) % array, 1)
-          ny = SIZE(input_buffer(1) % array, 2)
-          nz = SIZE(input_buffer(1) % array, 3)
-
-          DO k = 1, nz
-          DO j = 1, ny
-          DO i = 1, nx
-             input_buffer(1) % array(i,j,k) =                                  &
-                 input_buffer(1) % array(i,j,k) * d_depth_rho_inv(k)
+          nx = SIZE(input_buffer(1)%array, 1)
+          ny = SIZE(input_buffer(1)%array, 2)
+          nz = SIZE(input_buffer(1)%array, 3)
+
+          DO  k = 1, nz
+          DO  j = 1, ny
+          DO  i = 1, nx
+             input_buffer(1)%array(i,j,k) =                                  &
+                 input_buffer(1)%array(i,j,k) * d_depth_rho_inv(k)
           ENDDO
           ENDDO
@@ -3875,13 +3892,11 @@
           CALL report('preprocess', message)
 
-          input_buffer(:) % is_preprocessed = .TRUE.
- CALL run_control('time', 'comp')
+          input_buffer(:)%is_preprocessed = .TRUE.
 
        CASE( 'surface' ) ! 
-          input_buffer(:) % is_preprocessed = .TRUE.
- CALL run_control('time', 'comp')
+          input_buffer(:)%is_preprocessed = .TRUE.
 
        CASE( 'accumulated' ) ! 
-          message = "De-accumulating '" // TRIM(group % in_var_list(1) % name) //&
+          message = "De-accumulating '" // TRIM(group%in_var_list(1)%name) //&
                     "' in iteration " // TRIM(str(iter)) 
           CALL report('preprocess', message)
@@ -3892,42 +3907,41 @@
 
 !
-!--       input has been accumulated over one hour. Leave as is
-!--       input_buffer(1) % array(:,:,:) carrries one-hour integral
-          CASE(1)
-
-!
-!--       input has been accumulated over two hours. Subtract previous step
-!--       input_buffer(1) % array(:,:,:) carrries one-hour integral
-!--       input_buffer(2) % array(:,:,:) carrries two-hour integral
-          CASE(2)
-             CALL deaverage(                                                   &
-                      avg_1 = input_buffer(1) % array(:,:,:), t1 = 1.0_dp,     &
-                      avg_2 = input_buffer(2) % array(:,:,:), t2 = 1.0_dp,     &
-                      avg_3 = input_buffer(1) % array(:,:,:), t3 = 1.0_dp )
-!
-!--          input_buffer(1) % array(:,:,:) carrries one-hour integral of second hour
-
-!
-!--       input has been accumulated over three hours. Subtract previous step
-!--       input_buffer(1) % array(:,:,:) carrries three-hour integral
-!--       input_buffer(2) % array(:,:,:) still carrries two-hour integral
-          CASE(3)
-             CALL deaverage(                                                   &
-                     avg_1 = input_buffer(2) % array(:,:,:), t1 = 1.0_dp,      &
-                     avg_2 = input_buffer(1) % array(:,:,:), t2 = 1.0_dp,      &
-                     avg_3 = input_buffer(1) % array(:,:,:), t3 = 1.0_dp )
-!
-!--          input_buffer(1) % array(:,:,:) carrries one-hour integral of third hourA
-
-          CASE DEFAULT
-             message = "Invalid averaging period '" // TRIM(str(dt)) // " hours"
-             CALL inifor_abort('preprocess', message)
+!--          input has been accumulated over one hour. Leave as is
+!--          input_buffer(1)%array(:,:,:) carrries one-hour integral
+             CASE(1)
+             
+!            
+!--          input has been accumulated over two hours. Subtract previous step
+!--          input_buffer(1)%array(:,:,:) carrries one-hour integral
+!--          input_buffer(2)%array(:,:,:) carrries two-hour integral
+             CASE(2)
+                CALL deaverage(                                                   &
+                         avg_1 = input_buffer(1)%array(:,:,:), t1 = 1.0_wp,     &
+                         avg_2 = input_buffer(2)%array(:,:,:), t2 = 1.0_wp,     &
+                         avg_3 = input_buffer(1)%array(:,:,:), t3 = 1.0_wp )
+!            
+!--             input_buffer(1)%array(:,:,:) carrries one-hour integral of second hour
+             
+!            
+!--          input has been accumulated over three hours. Subtract previous step
+!--          input_buffer(1)%array(:,:,:) carrries three-hour integral
+!--          input_buffer(2)%array(:,:,:) still carrries two-hour integral
+             CASE(3)
+                CALL deaverage(                                                   &
+                        avg_1 = input_buffer(2)%array(:,:,:), t1 = 1.0_wp,      &
+                        avg_2 = input_buffer(1)%array(:,:,:), t2 = 1.0_wp,      &
+                        avg_3 = input_buffer(1)%array(:,:,:), t3 = 1.0_wp )
+!            
+!--             input_buffer(1)%array(:,:,:) carrries one-hour integral of third hourA
+             
+             CASE DEFAULT
+                message = "Invalid averaging period '" // TRIM(str(dt)) // " hours"
+                CALL inifor_abort('preprocess', message)
 
           END SELECT
-          input_buffer(:) % is_preprocessed = .TRUE.
- CALL run_control('time', 'comp')
+          input_buffer(:)%is_preprocessed = .TRUE.
 
        CASE( 'running average' ) ! 
-          message = "De-averaging '" // TRIM(group % in_var_list(1) % name) //   &
+          message = "De-averaging '" // TRIM(group%in_var_list(1)%name) //   &
                     "' in iteration " // TRIM(str(iter)) 
           CALL report('preprocess', message)
@@ -3939,45 +3953,45 @@
           SELECT CASE(dt)
 !
-!--       input has been accumulated over one hour. Leave as is
-!--       input_buffer(1) % array(:,:,:) carrries one-hour integral
-          CASE(1)
-
-!
-!--       input has been accumulated over two hours. Subtract previous step
-!--       input_buffer(1) % array(:,:,:) carrries one-hour integral
-!--       input_buffer(2) % array(:,:,:) carrries two-hour integral
-          CASE(2)
-             CALL deaverage( input_buffer(1) % array(:,:,:), 1.0_dp,           &
-                             input_buffer(2) % array(:,:,:), 2.0_dp,           &
-                             input_buffer(1) % array(:,:,:), 1.0_dp)
-!
-!--          input_buffer(1) % array(:,:,:) carrries one-hour integral of second hour
-
-!
-!--       input has been accumulated over three hours. Subtract previous step
-!--       input_buffer(1) % array(:,:,:) carrries three-hour integral
-!--       input_buffer(2) % array(:,:,:) still carrries two-hour integral
-          CASE(3)
-             CALL deaverage( input_buffer(2) % array(:,:,:), 2.0_dp,           &
-                             input_buffer(1) % array(:,:,:), 3.0_dp,           &
-                             input_buffer(1) % array(:,:,:), 1.0_dp)
-!
-!--          input_buffer(1) % array(:,:,:) carrries one-hour integral of third hourA
-
-          CASE DEFAULT
-             message = "Invalid averaging period '" // TRIM(str(dt)) // " hours"
-             CALL inifor_abort('preprocess', message)
+!--          input has been accumulated over one hour. Leave as is
+!--          input_buffer(1)%array(:,:,:) carrries one-hour integral
+             CASE(1)
+             
+!            
+!--          input has been accumulated over two hours. Subtract previous step
+!--          input_buffer(1)%array(:,:,:) carrries one-hour integral
+!--          input_buffer(2)%array(:,:,:) carrries two-hour integral
+             CASE(2)
+                CALL deaverage( input_buffer(1)%array(:,:,:), 1.0_wp,           &
+                                input_buffer(2)%array(:,:,:), 2.0_wp,           &
+                                input_buffer(1)%array(:,:,:), 1.0_wp)
+!            
+!--             input_buffer(1)%array(:,:,:) carrries one-hour integral of second hour
+             
+!            
+!--          input has been accumulated over three hours. Subtract previous step
+!--          input_buffer(1)%array(:,:,:) carrries three-hour integral
+!--          input_buffer(2)%array(:,:,:) still carrries two-hour integral
+             CASE(3)
+                CALL deaverage( input_buffer(2)%array(:,:,:), 2.0_wp,           &
+                                input_buffer(1)%array(:,:,:), 3.0_wp,           &
+                                input_buffer(1)%array(:,:,:), 1.0_wp)
+!            
+!--             input_buffer(1)%array(:,:,:) carrries one-hour integral of third hourA
+             
+             CASE DEFAULT
+                message = "Invalid averaging period '" // TRIM(str(dt)) // " hours"
+                CALL inifor_abort('preprocess', message)
 
           END SELECT
-          input_buffer(:) % is_preprocessed = .TRUE.
+          input_buffer(:)%is_preprocessed = .TRUE.
 
        CASE DEFAULT
-          message = "IO group kind '" // TRIM(group % kind) // "' is not supported."
+          message = "IO group kind '" // TRIM(group%kind) // "' is not supported."
           CALL inifor_abort('prerpocess', message)
 
-       END SELECT
- CALL run_control('time', 'comp')
-
-    END SUBROUTINE preprocess
+    END SELECT
+    CALL log_runtime('time', 'comp')
+
+ END SUBROUTINE preprocess
 
 
@@ -4006,68 +4020,68 @@
 !> array : the soil array (i.e. water content or temperature)
 !------------------------------------------------------------------------------!
-    SUBROUTINE fill_water_cells(soiltyp, array, nz, niter)
-       INTEGER(hp), DIMENSION(:,:,:), INTENT(IN) :: soiltyp
-       REAL(dp), DIMENSION(:,:,:), INTENT(INOUT) :: array
-       INTEGER, INTENT(IN)                       :: nz, niter
-
-       REAL(dp), DIMENSION(nz)                   :: column
-       INTEGER(hp), DIMENSION(:,:), ALLOCATABLE  :: old_soiltyp, new_soiltyp
-       INTEGER                                   :: l, i, j, nx, ny, n_cells, ii, jj, iter
-       INTEGER, DIMENSION(8)                     :: di, dj
-
-       nx = SIZE(array, 1)
-       ny = SIZE(array, 2)
-       di = (/ -1, -1, -1, 0,  0,  1, 1, 1 /)
-       dj = (/ -1,  0,  1, -1, 1, -1, 0, 1 /)
-
-       ALLOCATE(old_soiltyp(SIZE(soiltyp,1), &
-                            SIZE(soiltyp,2) ))
-
-       ALLOCATE(new_soiltyp(SIZE(soiltyp,1), &
-                            SIZE(soiltyp,2) ))
-
-       old_soiltyp(:,:) = soiltyp(:,:,1)
-       new_soiltyp(:,:) = soiltyp(:,:,1)
-
-       DO iter = 1, niter
-
-          DO j = 1, ny
-          DO i = 1, nx
-          
-             IF (old_soiltyp(i,j) == WATER_ID)  THEN 
-
-                n_cells = 0
-                column(:) = 0.0_dp
-                DO l = 1, SIZE(di)
-
-                   ii = MIN(nx, MAX(1, i + di(l)))
-                   jj = MIN(ny, MAX(1, j + dj(l)))
-
-                   IF (old_soiltyp(ii,jj) .NE. WATER_ID)  THEN
-                      n_cells = n_cells + 1
-                      column(:) = column(:) + array(ii,jj,:)
-                   ENDIF
-
-                ENDDO
-
-!
-!--             Overwrite if at least one non-water neighbour cell is available
-                IF (n_cells > 0)  THEN
-                   array(i,j,:) = column(:) / n_cells
-                   new_soiltyp(i,j) = 0
+ SUBROUTINE fill_water_cells(soiltyp, array, nz, niter)
+    INTEGER(iwp), DIMENSION(:,:,:), INTENT(IN) :: soiltyp
+    REAL(wp), DIMENSION(:,:,:), INTENT(INOUT)  :: array
+    INTEGER, INTENT(IN)                        :: nz, niter
+
+    REAL(wp), DIMENSION(nz)                    :: column
+    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE  :: old_soiltyp, new_soiltyp
+    INTEGER                                    :: l, i, j, nx, ny, n_cells, ii, jj, iter
+    INTEGER, DIMENSION(8)                      :: di, dj
+
+    nx = SIZE(array, 1)
+    ny = SIZE(array, 2)
+    di = (/ -1, -1, -1, 0,  0,  1, 1, 1 /)
+    dj = (/ -1,  0,  1, -1, 1, -1, 0, 1 /)
+
+    ALLOCATE(old_soiltyp(SIZE(soiltyp,1), &
+                         SIZE(soiltyp,2) ))
+
+    ALLOCATE(new_soiltyp(SIZE(soiltyp,1), &
+                         SIZE(soiltyp,2) ))
+
+    old_soiltyp(:,:) = soiltyp(:,:,1)
+    new_soiltyp(:,:) = soiltyp(:,:,1)
+
+    DO  iter = 1, niter
+
+       DO  j = 1, ny
+       DO  i = 1, nx
+       
+          IF (old_soiltyp(i,j) == WATER_ID)  THEN 
+
+             n_cells = 0
+             column(:) = 0.0_wp
+             DO  l = 1, SIZE(di)
+
+                ii = MIN(nx, MAX(1, i + di(l)))
+                jj = MIN(ny, MAX(1, j + dj(l)))
+
+                IF (old_soiltyp(ii,jj) .NE. WATER_ID)  THEN
+                   n_cells = n_cells + 1
+                   column(:) = column(:) + array(ii,jj,:)
                 ENDIF
 
+             ENDDO
+
+!
+!--          Overwrite if at least one non-water neighbour cell is available
+             IF (n_cells > 0)  THEN
+                array(i,j,:) = column(:) / n_cells
+                new_soiltyp(i,j) = 0
              ENDIF
 
-          ENDDO
-          ENDDO
-
-          old_soiltyp(:,:) = new_soiltyp(:,:)
+          ENDIF
 
        ENDDO
-
-       DEALLOCATE(old_soiltyp, new_soiltyp)
-
-    END SUBROUTINE fill_water_cells
+       ENDDO
+
+       old_soiltyp(:,:) = new_soiltyp(:,:)
+
+    ENDDO
+
+    DEALLOCATE(old_soiltyp, new_soiltyp)
+
+ END SUBROUTINE fill_water_cells
 
 
Index: /palm/trunk/UTIL/inifor/src/inifor_io.f90
===================================================================
--- /palm/trunk/UTIL/inifor/src/inifor_io.f90	(revision 3865)
+++ /palm/trunk/UTIL/inifor/src/inifor_io.f90	(revision 3866)
@@ -26,4 +26,9 @@
 ! -----------------
 ! $Id$
+! Use PALM's working precision
+! Improved coding style
+!
+! 
+! 3801 2019-03-15 17:14:25Z eckhard
 ! Added routine get_cosmo_grid() to read in COSMO rotated pole from COSMO domain
 ! Moved get_soil_layer_thickness() here from inifor_grid
@@ -118,8 +123,8 @@
     USE inifor_control
     USE inifor_defs,                                                           &
-        ONLY:  DATE, SNAME, PATH, PI, dp, hp, TO_RADIANS, TO_DEGREES, VERSION, &
+        ONLY:  DATE, SNAME, PATH, PI, TO_RADIANS, TO_DEGREES, VERSION,         &
                NC_DEPTH_NAME, NC_HHL_NAME, NC_RLAT_NAME, NC_RLON_NAME,         &
                NC_ROTATED_POLE_NAME, NC_POLE_LATITUDE_NAME,                    &
-               NC_POLE_LONGITUDE_NAME, RHO_L
+               NC_POLE_LONGITUDE_NAME, RHO_L, wp, iwp
     USE inifor_types
     USE inifor_util,                                                           &
@@ -133,5 +138,5 @@
 ! ------------
 !> get_netcdf_variable() reads the netCDF data and metadate for the netCDF
-!> variable 'in_var % name' from the file 'in_file'. The netCDF data array is
+!> variable 'in_var%name' from the file 'in_file'. The netCDF data array is
 !> stored in the 'buffer' array and metadata added to the respective members of
 !> the given 'in_var'.
@@ -152,40 +157,40 @@
 !> get_netcdf_variable interface.
 !------------------------------------------------------------------------------!
-    SUBROUTINE get_netcdf_variable_int(in_file, in_var, buffer)
-
-       CHARACTER(LEN=PATH), INTENT(IN)         ::  in_file
-       TYPE(nc_var), INTENT(INOUT)             ::  in_var
-       INTEGER(hp), ALLOCATABLE, INTENT(INOUT) ::  buffer(:,:,:)
-
-       INTEGER               ::  ncid
-       INTEGER, DIMENSION(3) ::  start, count
-
-       IF ( nf90_open( TRIM(in_file), NF90_NOWRITE, ncid ) .EQ. NF90_NOERR .AND. &
-            nf90_inq_varid( ncid, in_var % name, in_var % varid ) .EQ. NF90_NOERR )  THEN
-
-          CALL get_input_dimensions(in_var, ncid)
-
-          CALL get_netcdf_start_and_count(in_var, start, count)
- CALL run_control('time', 'read')
-
-          ALLOCATE( buffer( count(1), count(2), count(3) ) )
- CALL run_control('time', 'alloc')
-
-          CALL check(nf90_get_var( ncid, in_var % varid, buffer,                  &
-                                   start = start,                                 &
-                                   count = count ))
-
-       ELSE
-
-          message = "Failed to read '" // TRIM(in_var % name) // &
-             "' from file '" // TRIM(in_file) // "'."
-          CALL inifor_abort('get_netcdf_variable', message)
-
-       ENDIF
-
-       CALL check(nf90_close(ncid))
- CALL run_control('time', 'read')
-
-    END SUBROUTINE get_netcdf_variable_int
+ SUBROUTINE get_netcdf_variable_int(in_file, in_var, buffer)
+
+    CHARACTER(LEN=PATH), INTENT(IN)          ::  in_file
+    TYPE(nc_var), INTENT(INOUT)              ::  in_var
+    INTEGER(iwp), ALLOCATABLE, INTENT(INOUT) ::  buffer(:,:,:)
+
+    INTEGER               ::  ncid
+    INTEGER, DIMENSION(3) ::  start, count
+
+    IF ( nf90_open( TRIM(in_file), NF90_NOWRITE, ncid ) .EQ. NF90_NOERR .AND. &
+         nf90_inq_varid( ncid, in_var%name, in_var%varid ) .EQ. NF90_NOERR )  THEN
+
+       CALL get_input_dimensions(in_var, ncid)
+
+       CALL get_netcdf_start_and_count(in_var, start, count)
+       CALL log_runtime('time', 'read')
+
+       ALLOCATE( buffer( count(1), count(2), count(3) ) )
+       CALL log_runtime('time', 'alloc')
+
+       CALL check(nf90_get_var( ncid, in_var%varid, buffer,                  &
+                                start = start,                                 &
+                                count = count ))
+
+    ELSE
+
+       message = "Failed to read '" // TRIM(in_var%name) // &
+          "' from file '" // TRIM(in_file) // "'."
+       CALL inifor_abort('get_netcdf_variable', message)
+
+    ENDIF
+
+    CALL check(nf90_close(ncid))
+    CALL log_runtime('time', 'read')
+
+ END SUBROUTINE get_netcdf_variable_int
 
 
@@ -196,40 +201,40 @@
 !> get_netcdf_variable interface.
 !------------------------------------------------------------------------------!
-    SUBROUTINE get_netcdf_variable_real(in_file, in_var, buffer)
-
-       CHARACTER(LEN=PATH), INTENT(IN)      ::  in_file
-       TYPE(nc_var), INTENT(INOUT)          ::  in_var
-       REAL(dp), ALLOCATABLE, INTENT(INOUT) ::  buffer(:,:,:)
-
-       INTEGER               ::  ncid
-       INTEGER, DIMENSION(3) ::  start, count
-
-       IF ( nf90_open( TRIM(in_file), NF90_NOWRITE, ncid ) .EQ. NF90_NOERR .AND. &
-            nf90_inq_varid( ncid, in_var % name, in_var % varid ) .EQ. NF90_NOERR )  THEN
-
-          CALL get_input_dimensions(in_var, ncid)
-
-          CALL get_netcdf_start_and_count(in_var, start, count)
- CALL run_control('time', 'read')
-
-          ALLOCATE( buffer( count(1), count(2), count(3) ) )
- CALL run_control('time', 'alloc')
-
-          CALL check(nf90_get_var( ncid, in_var % varid, buffer,                  &
-                                   start = start,                                 &
-                                   count = count ))
-
-       ELSE
-
-          message = "Failed to read '" // TRIM(in_var % name) // &
-             "' from file '" // TRIM(in_file) // "'."
-          CALL inifor_abort('get_netcdf_variable', message)
-
-       ENDIF
-
-       CALL check(nf90_close(ncid))
- CALL run_control('time', 'read')
-
-    END SUBROUTINE get_netcdf_variable_real
+ SUBROUTINE get_netcdf_variable_real(in_file, in_var, buffer)
+
+    CHARACTER(LEN=PATH), INTENT(IN)      ::  in_file
+    TYPE(nc_var), INTENT(INOUT)          ::  in_var
+    REAL(wp), ALLOCATABLE, INTENT(INOUT) ::  buffer(:,:,:)
+
+    INTEGER               ::  ncid
+    INTEGER, DIMENSION(3) ::  start, count
+
+    IF ( nf90_open( TRIM(in_file), NF90_NOWRITE, ncid ) .EQ. NF90_NOERR .AND. &
+         nf90_inq_varid( ncid, in_var%name, in_var%varid ) .EQ. NF90_NOERR )  THEN
+
+       CALL get_input_dimensions(in_var, ncid)
+
+       CALL get_netcdf_start_and_count(in_var, start, count)
+       CALL log_runtime('time', 'read')
+
+       ALLOCATE( buffer( count(1), count(2), count(3) ) )
+       CALL log_runtime('time', 'alloc')
+
+       CALL check(nf90_get_var( ncid, in_var%varid, buffer,                  &
+                                start = start,                                 &
+                                count = count ))
+
+    ELSE
+
+       message = "Failed to read '" // TRIM(in_var%name) // &
+          "' from file '" // TRIM(in_file) // "'."
+       CALL inifor_abort('get_netcdf_variable', message)
+
+    ENDIF
+
+    CALL check(nf90_close(ncid))
+    CALL log_runtime('time', 'read')
+
+ END SUBROUTINE get_netcdf_variable_real
 
 
@@ -240,31 +245,31 @@
 !> netCDF file 'filename'.
 !------------------------------------------------------------------------------!
-    SUBROUTINE get_netcdf_dim_vector(filename, coordname, coords)
-
-       CHARACTER(LEN=*), INTENT(IN)         ::  filename
-       CHARACTER(LEN=*), INTENT(IN)         ::  coordname
-       REAL(dp), ALLOCATABLE, INTENT(INOUT) ::  coords(:)
-
-       INTEGER ::  ncid, varid, dimlen
-       INTEGER ::  dimids(NF90_MAX_VAR_DIMS)
-
-       IF ( nf90_open( TRIM(filename), NF90_NOWRITE, ncid ) .EQ. NF90_NOERR .AND. &
-            nf90_inq_varid( ncid, coordname, varid ) .EQ. NF90_NOERR )  THEN
-
-          CALL check(nf90_inquire_variable( ncid, varid, dimids = dimids ))
-          CALL check(nf90_inquire_dimension( ncid, dimids(1), len = dimlen ))
-
-          ALLOCATE(coords(dimlen))
-          CALL check(nf90_get_var( ncid, varid, coords))
-
-       ELSE
-
-          message = "Failed to read '" // TRIM(coordname) // &
-             "' from file '" // TRIM(filename) // "'."
-          CALL inifor_abort('get_netcdf_dim_vector', message)
-
-       ENDIF
-
-    END SUBROUTINE get_netcdf_dim_vector
+ SUBROUTINE get_netcdf_dim_vector(filename, coordname, coords)
+
+    CHARACTER(LEN=*), INTENT(IN)         ::  filename
+    CHARACTER(LEN=*), INTENT(IN)         ::  coordname
+    REAL(wp), ALLOCATABLE, INTENT(INOUT) ::  coords(:)
+
+    INTEGER ::  ncid, varid, dimlen
+    INTEGER ::  dimids(NF90_MAX_VAR_DIMS)
+
+    IF ( nf90_open( TRIM(filename), NF90_NOWRITE, ncid ) .EQ. NF90_NOERR .AND. &
+         nf90_inq_varid( ncid, coordname, varid ) .EQ. NF90_NOERR )  THEN
+
+       CALL check(nf90_inquire_variable( ncid, varid, dimids = dimids ))
+       CALL check(nf90_inquire_dimension( ncid, dimids(1), len = dimlen ))
+
+       ALLOCATE(coords(dimlen))
+       CALL check(nf90_get_var( ncid, varid, coords))
+
+    ELSE
+
+       message = "Failed to read '" // TRIM(coordname) // &
+          "' from file '" // TRIM(filename) // "'."
+       CALL inifor_abort('get_netcdf_dim_vector', message)
+
+    ENDIF
+
+ END SUBROUTINE get_netcdf_dim_vector
 
 
@@ -273,30 +278,30 @@
 ! ------------
 !> get_input_dimensions() reads dimensions metadata of the netCDF variable given
-!> by 'in_var % name' into 'in_var' data structure.
-!------------------------------------------------------------------------------!
-    SUBROUTINE get_input_dimensions(in_var, ncid)
-
-       TYPE(nc_var), INTENT(INOUT) ::  in_var
-       INTEGER, INTENT(IN)         ::  ncid
-
-       INTEGER ::  i
-
-       CALL check(nf90_get_att( ncid, in_var % varid, "long_name",             &
-                                in_var % long_name))
-
-       CALL check(nf90_get_att( ncid, in_var % varid, "units",                 &
-                                in_var % units ))
-
-       CALL check(nf90_inquire_variable( ncid, in_var % varid,                 &
-                                         ndims  = in_var % ndim,               &
-                                         dimids = in_var % dimids ))
-
-       DO i = 1, in_var % ndim
-          CALL check(nf90_inquire_dimension( ncid, in_var % dimids(i),         &
-                                             name = in_var % dimname(i),       &
-                                             len  = in_var % dimlen(i) ))
-       ENDDO
-
-    END SUBROUTINE get_input_dimensions
+!> by 'in_var%name' into 'in_var' data structure.
+!------------------------------------------------------------------------------!
+ SUBROUTINE get_input_dimensions(in_var, ncid)
+
+    TYPE(nc_var), INTENT(INOUT) ::  in_var
+    INTEGER, INTENT(IN)         ::  ncid
+
+    INTEGER ::  i
+
+    CALL check(nf90_get_att( ncid, in_var%varid, "long_name",             &
+                             in_var%long_name))
+
+    CALL check(nf90_get_att( ncid, in_var%varid, "units",                 &
+                             in_var%units ))
+
+    CALL check(nf90_inquire_variable( ncid, in_var%varid,                 &
+                                      ndims  = in_var%ndim,               &
+                                      dimids = in_var%dimids ))
+
+    DO  i = 1, in_var%ndim
+       CALL check(nf90_inquire_dimension( ncid, in_var%dimids(i),         &
+                                          name = in_var%dimname(i),       &
+                                          len  = in_var%dimlen(i) ))
+    ENDDO
+
+ END SUBROUTINE get_input_dimensions
 
 
@@ -308,41 +313,41 @@
 !> and _real() for reading input variables..
 !------------------------------------------------------------------------------!
-    SUBROUTINE get_netcdf_start_and_count(in_var, start, count)
-
-       TYPE(nc_var), INTENT(INOUT)        ::  in_var
-       INTEGER, DIMENSION(3), INTENT(OUT) ::  start, count
-
-       INTEGER ::  ndim
-
-       IF ( in_var % ndim .LT. 2  .OR.  in_var % ndim .GT. 4 )  THEN
-
-          message = "Failed reading NetCDF variable " //                       &
-             TRIM(in_var % name) // " with " // TRIM(str(in_var % ndim)) //    &
-             " dimensions because only two- and and three-dimensional" //      &
-             " variables are supported."
-          CALL inifor_abort('get_netcdf_start_and_count', message)
-
-       ENDIF
-
-       start = (/ 1, 1, 1 /)
-       IF ( TRIM(in_var % name) .EQ. 'T_SO' )  THEN
-!
-!--       Skip depth = 0.0 for T_SO and reduce number of depths from 9 to 8
-          in_var % dimlen(3) = in_var % dimlen(3) - 1
-
-!
-!--       Start reading from second level, e.g. depth = 0.005 instead of 0.0
-          start(3) = 2
-       ENDIF
-
-       IF (in_var % ndim .EQ. 2)  THEN
-          in_var % dimlen(3) = 1
-       ENDIF
-
-       ndim = MIN(in_var % ndim, 3)
-       count = (/ 1, 1, 1 /)
-       count(1:ndim) = in_var % dimlen(1:ndim)
-
-    END SUBROUTINE get_netcdf_start_and_count
+ SUBROUTINE get_netcdf_start_and_count(in_var, start, count)
+
+    TYPE(nc_var), INTENT(INOUT)        ::  in_var
+    INTEGER, DIMENSION(3), INTENT(OUT) ::  start, count
+
+    INTEGER ::  ndim
+
+    IF ( in_var%ndim .LT. 2  .OR.  in_var%ndim .GT. 4 )  THEN
+
+       message = "Failed reading NetCDF variable " //                       &
+          TRIM(in_var%name) // " with " // TRIM(str(in_var%ndim)) //    &
+          " dimensions because only two- and and three-dimensional" //      &
+          " variables are supported."
+       CALL inifor_abort('get_netcdf_start_and_count', message)
+
+    ENDIF
+
+    start = (/ 1, 1, 1 /)
+    IF ( TRIM(in_var%name) .EQ. 'T_SO' )  THEN
+!
+!--    Skip depth = 0.0 for T_SO and reduce number of depths from 9 to 8
+       in_var%dimlen(3) = in_var%dimlen(3) - 1
+
+!
+!--    Start reading from second level, e.g. depth = 0.005 instead of 0.0
+       start(3) = 2
+    ENDIF
+
+    IF (in_var%ndim .EQ. 2)  THEN
+       in_var%dimlen(3) = 1
+    ENDIF
+
+    ndim = MIN(in_var%ndim, 3)
+    count = (/ 1, 1, 1 /)
+    count(1:ndim) = in_var%dimlen(1:ndim)
+
+ END SUBROUTINE get_netcdf_start_and_count
 
 
@@ -353,19 +358,19 @@
 !> output.
 !------------------------------------------------------------------------------!
-    SUBROUTINE netcdf_define_variable(var, ncid)
-
-        TYPE(nc_var), INTENT(INOUT) ::  var
-        INTEGER, INTENT(IN)         ::  ncid
-
-        CALL check(nf90_def_var(ncid, var % name, NF90_FLOAT,       var % dimids(1:var % ndim), var % varid))
-        CALL check(nf90_put_att(ncid, var % varid, "long_name",     var % long_name))
-        CALL check(nf90_put_att(ncid, var % varid, "units",         var % units))
-        IF ( var % lod .GE. 0 )  THEN
-           CALL check(nf90_put_att(ncid, var % varid, "lod",           var % lod))
-        ENDIF 
-        CALL check(nf90_put_att(ncid, var % varid, "source",        var % source))
-        CALL check(nf90_put_att(ncid, var % varid, "_FillValue",    NF90_FILL_REAL))
-
-    END SUBROUTINE netcdf_define_variable
+ SUBROUTINE netcdf_define_variable(var, ncid)
+
+     TYPE(nc_var), INTENT(INOUT) ::  var
+     INTEGER, INTENT(IN)         ::  ncid
+
+     CALL check(nf90_def_var(ncid, var%name, NF90_FLOAT,       var%dimids(1:var%ndim), var%varid))
+     CALL check(nf90_put_att(ncid, var%varid, "long_name",     var%long_name))
+     CALL check(nf90_put_att(ncid, var%varid, "units",         var%units))
+     IF ( var%lod .GE. 0 )  THEN
+        CALL check(nf90_put_att(ncid, var%varid, "lod",           var%lod))
+     ENDIF 
+     CALL check(nf90_put_att(ncid, var%varid, "source",        var%source))
+     CALL check(nf90_put_att(ncid, var%varid, "_FillValue",    NF90_FILL_REAL))
+
+ END SUBROUTINE netcdf_define_variable
     
 
@@ -377,18 +382,18 @@
 !> for writing output variables in update_output().
 !------------------------------------------------------------------------------!
-    SUBROUTINE netcdf_get_dimensions(var, ncid)
-
-        TYPE(nc_var), INTENT(INOUT) ::  var
-        INTEGER, INTENT(IN)         ::  ncid
-        INTEGER                     ::  i
-        CHARACTER(SNAME)            ::  null
-
-        DO i = 1, var % ndim
-           CALL check(nf90_inquire_dimension(ncid, var % dimids(i), &
-                                             name = null, &
-                                             len  = var % dimlen(i)  ) )
-        ENDDO
-
-    END SUBROUTINE netcdf_get_dimensions
+ SUBROUTINE netcdf_get_dimensions(var, ncid)
+
+     TYPE(nc_var), INTENT(INOUT) ::  var
+     INTEGER, INTENT(IN)         ::  ncid
+     INTEGER                     ::  i
+     CHARACTER(SNAME)            ::  null
+
+     DO  i = 1, var%ndim
+        CALL check(nf90_inquire_dimension(ncid, var%dimids(i), &
+                                          name = null, &
+                                          len  = var%dimlen(i)  ) )
+     ENDDO
+
+ END SUBROUTINE netcdf_get_dimensions
 
 
@@ -399,65 +404,65 @@
 !> resulting settings in the 'cfg' data structure.
 !------------------------------------------------------------------------------!
-    SUBROUTINE parse_command_line_arguments( cfg )
-
-       TYPE(inifor_config), INTENT(INOUT) ::  cfg
-
-       CHARACTER(LEN=PATH)                ::  option, arg
-       INTEGER                            ::  arg_count, i
-
-       cfg % p0_is_set = .FALSE.
-       cfg % ug_defined_by_user = .FALSE.
-       cfg % vg_defined_by_user = .FALSE.
-       cfg % flow_prefix_is_set = .FALSE.
-       cfg % input_prefix_is_set = .FALSE.
-       cfg % radiation_prefix_is_set = .FALSE.
-       cfg % soil_prefix_is_set = .FALSE.
-       cfg % soilmoisture_prefix_is_set = .FALSE.
-
-       arg_count = COMMAND_ARGUMENT_COUNT()
-       IF (arg_count .GT. 0)  THEN
-
-          message = "The -clon and -clat command line options are depricated. " // &
-             "Please remove them form your inifor command and specify the " // &
-             "location of the PALM-4U origin either" // NEW_LINE(' ') // &
-             "   - by setting the namelist parameters 'longitude' and 'latitude', or" // NEW_LINE(' ') // &
-             "   - by providing a static driver netCDF file via the -static command-line option."
-
-          i = 1
-          DO WHILE (i .LE. arg_count)
-
-             CALL GET_COMMAND_ARGUMENT( i, option )
-
-             SELECT CASE( TRIM(option) )
+ SUBROUTINE parse_command_line_arguments( cfg )
+
+    TYPE(inifor_config), INTENT(INOUT) ::  cfg
+
+    CHARACTER(LEN=PATH)                ::  option, arg
+    INTEGER                            ::  arg_count, i
+
+    cfg%p0_is_set = .FALSE.
+    cfg%ug_defined_by_user = .FALSE.
+    cfg%vg_defined_by_user = .FALSE.
+    cfg%flow_prefix_is_set = .FALSE.
+    cfg%input_prefix_is_set = .FALSE.
+    cfg%radiation_prefix_is_set = .FALSE.
+    cfg%soil_prefix_is_set = .FALSE.
+    cfg%soilmoisture_prefix_is_set = .FALSE.
+
+    arg_count = COMMAND_ARGUMENT_COUNT()
+    IF (arg_count .GT. 0)  THEN
+
+       message = "The -clon and -clat command line options are depricated. " // &
+          "Please remove them form your inifor command and specify the " // &
+          "location of the PALM-4U origin either" // NEW_LINE(' ') // &
+          "   - by setting the namelist parameters 'longitude' and 'latitude', or" // NEW_LINE(' ') // &
+          "   - by providing a static driver netCDF file via the -static command-line option."
+
+       i = 1
+       DO  WHILE (i .LE. arg_count)
+
+          CALL GET_COMMAND_ARGUMENT( i, option )
+
+          SELECT CASE( TRIM(option) )
 
              CASE( '--averaging-mode' )
                 CALL get_option_argument( i, arg )
-                cfg % averaging_mode = TRIM(arg)
+                cfg%averaging_mode = TRIM(arg)
 
              CASE( '-date', '-d', '--date' )
                 CALL get_option_argument( i, arg )
-                cfg % start_date = TRIM(arg)
+                cfg%start_date = TRIM(arg)
 
              CASE( '--debug' )
-                cfg % debug = .TRUE.
+                cfg%debug = .TRUE.
 
              CASE( '-z0', '-z', '--elevation' )
                 CALL get_option_argument( i, arg )
-                READ(arg, *) cfg % z0
+                READ(arg, *) cfg%z0
 
              CASE( '-p0', '-r', '--surface-pressure' )
-                cfg % p0_is_set = .TRUE.
-                CALL get_option_argument( i, arg )
-                READ(arg, *) cfg % p0
+                cfg%p0_is_set = .TRUE.
+                CALL get_option_argument( i, arg )
+                READ(arg, *) cfg%p0
 
              CASE( '-ug', '-u', '--geostrophic-u' )
-                cfg % ug_defined_by_user = .TRUE.
-                CALL get_option_argument( i, arg )
-                READ(arg, *) cfg % ug
+                cfg%ug_defined_by_user = .TRUE.
+                CALL get_option_argument( i, arg )
+                READ(arg, *) cfg%ug
 
              CASE( '-vg', '-v', '--geostrophic-v' )
-                cfg % vg_defined_by_user = .TRUE.
-                CALL get_option_argument( i, arg )
-                READ(arg, *) cfg % vg
+                cfg%vg_defined_by_user = .TRUE.
+                CALL get_option_argument( i, arg )
+                READ(arg, *) cfg%vg
 
              CASE( '-clon', '-clat' )
@@ -466,69 +471,69 @@
              CASE( '-path', '-p', '--path' )
                 CALL get_option_argument( i, arg )
-                 cfg % input_path = TRIM(arg)
+                 cfg%input_path = TRIM(arg)
 
              CASE( '-hhl', '-l', '--hhl-file' )
                 CALL get_option_argument( i, arg )
-                cfg % hhl_file = TRIM(arg)
+                cfg%hhl_file = TRIM(arg)
 
              CASE( '--input-prefix')
                 CALL get_option_argument( i, arg )
-                cfg % input_prefix = TRIM(arg)
-                cfg % input_prefix_is_set = .TRUE.
+                cfg%input_prefix = TRIM(arg)
+                cfg%input_prefix_is_set = .TRUE.
    
              CASE( '-a', '--averaging-angle' )
                 CALL get_option_argument( i, arg )
-                READ(arg, *) cfg % averaging_angle
+                READ(arg, *) cfg%averaging_angle
 
              CASE( '-static', '-t', '--static-driver' )
                 CALL get_option_argument( i, arg )
-                cfg % static_driver_file = TRIM(arg)
+                cfg%static_driver_file = TRIM(arg)
 
              CASE( '-soil', '-s', '--soil-file')
                 CALL get_option_argument( i, arg )
-                cfg % soiltyp_file = TRIM(arg)
+                cfg%soiltyp_file = TRIM(arg)
 
              CASE( '--flow-prefix')
                 CALL get_option_argument( i, arg )
-                cfg % flow_prefix = TRIM(arg)
-                cfg % flow_prefix_is_set = .TRUE.
+                cfg%flow_prefix = TRIM(arg)
+                cfg%flow_prefix_is_set = .TRUE.
    
              CASE( '--radiation-prefix')
                 CALL get_option_argument( i, arg )
-                cfg % radiation_prefix = TRIM(arg)
-                cfg % radiation_prefix_is_set = .TRUE.
+                cfg%radiation_prefix = TRIM(arg)
+                cfg%radiation_prefix_is_set = .TRUE.
    
              CASE( '--soil-prefix')
                 CALL get_option_argument( i, arg )
-                cfg % soil_prefix = TRIM(arg)
-                cfg % soil_prefix_is_set = .TRUE.
+                cfg%soil_prefix = TRIM(arg)
+                cfg%soil_prefix_is_set = .TRUE.
    
              CASE( '--soilmoisture-prefix')
                 CALL get_option_argument( i, arg )
-                cfg % soilmoisture_prefix = TRIM(arg)
-                cfg % soilmoisture_prefix_is_set = .TRUE.
+                cfg%soilmoisture_prefix = TRIM(arg)
+                cfg%soilmoisture_prefix_is_set = .TRUE.
 
              CASE( '-o', '--output' )
                 CALL get_option_argument( i, arg )
-                cfg % output_file = TRIM(arg)
+                cfg%output_file = TRIM(arg)
 
              CASE( '-n', '--namelist' )
                 CALL get_option_argument( i, arg )
-                cfg % namelist_file = TRIM(arg)
+                cfg%namelist_file = TRIM(arg)
 
              CASE( '-mode', '-i', '--init-mode' )
                 CALL get_option_argument( i, arg )
-                cfg % ic_mode = TRIM(arg)
+                cfg%ic_mode = TRIM(arg)
 
              CASE( '-f', '--forcing-mode' )
                 CALL get_option_argument( i, arg )
-                cfg % bc_mode = TRIM(arg)
+                cfg%bc_mode = TRIM(arg)
 
              CASE( '--version' )
-                CALL print_version()
+                CALL print_version
                 STOP
 
              CASE( '--help' )
-                CALL print_version()
+                CALL print_version
                 PRINT *, ""
                 PRINT *, "For a list of command-line options have a look at the README file."
@@ -539,47 +544,47 @@
                 CALL inifor_abort('parse_command_line_arguments', message)
 
-             END SELECT
-
-             i = i + 1
-
-          ENDDO
-
-       ELSE
-            
-          message = "No arguments present, using default input and output files"
-          CALL report('parse_command_line_arguments', message)
-
-       ENDIF
-
-   END SUBROUTINE parse_command_line_arguments
+          END SELECT
+
+          i = i + 1
+
+       ENDDO
+
+    ELSE
+         
+       message = "No arguments present, using default input and output files"
+       CALL report('parse_command_line_arguments', message)
+
+    ENDIF
+
+ END SUBROUTINE parse_command_line_arguments
 
    
 
-   SUBROUTINE get_datetime_file_list( start_date_string, start_hour, end_hour, &
-                                      step_hour, input_path, prefix, suffix,   &
-                                      file_list )
-
-      CHARACTER (LEN=DATE), INTENT(IN) ::  start_date_string
-      CHARACTER (LEN=*),    INTENT(IN) ::  prefix, suffix, input_path
-      INTEGER,              INTENT(IN) ::  start_hour, end_hour, step_hour
-      CHARACTER(LEN=*), ALLOCATABLE, INTENT(INOUT) ::  file_list(:)
-
-      INTEGER             ::  number_of_intervals, hour, i
-      CHARACTER(LEN=DATE) ::  date_string
-
-      number_of_intervals = CEILING( REAL(end_hour - start_hour) / step_hour )
-      ALLOCATE( file_list(number_of_intervals + 1) )
-
-      DO i = 0, number_of_intervals
-
-         hour = start_hour + i * step_hour
-         date_string = add_hours_to(start_date_string, hour)
-
-         file_list(i+1) = TRIM(input_path) // TRIM(prefix) //                  &
-                          TRIM(date_string) // TRIM(suffix) // '.nc'
-
-      ENDDO
-
-   END SUBROUTINE get_datetime_file_list
+ SUBROUTINE get_datetime_file_list( start_date_string, start_hour, end_hour, &
+                                    step_hour, input_path, prefix, suffix,   &
+                                    file_list )
+
+    CHARACTER (LEN=DATE), INTENT(IN) ::  start_date_string
+    CHARACTER (LEN=*),    INTENT(IN) ::  prefix, suffix, input_path
+    INTEGER,              INTENT(IN) ::  start_hour, end_hour, step_hour
+    CHARACTER(LEN=*), ALLOCATABLE, INTENT(INOUT) ::  file_list(:)
+
+    INTEGER             ::  number_of_intervals, hour, i
+    CHARACTER(LEN=DATE) ::  date_string
+
+    number_of_intervals = CEILING( REAL(end_hour - start_hour) / step_hour )
+    ALLOCATE( file_list(number_of_intervals + 1) )
+
+    DO  i = 0, number_of_intervals
+
+       hour = start_hour + i * step_hour
+       date_string = add_hours_to(start_date_string, hour)
+
+       file_list(i+1) = TRIM(input_path) // TRIM(prefix) //                  &
+                        TRIM(date_string) // TRIM(suffix) // '.nc'
+
+    ENDDO
+
+ END SUBROUTINE get_datetime_file_list
 
 !------------------------------------------------------------------------------!
@@ -589,38 +594,38 @@
 !> prefixes and suffixes.
 !------------------------------------------------------------------------------!
-   SUBROUTINE get_input_file_list( start_date_string, start_hour, end_hour,    &
-                                   step_hour, input_path, prefix, suffix,      &
-                                   file_list, nocheck )
-
-      CHARACTER (LEN=DATE), INTENT(IN) ::  start_date_string
-      CHARACTER (LEN=*),    INTENT(IN) ::  prefix, suffix, input_path
-      INTEGER,              INTENT(IN) ::  start_hour, end_hour, step_hour
-      CHARACTER(LEN=*), ALLOCATABLE, INTENT(INOUT) ::  file_list(:)
-      LOGICAL, OPTIONAL, INTENT(IN)    ::  nocheck
-
-      INTEGER ::  i
-      LOGICAL ::  check_files
-
-      CALL get_datetime_file_list( start_date_string, start_hour, end_hour,    &
-                                   step_hour, input_path, prefix, suffix,      &
-                                   file_list )
-
-      check_files = .TRUE.
-      IF ( PRESENT ( nocheck ) )  THEN
-         IF ( nocheck )  check_files = .FALSE.
-      ENDIF
-
-      IF ( check_files )  THEN
-
-         tip = "Please check if you specified the correct file prefix " //     &
-               "using the options --input-prefix, --flow-prefix, etc."
-
-         DO i = 1, SIZE(file_list)
-             CALL verify_file(file_list(i), 'input', tip)
-         ENDDO
-
-      ENDIF
-
-   END SUBROUTINE get_input_file_list
+ SUBROUTINE get_input_file_list( start_date_string, start_hour, end_hour,    &
+                                 step_hour, input_path, prefix, suffix,      &
+                                 file_list, nocheck )
+
+    CHARACTER (LEN=DATE), INTENT(IN) ::  start_date_string
+    CHARACTER (LEN=*),    INTENT(IN) ::  prefix, suffix, input_path
+    INTEGER,              INTENT(IN) ::  start_hour, end_hour, step_hour
+    CHARACTER(LEN=*), ALLOCATABLE, INTENT(INOUT) ::  file_list(:)
+    LOGICAL, OPTIONAL, INTENT(IN)    ::  nocheck
+
+    INTEGER ::  i
+    LOGICAL ::  check_files
+
+    CALL get_datetime_file_list( start_date_string, start_hour, end_hour,    &
+                                 step_hour, input_path, prefix, suffix,      &
+                                 file_list )
+
+    check_files = .TRUE.
+    IF ( PRESENT ( nocheck ) )  THEN
+       IF ( nocheck )  check_files = .FALSE.
+    ENDIF
+
+    IF ( check_files )  THEN
+
+       tip = "Please check if you specified the correct file prefix " //     &
+             "using the options --input-prefix, --flow-prefix, etc."
+
+       DO  i = 1, SIZE(file_list)
+           CALL verify_file(file_list(i), 'input', tip)
+       ENDDO
+
+    ENDIF
+
+ END SUBROUTINE get_input_file_list
 
 
@@ -630,34 +635,34 @@
 !> Abort INIFOR if the given file is not present.
 !------------------------------------------------------------------------------!
-   SUBROUTINE verify_file(file_name, file_kind, tip)
-
-      CHARACTER(LEN=*), INTENT(IN)           ::  file_name, file_kind
-      CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  tip
-
-      IF (.NOT. file_present(file_name))  THEN
-
-         IF (LEN(TRIM(file_name)) == 0)  THEN
-
-            message = "No name was given for the " // TRIM(file_kind) // " file."
-
-         ELSE
-
-            message = "The " // TRIM(file_kind) // " file '" //                &
-                      TRIM(file_name) // "' was not found."
-
-            IF (PRESENT(tip))  THEN
-               message = TRIM(message) // " " // TRIM(tip)
-            ENDIF
-
-         ENDIF
-
-         CALL inifor_abort('verify_file', message)
-
-      ENDIF
-
-      message = "Set up input file name '" // TRIM(file_name) // "'"
-      CALL report('verify_file', message)
-
-   END SUBROUTINE verify_file
+ SUBROUTINE verify_file(file_name, file_kind, tip)
+
+    CHARACTER(LEN=*), INTENT(IN)           ::  file_name, file_kind
+    CHARACTER(LEN=*), INTENT(IN), OPTIONAL ::  tip
+
+    IF (.NOT. file_present(file_name))  THEN
+
+       IF (LEN(TRIM(file_name)) == 0)  THEN
+
+          message = "No name was given for the " // TRIM(file_kind) // " file."
+
+       ELSE
+
+          message = "The " // TRIM(file_kind) // " file '" //                &
+                    TRIM(file_name) // "' was not found."
+
+          IF (PRESENT(tip))  THEN
+             message = TRIM(message) // " " // TRIM(tip)
+          ENDIF
+
+       ENDIF
+
+       CALL inifor_abort('verify_file', message)
+
+    ENDIF
+
+    message = "Set up input file name '" // TRIM(file_name) // "'"
+    CALL report('verify_file', message)
+
+ END SUBROUTINE verify_file
 
 
@@ -668,12 +673,12 @@
 !> i+1 of the argument list.
 !------------------------------------------------------------------------------!
-   SUBROUTINE get_option_argument(i, arg)
-      CHARACTER(LEN=PATH), INTENT(INOUT) ::  arg
-      INTEGER, INTENT(INOUT)             ::  i
-
-      i = i + 1
-      CALL GET_COMMAND_ARGUMENT(i, arg)
-
-   END SUBROUTINE
+ SUBROUTINE get_option_argument(i, arg)
+    CHARACTER(LEN=PATH), INTENT(INOUT) ::  arg
+    INTEGER, INTENT(INOUT)             ::  i
+
+    i = i + 1
+    CALL GET_COMMAND_ARGUMENT(i, arg)
+
+ END SUBROUTINE
 
 
@@ -683,147 +688,146 @@
 !> Checks the INIFOR configuration 'cfg' for plausibility.
 !------------------------------------------------------------------------------!
-   SUBROUTINE validate_config(cfg)
-      TYPE(inifor_config), INTENT(IN) ::  cfg
-
-      CALL verify_file(cfg % hhl_file, 'HHL')
-      CALL verify_file(cfg % namelist_file, 'NAMELIST')
-      CALL verify_file(cfg % soiltyp_file, 'SOILTYP')
-
-!
-!--   Only check optional static driver file name, if it has been given.
-      IF (TRIM(cfg % static_driver_file) .NE. '')  THEN
-         CALL verify_file(cfg % static_driver_file, 'static driver')
-      ENDIF
-
-      SELECT CASE( TRIM(cfg % ic_mode) )
-      CASE( 'profile', 'volume')
-      CASE DEFAULT
-         message = "Initialization mode '" // TRIM(cfg % ic_mode) //&
-                   "' is not supported. " //&
-                   "Please select either 'profile' or 'volume', " //&
-                   "or omit the -i/--init-mode/-mode option entirely, which corresponds "//&
-                   "to the latter."
-         CALL inifor_abort( 'validate_config', message )
-      END SELECT
-
-
-      SELECT CASE( TRIM(cfg % bc_mode) )
-      CASE( 'real', 'ideal')
-      CASE DEFAULT
-         message = "Forcing mode '" // TRIM(cfg % bc_mode) //& 
-                   "' is not supported. " //&
-                   "Please select either 'real' or 'ideal', " //&
-                   "or omit the -f/--forcing-mode option entirely, which corresponds "//&
-                   "to the latter."
-         CALL inifor_abort( 'validate_config', message )
-      END SELECT
-
-      SELECT CASE( TRIM(cfg % averaging_mode) )
-      CASE( 'level' )
-      CASE( 'height' )
-         message = "Averaging mode '" // TRIM(cfg % averaging_mode) //&
-                   "' is currently not supported. " //&
-                   "Please use level-based averaging by selecting 'level', " //&
-                   "or by omitting the --averaging-mode option entirely."
-         CALL inifor_abort( 'validate_config', message )
-      CASE DEFAULT
-         message = "Averaging mode '" // TRIM(cfg % averaging_mode) //&
-                   "' is not supported. " //&
-         !          "Please select either 'height' or 'level', " //&
-         !          "or omit the --averaging-mode option entirely, which corresponds "//&
-         !          "to the latter."
-                   "Please use level-based averaging by selecting 'level', " //&
-                   "or by omitting the --averaging-mode option entirely."
-         CALL inifor_abort( 'validate_config', message )
-      END SELECT
-
-      IF ( cfg % ug_defined_by_user .NEQV. cfg % vg_defined_by_user )  THEN
-         message = "You specified only one component of the geostrophic " // &
-                   "wind. Please specify either both or none."
-         CALL inifor_abort( 'validate_config', message )
-      ENDIF
-
-   END SUBROUTINE validate_config
-
-
-   SUBROUTINE get_cosmo_grid( cfg, soil_file, rlon, rlat, hhl, hfl, depths, &
-                              d_depth, d_depth_rho_inv, phi_n, lambda_n,       &
-                              phi_equat,                                       &
-                              lonmin_cosmo, lonmax_cosmo,                      &
-                              latmin_cosmo, latmax_cosmo,                      &
-                              nlon, nlat, nlev, ndepths )
-
-      TYPE(inifor_config), INTENT(IN)                      ::  cfg
-      CHARACTER(LEN=PATH), INTENT(IN)                      ::  soil_file !< list of soil input files (temperature, moisture, <prefix>YYYYMMDDHH-soil.nc)
-      REAL(dp), DIMENSION(:), ALLOCATABLE, INTENT(OUT)     ::  rlon      !< longitudes of COSMO-DE's rotated-pole grid
-      REAL(dp), DIMENSION(:), ALLOCATABLE, INTENT(OUT)     ::  rlat      !< latitudes of COSMO-DE's rotated-pole grid
-      REAL(dp), DIMENSION(:,:,:), ALLOCATABLE, INTENT(OUT) ::  hhl       !< heights of half layers (cell faces) above sea level in COSMO-DE, read in from external file
-      REAL(dp), DIMENSION(:,:,:), ALLOCATABLE, INTENT(OUT) ::  hfl       !< heights of full layers (cell centres) above sea level in COSMO-DE, computed as arithmetic average of hhl
-      REAL(dp), DIMENSION(:), ALLOCATABLE, INTENT(OUT)     ::  depths    !< COSMO-DE's TERRA-ML soil layer depths
-      REAL(dp), DIMENSION(:), ALLOCATABLE, INTENT(OUT)     ::  d_depth
-      REAL(dp), DIMENSION(:), ALLOCATABLE, INTENT(OUT)     ::  d_depth_rho_inv
-      REAL(dp), INTENT(OUT)                                ::  phi_n
-      REAL(dp), INTENT(OUT)                                ::  phi_equat
-      REAL(dp), INTENT(OUT)                                ::  lambda_n
-      REAL(dp), INTENT(OUT)                                ::  lonmin_cosmo !< Minimunm longitude of COSMO-DE's rotated-pole grid [COSMO rotated-pole rad]
-      REAL(dp), INTENT(OUT)                                ::  lonmax_cosmo !< Maximum longitude of COSMO-DE's rotated-pole grid [COSMO rotated-pole rad]
-      REAL(dp), INTENT(OUT)                                ::  latmin_cosmo !< Minimunm latitude of COSMO-DE's rotated-pole grid [COSMO rotated-pole rad]
-      REAL(dp), INTENT(OUT)                                ::  latmax_cosmo !< Maximum latitude of COSMO-DE's rotated-pole grid [COSMO rotated-pole rad]
-      INTEGER, INTENt(OUT)                                 ::  nlon, nlat, nlev, ndepths
-
-      TYPE(nc_var) ::  cosmo_var !< COSMO dummy variable, used for reading HHL, rlon, rlat
-      INTEGER      ::  k
-
-!
-!--   Read in COSMO's heights of half layers (vertical cell faces)
-      cosmo_var % name = NC_HHL_NAME
-      CALL get_netcdf_variable( cfg % hhl_file, cosmo_var, hhl )
-      CALL get_netcdf_dim_vector( cfg % hhl_file, NC_RLON_NAME, rlon )
-      CALL get_netcdf_dim_vector( cfg % hhl_file, NC_RLAT_NAME, rlat )
-      CALL get_netcdf_dim_vector( soil_file, NC_DEPTH_NAME, depths)
- CALL run_control( 'time', 'read' )
-
-      CALL reverse( hhl )
-      nlon = SIZE( hhl, 1 )
-      nlat = SIZE( hhl, 2 )
-      nlev = SIZE( hhl, 3 )
-      ndepths = SIZE( depths )
-
- CALL run_control( 'time', 'comp' )
-
-      ALLOCATE( hfl( nlon, nlat, nlev-1 ) )
-      ALLOCATE( d_depth( ndepths ), d_depth_rho_inv( ndepths ) )
- CALL run_control('time', 'alloc')
-
-      CALL get_soil_layer_thickness( depths, d_depth )
-      d_depth_rho_inv = 1.0_dp / ( d_depth * RHO_L )
-
-!
-!--   Compute COSMO's heights of full layers (cell centres)
-      DO k = 1, nlev-1
-         hfl(:,:,k) = 0.5_dp * ( hhl(:,:,k) +                                  &
-                                 hhl(:,:,k+1) )
-      ENDDO
-!
-!--   COSMO rotated pole coordinates
-      phi_n = TO_RADIANS                                                       &
-            * get_netcdf_variable_attribute( cfg % hhl_file,                   &
-                                             NC_ROTATED_POLE_NAME,             &
-                                             NC_POLE_LATITUDE_NAME )
-
-      lambda_n = TO_RADIANS                                                    &
-               * get_netcdf_variable_attribute( cfg % hhl_file,                &
-                                                NC_ROTATED_POLE_NAME,          &
-                                                NC_POLE_LONGITUDE_NAME )
-
-      phi_equat = 90.0_dp * TO_RADIANS - phi_n
-
-      lonmin_cosmo = MINVAL( rlon ) * TO_RADIANS
-      lonmax_cosmo = MAXVAL( rlon ) * TO_RADIANS
-      latmin_cosmo = MINVAL( rlat ) * TO_RADIANS
-      latmax_cosmo = MAXVAL( rlat ) * TO_RADIANS
- CALL run_control('time', 'comp')
-
-   END SUBROUTINE get_cosmo_grid
+ SUBROUTINE validate_config(cfg)
+    TYPE(inifor_config), INTENT(IN) ::  cfg
+
+    CALL verify_file(cfg%hhl_file, 'HHL')
+    CALL verify_file(cfg%namelist_file, 'NAMELIST')
+    CALL verify_file(cfg%soiltyp_file, 'SOILTYP')
+
+!
+!-- Only check optional static driver file name, if it has been given.
+    IF (TRIM(cfg%static_driver_file) .NE. '')  THEN
+       CALL verify_file(cfg%static_driver_file, 'static driver')
+    ENDIF
+
+    SELECT CASE( TRIM(cfg%ic_mode) )
+       CASE( 'profile', 'volume')
+       CASE DEFAULT
+          message = "Initialization mode '" // TRIM(cfg%ic_mode) //&
+                    "' is not supported. " //&
+                    "Please select either 'profile' or 'volume', " //&
+                    "or omit the -i/--init-mode/-mode option entirely, which corresponds "//&
+                    "to the latter."
+          CALL inifor_abort( 'validate_config', message )
+    END SELECT
+
+    SELECT CASE( TRIM(cfg%bc_mode) )
+       CASE( 'real', 'ideal')
+       CASE DEFAULT
+          message = "Forcing mode '" // TRIM(cfg%bc_mode) //& 
+                    "' is not supported. " //&
+                    "Please select either 'real' or 'ideal', " //&
+                    "or omit the -f/--forcing-mode option entirely, which corresponds "//&
+                    "to the latter."
+          CALL inifor_abort( 'validate_config', message )
+    END SELECT
+
+    SELECT CASE( TRIM(cfg%averaging_mode) )
+       CASE( 'level' )
+       CASE( 'height' )
+          message = "Averaging mode '" // TRIM(cfg%averaging_mode) //&
+                    "' is currently not supported. " //&
+                    "Please use level-based averaging by selecting 'level', " //&
+                    "or by omitting the --averaging-mode option entirely."
+          CALL inifor_abort( 'validate_config', message )
+       CASE DEFAULT
+          message = "Averaging mode '" // TRIM(cfg%averaging_mode) //&
+                    "' is not supported. " //&
+          !          "Please select either 'height' or 'level', " //&
+          !          "or omit the --averaging-mode option entirely, which corresponds "//&
+          !          "to the latter."
+                    "Please use level-based averaging by selecting 'level', " //&
+                    "or by omitting the --averaging-mode option entirely."
+          CALL inifor_abort( 'validate_config', message )
+    END SELECT
+
+    IF ( cfg%ug_defined_by_user .NEQV. cfg%vg_defined_by_user )  THEN
+       message = "You specified only one component of the geostrophic " // &
+                 "wind. Please specify either both or none."
+       CALL inifor_abort( 'validate_config', message )
+    ENDIF
+
+ END SUBROUTINE validate_config
+
+
+ SUBROUTINE get_cosmo_grid( cfg, soil_file, rlon, rlat, hhl, hfl, depths, &
+                            d_depth, d_depth_rho_inv, phi_n, lambda_n,       &
+                            phi_equat,                                       &
+                            lonmin_cosmo, lonmax_cosmo,                      &
+                            latmin_cosmo, latmax_cosmo,                      &
+                            nlon, nlat, nlev, ndepths )
+
+    TYPE(inifor_config), INTENT(IN)                      ::  cfg
+    CHARACTER(LEN=PATH), INTENT(IN)                      ::  soil_file !< list of soil input files (temperature, moisture, <prefix>YYYYMMDDHH-soil.nc)
+    REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(OUT)     ::  rlon      !< longitudes of COSMO-DE's rotated-pole grid
+    REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(OUT)     ::  rlat      !< latitudes of COSMO-DE's rotated-pole grid
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE, INTENT(OUT) ::  hhl       !< heights of half layers (cell faces) above sea level in COSMO-DE, read in from external file
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE, INTENT(OUT) ::  hfl       !< heights of full layers (cell centres) above sea level in COSMO-DE, computed as arithmetic average of hhl
+    REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(OUT)     ::  depths    !< COSMO-DE's TERRA-ML soil layer depths
+    REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(OUT)     ::  d_depth
+    REAL(wp), DIMENSION(:), ALLOCATABLE, INTENT(OUT)     ::  d_depth_rho_inv
+    REAL(wp), INTENT(OUT)                                ::  phi_n
+    REAL(wp), INTENT(OUT)                                ::  phi_equat
+    REAL(wp), INTENT(OUT)                                ::  lambda_n
+    REAL(wp), INTENT(OUT)                                ::  lonmin_cosmo !< Minimunm longitude of COSMO-DE's rotated-pole grid [COSMO rotated-pole rad]
+    REAL(wp), INTENT(OUT)                                ::  lonmax_cosmo !< Maximum longitude of COSMO-DE's rotated-pole grid [COSMO rotated-pole rad]
+    REAL(wp), INTENT(OUT)                                ::  latmin_cosmo !< Minimunm latitude of COSMO-DE's rotated-pole grid [COSMO rotated-pole rad]
+    REAL(wp), INTENT(OUT)                                ::  latmax_cosmo !< Maximum latitude of COSMO-DE's rotated-pole grid [COSMO rotated-pole rad]
+    INTEGER, INTENt(OUT)                                 ::  nlon, nlat, nlev, ndepths
+
+    TYPE(nc_var) ::  cosmo_var !< COSMO dummy variable, used for reading HHL, rlon, rlat
+    INTEGER      ::  k
+
+!
+!-- Read in COSMO's heights of half layers (vertical cell faces)
+    cosmo_var%name = NC_HHL_NAME
+    CALL get_netcdf_variable( cfg%hhl_file, cosmo_var, hhl )
+    CALL get_netcdf_dim_vector( cfg%hhl_file, NC_RLON_NAME, rlon )
+    CALL get_netcdf_dim_vector( cfg%hhl_file, NC_RLAT_NAME, rlat )
+    CALL get_netcdf_dim_vector( soil_file, NC_DEPTH_NAME, depths)
+    CALL log_runtime( 'time', 'read' )
+
+    CALL reverse( hhl )
+    nlon = SIZE( hhl, 1 )
+    nlat = SIZE( hhl, 2 )
+    nlev = SIZE( hhl, 3 )
+    ndepths = SIZE( depths )
+
+    CALL log_runtime( 'time', 'comp' )
+
+    ALLOCATE( hfl( nlon, nlat, nlev-1 ) )
+    ALLOCATE( d_depth( ndepths ), d_depth_rho_inv( ndepths ) )
+    CALL log_runtime('time', 'alloc')
+
+    CALL get_soil_layer_thickness( depths, d_depth )
+    d_depth_rho_inv = 1.0_wp / ( d_depth * RHO_L )
+
+!
+!-- Compute COSMO's heights of full layers (cell centres)
+    DO  k = 1, nlev-1
+       hfl(:,:,k) = 0.5_wp * ( hhl(:,:,k) +                                  &
+                               hhl(:,:,k+1) )
+    ENDDO
+!
+!-- COSMO rotated pole coordinates
+    phi_n = TO_RADIANS                                                       &
+          * get_netcdf_variable_attribute( cfg%hhl_file,                   &
+                                           NC_ROTATED_POLE_NAME,             &
+                                           NC_POLE_LATITUDE_NAME )
+
+    lambda_n = TO_RADIANS                                                    &
+             * get_netcdf_variable_attribute( cfg%hhl_file,                &
+                                              NC_ROTATED_POLE_NAME,          &
+                                              NC_POLE_LONGITUDE_NAME )
+
+    phi_equat = 90.0_wp * TO_RADIANS - phi_n
+
+    lonmin_cosmo = MINVAL( rlon ) * TO_RADIANS
+    lonmax_cosmo = MAXVAL( rlon ) * TO_RADIANS
+    latmin_cosmo = MINVAL( rlat ) * TO_RADIANS
+    latmax_cosmo = MAXVAL( rlat ) * TO_RADIANS
+    CALL log_runtime('time', 'comp')
+
+ END SUBROUTINE get_cosmo_grid
 
 
@@ -856,13 +860,13 @@
 !>
 !------------------------------------------------------------------------------!
-    SUBROUTINE get_soil_layer_thickness(depths, d_depth)
-
-       REAL(dp), INTENT(IN)  ::  depths(:)
-       REAL(dp), INTENT(OUT) ::  d_depth(:)
-
-       d_depth(:) = depths(:)
-       d_depth(1) = 2.0_dp * depths(1)
-
-    END SUBROUTINE get_soil_layer_thickness
+ SUBROUTINE get_soil_layer_thickness(depths, d_depth)
+
+    REAL(wp), INTENT(IN)  ::  depths(:)
+    REAL(wp), INTENT(OUT) ::  d_depth(:)
+
+    d_depth(:) = depths(:)
+    d_depth(1) = 2.0_wp * depths(1)
+
+ END SUBROUTINE get_soil_layer_thickness
 !------------------------------------------------------------------------------!
 ! Description:
@@ -870,10 +874,10 @@
 !> Check whether the given file is present on the filesystem.
 !------------------------------------------------------------------------------!
-   LOGICAL FUNCTION file_present(filename)
-      CHARACTER(LEN=PATH), INTENT(IN) ::  filename
-
-      INQUIRE(FILE=filename, EXIST=file_present)
-
-   END FUNCTION file_present
+ LOGICAL FUNCTION file_present(filename)
+    CHARACTER(LEN=PATH), INTENT(IN) ::  filename
+
+    INQUIRE(FILE=filename, EXIST=file_present)
+
+ END FUNCTION file_present
 
 
@@ -888,29 +892,29 @@
 !> writes the actual data.
 !------------------------------------------------------------------------------!
-   SUBROUTINE setup_netcdf_dimensions(output_file, palm_grid,                  &
-                                      start_date_string, origin_lon, origin_lat)
-
-       TYPE(nc_file), INTENT(INOUT)      ::  output_file
-       TYPE(grid_definition), INTENT(IN) ::  palm_grid
-       CHARACTER (LEN=DATE), INTENT(IN)  ::  start_date_string
-       REAL(dp), INTENT(IN)              ::  origin_lon, origin_lat
-
-       CHARACTER (LEN=8)     ::  date_string
-       CHARACTER (LEN=10)    ::  time_string
-       CHARACTER (LEN=5)     ::  zone_string
-       CHARACTER (LEN=SNAME) ::  history_string
-       INTEGER               ::  ncid, nx, ny, nz, nt, dimids(3), dimvarids(3)
-       REAL(dp)              ::  z0
-
-       message = "Initializing PALM-4U dynamic driver file '" //               &
-                 TRIM(output_file % name) // "' and setting up dimensions."
-       CALL report('setup_netcdf_dimensions', message)
-
-!
-!--    Create the netCDF file as in netCDF-4/HDF5 format if __netcdf4 preprocessor flag is given
+ SUBROUTINE setup_netcdf_dimensions( output_file, palm_grid,                  &
+                                     start_date_string, origin_lon, origin_lat )
+
+    TYPE(nc_file), INTENT(INOUT)      ::  output_file
+    TYPE(grid_definition), INTENT(IN) ::  palm_grid
+    CHARACTER (LEN=DATE), INTENT(IN)  ::  start_date_string
+    REAL(wp), INTENT(IN)              ::  origin_lon, origin_lat
+
+    CHARACTER (LEN=8)     ::  date_string
+    CHARACTER (LEN=10)    ::  time_string
+    CHARACTER (LEN=5)     ::  zone_string
+    CHARACTER (LEN=SNAME) ::  history_string
+    INTEGER               ::  ncid, nx, ny, nz, nt, dimids(3), dimvarids(3)
+    REAL(wp)              ::  z0
+
+    message = "Initializing PALM-4U dynamic driver file '" //               &
+              TRIM(output_file%name) // "' and setting up dimensions."
+    CALL report('setup_netcdf_dimensions', message)
+
+!
+!-- Create the netCDF file as in netCDF-4/HDF5 format if __netcdf4 preprocessor flag is given
 #if defined( __netcdf4 )
-       CALL check(nf90_create(TRIM(output_file % name), OR(NF90_CLOBBER, NF90_HDF5), ncid))
+    CALL check(nf90_create(TRIM(output_file%name), OR(NF90_CLOBBER, NF90_HDF5), ncid))
 #else
-       CALL check(nf90_create(TRIM(output_file % name), NF90_CLOBBER, ncid))
+    CALL check(nf90_create(TRIM(output_file%name), NF90_CLOBBER, ncid))
 #endif
 
@@ -918,9 +922,9 @@
 !- Section 1: Define NetCDF dimensions and coordinates
 !------------------------------------------------------------------------------
-       nt = SIZE(output_file % time)
-       nx = palm_grid % nx
-       ny = palm_grid % ny
-       nz = palm_grid % nz
-       z0 = palm_grid % z0
+    nt = SIZE(output_file%time)
+    nx = palm_grid%nx
+    ny = palm_grid%ny
+    nz = palm_grid%nz
+    z0 = palm_grid%z0
 
 
@@ -929,25 +933,25 @@
 !- Section 2: Write global NetCDF attributes
 !------------------------------------------------------------------------------
-       CALL date_and_time(DATE=date_string, TIME=time_string, ZONE=zone_string)
-       history_string =                                                        &
-           'Created on '// date_string      //                                 &
-           ' at '       // time_string(1:2) // ':' // time_string(3:4) //      &
-           ' (UTC'      // zone_string // ')'
-
-       CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'title',          'PALM input file for scenario ...'))
-       CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'institution',    'Deutscher Wetterdienst, Offenbach'))
-       CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'author',         'Eckhard Kadasch, eckhard.kadasch@dwd.de'))
-       CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'history',        TRIM(history_string)))
-       CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'references',     '--'))
-       CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'comment',        '--'))
-       CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'origin_lat',     TRIM(real_to_str(origin_lat*TO_DEGREES, '(F18.13)'))))
-       CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'origin_lon',     TRIM(real_to_str(origin_lon*TO_DEGREES, '(F18.13)'))))
-!
-!--    FIXME: This is the elevation relative to COSMO-DE/D2 sea level and does
-!--    FIXME: not necessarily comply with DHHN2016 (c.f. PALM Input Data
-!--    FIXME: Standard v1.9., origin_z)
-       CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'origin_z',       TRIM(real_to_str(z0, '(F18.13)'))))
-       CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'inifor_version', TRIM(VERSION)))
-       CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'palm_version',   '--'))
+    CALL date_and_time(DATE=date_string, TIME=time_string, ZONE=zone_string)
+    history_string =                                                        &
+        'Created on '// date_string      //                                 &
+        ' at '       // time_string(1:2) // ':' // time_string(3:4) //      &
+        ' (UTC'      // zone_string // ')'
+
+    CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'title',          'PALM input file for scenario ...'))
+    CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'institution',    'Deutscher Wetterdienst, Offenbach'))
+    CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'author',         'Eckhard Kadasch, eckhard.kadasch@dwd.de'))
+    CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'history',        TRIM(history_string)))
+    CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'references',     '--'))
+    CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'comment',        '--'))
+    CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'origin_lat',     TRIM(real_to_str(origin_lat*TO_DEGREES, '(F18.13)'))))
+    CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'origin_lon',     TRIM(real_to_str(origin_lon*TO_DEGREES, '(F18.13)'))))
+!
+!-- FIXME: This is the elevation relative to COSMO-DE/D2 sea level and does
+!-- FIXME: not necessarily comply with DHHN2016 (c.f. PALM Input Data
+!-- FIXME: Standard v1.9., origin_z)
+    CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'origin_z',       TRIM(real_to_str(z0, '(F18.13)'))))
+    CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'inifor_version', TRIM(VERSION)))
+    CALL check(nf90_put_att(ncid, NF90_GLOBAL, 'palm_version',   '--'))
 
 !
@@ -957,47 +961,47 @@
 !------------------------------------------------------------------------------
 !
-!--    reset dimids first
-       dimids = (/0, 0, 0/)
-       CALL check( nf90_def_dim(ncid, "x", nx+1, dimids(1)) )
-       CALL check( nf90_def_dim(ncid, "y", ny+1, dimids(2)) )
-       CALL check( nf90_def_dim(ncid, "z", nz, dimids(3)) )
-!
-!--    save dimids for later
-       output_file % dimids_scl = dimids 
-
-!
-!--    reset dimvarids first
-       dimvarids = (/0, 0, 0/)
-       CALL check(nf90_def_var(ncid, "x", NF90_FLOAT, dimids(1), dimvarids(1)))
-       CALL check(nf90_put_att(ncid, dimvarids(1), "standard_name", "x coordinate of cell centers"))
-       CALL check(nf90_put_att(ncid, dimvarids(1), "units", "m"))
-
-       CALL check(nf90_def_var(ncid, "y", NF90_FLOAT, dimids(2), dimvarids(2)))
-       CALL check(nf90_put_att(ncid, dimvarids(2), "standard_name", "y coordinate of cell centers"))
-       CALL check(nf90_put_att(ncid, dimvarids(2), "units", "m"))
-
-       CALL check(nf90_def_var(ncid, "z", NF90_FLOAT, dimids(3), dimvarids(3)))
-       CALL check(nf90_put_att(ncid, dimvarids(3), "standard_name", "z coordinate of cell centers"))
-       CALL check(nf90_put_att(ncid, dimvarids(3), "units", "m"))
-!
-!--    save dimvarids for later
-       output_file % dimvarids_scl = dimvarids
-
-!
-!--    overwrite third dimid with the one of depth
-       CALL check(nf90_def_dim(ncid, "zsoil", SIZE(palm_grid % depths), dimids(3)) )
-!
-!--    save dimids for later
-       output_file % dimids_soil = dimids
-
-!
-!--    overwrite third dimvarid with the one of depth
-       CALL check(nf90_def_var(ncid, "zsoil", NF90_FLOAT, output_file % dimids_soil(3), dimvarids(3)))
-       CALL check(nf90_put_att(ncid, dimvarids(3), "standard_name", "depth_below_land"))
-       CALL check(nf90_put_att(ncid, dimvarids(3), "positive", "down"))
-       CALL check(nf90_put_att(ncid, dimvarids(3), "units", "m"))
-!
-!--    save dimvarids for later
-       output_file % dimvarids_soil = dimvarids
+!-- reset dimids first
+    dimids = (/0, 0, 0/)
+    CALL check( nf90_def_dim(ncid, "x", nx+1, dimids(1)) )
+    CALL check( nf90_def_dim(ncid, "y", ny+1, dimids(2)) )
+    CALL check( nf90_def_dim(ncid, "z", nz, dimids(3)) )
+!
+!-- save dimids for later
+    output_file%dimids_scl = dimids 
+
+!
+!-- reset dimvarids first
+    dimvarids = (/0, 0, 0/)
+    CALL check(nf90_def_var(ncid, "x", NF90_FLOAT, dimids(1), dimvarids(1)))
+    CALL check(nf90_put_att(ncid, dimvarids(1), "standard_name", "x coordinate of cell centers"))
+    CALL check(nf90_put_att(ncid, dimvarids(1), "units", "m"))
+
+    CALL check(nf90_def_var(ncid, "y", NF90_FLOAT, dimids(2), dimvarids(2)))
+    CALL check(nf90_put_att(ncid, dimvarids(2), "standard_name", "y coordinate of cell centers"))
+    CALL check(nf90_put_att(ncid, dimvarids(2), "units", "m"))
+
+    CALL check(nf90_def_var(ncid, "z", NF90_FLOAT, dimids(3), dimvarids(3)))
+    CALL check(nf90_put_att(ncid, dimvarids(3), "standard_name", "z coordinate of cell centers"))
+    CALL check(nf90_put_att(ncid, dimvarids(3), "units", "m"))
+!
+!-- save dimvarids for later
+    output_file%dimvarids_scl = dimvarids
+
+!
+!-- overwrite third dimid with the one of depth
+    CALL check(nf90_def_dim(ncid, "zsoil", SIZE(palm_grid%depths), dimids(3)) )
+!
+!-- save dimids for later
+    output_file%dimids_soil = dimids
+
+!
+!-- overwrite third dimvarid with the one of depth
+    CALL check(nf90_def_var(ncid, "zsoil", NF90_FLOAT, output_file%dimids_soil(3), dimvarids(3)))
+    CALL check(nf90_put_att(ncid, dimvarids(3), "standard_name", "depth_below_land"))
+    CALL check(nf90_put_att(ncid, dimvarids(3), "positive", "down"))
+    CALL check(nf90_put_att(ncid, dimvarids(3), "units", "m"))
+!
+!-- save dimvarids for later
+    output_file%dimvarids_soil = dimvarids
 !
 !------------------------------------------------------------------------------
@@ -1005,30 +1009,30 @@
 !------------------------------------------------------------------------------
 !
-!--    reset dimids first
-       dimids = (/0, 0, 0/)
-       CALL check(nf90_def_dim(ncid, "xu", nx, dimids(1)) )
-       CALL check(nf90_def_dim(ncid, "yv", ny, dimids(2)) )
-       CALL check(nf90_def_dim(ncid, "zw", nz-1, dimids(3)) )
-!
-!--    save dimids for later
-       output_file % dimids_vel = dimids
-
-!
-!--    reset dimvarids first
-       dimvarids = (/0, 0, 0/)
-       CALL check(nf90_def_var(ncid, "xu", NF90_FLOAT, dimids(1), dimvarids(1)))
-       CALL check(nf90_put_att(ncid, dimvarids(1), "standard_name", "x coordinate of cell faces"))
-       CALL check(nf90_put_att(ncid, dimvarids(1), "units", "m"))
-
-       CALL check(nf90_def_var(ncid, "yv", NF90_FLOAT, dimids(2), dimvarids(2)))
-       CALL check(nf90_put_att(ncid, dimvarids(2), "standard_name", "y coordinate of cell faces"))
-       CALL check(nf90_put_att(ncid, dimvarids(2), "units", "m"))
-
-       CALL check(nf90_def_var(ncid, "zw", NF90_FLOAT, dimids(3), dimvarids(3)))
-       CALL check(nf90_put_att(ncid, dimvarids(3), "standard_name", "z coordinate of cell faces"))
-       CALL check(nf90_put_att(ncid, dimvarids(3), "units", "m"))
-!
-!--    save dimvarids for later
-       output_file % dimvarids_vel = dimvarids
+!-- reset dimids first
+    dimids = (/0, 0, 0/)
+    CALL check(nf90_def_dim(ncid, "xu", nx, dimids(1)) )
+    CALL check(nf90_def_dim(ncid, "yv", ny, dimids(2)) )
+    CALL check(nf90_def_dim(ncid, "zw", nz-1, dimids(3)) )
+!
+!-- save dimids for later
+    output_file%dimids_vel = dimids
+
+!
+!-- reset dimvarids first
+    dimvarids = (/0, 0, 0/)
+    CALL check(nf90_def_var(ncid, "xu", NF90_FLOAT, dimids(1), dimvarids(1)))
+    CALL check(nf90_put_att(ncid, dimvarids(1), "standard_name", "x coordinate of cell faces"))
+    CALL check(nf90_put_att(ncid, dimvarids(1), "units", "m"))
+
+    CALL check(nf90_def_var(ncid, "yv", NF90_FLOAT, dimids(2), dimvarids(2)))
+    CALL check(nf90_put_att(ncid, dimvarids(2), "standard_name", "y coordinate of cell faces"))
+    CALL check(nf90_put_att(ncid, dimvarids(2), "units", "m"))
+
+    CALL check(nf90_def_var(ncid, "zw", NF90_FLOAT, dimids(3), dimvarids(3)))
+    CALL check(nf90_put_att(ncid, dimvarids(3), "standard_name", "z coordinate of cell faces"))
+    CALL check(nf90_put_att(ncid, dimvarids(3), "units", "m"))
+!
+!-- save dimvarids for later
+    output_file%dimvarids_vel = dimvarids
 
 !
@@ -1036,14 +1040,14 @@
 !- Section 2c: Define time dimension
 !------------------------------------------------------------------------------
-       CALL check(nf90_def_dim(ncid, "time", nt, output_file % dimid_time) )
-       CALL check(nf90_def_var(ncid, "time", NF90_FLOAT, &
-                                             output_file % dimid_time, &
-                                             output_file % dimvarid_time))
-       CALL check(nf90_put_att(ncid, output_file % dimvarid_time, "standard_name", "time"))
-       CALL check(nf90_put_att(ncid, output_file % dimvarid_time, "long_name", "time"))
-       CALL check(nf90_put_att(ncid, output_file % dimvarid_time, "units",     &
-                               "seconds since " // start_date_string // " UTC"))
-
-       CALL check(nf90_enddef(ncid))
+    CALL check(nf90_def_dim(ncid, "time", nt, output_file%dimid_time) )
+    CALL check(nf90_def_var(ncid, "time", NF90_FLOAT, &
+                                          output_file%dimid_time, &
+                                          output_file%dimvarid_time))
+    CALL check(nf90_put_att(ncid, output_file%dimvarid_time, "standard_name", "time"))
+    CALL check(nf90_put_att(ncid, output_file%dimvarid_time, "long_name", "time"))
+    CALL check(nf90_put_att(ncid, output_file%dimvarid_time, "units",     &
+                            "seconds since " // start_date_string // " UTC"))
+
+    CALL check(nf90_enddef(ncid))
 
 !
@@ -1051,25 +1055,25 @@
 !- Section 3: Write grid coordinates
 !------------------------------------------------------------------------------
-       CALL check(nf90_put_var(ncid, output_file % dimvarids_scl(1), palm_grid%x))
-       CALL check(nf90_put_var(ncid, output_file % dimvarids_scl(2), palm_grid%y))
-       CALL check(nf90_put_var(ncid, output_file % dimvarids_scl(3), palm_grid%z))
-
-       CALL check(nf90_put_var(ncid, output_file % dimvarids_vel(1), palm_grid%xu))
-       CALL check(nf90_put_var(ncid, output_file % dimvarids_vel(2), palm_grid%yv))
-       CALL check(nf90_put_var(ncid, output_file % dimvarids_vel(3), palm_grid%zw))
-
-!
-!--    TODO Read in soil depths from input file before this.
-       CALL check(nf90_put_var(ncid, output_file % dimvarids_soil(3), palm_grid%depths))
-
-!
-!--    Write time vector
-       CALL check(nf90_put_var(ncid, output_file % dimvarid_time, output_file % time))
-
-!
-!--    Close the file
-       CALL check(nf90_close(ncid))
-
-    END SUBROUTINE setup_netcdf_dimensions
+    CALL check(nf90_put_var(ncid, output_file%dimvarids_scl(1), palm_grid%x))
+    CALL check(nf90_put_var(ncid, output_file%dimvarids_scl(2), palm_grid%y))
+    CALL check(nf90_put_var(ncid, output_file%dimvarids_scl(3), palm_grid%z))
+
+    CALL check(nf90_put_var(ncid, output_file%dimvarids_vel(1), palm_grid%xu))
+    CALL check(nf90_put_var(ncid, output_file%dimvarids_vel(2), palm_grid%yv))
+    CALL check(nf90_put_var(ncid, output_file%dimvarids_vel(3), palm_grid%zw))
+
+!
+!-- TODO Read in soil depths from input file before this.
+    CALL check(nf90_put_var(ncid, output_file%dimvarids_soil(3), palm_grid%depths))
+
+!
+!-- Write time vector
+    CALL check(nf90_put_var(ncid, output_file%dimvarid_time, output_file%time))
+
+!
+!-- Close the file
+    CALL check(nf90_close(ncid))
+
+ END SUBROUTINE setup_netcdf_dimensions
 
 
@@ -1079,44 +1083,44 @@
 !> Defines the netCDF variables to be written to the dynamic driver file
 !------------------------------------------------------------------------------!
-    SUBROUTINE setup_netcdf_variables(filename, output_variable_table)
-
-       CHARACTER (LEN=*), INTENT(IN)        ::  filename
-       TYPE(nc_var), INTENT(INOUT), TARGET  ::  output_variable_table(:)
-
-       TYPE(nc_var), POINTER                ::  var
-       INTEGER                              ::  i, ncid
-       LOGICAL                              ::  to_be_written
-
-       message = "Defining variables in dynamic driver '" // TRIM(filename) // "'."
-       CALL report('setup_netcdf_variables', message)
-
-       CALL check(nf90_open(TRIM(filename), NF90_WRITE, ncid))
-       CALL check(nf90_redef(ncid))
-
-       DO i = 1, SIZE(output_variable_table)
-
-          var => output_variable_table(i)
-
-          !to_be_written = ( var % to_be_processed  .AND. .NOT. var % is_internal) .OR. &
-          !                ( var % is_internal  .AND.  debug )
-          to_be_written = ( var % to_be_processed  .AND. .NOT. var % is_internal)
-
-          IF ( to_be_written )  THEN
-             message = "  variable #" // TRIM(str(i)) // " '" // TRIM(var%name) // "'."
-             CALL report('setup_netcdf_variables', message)
-
-             CALL netcdf_define_variable(var, ncid)
-             CALL netcdf_get_dimensions(var, ncid)
-          ENDIF
-           
-       ENDDO
-
-       CALL check(nf90_enddef(ncid))
-       CALL check(nf90_close(ncid))
-
-       message = "Dynamic driver '" // TRIM(filename) // "' initialized successfully."
-       CALL report('setup_netcdf_variables', message)
-
-    END SUBROUTINE setup_netcdf_variables
+ SUBROUTINE setup_netcdf_variables(filename, output_variable_table)
+
+    CHARACTER (LEN=*), INTENT(IN)        ::  filename
+    TYPE(nc_var), INTENT(INOUT), TARGET  ::  output_variable_table(:)
+
+    TYPE(nc_var), POINTER                ::  var
+    INTEGER                              ::  i, ncid
+    LOGICAL                              ::  to_be_written
+
+    message = "Defining variables in dynamic driver '" // TRIM(filename) // "'."
+    CALL report('setup_netcdf_variables', message)
+
+    CALL check(nf90_open(TRIM(filename), NF90_WRITE, ncid))
+    CALL check(nf90_redef(ncid))
+
+    DO  i = 1, SIZE(output_variable_table)
+
+       var => output_variable_table(i)
+
+       !to_be_written = ( var%to_be_processed  .AND. .NOT. var%is_internal) .OR. &
+       !                ( var%is_internal  .AND.  debug )
+       to_be_written = ( var%to_be_processed  .AND. .NOT. var%is_internal)
+
+       IF ( to_be_written )  THEN
+          message = "  variable #" // TRIM(str(i)) // " '" // TRIM(var%name) // "'."
+          CALL report('setup_netcdf_variables', message)
+
+          CALL netcdf_define_variable(var, ncid)
+          CALL netcdf_get_dimensions(var, ncid)
+       ENDIF
+        
+    ENDDO
+
+    CALL check(nf90_enddef(ncid))
+    CALL check(nf90_close(ncid))
+
+    message = "Dynamic driver '" // TRIM(filename) // "' initialized successfully."
+    CALL report('setup_netcdf_variables', message)
+
+ END SUBROUTINE setup_netcdf_variables
 
 
@@ -1134,17 +1138,17 @@
 !> record netCDF IDs in the 'in_var_list()' member variable.
 !------------------------------------------------------------------------------!
-    SUBROUTINE read_input_variables(group, iter, buffer)
-       TYPE(io_group), INTENT(INOUT), TARGET       ::  group
-       INTEGER, INTENT(IN)                         ::  iter
-       TYPE(container), ALLOCATABLE, INTENT(INOUT) ::  buffer(:)
-       INTEGER                                     ::  hour, buf_id
-       TYPE(nc_var), POINTER                       ::  input_var
-       CHARACTER(LEN=PATH), POINTER                ::  input_file
-       INTEGER                                     ::  ivar, nbuffers
-
-       message = "Reading data for I/O group '" // TRIM(group % in_var_list(1) % name) // "'."
-       CALL report('read_input_variables', message)
-
-       input_file => group % in_files(iter)
+ SUBROUTINE read_input_variables(group, iter, buffer)
+    TYPE(io_group), INTENT(INOUT), TARGET       ::  group
+    INTEGER, INTENT(IN)                         ::  iter
+    TYPE(container), ALLOCATABLE, INTENT(INOUT) ::  buffer(:)
+    INTEGER                                     ::  hour, buf_id
+    TYPE(nc_var), POINTER                       ::  input_var
+    CHARACTER(LEN=PATH), POINTER                ::  input_file
+    INTEGER                                     ::  ivar, nbuffers
+
+    message = "Reading data for I/O group '" // TRIM(group%in_var_list(1)%name) // "'."
+    CALL report('read_input_variables', message)
+
+    input_file => group%in_files(iter)
 
 !
@@ -1153,77 +1157,77 @@
 !------------------------------------------------------------------------------
 !
-!--    radiation budgets, precipitation
-       IF (group % kind == 'running average' .OR.                              &
-           group % kind == 'accumulated')  THEN
-
-          IF (SIZE(group % in_var_list) .GT. 1 ) THEN
-             message = "I/O groups may not contain more than one " // & 
-                       "accumulated variable. Group '" // TRIM(group % kind) //&
-                       "' contains " //                                        &
-                       TRIM( str(SIZE(group % in_var_list)) ) // "."
-             CALL inifor_abort('read_input_variables | accumulation', message)
+!-- radiation budgets, precipitation
+    IF (group%kind == 'running average' .OR.                              &
+        group%kind == 'accumulated')  THEN
+
+       IF (SIZE(group%in_var_list) .GT. 1 ) THEN
+          message = "I/O groups may not contain more than one " // & 
+                    "accumulated variable. Group '" // TRIM(group%kind) //&
+                    "' contains " //                                        &
+                    TRIM( str(SIZE(group%in_var_list)) ) // "."
+          CALL inifor_abort('read_input_variables | accumulation', message)
+       ENDIF
+
+!
+!--    use two buffer arrays
+       nbuffers = 2
+       IF ( .NOT. ALLOCATED( buffer ) )  ALLOCATE( buffer(nbuffers) )
+
+!
+!--    hour of the day
+       hour = iter - 1
+!
+!--    chose correct buffer array
+       buf_id = select_buffer(hour)
+
+       CALL log_runtime('time', 'read')
+       IF ( ALLOCATED(buffer(buf_id)%array) )  DEALLOCATE(buffer(buf_id)%array)
+       CALL log_runtime('time', 'alloc')
+
+       input_var => group%in_var_list(1)
+       CALL get_netcdf_variable(input_file, input_var, buffer(buf_id)%array)
+       CALL report('read_input_variables', "Read accumulated " // TRIM(group%in_var_list(1)%name)) 
+
+       IF ( input_var%is_upside_down )  CALL reverse(buffer(buf_id)%array)
+       CALL log_runtime('time', 'comp')
+          
+!------------------------------------------------------------------------------
+!- Section 2: Load input buffers for normal I/O groups
+!------------------------------------------------------------------------------
+    ELSE
+
+!
+!--    Allocate one input buffer per input_variable. If more quantities
+!--    have to be computed than input variables exist in this group,
+!--    allocate more buffers. For instance, in the thermodynamics group,
+!--    there are three input variabels (PP, T, Qv) and four quantities
+!--    necessart (P, Theta, Rho, qv) for the corresponding output fields
+!--    (p0, Theta, qv, ug, and vg)
+       nbuffers = MAX( group%n_inputs, group%n_output_quantities )
+       ALLOCATE( buffer(nbuffers) )
+       CALL log_runtime('time', 'alloc')
+       
+!
+!--    Read in all input variables, leave extra buffers-if any-untouched.
+       DO  ivar = 1, group%n_inputs
+
+          input_var => group%in_var_list(ivar)
+
+!
+!         Check wheather P or PP is present in input file
+          IF (input_var%name == 'P')  THEN
+             input_var%name = TRIM( get_pressure_varname(input_file) )
+          CALL log_runtime('time', 'read')
           ENDIF
 
-!
-!--       use two buffer arrays
-          nbuffers = 2
-          IF ( .NOT. ALLOCATED( buffer ) )  ALLOCATE( buffer(nbuffers) )
-
-!
-!--       hour of the day
-          hour = iter - 1
-!
-!--       chose correct buffer array
-          buf_id = select_buffer(hour)
-
- CALL run_control('time', 'read')
-          IF ( ALLOCATED(buffer(buf_id) % array) )  DEALLOCATE(buffer(buf_id) % array)
- CALL run_control('time', 'alloc')
-
-          input_var => group % in_var_list(1)
-          CALL get_netcdf_variable(input_file, input_var, buffer(buf_id) % array)
-          CALL report('read_input_variables', "Read accumulated " // TRIM(group % in_var_list(1) % name)) 
-
-          IF ( input_var % is_upside_down )  CALL reverse(buffer(buf_id) % array)
- CALL run_control('time', 'comp')
-          
-!------------------------------------------------------------------------------
-!- Section 2: Load input buffers for normal I/O groups
-!------------------------------------------------------------------------------
-       ELSE
-
-!
-!--       Allocate one input buffer per input_variable. If more quantities
-!--       have to be computed than input variables exist in this group,
-!--       allocate more buffers. For instance, in the thermodynamics group,
-!--       there are three input variabels (PP, T, Qv) and four quantities
-!--       necessart (P, Theta, Rho, qv) for the corresponding output fields
-!--       (p0, Theta, qv, ug, and vg)
-          nbuffers = MAX( group % n_inputs, group % n_output_quantities )
-          ALLOCATE( buffer(nbuffers) )
- CALL run_control('time', 'alloc')
-          
-!
-!--       Read in all input variables, leave extra buffers-if any-untouched.
-          DO ivar = 1, group % n_inputs
-
-             input_var => group % in_var_list(ivar)
-
-!
-!            Check wheather P or PP is present in input file
-             IF (input_var % name == 'P')  THEN
-                input_var % name = TRIM( get_pressure_varname(input_file) )
- CALL run_control('time', 'read')
-             ENDIF
-
-             CALL get_netcdf_variable(input_file, input_var, buffer(ivar) % array)
-
-             IF ( input_var % is_upside_down )  CALL reverse(buffer(ivar) % array)
- CALL run_control('time', 'comp')
-
-          ENDDO
-       ENDIF
-
-    END SUBROUTINE read_input_variables
+          CALL get_netcdf_variable(input_file, input_var, buffer(ivar)%array)
+
+          IF ( input_var%is_upside_down )  CALL reverse(buffer(ivar)%array)
+          CALL log_runtime('time', 'comp')
+
+       ENDDO
+    ENDIF
+
+ END SUBROUTINE read_input_variables
 
 
@@ -1234,12 +1238,12 @@
 !> depending on the current hour.
 !------------------------------------------------------------------------------!
-    INTEGER FUNCTION select_buffer(hour)
-       INTEGER, INTENT(IN) ::  hour
-       INTEGER             ::  step
-
-       select_buffer = 0
-       step = MODULO(hour, 3) + 1
-
-       SELECT CASE(step)
+ INTEGER FUNCTION select_buffer(hour)
+    INTEGER, INTENT(IN) ::  hour
+    INTEGER             ::  step
+
+    select_buffer = 0
+    step = MODULO(hour, 3) + 1
+
+    SELECT CASE(step)
        CASE(1, 3)
            select_buffer = 1
@@ -1249,6 +1253,6 @@
            message = "Invalid step '" // TRIM(str(step))
            CALL inifor_abort('select_buffer', message)
-       END SELECT
-    END FUNCTION select_buffer
+    END SELECT
+ END FUNCTION select_buffer
 
 
@@ -1259,57 +1263,57 @@
 !> perturbation, 'PP', and returns the appropriate string.
 !------------------------------------------------------------------------------!
-    CHARACTER(LEN=2) FUNCTION get_pressure_varname(input_file) RESULT(var)
-       CHARACTER(LEN=*) ::  input_file
-       INTEGER          ::  ncid, varid
-
-       CALL check(nf90_open( TRIM(input_file), NF90_NOWRITE, ncid ))
-       IF ( nf90_inq_varid( ncid, 'P', varid ) .EQ. NF90_NOERR )  THEN
-
-          var = 'P'
-
-       ELSE IF ( nf90_inq_varid( ncid, 'PP', varid ) .EQ. NF90_NOERR )  THEN
-
-          var = 'PP'
-          CALL report('get_pressure_var', 'Using PP instead of P')
-
-       ELSE
-
-          message = "Failed to read '" // TRIM(var) // &
-                    "' from file '" // TRIM(input_file) // "'."
-          CALL inifor_abort('get_pressure_var', message)
-
-       ENDIF
-
+ CHARACTER(LEN=2) FUNCTION get_pressure_varname(input_file) RESULT(var)
+    CHARACTER(LEN=*) ::  input_file
+    INTEGER          ::  ncid, varid
+
+    CALL check(nf90_open( TRIM(input_file), NF90_NOWRITE, ncid ))
+    IF ( nf90_inq_varid( ncid, 'P', varid ) .EQ. NF90_NOERR )  THEN
+
+       var = 'P'
+
+    ELSE IF ( nf90_inq_varid( ncid, 'PP', varid ) .EQ. NF90_NOERR )  THEN
+
+       var = 'PP'
+       CALL report('get_pressure_var', 'Using PP instead of P')
+
+    ELSE
+
+       message = "Failed to read '" // TRIM(var) // &
+                 "' from file '" // TRIM(input_file) // "'."
+       CALL inifor_abort('get_pressure_var', message)
+
+    ENDIF
+
+    CALL check(nf90_close(ncid))
+
+ END FUNCTION get_pressure_varname
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Read the given global attribute form the given netCDF file.
+!------------------------------------------------------------------------------!
+ FUNCTION get_netcdf_attribute(filename, attribute) RESULT(attribute_value)
+
+    CHARACTER(LEN=*), INTENT(IN) ::  filename, attribute
+    REAL(wp)                     ::  attribute_value
+
+    INTEGER                      ::  ncid
+
+    IF ( nf90_open( TRIM(filename), NF90_NOWRITE, ncid ) == NF90_NOERR )  THEN
+
+       CALL check(nf90_get_att(ncid, NF90_GLOBAL, TRIM(attribute), attribute_value))
        CALL check(nf90_close(ncid))
 
-    END FUNCTION get_pressure_varname
-
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Read the given global attribute form the given netCDF file.
-!------------------------------------------------------------------------------!
-    FUNCTION get_netcdf_attribute(filename, attribute) RESULT(attribute_value)
-
-       CHARACTER(LEN=*), INTENT(IN) ::  filename, attribute
-       REAL(dp)                     ::  attribute_value
-
-       INTEGER                      ::  ncid
-
-       IF ( nf90_open( TRIM(filename), NF90_NOWRITE, ncid ) == NF90_NOERR )  THEN
-
-          CALL check(nf90_get_att(ncid, NF90_GLOBAL, TRIM(attribute), attribute_value))
-          CALL check(nf90_close(ncid))
-
-       ELSE
-
-          message = "Failed to read '" // TRIM(attribute) // &
-                    "' from file '" // TRIM(filename) // "'."
-          CALL inifor_abort('get_netcdf_attribute', message)
-
-       ENDIF
-
-    END FUNCTION get_netcdf_attribute
+    ELSE
+
+       message = "Failed to read '" // TRIM(attribute) // &
+                 "' from file '" // TRIM(filename) // "'."
+       CALL inifor_abort('get_netcdf_attribute', message)
+
+    ENDIF
+
+ END FUNCTION get_netcdf_attribute
 
 
@@ -1319,28 +1323,28 @@
 !> Read the attribute of the given variable form the given netCDF file.
 !------------------------------------------------------------------------------!
-    FUNCTION get_netcdf_variable_attribute(filename, varname, attribute)       &
-       RESULT(attribute_value)
-
-       CHARACTER(LEN=*), INTENT(IN) ::  filename, varname, attribute
-       REAL(dp)                     ::  attribute_value
-
-       INTEGER                      ::  ncid, varid
-
-       IF ( nf90_open( TRIM(filename), NF90_NOWRITE, ncid ) == NF90_NOERR )  THEN
-
-          CALL check( nf90_inq_varid( ncid, TRIM( varname ), varid ) )
-          CALL check( nf90_get_att( ncid, varid, TRIM( attribute ),            &
-                      attribute_value ) )
-          CALL check( nf90_close( ncid ) )
-
-       ELSE
-
-          message = "Failed to read '" // TRIM( varname ) // ":" //            &
-                    TRIM( attribute ) // "' from file '" // TRIM(filename) // "'."
-          CALL inifor_abort('get_netcdf_variable_attribute', message)
-
-       ENDIF
-
-    END FUNCTION get_netcdf_variable_attribute
+ FUNCTION get_netcdf_variable_attribute(filename, varname, attribute)       &
+    RESULT(attribute_value)
+
+    CHARACTER(LEN=*), INTENT(IN) ::  filename, varname, attribute
+    REAL(wp)                     ::  attribute_value
+
+    INTEGER                      ::  ncid, varid
+
+    IF ( nf90_open( TRIM(filename), NF90_NOWRITE, ncid ) == NF90_NOERR )  THEN
+
+       CALL check( nf90_inq_varid( ncid, TRIM( varname ), varid ) )
+       CALL check( nf90_get_att( ncid, varid, TRIM( attribute ),            &
+                   attribute_value ) )
+       CALL check( nf90_close( ncid ) )
+
+    ELSE
+
+       message = "Failed to read '" // TRIM( varname ) // ":" //            &
+                 TRIM( attribute ) // "' from file '" // TRIM(filename) // "'."
+       CALL inifor_abort('get_netcdf_variable_attribute', message)
+
+    ENDIF
+
+ END FUNCTION get_netcdf_variable_attribute
 
 !------------------------------------------------------------------------------!
@@ -1350,32 +1354,32 @@
 !> variable at the current time step.
 !------------------------------------------------------------------------------!
-    SUBROUTINE update_output(var, array, iter, output_file, cfg)
-       TYPE(nc_var), INTENT(IN)  ::  var
-       REAL(dp), INTENT(IN)      ::  array(:,:,:)
-       INTEGER, INTENT(IN)       ::  iter
-       TYPE(nc_file), INTENT(IN) ::  output_file
-       TYPE(inifor_config)       ::  cfg
-
-       INTEGER ::  ncid, ndim, start(4), count(4)
-       LOGICAL ::  var_is_time_dependent
-
-       var_is_time_dependent = (                                               &
-          var % dimids( var % ndim ) == output_file % dimid_time               &
-       )
-
-!
-!--    Skip time dimension for output
-       ndim = var % ndim
-       IF ( var_is_time_dependent )  ndim = var % ndim - 1
-
-       start(:)      = (/1,1,1,1/)
-       start(ndim+1) = iter
-       count(1:ndim) = var%dimlen(1:ndim)
-
-       CALL check(nf90_open(output_file % name, NF90_WRITE, ncid))
-
-!
-!--    Reduce dimension of output array according to variable kind
-       SELECT CASE (TRIM(var % kind))
+ SUBROUTINE update_output(var, array, iter, output_file, cfg)
+    TYPE(nc_var), INTENT(IN)  ::  var
+    REAL(wp), INTENT(IN)      ::  array(:,:,:)
+    INTEGER, INTENT(IN)       ::  iter
+    TYPE(nc_file), INTENT(IN) ::  output_file
+    TYPE(inifor_config)       ::  cfg
+
+    INTEGER ::  ncid, ndim, start(4), count(4)
+    LOGICAL ::  var_is_time_dependent
+
+    var_is_time_dependent = (                                               &
+       var%dimids( var%ndim ) == output_file%dimid_time               &
+    )
+
+!
+!-- Skip time dimension for output
+    ndim = var%ndim
+    IF ( var_is_time_dependent )  ndim = var%ndim - 1
+
+    start(:)      = (/1,1,1,1/)
+    start(ndim+1) = iter
+    count(1:ndim) = var%dimlen(1:ndim)
+
+    CALL check(nf90_open(output_file%name, NF90_WRITE, ncid))
+
+!
+!-- Reduce dimension of output array according to variable kind
+    SELECT CASE (TRIM(var%kind))
        
        CASE ( 'init scalar profile', 'init u profile', 'init v profile',       &
@@ -1395,7 +1399,7 @@
           
 
-          IF (.NOT. SIZE(array, 2) .EQ. var % dimlen(1))  THEN
+          IF (.NOT. SIZE(array, 2) .EQ. var%dimlen(1))  THEN
              PRINT *, "inifor: update_output: Dimension ", 1, " of variable ", &
-                 TRIM(var % name), " (", var % dimlen(1),                      &
+                 TRIM(var%name), " (", var%dimlen(1),                      &
                  ") does not match the dimension of the output array (",       &
                  SIZE(array, 2), ")."
@@ -1448,5 +1452,5 @@
        CASE ( 'internal profile' )
 
-          IF ( cfg % debug )  THEN
+          IF ( cfg%debug )  THEN
              CALL check(nf90_put_var( ncid, var%varid, array(1,1,:),           &
                                       start=start(1:ndim+1),                   &
@@ -1461,13 +1465,13 @@
        CASE DEFAULT
 
-           message = "Variable kind '" // TRIM(var % kind) //                  &
+           message = "Variable kind '" // TRIM(var%kind) //                  &
                     "' not recognized."
            CALL inifor_abort('update_output', message)
 
-       END SELECT
-
-       CALL check(nf90_close(ncid))
-
-    END SUBROUTINE update_output
+    END SELECT
+
+    CALL check(nf90_close(ncid))
+
+ END SUBROUTINE update_output
 
 
@@ -1477,15 +1481,15 @@
 !> Checks the status of a netCDF API call and aborts if an error occured
 !------------------------------------------------------------------------------!
-    SUBROUTINE check(status)
-
-       INTEGER, INTENT(IN) ::  status
-
-       IF (status /= nf90_noerr)  THEN
-          message = "NetCDF API call failed with error: " //                     &
-                    TRIM( nf90_strerror(status) )
-          CALL inifor_abort('io.check', message)
-       ENDIF
-
-    END SUBROUTINE check
+ SUBROUTINE check(status)
+
+    INTEGER, INTENT(IN) ::  status
+
+    IF (status /= nf90_noerr)  THEN
+       message = "NetCDF API call failed with error: " //                     &
+                 TRIM( nf90_strerror(status) )
+       CALL inifor_abort('io.check', message)
+    ENDIF
+
+ END SUBROUTINE check
 
  END MODULE inifor_io
Index: /palm/trunk/UTIL/inifor/src/inifor_transform.f90
===================================================================
--- /palm/trunk/UTIL/inifor/src/inifor_transform.f90	(revision 3865)
+++ /palm/trunk/UTIL/inifor/src/inifor_transform.f90	(revision 3866)
@@ -26,4 +26,9 @@
 ! -----------------
 ! $Id$
+! Use PALM's working precision
+! Improved coding style
+! 
+! 
+! 3785 2019-03-06 10:41:14Z eckhard
 ! Remove basic state pressure before computing geostrophic wind
 !  - Introduced new level-based profile averaging routine that does not rely on
@@ -106,5 +111,5 @@
     USE inifor_control
     USE inifor_defs,                                                           &
-        ONLY: BETA, dp, G, P_SL, PI, RD, T_SL, TO_DEGREES, TO_RADIANS
+        ONLY: BETA, G, P_SL, PI, RD, T_SL, TO_DEGREES, TO_RADIANS, wp
     USE inifor_types
     USE inifor_util,                                                           &
@@ -116,32 +121,32 @@
 
 
-    SUBROUTINE interpolate_1d(in_arr, out_arr, outgrid)
-       TYPE(grid_definition), INTENT(IN) ::  outgrid
-       REAL(dp), INTENT(IN)              ::  in_arr(:)
-       REAL(dp), INTENT(OUT)             ::  out_arr(:)
-
-       INTEGER :: k, l, nz
-
-       nz = UBOUND(out_arr, 1)
-
-       DO k = nz, LBOUND(out_arr, 1), -1
-
-!
-!--       TODO: Remove IF clause and extrapolate based on a critical vertical
-!--       TODO: index marking the lower bound of COSMO-DE data coverage.
-!--       Check for negative interpolation weights indicating grid points 
-!--       below COSMO-DE domain and extrapolate from the top in such cells.
-          IF (outgrid % w(1,k,1) < -1.0_dp .AND. k < nz)  THEN
-             out_arr(k) = out_arr(k+1)
-          ELSE
-             out_arr(k) = 0.0_dp
-             DO l = 1, 2
-                out_arr(k) = out_arr(k) +                                      &
-                    outgrid % w(1,k,l) * in_arr(outgrid % kkk(1,k,l) )
-             ENDDO
-          ENDIF
-       ENDDO
-
-    END SUBROUTINE interpolate_1d
+ SUBROUTINE interpolate_1d(in_arr, out_arr, outgrid)
+    TYPE(grid_definition), INTENT(IN) ::  outgrid
+    REAL(wp), INTENT(IN)              ::  in_arr(:)
+    REAL(wp), INTENT(OUT)             ::  out_arr(:)
+
+    INTEGER :: k, l, nz
+
+    nz = UBOUND(out_arr, 1)
+
+    DO  k = nz, LBOUND(out_arr, 1), -1
+
+!
+!--    TODO: Remove IF clause and extrapolate based on a critical vertical
+!--    TODO: index marking the lower bound of COSMO-DE data coverage.
+!--    Check for negative interpolation weights indicating grid points 
+!--    below COSMO-DE domain and extrapolate from the top in such cells.
+       IF (outgrid%w(1,k,1) < -1.0_wp .AND. k < nz)  THEN
+          out_arr(k) = out_arr(k+1)
+       ELSE
+          out_arr(k) = 0.0_wp
+          DO  l = 1, 2
+             out_arr(k) = out_arr(k) +                                      &
+                 outgrid%w(1,k,l) * in_arr(outgrid%kkk(1,k,l) )
+          ENDDO
+       ENDIF
+    ENDDO
+
+ END SUBROUTINE interpolate_1d
 
 
@@ -158,9 +163,9 @@
 !> invar : Array of source data
 !> 
-!> outgrid % kk : Array of vertical neighbour indices. kk(i,j,k,:) contain the
+!> outgrid%kk : Array of vertical neighbour indices. kk(i,j,k,:) contain the
 !>     indices of the two vertical neighbors of PALM-4U point (i,j,k) on the
 !>     input grid corresponding to the source data invar.
 !> 
-!> outgrid % w_verti : Array of weights for vertical linear interpolation
+!> outgrid%w_verti : Array of weights for vertical linear interpolation
 !>     corresponding to neighbour points indexed by kk.
 !>
@@ -169,36 +174,36 @@
 !> outvar : Array of interpolated data
 !------------------------------------------------------------------------------!
-    SUBROUTINE interpolate_1d_arr(in_arr, out_arr, outgrid)
-       TYPE(grid_definition), INTENT(IN) ::  outgrid
-       REAL(dp), INTENT(IN)              ::  in_arr(0:,0:,0:)
-       REAL(dp), INTENT(OUT)             ::  out_arr(0:,0:,:)
-
-       INTEGER :: i, j, k, l, nz
-
-       nz = UBOUND(out_arr, 3)
-
-       DO j = LBOUND(out_arr, 2), UBOUND(out_arr, 2)
-       DO i = LBOUND(out_arr, 1), UBOUND(out_arr, 1)
-       DO k = nz, LBOUND(out_arr, 3), -1
-
-!
-!--       TODO: Remove IF clause and extrapolate based on a critical vertical
-!--       TODO: index marking the lower bound of COSMO-DE data coverage.
-!--       Check for negative interpolation weights indicating grid points 
-!--       below COSMO-DE domain and extrapolate from the top in such cells.
-          IF (outgrid % w_verti(i,j,k,1) < -1.0_dp .AND. k < nz)  THEN
-             out_arr(i,j,k) = out_arr(i,j,k+1)
-          ELSE
-             out_arr(i,j,k) = 0.0_dp
-             DO l = 1, 2
-                out_arr(i,j,k) = out_arr(i,j,k) +                              &
-                    outgrid % w_verti(i,j,k,l) *                               &
-                    in_arr(i,j,outgrid % kk(i,j,k, l) )
-             ENDDO
-          ENDIF
-       ENDDO
-       ENDDO
-       ENDDO
-    END SUBROUTINE interpolate_1d_arr
+ SUBROUTINE interpolate_1d_arr(in_arr, out_arr, outgrid)
+    TYPE(grid_definition), INTENT(IN) ::  outgrid
+    REAL(wp), INTENT(IN)              ::  in_arr(0:,0:,0:)
+    REAL(wp), INTENT(OUT)             ::  out_arr(0:,0:,:)
+
+    INTEGER :: i, j, k, l, nz
+
+    nz = UBOUND(out_arr, 3)
+
+    DO  j = LBOUND(out_arr, 2), UBOUND(out_arr, 2)
+    DO  i = LBOUND(out_arr, 1), UBOUND(out_arr, 1)
+    DO  k = nz, LBOUND(out_arr, 3), -1
+
+!
+!--    TODO: Remove IF clause and extrapolate based on a critical vertical
+!--    TODO: index marking the lower bound of COSMO-DE data coverage.
+!--    Check for negative interpolation weights indicating grid points 
+!--    below COSMO-DE domain and extrapolate from the top in such cells.
+       IF (outgrid%w_verti(i,j,k,1) < -1.0_wp .AND. k < nz)  THEN
+          out_arr(i,j,k) = out_arr(i,j,k+1)
+       ELSE
+          out_arr(i,j,k) = 0.0_wp
+          DO  l = 1, 2
+             out_arr(i,j,k) = out_arr(i,j,k) +                              &
+                 outgrid%w_verti(i,j,k,l) *                               &
+                 in_arr(i,j,outgrid%kk(i,j,k, l) )
+          ENDDO
+       ENDIF
+    ENDDO
+    ENDDO
+    ENDDO
+ END SUBROUTINE interpolate_1d_arr
 
 
@@ -214,5 +219,5 @@
 !> invar : Array of source data
 !> 
-!> outgrid % ii, % jj : Array of neighbour indices in x and y direction.
+!> outgrid%ii,%jj : Array of neighbour indices in x and y direction.
 !>     ii(i,j,k,:), and jj(i,j,k,:) contain the four horizontal neighbour points
 !>     of PALM-4U point (i,j,k) on the input grid corresponding to the source
@@ -220,5 +225,5 @@
 !      form of the interpolation weights.)
 !> 
-!> outgrid % w_horiz: Array of weights for horizontal bi-linear interpolation
+!> outgrid%w_horiz: Array of weights for horizontal bi-linear interpolation
 !>     corresponding to neighbour points indexed by ii and jj.
 !>
@@ -227,40 +232,40 @@
 !> outvar : Array of interpolated data
 !------------------------------------------------------------------------------!
-    SUBROUTINE interpolate_2d(invar, outvar, outgrid, ncvar)
-!
-!--    I index 0-based for the indices of the outvar to be consistent with the
-!--    outgrid indices and interpolation weights.
-       TYPE(grid_definition), INTENT(IN)  ::  outgrid
-       REAL(dp), INTENT(IN)               ::  invar(0:,0:,0:)
-       REAL(dp), INTENT(OUT)              ::  outvar(0:,0:,0:)
-       TYPE(nc_var), INTENT(IN), OPTIONAL ::  ncvar
-
-       INTEGER ::  i, j, k, l
-
-!
-!--    TODO: check if input dimensions are consistent, i.e. ranges are correct
-       IF ( UBOUND(outvar, 3) .GT. UBOUND(invar, 3) )  THEN
-           message = "Output array for '" // TRIM(ncvar % name) // "' has ' more levels (" // &
-              TRIM(str(UBOUND(outvar, 3))) // ") than input variable ("//&
-              TRIM(str(UBOUND(invar, 3))) // ")."
-           CALL inifor_abort('interpolate_2d', message)
-       ENDIF
-
-       DO k = 0, UBOUND(outvar, 3)
-       DO j = 0, UBOUND(outvar, 2)
-       DO i = 0, UBOUND(outvar, 1)
-          outvar(i,j,k) = 0.0_dp
-          DO l = 1, 4
-             
-             outvar(i,j,k) = outvar(i,j,k) +                                   &
-                outgrid % w_horiz(i,j,l) * invar( outgrid % ii(i,j,l),         & 
-                                                  outgrid % jj(i,j,l),         &
-                                                  k )
-          ENDDO
+ SUBROUTINE interpolate_2d(invar, outvar, outgrid, ncvar)
+!
+!-- I index 0-based for the indices of the outvar to be consistent with the
+!-- outgrid indices and interpolation weights.
+    TYPE(grid_definition), INTENT(IN)  ::  outgrid
+    REAL(wp), INTENT(IN)               ::  invar(0:,0:,0:)
+    REAL(wp), INTENT(OUT)              ::  outvar(0:,0:,0:)
+    TYPE(nc_var), INTENT(IN), OPTIONAL ::  ncvar
+
+    INTEGER ::  i, j, k, l
+
+!
+!-- TODO: check if input dimensions are consistent, i.e. ranges are correct
+    IF ( UBOUND(outvar, 3) .GT. UBOUND(invar, 3) )  THEN
+        message = "Output array for '" // TRIM(ncvar%name) // "' has ' more levels (" // &
+           TRIM(str(UBOUND(outvar, 3))) // ") than input variable ("//&
+           TRIM(str(UBOUND(invar, 3))) // ")."
+        CALL inifor_abort('interpolate_2d', message)
+    ENDIF
+
+    DO  k = 0, UBOUND(outvar, 3)
+    DO  j = 0, UBOUND(outvar, 2)
+    DO  i = 0, UBOUND(outvar, 1)
+       outvar(i,j,k) = 0.0_wp
+       DO  l = 1, 4
+          
+          outvar(i,j,k) = outvar(i,j,k) +                                   &
+             outgrid%w_horiz(i,j,l) * invar( outgrid%ii(i,j,l),         & 
+                                               outgrid%jj(i,j,l),         &
+                                               k )
        ENDDO
-       ENDDO
-       ENDDO
+    ENDDO
+    ENDDO
+    ENDDO
         
-    END SUBROUTINE interpolate_2d
+ END SUBROUTINE interpolate_2d
 
 
@@ -271,40 +276,40 @@
 !> out_arr(:)
 !------------------------------------------------------------------------------!
-    SUBROUTINE average_2d(in_arr, out_arr, ii, jj)
-       REAL(dp), INTENT(IN)              ::  in_arr(0:,0:,0:)
-       REAL(dp), INTENT(OUT)             ::  out_arr(0:)
-       INTEGER, INTENT(IN), DIMENSION(:) ::  ii, jj
-
-       INTEGER  ::  i, j, k, l
-       REAL(dp) ::  ni
-
-       IF (SIZE(ii) /= SIZE(jj))  THEN
-          message = "Length of 'ii' and 'jj' index lists do not match." //     &
-             NEW_LINE(' ') // "ii has " // str(SIZE(ii)) // " elements, " //   &
-             NEW_LINE(' ') // "jj has " // str(SIZE(jj)) // "."
-          CALL inifor_abort('average_2d', message)
-       ENDIF
-
-       IF (SIZE(ii) == 0)  THEN
-          message = "No columns to average over; " //                          &
-                    "size of index lists 'ii' and 'jj' is zero."
-          CALL inifor_abort('average_2d', message)
-       ENDIF
-
-       DO k = 0, UBOUND(out_arr, 1)
-
-          out_arr(k) = 0.0_dp
-          DO l = 1, UBOUND(ii, 1)
-             i = ii(l)
-             j = jj(l)
-             out_arr(k) = out_arr(k) + in_arr(i, j, k)
-          ENDDO
-
+ SUBROUTINE average_2d(in_arr, out_arr, ii, jj)
+    REAL(wp), INTENT(IN)              ::  in_arr(0:,0:,0:)
+    REAL(wp), INTENT(OUT)             ::  out_arr(0:)
+    INTEGER, INTENT(IN), DIMENSION(:) ::  ii, jj
+
+    INTEGER  ::  i, j, k, l
+    REAL(wp) ::  ni
+
+    IF (SIZE(ii) /= SIZE(jj))  THEN
+       message = "Length of 'ii' and 'jj' index lists do not match." //     &
+          NEW_LINE(' ') // "ii has " // str(SIZE(ii)) // " elements, " //   &
+          NEW_LINE(' ') // "jj has " // str(SIZE(jj)) // "."
+       CALL inifor_abort('average_2d', message)
+    ENDIF
+
+    IF (SIZE(ii) == 0)  THEN
+       message = "No columns to average over; " //                          &
+                 "size of index lists 'ii' and 'jj' is zero."
+       CALL inifor_abort('average_2d', message)
+    ENDIF
+
+    DO  k = 0, UBOUND(out_arr, 1)
+
+       out_arr(k) = 0.0_wp
+       DO  l = 1, UBOUND(ii, 1)
+          i = ii(l)
+          j = jj(l)
+          out_arr(k) = out_arr(k) + in_arr(i, j, k)
        ENDDO
 
-       ni = 1.0_dp / SIZE(ii)
-       out_arr(:) = out_arr(:) * ni
-
-    END SUBROUTINE average_2d
+    ENDDO
+
+    ni = 1.0_wp / SIZE(ii)
+    out_arr(:) = out_arr(:) * ni
+
+ END SUBROUTINE average_2d
 
 
@@ -320,31 +325,31 @@
 !> as coarse as COSMO, horizontally as fine as PALM).
 !------------------------------------------------------------------------------!
-    SUBROUTINE interpolate_3d(source_array, palm_array, palm_intermediate, palm_grid)
-       TYPE(grid_definition), INTENT(IN) ::  palm_intermediate, palm_grid
-       REAL(dp), DIMENSION(:,:,:), INTENT(IN)  ::  source_array
-       REAL(dp), DIMENSION(:,:,:), INTENT(OUT) ::  palm_array
-       REAL(dp), DIMENSION(:,:,:), ALLOCATABLE ::  intermediate_array
-       INTEGER ::  nx, ny, nlev
-
-       nx = palm_intermediate % nx
-       ny = palm_intermediate % ny
-       nlev = palm_intermediate % nz
-
-!
-!--    Interpolate from COSMO to intermediate grid. Allocating with one
-!--    less point in the vertical, since scalars like T have 50 instead of 51
-!--    points in COSMO.
-       ALLOCATE(intermediate_array(0:nx, 0:ny, 0:nlev-1)) !
-
-       CALL interpolate_2d(source_array, intermediate_array, palm_intermediate)
-
-!
-!--    Interpolate from intermediate grid to palm_grid grid, includes
-!--    extrapolation for cells below COSMO domain.
-       CALL interpolate_1d_arr(intermediate_array, palm_array, palm_grid)
-
-       DEALLOCATE(intermediate_array)
-
-    END SUBROUTINE interpolate_3d
+ SUBROUTINE interpolate_3d(source_array, palm_array, palm_intermediate, palm_grid)
+    TYPE(grid_definition), INTENT(IN) ::  palm_intermediate, palm_grid
+    REAL(wp), DIMENSION(:,:,:), INTENT(IN)  ::  source_array
+    REAL(wp), DIMENSION(:,:,:), INTENT(OUT) ::  palm_array
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  intermediate_array
+    INTEGER ::  nx, ny, nlev
+
+    nx = palm_intermediate%nx
+    ny = palm_intermediate%ny
+    nlev = palm_intermediate%nz
+
+!
+!-- Interpolate from COSMO to intermediate grid. Allocating with one
+!-- less point in the vertical, since scalars like T have 50 instead of 51
+!-- points in COSMO.
+    ALLOCATE(intermediate_array(0:nx, 0:ny, 0:nlev-1)) !
+
+    CALL interpolate_2d(source_array, intermediate_array, palm_intermediate)
+
+!
+!-- Interpolate from intermediate grid to palm_grid grid, includes
+!-- extrapolation for cells below COSMO domain.
+    CALL interpolate_1d_arr(intermediate_array, palm_array, palm_grid)
+
+    DEALLOCATE(intermediate_array)
+
+ END SUBROUTINE interpolate_3d
 
 
@@ -355,52 +360,52 @@
 !> averaging grid 'avg_grid' and store the result in 'profile_array'.
 !------------------------------------------------------------------------------!
-    SUBROUTINE interp_average_profile(source_array, profile_array, avg_grid)
-       TYPE(grid_definition), INTENT(IN)          ::  avg_grid
-       REAL(dp), DIMENSION(:,:,:), INTENT(IN)     ::  source_array
-       REAL(dp), DIMENSION(:), INTENT(OUT)        ::  profile_array
-
-       INTEGER ::  i_source, j_source, k_profile, k_source, l, m
-
-       REAL ::  ni_columns
-
-       profile_array(:) = 0.0_dp
-
-       DO l = 1, avg_grid % n_columns
-          i_source = avg_grid % iii(l)
-          j_source = avg_grid % jjj(l)
-
-!
-!--       Loop over PALM levels
-          DO k_profile = avg_grid % k_min, UBOUND(profile_array, 1)
-
-!
-!--          Loop over vertical interpolation neighbours
-             DO m = 1, 2
-
-                k_source = avg_grid % kkk(l, k_profile, m)
-
-                profile_array(k_profile) = profile_array(k_profile)            &
-                   + avg_grid % w(l, k_profile, m)                             &
-                   * source_array(i_source, j_source, k_source)
-!
-!--          Loop over vertical interpolation neighbours m
-             ENDDO
-
-!
-!--       Loop over PALM levels k_profile
+ SUBROUTINE interp_average_profile(source_array, profile_array, avg_grid)
+    TYPE(grid_definition), INTENT(IN)          ::  avg_grid
+    REAL(wp), DIMENSION(:,:,:), INTENT(IN)     ::  source_array
+    REAL(wp), DIMENSION(:), INTENT(OUT)        ::  profile_array
+
+    INTEGER ::  i_source, j_source, k_profile, k_source, l, m
+
+    REAL ::  ni_columns
+
+    profile_array(:) = 0.0_wp
+
+    DO  l = 1, avg_grid%n_columns
+       i_source = avg_grid%iii(l)
+       j_source = avg_grid%jjj(l)
+
+!
+!--    Loop over PALM levels
+       DO  k_profile = avg_grid%k_min, UBOUND(profile_array, 1)
+
+!
+!--       Loop over vertical interpolation neighbours
+          DO  m = 1, 2
+
+             k_source = avg_grid%kkk(l, k_profile, m)
+
+             profile_array(k_profile) = profile_array(k_profile)            &
+                + avg_grid%w(l, k_profile, m)                             &
+                * source_array(i_source, j_source, k_source)
+!
+!--       Loop over vertical interpolation neighbours m
           ENDDO
 
 !
-!--    Loop over horizontal neighbours l
+!--    Loop over PALM levels k_profile
        ENDDO
 
-       ni_columns = 1.0_dp / avg_grid % n_columns
-       profile_array(:) = profile_array(:) * ni_columns
-
-!
-!--    Constant extrapolation to the bottom
-       profile_array(1:avg_grid % k_min-1) = profile_array(avg_grid % k_min)
-
-    END SUBROUTINE interp_average_profile
+!
+!-- Loop over horizontal neighbours l
+    ENDDO
+
+    ni_columns = 1.0_wp / avg_grid%n_columns
+    profile_array(:) = profile_array(:) * ni_columns
+
+!
+!-- Constant extrapolation to the bottom
+    profile_array(1:avg_grid%k_min-1) = profile_array(avg_grid%k_min)
+
+ END SUBROUTINE interp_average_profile
 
 
@@ -411,40 +416,40 @@
 !> averaging grid 'avg_grid' and store the result in 'profile_array'.
 !------------------------------------------------------------------------------!
-    SUBROUTINE average_profile( source_array, profile_array, avg_grid )
-
-       TYPE(grid_definition), INTENT(IN)          ::  avg_grid
-       REAL(dp), DIMENSION(:,:,:), INTENT(IN)     ::  source_array 
-       REAL(dp), DIMENSION(:), INTENT(OUT)        ::  profile_array
-
-       INTEGER ::  i_source, j_source, l, nz, nlev
-
-       REAL(dp) ::  ni_columns
-
-       nlev = SIZE( source_array, 3 )
-       nz   = SIZE( profile_array, 1 )
-
-       IF ( nlev /= nz )  THEN
-          message = "Lengths of input and output profiles do not match: " //   &
-                    "cosmo_pressure(" // TRIM( str( nlev ) ) //                &
-                    "), profile_array(" // TRIM( str( nz ) )  // ")."
-          CALL inifor_abort('average_pressure_perturbation', message)
-       ENDIF
-      
-       profile_array(:) = 0.0_dp
-
-       DO l = 1, avg_grid % n_columns
-
-          i_source = avg_grid % iii(l)
-          j_source = avg_grid % jjj(l)
-
-          profile_array(:) = profile_array(:)                                  &
-                           + source_array(i_source, j_source, :)
-
-       ENDDO
-
-       ni_columns = 1.0_dp / avg_grid % n_columns
-       profile_array(:) = profile_array(:) * ni_columns
-
-    END SUBROUTINE average_profile
+ SUBROUTINE average_profile( source_array, profile_array, avg_grid )
+
+    TYPE(grid_definition), INTENT(IN)          ::  avg_grid
+    REAL(wp), DIMENSION(:,:,:), INTENT(IN)     ::  source_array 
+    REAL(wp), DIMENSION(:), INTENT(OUT)        ::  profile_array
+
+    INTEGER ::  i_source, j_source, l, nz, nlev
+
+    REAL(wp) ::  ni_columns
+
+    nlev = SIZE( source_array, 3 )
+    nz   = SIZE( profile_array, 1 )
+
+    IF ( nlev /= nz )  THEN
+       message = "Lengths of input and output profiles do not match: " //   &
+                 "cosmo_pressure(" // TRIM( str( nlev ) ) //                &
+                 "), profile_array(" // TRIM( str( nz ) )  // ")."
+       CALL inifor_abort('average_pressure_perturbation', message)
+    ENDIF
+    
+    profile_array(:) = 0.0_wp
+
+    DO  l = 1, avg_grid%n_columns
+
+       i_source = avg_grid%iii(l)
+       j_source = avg_grid%jjj(l)
+
+       profile_array(:) = profile_array(:)                                  &
+                        + source_array(i_source, j_source, :)
+
+    ENDDO
+
+    ni_columns = 1.0_wp / avg_grid%n_columns
+    profile_array(:) = profile_array(:) * ni_columns
+
+ END SUBROUTINE average_profile
 
 
@@ -456,52 +461,52 @@
 !> averaging.
 !------------------------------------------------------------------------------!
-    SUBROUTINE average_pressure_perturbation( cosmo_pressure, profile_array,   &
-                                              cosmo_grid, avg_grid )
-
-       TYPE(grid_definition), INTENT(IN)          ::  cosmo_grid, avg_grid
-       REAL(dp), DIMENSION(:,:,:), INTENT(IN)     ::  cosmo_pressure
-       REAL(dp), DIMENSION(:), INTENT(OUT)        ::  profile_array
-
-       INTEGER ::  i_source, j_source, l, nz, nlev
-
-       REAL(dp)                            ::  ni_columns
-       REAL(dp), DIMENSION(:), ALLOCATABLE ::  basic_state_pressure
-
-       nlev = SIZE( cosmo_pressure, 3 )
-       nz   = SIZE( profile_array, 1 )
-
-       IF ( nlev /= nz )  THEN
-          message = "Lengths of input and output profiles do not match: " //   &
-                    "cosmo_pressure(" // TRIM( str( nlev ) ) //                &
-                    "), profile_array(" // TRIM( str( nz ) )  // ")."
-          CALL inifor_abort('average_pressure_perturbation', message)
-       ENDIF
-
-       ALLOCATE( basic_state_pressure(nz) )
-       profile_array(:) = 0.0_dp
-
-       DO l = 1, avg_grid % n_columns
-          i_source = avg_grid % iii(l)
-          j_source = avg_grid % jjj(l)
-
-!
-!--       Compute pressure perturbation by removing COSMO basic state pressure
-          CALL get_basic_state( cosmo_grid % hfl(i_source,j_source,:), BETA,   &
-                                P_SL, T_SL, RD, G, basic_state_pressure )
-
-          profile_array(:) = profile_array(:)                                  &
-                           + cosmo_pressure(i_source, j_source, :)             &
-                           - basic_state_pressure(:)
-
-!
-!--    Loop over horizontal neighbours l
-       ENDDO
-
-       DEALLOCATE( basic_state_pressure )
-
-       ni_columns = 1.0_dp / avg_grid % n_columns
-       profile_array(:) = profile_array(:) * ni_columns
-
-    END SUBROUTINE average_pressure_perturbation
+ SUBROUTINE average_pressure_perturbation( cosmo_pressure, profile_array,   &
+                                           cosmo_grid, avg_grid )
+
+    TYPE(grid_definition), INTENT(IN)          ::  cosmo_grid, avg_grid
+    REAL(wp), DIMENSION(:,:,:), INTENT(IN)     ::  cosmo_pressure
+    REAL(wp), DIMENSION(:), INTENT(OUT)        ::  profile_array
+
+    INTEGER ::  i_source, j_source, l, nz, nlev
+
+    REAL(wp)                            ::  ni_columns
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  basic_state_pressure
+
+    nlev = SIZE( cosmo_pressure, 3 )
+    nz   = SIZE( profile_array, 1 )
+
+    IF ( nlev /= nz )  THEN
+       message = "Lengths of input and output profiles do not match: " //   &
+                 "cosmo_pressure(" // TRIM( str( nlev ) ) //                &
+                 "), profile_array(" // TRIM( str( nz ) )  // ")."
+       CALL inifor_abort('average_pressure_perturbation', message)
+    ENDIF
+
+    ALLOCATE( basic_state_pressure(nz) )
+    profile_array(:) = 0.0_wp
+
+    DO  l = 1, avg_grid%n_columns
+       i_source = avg_grid%iii(l)
+       j_source = avg_grid%jjj(l)
+
+!
+!--    Compute pressure perturbation by removing COSMO basic state pressure
+       CALL get_basic_state( cosmo_grid%hfl(i_source,j_source,:), BETA,   &
+                             P_SL, T_SL, RD, G, basic_state_pressure )
+
+       profile_array(:) = profile_array(:)                                  &
+                        + cosmo_pressure(i_source, j_source, :)             &
+                        - basic_state_pressure(:)
+
+!
+!-- Loop over horizontal neighbours l
+    ENDDO
+
+    DEALLOCATE( basic_state_pressure )
+
+    ni_columns = 1.0_wp / avg_grid%n_columns
+    profile_array(:) = profile_array(:) * ni_columns
+
+ END SUBROUTINE average_pressure_perturbation
 
 
@@ -513,20 +518,20 @@
 !> Extrapolates density linearly from the level 'k_min' downwards.
 !------------------------------------------------------------------------------!
-    SUBROUTINE extrapolate_density(rho, avg_grid)
-       REAL(dp), DIMENSION(:), INTENT(INOUT) ::  rho
-       TYPE(grid_definition), INTENT(IN)     ::  avg_grid
-
-       REAL(dp) ::  drhodz, dz, zk, rhok
-       INTEGER  ::  k_min
-
-       k_min  = avg_grid % k_min
-       zk     = avg_grid % z(k_min)
-       rhok   = rho(k_min)
-       dz     = avg_grid % z(k_min + 1) - avg_grid % z(k_min)
-       drhodz = (rho(k_min + 1) - rho(k_min)) / dz
-
-       rho(1:k_min-1) = rhok + drhodz * (avg_grid % z(1:k_min-1) - zk)
-
-    END SUBROUTINE extrapolate_density
+ SUBROUTINE extrapolate_density(rho, avg_grid)
+    REAL(wp), DIMENSION(:), INTENT(INOUT) ::  rho
+    TYPE(grid_definition), INTENT(IN)     ::  avg_grid
+
+    REAL(wp) ::  drhodz, dz, zk, rhok
+    INTEGER  ::  k_min
+
+    k_min  = avg_grid%k_min
+    zk     = avg_grid%z(k_min)
+    rhok   = rho(k_min)
+    dz     = avg_grid%z(k_min + 1) - avg_grid%z(k_min)
+    drhodz = (rho(k_min + 1) - rho(k_min)) / dz
+
+    rho(1:k_min-1) = rhok + drhodz * (avg_grid%z(1:k_min-1) - zk)
+
+ END SUBROUTINE extrapolate_density
 
 
@@ -536,24 +541,24 @@
 !> Driver for extrapolating pressure from PALM level k_min downwards
 !------------------------------------------------------------------------------!
-    SUBROUTINE extrapolate_pressure(p, rho, avg_grid)
-       REAL(dp), DIMENSION(:), INTENT(IN)    ::  rho
-       REAL(dp), DIMENSION(:), INTENT(INOUT) ::  p
-       TYPE(grid_definition), INTENT(IN)     ::  avg_grid
-
-       REAL(dp) ::  drhodz, dz, zk, rhok
-       INTEGER  ::  k, k_min
-
-       k_min = avg_grid % k_min
-       zk    = avg_grid % z(k_min)
-       rhok  = rho(k_min)
-       dz    = avg_grid % z(k_min + 1) - avg_grid % z(k_min)
-       drhodz = 0.5_dp * (rho(k_min + 1) - rho(k_min)) / dz
-
-       DO k = 1, k_min-1
-          p(k) = constant_density_pressure(p(k_min), zk, rhok, drhodz,         &
-                                           avg_grid % z(k), G)
-       ENDDO
-
-    END SUBROUTINE extrapolate_pressure
+ SUBROUTINE extrapolate_pressure(p, rho, avg_grid)
+    REAL(wp), DIMENSION(:), INTENT(IN)    ::  rho
+    REAL(wp), DIMENSION(:), INTENT(INOUT) ::  p
+    TYPE(grid_definition), INTENT(IN)     ::  avg_grid
+
+    REAL(wp) ::  drhodz, dz, zk, rhok
+    INTEGER  ::  k, k_min
+
+    k_min = avg_grid%k_min
+    zk    = avg_grid%z(k_min)
+    rhok  = rho(k_min)
+    dz    = avg_grid%z(k_min + 1) - avg_grid%z(k_min)
+    drhodz = 0.5_wp * (rho(k_min + 1) - rho(k_min)) / dz
+
+    DO  k = 1, k_min-1
+       p(k) = constant_density_pressure(p(k_min), zk, rhok, drhodz,         &
+                                        avg_grid%z(k), G)
+    ENDDO
+
+ END SUBROUTINE extrapolate_pressure
 
 
@@ -564,32 +569,32 @@
 !> extrapolated pressure at the surface.
 !------------------------------------------------------------------------------!
-    SUBROUTINE get_surface_pressure(p, rho, avg_grid)
-       REAL(dp), DIMENSION(:), INTENT(IN)    ::  rho
-       REAL(dp), DIMENSION(:), INTENT(INOUT) ::  p
-       TYPE(grid_definition), INTENT(IN)     ::  avg_grid
-
-       REAL(dp) ::  drhodz, dz, zk, rhok
-       INTEGER  ::  k_min
-
-       k_min = avg_grid % k_min
-       zk    = avg_grid % z(k_min)
-       rhok  = rho(k_min)
-       dz    = avg_grid % z(k_min + 1) - avg_grid % z(k_min)
-       drhodz = 0.5_dp * (rho(k_min + 1) - rho(k_min)) / dz
-
-       p(1) = constant_density_pressure(p(k_min), zk, rhok, drhodz,            &
-                                        0.0_dp, G)
-
-    END SUBROUTINE get_surface_pressure
-
-
-    FUNCTION constant_density_pressure(pk, zk, rhok, drhodz, z, g)  RESULT(p)
-
-       REAL(dp), INTENT(IN)  ::  pk, zk, rhok, drhodz, g, z
-       REAL(dp) ::  p
-
-       p = pk + ( zk - z ) * g * ( rhok + 0.5*drhodz * (zk - z) )
-
-    END FUNCTION constant_density_pressure
+ SUBROUTINE get_surface_pressure(p, rho, avg_grid)
+    REAL(wp), DIMENSION(:), INTENT(IN)    ::  rho
+    REAL(wp), DIMENSION(:), INTENT(INOUT) ::  p
+    TYPE(grid_definition), INTENT(IN)     ::  avg_grid
+
+    REAL(wp) ::  drhodz, dz, zk, rhok
+    INTEGER  ::  k_min
+
+    k_min = avg_grid%k_min
+    zk    = avg_grid%z(k_min)
+    rhok  = rho(k_min)
+    dz    = avg_grid%z(k_min + 1) - avg_grid%z(k_min)
+    drhodz = 0.5_wp * (rho(k_min + 1) - rho(k_min)) / dz
+
+    p(1) = constant_density_pressure(p(k_min), zk, rhok, drhodz,            &
+                                     0.0_wp, G)
+
+ END SUBROUTINE get_surface_pressure
+
+
+ FUNCTION constant_density_pressure(pk, zk, rhok, drhodz, z, g)  RESULT(p)
+
+    REAL(wp), INTENT(IN)  ::  pk, zk, rhok, drhodz, g, z
+    REAL(wp) ::  p
+
+    p = pk + ( zk - z ) * g * ( rhok + 0.5*drhodz * (zk - z) )
+
+ END FUNCTION constant_density_pressure
 
 !-----------------------------------------------------------------------------!
@@ -599,15 +604,15 @@
 !> vg.
 !-----------------------------------------------------------------------------!
-    SUBROUTINE geostrophic_winds(p_north, p_south, p_east, p_west, rho, f3,    &
-                                 Lx, Ly, phi_n, lam_n, phi_g, lam_g, ug, vg)
-
-    REAL(dp), DIMENSION(:), INTENT(IN)  ::  p_north, p_south, p_east, p_west,  &
+ SUBROUTINE geostrophic_winds(p_north, p_south, p_east, p_west, rho, f3,    &
+                              Lx, Ly, phi_n, lam_n, phi_g, lam_g, ug, vg)
+
+    REAL(wp), DIMENSION(:), INTENT(IN)  ::  p_north, p_south, p_east, p_west,  &
                                             rho
-    REAL(dp), INTENT(IN)                ::  f3, Lx, Ly, phi_n, lam_n, phi_g, lam_g
-    REAL(dp), DIMENSION(:), INTENT(OUT) ::  ug, vg
-    REAL(dp)                            ::  facx, facy
-
-    facx = 1.0_dp / (Lx * f3)
-    facy = 1.0_dp / (Ly * f3)
+    REAL(wp), INTENT(IN)                ::  f3, Lx, Ly, phi_n, lam_n, phi_g, lam_g
+    REAL(wp), DIMENSION(:), INTENT(OUT) ::  ug, vg
+    REAL(wp)                            ::  facx, facy
+
+    facx = 1.0_wp / (Lx * f3)
+    facy = 1.0_wp / (Ly * f3)
     ug(:) = - facy / rho(:) * (p_north(:) - p_south(:))
     vg(:) =   facx / rho(:) * (p_east(:) - p_west(:))
@@ -617,5 +622,5 @@
     )
 
-    END SUBROUTINE geostrophic_winds
+ END SUBROUTINE geostrophic_winds
 
 
@@ -627,18 +632,18 @@
 !> lngitude of a geographical system centered at x0 and y0.
 !-----------------------------------------------------------------------------!
-    SUBROUTINE inv_plate_carree(x, y, x0, y0, r, lat, lon)
-       REAL(dp), INTENT(IN)  ::  x(:), y(:), x0, y0, r
-       REAL(dp), INTENT(OUT) ::  lat(:), lon(:)
-       
-       REAL(dp) :: ri
-
-!
-!--    TODO check dimensions of lat/lon and y/x match
-
-       ri = 1.0_dp / r
-       
-       lat(:) = (y(:) - y0) * ri
-       lon(:) = (x(:) - x0) * ri
-    END SUBROUTINE 
+ SUBROUTINE inv_plate_carree(x, y, x0, y0, r, lat, lon)
+    REAL(wp), INTENT(IN)  ::  x(:), y(:), x0, y0, r
+    REAL(wp), INTENT(OUT) ::  lat(:), lon(:)
+    
+    REAL(wp) :: ri
+
+!
+!-- TODO check dimensions of lat/lon and y/x match
+
+    ri = 1.0_wp / r
+    
+    lat(:) = (y(:) - y0) * ri
+    lon(:) = (x(:) - x0) * ri
+ END SUBROUTINE 
 
 
@@ -663,17 +668,17 @@
 !>     coordinate xy.
 !------------------------------------------------------------------------------!
-    ELEMENTAL REAL(dp) FUNCTION project(xy, xy0, r)
-       REAL(dp), INTENT(IN)  ::  xy, xy0, r
-       REAL(dp) :: ri
-
-!
-!--    If this elemental function is called with a large array as xy, it is
-!--    computationally more efficient to precompute the inverse radius and
-!--    then muliply.
-       ri = 1.0_dp / r
-
-       project = (xy - xy0) * ri
-
-    END FUNCTION project
+ ELEMENTAL REAL(wp) FUNCTION project(xy, xy0, r)
+    REAL(wp), INTENT(IN)  ::  xy, xy0, r
+    REAL(wp) :: ri
+
+!
+!-- If this elemental function is called with a large array as xy, it is
+!-- computationally more efficient to precompute the inverse radius and
+!-- then muliply.
+    ri = 1.0_wp / r
+
+    project = (xy - xy0) * ri
+
+ END FUNCTION project
 
 
@@ -684,10 +689,10 @@
 !> compute the geographical latitude of its rotated north pole.
 !------------------------------------------------------------------------------!
-    REAL(dp) FUNCTION phic_to_phin(phi_c)
-        REAL(dp), INTENT(IN) ::  phi_c
-
-        phic_to_phin = 0.5_dp * PI - ABS(phi_c)
-
-    END FUNCTION phic_to_phin
+ REAL(wp) FUNCTION phic_to_phin(phi_c)
+     REAL(wp), INTENT(IN) ::  phi_c
+
+     phic_to_phin = 0.5_wp * PI - ABS(phi_c)
+
+ END FUNCTION phic_to_phin
 
     
@@ -699,13 +704,13 @@
 !> north pole.
 !------------------------------------------------------------------------------!
-    REAL(dp) FUNCTION lamc_to_lamn(phi_c, lam_c)
-       REAL(dp), INTENT(IN) ::  phi_c, lam_c
-        
-       lamc_to_lamn = lam_c
-       IF (phi_c > 0.0_dp)  THEN
-          lamc_to_lamn = lam_c - SIGN(PI, lam_c)
-       ENDIF
-
-    END FUNCTION lamc_to_lamn
+ REAL(wp) FUNCTION lamc_to_lamn(phi_c, lam_c)
+    REAL(wp), INTENT(IN) ::  phi_c, lam_c
+     
+    lamc_to_lamn = lam_c
+    IF (phi_c > 0.0_wp)  THEN
+       lamc_to_lamn = lam_c - SIGN(PI, lam_c)
+    ENDIF
+
+ END FUNCTION lamc_to_lamn
 
 
@@ -718,18 +723,18 @@
 !> rotated-pole coordinate transformations.
 !------------------------------------------------------------------------------!
-    REAL(dp) FUNCTION gamma_from_hemisphere(phi_cg, phi_ref)
-       REAL(dp), INTENT(IN) ::  phi_cg
-       REAL(dp), INTENT(IN) ::  phi_ref
-
-       LOGICAL ::  palm_origin_is_south_of_cosmo_origin
-       
-       palm_origin_is_south_of_cosmo_origin = (phi_cg < phi_ref)
-
-       IF (palm_origin_is_south_of_cosmo_origin)  THEN
-           gamma_from_hemisphere = PI
-       ELSE
-           gamma_from_hemisphere = 0.0_dp
-       ENDIF
-    END FUNCTION gamma_from_hemisphere
+ REAL(wp) FUNCTION gamma_from_hemisphere(phi_cg, phi_ref)
+    REAL(wp), INTENT(IN) ::  phi_cg
+    REAL(wp), INTENT(IN) ::  phi_ref
+
+    LOGICAL ::  palm_origin_is_south_of_cosmo_origin
+    
+    palm_origin_is_south_of_cosmo_origin = (phi_cg < phi_ref)
+
+    IF (palm_origin_is_south_of_cosmo_origin)  THEN
+        gamma_from_hemisphere = PI
+    ELSE
+        gamma_from_hemisphere = 0.0_wp
+    ENDIF
+ END FUNCTION gamma_from_hemisphere
 
 
@@ -760,45 +765,45 @@
 !> phi(:,:), lam(:,:): geographical latitudes and logitudes
 !------------------------------------------------------------------------------!
-    SUBROUTINE rotate_to_cosmo(phir, lamr, phip, lamp, phi, lam, gam)
-       REAL(dp), INTENT(IN)  ::  phir(0:), lamr(0:), phip, lamp, gam
-       REAL(dp), INTENT(OUT) ::  phi(0:,0:), lam(0:,0:)
-
-       INTEGER ::  i, j
+ SUBROUTINE rotate_to_cosmo(phir, lamr, phip, lamp, phi, lam, gam)
+    REAL(wp), INTENT(IN)  ::  phir(0:), lamr(0:), phip, lamp, gam
+    REAL(wp), INTENT(OUT) ::  phi(0:,0:), lam(0:,0:)
+
+    INTEGER ::  i, j
+    
+    IF ( SIZE(phi, 1) .NE. SIZE(lam, 1) .OR. &
+         SIZE(phi, 2) .NE. SIZE(lam, 2) )  THEN
+       PRINT *, "inifor: rotate_to_cosmo: Dimensions of phi and lambda do not match. Dimensions are:"
+       PRINT *, "inifor: rotate_to_cosmo: phi: ", SIZE(phi, 1), SIZE(phi, 2)
+       PRINT *, "inifor: rotate_to_cosmo: lam: ", SIZE(lam, 1), SIZE(lam, 2)
+       STOP
+    ENDIF
+
+    IF ( SIZE(phir) .NE. SIZE(phi, 2) .OR. &
+         SIZE(lamr) .NE. SIZE(phi, 1) )  THEN
+       PRINT *, "inifor: rotate_to_cosmo: Dimensions of phir and lamr do not match. Dimensions are:"
+       PRINT *, "inifor: rotate_to_cosmo: phir: ", SIZE(phir), SIZE(phi, 2)
+       PRINT *, "inifor: rotate_to_cosmo: lamr: ", SIZE(lamr), SIZE(phi, 1)
+       STOP
+    ENDIF
+    
+    DO  j = 0, UBOUND(phir, 1)
+       DO  i = 0, UBOUND(lamr, 1)
+
+          phi(i,j) = phirot2phi(phir(j) * TO_DEGREES,                       &
+                                lamr(i) * TO_DEGREES,                       &
+                                phip * TO_DEGREES,                          &
+                                gam  * TO_DEGREES) * TO_RADIANS
+
+          lam(i,j) = rlarot2rla(phir(j) * TO_DEGREES,                       &
+                                lamr(i) * TO_DEGREES,                       &
+                                phip * TO_DEGREES,                          &
+                                lamp * TO_DEGREES,                          &
+                                gam  * TO_DEGREES) * TO_RADIANS
+
+       ENDDO
+    ENDDO
+
+ END SUBROUTINE rotate_to_cosmo
        
-       IF ( SIZE(phi, 1) .NE. SIZE(lam, 1) .OR. &
-            SIZE(phi, 2) .NE. SIZE(lam, 2) )  THEN
-          PRINT *, "inifor: rotate_to_cosmo: Dimensions of phi and lambda do not match. Dimensions are:"
-          PRINT *, "inifor: rotate_to_cosmo: phi: ", SIZE(phi, 1), SIZE(phi, 2)
-          PRINT *, "inifor: rotate_to_cosmo: lam: ", SIZE(lam, 1), SIZE(lam, 2)
-          STOP
-       ENDIF
-
-       IF ( SIZE(phir) .NE. SIZE(phi, 2) .OR. &
-            SIZE(lamr) .NE. SIZE(phi, 1) )  THEN
-          PRINT *, "inifor: rotate_to_cosmo: Dimensions of phir and lamr do not match. Dimensions are:"
-          PRINT *, "inifor: rotate_to_cosmo: phir: ", SIZE(phir), SIZE(phi, 2)
-          PRINT *, "inifor: rotate_to_cosmo: lamr: ", SIZE(lamr), SIZE(phi, 1)
-          STOP
-       ENDIF
-       
-       DO j = 0, UBOUND(phir, 1)
-          DO i = 0, UBOUND(lamr, 1)
-
-             phi(i,j) = phirot2phi(phir(j) * TO_DEGREES,                       &
-                                   lamr(i) * TO_DEGREES,                       &
-                                   phip * TO_DEGREES,                          &
-                                   gam  * TO_DEGREES) * TO_RADIANS
-
-             lam(i,j) = rlarot2rla(phir(j) * TO_DEGREES,                       &
-                                   lamr(i) * TO_DEGREES,                       &
-                                   phip * TO_DEGREES,                          &
-                                   lamp * TO_DEGREES,                          &
-                                   gam  * TO_DEGREES) * TO_RADIANS
-
-          ENDDO
-       ENDDO
-
-    END SUBROUTINE rotate_to_cosmo
-       
 
 !------------------------------------------------------------------------------!
@@ -807,30 +812,30 @@
 !> Rotate the given vector field (x(:), y(:)) by the given 'angle'.
 !------------------------------------------------------------------------------!
-    SUBROUTINE rotate_vector_field(x, y, angle)
-       REAL(dp), DIMENSION(:), INTENT(INOUT) :: x, y  !< x and y coodrinate in arbitrary units
-       REAL(dp), INTENT(IN)                  :: angle !< rotation angle [deg]
-
-       INTEGER  :: i
-       REAL(dp) :: sine, cosine, v_rot(2), rotation(2,2)
-
-       sine = SIN(angle * TO_RADIANS)
-       cosine = COS(angle * TO_RADIANS)
-!
-!--    RESAHPE() fills columns first, so the rotation matrix becomes
-!--    
-!--    rotation = [ cosine   -sine  ]
-!--               [  sine    cosine ]
-       rotation = RESHAPE( (/cosine, sine, -sine, cosine/), (/2, 2/) )
-
-       DO i = LBOUND(x, 1), UBOUND(x, 1)
-
-          v_rot(:) = MATMUL(rotation, (/x(i), y(i)/))
-
-          x(i) = v_rot(1)
-          y(i) = v_rot(2)
-
-       ENDDO
-
-    END SUBROUTINE rotate_vector_field
+ SUBROUTINE rotate_vector_field(x, y, angle)
+    REAL(wp), DIMENSION(:), INTENT(INOUT) :: x, y  !< x and y coodrinate in arbitrary units
+    REAL(wp), INTENT(IN)                  :: angle !< rotation angle [deg]
+
+    INTEGER  :: i
+    REAL(wp) :: sine, cosine, v_rot(2), rotation(2,2)
+
+    sine = SIN(angle * TO_RADIANS)
+    cosine = COS(angle * TO_RADIANS)
+!
+!-- RESAHPE() fills columns first, so the rotation matrix becomes
+!-- 
+!-- rotation = [ cosine   -sine  ]
+!--            [  sine    cosine ]
+    rotation = RESHAPE( (/cosine, sine, -sine, cosine/), (/2, 2/) )
+
+    DO  i = LBOUND(x, 1), UBOUND(x, 1)
+
+       v_rot(:) = MATMUL(rotation, (/x(i), y(i)/))
+
+       x(i) = v_rot(1)
+       y(i) = v_rot(2)
+
+    ENDDO
+
+ END SUBROUTINE rotate_vector_field
 
 
@@ -847,15 +852,15 @@
 !>    https://www.dwd.de/SharedDocs/downloads/DE/modelldokumentationen/nwv/cosmo_d2/cosmo_d2_dbbeschr_aktuell.pdf?__blob=publicationFile&v=2
 !------------------------------------------------------------------------------!
-    FUNCTION meridian_convergence_rotated(phi_n, lam_n, phi_g, lam_g)          &
-       RESULT(delta)
-
-       REAL(dp), INTENT(IN) ::  phi_n, lam_n, phi_g, lam_g
-       REAL(dp)             ::  delta
-
-       delta = atan2( COS(phi_n) * SIN(lam_n - lam_g),                         &
-                      COS(phi_g) * SIN(phi_n) -                                &
-                      SIN(phi_g) * COS(phi_n) * COS(lam_n - lam_g) )
-
-    END FUNCTION meridian_convergence_rotated
+ FUNCTION meridian_convergence_rotated(phi_n, lam_n, phi_g, lam_g)          &
+    RESULT(delta)
+
+    REAL(wp), INTENT(IN) ::  phi_n, lam_n, phi_g, lam_g
+    REAL(wp)             ::  delta
+
+    delta = atan2( COS(phi_n) * SIN(lam_n - lam_g),                         &
+                   COS(phi_g) * SIN(phi_n) -                                &
+                   SIN(phi_g) * COS(phi_n) * COS(lam_n - lam_g) )
+
+ END FUNCTION meridian_convergence_rotated
 
 !------------------------------------------------------------------------------!
@@ -903,56 +908,56 @@
 !> 
 !------------------------------------------------------------------------------!
-    SUBROUTINE find_horizontal_neighbours(cosmo_lat, cosmo_lon,                &
-                                          palm_clat, palm_clon,                &
-                                          palm_ii, palm_jj)
-
-       REAL(dp), DIMENSION(0:), INTENT(IN)        ::  cosmo_lat, cosmo_lon
-       REAL(dp), DIMENSION(0:,0:), INTENT(IN)     ::  palm_clat, palm_clon
-       REAL(dp)                                   ::  cosmo_dxi, cosmo_dyi
-       INTEGER, DIMENSION(0:,0:,1:), INTENT(OUT)  ::  palm_ii, palm_jj
-
-       REAL(dp) ::  lonpos, latpos, lon0, lat0
-       INTEGER  ::  i, j
-
-       lon0 = cosmo_lon(0)
-       lat0 = cosmo_lat(0)
-       cosmo_dxi = 1.0_dp / (cosmo_lon(1) - cosmo_lon(0))
-       cosmo_dyi = 1.0_dp / (cosmo_lat(1) - cosmo_lat(0))
-
-       DO j = 0, UBOUND(palm_clon, 2)!palm_grid % ny
-       DO i = 0, UBOUND(palm_clon, 1)!palm_grid % nx
-!
-!--       Compute the floating point index corrseponding to PALM-4U grid point
-!--       location along target grid (COSMO-DE) axes.
-          lonpos = (palm_clon(i,j) - lon0) * cosmo_dxi
-          latpos = (palm_clat(i,j) - lat0) * cosmo_dyi
-
-          IF (lonpos < 0.0_dp .OR. latpos < 0.0_dp)  THEN
-             message = "lonpos or latpos out of bounds " //                    &
-                "while finding interpolation neighbours!" // NEW_LINE(' ') //  &
-                "          (i,j) = (" //                                       &
-                TRIM(str(i)) // ", " // TRIM(str(j)) // ")" // NEW_LINE(' ') //&
-                "          lonpos " // TRIM(real_to_str(lonpos*TO_DEGREES)) // &
-                ", latpos " // TRIM(real_to_str(latpos*TO_DEGREES)) // NEW_LINE(' ') // &
-                "          lon0 " // TRIM(real_to_str(lon0  *TO_DEGREES)) //   &
-                ", lat0   " // TRIM(real_to_str(lat0*TO_DEGREES)) // NEW_LINE(' ') // &
-                "          PALM lon " // TRIM(real_to_str(palm_clon(i,j)*TO_DEGREES)) // &
-                ", PALM lat " // TRIM(real_to_str(palm_clat(i,j)*TO_DEGREES))
-             CALL inifor_abort('find_horizontal_neighbours', message)
-          ENDIF
-
-          palm_ii(i,j,1) = FLOOR(lonpos)
-          palm_ii(i,j,2) = FLOOR(lonpos)
-          palm_ii(i,j,3) = CEILING(lonpos)
-          palm_ii(i,j,4) = CEILING(lonpos)
-
-          palm_jj(i,j,1) = FLOOR(latpos)
-          palm_jj(i,j,2) = CEILING(latpos)
-          palm_jj(i,j,3) = CEILING(latpos)
-          palm_jj(i,j,4) = FLOOR(latpos)
-       ENDDO
-       ENDDO
-
-    END SUBROUTINE find_horizontal_neighbours
+ SUBROUTINE find_horizontal_neighbours(cosmo_lat, cosmo_lon,                &
+                                       palm_clat, palm_clon,                &
+                                       palm_ii, palm_jj)
+
+    REAL(wp), DIMENSION(0:), INTENT(IN)        ::  cosmo_lat, cosmo_lon
+    REAL(wp), DIMENSION(0:,0:), INTENT(IN)     ::  palm_clat, palm_clon
+    REAL(wp)                                   ::  cosmo_dxi, cosmo_dyi
+    INTEGER, DIMENSION(0:,0:,1:), INTENT(OUT)  ::  palm_ii, palm_jj
+
+    REAL(wp) ::  lonpos, latpos, lon0, lat0
+    INTEGER  ::  i, j
+
+    lon0 = cosmo_lon(0)
+    lat0 = cosmo_lat(0)
+    cosmo_dxi = 1.0_wp / (cosmo_lon(1) - cosmo_lon(0))
+    cosmo_dyi = 1.0_wp / (cosmo_lat(1) - cosmo_lat(0))
+
+    DO  j = 0, UBOUND(palm_clon, 2)!palm_grid%ny
+    DO  i = 0, UBOUND(palm_clon, 1)!palm_grid%nx
+!
+!--    Compute the floating point index corrseponding to PALM-4U grid point
+!--    location along target grid (COSMO-DE) axes.
+       lonpos = (palm_clon(i,j) - lon0) * cosmo_dxi
+       latpos = (palm_clat(i,j) - lat0) * cosmo_dyi
+
+       IF (lonpos < 0.0_wp .OR. latpos < 0.0_wp)  THEN
+          message = "lonpos or latpos out of bounds " //                    &
+             "while finding interpolation neighbours!" // NEW_LINE(' ') //  &
+             "          (i,j) = (" //                                       &
+             TRIM(str(i)) // ", " // TRIM(str(j)) // ")" // NEW_LINE(' ') //&
+             "          lonpos " // TRIM(real_to_str(lonpos*TO_DEGREES)) // &
+             ", latpos " // TRIM(real_to_str(latpos*TO_DEGREES)) // NEW_LINE(' ') // &
+             "          lon0 " // TRIM(real_to_str(lon0  *TO_DEGREES)) //   &
+             ", lat0   " // TRIM(real_to_str(lat0*TO_DEGREES)) // NEW_LINE(' ') // &
+             "          PALM lon " // TRIM(real_to_str(palm_clon(i,j)*TO_DEGREES)) // &
+             ", PALM lat " // TRIM(real_to_str(palm_clat(i,j)*TO_DEGREES))
+          CALL inifor_abort('find_horizontal_neighbours', message)
+       ENDIF
+
+       palm_ii(i,j,1) = FLOOR(lonpos)
+       palm_ii(i,j,2) = FLOOR(lonpos)
+       palm_ii(i,j,3) = CEILING(lonpos)
+       palm_ii(i,j,4) = CEILING(lonpos)
+
+       palm_jj(i,j,1) = FLOOR(latpos)
+       palm_jj(i,j,2) = CEILING(latpos)
+       palm_jj(i,j,3) = CEILING(latpos)
+       palm_jj(i,j,4) = FLOOR(latpos)
+    ENDDO
+    ENDDO
+
+ END SUBROUTINE find_horizontal_neighbours
 
     
@@ -963,106 +968,106 @@
 !> column of the given palm grid.
 !------------------------------------------------------------------------------!
-    SUBROUTINE find_vertical_neighbours_and_weights_interp( palm_grid,         &
+ SUBROUTINE find_vertical_neighbours_and_weights_interp( palm_grid,         &
                                                             palm_intermediate )
-       TYPE(grid_definition), INTENT(INOUT) ::  palm_grid
-       TYPE(grid_definition), INTENT(IN)    ::  palm_intermediate
-
-       INTEGER  ::  i, j, k, nx, ny, nz, nlev, k_intermediate
-       LOGICAL  ::  point_is_below_grid, point_is_above_grid,                  &
-                    point_is_in_current_cell
-       REAL(dp) ::  current_height, column_base, column_top, h_top, h_bottom,  &
-                    weight
-
-       nx   = palm_grid % nx
-       ny   = palm_grid % ny
-       nz   = palm_grid % nz
-       nlev = palm_intermediate % nz
-
-!
-!--    in each column of the fine grid, find vertical neighbours of every cell
-       DO j = 0, ny
-       DO i = 0, nx
-
-          k_intermediate = 0
-
-          column_base = palm_intermediate % h(i,j,0)
-          column_top  = palm_intermediate % h(i,j,nlev)
-
-!
-!--       scan through palm_grid column and set neighbour indices in
-!--       case current_height is either below column_base, in the current
-!--       cell, or above column_top. Keep increasing current cell index until
-!--       the current cell overlaps with the current_height.
-          DO k = 1, nz
-
-!
-!--          Memorize the top and bottom boundaries of the coarse cell and the
-!--          current height within it
-             current_height = palm_grid % z(k) + palm_grid % z0
-             h_top    = palm_intermediate % h(i,j,k_intermediate+1)
-             h_bottom = palm_intermediate % h(i,j,k_intermediate)
-
-             point_is_above_grid = (current_height > column_top) !22000m, very unlikely
-             point_is_below_grid = (current_height < column_base)
-
-             point_is_in_current_cell = (                                      &
-                current_height >= h_bottom .AND.                               &
-                current_height <  h_top                                        &
-             )
-
-!
-!--          set default weights
-             palm_grid % w_verti(i,j,k,1:2) = 0.0_dp
-
-             IF (point_is_above_grid)  THEN
-
-                palm_grid % kk(i,j,k,1:2) = nlev
-                palm_grid % w_verti(i,j,k,1:2) = - 2.0_dp
-
-                message = "PALM-4U grid extends above COSMO-DE model top."
-                CALL inifor_abort('find_vertical_neighbours_and_weights', message)
-
-             ELSE IF (point_is_below_grid)  THEN
-
-                palm_grid % kk(i,j,k,1:2) = 0
-                palm_grid % w_verti(i,j,k,1:2) = - 2.0_dp
-
-             ELSE
-!
-!--             cycle through intermediate levels until current
-!--             intermediate-grid cell overlaps with current_height
-                DO WHILE (.NOT. point_is_in_current_cell .AND. k_intermediate <= nlev-1)
-                   k_intermediate = k_intermediate + 1
-
-                   h_top    = palm_intermediate % h(i,j,k_intermediate+1)
-                   h_bottom = palm_intermediate % h(i,j,k_intermediate)
-                   point_is_in_current_cell = (                                &
-                      current_height >= h_bottom .AND.                         &
-                      current_height <  h_top                                  &
-                   )
-                ENDDO
-
-                IF (k_intermediate > nlev-1)  THEN
-                   message = "Index " // TRIM(str(k_intermediate)) //          &
-                             " is above intermediate grid range."
-                   CALL inifor_abort('find_vertical_neighbours', message)
-                ENDIF
+    TYPE(grid_definition), INTENT(INOUT) ::  palm_grid
+    TYPE(grid_definition), INTENT(IN)    ::  palm_intermediate
+
+    INTEGER  ::  i, j, k, nx, ny, nz, nlev, k_intermediate
+    LOGICAL  ::  point_is_below_grid, point_is_above_grid,                  &
+                 point_is_in_current_cell
+    REAL(wp) ::  current_height, column_base, column_top, h_top, h_bottom,  &
+                 weight
+
+    nx   = palm_grid%nx
+    ny   = palm_grid%ny
+    nz   = palm_grid%nz
+    nlev = palm_intermediate%nz
+
+!
+!-- in each column of the fine grid, find vertical neighbours of every cell
+    DO  j = 0, ny
+    DO  i = 0, nx
+
+       k_intermediate = 0
+
+       column_base = palm_intermediate%h(i,j,0)
+       column_top  = palm_intermediate%h(i,j,nlev)
+
+!
+!--    scan through palm_grid column and set neighbour indices in
+!--    case current_height is either below column_base, in the current
+!--    cell, or above column_top. Keep increasing current cell index until
+!--    the current cell overlaps with the current_height.
+       DO  k = 1, nz
+
+!
+!--       Memorize the top and bottom boundaries of the coarse cell and the
+!--       current height within it
+          current_height = palm_grid%z(k) + palm_grid%z0
+          h_top    = palm_intermediate%h(i,j,k_intermediate+1)
+          h_bottom = palm_intermediate%h(i,j,k_intermediate)
+
+          point_is_above_grid = (current_height > column_top) !22000m, very unlikely
+          point_is_below_grid = (current_height < column_base)
+
+          point_is_in_current_cell = (                                      &
+             current_height >= h_bottom .AND.                               &
+             current_height <  h_top                                        &
+          )
+
+!
+!--       set default weights
+          palm_grid%w_verti(i,j,k,1:2) = 0.0_wp
+
+          IF (point_is_above_grid)  THEN
+
+             palm_grid%kk(i,j,k,1:2) = nlev
+             palm_grid%w_verti(i,j,k,1:2) = - 2.0_wp
+
+             message = "PALM-4U grid extends above COSMO-DE model top."
+             CALL inifor_abort('find_vertical_neighbours_and_weights', message)
+
+          ELSE IF (point_is_below_grid)  THEN
+
+             palm_grid%kk(i,j,k,1:2) = 0
+             palm_grid%w_verti(i,j,k,1:2) = - 2.0_wp
+
+          ELSE
+!
+!--          cycle through intermediate levels until current
+!--          intermediate-grid cell overlaps with current_height
+             DO  WHILE (.NOT. point_is_in_current_cell .AND. k_intermediate <= nlev-1)
+                k_intermediate = k_intermediate + 1
+
+                h_top    = palm_intermediate%h(i,j,k_intermediate+1)
+                h_bottom = palm_intermediate%h(i,j,k_intermediate)
+                point_is_in_current_cell = (                                &
+                   current_height >= h_bottom .AND.                         &
+                   current_height <  h_top                                  &
+                )
+             ENDDO
+
+             IF (k_intermediate > nlev-1)  THEN
+                message = "Index " // TRIM(str(k_intermediate)) //          &
+                          " is above intermediate grid range."
+                CALL inifor_abort('find_vertical_neighbours', message)
+             ENDIF
    
-                palm_grid % kk(i,j,k,1) = k_intermediate
-                palm_grid % kk(i,j,k,2) = k_intermediate + 1
-
-!
-!--             compute vertical weights
-                weight = (h_top - current_height) / (h_top - h_bottom)
-                palm_grid % w_verti(i,j,k,1) = weight
-                palm_grid % w_verti(i,j,k,2) = 1.0_dp - weight
-             ENDIF
-
-          ENDDO
+             palm_grid%kk(i,j,k,1) = k_intermediate
+             palm_grid%kk(i,j,k,2) = k_intermediate + 1
+
+!
+!--          compute vertical weights
+             weight = (h_top - current_height) / (h_top - h_bottom)
+             palm_grid%w_verti(i,j,k,1) = weight
+             palm_grid%w_verti(i,j,k,2) = 1.0_wp - weight
+          ENDIF
 
        ENDDO
-       ENDDO
-
-    END SUBROUTINE find_vertical_neighbours_and_weights_interp
+
+    ENDDO
+    ENDDO
+
+ END SUBROUTINE find_vertical_neighbours_and_weights_interp
 
 
@@ -1079,130 +1084,130 @@
 !> iii(:) and jjj(:).
 !------------------------------------------------------------------------------!
-    SUBROUTINE find_vertical_neighbours_and_weights_average(                   &
-       avg_grid, level_based_averaging                                         &
-    )
-
-       TYPE(grid_definition), INTENT(INOUT), TARGET ::  avg_grid
-       LOGICAL                                      ::  level_based_averaging
-
-       INTEGER           ::  i, j, k_palm, k_intermediate, l, nlev
-       LOGICAL           ::  point_is_below_grid, point_is_above_grid,         &
-                             point_is_in_current_cell
-       REAL(dp)          ::  current_height, column_base, column_top, h_top,   &
-                             h_bottom, weight
-       REAL(dp), POINTER ::  cosmo_h(:,:,:)
-
-
-       avg_grid % k_min = LBOUND(avg_grid % z, 1)
-
-       nlev = SIZE(avg_grid % cosmo_h, 3)
+ SUBROUTINE find_vertical_neighbours_and_weights_average(                   &
+    avg_grid, level_based_averaging                                         &
+ )
+
+    TYPE(grid_definition), INTENT(INOUT), TARGET ::  avg_grid
+    LOGICAL                                      ::  level_based_averaging
+
+    INTEGER           ::  i, j, k_palm, k_intermediate, l, nlev
+    LOGICAL           ::  point_is_below_grid, point_is_above_grid,         &
+                          point_is_in_current_cell
+    REAL(wp)          ::  current_height, column_base, column_top, h_top,   &
+                          h_bottom, weight
+    REAL(wp), POINTER ::  cosmo_h(:,:,:)
+
+
+    avg_grid%k_min = LBOUND(avg_grid%z, 1)
+
+    nlev = SIZE(avg_grid%cosmo_h, 3)
+
+    IF (level_based_averaging)  THEN
+       cosmo_h => avg_grid%h
+    ELSE
+       cosmo_h => avg_grid%cosmo_h
+    ENDIF
+
+!
+!-- in each column of the fine grid, find vertical neighbours of every cell
+    DO  l = 1, avg_grid%n_columns
 
        IF (level_based_averaging)  THEN
-          cosmo_h => avg_grid % h
+          i = 1
+          j = 1
        ELSE
-          cosmo_h => avg_grid % cosmo_h
+          i = avg_grid%iii(l)
+          j = avg_grid%jjj(l)
        ENDIF
 
-!
-!--    in each column of the fine grid, find vertical neighbours of every cell
-       DO l = 1, avg_grid % n_columns
-
-          IF (level_based_averaging)  THEN
-             i = 1
-             j = 1
+       column_base = cosmo_h(i,j,1)
+       column_top  = cosmo_h(i,j,nlev)
+
+!
+!--    scan through avg_grid column until and set neighbour indices in
+!--    case current_height is either below column_base, in the current
+!--    cell, or above column_top. Keep increasing current cell index until
+!--    the current cell overlaps with the current_height.
+       k_intermediate = 1 !avg_grid%cosmo_h is indezed 1-based.
+       DO  k_palm = 1, avg_grid%nz
+
+!
+!--       Memorize the top and bottom boundaries of the coarse cell and the
+!--       current height within it
+          current_height = avg_grid%z(k_palm) + avg_grid%z0
+          h_top    = cosmo_h(i,j,k_intermediate+1)
+          h_bottom = cosmo_h(i,j,k_intermediate)
+
+!
+!--       COSMO column top is located at 22000m, point_is_above_grid is very
+!--       unlikely.
+          point_is_above_grid = (current_height > column_top)
+          point_is_below_grid = (current_height < column_base)
+
+          point_is_in_current_cell = (                                      &
+             current_height >= h_bottom .AND.                               &
+             current_height <  h_top                                        &
+          )
+
+!
+!--       set default weights
+          avg_grid%w(l,k_palm,1:2) = 0.0_wp
+
+          IF (point_is_above_grid)  THEN
+
+             avg_grid%kkk(l,k_palm,1:2) = nlev
+             avg_grid%w(l,k_palm,1:2) = - 2.0_wp
+
+             message = "PALM-4U grid extends above COSMO-DE model top."
+             CALL inifor_abort('find_vertical_neighbours_and_weights_average', message)
+
+          ELSE IF (point_is_below_grid)  THEN
+
+             avg_grid%kkk(l,k_palm,1:2) = 0
+             avg_grid%w(l,k_palm,1:2) = - 2.0_wp
+             avg_grid%k_min = MAX(k_palm + 1, avg_grid%k_min)
           ELSE
-             i = avg_grid % iii(l)
-             j = avg_grid % jjj(l)
+!
+!--          cycle through intermediate levels until current
+!--          intermediate-grid cell overlaps with current_height
+             DO  WHILE (.NOT. point_is_in_current_cell .AND. k_intermediate <= nlev-1)
+                k_intermediate = k_intermediate + 1
+
+                h_top    = cosmo_h(i,j,k_intermediate+1)
+                h_bottom = cosmo_h(i,j,k_intermediate)
+                point_is_in_current_cell = (                                &
+                   current_height >= h_bottom .AND.                         &
+                   current_height <  h_top                                  &
+                )
+             ENDDO
+
+!
+!--          k_intermediate = 48 indicates the last section (indices 48 and 49), i.e.
+!--          k_intermediate = 49 is not the beginning of a valid cell.
+             IF (k_intermediate > nlev-1)  THEN
+                message = "Index " // TRIM(str(k_intermediate)) //          &
+                          " is above intermediate grid range."
+                CALL inifor_abort('find_vertical_neighbours', message)
+             ENDIF
+   
+             avg_grid%kkk(l,k_palm,1) = k_intermediate
+             avg_grid%kkk(l,k_palm,2) = k_intermediate + 1
+
+!
+!--          compute vertical weights
+             weight = (h_top - current_height) / (h_top - h_bottom)
+             avg_grid%w(l,k_palm,1) = weight
+             avg_grid%w(l,k_palm,2) = 1.0_wp - weight
           ENDIF
 
-          column_base = cosmo_h(i,j,1)
-          column_top  = cosmo_h(i,j,nlev)
-
-!
-!--       scan through avg_grid column until and set neighbour indices in
-!--       case current_height is either below column_base, in the current
-!--       cell, or above column_top. Keep increasing current cell index until
-!--       the current cell overlaps with the current_height.
-          k_intermediate = 1 !avg_grid % cosmo_h is indezed 1-based.
-          DO k_palm = 1, avg_grid % nz
-
-!
-!--          Memorize the top and bottom boundaries of the coarse cell and the
-!--          current height within it
-             current_height = avg_grid % z(k_palm) + avg_grid % z0
-             h_top    = cosmo_h(i,j,k_intermediate+1)
-             h_bottom = cosmo_h(i,j,k_intermediate)
-
-!
-!--          COSMO column top is located at 22000m, point_is_above_grid is very
-!--          unlikely.
-             point_is_above_grid = (current_height > column_top)
-             point_is_below_grid = (current_height < column_base)
-
-             point_is_in_current_cell = (                                      &
-                current_height >= h_bottom .AND.                               &
-                current_height <  h_top                                        &
-             )
-
-!
-!--          set default weights
-             avg_grid % w(l,k_palm,1:2) = 0.0_dp
-
-             IF (point_is_above_grid)  THEN
-
-                avg_grid % kkk(l,k_palm,1:2) = nlev
-                avg_grid % w(l,k_palm,1:2) = - 2.0_dp
-
-                message = "PALM-4U grid extends above COSMO-DE model top."
-                CALL inifor_abort('find_vertical_neighbours_and_weights_average', message)
-
-             ELSE IF (point_is_below_grid)  THEN
-
-                avg_grid % kkk(l,k_palm,1:2) = 0
-                avg_grid % w(l,k_palm,1:2) = - 2.0_dp
-                avg_grid % k_min = MAX(k_palm + 1, avg_grid % k_min)
-             ELSE
-!
-!--             cycle through intermediate levels until current
-!--             intermediate-grid cell overlaps with current_height
-                DO WHILE (.NOT. point_is_in_current_cell .AND. k_intermediate <= nlev-1)
-                   k_intermediate = k_intermediate + 1
-
-                   h_top    = cosmo_h(i,j,k_intermediate+1)
-                   h_bottom = cosmo_h(i,j,k_intermediate)
-                   point_is_in_current_cell = (                                &
-                      current_height >= h_bottom .AND.                         &
-                      current_height <  h_top                                  &
-                   )
-                ENDDO
-
-!
-!--             k_intermediate = 48 indicates the last section (indices 48 and 49), i.e.
-!--             k_intermediate = 49 is not the beginning of a valid cell.
-                IF (k_intermediate > nlev-1)  THEN
-                   message = "Index " // TRIM(str(k_intermediate)) //          &
-                             " is above intermediate grid range."
-                   CALL inifor_abort('find_vertical_neighbours', message)
-                ENDIF
-   
-                avg_grid % kkk(l,k_palm,1) = k_intermediate
-                avg_grid % kkk(l,k_palm,2) = k_intermediate + 1
-
-!
-!--             compute vertical weights
-                weight = (h_top - current_height) / (h_top - h_bottom)
-                avg_grid % w(l,k_palm,1) = weight
-                avg_grid % w(l,k_palm,2) = 1.0_dp - weight
-             ENDIF
-
-!
-!--       Loop over PALM levels k
-          ENDDO
-
-!
-!--       Loop over averaging columns l
+!
+!--    Loop over PALM levels k
        ENDDO
+
+!
+!--    Loop over averaging columns l
+    ENDDO
  
-    END SUBROUTINE find_vertical_neighbours_and_weights_average
+ END SUBROUTINE find_vertical_neighbours_and_weights_average
 
 !------------------------------------------------------------------------------!
@@ -1215,19 +1220,19 @@
 !> Input parameters:
 !> -----------------
-!> palm_grid % clon : longitudes of PALM-4U scalars (cell centres) in COSMO-DE's rotated-pole grid [rad]
-!>
-!> palm_grid % clat : latitudes of PALM-4U cell centres in COSMO-DE's rotated-pole grid [rad]
-!>
-!> cosmo_grid % lon : rotated-pole longitudes of scalars (cell centres) of the COSMO-DE grid [rad] 
-!>
-!> cosmo_grid % lat : rotated-pole latitudes of scalars (cell centers) of the COSMO-DE grid [rad]
-!>
-!> cosmo_grid % dxi : inverse grid spacing in the first dimension [m^-1]
-!>
-!> cosmo_grid % dyi : inverse grid spacing in the second dimension [m^-1]
+!> palm_grid%clon : longitudes of PALM-4U scalars (cell centres) in COSMO-DE's rotated-pole grid [rad]
+!>
+!> palm_grid%clat : latitudes of PALM-4U cell centres in COSMO-DE's rotated-pole grid [rad]
+!>
+!> cosmo_grid%lon : rotated-pole longitudes of scalars (cell centres) of the COSMO-DE grid [rad] 
+!>
+!> cosmo_grid%lat : rotated-pole latitudes of scalars (cell centers) of the COSMO-DE grid [rad]
+!>
+!> cosmo_grid%dxi : inverse grid spacing in the first dimension [m^-1]
+!>
+!> cosmo_grid%dyi : inverse grid spacing in the second dimension [m^-1]
 !>
 !> Output parameters:
 !> ------------------
-!> palm_grid % w_horiz(:,:,1-4) : weights for bilinear horizontal interpolation
+!> palm_grid%w_horiz(:,:,1-4) : weights for bilinear horizontal interpolation
 !
 !                               COSMO-DE grid
@@ -1251,51 +1256,51 @@
 !          
 !------------------------------------------------------------------------------!
-    SUBROUTINE compute_horizontal_interp_weights(cosmo_lat, cosmo_lon,         &
-       palm_clat, palm_clon, palm_ii, palm_jj, palm_w_horiz)
+ SUBROUTINE compute_horizontal_interp_weights(cosmo_lat, cosmo_lon,         &
+    palm_clat, palm_clon, palm_ii, palm_jj, palm_w_horiz)
        
-       REAL(dp), DIMENSION(0:), INTENT(IN)        ::  cosmo_lat, cosmo_lon
-       REAL(dp)                                   ::  cosmo_dxi, cosmo_dyi
-       REAL(dp), DIMENSION(0:,0:), INTENT(IN)     ::  palm_clat, palm_clon
-       INTEGER, DIMENSION(0:,0:,1:), INTENT(IN)   ::  palm_ii, palm_jj
-
-       REAL(dp), DIMENSION(0:,0:,1:), INTENT(OUT) ::  palm_w_horiz
-
-       REAL(dp) ::  wl, wp
-       INTEGER  ::  i, j
-
-       cosmo_dxi = 1.0_dp / (cosmo_lon(1) - cosmo_lon(0))
-       cosmo_dyi = 1.0_dp / (cosmo_lat(1) - cosmo_lat(0))
-
-       DO j = 0, UBOUND(palm_clon, 2)
-       DO i = 0, UBOUND(palm_clon, 1)
-      
-!
-!--       weight in lambda direction
-          wl = ( cosmo_lon(palm_ii(i,j,4)) - palm_clon(i,j) ) * cosmo_dxi
-
-!
-!--       weight in phi direction
-          wp = ( cosmo_lat(palm_jj(i,j,2)) - palm_clat(i,j) ) * cosmo_dyi
-
-          IF (wl > 1.0_dp .OR. wl < 0.0_dp)  THEN
-              message = "Horizontal weight wl = " // TRIM(real_to_str(wl)) //   &
-                        " is out bounds."
-              CALL inifor_abort('compute_horizontal_interp_weights', message)
-          ENDIF
-          IF (wp > 1.0_dp .OR. wp < 0.0_dp)  THEN
-              message = "Horizontal weight wp = " // TRIM(real_to_str(wp)) //   &
-                        " is out bounds."
-              CALL inifor_abort('compute_horizontal_interp_weights', message)
-          ENDIF
-
-          palm_w_horiz(i,j,1) = wl * wp
-          palm_w_horiz(i,j,2) = wl * (1.0_dp - wp)
-          palm_w_horiz(i,j,3) = (1.0_dp - wl) * (1.0_dp - wp)
-          palm_w_horiz(i,j,4) = 1.0_dp - SUM( palm_w_horiz(i,j,1:3) )
-
-       ENDDO
-       ENDDO
+    REAL(wp), DIMENSION(0:), INTENT(IN)        ::  cosmo_lat, cosmo_lon
+    REAL(wp)                                   ::  cosmo_dxi, cosmo_dyi
+    REAL(wp), DIMENSION(0:,0:), INTENT(IN)     ::  palm_clat, palm_clon
+    INTEGER, DIMENSION(0:,0:,1:), INTENT(IN)   ::  palm_ii, palm_jj
+
+    REAL(wp), DIMENSION(0:,0:,1:), INTENT(OUT) ::  palm_w_horiz
+
+    REAL(wp) ::  wlambda, wphi
+    INTEGER  ::  i, j
+
+    cosmo_dxi = 1.0_wp / (cosmo_lon(1) - cosmo_lon(0))
+    cosmo_dyi = 1.0_wp / (cosmo_lat(1) - cosmo_lat(0))
+
+    DO  j = 0, UBOUND(palm_clon, 2)
+    DO  i = 0, UBOUND(palm_clon, 1)
+    
+!
+!--    weight in lambda direction
+       wlambda = ( cosmo_lon(palm_ii(i,j,4)) - palm_clon(i,j) ) * cosmo_dxi
+
+!
+!--    weight in phi direction
+       wphi = ( cosmo_lat(palm_jj(i,j,2)) - palm_clat(i,j) ) * cosmo_dyi
+
+       IF (wlambda > 1.0_wp .OR. wlambda < 0.0_wp)  THEN
+           message = "Horizontal weight wlambda = " // TRIM(real_to_str(wlambda)) //   &
+                     " is out bounds."
+           CALL inifor_abort('compute_horizontal_interp_weights', message)
+       ENDIF
+       IF (wphi > 1.0_wp .OR. wphi < 0.0_wp)  THEN
+           message = "Horizontal weight wphi = " // TRIM(real_to_str(wphi)) //   &
+                     " is out bounds."
+           CALL inifor_abort('compute_horizontal_interp_weights', message)
+       ENDIF
+
+       palm_w_horiz(i,j,1) = wlambda * wphi
+       palm_w_horiz(i,j,2) = wlambda * (1.0_wp - wphi)
+       palm_w_horiz(i,j,3) = (1.0_wp - wlambda) * (1.0_wp - wphi)
+       palm_w_horiz(i,j,4) = 1.0_wp - SUM( palm_w_horiz(i,j,1:3) )
+
+    ENDDO
+    ENDDO
        
-    END SUBROUTINE compute_horizontal_interp_weights
+ END SUBROUTINE compute_horizontal_interp_weights
 
 
@@ -1311,18 +1316,18 @@
 !> which means the first centre point has to be omitted and is set to zero.
 !------------------------------------------------------------------------------!
-    SUBROUTINE centre_velocities(u_face, v_face, u_centre, v_centre)
-       REAL(dp), DIMENSION(0:,0:,0:), INTENT(IN)  ::  u_face, v_face
-       REAL(dp), DIMENSION(0:,0:,0:), INTENT(OUT) ::  u_centre, v_centre
-       INTEGER ::  nx, ny
-
-       nx = UBOUND(u_face, 1)
-       ny = UBOUND(u_face, 2)
-
-       u_centre(0,:,:)  = 0.0_dp
-       u_centre(1:,:,:) = 0.5_dp * ( u_face(0:nx-1,:,:) + u_face(1:,:,:) )
-
-       v_centre(:,0,:)  = 0.0_dp
-       v_centre(:,1:,:) = 0.5_dp * ( v_face(:,0:ny-1,:) + v_face(:,1:,:) )
-    END SUBROUTINE centre_velocities
+ SUBROUTINE centre_velocities(u_face, v_face, u_centre, v_centre)
+    REAL(wp), DIMENSION(0:,0:,0:), INTENT(IN)  ::  u_face, v_face
+    REAL(wp), DIMENSION(0:,0:,0:), INTENT(OUT) ::  u_centre, v_centre
+    INTEGER ::  nx, ny
+
+    nx = UBOUND(u_face, 1)
+    ny = UBOUND(u_face, 2)
+
+    u_centre(0,:,:)  = 0.0_wp
+    u_centre(1:,:,:) = 0.5_wp * ( u_face(0:nx-1,:,:) + u_face(1:,:,:) )
+
+    v_centre(:,0,:)  = 0.0_wp
+    v_centre(:,1:,:) = 0.5_wp * ( v_face(:,0:ny-1,:) + v_face(:,1:,:) )
+ END SUBROUTINE centre_velocities
 
 
@@ -1332,130 +1337,130 @@
 !> Compute the geographical latitude of a point given in rotated-pole cordinates
 !------------------------------------------------------------------------------!
-    FUNCTION phirot2phi (phirot, rlarot, polphi, polgam)
-    
-       REAL(dp), INTENT (IN) ::  polphi      !< latitude of the rotated north pole
-       REAL(dp), INTENT (IN) ::  phirot      !< latitude in the rotated system
-       REAL(dp), INTENT (IN) ::  rlarot      !< longitude in the rotated system
-       REAL(dp), INTENT (IN) ::  polgam      !< angle between the north poles of the systems
-
-       REAL(dp)              ::  phirot2phi  !< latitude in the geographical system
-       
-       REAL(dp)              ::  zsinpol, zcospol, zphis, zrlas, zarg, zgam
-    
-       zsinpol = SIN(polphi * TO_RADIANS)
-       zcospol = COS(polphi * TO_RADIANS)
-       zphis   = phirot * TO_RADIANS
-
-       IF (rlarot > 180.0_dp)  THEN
-          zrlas = rlarot - 360.0_dp
-       ELSE
-          zrlas = rlarot
-       ENDIF
-       zrlas = zrlas * TO_RADIANS
+ FUNCTION phirot2phi (phirot, rlarot, polphi, polgam)
+ 
+    REAL(wp), INTENT (IN) ::  polphi      !< latitude of the rotated north pole
+    REAL(wp), INTENT (IN) ::  phirot      !< latitude in the rotated system
+    REAL(wp), INTENT (IN) ::  rlarot      !< longitude in the rotated system
+    REAL(wp), INTENT (IN) ::  polgam      !< angle between the north poles of the systems
+
+    REAL(wp)              ::  phirot2phi  !< latitude in the geographical system
+    
+    REAL(wp)              ::  zsinpol, zcospol, zphis, zrlas, zarg, zgam
+ 
+    zsinpol = SIN(polphi * TO_RADIANS)
+    zcospol = COS(polphi * TO_RADIANS)
+    zphis   = phirot * TO_RADIANS
+
+    IF (rlarot > 180.0_wp)  THEN
+       zrlas = rlarot - 360.0_wp
+    ELSE
+       zrlas = rlarot
+    ENDIF
+    zrlas = zrlas * TO_RADIANS
+  
+    IF (polgam /= 0.0_wp)  THEN
+       zgam = polgam * TO_RADIANS
+       zarg = zsinpol * SIN (zphis) +                                       &
+              zcospol * COS(zphis) * ( COS(zrlas) * COS(zgam) -             &
+                                       SIN(zgam)  * SIN(zrlas) )
+    ELSE
+       zarg = zcospol * COS (zphis) * COS (zrlas) + zsinpol * SIN (zphis)
+    ENDIF
+   
+    phirot2phi = ASIN (zarg) * TO_DEGREES
+ 
+ END FUNCTION phirot2phi
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Compute the geographical latitude of a point given in rotated-pole cordinates
+!------------------------------------------------------------------------------!
+ FUNCTION phi2phirot (phi, rla, polphi, pollam)
+ 
+    REAL(wp), INTENT (IN) ::  polphi !< latitude of the rotated north pole
+    REAL(wp), INTENT (IN) ::  pollam !< longitude of the rotated north pole
+    REAL(wp), INTENT (IN) ::  phi    !< latitude in the geographical system
+    REAL(wp), INTENT (IN) ::  rla    !< longitude in the geographical system
+    
+    REAL(wp) ::  phi2phirot          !< longitude in the rotated system
+    
+    REAL(wp) ::  zsinpol, zcospol, zlampol, zphi, zrla, zarg1, zarg2, zrla1
+    
+    zsinpol = SIN(polphi * TO_RADIANS)
+    zcospol = COS(polphi * TO_RADIANS)
+    zlampol = pollam * TO_RADIANS
+    zphi    = phi * TO_RADIANS
+
+    IF (rla > 180.0_wp)  THEN
+       zrla1 = rla - 360.0_wp
+    ELSE
+       zrla1 = rla
+    ENDIF
+    zrla = zrla1 * TO_RADIANS
+    
+    zarg1 = SIN(zphi) * zsinpol
+    zarg2 = COS(zphi) * zcospol * COS(zrla - zlampol)
+    
+    phi2phirot = ASIN(zarg1 + zarg2) * TO_DEGREES
+ 
+ END FUNCTION phi2phirot
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Compute the geographical longitude of a point given in rotated-pole cordinates
+!------------------------------------------------------------------------------!
+ FUNCTION rlarot2rla(phirot, rlarot, polphi, pollam, polgam)
+ 
+    REAL(wp), INTENT (IN) ::  polphi !< latitude of the rotated north pole
+    REAL(wp), INTENT (IN) ::  pollam !< longitude of the rotated north pole
+    REAL(wp), INTENT (IN) ::  phirot !< latitude in the rotated system
+    REAL(wp), INTENT (IN) ::  rlarot !< longitude in the rotated system
+    REAL(wp), INTENT (IN) ::  polgam !< angle between the north poles of the systems
+    
+    REAL(wp) ::  rlarot2rla          !< latitude in the geographical system
+    
+    REAL(wp) ::  zsinpol, zcospol, zlampol, zphis, zrlas, zarg1, zarg2, zgam
+    
+    zsinpol = SIN(TO_RADIANS * polphi)
+    zcospol = COS(TO_RADIANS * polphi)
+    zlampol = TO_RADIANS * pollam
+    zphis   = TO_RADIANS * phirot
+
+    IF (rlarot > 180.0_wp)  THEN
+       zrlas = rlarot - 360.0_wp
+    ELSE
+       zrlas = rlarot
+    ENDIF
+    zrlas   = TO_RADIANS * zrlas
+   
+    IF (polgam /= 0.0_wp)  THEN
+       zgam  = TO_RADIANS * polgam
+       zarg1 = SIN(zlampol) * (zcospol * SIN(zphis) - zsinpol*COS(zphis) *  &
+               (COS(zrlas) * COS(zgam) - SIN(zrlas) * SIN(zgam)) ) -        &
+               COS(zlampol) * COS(zphis) * ( SIN(zrlas) * COS(zgam) +       &
+                                             COS(zrlas) * SIN(zgam) )
+    
+       zarg2 = COS (zlampol) * (zcospol * SIN(zphis) - zsinpol*COS(zphis) * &
+               (COS(zrlas) * COS(zgam) - SIN(zrlas) * SIN(zgam)) ) +        &
+               SIN(zlampol) * COS(zphis) * ( SIN(zrlas) * COS(zgam) +       &
+                                             COS(zrlas) * SIN(zgam) )
+    ELSE
+       zarg1   = SIN (zlampol) * (-zsinpol * COS(zrlas) * COS(zphis)  +     &
+                                   zcospol *              SIN(zphis)) -     &
+                 COS (zlampol) *             SIN(zrlas) * COS(zphis)
+       zarg2   = COS (zlampol) * (-zsinpol * COS(zrlas) * COS(zphis)  +     &
+                                   zcospol *              SIN(zphis)) +     &
+                 SIN (zlampol) *             SIN(zrlas) * COS(zphis)
+    ENDIF
+   
+    IF (zarg2 == 0.0_wp)  zarg2 = 1.0E-20_wp
+   
+    rlarot2rla = ATAN2(zarg1,zarg2) * TO_DEGREES
      
-       IF (polgam /= 0.0_dp)  THEN
-          zgam = polgam * TO_RADIANS
-          zarg = zsinpol * SIN (zphis) +                                       &
-                 zcospol * COS(zphis) * ( COS(zrlas) * COS(zgam) -             &
-                                          SIN(zgam)  * SIN(zrlas) )
-       ELSE
-          zarg = zcospol * COS (zphis) * COS (zrlas) + zsinpol * SIN (zphis)
-       ENDIF
-      
-       phirot2phi = ASIN (zarg) * TO_DEGREES
-    
-    END FUNCTION phirot2phi
-
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Compute the geographical latitude of a point given in rotated-pole cordinates
-!------------------------------------------------------------------------------!
-    FUNCTION phi2phirot (phi, rla, polphi, pollam)
-    
-       REAL(dp), INTENT (IN) ::  polphi !< latitude of the rotated north pole
-       REAL(dp), INTENT (IN) ::  pollam !< longitude of the rotated north pole
-       REAL(dp), INTENT (IN) ::  phi    !< latitude in the geographical system
-       REAL(dp), INTENT (IN) ::  rla    !< longitude in the geographical system
-       
-       REAL(dp) ::  phi2phirot          !< longitude in the rotated system
-       
-       REAL(dp) ::  zsinpol, zcospol, zlampol, zphi, zrla, zarg1, zarg2, zrla1
-       
-       zsinpol = SIN(polphi * TO_RADIANS)
-       zcospol = COS(polphi * TO_RADIANS)
-       zlampol = pollam * TO_RADIANS
-       zphi    = phi * TO_RADIANS
-
-       IF (rla > 180.0_dp)  THEN
-          zrla1 = rla - 360.0_dp
-       ELSE
-          zrla1 = rla
-       ENDIF
-       zrla = zrla1 * TO_RADIANS
-       
-       zarg1 = SIN(zphi) * zsinpol
-       zarg2 = COS(zphi) * zcospol * COS(zrla - zlampol)
-       
-       phi2phirot = ASIN(zarg1 + zarg2) * TO_DEGREES
-    
-    END FUNCTION phi2phirot
-
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Compute the geographical longitude of a point given in rotated-pole cordinates
-!------------------------------------------------------------------------------!
-    FUNCTION rlarot2rla(phirot, rlarot, polphi, pollam, polgam)
-    
-       REAL(dp), INTENT (IN) ::  polphi !< latitude of the rotated north pole
-       REAL(dp), INTENT (IN) ::  pollam !< longitude of the rotated north pole
-       REAL(dp), INTENT (IN) ::  phirot !< latitude in the rotated system
-       REAL(dp), INTENT (IN) ::  rlarot !< longitude in the rotated system
-       REAL(dp), INTENT (IN) ::  polgam !< angle between the north poles of the systems
-       
-       REAL(dp) ::  rlarot2rla          !< latitude in the geographical system
-       
-       REAL(dp) ::  zsinpol, zcospol, zlampol, zphis, zrlas, zarg1, zarg2, zgam
-       
-       zsinpol = SIN(TO_RADIANS * polphi)
-       zcospol = COS(TO_RADIANS * polphi)
-       zlampol = TO_RADIANS * pollam
-       zphis   = TO_RADIANS * phirot
-
-       IF (rlarot > 180.0_dp)  THEN
-          zrlas = rlarot - 360.0_dp
-       ELSE
-          zrlas = rlarot
-       ENDIF
-       zrlas   = TO_RADIANS * zrlas
-      
-       IF (polgam /= 0.0_dp)  THEN
-          zgam  = TO_RADIANS * polgam
-          zarg1 = SIN(zlampol) * (zcospol * SIN(zphis) - zsinpol*COS(zphis) *  &
-                  (COS(zrlas) * COS(zgam) - SIN(zrlas) * SIN(zgam)) ) -        &
-                  COS(zlampol) * COS(zphis) * ( SIN(zrlas) * COS(zgam) +       &
-                                                COS(zrlas) * SIN(zgam) )
-       
-          zarg2 = COS (zlampol) * (zcospol * SIN(zphis) - zsinpol*COS(zphis) * &
-                  (COS(zrlas) * COS(zgam) - SIN(zrlas) * SIN(zgam)) ) +        &
-                  SIN(zlampol) * COS(zphis) * ( SIN(zrlas) * COS(zgam) +       &
-                                                COS(zrlas) * SIN(zgam) )
-       ELSE
-          zarg1   = SIN (zlampol) * (-zsinpol * COS(zrlas) * COS(zphis)  +     &
-                                      zcospol *              SIN(zphis)) -     &
-                    COS (zlampol) *             SIN(zrlas) * COS(zphis)
-          zarg2   = COS (zlampol) * (-zsinpol * COS(zrlas) * COS(zphis)  +     &
-                                      zcospol *              SIN(zphis)) +     &
-                    SIN (zlampol) *             SIN(zrlas) * COS(zphis)
-       ENDIF
-      
-       IF (zarg2 == 0.0_dp)  zarg2 = 1.0E-20_dp
-      
-       rlarot2rla = ATAN2(zarg1,zarg2) * TO_DEGREES
-        
-    END FUNCTION rlarot2rla
+ END FUNCTION rlarot2rla
 
 
@@ -1465,41 +1470,41 @@
 !> Compute the rotated-pole longitude of a point given in geographical cordinates
 !------------------------------------------------------------------------------!
-    FUNCTION rla2rlarot ( phi, rla, polphi, pollam, polgam )
-
-       REAL(dp), INTENT (IN) ::  polphi !< latitude of the rotated north pole
-       REAL(dp), INTENT (IN) ::  pollam !< longitude of the rotated north pole
-       REAL(dp), INTENT (IN) ::  phi    !< latitude in geographical system
-       REAL(dp), INTENT (IN) ::  rla    !< longitude in geographical system
-       REAL(dp), INTENT (IN) ::  polgam !< angle between the north poles of the systems
-       
-       REAL (KIND=dp) ::  rla2rlarot    !< latitude in the the rotated system
-       
-       REAL (KIND=dp) ::  zsinpol, zcospol, zlampol, zphi, zrla, zarg1, zarg2, zrla1
-       
-       zsinpol = SIN(polphi * TO_RADIANS)
-       zcospol = COS(polphi * TO_RADIANS)
-       zlampol = pollam * TO_RADIANS
-       zphi    = phi * TO_RADIANS
-
-       IF (rla > 180.0_dp)  THEN
-          zrla1 = rla - 360.0_dp
-       ELSE
-          zrla1 = rla
-       ENDIF
-       zrla = zrla1 * TO_RADIANS
-       
-       zarg1 = - SIN (zrla-zlampol) * COS(zphi)
-       zarg2 = - zsinpol * COS(zphi) * COS(zrla-zlampol) + zcospol * SIN(zphi)
-       
-       IF (zarg2 == 0.0_dp)  zarg2 = 1.0E-20_dp
-       
-       rla2rlarot = ATAN2 (zarg1,zarg2) * TO_DEGREES
-       
-       IF (polgam /= 0.0_dp )  THEN
-          rla2rlarot = polgam + rla2rlarot
-          IF (rla2rlarot > 180._dp)  rla2rlarot = rla2rlarot - 360.0_dp
-       ENDIF
-       
-    END FUNCTION rla2rlarot
+ FUNCTION rla2rlarot ( phi, rla, polphi, pollam, polgam )
+
+    REAL(wp), INTENT (IN) ::  polphi !< latitude of the rotated north pole
+    REAL(wp), INTENT (IN) ::  pollam !< longitude of the rotated north pole
+    REAL(wp), INTENT (IN) ::  phi    !< latitude in geographical system
+    REAL(wp), INTENT (IN) ::  rla    !< longitude in geographical system
+    REAL(wp), INTENT (IN) ::  polgam !< angle between the north poles of the systems
+    
+    REAL(wp) ::  rla2rlarot    !< latitude in the the rotated system
+    
+    REAL(wp) ::  zsinpol, zcospol, zlampol, zphi, zrla, zarg1, zarg2, zrla1
+    
+    zsinpol = SIN(polphi * TO_RADIANS)
+    zcospol = COS(polphi * TO_RADIANS)
+    zlampol = pollam * TO_RADIANS
+    zphi    = phi * TO_RADIANS
+
+    IF (rla > 180.0_wp)  THEN
+       zrla1 = rla - 360.0_wp
+    ELSE
+       zrla1 = rla
+    ENDIF
+    zrla = zrla1 * TO_RADIANS
+    
+    zarg1 = - SIN (zrla-zlampol) * COS(zphi)
+    zarg2 = - zsinpol * COS(zphi) * COS(zrla-zlampol) + zcospol * SIN(zphi)
+    
+    IF (zarg2 == 0.0_wp)  zarg2 = 1.0E-20_wp
+    
+    rla2rlarot = ATAN2 (zarg1,zarg2) * TO_DEGREES
+    
+    IF (polgam /= 0.0_wp )  THEN
+       rla2rlarot = polgam + rla2rlarot
+       IF (rla2rlarot > 180._wp)  rla2rlarot = rla2rlarot - 360.0_wp
+    ENDIF
+    
+ END FUNCTION rla2rlarot
 
 
@@ -1510,27 +1515,27 @@
 !> rotated-pole system
 !------------------------------------------------------------------------------!
-    SUBROUTINE uv2uvrot(u, v, rlat, rlon, pollat, pollon, urot, vrot)
-    
-       REAL(dp), INTENT (IN)  ::  u, v           !< wind components in the true geographical system
-       REAL(dp), INTENT (IN)  ::  rlat, rlon     !< coordinates in the true geographical system
-       REAL(dp), INTENT (IN)  ::  pollat, pollon !< latitude and longitude of the north pole of the rotated grid
-       
-       REAL(dp), INTENT (OUT) ::  urot, vrot     !< wind components in the rotated grid             
-       
-       REAL (dp) ::  zsinpol, zcospol, zlonp, zlat, zarg1, zarg2, znorm
-       
-       zsinpol = SIN(pollat * TO_RADIANS)
-       zcospol = COS(pollat * TO_RADIANS)
-       zlonp   = (pollon-rlon) * TO_RADIANS
-       zlat    = rlat * TO_RADIANS
-       
-       zarg1 = zcospol * SIN(zlonp)
-       zarg2 = zsinpol * COS(zlat) - zcospol * SIN(zlat) * COS(zlonp)
-       znorm = 1.0_dp / SQRT(zarg1*zarg1 + zarg2*zarg2)
-       
-       urot = u * zarg2 * znorm - v * zarg1 * znorm
-       vrot = u * zarg1 * znorm + v * zarg2 * znorm
-    
-    END SUBROUTINE uv2uvrot
+ SUBROUTINE uv2uvrot(u, v, rlat, rlon, pollat, pollon, urot, vrot)
+ 
+    REAL(wp), INTENT (IN)  ::  u, v           !< wind components in the true geographical system
+    REAL(wp), INTENT (IN)  ::  rlat, rlon     !< coordinates in the true geographical system
+    REAL(wp), INTENT (IN)  ::  pollat, pollon !< latitude and longitude of the north pole of the rotated grid
+    
+    REAL(wp), INTENT (OUT) ::  urot, vrot     !< wind components in the rotated grid             
+    
+    REAL (wp) ::  zsinpol, zcospol, zlonp, zlat, zarg1, zarg2, znorm
+    
+    zsinpol = SIN(pollat * TO_RADIANS)
+    zcospol = COS(pollat * TO_RADIANS)
+    zlonp   = (pollon-rlon) * TO_RADIANS
+    zlat    = rlat * TO_RADIANS
+    
+    zarg1 = zcospol * SIN(zlonp)
+    zarg2 = zsinpol * COS(zlat) - zcospol * SIN(zlat) * COS(zlonp)
+    znorm = 1.0_wp / SQRT(zarg1*zarg1 + zarg2*zarg2)
+    
+    urot = u * zarg2 * znorm - v * zarg1 * znorm
+    vrot = u * zarg1 * znorm + v * zarg2 * znorm
+ 
+ END SUBROUTINE uv2uvrot
 
 
@@ -1541,27 +1546,27 @@
 !> geographical system
 !------------------------------------------------------------------------------!
-    SUBROUTINE uvrot2uv (urot, vrot, rlat, rlon, pollat, pollon, u, v)
-    
-       REAL(dp), INTENT(IN) ::  urot, vrot     !< wind components in the rotated grid
-       REAL(dp), INTENT(IN) ::  rlat, rlon     !< latitude and longitude in the true geographical system
-       REAL(dp), INTENT(IN) ::  pollat, pollon !< latitude and longitude of the north pole of the rotated grid
-       
-       REAL(dp), INTENT(OUT) ::  u, v          !< wind components in the true geographical system
-       
-       REAL(dp) ::  zsinpol, zcospol, zlonp, zlat, zarg1, zarg2, znorm
-     
-       zsinpol = SIN(pollat * TO_RADIANS)
-       zcospol = COS(pollat * TO_RADIANS)
-       zlonp   = (pollon-rlon) * TO_RADIANS
-       zlat    = rlat * TO_RADIANS
-     
-       zarg1 = zcospol * SIN(zlonp)
-       zarg2 = zsinpol * COS(zlat) - zcospol * SIN(zlat) * COS(zlonp)
-       znorm = 1.0_dp / SQRT(zarg1*zarg1 + zarg2*zarg2)
-     
-       u =   urot * zarg2 * znorm + vrot * zarg1 * znorm
-       v = - urot * zarg1 * znorm + vrot * zarg2 * znorm
-    
-    END SUBROUTINE uvrot2uv
+ SUBROUTINE uvrot2uv (urot, vrot, rlat, rlon, pollat, pollon, u, v)
+ 
+    REAL(wp), INTENT(IN) ::  urot, vrot     !< wind components in the rotated grid
+    REAL(wp), INTENT(IN) ::  rlat, rlon     !< latitude and longitude in the true geographical system
+    REAL(wp), INTENT(IN) ::  pollat, pollon !< latitude and longitude of the north pole of the rotated grid
+    
+    REAL(wp), INTENT(OUT) ::  u, v          !< wind components in the true geographical system
+    
+    REAL(wp) ::  zsinpol, zcospol, zlonp, zlat, zarg1, zarg2, znorm
+  
+    zsinpol = SIN(pollat * TO_RADIANS)
+    zcospol = COS(pollat * TO_RADIANS)
+    zlonp   = (pollon-rlon) * TO_RADIANS
+    zlat    = rlat * TO_RADIANS
+  
+    zarg1 = zcospol * SIN(zlonp)
+    zarg2 = zsinpol * COS(zlat) - zcospol * SIN(zlat) * COS(zlonp)
+    znorm = 1.0_wp / SQRT(zarg1*zarg1 + zarg2*zarg2)
+  
+    u =   urot * zarg2 * znorm + vrot * zarg1 * znorm
+    v = - urot * zarg1 * znorm + vrot * zarg2 * znorm
+ 
+ END SUBROUTINE uvrot2uv
 
  END MODULE inifor_transform
Index: /palm/trunk/UTIL/inifor/src/inifor_types.f90
===================================================================
--- /palm/trunk/UTIL/inifor/src/inifor_types.f90	(revision 3865)
+++ /palm/trunk/UTIL/inifor/src/inifor_types.f90	(revision 3866)
@@ -26,4 +26,8 @@
 ! -----------------
 ! $Id$
+! Use PALM's working precision
+!
+! 
+! 3779 2019-03-05 11:13:35Z eckhard
 ! Improved variable naming
 ! 
@@ -68,11 +72,13 @@
 !> The types module provides derived data types used in INIFOR.
 !------------------------------------------------------------------------------!
-#if defined ( __netcdf )
  MODULE inifor_types
  
  USE inifor_defs,                                                              &
-    ONLY:  dp, DATE, PATH, SNAME, LNAME
+    ONLY:  DATE, PATH, SNAME, LNAME, wp
+
+#if defined ( __netcdf )
  USE netcdf,                                                                   &
     ONLY:  NF90_MAX_VAR_DIMS, NF90_MAX_NAME
+#endif
 
  IMPLICIT NONE
@@ -104,9 +110,9 @@
     CHARACTER(LEN=SNAME) ::  rotation_method      !< selects method for velocity rotation
 
-    REAL(dp)             ::  p0                   !< manually specified surface pressure [Pa]
-    REAL(dp)             ::  ug                   !< manually spefied geostrophic wind component in x direction [m/s]
-    REAL(dp)             ::  vg                   !< manually spefied geostrophic wind component in y direction [m/s]
-    REAL(dp)             ::  z0                   !< elevation of the PALM-4U domain above sea level [m]
-    REAL(dp)             ::  averaging_angle      !< latitudal and longitudal width of averaging regions [deg]
+    REAL(wp)             ::  p0                   !< manually specified surface pressure [Pa]
+    REAL(wp)             ::  ug                   !< manually spefied geostrophic wind component in x direction [m/s]
+    REAL(wp)             ::  vg                   !< manually spefied geostrophic wind component in y direction [m/s]
+    REAL(wp)             ::  z0                   !< elevation of the PALM-4U domain above sea level [m]
+    REAL(wp)             ::  averaging_angle      !< latitudal and longitudal width of averaging regions [deg]
     
     LOGICAL              ::  debug                       !< indicates whether --debug option was given
@@ -143,33 +149,33 @@
     INTEGER, ALLOCATABLE  ::  jjj(:)        !< profile averaging neighbour indices 
     INTEGER, ALLOCATABLE  ::  kkk(:,:,:)    !< indices of vertical interpolation neightbours, kkk(<source column>, <PALM k level>, <neighbour index>)
-    REAL(dp)              ::  lx            !< domain length in the first dimension [m]
-    REAL(dp)              ::  ly            !< domain length in the second dimension [m]
-    REAL(dp)              ::  x0            !< x coordinate of PALM-4U domain projection centre, i.e. location of zero distortion
-    REAL(dp)              ::  y0            !< y coordinate of PALM-4U domain projection centre, i.e. location of zwro distortion
-    REAL(dp)              ::  z0            !< displacement of the coordinate origin above sea level [m]
-    REAL(dp), ALLOCATABLE ::  x(:)          !< coordinates of cell centers in x direction [m]
-    REAL(dp), ALLOCATABLE ::  y(:)          !< coordinates of cell centers in y direction [m]
-    REAL(dp), POINTER     ::  z(:)          !< coordinates of cell centers in z direction [m]
-    REAL(dp), ALLOCATABLE ::  h(:,:,:)      !< heights grid point for intermediate grids [m]
-    REAL(dp), POINTER     ::  cosmo_h(:,:,:)!< pointer to appropriate COSMO level heights (scalar/w) [m]
-    REAL(dp), POINTER     ::  hhl(:,:,:)    !< heights of half layers (cell faces) above sea level in COSMO-DE, read in from 
-    REAL(dp), POINTER     ::  hfl(:,:,:)    !< heights of full layers (cell centres) above sea level in COSMO-DE, computed as arithmetic average of hhl
-    REAL(dp), POINTER     ::  depths(:)     !< depths of output soil layers, equal the depths of the source model (e.g. COSMO-DE)
-    REAL(dp), ALLOCATABLE ::  xu(:)         !< coordinates of cell faces in x direction [m]
-    REAL(dp), ALLOCATABLE ::  yv(:)         !< coordinates of cell faces in y direction [m]
-    REAL(dp), POINTER     ::  zw(:)         !< coordinates of cell faces in z direction [m]
-    REAL(dp), ALLOCATABLE ::  lat(:)        !< rotated-pole latitudes of scalars (cell centers) of the COSMO-DE grid [rad]
-    REAL(dp), ALLOCATABLE ::  lon(:)        !< rotated-pole longitudes of scalars (cell centres) of the COSMO-DE grid [rad]
-    REAL(dp), ALLOCATABLE ::  latv(:)       !< rotated-pole latitudes of v winds (face centres in latitudal/y direction) [rad]
-    REAL(dp), ALLOCATABLE ::  lonu(:)       !< rotated-pole latitudes of u winds (face centres in longitudal/x direction) [rad]
-    REAL(dp), ALLOCATABLE ::  clat(:,:)     !< latitudes of PALM-4U cell centres in COSMO-DE's rotated-pole grid [rad]
-    REAL(dp), ALLOCATABLE ::  clon(:,:)     !< longitudes of PALM-4U scalars (cell centres) in COSMO-DE's rotated-pole grid [rad]
-    REAL(dp), ALLOCATABLE ::  clatu(:,:)    !< latitudes of PALM-4U u winds (cell faces in u direction) in COSMO-DE's rotated-pole grid [rad]
-    REAL(dp), ALLOCATABLE ::  clonu(:,:)    !< longitudes of PALM-4U u winds (cell faces in u direction) in COSMO-DE's rotated-pole grid [rad]
-    REAL(dp), ALLOCATABLE ::  clatv(:,:)    !< latitudes of PALM-4U v winds (cell faces in v direction) in COSMO-DE's rotated-pole grid [rad]
-    REAL(dp), ALLOCATABLE ::  clonv(:,:)    !< longitudes of PALM-4U v winds (cell faces in v direction) in COSMO-DE's rotated-pole grid [rad]
-    REAL(dp), ALLOCATABLE ::  w_horiz(:,:,:)   !< weights for bilinear horizontal interpolation
-    REAL(dp), ALLOCATABLE ::  w_verti(:,:,:,:) !< weights for linear vertical interpolation
-    REAL(dp), ALLOCATABLE ::  w(:,:,:)      !< vertical interpolation weights, w(<source_column>, <PALM k level>, <neighbour index>) [-]
+    REAL(wp)              ::  lx            !< domain length in the first dimension [m]
+    REAL(wp)              ::  ly            !< domain length in the second dimension [m]
+    REAL(wp)              ::  x0            !< x coordinate of PALM-4U domain projection centre, i.e. location of zero distortion
+    REAL(wp)              ::  y0            !< y coordinate of PALM-4U domain projection centre, i.e. location of zwro distortion
+    REAL(wp)              ::  z0            !< displacement of the coordinate origin above sea level [m]
+    REAL(wp), ALLOCATABLE ::  x(:)          !< coordinates of cell centers in x direction [m]
+    REAL(wp), ALLOCATABLE ::  y(:)          !< coordinates of cell centers in y direction [m]
+    REAL(wp), POINTER     ::  z(:)          !< coordinates of cell centers in z direction [m]
+    REAL(wp), ALLOCATABLE ::  h(:,:,:)      !< heights grid point for intermediate grids [m]
+    REAL(wp), POINTER     ::  cosmo_h(:,:,:)!< pointer to appropriate COSMO level heights (scalar/w) [m]
+    REAL(wp), POINTER     ::  hhl(:,:,:)    !< heights of half layers (cell faces) above sea level in COSMO-DE, read in from 
+    REAL(wp), POINTER     ::  hfl(:,:,:)    !< heights of full layers (cell centres) above sea level in COSMO-DE, computed as arithmetic average of hhl
+    REAL(wp), POINTER     ::  depths(:)     !< depths of output soil layers, equal the depths of the source model (e.g. COSMO-DE)
+    REAL(wp), ALLOCATABLE ::  xu(:)         !< coordinates of cell faces in x direction [m]
+    REAL(wp), ALLOCATABLE ::  yv(:)         !< coordinates of cell faces in y direction [m]
+    REAL(wp), POINTER     ::  zw(:)         !< coordinates of cell faces in z direction [m]
+    REAL(wp), ALLOCATABLE ::  lat(:)        !< rotated-pole latitudes of scalars (cell centers) of the COSMO-DE grid [rad]
+    REAL(wp), ALLOCATABLE ::  lon(:)        !< rotated-pole longitudes of scalars (cell centres) of the COSMO-DE grid [rad]
+    REAL(wp), ALLOCATABLE ::  latv(:)       !< rotated-pole latitudes of v winds (face centres in latitudal/y direction) [rad]
+    REAL(wp), ALLOCATABLE ::  lonu(:)       !< rotated-pole latitudes of u winds (face centres in longitudal/x direction) [rad]
+    REAL(wp), ALLOCATABLE ::  clat(:,:)     !< latitudes of PALM-4U cell centres in COSMO-DE's rotated-pole grid [rad]
+    REAL(wp), ALLOCATABLE ::  clon(:,:)     !< longitudes of PALM-4U scalars (cell centres) in COSMO-DE's rotated-pole grid [rad]
+    REAL(wp), ALLOCATABLE ::  clatu(:,:)    !< latitudes of PALM-4U u winds (cell faces in u direction) in COSMO-DE's rotated-pole grid [rad]
+    REAL(wp), ALLOCATABLE ::  clonu(:,:)    !< longitudes of PALM-4U u winds (cell faces in u direction) in COSMO-DE's rotated-pole grid [rad]
+    REAL(wp), ALLOCATABLE ::  clatv(:,:)    !< latitudes of PALM-4U v winds (cell faces in v direction) in COSMO-DE's rotated-pole grid [rad]
+    REAL(wp), ALLOCATABLE ::  clonv(:,:)    !< longitudes of PALM-4U v winds (cell faces in v direction) in COSMO-DE's rotated-pole grid [rad]
+    REAL(wp), ALLOCATABLE ::  w_horiz(:,:,:)   !< weights for bilinear horizontal interpolation
+    REAL(wp), ALLOCATABLE ::  w_verti(:,:,:,:) !< weights for linear vertical interpolation
+    REAL(wp), ALLOCATABLE ::  w(:,:,:)      !< vertical interpolation weights, w(<source_column>, <PALM k level>, <neighbour index>) [-]
  END TYPE grid_definition
 
@@ -190,5 +196,5 @@
     INTEGER               ::  dimvarids_vel(3)  !< NetCDF IDs of the grid coordinates of velocities xu, yu, zu. Note that velocities are located at mix of both coordinates, e.g. u(xu, y, z).
     INTEGER               ::  dimvarids_soil(3) !< NetCDF IDs of the grid coordinates for soil points x, y, depth 
-    REAL(dp), POINTER     ::  time(:)           !< vector of output time steps
+    REAL(wp), POINTER     ::  time(:)           !< vector of output time steps
  END TYPE nc_file
 
@@ -199,4 +205,5 @@
 !> Metadata container for netCDF variables
 !------------------------------------------------------------------------------!
+#if defined ( __netcdf )
  TYPE nc_var
     INTEGER                               ::  varid     !< NetCDF ID of the variable
@@ -247,5 +254,5 @@
     LOGICAL                          ::  is_preprocessed = .FALSE. !< Inifor flag indicating whether the I/O group has been preprocessed
  END TYPE io_group 
-
+#endif
 
 !------------------------------------------------------------------------------!
@@ -257,9 +264,7 @@
 !------------------------------------------------------------------------------!
  TYPE container
-   REAL(dp), ALLOCATABLE ::  array(:,:,:)               !< generic data array
+   REAL(wp), ALLOCATABLE ::  array(:,:,:)               !< generic data array
    LOGICAL               ::  is_preprocessed = .FALSE.  !< flag indicating whether input array has been preprocessed
  END TYPE container
 
  END MODULE inifor_types
-#endif
-
Index: /palm/trunk/UTIL/inifor/src/inifor_util.f90
===================================================================
--- /palm/trunk/UTIL/inifor/src/inifor_util.f90	(revision 3865)
+++ /palm/trunk/UTIL/inifor/src/inifor_util.f90	(revision 3866)
@@ -26,4 +26,9 @@
 ! -----------------
 ! $Id$
+! Use PALM's working precision
+! Improved coding style
+! 
+! 
+! 3785 2019-03-06 10:41:14Z eckhard
 ! Prefixed all INIFOR modules with inifor_
 ! 
@@ -58,9 +63,8 @@
 !> The util module provides miscellaneous utility routines for INIFOR.
 !------------------------------------------------------------------------------!
-#if defined ( __netcdf )
  MODULE inifor_util
 
     USE inifor_defs,                                                           &
-        ONLY :  dp, PI, DATE, SNAME
+        ONLY :  PI, DATE, SNAME, wp
     USE inifor_types,                                                          &
         ONLY :  grid_definition
@@ -87,5 +91,5 @@
     END TYPE
 
-    INTERFACE
+ INTERFACE
 
 !------------------------------------------------------------------------------!
@@ -95,14 +99,14 @@
 !> structure.
 !------------------------------------------------------------------------------!
-       FUNCTION strptime(string, format, timeinfo) BIND(c, NAME='strptime')
-          IMPORT :: C_CHAR, C_SIZE_T, tm_struct
-
-          IMPLICIT NONE
-
-          CHARACTER(KIND=C_CHAR), DIMENSION(*), INTENT(IN) ::  string, format
-          TYPE(tm_struct), INTENT(OUT)                     ::  timeinfo
-
-          INTEGER(C_SIZE_T)                                ::  strptime
-       END FUNCTION
+    FUNCTION strptime(string, format, timeinfo) BIND(c, NAME='strptime')
+       IMPORT :: C_CHAR, C_SIZE_T, tm_struct
+
+       IMPLICIT NONE
+
+       CHARACTER(KIND=C_CHAR), DIMENSION(*), INTENT(IN) ::  string, format
+       TYPE(tm_struct), INTENT(OUT)                     ::  timeinfo
+
+       INTEGER(C_SIZE_T)                                ::  strptime
+    END FUNCTION
 
 
@@ -113,16 +117,16 @@
 !> structure to a string in the given 'format'.
 !------------------------------------------------------------------------------!
-       FUNCTION strftime(string, string_len, format, timeinfo) BIND(c, NAME='strftime')
-          IMPORT :: C_CHAR, C_SIZE_T, tm_struct
-
-          IMPLICIT NONE
-
-          CHARACTER(KIND=C_CHAR), DIMENSION(*), INTENT(OUT) ::  string
-          CHARACTER(KIND=C_CHAR), DIMENSION(*), INTENT(IN)  ::  format
-          INTEGER(C_SIZE_T), INTENT(IN)                     ::  string_len
-          TYPE(tm_struct), INTENT(IN)                       ::  timeinfo
-
-          INTEGER(C_SIZE_T)                                 ::  strftime
-       END FUNCTION
+    FUNCTION strftime(string, string_len, format, timeinfo) BIND(c, NAME='strftime')
+       IMPORT :: C_CHAR, C_SIZE_T, tm_struct
+
+       IMPLICIT NONE
+
+       CHARACTER(KIND=C_CHAR), DIMENSION(*), INTENT(OUT) ::  string
+       CHARACTER(KIND=C_CHAR), DIMENSION(*), INTENT(IN)  ::  format
+       INTEGER(C_SIZE_T), INTENT(IN)                     ::  string_len
+       TYPE(tm_struct), INTENT(IN)                       ::  timeinfo
+
+       INTEGER(C_SIZE_T)                                 ::  strftime
+    END FUNCTION
 
 
@@ -135,15 +139,15 @@
 !> e.g. increments the date if hours overfow 24.
 !------------------------------------------------------------------------------!
-       FUNCTION mktime(timeinfo) BIND(c, NAME='mktime')
-          IMPORT :: C_PTR, tm_struct
-
-          IMPLICIT NONE
-
-          TYPE(tm_struct), INTENT(IN) ::  timeinfo
-
-          TYPE(C_PTR)                 ::  mktime
-       END FUNCTION
-
-    END INTERFACE
+    FUNCTION mktime(timeinfo) BIND(c, NAME='mktime')
+       IMPORT :: C_PTR, tm_struct
+
+       IMPLICIT NONE
+
+       TYPE(tm_struct), INTENT(IN) ::  timeinfo
+
+       TYPE(C_PTR)                 ::  mktime
+    END FUNCTION
+
+ END INTERFACE
 
  CONTAINS
@@ -156,52 +160,52 @@
 !> format
 !------------------------------------------------------------------------------!
-    CHARACTER(LEN=DATE) FUNCTION add_hours_to(date_string, hours)
-       CHARACTER(LEN=DATE), INTENT(IN)          ::  date_string
-       INTEGER, INTENT(IN)                      ::  hours
-
-       CHARACTER(KIND=C_CHAR, LEN=*), PARAMETER ::  format_string = "%Y%m%d%H"
-       CHARACTER(KIND=C_CHAR, LEN=DATE)         ::  c_date_string
-       TYPE(C_PTR)                              ::  c_pointer
-       TYPE(tm_struct)                          ::  time_info
-       INTEGER                                  ::  err
-
-       c_date_string = date_string
-
-       ! Convert C string to C tm struct
-       CALL init_tm(time_info)
-       err = strptime(c_date_string, format_string, time_info)
+ CHARACTER(LEN=DATE) FUNCTION add_hours_to(date_string, hours)
+    CHARACTER(LEN=DATE), INTENT(IN)          ::  date_string
+    INTEGER, INTENT(IN)                      ::  hours
+
+    CHARACTER(KIND=C_CHAR, LEN=*), PARAMETER ::  format_string = "%Y%m%d%H"
+    CHARACTER(KIND=C_CHAR, LEN=DATE)         ::  c_date_string
+    TYPE(C_PTR)                              ::  c_pointer
+    TYPE(tm_struct)                          ::  time_info
+    INTEGER                                  ::  err
+
+    c_date_string = date_string
+
+    ! Convert C string to C tm struct
+    CALL init_tm(time_info)
+    err = strptime(c_date_string, format_string, time_info)
+ 
+    ! Manipulate and normalize C tm struct
+    time_info%tm_hour = time_info%tm_hour + hours
+    c_pointer = mktime(time_info)
+
+    ! Convert back to C string
+    err = strftime(c_date_string, INT(DATE, KIND=C_SIZE_T),                 &
+                   format_string, time_info)
+
+    add_hours_to = c_date_string
+ END FUNCTION
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Print all members of the given tm structure
+!------------------------------------------------------------------------------!
+ SUBROUTINE print_tm(timeinfo)
+    TYPE(tm_struct), INTENT(IN) :: timeinfo
+
+    PRINT *, "sec: ", timeinfo%tm_sec,  &  !< seconds after the minute [0, 61]
+             "min: ", timeinfo%tm_min,  &  !< minutes after the hour [0, 59]
+             "hr:  ", timeinfo%tm_hour, &  !< hours since midnight [0, 23]
+             "day: ", timeinfo%tm_mday, &  !< day of the month [1, 31]
+             "mon: ", timeinfo%tm_mon,  &  !< month since January [0, 11]
+             "yr:  ", timeinfo%tm_year, &  !< years since 1900
+             "wday:", timeinfo%tm_wday, &  !< days since Sunday [0, 6]
+             "yday:", timeinfo%tm_yday, &  !< days since January 1st [0, 356]
+             "dst: ", timeinfo%tm_isdst    !< Daylight Saving time flag
+ END SUBROUTINE print_tm
+
     
-       ! Manipulate and normalize C tm struct
-       time_info % tm_hour = time_info % tm_hour + hours
-       c_pointer = mktime(time_info)
-
-       ! Convert back to C string
-       err = strftime(c_date_string, INT(DATE, KIND=C_SIZE_T),                 &
-                      format_string, time_info)
-
-       add_hours_to = c_date_string
-    END FUNCTION
-
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Print all members of the given tm structure
-!------------------------------------------------------------------------------!
-    SUBROUTINE print_tm(timeinfo)
-       TYPE(tm_struct), INTENT(IN) :: timeinfo
-
-       PRINT *, "sec: ", timeinfo % tm_sec,  &  !< seconds after the minute [0, 61]
-                "min: ", timeinfo % tm_min,  &  !< minutes after the hour [0, 59]
-                "hr:  ", timeinfo % tm_hour, &  !< hours since midnight [0, 23]
-                "day: ", timeinfo % tm_mday, &  !< day of the month [1, 31]
-                "mon: ", timeinfo % tm_mon,  &  !< month since January [0, 11]
-                "yr:  ", timeinfo % tm_year, &  !< years since 1900
-                "wday:", timeinfo % tm_wday, &  !< days since Sunday [0, 6]
-                "yday:", timeinfo % tm_yday, &  !< days since January 1st [0, 356]
-                "dst: ", timeinfo % tm_isdst    !< Daylight Saving time flag
-    END SUBROUTINE print_tm
-
-    
 !------------------------------------------------------------------------------!
 ! Description:
@@ -209,21 +213,21 @@
 !> Initialize the given tm structure with zero values
 !------------------------------------------------------------------------------!
-    SUBROUTINE init_tm(timeinfo)
-       TYPE(tm_struct), INTENT(INOUT) :: timeinfo
-
-       timeinfo % tm_sec   = 0
-       timeinfo % tm_min   = 0
-       timeinfo % tm_hour  = 0
-       timeinfo % tm_mday  = 0
-       timeinfo % tm_mon   = 0
-       timeinfo % tm_year  = 0
-       timeinfo % tm_wday  = 0
-       timeinfo % tm_yday  = 0
-
-       ! We use UTC times, so marking Daylight Saving Time (DST) 'not available'
-       ! (< 0). If this is set to 0, mktime will convert the timeinfo to DST and
-       ! add one hour.
-       timeinfo % tm_isdst = -1
-    END SUBROUTINE init_tm
+ SUBROUTINE init_tm(timeinfo)
+    TYPE(tm_struct), INTENT(INOUT) :: timeinfo
+
+    timeinfo%tm_sec   = 0
+    timeinfo%tm_min   = 0
+    timeinfo%tm_hour  = 0
+    timeinfo%tm_mday  = 0
+    timeinfo%tm_mon   = 0
+    timeinfo%tm_year  = 0
+    timeinfo%tm_wday  = 0
+    timeinfo%tm_yday  = 0
+
+    ! We use UTC times, so marking Daylight Saving Time (DST) 'not available'
+    ! (< 0). If this is set to 0, mktime will convert the timeinfo to DST and
+    ! add one hour.
+    timeinfo%tm_isdst = -1
+ END SUBROUTINE init_tm
 
 
@@ -234,25 +238,25 @@
 !> and stop
 !------------------------------------------------------------------------------!
-    SUBROUTINE linspace(start, stop, array)
-
-       REAL(dp), INTENT(IN)    ::  start, stop
-       REAL(dp), INTENT(INOUT) ::  array(0:)
-       INTEGER                 ::  i, n
-
-       n = UBOUND(array, 1)
-
-       IF (n .EQ. 0)  THEN
-
-          array(0) = start
-
-       ELSE
-
-          DO i = 0, n
-             array(i) = start + REAL(i, dp) / n * (stop - start)
-          ENDDO
-
-       ENDIF
-       
-    END SUBROUTINE linspace
+ SUBROUTINE linspace(start, stop, array)
+
+    REAL(wp), INTENT(IN)    ::  start, stop
+    REAL(wp), INTENT(INOUT) ::  array(0:)
+    INTEGER                 ::  i, n
+
+    n = UBOUND(array, 1)
+
+    IF (n .EQ. 0)  THEN
+
+       array(0) = start
+
+    ELSE
+
+       DO i = 0, n
+          array(i) = start + REAL(i, wp) / n * (stop - start)
+       ENDDO
+
+    ENDIF
+    
+ END SUBROUTINE linspace
 
 
@@ -263,11 +267,11 @@
 !> (COSMO) to bottom-up (PALM)
 !------------------------------------------------------------------------------!
-    SUBROUTINE reverse(input_arr)
-
-       REAL(dp), INTENT(INOUT) ::  input_arr(:,:,:)
-
-       input_arr = input_arr(:,:,size(input_arr, 3):1:-1)
-
-    END SUBROUTINE reverse
+ SUBROUTINE reverse(input_arr)
+
+    REAL(wp), INTENT(INOUT) ::  input_arr(:,:,:)
+
+    input_arr = input_arr(:,:,size(input_arr, 3):1:-1)
+
+ END SUBROUTINE reverse
 
 
@@ -277,17 +281,17 @@
 !> 
 !------------------------------------------------------------------------------!
-    SUBROUTINE deaverage(avg_1, t1, avg_2, t2, avg_3, t3)
-
-       REAL(dp), DIMENSION(:,:,:), INTENT(IN)  ::  avg_1, avg_2
-       REAL(dp), INTENT(IN)                    ::  t1, t2, t3
-       REAL(dp), DIMENSION(:,:,:), INTENT(OUT) ::  avg_3
-
-       REAL(dp)                                ::  ti
+ SUBROUTINE deaverage(avg_1, t1, avg_2, t2, avg_3, t3)
+
+    REAL(wp), DIMENSION(:,:,:), INTENT(IN)  ::  avg_1, avg_2
+    REAL(wp), INTENT(IN)                    ::  t1, t2, t3
+    REAL(wp), DIMENSION(:,:,:), INTENT(OUT) ::  avg_3
+
+    REAL(wp)                                ::  ti
  
-       ti = 1.0_dp / t3
-
-       avg_3(:,:,:) = ti * ( t2 * avg_2(:,:,:) - t1 * avg_1(:,:,:) )
-
-    END SUBROUTINE deaverage
+    ti = 1.0_wp / t3
+
+    avg_3(:,:,:) = ti * ( t2 * avg_2(:,:,:) - t1 * avg_1(:,:,:) )
+
+ END SUBROUTINE deaverage
 
 
@@ -297,23 +301,23 @@
 !> Compute the COSMO-DE/-D2 basic state pressure profile
 !------------------------------------------------------------------------------!
-    SUBROUTINE get_basic_state(z, beta, p_sl, t_sl, rd, g, p0)
-
-       REAL(dp), INTENT(IN)  ::  z(1:)  !< height [m]
-       REAL(dp), INTENT(IN)  ::  beta   !< logarithmic lapse rate, dT / d ln(p) [K]
-       REAL(dp), INTENT(IN)  ::  p_sl   !< reference pressure [Pa]
-       REAL(dp), INTENT(IN)  ::  t_sl   !< reference tempereature [K]
-       REAL(dp), INTENT(IN)  ::  rd     !< ideal gas constant of dry air [J/kg/K]
-       REAL(dp), INTENT(IN)  ::  g      !< acceleration of Earth's gravity [m/s^2]
-       REAL(dp), INTENT(OUT) ::  p0(1:) !< COSMO basic state pressure [Pa]
-       REAL(dp) ::  root_frac, factor   !< precomputed factors
-
-       factor = - t_sl / beta
-       root_frac = (2.0_dp * beta * g) / (rd * t_sl*t_sl)
-
-       p0(:) = p_sl * EXP(                                                     &
-                  factor * ( 1.0_dp - SQRT( 1.0_dp - root_frac * z(:) ) )      &
-       )
-
-    END SUBROUTINE get_basic_state
+ SUBROUTINE get_basic_state(z, beta, p_sl, t_sl, rd, g, p0)
+
+    REAL(wp), INTENT(IN)  ::  z(1:)  !< height [m]
+    REAL(wp), INTENT(IN)  ::  beta   !< logarithmic lapse rate, dT / d ln(p) [K]
+    REAL(wp), INTENT(IN)  ::  p_sl   !< reference pressure [Pa]
+    REAL(wp), INTENT(IN)  ::  t_sl   !< reference tempereature [K]
+    REAL(wp), INTENT(IN)  ::  rd     !< ideal gas constant of dry air [J/kg/K]
+    REAL(wp), INTENT(IN)  ::  g      !< acceleration of Earth's gravity [m/s^2]
+    REAL(wp), INTENT(OUT) ::  p0(1:) !< COSMO basic state pressure [Pa]
+    REAL(wp) ::  root_frac, factor   !< precomputed factors
+
+    factor = - t_sl / beta
+    root_frac = (2.0_wp * beta * g) / (rd * t_sl*t_sl)
+
+    p0(:) = p_sl * EXP(                                                     &
+               factor * ( 1.0_wp - SQRT( 1.0_wp - root_frac * z(:) ) )      &
+    )
+
+ END SUBROUTINE get_basic_state
 
 
@@ -326,14 +330,14 @@
 !>     theta = T * (p_ref/p)^(R/c_p) = T * e^( R/c_p * ln(p_ref/p) )
 !------------------------------------------------------------------------------!
-    SUBROUTINE potential_temperature(t, p, p_ref, r, cp)
-       REAL(dp), DIMENSION(:,:,:), INTENT(INOUT) ::  t
-       REAL(dp), DIMENSION(:,:,:), INTENT(IN)    ::  p
-       REAL(dp), INTENT(IN)                      ::  p_ref, r, cp
-       REAL(dp)                                  ::  rcp
-
-       rcp = r/cp
-       t(:,:,:) =  t(:,:,:) * EXP( rcp * LOG(p_ref / p(:,:,:)) )
-
-    END SUBROUTINE potential_temperature
+ SUBROUTINE potential_temperature(t, p, p_ref, r, cp)
+    REAL(wp), DIMENSION(:,:,:), INTENT(INOUT) ::  t
+    REAL(wp), DIMENSION(:,:,:), INTENT(IN)    ::  p
+    REAL(wp), INTENT(IN)                      ::  p_ref, r, cp
+    REAL(wp)                                  ::  rcp
+
+    rcp = r/cp
+    t(:,:,:) =  t(:,:,:) * EXP( rcp * LOG(p_ref / p(:,:,:)) )
+
+ END SUBROUTINE potential_temperature
 
 
@@ -343,31 +347,34 @@
 !> Compute the density in place of the given temperature (t_rho).
 !------------------------------------------------------------------------------!
-   SUBROUTINE moist_density(t_rho, p, qv, rd, rv)
-       REAL(dp), DIMENSION(:,:,:), INTENT(INOUT) ::  t_rho
-       REAL(dp), DIMENSION(:,:,:), INTENT(IN)    ::  p, qv
-       REAL(dp), INTENT(IN)                      ::  rd, rv
-
-       t_rho(:,:,:) = p(:,:,:) / (                                             &
-          (rv * qv(:,:,:) + rd * (1.0_dp - qv(:,:,:))) * t_rho(:,:,:)          &
-       )
-
-    END SUBROUTINE moist_density
-
-
-    ! Convert a real number to a string in scientific notation
-    ! showing four significant digits.
-    CHARACTER(LEN=SNAME) FUNCTION real_to_str(val, format)
-
-        REAL(dp), INTENT(IN)                   ::  val
-        CHARACTER(LEN=*), OPTIONAL, INTENT(IN) ::  format
-
-        IF (PRESENT(format))  THEN
-           WRITE(real_to_str, format) val
-        ELSE
-           WRITE(real_to_str, '(E11.4)') val
-        ENDIF
-        real_to_str = ADJUSTL(real_to_str)
-
-    END FUNCTION real_to_str
+ SUBROUTINE moist_density(t_rho, p, qv, rd, rv)
+    REAL(wp), DIMENSION(:,:,:), INTENT(INOUT) ::  t_rho
+    REAL(wp), DIMENSION(:,:,:), INTENT(IN)    ::  p, qv
+    REAL(wp), INTENT(IN)                      ::  rd, rv
+
+    t_rho(:,:,:) = p(:,:,:) / (                                             &
+       (rv * qv(:,:,:) + rd * (1.0_wp - qv(:,:,:))) * t_rho(:,:,:)          &
+    )
+
+ END SUBROUTINE moist_density
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+! Convert a real number to a string in scientific notation showing four 
+! significant digits.
+!------------------------------------------------------------------------------!
+ CHARACTER(LEN=SNAME) FUNCTION real_to_str(val, format)
+
+    REAL(wp), INTENT(IN)                   ::  val
+    CHARACTER(LEN=*), OPTIONAL, INTENT(IN) ::  format
+
+    IF (PRESENT( format ) )  THEN
+       WRITE( real_to_str, format ) val
+    ELSE
+       WRITE( real_to_str, '(E11.4)' ) val
+    ENDIF
+    real_to_str = ADJUSTL( real_to_str )
+
+ END FUNCTION real_to_str
 
 
@@ -377,12 +384,12 @@
 !> Converts the given real value to a string
 !------------------------------------------------------------------------------!
-    CHARACTER(LEN=16) FUNCTION real_to_str_f(val)
-
-        REAL(dp), INTENT(IN) ::  val
-
-        WRITE(real_to_str_f, '(F16.8)') val
-        real_to_str_f = ADJUSTL(real_to_str_f)
-
-    END FUNCTION real_to_str_f
+ CHARACTER(LEN=16) FUNCTION real_to_str_f(val)
+
+     REAL(wp), INTENT(IN) ::  val
+
+     WRITE(real_to_str_f, '(F16.8)') val
+     real_to_str_f = ADJUSTL(real_to_str_f)
+
+ END FUNCTION real_to_str_f
 
 
@@ -392,12 +399,12 @@
 !> Converts the given integer value to a string
 !------------------------------------------------------------------------------!
-    CHARACTER(LEN=10) FUNCTION str(val)
-
-        INTEGER, INTENT(IN) ::  val
-
-        WRITE(str, '(i10)') val
-        str = ADJUSTL(str)
-
-    END FUNCTION str
+ CHARACTER(LEN=10) FUNCTION str(val)
+
+     INTEGER, INTENT(IN) ::  val
+
+     WRITE(str, '(i10)') val
+     str = ADJUSTL(str)
+
+ END FUNCTION str
 
 
@@ -407,18 +414,16 @@
 !> If the given path is not conlcuded by a slash, add one.
 !------------------------------------------------------------------------------!
-    SUBROUTINE normalize_path(path)
-        
-        CHARACTER(LEN=*), INTENT(INOUT) ::  path
-        INTEGER ::  n
-
-        n = LEN_TRIM(path)
-
-        IF (path(n:n) .NE. '/')  THEN
-           path = TRIM(path) // '/'
-        ENDIF
-
-    END SUBROUTINE
+ SUBROUTINE normalize_path(path)
+     
+     CHARACTER(LEN=*), INTENT(INOUT) ::  path
+     INTEGER ::  n
+
+     n = LEN_TRIM(path)
+
+     IF (path(n:n) .NE. '/')  THEN
+        path = TRIM(path) // '/'
+     ENDIF
+
+ END SUBROUTINE
 
  END MODULE inifor_util
-#endif
-
