From 7bd76b345d651be50251ec422928df0376235b48 Mon Sep 17 00:00:00 2001 From: Sam Harrison Date: Fri, 26 Mar 2021 17:19:28 +0000 Subject: [PATCH] Adding ability to create scalar variables --- README.md | 5 +++-- src/mo_netcdf.f90 | 16 +++++++++++++++- src/mo_types.mod | Bin 0 -> 621 bytes tests/test_setdata.f90 | 8 +++++++- 4 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 src/mo_types.mod 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 0000000000000000000000000000000000000000..747a6747e85385ba183b9e2ec5fa74e23fef44e3 GIT binary patch literal 621 zcmV-z0+Rh7iwFP!000001J#yYZ`v>vhVT6=&Yj6cCx-+YE~AiE(ZE4Y=+rB#h0sMy zlZntt`}aG}2P7rX0h>@22zGo#&SSqme(&#m5el2bYqoq#BKQ@p^EgX^>H`Xkb@a4} zmatl9uNWXVzkf#gm(_@oUQdI6@ga3XBiQWt_JK+n=b2o{=Qv%)=?m?8LP3XKZ|aZd z6Bp+EEBC+g8;^%B4@upf3E!#5w!aK7j1wLYJg3a1Ii#+tkNIdtzf76!F#5nWVgOyp zW%l-*L`O25*}|T>p({>LGLVi`!NQ-Zju-f{N?1L;$GPgUq{`;m{p8auUHn@m;C5mF7!&q-w+q|$LT)`wTdI<1_vWm^gH zJ)9~hx{h#HSK>u*-FhtG&~wCB7vx27-Lfp;(5J-Ke_-3EsI1kILP(B>Q~mJnGFt?D zFr7~D!-M*$|7$TcGa_Y1=pgbai56&>m64D?@ZiCGde~maX&x`M-BjcI-SJ$UZdLE* z+zCCOgDr&pDE&FEmmIq@myccUJeC=ezazkm+movN_Wrra@a$<=TLwnM2{JIZ#)-jz zv|R>+VdF?U46Lf=GZqm H*bD#w&j>jS literal 0 HcmV?d00001 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)