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());
27inline Exception dump_error(File& file,
const std::string& path) {
28 if (file.getObjectType(path) == ObjectType::Dataset) {
31 "H5Easy: Dataset already exists, dump with H5Easy::DumpMode::Overwrite "
32 "to overwrite (with an array of the same shape).");
37 "H5Easy: path exists, but does not correspond to a Dataset. Dump not possible.");
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);
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");
59 props.add(Chunking(chunks));
60 if (options.compress()) {
62 props.add(Deflate(options.getCompressionLevel()));
64 return file.createDataSet<T>(path,
DataSpace(shape), props, {},
true);
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");
73 throw dump_error(file, path);
78inline DataSet initScalarDataset(File& file,
79 const std::string& path,
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");
91 throw dump_error(file, path);
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");
104 if (file.getObjectType(path) != ObjectType::Dataset) {
105 throw error(file, path,
"H5Easy::dumpAttribute: path not a DataSet");
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");
120 "H5Easy: Attribute exists, overwrite with H5Easy::DumpMode::Overwrite.");
125inline Attribute initScalarAttribute(File& file,
126 const std::string& path,
127 const std::string& key,
129 const DumpOptions& options) {
130 if (!file.exist(path)) {
131 throw error(file, path,
"H5Easy::dumpAttribute: DataSet does not exist");
133 if (file.getObjectType(path) != ObjectType::Dataset) {
134 throw error(file, path,
"H5Easy::dumpAttribute: path not a DataSet");
136 DataSet dataset = file.getDataSet(path);
137 if (!dataset.hasAttribute(key)) {
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");
149 "H5Easy: Attribute exists, overwrite with H5Easy::DumpMode::Overwrite.");
static DataSpace From(const T &value)
Automatically deduce the DataSpace from a container/value.
Definition H5Dataspace_misc.hpp:128
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