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
|
#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_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 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{};
}
};
}
}
|