diff --git a/README.md b/README.md index 0307e04..e32a17c 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: diff --git a/src/mo_netcdf.f90 b/src/mo_netcdf.f90 index 798bb72..4427aba 100644 --- a/src/mo_netcdf.f90 +++ b/src/mo_netcdf.f90 @@ -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 @@ -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 diff --git a/src/mo_types.mod b/src/mo_types.mod new file mode 100644 index 0000000..747a674 Binary files /dev/null and b/src/mo_types.mod differ diff --git a/tests/test_setdata.f90 b/tests/test_setdata.f90 index b266cc1..01c17e8 100644 --- a/tests/test_setdata.f90 +++ b/tests/test_setdata.f90 @@ -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 @@ -21,6 +21,7 @@ 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) @@ -28,6 +29,11 @@ program test_putdata 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)