summaryrefslogtreecommitdiff
path: root/modules/codec-netcdf/c++/netcdf.hpp
blob: f93ecebdda7598a7e75db282b69c4f9df5809a56 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#pragma once

#include <forstio/string_literal.hpp>
#include <forstio/error.hpp>
#include <forstio/codec/data.hpp>

#include <netcdf.h>
#include <netcdf_mem.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::vector<uint8_t> buff_;
public:
		data() = default;

		data(std::vector<uint8_t> buff):
				buff_{std::move(buff)}
		{}

		template<size_t N>
		data(const std::array<uint8_t, N>& arr):
			buff_{arr.begin(), arr.end()}
		{}

		std::vector<uint8_t>& get_data() {
				return buff_;
		}

		const std::vector<uint8_t>& get_data() const {
				return buff_;
		}
};

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.hpp"
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(const data<Schema, FromEncoding>& from, data<Schema,encode::Netcdf>& to) {
			int rc{};
			int ncid{};

			rc = nc_create_mem("forstio_internal_write_memory", 0,/*NC_NETCDF4, */0, &ncid);
			if(rc != NC_NOERR){
				return make_error<err::critical>();
			}

			{
				auto eov = impl::netcdf_encode<Schema,FromEncoding>::encode_meta(from, ncid);
				if(eov.is_error()){
					nc_close(ncid);
					return eov;
				}
			}

			rc = nc_enddef(ncid);
			if(rc != NC_NOERR){
				nc_close(ncid);
				return make_error<err::critical>();
			}

			{
				auto eov = impl::netcdf_encode<Schema,FromEncoding>::encode(from, ncid);
				if(eov.is_error()){
					nc_close(ncid);
					return eov;
				}
			}

			NC_memio nc_memio;
			rc = nc_close_memio(ncid, &nc_memio);
			if(rc != NC_NOERR){
				return make_error<err::critical>();
			}

			if(!nc_memio.memory || nc_memio.size == 0){
				return make_error<err::critical>();
			}

			try {
				to.get_data().resize(nc_memio.size);
			}catch(const std::exception& e){
				(void) e;
				return make_error<err::out_of_memory>();
			}

			for(std::size_t i = 0; i < nc_memio.size; ++i){
				to.get_data().at(i) = static_cast<uint8_t*>(nc_memio.memory)[i];
			}

			free(nc_memio.memory);
			nc_memio.memory = nullptr;

			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_mem("forstio_internal_read_memory", NC_NOWRITE, from_decode.get_data().size(), &from_decode.get_data()[0], &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;
		}
};
}