Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ability to create scalar variables #6

Merged
merged 1 commit into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use mo_netcdf, only: NcDataset, NcDimension, NcVariable, NcGroup

type(NcDataset) :: nc
type(NcDimension) :: dim1, dim2, dim3
type(NcVariable) :: var
type(NcVariable) :: var, scalar
type(NcGroup) :: grp

! some data
Expand Down Expand Up @@ -59,8 +59,9 @@ dim3 = grp%setDimension("x", nx)
! args:
! variable name
! data type (currently available: "i8", "i16", "i32", "f32", "f64")
! dimensions array
! dimensions array (optional: skip for a scalar)
var = grp%setVariable("data", "i32", (/dim3, dim2, dim1/))
scalar = grp%setVariable("scalar", "i32")

! define a fill value
! args:
Expand Down
16 changes: 15 additions & 1 deletion src/mo_netcdf.f90
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,13 @@ module mo_netcdf
procedure, private :: setVariableWithTypes
procedure, private :: setVariableWithNames
procedure, private :: setVariableWithIds
procedure, private :: setVariableScalar

generic, public :: setVariable => &
setVariableWithNames, &
setVariableWithTypes, &
setVariableWithIds
setVariableWithIds, &
setVariableScalar

end type NcGroup

Expand Down Expand Up @@ -703,6 +705,18 @@ function setVariableWithTypes(self, name, dtype, dimensions, contiguous, &
cache_size, cache_nelems, cache_preemption)
end function setVariableWithTypes

function setVariableScalar(self, name, dtype)
class(NcGroup) , intent(in) :: self
character(*) , intent(in) :: name
character(*) , intent(in) :: dtype
type(NcVariable) :: setVariableScalar
integer(i32) :: varid, status

status = nf90_def_var(self%id, name, getDtypeFromString(dtype), varid=varid)
call check(status, "Failed to create variable: " // name)
setVariableScalar = NcVariable(varid, self)
end function setVariableScalar

function getDimensionById(self, id)
class(NcGroup), intent(in) :: self
integer(i32) :: id
Expand Down
Binary file added src/mo_types.mod
Binary file not shown.
8 changes: 7 additions & 1 deletion tests/test_setdata.f90
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ program test_putdata
use mo_testhelper

type(NcDataset) :: nc
type(NcVariable) :: var
type(NcVariable) :: var, scalar

integer(i32), parameter :: nx=10, ny=20, ntime=8
integer(i32) :: val
Expand All @@ -21,13 +21,19 @@ program test_putdata
"i32", &
[nc%setDimension("x", nx), nc%setDimension("y", ny), nc%setDimension("time")] &
)
scalar = nc%setVariable("scalar", "i32")

! set and get a single value
! i.e. (5,5)
call var%setData(42, start=[5,5])
call var%getData(val, start=[5,5])
call assertEqual(val, 42, "Failed to set a scalar value!")

! set and get a scalar
call scalar%setData(42)
call scalar%getData(val)
call assertEqual(val, 42, "Failed to set a scalar value!")

! set and get the all values for the first row in the third timestep
! i.e. (:,1,3)
write1d = full(nx, 21_i32)
Expand Down