summaryrefslogtreecommitdiff
path: root/c++/codec-netcdf/netcdf.h
diff options
context:
space:
mode:
authorClaudius "keldu" Holeksa <mail@keldu.de>2023-11-08 14:07:58 +0100
committerClaudius "keldu" Holeksa <mail@keldu.de>2023-11-08 14:08:19 +0100
commitfeae80e5e4236654ea5a843197e05d9211869750 (patch)
tree38a69e7c498f34a857f594df6bbe5082f863f68b /c++/codec-netcdf/netcdf.h
parentfd29f23d000db081da1976659e72a679b4ebb9c4 (diff)
codec-netcdf: Basic netcdf implementation
Diffstat (limited to 'c++/codec-netcdf/netcdf.h')
-rw-r--r--c++/codec-netcdf/netcdf.h93
1 files changed, 93 insertions, 0 deletions
diff --git a/c++/codec-netcdf/netcdf.h b/c++/codec-netcdf/netcdf.h
new file mode 100644
index 0000000..d579b7f
--- /dev/null
+++ b/c++/codec-netcdf/netcdf.h
@@ -0,0 +1,93 @@
+#pragma once
+
+#include <forstio/core/string_literal.h>
+#include <forstio/core/error.h>
+#include <forstio/codec/codec.h>
+
+#include <netcdf.h>
+
+namespace saw {
+namespace encode {
+/**
+ * Template type hint
+ */
+struct Netcdf {};
+}
+
+/**
+ * Class representing the files system netcdf file
+ */
+template<typename Schema>
+class data<Schema, encode::Netcdf> {
+private:
+ std::string path_;
+public:
+ data(const std::string& path):
+ path_{path}
+ {}
+
+ data(std::string&& path):
+ path_{std::move(path)}
+ {}
+
+ std::string_view get_path_view() {
+ return path_;
+ }
+
+ const std::string& get_path() const {
+ return path_;
+ }
+};
+
+template<typename Schema>
+class codec<Schema, encode::Netcdf>{
+ static_assert(always_false<Schema,encode::Netcdf>, "NetCDF only supports Structs as a root object");
+};
+
+}
+#include "netcdf.tmpl.h"
+namespace saw {
+
+template<typename... Vals, string_literal... Keys>
+class codec<schema::Struct<schema::Member<Vals,Keys>...>, encode::Netcdf> {
+private:
+ using Schema = schema::Struct<schema::Member<Vals,Keys>...>;
+public:
+ SAW_FORBID_COPY(codec);
+ SAW_DEFAULT_MOVE(codec);
+
+ /**
+ * Default constructor
+ */
+ codec() = default;
+
+ /**
+ * Encoder function
+ */
+ template<typename FromEncoding>
+ error_or<void> encode() {
+ return void_t{};
+ }
+
+ /**
+ * Decoder function
+ */
+ template<typename ToEncoding>
+ error_or<void> decode(data<Schema, encode::Netcdf>& from_decode, data<Schema,ToEncoding>& to_decode) {
+ int ncid{};
+ int rc{};
+
+ rc = nc_open(from_decode.get_path().c_str(), NC_NOWRITE, &ncid);
+ if(rc){
+ // Don't know how to get the error, so fail critically.
+ return make_error<err::critical>();
+ }
+
+ auto eov = impl::netcdf_decode<Schema, ToEncoding>::decode(to_decode, ncid);
+
+ nc_close(ncid);
+
+ return eov;
+ }
+};
+}