!> @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 . ! ! Copyright 2017-2018 Leibniz Universitaet Hannover ! Copyright 2017-2018 Deutscher Wetterdienst Offenbach !------------------------------------------------------------------------------! ! ! Current revisions: ! ----------------- ! Initial revision ! ! ! Former revisions: ! ----------------- ! $Id$ ! ! ! ! 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')