-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathh5readwrite.cpp
1159 lines (950 loc) · 35.5 KB
/
h5readwrite.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This source file is part of the Tomviz project, https://tomviz.org/.
It is released under the 3-Clause BSD License, see "LICENSE". */
#include "h5readwrite.h"
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include "h5capi.h"
#include "h5typemaps.h"
#include "hidcloser.h"
using std::cerr;
using std::cout;
using std::endl;
using std::map;
using std::string;
using std::vector;
namespace {
// Just a convenience function. Only sets "ok" if it is not nullptr.
void setOk(bool* ok, bool status)
{
if (ok)
*ok = status;
}
} // end namespace
namespace h5 {
using DataType = H5ReadWrite::DataType;
class ListAllDataSetsVisitor
{
public:
vector<string> dataSets;
static herr_t operation(hid_t /*o_id*/, const char* name,
const H5O_info_t* object_info, void* op_data)
{
// If this object isn't a dataset, continue
if (object_info->type != H5O_TYPE_DATASET)
return 0;
auto* self = reinterpret_cast<ListAllDataSetsVisitor*>(op_data);
self->dataSets.push_back(name);
return 0;
}
};
class H5ReadWrite::H5ReadWriteImpl
{
public:
H5ReadWriteImpl() {}
H5ReadWriteImpl(const string& file, OpenMode mode)
{
if (mode == OpenMode::ReadOnly) {
if (!openFile(file))
cerr << "Warning: failed to open file " << file << "\n";
} else if (mode == OpenMode::WriteOnly) {
if (!createFile(file))
cerr << "Warning: failed to create file " << file << "\n";
} else {
cerr << "Warning: open mode currently not implemented.\n";
}
}
~H5ReadWriteImpl() { clear(); }
bool openFile(const string& file)
{
m_fileId = H5Fopen(file.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
return fileIsValid();
}
bool createFile(const string& file)
{
m_fileId = H5Fcreate(file.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
return fileIsValid();
}
bool attributeExists(const string& path, const string& name)
{
if (!fileIsValid())
return false;
return H5Aexists_by_name(m_fileId, path.c_str(), name.c_str(),
H5P_DEFAULT) > 0;
}
bool hasAttribute(const string& path)
{
H5O_info_t info;
if (!getInfoByName(path, info)) {
cerr << "Failed to get info by name\n";
return false;
}
return info.num_attrs > 0;
}
bool attribute(const string& path, const string& name, void* value,
hid_t dataTypeId, hid_t memTypeId)
{
if (!attributeExists(path, name)) {
cerr << "Attribute " << path << name << " not found!" << endl;
return false;
}
hid_t attr = H5Aopen_by_name(m_fileId, path.c_str(), name.c_str(),
H5P_DEFAULT, H5P_DEFAULT);
hid_t type = H5Aget_type(attr);
// For automatic closing upon leaving scope
HIDCloser attrCloser(attr, H5Aclose);
HIDCloser typeCloser(type, H5Tclose);
if (H5Tequal(type, dataTypeId) == 0) {
// The type of the attribute does not match the requested type.
cerr << "Type determined does not match that requested." << endl;
cerr << type << " -> " << dataTypeId << endl;
return false;
} else if (H5Tequal(type, dataTypeId) < 0) {
cerr << "Something went really wrong....\n\n";
return false;
}
return H5Aread(attr, memTypeId, value) >= 0;
}
bool setAttribute(const string& path, const string& name, const void* value,
hid_t fileTypeId, hid_t typeId, hsize_t dims)
{
if (!fileIsValid()) {
cerr << "File is not valid\n";
return false;
}
bool onData = isDataSet(path);
hid_t parentId;
herr_t (*closer)(hid_t) = nullptr;
if (onData) {
parentId = H5Dopen(m_fileId, path.c_str(), H5P_DEFAULT);
closer = H5Dclose;
} else {
parentId = H5Gopen(m_fileId, path.c_str(), H5P_DEFAULT);
closer = H5Gclose;
}
HIDCloser parentCloser(parentId, closer);
hid_t dataspaceId = H5Screate_simple(1, &dims, nullptr);
hid_t attributeId = H5Acreate2(parentId, name.c_str(), fileTypeId,
dataspaceId, H5P_DEFAULT, H5P_DEFAULT);
HIDCloser dataspaceCloser(dataspaceId, H5Sclose);
HIDCloser attributeCloser(attributeId, H5Aclose);
return H5Awrite(attributeId, typeId, value) >= 0;
}
bool writeData(const string& path, const string& name,
const std::vector<int>& dims, const void* data,
hid_t dataTypeId, hid_t memTypeId)
{
if (!fileIsValid()) {
cerr << "File is invalid\n";
return false;
}
std::vector<hsize_t> h5dim;
for (size_t i = 0; i < dims.size(); ++i) {
h5dim.push_back(static_cast<hsize_t>(dims[i]));
}
hid_t groupId = H5Gopen(m_fileId, path.c_str(), H5P_DEFAULT);
hid_t dataSpaceId =
H5Screate_simple(static_cast<int>(dims.size()), &h5dim[0], nullptr);
hid_t dataId = H5Dcreate(groupId, name.c_str(), dataTypeId, dataSpaceId,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
HIDCloser groupCloser(groupId, H5Gclose);
HIDCloser spaceCloser(dataSpaceId, H5Sclose);
HIDCloser dataCloser(dataId, H5Dclose);
hid_t status =
H5Dwrite(dataId, memTypeId, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
return status >= 0;
}
bool createDataSet(const std::string& path, const std::string& name,
const std::vector<int>& dims, hid_t dataTypeId,
const std::vector<int>& chunkDims)
{
if (!fileIsValid()) {
cerr << "File is invalid\n";
return false;
}
std::vector<hsize_t> h5dim;
for (size_t i = 0; i < dims.size(); ++i) {
h5dim.push_back(static_cast<hsize_t>(dims[i]));
}
hid_t groupId = H5Gopen(m_fileId, path.c_str(), H5P_DEFAULT);
hid_t dcplId = H5P_DEFAULT;
if (!chunkDims.empty()) {
std::vector<hsize_t> h5ChunkDims;
for (size_t i = 0; i < chunkDims.size(); ++i) {
h5ChunkDims.push_back(static_cast<hsize_t>(chunkDims[i]));
}
dcplId = H5Pcreate(H5P_DATASET_CREATE);
H5Pset_chunk(dcplId, h5ChunkDims.size(), &h5ChunkDims[0]);
}
HIDCloser dcplCloser(dcplId, H5Pclose);
hid_t dataSpaceId =
H5Screate_simple(static_cast<int>(dims.size()), &h5dim[0], nullptr);
hid_t dataId = H5Dcreate(groupId, name.c_str(), dataTypeId, dataSpaceId,
H5P_DEFAULT, dcplId, H5P_DEFAULT);
HIDCloser groupCloser(groupId, H5Gclose);
HIDCloser spaceCloser(dataSpaceId, H5Sclose);
HIDCloser dataCloser(dataId, H5Dclose);
return dataId >= 0;
}
bool updateData(const string& path, const std::vector<int>& dataSetDimensions,
hid_t memTypeId, const void* data, size_t* start = nullptr,
size_t* counts = nullptr)
{
if (!fileIsValid()) {
cerr << "File is invalid\n";
return false;
}
hid_t dataSetId = H5Dopen(m_fileId, path.c_str(), H5P_DEFAULT);
if (dataSetId < 0) {
cerr << "Failed to get dataSetId\n";
return false;
}
HIDCloser dataSetCloser(dataSetId, H5Dclose);
hid_t dataSpaceId = H5Dget_space(dataSetId);
if (dataSpaceId < 0) {
cerr << "Failed to get dataSpaceId\n";
return false;
}
HIDCloser dataSpaceCloser(dataSpaceId, H5Sclose);
auto ndims = dataSetDimensions.size();
auto stridesVector = vector<hsize_t>(ndims, 1);
// stridesVector[2] = 10;
vector<hsize_t> startVector;
if (start)
startVector = vector<hsize_t>(start, start + ndims);
else
startVector = vector<hsize_t>(ndims, 0);
vector<hsize_t> countsVector;
if (counts) {
countsVector = vector<hsize_t>(counts, counts + ndims);
} else {
countsVector.resize(ndims);
for (size_t i = 0; i < countsVector.size(); ++i) {
countsVector[i] = dataSetDimensions[i] - startVector[i];
}
}
auto status =
H5Sselect_hyperslab(dataSpaceId, H5S_SELECT_SET, startVector.data(),
stridesVector.data(), countsVector.data(), nullptr);
if (status < 0) {
cerr << "Failed to select hyperslab\n";
return false;
}
auto memSpaceId = H5Screate_simple(ndims, countsVector.data(), nullptr);
if (memSpaceId < 0) {
cerr << "Failed to get memSpaceId\n";
return false;
}
HIDCloser memSpaceCloser(memSpaceId, H5Sclose);
status = H5Dwrite(dataSetId, memTypeId, memSpaceId, dataSpaceId,
H5P_DEFAULT, data);
return status >= 0;
}
vector<int> getDimensions(const string& path)
{
vector<int> result;
if (!isDataSet(path)) {
cerr << path << " is not a data set.\n";
return result;
}
hid_t dataSetId = H5Dopen(fileId(), path.c_str(), H5P_DEFAULT);
if (dataSetId < 0) {
cerr << "Failed to get dataSetId\n";
return result;
}
// Automatically close upon leaving scope
HIDCloser dataSetCloser(dataSetId, H5Dclose);
hid_t dataSpaceId = H5Dget_space(dataSetId);
if (dataSpaceId < 0) {
cerr << "Failed to get dataSpaceId\n";
return result;
}
HIDCloser dataSpaceCloser(dataSpaceId, H5Sclose);
int dimCount = H5Sget_simple_extent_ndims(dataSpaceId);
if (dimCount < 1) {
cerr << "Error: number of dimensions is less than 1\n";
return result;
}
hsize_t* h5dims = new hsize_t[dimCount];
int dimCount2 = H5Sget_simple_extent_dims(dataSpaceId, h5dims, nullptr);
if (dimCount != dimCount2) {
cerr << "Error: dimCounts do not match\n";
delete[] h5dims;
return result;
}
result.resize(dimCount);
std::copy(h5dims, h5dims + dimCount, result.begin());
delete[] h5dims;
return result;
}
// void* data needs to be of the appropiate type and size.
// start and counts, if set, get forwarded directly to
// H5Sselect_hyperslab().
bool readData(const string& path, hid_t dataTypeId, hid_t memTypeId,
void* data, int* strides = nullptr, size_t* start = nullptr,
size_t* counts = nullptr)
{
hid_t dataSetId = H5Dopen(m_fileId, path.c_str(), H5P_DEFAULT);
if (dataSetId < 0) {
cerr << "Failed to get dataSetId\n";
return false;
}
// Automatically close upon leaving scope
HIDCloser dataSetCloser(dataSetId, H5Dclose);
hid_t dataSpaceId = H5Dget_space(dataSetId);
if (dataSpaceId < 0) {
cerr << "Failed to get dataSpaceId\n";
return false;
}
HIDCloser dataSpaceCloser(dataSpaceId, H5Sclose);
hid_t memSpace = H5S_ALL;
HIDCloser memSpaceCloser(-1, H5Sclose);
// Select a hyperslab if needed
if (strides || start || counts) {
// First, get the dimensions
vector<int> dims = getDimensions(path);
size_t ndims = dims.size();
// Set defaults if needed
vector<hsize_t> stridesVector;
if (strides)
stridesVector = vector<hsize_t>(strides, strides + ndims);
else
stridesVector = vector<hsize_t>(ndims, 1);
vector<hsize_t> startVector;
if (start)
startVector = vector<hsize_t>(start, start + ndims);
else
startVector = vector<hsize_t>(ndims, 0);
vector<hsize_t> countsVector;
if (counts) {
countsVector = vector<hsize_t>(counts, counts + ndims);
} else {
countsVector.resize(ndims);
for (size_t i = 0; i < countsVector.size(); ++i)
countsVector[i] = (dims[i] - startVector[i]) / stridesVector[i];
}
// Next, select the hyperslab
H5Sselect_hyperslab(dataSpaceId, H5S_SELECT_SET, startVector.data(),
stridesVector.data(), countsVector.data(), nullptr);
// Finally, create the mem space
memSpace = H5Screate_simple(ndims, countsVector.data(), nullptr);
memSpaceCloser.reset(memSpace);
}
hid_t typeId = H5Dget_type(dataSetId);
HIDCloser dataTypeCloser(typeId, H5Tclose);
if (H5Tequal(typeId, dataTypeId) == 0) {
// The type of the data does not match the requested type.
cerr << "Type determined does not match that requested." << endl;
cerr << typeId << " -> " << dataTypeId << endl;
return false;
} else if (H5Tequal(typeId, dataTypeId) < 0) {
cerr << "Something went really wrong....\n\n";
return false;
}
return H5Dread(dataSetId, memTypeId, memSpace, dataSpaceId, H5P_DEFAULT,
data) >= 0;
}
bool getInfoByName(const string& path, H5O_info_t& info)
{
if (!fileIsValid())
return false;
return H5Oget_info_by_name(m_fileId, path.c_str(), &info, H5P_DEFAULT) >= 0;
}
bool isDataSet(const string& path)
{
// It's okay if some of these functions fail, turn off errors
turnOffErrors();
H5O_info_t info;
if (!getInfoByName(path, info)) {
turnOnErrors();
return false;
}
turnOnErrors();
return info.type == H5O_TYPE_DATASET;
}
bool isGroup(const string& path)
{
// It's okay if some of these functions fail, turn off errors
turnOffErrors();
H5O_info_t info;
if (!getInfoByName(path, info)) {
turnOnErrors();
return false;
}
turnOnErrors();
return info.type == H5O_TYPE_GROUP;
}
vector<string> allDataSets(const string& path)
{
if (!fileIsValid())
return vector<string>();
hid_t objectId;
HIDCloser groupCloser(-1, H5Gclose);
if (path.empty() || path == "/") {
objectId = fileId();
} else {
objectId = H5Gopen(fileId(), path.c_str(), H5P_DEFAULT);
groupCloser.reset(objectId);
}
ListAllDataSetsVisitor visitor;
herr_t code = H5Ovisit(objectId, H5_INDEX_NAME, H5_ITER_INC,
&visitor.operation, &visitor);
if (code < 0)
return vector<string>();
return visitor.dataSets;
}
DataType getH5ToDataType(hid_t h5type)
{
// Find the type
auto it = std::find_if(H5ToDataType.cbegin(), H5ToDataType.cend(),
[h5type](const std::pair<hid_t, DataType>& t) {
return H5Tequal(t.first, h5type);
});
if (it == H5ToDataType.end()) {
cerr << "H5ToDataType map does not contain H5 type: " << h5type << endl;
return DataType::None;
}
return it->second;
}
// Turn off error messages and save the handlers and data
// in member variables
void turnOffErrors()
{
if (m_errorHandlingIsOff)
return;
H5Eget_auto2(H5E_DEFAULT, &m_errorHandler, &m_clientErrorData);
H5Eset_auto2(H5E_DEFAULT, nullptr, nullptr);
m_errorHandlingIsOff = true;
}
// Turn back on error messages using the saved handlers
// and data
void turnOnErrors()
{
if (!m_errorHandlingIsOff)
return;
H5Eset_auto2(H5E_DEFAULT, m_errorHandler, m_clientErrorData);
m_errorHandler = nullptr;
m_clientErrorData = nullptr;
m_errorHandlingIsOff = false;
}
bool fileIsValid() { return m_fileId >= 0; }
void clear()
{
if (fileIsValid()) {
H5Fclose(m_fileId);
m_fileId = H5I_INVALID_HID;
}
}
hid_t fileId() const { return m_fileId; }
hid_t m_fileId = H5I_INVALID_HID;
// The error handlers are saved when error handling is turned off,
// and they will be restored when error handling is turned back on.
bool m_errorHandlingIsOff = false;
H5E_auto_t m_errorHandler;
void* m_clientErrorData;
};
H5ReadWrite::H5ReadWrite(const string& file, OpenMode mode)
: m_impl(new H5ReadWriteImpl(file, mode))
{}
H5ReadWrite::~H5ReadWrite() = default;
void H5ReadWrite::close()
{
m_impl->clear();
}
vector<string> H5ReadWrite::children(const string& path, bool* ok)
{
setOk(ok, false);
vector<string> result;
if (!m_impl->fileIsValid())
return result;
constexpr int maxNameSize = 2048;
char groupName[maxNameSize];
hid_t groupId = H5Gopen(m_impl->fileId(), path.c_str(), H5P_DEFAULT);
if (groupId < 0) {
cerr << "Failed to open group: " << path << "\n";
return result;
}
// For automatic closing upon leaving scope
HIDCloser groupCloser(groupId, H5Gclose);
hsize_t objCount = 0;
H5Gget_num_objs(groupId, &objCount);
for (hsize_t i = 0; i < objCount; ++i) {
H5Gget_objname_by_idx(groupId, i, groupName, maxNameSize);
result.push_back(groupName);
}
setOk(ok, true);
return result;
}
template <typename T>
T H5ReadWrite::attribute(const string& path, const string& name, bool* ok)
{
setOk(ok, false);
T result;
const hid_t dataTypeId = BasicTypeToH5<T>::dataTypeId();
const hid_t memTypeId = BasicTypeToH5<T>::memTypeId();
if (m_impl->attribute(path, name, &result, dataTypeId, memTypeId))
setOk(ok, true);
return result;
}
// We have a specialization for std::string
template <>
string H5ReadWrite::attribute<string>(const string& path, const string& name,
bool* ok)
{
setOk(ok, false);
string result;
if (!m_impl->attributeExists(path, name)) {
cerr << "Attribute " << path << name << " not found!" << endl;
return result;
}
hid_t fileId = m_impl->fileId();
hid_t attr = H5Aopen_by_name(fileId, path.c_str(), name.c_str(), H5P_DEFAULT,
H5P_DEFAULT);
hid_t type = H5Aget_type(attr);
// For automatic closing upon leaving scope
HIDCloser attrCloser(attr, H5Aclose);
HIDCloser typeCloser(type, H5Tclose);
if (H5T_STRING != H5Tget_class(type)) {
cerr << path << name << " is not a string" << endl;
return result;
}
char* tmpString;
int is_var_str = H5Tis_variable_str(type);
if (is_var_str > 0) { // if it is a variable-length string
if (H5Aread(attr, type, &tmpString) < 0) {
cerr << "Failed to read attribute " << path << " " << name << endl;
return result;
}
result = tmpString;
free(tmpString);
} else if (is_var_str == 0) { // If it is not a variable-length string
// it must be fixed length since the "is a string" check earlier passed.
size_t size = H5Tget_size(type);
if (size == 0) {
cerr << "Unknown error occurred" << endl;
return result;
}
tmpString = new char[size + 1];
if (H5Aread(attr, type, tmpString) < 0) {
cerr << "Failed to read attribute " << path << " " << name << endl;
delete[] tmpString;
return result;
}
tmpString[size] = '\0'; // set null byte, hdf5 doesn't do this for you
result = tmpString;
delete[] tmpString;
} else {
cerr << "Unknown error occurred" << endl;
return result;
}
setOk(ok, true);
return result;
}
bool H5ReadWrite::hasAttribute(const string& path)
{
return m_impl->hasAttribute(path);
}
bool H5ReadWrite::hasAttribute(const string& path, const string& name)
{
return m_impl->attributeExists(path, name);
}
DataType H5ReadWrite::attributeType(const string& path, const string& name)
{
if (!m_impl->attributeExists(path, name)) {
cerr << "Attribute " << path << name << " not found!" << endl;
return DataType::None;
}
hid_t fileId = m_impl->fileId();
hid_t attr = H5Aopen_by_name(fileId, path.c_str(), name.c_str(), H5P_DEFAULT,
H5P_DEFAULT);
hid_t h5type = H5Aget_type(attr);
// For automatic closing upon leaving scope
HIDCloser attrCloser(attr, H5Aclose);
HIDCloser typeCloser(h5type, H5Tclose);
// Special case for strings
if (H5T_STRING == H5Tget_class(h5type))
return DataType::String;
return m_impl->getH5ToDataType(h5type);
}
bool H5ReadWrite::isDataSet(const string& path)
{
return m_impl->isDataSet(path);
}
bool H5ReadWrite::isGroup(const string& path)
{
return m_impl->isGroup(path);
}
vector<string> H5ReadWrite::allDataSets(const string& path)
{
return m_impl->allDataSets(path);
}
DataType H5ReadWrite::dataType(const string& path)
{
if (!m_impl->isDataSet(path)) {
cerr << path << " is not a data set.\n";
return DataType::None;
}
hid_t dataSetId = H5Dopen(m_impl->fileId(), path.c_str(), H5P_DEFAULT);
if (dataSetId < 0) {
cerr << "Failed to get data set id\n";
return DataType::None;
}
hid_t dataTypeId = H5Dget_type(dataSetId);
// Automatically close
HIDCloser dataSetCloser(dataSetId, H5Dclose);
HIDCloser dataTypeCloser(dataTypeId, H5Tclose);
return m_impl->getH5ToDataType(dataTypeId);
}
vector<int> H5ReadWrite::getDimensions(const string& path)
{
return m_impl->getDimensions(path);
}
int H5ReadWrite::dimensionCount(const string& path)
{
vector<int> dims = getDimensions(path);
if (dims.empty()) {
cerr << "Failed to get the dimensions\n";
return -1;
}
return dims.size();
}
template <typename T>
vector<T> H5ReadWrite::readData(const string& path)
{
vector<int> dims;
vector<T> result = readData<T>(path, dims);
if (result.empty()) {
cerr << "Failed to read the data\n";
return result;
}
// Make sure there is one dimension
if (dims.size() != 1) {
cerr << "Warning: single-dimensional readData() called, but "
<< "multi-dimensional data was obtained.\n";
cerr << "Number of dims is: " << dims.size() << "\n";
return vector<T>();
}
return result;
}
template <typename T>
vector<T> H5ReadWrite::readData(const string& path, vector<int>& dims)
{
vector<T> result;
dims = getDimensions(path);
if (dims.empty()) {
cerr << "Failed to get the dimensions\n";
return result;
}
// Multiply all the dimensions together
auto size =
std::accumulate(dims.cbegin(), dims.cend(), 1, std::multiplies<int>());
result.resize(size);
if (!readData(path, result.data())) {
cerr << "Failed to read the data\n";
return vector<T>();
}
return result;
}
template <typename T>
bool H5ReadWrite::readData(const string& path, T* data)
{
const hid_t dataTypeId = BasicTypeToH5<T>::dataTypeId();
const hid_t memTypeId = BasicTypeToH5<T>::memTypeId();
if (!m_impl->readData(path, dataTypeId, memTypeId, data)) {
cerr << "Failed to read the data\n";
return false;
}
return true;
}
bool H5ReadWrite::readData(const string& path, const DataType& type, void* data,
int* strides, size_t* start, size_t* counts)
{
auto it = DataTypeToH5DataType.find(type);
if (it == DataTypeToH5DataType.end()) {
cerr << "Failed to get H5 data type for " << dataTypeToString(type) << "\n";
return false;
}
hid_t dataTypeId = it->second;
auto memIt = DataTypeToH5MemType.find(type);
if (memIt == DataTypeToH5MemType.end()) {
cerr << "Failed to get H5 mem type for " << dataTypeToString(type) << "\n";
return false;
}
hid_t memTypeId = memIt->second;
if (!m_impl->readData(path, dataTypeId, memTypeId, data, strides, start,
counts)) {
cerr << "Failed to read the data\n";
return false;
}
return true;
}
template <typename T>
bool H5ReadWrite::writeData(const string& path, const string& name,
const vector<int>& dims, const T* data)
{
const hid_t dataTypeId = BasicTypeToH5<T>::dataTypeId();
const hid_t memTypeId = BasicTypeToH5<T>::memTypeId();
return m_impl->writeData(path, name, dims, data, dataTypeId, memTypeId);
}
bool H5ReadWrite::writeData(const string& path, const string& name,
const vector<int>& dims, const DataType& type,
const void* data)
{
auto it = DataTypeToH5DataType.find(type);
if (it == DataTypeToH5DataType.end()) {
cerr << "Failed to get H5 data type for " << dataTypeToString(type) << "\n";
return false;
}
hid_t dataTypeId = it->second;
auto memIt = DataTypeToH5MemType.find(type);
if (memIt == DataTypeToH5MemType.end()) {
cerr << "Failed to get H5 mem type for " << dataTypeToString(type) << "\n";
return false;
}
hid_t memTypeId = memIt->second;
return m_impl->writeData(path, name, dims, data, dataTypeId, memTypeId);
}
bool H5ReadWrite::createDataSet(const std::string& path,
const std::string& name,
const std::vector<int>& dims,
const DataType& type,
const std::vector<int>& chunkDims)
{
auto it = DataTypeToH5DataType.find(type);
if (it == DataTypeToH5DataType.end()) {
cerr << "Failed to get H5 data type for " << dataTypeToString(type) << "\n";
return false;
}
hid_t dataTypeId = it->second;
return m_impl->createDataSet(path, name, dims, dataTypeId, chunkDims);
}
bool H5ReadWrite::updateData(const std::string& path,
const std::vector<int>& dimensions,
const DataType& type, void* data, size_t* start,
size_t* counts)
{
auto memIt = DataTypeToH5MemType.find(type);
if (memIt == DataTypeToH5MemType.end()) {
cerr << "Failed to get H5 mem type for " << dataTypeToString(type) << "\n";
return false;
}
hid_t memTypeId = memIt->second;
return m_impl->updateData(path, dimensions, memTypeId, data, start, counts);
}
template <typename T>
bool H5ReadWrite::setAttribute(const string& path, const string& name, T value)
{
const hid_t dataTypeId = BasicTypeToH5<T>::dataTypeId();
const hid_t memTypeId = BasicTypeToH5<T>::memTypeId();
return m_impl->setAttribute(path, name, &value, dataTypeId, memTypeId, 1);
}
template <typename T>
bool H5ReadWrite::setAttribute(const string& path, const string& name,
const vector<T>& values)
{
const hid_t dataTypeId = BasicTypeToH5<T>::dataTypeId();
const hid_t memTypeId = BasicTypeToH5<T>::memTypeId();
return m_impl->setAttribute(path, name, values.data(), dataTypeId, memTypeId,
values.size());
}
// Specialization for c string
template <>
bool H5ReadWrite::setAttribute<const char*>(const string& path,
const string& name,
const char* value)
{
if (!m_impl->fileIsValid()) {
cerr << "File is not valid\n";
return false;
}
hid_t fileId = m_impl->fileId();
bool onData = m_impl->isDataSet(path);
hid_t parentId;
herr_t (*closer)(hid_t) = nullptr;
if (onData) {
parentId = H5Dopen(fileId, path.c_str(), H5P_DEFAULT);
closer = H5Dclose;
} else {
parentId = H5Gopen(fileId, path.c_str(), H5P_DEFAULT);
closer = H5Gclose;
}
HIDCloser parentCloser(parentId, closer);
hsize_t dims = 1;
hid_t dataSpaceId = H5Screate_simple(1, &dims, nullptr);
HIDCloser dataSpaceCloser(dataSpaceId, H5Sclose);
hid_t dataType = H5Tcopy(H5T_C_S1);
herr_t status = H5Tset_size(dataType, H5T_VARIABLE);
if (status < 0) {
cerr << "Failed to set the size\n";
return false;
}
hid_t attributeId = H5Acreate2(parentId, name.c_str(), dataType, dataSpaceId,
H5P_DEFAULT, H5P_DEFAULT);
if (attributeId < 0) {
cerr << "Failed to create attribute\n";
return false;
}
HIDCloser attributeCloser(attributeId, H5Aclose);
// Need a char**
return H5Awrite(attributeId, dataType, &value) >= 0;
}
// Specialization for string
template <>
bool H5ReadWrite::setAttribute<const string&>(const string& path,
const string& name,
const string& value)
{
return setAttribute(path, name, value.c_str());
}
bool H5ReadWrite::createGroup(const string& path)
{
if (!m_impl->fileIsValid()) {
cerr << "File is not valid\n";
return false;
}