The UFZ services GitLab and Mattermost will be unavailable on Monday, October 25 from 06:00 AM to 08:00 AM due to maintenance work.

Commit 429c0d0f authored by Stephan Thober's avatar Stephan Thober
Browse files

starting to update writing of netcdf file

parent f45746c1
......@@ -31,6 +31,7 @@ program ED_Kriging
grid, gridMeteo, & ! grid properties of input and output grid
nCell ! number of cells
use mo_setVario , only: setVario
use mo_netcdf , only: NcDataset, NcVariable
use kriging
implicit none
......@@ -42,6 +43,8 @@ program ED_Kriging
integer(i4) :: netcdfid ! id of netcdf files
real(dp), dimension(:,:), allocatable :: tmp_array ! temporal array for output
real(dp) :: param(3) ! variogram parameters
type(NcDataset) :: nc_out
type(NcVariable) :: nc_data, nc_time
call Timer
call ReadDataMain
......@@ -64,7 +67,7 @@ program ED_Kriging
!
if (flagEDK) then
! open netcdf if necessary
if (outputformat=='nc') call WriteFluxStateInit(netcdfid)
call open_netcdf(fname, ncols, nrows, nc_out, nc_data, nc_time)
timeloop: do jday = jStart, jEnd
......@@ -94,13 +97,16 @@ program ED_Kriging
! write output
allocate(tmp_array(gridMeteo%nrows, gridMeteo%ncols)); tmp_array=real(grid%nodata_value, dp)
tmp_array = real(reshape(cell(:)%z,(/gridMeteo%nrows, gridMeteo%ncols/)), dp)
call WriteFluxState((jday-jStart+1), netcdfid, transpose(tmp_array))
! call WriteFluxState((jday-jStart+1), netcdfid, transpose(tmp_array))
call nc_time%setData(jday, start=(/jday/))
call nc_data%setData(tmp_array, start=(/1, 1, jday/))
deallocate(tmp_array)
end do timeloop
! close netcdf if necessary
if (outputformat=='nc') call CloseFluxState_file(netcdfid)
call nc_out%close()
end if
! deallocate memory
......
!> \file mo_netcdf.f90
!> \brief NetCDF Fortran 90 interface wrapper
!> \details A thin wrapper around the NetCDF Fortran 90 interface.
!> Provided are currently 3 user facing derived Types:
!> 1. NcDataset
!> 2. NcDimension
!> 3. NcVariable
!
!> \authors David Schaefer
!> \date Jun 2015
module mo_netcdf
! This module provides a thin wrapper around the NetCDF Fortran 90 interface,
! following a somehow object-oriented approach.
! Written David Schaefer, Jun 2015
! Modified Matthias Cuntz, Jan 2016 - compiled with PGI Fortran rev 15.9 - no automatic allocation of left-hand-side
! License
! -------
! This file is part of the UFZ Fortran library.
! The UFZ Fortran library is free software: you can redistribute it and/or modify
! it under the terms of the GNU Lesser General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
! The UFZ Fortran library 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 Lesser General Public License for more details.
! You should have received a copy of the GNU Lesser General Public License
! along with the UFZ Fortran library (cf. gpl.txt and lgpl.txt).
! If not, see <http://www.gnu.org/licenses/>.
! Copyright 2011-2015 David Schaefer
use mo_kind, only: i1, i2, i4, i8, sp, dp
use netcdf, only: &
nf90_open, nf90_close, nf90_strerror, nf90_def_dim, nf90_def_var, &
nf90_put_var, nf90_get_var, nf90_put_att, nf90_get_att, &
nf90_inquire, nf90_inq_dimid, nf90_inquire_dimension, &
nf90_inq_varid, nf90_inq_varids, nf90_inquire_variable, nf90_inquire_attribute, &
NF90_OPEN, NF90_NETCDF4, NF90_CREATE, NF90_WRITE, NF90_NOWRITE, &
NF90_BYTE, NF90_SHORT, NF90_INT, NF90_INT64, NF90_FLOAT, NF90_DOUBLE, &
NF90_FILL_BYTE, NF90_FILL_SHORT, NF90_FILL_INT, NF90_FILL_FLOAT , NF90_FILL_DOUBLE, &
NF90_NOERR, NF90_UNLIMITED, NF90_GLOBAL
implicit none
! --------------------------------------------------------------------------------------
!
! NAME
! NcDataset
!
! PURPOSE
!> \brief Provides basic file modification functionality
!> \details Bound to this derived type is the basic file level create/retrieve
!> functionality, i.e. functions/subroutines to create/retrieve
!> dimensions, variables and global attributes.
!> All files created by this derived type and its procedures are
!> are NF90_NETCDF4 only.
!> The supported modes are:
!> r: read
!> w: write/create
!< a: alter
!
! INTENT(IN)
!> \param[in] "character(*) :: fname"
!> \param[in] "character(1) :: mode"
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! RETURN
!> \return "type(NcDataset)"
!
! EXAMPLE
! See test file
!
! HISTORY
! \author David Schaefer
! \date June 2015
!
! --------------------------------------------------------------------------------------
type NcDataset
character(256) :: fname !> Filename of the opened dataset
character(1) :: mode !> File open mode
integer(i4) :: id !> NetCDF id
contains
procedure, public :: initNcDataset
procedure, public :: getNoVariables
procedure, public :: getVariableIds
procedure, public :: getVariables
procedure, private :: setGlobalAttributeChar
procedure, private :: setGlobalAttributeI8
procedure, private :: setGlobalAttributeI16
procedure, private :: setGlobalAttributeI32
procedure, private :: setGlobalAttributeI64
procedure, private :: setGlobalAttributeF32
procedure, private :: setGlobalAttributeF64
procedure, private :: getGlobalAttributeChar
procedure, private :: getGlobalAttributeI8
procedure, private :: getGlobalAttributeI16
procedure, private :: getGlobalAttributeI32
procedure, private :: getGlobalAttributeI64
procedure, private :: getGlobalAttributeF32
procedure, private :: getGlobalAttributeF64
procedure, private :: getDimensionByName
procedure, private :: getDimensionById
procedure, private :: setVariableWithTypes
procedure, private :: setVariableWithNames
procedure, private :: setVariableWithIds
procedure, private :: getVariableByName
! -----------------------------------------------------------------------------------
!
! NAME
! hasVariable
!
! PURPOSE
!> \brief Check if variable exists
!> \details Returns true if a variable with the given name exists, false
!> otherwise.
!
! INTENT(IN)
!> \param[in] "character(*) :: name"
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! RETURN
!> \return "logical"
!
! EXAMPLE
! See test file
!
! HISTORY
!> \author David Schaefer
!> \date June 2015
!
! -----------------------------------------------------------------------------------
procedure, public :: hasVariable
! -----------------------------------------------------------------------------------
!
! NAME
! hasDimension
!
! PURPOSE
!> \brief Check if dimension exists
!> \details Returns true if a dimension with the given name exists, false
!> otherwise.
!
! INTENT(IN)
!> \param[in] "character(*) :: name"
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! RETURN
!> \return "logical"
!
! EXAMPLE
! See test file
!
! HISTORY
!> \author David Schaefer
!> \date June 2015
!
! -----------------------------------------------------------------------------------
procedure, public :: hasDimension
! -----------------------------------------------------------------------------------
!
! NAME
! getUnlimitedDimension
!
! PURPOSE
!> \brief Return the unlimited dimension of the dataset
!> \details Returns the NcDimension derived type of the unlimited dimension.
!> The program will teminate abruptly if no such dimension exists.
!
! INTENT(IN)
!> \param[in] "character(*) :: name"
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! RETURN
!> \return "logical"
!
! EXAMPLE
! See test file
!
! HISTORY
!> \author David Schaefer
!> \date June 2015
!
! -----------------------------------------------------------------------------------
procedure, public :: getUnlimitedDimension
! -----------------------------------------------------------------------------------
!
! NAME
! isUnlimited
!
! PURPOSE
!> \brief Check if the dataset is unlimited
!> \details Returns true if the dataset contains an unlimited dimension,
!> false otherwise.
!
! INTENT(IN)
! None
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! RETURN
!> \return "logical"
!
! EXAMPLE
! See test file
!
! HISTORY
!> \author David Schaefer
!> \date June 2015
!
! -----------------------------------------------------------------------------------
procedure, public :: isUnlimited => isDatasetUnlimited
! -----------------------------------------------------------------------------------
!
! NAME
! close
!
! PURPOSE
!> \brief Close the datset
!> \details Close the NetCDF datset. The program will teminate abruptly if
!> the file cannot be closed correctly.
!
! INTENT(IN)
! None
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! RETURN
! None
!
! EXAMPLE
! See test file
!
! HISTORY
!> \author David Schaefer
!> \date June 2015
!
! -----------------------------------------------------------------------------------
procedure, public :: close
! -----------------------------------------------------------------------------------
!
! NAME
! setDimension
!
! PURPOSE
!> \brief Create a new dimension
!> \details Create a new dimension of given length. A length < 0 indicates an
!> unlimited dimension. The program will teminate abruptly if the
!> dimension cannot be created.
!
! INTENT(IN)
!> \param[in] "character(*) :: name"
!> \param[in] "integer(i4) :: length"
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! RETURN
!> \return NcDimension
!
! EXAMPLE
! See test file
!
! HISTORY
!> \author David Schaefer
!> \date June 2015
!
! -----------------------------------------------------------------------------------
procedure, public :: setDimension
! -----------------------------------------------------------------------------------
!
! NAME
! setAttribute
!
! PURPOSE
!> \brief Create a new global Attribute
!> \details Create a new global attribute from given name and value.
!> The program will teminate abruptly if the attribute cannot be
!> created.
!
! INTENT(IN)
!> \param[in] "character(*) :: name"
!> \param[in] "character(*)/integer(i4)/real(sp)/real(dp) :: value"
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! RETURN
! None
!
! EXAMPLE
! See test file
!
! HISTORY
!> \author David Schaefer
!> \date June 2015
!
! -----------------------------------------------------------------------------------
generic, public :: setAttribute => &
setGlobalAttributeChar, &
setGlobalAttributeI8, &
setGlobalAttributeI16, &
setGlobalAttributeI32, &
setGlobalAttributeI64, &
setGlobalAttributeF32, &
setGlobalAttributeF64
! -----------------------------------------------------------------------------------
!
! NAME
! getAttribute
!
! PURPOSE
!> \brief Retrieve global attribute value
!> \details Retrieve the value for a global attribute specified by its name.
!> The program will teminate abruptly if the attribute does not
!> exist.
!
! INTENT(IN)
!> \param[in] "character(*) :: name"
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
!> \param[out] "character(*)/integer(i4)/real(sp)/real(dp) :: value"
!
! RETURN
! None
!
! EXAMPLE
! See test file
!
! HISTORY
!> \author David Schaefer
!> \date June 2015
!
! -----------------------------------------------------------------------------------
generic, public :: getAttribute => &
getGlobalAttributeChar, &
getGlobalAttributeI8, &
getGlobalAttributeI16, &
getGlobalAttributeI32, &
getGlobalAttributeI64, &
getGlobalAttributeF32, &
getGlobalAttributeF64
! -----------------------------------------------------------------------------------
!
! NAME
! getDimension
!
! PURPOSE
!> \brief Retrieve NcDimension
!> \details Retrieve the NcDimension derived type for the dimension specified by
!> its name or id. The program will teminate abruptly if no such
!> dimension exists.
!
! INTENT(IN)
!> \param[in] "character(*)/integer(i4) :: name/id"
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! RETURN
!> \return NcDimension
!
! EXAMPLE
! See test file
!
! HISTORY
!> \author David Schaefer
!> \date June 2015
!
! -----------------------------------------------------------------------------------
generic, public :: getDimension => &
getDimensionById, &
getDimensionByName
! -----------------------------------------------------------------------------------
!
! NAME
! setVariable
!
! PURPOSE
!> \brief Create a NetCDF variable
!> \details Create a NetCDF Variable with given name, data type and dimensions.
!> All optional arguments to the nf90_def_var function are supported.
!> The program will teminate abruptly if the variable cannot be
!> created.
!> Supported data types and their string encodings:
!> NF90_BYTE -> "i8"
!> NF90_SHORT -> "i16"
!> NF90_INT -> "i32"
!> NF90_INT64 -> "i64"
!> NF90_FLOAT -> "f32"
!> NF90_DOUBLE -> "f64"
!
! INTENT(IN)
!> \param[in] "character(*) :: name"
!> \param[in] "character(3) :: dtype"
!> \param[in] "integer(i4)/character(*)/type(NcDataset) :: dimensions(:)"
!
! INTENT(IN), OPTIONAL
!> \param[in] "logical :: contiguous"
!> \param[in] "integer(i4) :: chunksizes(:)"
!> \param[in] "integer(i4) :: deflate_level"
!> \param[in] "logical :: shuffle"
!> \param[in] "logical :: fletcher32"
!> \param[in] "integer(i4) :: endianess"
!> \param[in] "integer(i4) :: cache_size"
!> \param[in] "integer(i4) :: cache_nelems"
!> \param[in] "integer(i4) :: cache_preemption"
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! RETURN
!> \return NcVariable
!
! EXAMPLE
! See test file
!
! HISTORY
!> \author David Schaefer
!> \date June 2015
!
! -----------------------------------------------------------------------------------
generic, public :: setVariable => &
setVariableWithNames, &
setVariableWithTypes, &
setVariableWithIds
! -----------------------------------------------------------------------------------
!
! NAME
! getVariable
!
! PURPOSE
!> \brief Retrieve NcVariable
!> \details Retrieve the NcVariable derived type for the variable specified by its
!> name. The program will teminate abruptly if no such dimension
!> exists.
!
! INTENT(IN)
!> \param[in] "character(*) :: name"
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! RETURN
!> \return NcVariable
!
! EXAMPLE
! See test file
!
! HISTORY
!> \author David Schaefer
!> \date June 2015
!
! -----------------------------------------------------------------------------------
generic, public :: getVariable => &
getVariableByName
end type NcDataset
interface NcDataset
procedure newNcDataset
end interface NcDataset
! --------------------------------------------------------------------------------------
!
! NAME
! NcDimension
!
! PURPOSE
!> \brief Provides the dimension access functionality
!> \details Bound to this derived type is some necessary inquire functionality.
!> This type is not to be instantiated directly! Use the
!> getDimension/setDimension functions of a NcDataset instance as a
!> "constructor".
!
! INTENT(IN)
! None
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! RETURN
! None
!
! EXAMPLE
! See test file
!
! HISTORY
! \author David Schaefer
! \date June 2015
!
! --------------------------------------------------------------------------------------
type NcDimension
integer(i4) :: id !> The NetCDF dimension id
type(NcDataset) :: parent !> The dimension's parent
contains
procedure, public :: initNcDimension
procedure, public :: getName => getDimensionName
! -----------------------------------------------------------------------------------
!
! NAME
! getLength
!