9 #ifndef H5EASY_BITS_MISC_HPP
10 #define H5EASY_BITS_MISC_HPP
12 #include "../H5Easy.hpp"
19 inline Exception error(
const File& file,
20 const std::string& path,
21 const std::string& message) {
22 std::ostringstream ss;
23 ss << message << std::endl
24 <<
"Path: " << path << std::endl
25 <<
"Filename: " << file.getName() << std::endl;
26 return Exception(ss.str());
30 inline Exception dump_error(File& file,
const std::string& path)
32 if (file.getObjectType(path) == ObjectType::Dataset) {
33 return error(file, path,
34 "H5Easy: Dataset already exists, dump with H5Easy::DumpMode::Overwrite "
35 "to overwrite (with an array of the same shape).");
37 return error(file, path,
38 "H5Easy: path exists, but does not correspond to a Dataset. Dump not possible.");
44 inline DataSet initDataset(File& file,
45 const std::string& path,
46 const std::vector<size_t>& shape,
47 const DumpOptions& options)
49 if (!file.exist(path)) {
50 if (!options.compress() && !options.isChunked()) {
51 return file.createDataSet<T>(path,
DataSpace(shape), {}, {},
true);
53 std::vector<hsize_t> chunks(shape.begin(), shape.end());
54 if (options.isChunked()) {
55 chunks = options.getChunkSize();
56 if (chunks.size() != shape.size()) {
57 throw error(file, path,
"H5Easy::dump: Incorrect rank ChunkSize");
61 props.add(Chunking(chunks));
62 if (options.compress()) {
64 props.add(Deflate(options.getCompressionLevel()));
66 return file.createDataSet<T>(path,
DataSpace(shape), props, {},
true);
68 }
else if (options.overwrite() && file.getObjectType(path) == ObjectType::Dataset) {
69 DataSet dataset = file.getDataSet(path);
70 if (dataset.getDimensions() != shape) {
71 throw error(file, path,
"H5Easy::dump: Inconsistent dimensions");
75 throw dump_error(file, path);
80 inline DataSet initScalarDataset(File& file,
81 const std::string& path,
83 const DumpOptions& options)
85 if (!file.exist(path)) {
86 return file.createDataSet<T>(path,
DataSpace::From(data), {}, {},
true);
87 }
else if (options.overwrite() && file.getObjectType(path) == ObjectType::Dataset) {
88 DataSet dataset = file.getDataSet(path);
89 if (dataset.getElementCount() != 1) {
90 throw error(file, path,
"H5Easy::dump: Existing field not a scalar");
94 throw dump_error(file, path);
99 inline Attribute initAttribute(File& file,
100 const std::string& path,
101 const std::string& key,
102 const std::vector<size_t>& shape,
103 const DumpOptions& options)
105 if (!file.exist(path)) {
106 throw error(file, path,
"H5Easy::dumpAttribute: DataSet does not exist");
108 if (file.getObjectType(path) != ObjectType::Dataset) {
109 throw error(file, path,
"H5Easy::dumpAttribute: path not a DataSet");
111 DataSet dataset = file.getDataSet(path);
112 if (!dataset.hasAttribute(key)) {
113 return dataset.createAttribute<T>(key,
DataSpace(shape));
114 }
else if (options.overwrite()) {
115 Attribute attribute = dataset.getAttribute(key);
116 DataSpace dataspace = attribute.getSpace();
117 if (dataspace.getDimensions() != shape) {
118 throw error(file, path,
"H5Easy::dumpAttribute: Inconsistent dimensions");
122 throw error(file, path,
123 "H5Easy: Attribute exists, overwrite with H5Easy::DumpMode::Overwrite.");
128 inline Attribute initScalarAttribute(File& file,
129 const std::string& path,
130 const std::string& key,
132 const DumpOptions& options)
134 if (!file.exist(path)) {
135 throw error(file, path,
"H5Easy::dumpAttribute: DataSet does not exist");
137 if (file.getObjectType(path) != ObjectType::Dataset) {
138 throw error(file, path,
"H5Easy::dumpAttribute: path not a DataSet");
140 DataSet dataset = file.getDataSet(path);
141 if (!dataset.hasAttribute(key)) {
143 }
else if (options.overwrite()) {
144 Attribute attribute = dataset.getAttribute(key);
145 DataSpace dataspace = attribute.getSpace();
146 if (dataspace.getElementCount() != 1) {
147 throw error(file, path,
"H5Easy::dumpAttribute: Existing field not a scalar");
151 throw error(file, path,
152 "H5Easy: Attribute exists, overwrite with H5Easy::DumpMode::Overwrite.");
static DataSpace From(const T &value)
Create a dataspace matching a type accepted by details::inspector.
Definition: H5Dataspace_misc.hpp:133
Read/dump DataSets or Attribute using a minimalistic syntax. To this end, the functions are templated...
Definition: H5Easy.hpp:60
PropertyList< PropertyType::DATASET_CREATE > DataSetCreateProps
Definition: H5PropertyList.hpp:93