-
Notifications
You must be signed in to change notification settings - Fork 16
/
hdfgroup.hpp
193 lines (173 loc) · 6.25 KB
/
hdfgroup.hpp
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
#ifndef hdfgroupH
#define hdfgroupH
#include <hdf5/hdf5/traits.hpp>
#include <hdf5/hdfdataset.hpp>
#include <hdf5/hdfattribute.hpp>
#include <hdf5/slab.hpp>
#include <string>
namespace hdf {
template<typename>
class HDFFile;
/**
* Represents a group object in a hdf file
*/
template<class HDFImpl=HDF5Traits>
class HDFGroup {
HDFGroup(std::shared_ptr<typename HDFImpl::group_type> group) : group(group) {
}
protected:
HDFGroup() {};
/**
* Used by HDFFile to act like as a group - it is by default the group "/"
* but a path can be specified to support mounting files as a group
* within another file
*/
void initFileGroup(typename HDFImpl::file_handle_type &file, const std::string & path = std::string("/")) {
group = HDFImpl::openGroup(file, path, false); //pass true to create here for the case where we are mounting a file in a file where the mount point doesn't yet exist - but only if path != "/"
}
#ifdef H5_HAVE_PARALLEL
void initFileGroup(typename HDFImpl::parallel_file_handle_type &file, const std::string & path = std::string("/")) {
group = HDFImpl::openGroup(file, path, false); //pass true to create here for the case where we are mounting a file in a file where the mount point doesn't yet exist - but only if path != "/"
}
#endif
public:
/**
* Open a group from an existing file
*/
HDFGroup(HDFFile<HDFImpl> & file, const std::string &path) {
group = HDFImpl::openGroup(file, path);
}
/**
* Open an existing group
* Create the group if it doesn't exist and create=true
*/
std::unique_ptr<HDFGroup<HDFImpl> >
openGroup(const std::string & path, bool create=false) {
try {
return std::unique_ptr<HDFGroup<HDFImpl> >
(new HDFGroup<HDFImpl>
(HDFImpl::openGroup(*group, path, create)));
} catch(GroupNotFound e) {
return std::unique_ptr<HDFGroup<HDFImpl> >();
}
}
/**
* Create a new group
*/
std::unique_ptr<HDFGroup<HDFImpl> >
createGroup(const std::string & path) {
return std::unique_ptr<HDFGroup<HDFImpl> >
(new HDFGroup<HDFImpl>
(HDFImpl::createGroup(*group, path)));
}
/**
* Create a new external link
*/
std::unique_ptr<HDFGroup<HDFImpl> >
createExternalLink(const std::string & externalFile,
const std::string & externalPath,
const std::string & path) {
return std::unique_ptr<HDFGroup<HDFImpl> >
(new HDFGroup<HDFImpl>
(HDFImpl::createExternalLink(*group,externalFile,
externalPath, path)));
}
/**
* Create an attribute attached to this group
*/
template<typename Type>
std::unique_ptr<HDFAttribute<HDFImpl> >
createAttribute(const std::string &name, std::vector<hsize_t> dims) {
return std::unique_ptr<HDFAttribute<HDFImpl> >
(new HDFAttribute<HDFImpl>
(HDFImpl::template createAttribute<Type>(*group, name, dims, dims)));
}
/**
* Open an existing attribute of this group
*/
std::unique_ptr<HDFAttribute<HDFImpl> >
openAttribute(const std::string &name) {
return std::unique_ptr<HDFAttribute<HDFImpl> >
(new HDFAttribute<HDFImpl>
(HDFImpl::openAttribute(*group, name)));
}
/**
* Open an existing dataset
*/
std::unique_ptr<HDFDataSet<HDFImpl> >
openDataset(const std::string & path) {
return std::unique_ptr<HDFDataSet<HDFImpl> >
(new HDFDataSet<HDFImpl>
(HDFImpl::openDataSet(*group, path)));
}
/**
* Create a new dataset as a child of this group
* dims specifies the dimensions of the dataset in the file
*/
template<typename Type, int order>
std::unique_ptr<HDFDataSet<HDFImpl> >
createDataset(const std::string & path, const Slab<order, HDFImpl> &dims) {
try {
return std::unique_ptr<HDFDataSet<HDFImpl> >
(new HDFDataSet<HDFImpl>
(HDFImpl::template createDataSet<Type>(*group, path, dims)));
} catch (DatasetExists &) {
HDFImpl::deleteDataset(*group, path);
return std::unique_ptr<HDFDataSet<HDFImpl> >
(new HDFDataSet<HDFImpl>
(HDFImpl::template createDataSet<Type>(*group, path, dims)));
}
}
template<typename Type>
std::unique_ptr<HDFAttribute<HDFImpl> >
writeAttribute(const std::string & path, const Type & data) {
std::vector<hsize_t> dims(1,1);
std::unique_ptr<HDFAttribute<HDFImpl> > attr;
try {
attr = createAttribute<Type>(path, dims);
} catch (AttributeExists &) {
attr = openAttribute(path);
}
attr->writeData(data);
return attr;
}
/**
* Writes a 1D dataset stored in a std::vector to a dataset
* at the given path
* Assumes the same layout in the file as in memory
*/
template<typename Type>
std::unique_ptr<HDFDataSet<HDFImpl> >
writeDataset(const std::string & path, const std::vector<Type> & data) {
std::vector<hsize_t> dims(1,data.size());
std::unique_ptr<HDFDataSet<HDFImpl> > dataset;
try {
dataset = createDataset<Type,1>(path, dims);
} catch (DatasetExists &) {
dataset = openDataset(path);
}
dataset->writeData(data);
return dataset;
}
/**
* Writes a dataset stored in data with memory layout specified
* by memslab to a dataset
* at the given path with the fileformat given by fileslab
*/
template<int order, typename Type>
std::unique_ptr<HDFDataSet<HDFImpl> >
writeDataset(const std::string & path, const Type* data, const Slab<order, HDFImpl> &memslab, const Slab<order, HDFImpl> &fileslab) {
std::unique_ptr<HDFDataSet<HDFImpl> > dataset;
try {
dataset = createDataset<Type>(path, fileslab);
} catch (DatasetExists &) {
dataset = openDataset(path);
}
dataset->writeData(data, memslab);
return dataset;
}
protected:
std::shared_ptr<typename HDFImpl::group_type> group;
};
}
#endif