h5gt 0.2.0
C++ wrapper for HDF5 library (based on HighFive project)
Loading...
Searching...
No Matches
H5PropertyList_misc.hpp
1/*
2 * Copyright (c), 2017-2018, Adrien Devresse <adrien.devresse@epfl.ch>
3 * Juan Hernando <juan.hernando@epfl.ch>
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 */
9#ifndef H5PROPERTY_LIST_MISC_HPP
10#define H5PROPERTY_LIST_MISC_HPP
11
12#include <H5Ppublic.h>
13
14namespace h5gt {
15
16namespace {
17inline hid_t convert_plist_type(PropertyType propertyType) {
18 // The HP5_XXX are macros with function calls so we can't assign
19 // them as the enum values
20 switch (propertyType) {
21 case PropertyType::OBJECT_CREATE:
22 return H5P_OBJECT_CREATE;
23 case PropertyType::FILE_CREATE:
24 return H5P_FILE_CREATE;
25 case PropertyType::FILE_ACCESS:
26 return H5P_FILE_ACCESS;
27 case PropertyType::DATASET_CREATE:
28 return H5P_DATASET_CREATE;
29 case PropertyType::DATASET_ACCESS:
30 return H5P_DATASET_ACCESS;
31 case PropertyType::DATASET_XFER:
32 return H5P_DATASET_XFER;
33 case PropertyType::GROUP_CREATE:
34 return H5P_GROUP_CREATE;
35 case PropertyType::GROUP_ACCESS:
36 return H5P_GROUP_ACCESS;
37 case PropertyType::DATATYPE_CREATE:
38 return H5P_DATATYPE_CREATE;
39 case PropertyType::DATATYPE_ACCESS:
40 return H5P_DATATYPE_ACCESS;
41 case PropertyType::STRING_CREATE:
42 return H5P_STRING_CREATE;
43 case PropertyType::ATTRIBUTE_CREATE:
44 return H5P_ATTRIBUTE_CREATE;
45 case PropertyType::OBJECT_COPY:
46 return H5P_OBJECT_COPY;
47 case PropertyType::LINK_CREATE:
48 return H5P_LINK_CREATE;
49 case PropertyType::LINK_ACCESS:
50 return H5P_LINK_ACCESS;
51 default:
52 HDF5ErrMapper::ToException<PropertyException>(
53 "Unsupported property list type");
54 }
55}
56
57} // namespace
58
59template <PropertyType T>
60inline void PropertyList<T>::initializeId() {
61 if ((_hid = H5Pcreate(convert_plist_type(T))) < 0) {
62 HDF5ErrMapper::ToException<PropertyException>(
63 "Unable to create property list");
64 }
65}
66
67template <PropertyType T>
79inline void PropertyList<T>::setExternalLinkPrefix(const std::string& prefix) {
80 if (H5Pset_elink_prefix(_hid, prefix.c_str()) < 0){
81 HDF5ErrMapper::ToException<PropertyException>(
82 "Unable to set external link prefix property");
83 }
84}
85
86inline void LinkCreateProps::setCreateIntermediateGroup(unsigned val) {
87 if (H5Pset_create_intermediate_group(_hid, val) < 0){
88 HDF5ErrMapper::ToException<PropertyException>(
89 "Unable to set create intermediate group property");
90 }
91}
92
93inline void DataSetAccessProps::setChunkCache(
94 const size_t& numSlots, const size_t& cacheSize, const double& w0)
95{
96 if (H5Pset_chunk_cache(_hid, numSlots, cacheSize, w0) < 0){
97 HDF5ErrMapper::ToException<PropertyException>(
98 "Unable to set chunk cache property");
99 }
100}
101
102inline void DataSetAccessProps::getChunkCache(
103 size_t& numSlots, size_t& cacheSize, double& w0)
104{
105 if (H5Pget_chunk_cache(_hid, &numSlots, &cacheSize, &w0) < 0){
106 HDF5ErrMapper::ToException<PropertyException>(
107 "Unable to get chunk cache property");
108 }
109}
110
111inline void DataSetCreateProps::addExternalFile(const std::string& file, off_t offset, hsize_t size)
112{
113 if (size == 0){
114 try {
115 size = std::filesystem::file_size(file);
116 size = size - offset;
117 } catch(std::filesystem::filesystem_error& e) {
118 std::cout << e.what() << std::endl;
119 }
121
122 if (H5Pset_external(_hid, file.c_str(), offset, size) < 0){
123 HDF5ErrMapper::ToException<PropertyException>(
124 "Unable to add external file");
125 }
126}
127
129 const DataSpace& vSpace,
130 const DataSet& srcDset,
131 const DataSpace& srcSpace)
132{
133 if (H5Pset_virtual( _hid, vSpace.getId(), srcDset.getFileName().c_str(),
134 srcDset.getPath().c_str(), srcSpace.getId()) < 0){
135 HDF5ErrMapper::ToException<PropertyException>(
136 "Unable to add set dataset virtual");
137 }
138}
139
140inline void DataSetCreateProps::setShuffle() {
141 if (!H5Zfilter_avail(H5Z_FILTER_SHUFFLE)){
142 HDF5ErrMapper::ToException<PropertyException>(
143 "Z-FILTER is unavailable");
144 }
145
146 if (H5Pset_shuffle(_hid) < 0){
147 HDF5ErrMapper::ToException<PropertyException>(
148 "Unable to set shuffle property");
149 }
150}
151
152inline void DataSetCreateProps::setDeflate(const unsigned& level) {
153 if (!H5Zfilter_avail(H5Z_FILTER_SHUFFLE)){
154 HDF5ErrMapper::ToException<PropertyException>(
155 "Z-FILTER is unavailable");
156 }
157
158 if (H5Pset_deflate(_hid, level) < 0){
159 HDF5ErrMapper::ToException<PropertyException>(
160 "Unable to set deflate property");
161 }
162}
163
164inline void DataSetCreateProps::setChunk(const std::vector<hsize_t>& dims) {
165 if (H5Pset_chunk(_hid, static_cast<int>(dims.size()), dims.data()) < 0){
166 HDF5ErrMapper::ToException<PropertyException>(
167 "Unable to set chunk property");
168 }
169}
170
172 int num = H5Pget_external_count(_hid);
173 if (num < 0){
174 HDF5ErrMapper::ToException<PropertyException>(
175 "Unable to get number of external files");
176 }
177 return num;
178}
179
181 unsigned idx, off_t& offset, hsize_t& fileSize) {
182 char buffer[H5GT_MAX_PATH_LEN + 1];
183 herr_t status = H5Pget_external(
184 _hid, idx, static_cast<hsize_t>(H5GT_MAX_PATH_LEN) + 1,
185 buffer, &offset, &fileSize);
186 if (status < 0){
187 HDF5ErrMapper::ToException<PropertyException>(
188 "Unable to get info about external file");
189 }
190 return std::string(buffer);
191}
192
194 size_t num;
195 herr_t status = H5Pget_virtual_count(_hid, &num);
196 if (status < 0){
197 HDF5ErrMapper::ToException<PropertyException>(
198 "Unable to get number of virtual datasets");
199 }
200 return num;
201}
202
203inline std::string DataSetCreateProps::getVirtualDataSetName(size_t idx){
204 char buffer[H5GT_MAX_PATH_LEN + 1];
205 ssize_t retcode = H5Pget_virtual_dsetname(
206 _hid, idx, buffer,
207 static_cast<size_t>(H5GT_MAX_PATH_LEN) + 1);
208 if (retcode < 0) {
209 HDF5ErrMapper::ToException<PropertyException>("Error accessing object name");
210 }
211 const size_t length = static_cast<std::size_t>(retcode);
212 if (length <= H5GT_MAX_PATH_LEN) {
213 return std::string(buffer, length);
214 }
215 std::vector<char> bigBuffer(length + 1, 0);
216 H5Pget_virtual_dsetname(
217 _hid, idx, bigBuffer.data(),
218 static_cast<hsize_t>(length) + 1);
219 return std::string(bigBuffer.data(), length);
220}
221
222inline std::string DataSetCreateProps::getVirtualFileName(size_t idx) {
223 char buffer[H5GT_MAX_PATH_LEN + 1];
224 ssize_t retcode = H5Pget_virtual_filename(
225 _hid, idx, buffer,
226 static_cast<size_t>(H5GT_MAX_PATH_LEN) + 1);
227 if (retcode < 0) {
228 HDF5ErrMapper::ToException<PropertyException>("Error accessing object name");
229 }
230 const size_t length = static_cast<std::size_t>(retcode);
231 if (length <= H5GT_MAX_PATH_LEN) {
232 return std::string(buffer, length);
233 }
234 std::vector<char> bigBuffer(length + 1, 0);
235 H5Pget_virtual_filename(
236 _hid, idx, bigBuffer.data(),
237 static_cast<hsize_t>(length) + 1);
238 return std::string(bigBuffer.data(), length);
239}
240
241inline DataSpace DataSetCreateProps::getVirtualSrcSpace(size_t idx){
242 return DataSpace::FromId(H5Pget_virtual_srcspace(_hid, idx));
243}
244
245inline DataSpace DataSetCreateProps::getVirtualVSpace(size_t idx){
246 return DataSpace::FromId(H5Pget_virtual_vspace(_hid, idx));
247}
248
249inline std::vector<hsize_t> DataSetCreateProps::getChunk(int max_ndims)
250{
251 // initialize with zeros
252 hsize_t* dims = (hsize_t *) calloc(max_ndims, sizeof(hsize_t*));
253 if (H5Pget_chunk(_hid, max_ndims, dims) < 0 ){
254 HDF5ErrMapper::ToException<PropertyException>(
255 "Unable to get chunk property");
256 }
257 std::vector<hsize_t> v;
258 v.reserve(max_ndims);
259 for (int i = 0; i < max_ndims; i++){
260 if (dims[i] == 0)
261 break;
262 v.push_back(dims[i]);
263 }
264
265 v.shrink_to_fit();
266 free(dims);
267 return v;
268}
269
270inline LayoutType DataSetCreateProps::getLayoutType()
271{
272 return _convert_layout_type(H5Pget_layout(_hid));
273}
274
275inline bool DataSetCreateProps::isCompact(){
276 if (getLayoutType() == LayoutType::COMPACT)
277 return true;
278 return false;
279}
280
281inline bool DataSetCreateProps::isContiguous(){
282 if (getLayoutType() == LayoutType::CONTIGUOUS)
283 return true;
284 return false;
285}
286
287inline bool DataSetCreateProps::isChunked(){
288 if (getLayoutType() == LayoutType::CHUNKED)
289 return true;
290 return false;
291}
292
293inline bool DataSetCreateProps::isVirtual(){
294 if (getLayoutType() == LayoutType::VIRTUAL)
295 return true;
296 return false;
297}
298
299
300} // namespace h5gt
301
302#endif // H5PROPERTY_LIST_HPP
void addVirtualDataSet(const DataSpace &vSpace, const DataSet &srcDset, const DataSpace &srcSpace)
map source dataset Spaces must have equal number of selected elements Spaces may be given from select...
Definition H5PropertyList_misc.hpp:128
size_t getExternalCount()
return number of external files
Definition H5PropertyList_misc.hpp:171
std::string getExternal(unsigned idx, off_t &offset, hsize_t &fileSize)
retrieve info about external file under index 'idx'
Definition H5PropertyList_misc.hpp:180
std::string getVirtualDataSetName(size_t idx)
resturn virtual (source) dataset name
Definition H5PropertyList_misc.hpp:203
void addExternalFile(const std::string &file, off_t offset=0, hsize_t size=0)
map external binary file to a dataset
Definition H5PropertyList_misc.hpp:111
size_t getVirtualCount()
return number of virtual datasets
Definition H5PropertyList_misc.hpp:193
Class representing a dataset.
Definition H5DataSet.hpp:28
Class representing the space (dimensions) of a dataset.
Definition H5DataSpace.hpp:37
std::string getPath() const
return the path to the current group, dataset, datatype or attribute's holder
Definition H5Object_misc.hpp:185
hid_t getId(const bool &increaseRefCount=false) const noexcept
getId
Definition H5Object_misc.hpp:172
void setExternalLinkPrefix(const std::string &prefix)
setExternalLinkPrefix Let’s say you’ve created an external link in foo.h5 and the external link refer...
Definition H5PropertyList_misc.hpp:79