From 3353cad0b365bc870306ccaa21f701859e68a92f Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Wed, 8 Nov 2023 16:20:32 +0100 Subject: codec-netcdf: Multidimensional arrays implemented --- c++/codec-netcdf/netcdf.h | 6 +- c++/codec-netcdf/netcdf.tmpl.h | 123 +++++++++++++++++++++++++++++++++++++---- tests/.nix/derivation.nix | 2 + tests/SConstruct | 8 ++- tests/codec-netcdf.cpp | 43 +++++++++++--- tests/data/array.nc | Bin 0 -> 156 bytes tests/data/primitive.nc | Bin 0 -> 112 bytes 7 files changed, 159 insertions(+), 23 deletions(-) create mode 100644 tests/data/array.nc create mode 100644 tests/data/primitive.nc diff --git a/c++/codec-netcdf/netcdf.h b/c++/codec-netcdf/netcdf.h index d579b7f..c99217e 100644 --- a/c++/codec-netcdf/netcdf.h +++ b/c++/codec-netcdf/netcdf.h @@ -2,7 +2,7 @@ #include #include -#include +#include #include @@ -66,7 +66,7 @@ public: */ template error_or encode() { - return void_t{}; + return make_error(); } /** @@ -78,7 +78,7 @@ public: int rc{}; rc = nc_open(from_decode.get_path().c_str(), NC_NOWRITE, &ncid); - if(rc){ + if(rc != NC_NOERR){ // Don't know how to get the error, so fail critically. return make_error(); } diff --git a/c++/codec-netcdf/netcdf.tmpl.h b/c++/codec-netcdf/netcdf.tmpl.h index 921c84e..f3b36b8 100644 --- a/c++/codec-netcdf/netcdf.tmpl.h +++ b/c++/codec-netcdf/netcdf.tmpl.h @@ -2,6 +2,41 @@ namespace saw { namespace impl { +template +struct netcdf_primitive_id { + static_assert(always_false, "Not a primitive id"); +}; + +template<> +struct netcdf_primitive_id { + static constexpr nc_type value = NC_INT; +}; + +template<> +struct netcdf_primitive_id { + static constexpr nc_type value = NC_UINT; +}; + +template<> +struct netcdf_primitive_id { + static constexpr nc_type value = NC_INT64; +}; + +template<> +struct netcdf_primitive_id { + static constexpr nc_type value = NC_UINT64; +}; + +template<> +struct netcdf_primitive_id { + static constexpr nc_type value = NC_FLOAT; +}; + +template<> +struct netcdf_primitive_id { + static constexpr nc_type value = NC_DOUBLE; +}; + template struct netcdf_is_group { static constexpr bool value = false; @@ -12,12 +47,13 @@ struct netcdf_is_group...>> { static constexpr bool value = true; }; -template +template struct netcdf_decode; -template -struct netcdf_decode { - using Schema = schema::Int32; +template +struct netcdf_decode, ToDecode> { + using Schema = schema::Primitive; + static error_or decode(data& to, int from, int nc_varid){ int rc{}; @@ -29,15 +65,15 @@ struct netcdf_decode { if(rc != NC_NOERR){ return make_error(); } - if(nc_vartype != NC_INT){ + if(nc_vartype != netcdf_primitive_id>::value){ return make_error(); } if(nc_dimnum != 0){ return make_error(); } - int32_t val{}; - rc = nc_get_var_int(from, nc_varid, &val); + typename native_data_type>::type val{}; + rc = nc_get_var(from, nc_varid, &val); if(rc != NC_NOERR){ return make_error(); } @@ -48,8 +84,71 @@ struct netcdf_decode { } }; -template -struct netcdf_decode...>, RootSchema, ToDecode> { +template +struct netcdf_decode, ToDecode> { + using Schema = schema::Array; + + template + static error_or decode_level(data& to, int from, int nc_varid, std::array& index, const std::array& count){ + if constexpr ( Level == Dim ){ + int rc{}; + typename native_data_type::type val; + rc = nc_get_vara(from, nc_varid, index.data(), count.data(), &val); + if(rc != NC_NOERR){ + return make_error(); + } + to.at(index).set(val); + }else{ + const std::size_t dim_size = to.get_dim_size(Level); + for(index[Level] = 0; index[Level] < dim_size; ++index[Level]){ + auto eov = decode_level(to, from, nc_varid, index, count); + if(eov.is_error()){ + return eov; + } + } + } + + return void_t{}; + } + + static error_or decode(data& to, int from, int nc_varid){ + int rc{}; + + nc_type nc_vartype{}; + + int nc_dimnum{}; + std::array nc_dimids; + + rc = nc_inq_var(from, nc_varid, nullptr, &nc_vartype, &nc_dimnum, &nc_dimids[0], nullptr); + if(rc != NC_NOERR){ + return make_error(); + } + if(nc_vartype != netcdf_primitive_id::value){ + return make_error(); + } + if(nc_dimnum != Dim){ + return make_error(); + } + + std::array dims; + std::array count; + for(std::size_t i = 0; i < Dim; ++i){ + rc = nc_inq_dim(from, nc_dimids[i], nullptr, &dims[i]); + if(rc != NC_NOERR){ + return make_error(); + } + count[i] = 1; + } + + to = {dims}; + std::array index; + + return decode_level<0>(to, from, nc_varid, index, count); + } +}; + +template +struct netcdf_decode...>, ToDecode> { using Schema = schema::Struct...>; template @@ -67,7 +166,7 @@ struct netcdf_decode...>, RootSchema, ToDe if(rc != NC_NOERR) { return make_error(); } - auto eov = netcdf_decode::decode(to.template get(), nc_group_id); + auto eov = netcdf_decode::decode(to.template get(), nc_group_id); if(eov.is_error()){ return eov; } @@ -78,12 +177,12 @@ struct netcdf_decode...>, RootSchema, ToDe if(rc != NC_NOERR) { return make_error(); } - auto eov = netcdf_decode::decode(to.template get(), from, nc_varid); + auto eov = netcdf_decode::decode(to.template get(), from, nc_varid); } } if constexpr ((i+1) < sizeof...(T)){ - auto eov = decode_member(from, to, ncid); + auto eov = decode_member(to, from); if(eov.is_error()){ return eov; } diff --git a/tests/.nix/derivation.nix b/tests/.nix/derivation.nix index e096e70..ea40920 100644 --- a/tests/.nix/derivation.nix +++ b/tests/.nix/derivation.nix @@ -4,6 +4,7 @@ , clang-tools , version , forstio +, netcdf }: stdenv.mkDerivation { @@ -24,6 +25,7 @@ stdenv.mkDerivation { forstio.io forstio.test forstio.window + netcdf ]; doCheck = true; diff --git a/tests/SConstruct b/tests/SConstruct index 4434d20..88477f7 100644 --- a/tests/SConstruct +++ b/tests/SConstruct @@ -46,7 +46,13 @@ env_vars.Add('prefix', env=Environment(ENV=os.environ, variables=env_vars, CPPPATH=[], CPPDEFINES=['SAW_UNIX'], CXXFLAGS=['-std=c++20','-g','-Wall','-Wextra'], - LIBS=['forstio-core', 'forstio-async', 'forstio-test']) + LIBS=[ + 'forstio-core', + 'forstio-async', + 'forstio-test', + 'netcdf' + ] +); env.__class__.add_source_files = add_kel_source_files env.Tool('compilation_db'); env.cdb = env.CompilationDatabase('compile_commands.json'); diff --git a/tests/codec-netcdf.cpp b/tests/codec-netcdf.cpp index df0037b..72fc5ea 100644 --- a/tests/codec-netcdf.cpp +++ b/tests/codec-netcdf.cpp @@ -5,21 +5,50 @@ namespace { namespace schema { using namespace saw::schema; using TestStruct = Struct< - Member + Member, + Member +>; + +using TestArrayStruct = Struct< + Member, "data"> >; } -SAW_TEST("NetCDF read"){ +SAW_TEST("NetCDF Struct Primitive read"){ using namespace saw; - data net{"./data/simple.nc"}; + data netcdf{"./data/primitive.nc"}; - data kel; + data native; - codec codec; + codec codec; - auto eov = codec.decode(net, kel); + auto eov = codec.decode(netcdf, native); SAW_EXPECT(eov.is_value(), "Decoding failed"); - SAW_EXPECT(kel.get<"rh">.get() == 5, "Value incorrect"); + SAW_EXPECT(native.get<"data">().get() == 5, "Int Value incorrect"); + SAW_EXPECT(native.get<"other">().get() == 32.0, "Double Value incorrect"); +} + +SAW_TEST("NetCDF Struct Array read"){ + using namespace saw; + + data netcdf{"./data/array.nc"}; + + data native; + + codec codec; + + auto eov = codec.decode(netcdf, native); + SAW_EXPECT(eov.is_value(), "Decoding failed"); + auto& arr = native.get<"data">(); + SAW_EXPECT(arr.get_dim_size(0) == 5, "Incorrect dimension 0"); + SAW_EXPECT(arr.get_dim_size(1) == 3, "Incorrect dimension 1"); + + for(std::size_t i = 0; i < 5; ++i){ + for(std::size_t j = 0; j < 3; ++j){ + int64_t exp_val = i * 3 + j; + SAW_EXPECT(arr.at(i,j).get() == exp_val, "Incorrect value"); + } + } } } diff --git a/tests/data/array.nc b/tests/data/array.nc new file mode 100644 index 0000000..2155846 Binary files /dev/null and b/tests/data/array.nc differ diff --git a/tests/data/primitive.nc b/tests/data/primitive.nc new file mode 100644 index 0000000..ab49e3b Binary files /dev/null and b/tests/data/primitive.nc differ -- cgit v1.2.3