Index: /palm/trunk/UTIL/inifor/src/get_netcdf_variable.inc
===================================================================
--- /palm/trunk/UTIL/inifor/src/get_netcdf_variable.inc	(revision 3396)
+++ /palm/trunk/UTIL/inifor/src/get_netcdf_variable.inc	(revision 3396)
@@ -0,0 +1,118 @@
+!> @file src/get_netcdf_variable.inc
+!------------------------------------------------------------------------------!
+! This file is part of the PALM model system.
+!
+! PALM is free software: you can redistribute it and/or modify it under the
+! terms of the GNU General Public License as published by the Free Software
+! Foundation, either version 3 of the License, or (at your option) any later
+! version.
+!
+! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
+! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+!
+! You should have received a copy of the GNU General Public License along with
+! PALM. If not, see <http://www.gnu.org/licenses/>.
+!
+! Copyright 2017-2018 Leibniz Universitaet Hannover
+! Copyright 2017-2018 Deutscher Wetterdienst Offenbach
+!------------------------------------------------------------------------------!
+!
+! Current revisions:
+! -----------------
+! 
+! 
+! Former revisions:
+! -----------------
+! $Id$
+! Initial revision
+! 
+! 
+! 
+!
+! 
+!
+! Authors:
+! --------
+! @author Eckhard Kadasch
+!
+! Description:
+! ------------
+!> This file provides the implementation of the io.f90 subroutines
+!> get_netcdf_variable_int() and get_netcdf_variable_real(). Both routines
+!> differ only in the type of the netCDF variable to be read and otherwise share
+!> their functionality. In order to avoid duplicating code, both routines source
+!> the implementation below using Fortran's INCLUDE statement.
+!> 
+!------------------------------------------------------------------------------!
+ INTEGER ::  i, ncid, start(3)
+
+ ! Read in_var NetCDF attributes
+ 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 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))
+
+    ! Read in_var NetCDF dimensions
+    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) ))
+    END DO
+
+    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
+    END IF
+
+    SELECT CASE(in_var % ndim)
+
+    CASE (2)
+
+       ALLOCATE( buffer( in_var % dimlen(1),                             &
+                         in_var % dimlen(2),                             &
+                         1 ) )
+
+    CASE (3, 4)
+
+       ALLOCATE( buffer( in_var % dimlen(1),                             &
+                         in_var % dimlen(2),                             &
+                         in_var % dimlen(3) ) )
+
+    CASE DEFAULT
+
+       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 abort('get_netcdf_variable', message) 
+
+    END SELECT
+ CALL run_control('time', 'alloc')
+          
+    ! TODO: Check for matching dimensions of buffer and var
+    CALL check(nf90_get_var( ncid, in_var % varid, buffer,               &
+                             start = start,                              &
+                             count = in_var % dimlen(1:3) ) )
+
+ CALL run_control('time', 'read')
+ ELSE
+
+    message = "Failed to read '" // TRIM(in_var % name) // &
+       "' from file '" // TRIM(in_file) // "'."
+    CALL report('get_netcdf_variable', message)
+
+ END IF
+
+ CALL check(nf90_close(ncid))
+
+ CALL run_control('time', 'read')
Index: /palm/trunk/UTIL/inifor/tests/test-stretching.f90
===================================================================
--- /palm/trunk/UTIL/inifor/tests/test-stretching.f90	(revision 3396)
+++ /palm/trunk/UTIL/inifor/tests/test-stretching.f90	(revision 3396)
@@ -0,0 +1,100 @@
+!> @file tests/test-stretching.f90
+!------------------------------------------------------------------------------!
+! This file is part of the PALM model system.
+!
+! PALM is free software: you can redistribute it and/or modify it under the
+! terms of the GNU General Public License as published by the Free Software
+! Foundation, either version 3 of the License, or (at your option) any later
+! version.
+!
+! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
+! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+!
+! You should have received a copy of the GNU General Public License along with
+! PALM. If not, see <http://www.gnu.org/licenses/>.
+!
+! Copyright 2017-2018 Leibniz Universitaet Hannover
+! Copyright 2017-2018 Deutscher Wetterdienst Offenbach
+!------------------------------------------------------------------------------!
+!
+! Current revisions:
+! -----------------
+! 
+! 
+! Former revisions:
+! -----------------
+! $Id$
+! Initial revision
+! 
+! 
+! 
+! 
+!
+! Authors:
+! --------
+! @author Eckhard Kadasch
+!
+! Description:
+! ------------
+!> This program tests INIFOR's implementation of PALM's grid stretching.
+!> 
+!------------------------------------------------------------------------------!
+ PROGRAM test_stretching
+
+    USE defs, &
+        ONLY :  dp
+
+    USE grid, &
+        ONLY :  stretched_z
+
+    USE test_utils
+    
+    IMPLICIT NONE
+
+    CHARACTER(LEN=*), PARAMETER ::  title = "stretched grid"
+    LOGICAL                     ::  res
+
+    INTEGER, PARAMETER ::  nz = 9
+    INTEGER            ::  k  = nz - 2
+
+    REAL(dp) ::  z(1:nz)
+    REAL(dp) ::  dz(10)            = -1.0_dp
+    REAL(dp) ::  dz_max            = 1000.0_dp
+    REAL(dp) ::  dz_stretch_factor = 1.08_dp
+    REAL(dp) ::  dz_stretch_level  = 2.0_dp
+    REAL(dp) ::  dz_stretch_level_start(9) = -9999999.9_dp
+    REAL(dp) ::  dz_stretch_level_end(9) = 9999999.9_dp
+    REAL(dp) ::  dz_stretch_factor_array(9) = 1.08_dp
+
+    CALL begin_test(title, res)
+
+    ! Arange
+    z(:)   = 0.0_dp
+    dz(1)  = 1.0_dp
+
+    ! Act
+    CALL stretched_z(z, 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)
+
+    ! Assert, that the total distance covered by the stretched region 
+    ! matches the therotetial distance, i.e. the sum over the finite
+    ! exponential series
+    !
+    !          Sum_{i=0}^n( dz[i] ) = dz[0] * Sum_{i=0}^n( f^i )
+    !                               = dz[0] * (1 - f^(i+1)) / (1-f)
+    !
+    ! with f being the stretch factor.
+    res = res .AND. &
+          assert_equal( (/ z(UBOUND(z, 1)) - z(1)              /),             &
+                        (/ (1.0_dp - dz_stretch_factor**(k+1)) /               &
+                           (1.0_dp - dz_stretch_factor)        /),             &
+                        'length of stretched grid' )
+
+    CALL end_test(title, res)
+
+ END PROGRAM test_stretching
