HighFive 2.9.0
HighFive - Header-only C++ HDF5 interface
Loading...
Searching...
No Matches
H5Easy_misc.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
3 *
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#pragma once
10
11#include "../H5Easy.hpp"
12
13namespace H5Easy {
14
15namespace detail {
16
17// Generate error-stream and return "Exception" (not yet thrown).
18inline Exception error(const File& file, const std::string& path, const std::string& message) {
19 std::ostringstream ss;
20 ss << message << std::endl
21 << "Path: " << path << std::endl
22 << "Filename: " << file.getName() << std::endl;
23 return Exception(ss.str());
24}
25
26// Generate specific dump error
27inline Exception dump_error(File& file, const std::string& path) {
28 if (file.getObjectType(path) == ObjectType::Dataset) {
29 return error(file,
30 path,
31 "H5Easy: Dataset already exists, dump with H5Easy::DumpMode::Overwrite "
32 "to overwrite (with an array of the same shape).");
33 } else {
34 return error(
35 file,
36 path,
37 "H5Easy: path exists, but does not correspond to a Dataset. Dump not possible.");
38 }
39}
40
41// get a opened DataSet: nd-array
42template <class T>
43inline DataSet initDataset(File& file,
44 const std::string& path,
45 const std::vector<size_t>& shape,
46 const DumpOptions& options) {
47 if (!file.exist(path)) {
48 if (!options.compress() && !options.isChunked()) {
49 return file.createDataSet<T>(path, DataSpace(shape), {}, {}, true);
50 } else {
51 std::vector<hsize_t> chunks(shape.begin(), shape.end());
52 if (options.isChunked()) {
53 chunks = options.getChunkSize();
54 if (chunks.size() != shape.size()) {
55 throw error(file, path, "H5Easy::dump: Incorrect rank ChunkSize");
56 }
57 }
59 props.add(Chunking(chunks));
60 if (options.compress()) {
61 props.add(Shuffle());
62 props.add(Deflate(options.getCompressionLevel()));
63 }
64 return file.createDataSet<T>(path, DataSpace(shape), props, {}, true);
65 }
66 } else if (options.overwrite() && file.getObjectType(path) == ObjectType::Dataset) {
67 DataSet dataset = file.getDataSet(path);
68 if (dataset.getDimensions() != shape) {
69 throw error(file, path, "H5Easy::dump: Inconsistent dimensions");
70 }
71 return dataset;
72 }
73 throw dump_error(file, path);
74}
75
76// get a opened DataSet: scalar
77template <class T>
78inline DataSet initScalarDataset(File& file,
79 const std::string& path,
80 const T& data,
81 const DumpOptions& options) {
82 if (!file.exist(path)) {
83 return file.createDataSet<T>(path, DataSpace::From(data), {}, {}, true);
84 } else if (options.overwrite() && file.getObjectType(path) == ObjectType::Dataset) {
85 DataSet dataset = file.getDataSet(path);
86 if (dataset.getElementCount() != 1) {
87 throw error(file, path, "H5Easy::dump: Existing field not a scalar");
88 }
89 return dataset;
90 }
91 throw dump_error(file, path);
92}
93
94// get a opened Attribute: nd-array
95template <class T>
96inline Attribute initAttribute(File& file,
97 const std::string& path,
98 const std::string& key,
99 const std::vector<size_t>& shape,
100 const DumpOptions& options) {
101 if (!file.exist(path)) {
102 throw error(file, path, "H5Easy::dumpAttribute: DataSet does not exist");
103 }
104 if (file.getObjectType(path) != ObjectType::Dataset) {
105 throw error(file, path, "H5Easy::dumpAttribute: path not a DataSet");
106 }
107 DataSet dataset = file.getDataSet(path);
108 if (!dataset.hasAttribute(key)) {
109 return dataset.createAttribute<T>(key, DataSpace(shape));
110 } else if (options.overwrite()) {
111 Attribute attribute = dataset.getAttribute(key);
112 DataSpace dataspace = attribute.getSpace();
113 if (dataspace.getDimensions() != shape) {
114 throw error(file, path, "H5Easy::dumpAttribute: Inconsistent dimensions");
115 }
116 return attribute;
117 }
118 throw error(file,
119 path,
120 "H5Easy: Attribute exists, overwrite with H5Easy::DumpMode::Overwrite.");
121}
122
123// get a opened Attribute: scalar
124template <class T>
125inline Attribute initScalarAttribute(File& file,
126 const std::string& path,
127 const std::string& key,
128 const T& data,
129 const DumpOptions& options) {
130 if (!file.exist(path)) {
131 throw error(file, path, "H5Easy::dumpAttribute: DataSet does not exist");
132 }
133 if (file.getObjectType(path) != ObjectType::Dataset) {
134 throw error(file, path, "H5Easy::dumpAttribute: path not a DataSet");
135 }
136 DataSet dataset = file.getDataSet(path);
137 if (!dataset.hasAttribute(key)) {
138 return dataset.createAttribute<T>(key, DataSpace::From(data));
139 } else if (options.overwrite()) {
140 Attribute attribute = dataset.getAttribute(key);
141 DataSpace dataspace = attribute.getSpace();
142 if (dataspace.getElementCount() != 1) {
143 throw error(file, path, "H5Easy::dumpAttribute: Existing field not a scalar");
144 }
145 return attribute;
146 }
147 throw error(file,
148 path,
149 "H5Easy: Attribute exists, overwrite with H5Easy::DumpMode::Overwrite.");
150}
151
152} // namespace detail
153} // namespace H5Easy
static DataSpace From(const T &value)
Automatically deduce the DataSpace from a container/value.
Definition H5Dataspace_misc.hpp:122
PropertyList< PropertyType::DATASET_CREATE > DataSetCreateProps
Definition H5PropertyList.hpp:201
Read/dump DataSets or Attribute using a minimalistic syntax. To this end, the functions are templated...
Definition H5Easy.hpp:59