summaryrefslogtreecommitdiff
path: root/modules/codec-vtk/c++/netcdf.tmpl.hpp
blob: bf257e4f6bd9c09f9c79c9c9876f861ca459055a (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#pragma once

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;
};

template<typename... T, string_literal... Lits>
struct netcdf_is_group<schema::Struct<schema::Member<T,Lits>...>> {
		static constexpr bool value = true;
};

template<typename Schema, typename ToEncode>
struct netcdf_encode;

template<typename T, size_t N, typename ToEncode>
struct netcdf_encode<schema::Primitive<T,N>, ToEncode> {
	using Schema = schema::Primitive<T,N>;

	static error_or<void> encode(const data<Schema, ToEncode>& from, int ncid, int ncvarid){
		int rc{};

		typename native_data_type<Schema>::type val = from.get();

		rc = nc_put_var(ncid, ncvarid, &val);
		if(rc != NC_NOERR) {
			return make_error<err::critical>();
		}

		return void_t{};
	}
	
	static error_or<void> encode_meta(const data<Schema, ToEncode>& from, int ncid, int ncvarid){
		(void) from;
		(void) ncid;
		(void) ncvarid;
		return void_t{};
	}
};

template<typename T, size_t Dim, typename ToEncode>
struct netcdf_encode<schema::Array<T,Dim>, ToEncode> {
	using Schema = schema::Array<T,Dim>;

	template<size_t Level>
	static error_or<void> encode_level(data<Schema,ToEncode>& from, int ncid, int ncvarid){

		return make_error<err::not_implemented>();
	}

	static error_or<void> encode(data<Schema, ToEncode>& from, int ncid, int ncvarid){
		
		return make_error<err::not_implemented>();
	}

	static error_or<void> encode_meta(const data<Schema, ToEncode>& from, int ncid, int ncvarid){

		return make_error<err::not_implemented>();
	}
};

template<typename... T, string_literal... Lits, typename ToEncode>
struct netcdf_encode<schema::Struct<schema::Member<T,Lits>...>, ToEncode> {
	using Schema = schema::Struct<schema::Member<T,Lits>...>;

	template<std::size_t i>
	static error_or<void> encode_member(const data<Schema, ToEncode>& from, int ncid){
			using Type = typename parameter_pack_type<i,T...>::type;
			constexpr string_literal Literal = parameter_key_pack_type<i, Lits...>::literal;
			{
				/**
				 * We have to ask for the internal id of the netcdf structure
				 */
				if constexpr (netcdf_is_group<Type>::value){
					return make_error<err::not_implemented>();
				}else{
					int nc_varid{};
					int rc {};
					rc = nc_inq_varid(ncid, Literal.data.data(), &nc_varid);
					if(rc != NC_NOERR) {
							return make_error<err::critical>();
					}
					auto eov = netcdf_encode<Type, ToEncode>::encode(from.template get<Literal>(), ncid, nc_varid);
					if(eov.is_error()){
						return eov;
					}
				}

			}
			if constexpr ((i+1) < sizeof...(T)){
				auto eov = encode_member<i+1>(from, ncid);
				if(eov.is_error()){
						return eov;
				}
			}

		return void_t{};
	}
	
	static error_or<void> encode(const data<Schema, ToEncode>& from, int ncid){
		if constexpr (sizeof...(T) > 0){
				auto eov = encode_member<0>(from, ncid);
				return eov;
		}
		
		return void_t{};
	}

	template<std::size_t i>
	static error_or<void> encode_meta_member(const data<Schema, ToEncode>& from, int ncid){
			using Type = typename parameter_pack_type<i,T...>::type;
			constexpr string_literal Literal = parameter_key_pack_type<i, Lits...>::literal;
			{
				/**
				 * We have to ask for the internal id of the netcdf structure
				 */
				if constexpr (netcdf_is_group<Type>::value){
					return make_error<err::not_implemented>();
				}else{
					int nc_varid{};
					int rc {};
					rc = nc_def_var(ncid, Literal.data.data(), netcdf_primitive_id<Type>::value, 0, nullptr, &nc_varid);
					if(rc != NC_NOERR) {
							return make_error<err::critical>();
					}
					auto eov = netcdf_encode<Type, ToEncode>::encode_meta(from.template get<Literal>(), ncid, nc_varid);
					if(eov.is_error()){
						return eov;
					}
				}

			}
			if constexpr ((i+1) < sizeof...(T)){
				auto eov = encode_meta_member<i+1>(from, ncid);
				if(eov.is_error()){
						return eov;
				}
			}

		return void_t{};
	}

	static error_or<void> encode_meta(const data<Schema, ToEncode>& from, int ncid){
		if constexpr (sizeof...(T) > 0){
				auto eov = encode_meta_member<0>(from, ncid);
				return eov;
		}
		
		return void_t{};
	}
};

template<typename Schema, typename ToDecode>
struct netcdf_decode;

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{};
				
				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<schema::Primitive<T,N>>::value){
						return make_error<err::critical>();
				}
				if(nc_dimnum != 0){
						return make_error<err::critical>();
				}
				
				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>();
				}

				to.set(val);

				return void_t {};
		}
};

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>
	static error_or<void> decode_member(data<Schema,ToDecode>& to, int from){
			using Type = typename parameter_pack_type<i,T...>::type;
			constexpr string_literal Literal = parameter_key_pack_type<i, Lits...>::literal;
			{
				/**
				 * We have to ask for the internal id of the netcdf structure
				 */
				if constexpr (netcdf_is_group<Type>::value){
					int nc_group_id{};
					int rc {};
					rc = nc_inq_ncid(from, Literal.data.data(), &nc_group_id);
					if(rc != NC_NOERR) {
							return make_error<err::critical>();
					}
					auto eov = netcdf_decode<Type, ToDecode>::decode(to.template get<Literal>(), nc_group_id);
					if(eov.is_error()){
							return eov;
					}
				}else{
					int nc_varid{};
					int rc {};
					rc = nc_inq_varid(from, Literal.data.data(), &nc_varid);
					if(rc != NC_NOERR) {
							return make_error<err::critical>();
					}
					auto eov = netcdf_decode<Type, ToDecode>::decode(to.template get<Literal>(), from, nc_varid); 
					if(eov.is_error()){
						return eov;
					}
				}

			}
			if constexpr ((i+1) < sizeof...(T)){
				auto eov = decode_member<i+1>(to, from);
				if(eov.is_error()){
						return eov;
				}
			}

			return void_t{};
	}


	static error_or<void> decode(data<Schema, ToDecode>& to, int from){
		if constexpr (sizeof...(T) > 0){
				auto eov = decode_member<0>(to, from);
				return eov;
		}
		
		return void_t{};
	}
};
}
}