Skip to content

Commit

Permalink
Merge pull request #6 from samharrison7/scalar_var
Browse files Browse the repository at this point in the history
Adding ability to create scalar variables
  • Loading branch information
schaefed committed Mar 26, 2021
2 parents d903514 + 7bd76b3 commit 75a5611
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
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

0 comments on commit 75a5611

Please sign in to comment.