-
Notifications
You must be signed in to change notification settings - Fork 5
/
h5_attr.cpp
345 lines (286 loc) · 11 KB
/
h5_attr.cpp
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
#include "plugin.h"
#include "scopedptr.h"
#include <vector>
using namespace Gatan;
static void read_scalar_attr(const char* attr_name, hid_t attr_id, hid_t type_id, DM::TagGroup& tags)
{
switch (H5Tget_class(type_id)) {
case H5T_FLOAT:
if (H5Tget_size(type_id) <= 4) {
float value;
if (H5Aread(attr_id, H5T_NATIVE_FLOAT, &value) >= 0)
tags.SetTagAsFloat(attr_name, value);
} else {
double value;
if (H5Aread(attr_id, H5T_NATIVE_DOUBLE, &value) >= 0)
tags.SetTagAsDouble(attr_name, value);
}
break;
case H5T_INTEGER:
if (H5Tget_sign(type_id)) {
long value;
if (H5Aread(attr_id, H5T_NATIVE_LONG, &value) >= 0)
tags.SetTagAsLong(attr_name, value);
} else {
uint32_t value;
if (H5Aread(attr_id, H5T_NATIVE_UINT32, &value) >= 0)
tags.SetTagAsUInt32(attr_name, value);
}
break;
case H5T_STRING:
if (H5Tis_variable_str(type_id)) {
// variable length string
scoped_ptr<char, free> data;
type_handle_t strtype(H5Tcopy(H5T_C_S1));
H5Tset_size(strtype.get(), H5T_VARIABLE);
H5Tset_cset(strtype.get(), H5T_CSET_UTF8);
// Hack: scoped_ptr has same memory layout as pointer.
if (H5Aread(attr_id, strtype.get(), &data) < 0)
return;
tags.SetTagAsString(attr_name, from_UTF8(data.get()));
} else {
// Fixed size string
size_t size = H5Tget_size(type_id);
type_handle_t strtype(H5Tcopy(H5T_C_S1));
H5Tset_strpad(strtype.get(), H5T_STR_NULLTERM);
H5Tset_size(strtype.get(), size + 1);
H5Tset_cset(strtype.get(), H5T_CSET_UTF8);
std::vector<char> data(size+1);
if (H5Aread(attr_id, strtype.get(), &data[0]) < 0)
return;
tags.SetTagAsString(attr_name, from_UTF8(&data[0]));
}
break;
case H5T_COMPOUND:
{
type_handle_t memtype = create_compatible_complex_type(type_id);
if (!memtype.valid())
return;
if (H5Tget_size(memtype.get()) <= 8) {
complex64 value;
if (H5Aread(attr_id, memtype.get(), &value) >= 0)
tags.SetTagAsFloatComplex(attr_name, complex128(value));
} else {
complex128 value;
if (H5Aread(attr_id, memtype.get(), &value) >= 0)
tags.SetTagAsDoubleComplex(attr_name, value);
}
}
break;
default:
break;
}
}
static void read_array_attr(const char* attr_name, hid_t attr_id, hid_t type_id, hid_t space_id, DM::TagGroup& tags)
{
hssize_t size = H5Sget_simple_extent_npoints(space_id);
if (size <= 0)
return;
int rank = H5Sget_simple_extent_ndims(space_id);
if (rank <= 0 || rank > 4)
return;
hsize_t dims[4], ct[4];
if (H5Sget_simple_extent_dims(space_id, dims, NULL) < 0)
return;
DM::TagGroup list[4];
#define ITERATE_ARRAY(_insert_method, _defer_iter, _advance_iter) do {\
list[0] = DM::NewTagList(); \
ct[0] = 0; \
int n = 0; \
while (n >= 0) { \
if (n < (rank - 1)) { \
list[n + 1] = list[n].CreateListTagAtEnd(); \
ct[n + 1] = 0; \
++n; \
} else { \
for (int m = 0; m < dims[n]; ++m, _advance_iter) \
list[n]._insert_method(m, _defer_iter); \
--n; \
while (n >= 0) { \
if (++ct[n] < dims[n]) \
break; \
--n; \
} \
} \
} \
} while(0)
switch (H5Tget_class(type_id)) {
case H5T_FLOAT:
if (H5Tget_size(type_id) <= 4) {
std::vector<float> data(std::size_t(size), 0.0);
if (H5Aread(attr_id, H5T_NATIVE_FLOAT, &data[0]) < 0)
return;
std::vector<float>::const_iterator iter = data.begin();
ITERATE_ARRAY(InsertTagAsFloat, *iter, ++iter);
} else {
std::vector<double> data(std::size_t(size), 0.0);
if (H5Aread(attr_id, H5T_NATIVE_DOUBLE, &data[0]) < 0)
return;
std::vector<double>::const_iterator iter = data.begin();
ITERATE_ARRAY(InsertTagAsDouble, *iter, ++iter);
}
break;
case H5T_INTEGER:
if (H5Tget_sign(type_id)) {
std::vector<long> data(std::size_t(size), 0);
if (H5Aread(attr_id, H5T_NATIVE_LONG, &data[0]) < 0)
return;
std::vector<long>::const_iterator iter = data.begin();
ITERATE_ARRAY(InsertTagAsLong, *iter, ++iter);
} else {
std::vector<uint32_t> data(std::size_t(size), 0);
if (H5Aread(attr_id, H5T_NATIVE_UINT32, &data[0]) < 0)
return;
std::vector<uint32_t>::const_iterator iter = data.begin();
ITERATE_ARRAY(InsertTagAsUInt32, *iter, ++iter);
}
break;
case H5T_STRING:
if (H5Tis_variable_str(type_id)) {
// Read variable length array (untested)
type_handle_t strtype(H5Tcopy(H5T_C_S1));
H5Tset_size(strtype.get(), H5T_VARIABLE);
H5Tset_cset(strtype.get(), H5T_CSET_UTF8);
scoped_ptr_array<char, free> data(static_cast<std::size_t>(size));
if (H5Aread(attr_id, strtype.get(), data.unsafe_data()) < 0)
return;
std::size_t iter = 0;
ITERATE_ARRAY(InsertTagAsString, from_UTF8(data.get(iter)), ++iter);
} else {
// Read fixed size array
size_t elemsize = H5Tget_size(type_id);
if (elemsize < 0)
return;
elemsize += 1; // For NUL character
type_handle_t strtype(H5Tcopy(H5T_C_S1));
H5Tset_strpad(strtype.get(), H5T_STR_NULLTERM);
H5Tset_size(strtype.get(), elemsize);
H5Tset_cset(strtype.get(), H5T_CSET_UTF8);
std::vector<char> data(elemsize * (size_t)size);
if (H5Aread(attr_id, strtype.get(), &data[0]) < 0)
return;
const char* iter = &data[0];
ITERATE_ARRAY(InsertTagAsString, from_UTF8(iter), iter += elemsize);
}
break;
case H5T_COMPOUND:
{
type_handle_t memtype = create_compatible_complex_type(type_id);
if (!memtype.valid())
return;
if (H5Tget_size(memtype.get()) <= 8) {
std::vector<complex64> data(std::size_t(size), 0);
if (H5Aread(attr_id, memtype.get(), &data[0]) < 0)
return;
std::vector<complex64>::const_iterator iter = data.begin();
ITERATE_ARRAY(InsertTagAsFloatComplex, complex128(*iter), ++iter);
} else {
std::vector<complex128> data(std::size_t(size), 0);
if (H5Aread(attr_id, memtype.get(), &data[0]) < 0)
return;
std::vector<complex128>::const_iterator iter = data.begin();
ITERATE_ARRAY(InsertTagAsDoubleComplex, *iter, ++iter);
}
}
break;
default:
break;
}
// If we made it here, list[0] is the array we want to insert
tags.SetTagAsTagGroup(attr_name, list[0]);
}
static herr_t attr_iterator(hid_t loc_id, const char *attr_name, const H5A_info_t *ainfo, DM::TagGroup *tags)
{
// Open attribute
attr_handle_t attr(H5Aopen(loc_id, attr_name, H5P_DEFAULT));
if (!attr.valid()) {
debug("get_attr_operator: Error opening attribute \"%s\".\n", attr_name);
return 0;
}
space_handle_t space(H5Aget_space(attr.get()));
if (!space.valid())
return 0;
int rank = H5Sget_simple_extent_ndims(space.get());
if (rank < 0)
return 0;
// Dump type info
type_handle_t type(H5Aget_type(attr.get()));
if (!type.valid())
return 0;
try {
if (rank == 0)
read_scalar_attr(attr_name, attr.get(), type.get(), *tags);
else
read_array_attr(attr_name, attr.get(), type.get(), space.get(), *tags);
} catch (...) {
// pass
}
return 0;
}
DM_TagGroupToken_1Ref h5_read_attr(const char* filename, DM_StringToken location)
{
DM::TagGroup tags;
PLUG_IN_ENTRY
file_handle_t file(H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT));
if (!file.valid()) {
warning("h5_read_attr: Can't open file '%s'.", filename);
return NULL;
}
std::string loc_name = to_UTF8(DM::String(location));
object_handle_t loc(H5Oopen(file.get(), loc_name.c_str(), H5P_DEFAULT));
if (!loc.valid()) {
warning("h5_read_attr: Invalid location '%s'.", loc_name.c_str());
return NULL;
}
tags = DM::NewTagGroup();
hsize_t index = 0;
H5Aiterate(loc.get(), H5_INDEX_NAME, H5_ITER_NATIVE, &index, (H5A_operator2_t)attr_iterator, &tags);
PLUG_IN_EXIT
return tags.release();
}
bool h5_exists_attr(const char* filename, DM_StringToken location, DM_StringToken attr)
{
htri_t result;
PLUG_IN_ENTRY
file_handle_t file(H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT));
if (!file.valid()) {
warning("h5_attr_exists: Can't open file '%s'.", filename);
return NULL;
}
std::string loc_name = to_UTF8(DM::String(location));
object_handle_t loc(H5Oopen(file.get(), loc_name.c_str(), H5P_DEFAULT));
if (!loc.valid()) {
warning("h5_attr_exists: Invalid location '%s'.", loc_name.c_str());
return NULL;
}
std::string attr_name = to_UTF8(DM::String(attr));
result = H5Aexists_by_name(loc.get(), ".", attr_name.c_str(), H5P_DEFAULT);
if (result < 0) {
warning("h5_attr_exists: Error checking existance of attribute '%s'.", attr_name.c_str());
return false;
}
PLUG_IN_EXIT
return result;
}
bool h5_delete_attr(const char* filename, DM_StringToken location, DM_StringToken attr)
{
PLUG_IN_ENTRY
file_handle_t file(H5Fopen(filename, H5F_ACC_RDWR, H5P_DEFAULT));
if (!file.valid()) {
warning("h5_delete_attr: Can't open file '%s'.", filename);
return false;
}
std::string loc_name = to_UTF8(DM::String(location));
object_handle_t loc(H5Oopen(file.get(), loc_name.c_str(), H5P_DEFAULT));
if (!loc.valid()) {
warning("h5_delete_attr: Invalid location '%s'.", loc_name.c_str());
return false;
}
std::string attr_name = to_UTF8(DM::String(attr));
if (H5Adelete_by_name(loc.get(), ".", attr_name.c_str(), H5P_DEFAULT) < 0) {
warning("h5_delete_attr: Error deleting attribute '%s'.", attr_name.c_str());
return false;
}
PLUG_IN_EXIT
return true;
}