summaryrefslogtreecommitdiff
path: root/c++/codec-netcdf/netcdf.h
blob: c99217eb4434801a92d456174f829b6055d9f9ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#pragma once

#include <forstio/core/string_literal.h>
#include <forstio/core/error.h>
#include <forstio/codec/data.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 make_error<err::not_implemented>();
		}

		/**
		 * 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 != NC_NOERR){
						// 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;
		}
};
}