diff options
Diffstat (limited to 'c++/codec-netcdf')
-rw-r--r-- | c++/codec-netcdf/netcdf.h | 6 | ||||
-rw-r--r-- | c++/codec-netcdf/netcdf.tmpl.h | 123 |
2 files changed, 114 insertions, 15 deletions
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 <forstio/core/string_literal.h> #include <forstio/core/error.h> -#include <forstio/codec/codec.h> +#include <forstio/codec/data.h> #include <netcdf.h> @@ -66,7 +66,7 @@ public: */ template<typename FromEncoding> error_or<void> encode() { - return void_t{}; + return make_error<err::not_implemented>(); } /** @@ -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<err::critical>(); } 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 @@ -3,6 +3,41 @@ namespace saw { namespace impl { template<typename Schema> +struct netcdf_primitive_id { + static_assert(always_false<Schema>, "Not a primitive id"); +}; + +template<> +struct netcdf_primitive_id<schema::Int32> { + static constexpr nc_type value = NC_INT; +}; + +template<> +struct netcdf_primitive_id<schema::UInt32> { + static constexpr nc_type value = NC_UINT; +}; + +template<> +struct netcdf_primitive_id<schema::Int64> { + static constexpr nc_type value = NC_INT64; +}; + +template<> +struct netcdf_primitive_id<schema::UInt64> { + static constexpr nc_type value = NC_UINT64; +}; + +template<> +struct netcdf_primitive_id<schema::Float32> { + static constexpr nc_type value = NC_FLOAT; +}; + +template<> +struct netcdf_primitive_id<schema::Float64> { + static constexpr nc_type value = NC_DOUBLE; +}; + +template<typename Schema> struct netcdf_is_group { static constexpr bool value = false; }; @@ -12,12 +47,13 @@ struct netcdf_is_group<schema::Struct<schema::Member<T,Lits>...>> { static constexpr bool value = true; }; -template<typename Schema, typename RootSchema, typename ToEncode> +template<typename Schema, typename ToEncode> struct netcdf_decode; -template<typename RootSchema, typename ToDecode> -struct netcdf_decode<schema::Int32, RootSchema, ToDecode> { - using Schema = schema::Int32; +template<typename T, size_t N, typename ToDecode> +struct netcdf_decode<schema::Primitive<T,N>, ToDecode> { + using Schema = schema::Primitive<T,N>; + static error_or<void> decode(data<Schema, ToDecode>& to, int from, int nc_varid){ int rc{}; @@ -29,15 +65,15 @@ struct netcdf_decode<schema::Int32, RootSchema, ToDecode> { if(rc != NC_NOERR){ return make_error<err::critical>(); } - if(nc_vartype != NC_INT){ + if(nc_vartype != netcdf_primitive_id<schema::Primitive<T,N>>::value){ return make_error<err::critical>(); } if(nc_dimnum != 0){ return make_error<err::critical>(); } - int32_t val{}; - rc = nc_get_var_int(from, nc_varid, &val); + typename native_data_type<schema::Primitive<T,N>>::type val{}; + rc = nc_get_var(from, nc_varid, &val); if(rc != NC_NOERR){ return make_error<err::critical>(); } @@ -48,8 +84,71 @@ struct netcdf_decode<schema::Int32, RootSchema, ToDecode> { } }; -template<typename... T, string_literal... Lits, typename RootSchema, typename ToDecode> -struct netcdf_decode<schema::Struct<schema::Member<T,Lits>...>, RootSchema, ToDecode> { +template<typename T, size_t Dim, typename ToDecode> +struct netcdf_decode<schema::Array<T,Dim>, ToDecode> { + using Schema = schema::Array<T,Dim>; + + template<std::size_t Level> + static error_or<void> decode_level(data<Schema, ToDecode>& to, int from, int nc_varid, std::array<std::size_t, Dim>& index, const std::array<size_t,Dim>& count){ + if constexpr ( Level == Dim ){ + int rc{}; + typename native_data_type<T>::type val; + rc = nc_get_vara(from, nc_varid, index.data(), count.data(), &val); + if(rc != NC_NOERR){ + return make_error<err::critical>(); + } + 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<Level+1>(to, from, nc_varid, index, count); + if(eov.is_error()){ + return eov; + } + } + } + + return void_t{}; + } + + static error_or<void> decode(data<Schema, ToDecode>& to, int from, int nc_varid){ + int rc{}; + + nc_type nc_vartype{}; + + int nc_dimnum{}; + std::array<int, NC_MAX_VAR_DIMS> 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<err::critical>(); + } + if(nc_vartype != netcdf_primitive_id<T>::value){ + return make_error<err::critical>(); + } + if(nc_dimnum != Dim){ + return make_error<err::critical>(); + } + + std::array<std::size_t, Dim> dims; + std::array<size_t, Dim> 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<err::critical>(); + } + count[i] = 1; + } + + to = {dims}; + std::array<std::size_t, Dim> index; + + return decode_level<0>(to, from, nc_varid, index, count); + } +}; + +template<typename... T, string_literal... Lits, typename ToDecode> +struct netcdf_decode<schema::Struct<schema::Member<T,Lits>...>, ToDecode> { using Schema = schema::Struct<schema::Member<T,Lits>...>; template<std::size_t i> @@ -67,7 +166,7 @@ struct netcdf_decode<schema::Struct<schema::Member<T,Lits>...>, RootSchema, ToDe if(rc != NC_NOERR) { return make_error<err::critical>(); } - auto eov = netcdf_decode<Type, RootSchema, ToDecode>::decode(to.template get<Literal>(), nc_group_id); + auto eov = netcdf_decode<Type, ToDecode>::decode(to.template get<Literal>(), nc_group_id); if(eov.is_error()){ return eov; } @@ -78,12 +177,12 @@ struct netcdf_decode<schema::Struct<schema::Member<T,Lits>...>, RootSchema, ToDe if(rc != NC_NOERR) { return make_error<err::critical>(); } - auto eov = netcdf_decode<Type, RootSchema, ToDecode>::decode(to.template get<Literal>(), from, nc_varid); + auto eov = netcdf_decode<Type, ToDecode>::decode(to.template get<Literal>(), from, nc_varid); } } if constexpr ((i+1) < sizeof...(T)){ - auto eov = decode_member<i+1>(from, to, ncid); + auto eov = decode_member<i+1>(to, from); if(eov.is_error()){ return eov; } |