HighFive  2.3.1
HighFive - Header-only C++ HDF5 interface
H5Easy_scalar.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 #ifndef H5EASY_BITS_SCALAR_HPP
10 #define H5EASY_BITS_SCALAR_HPP
11 
12 #include "../H5Easy.hpp"
13 #include "H5Easy_misc.hpp"
14 
15 namespace H5Easy {
16 
17 namespace detail {
18 
19 /*
20 Base template for partial specialization: the fallback if specialized templates don't match.
21 Used e.g. for scalars.
22 */
23 template <typename T, typename = void>
24 struct io_impl {
25 
26  inline static DataSet dump(File& file,
27  const std::string& path,
28  const T& data,
29  const DumpOptions& options) {
30  DataSet dataset = initScalarDataset(file, path, data, options);
31  dataset.write(data);
32  if (options.flush()) {
33  file.flush();
34  }
35  return dataset;
36  }
37 
38  inline static T load(const File& file, const std::string& path) {
39  DataSet dataset = file.getDataSet(path);
40  T data;
41  dataset.read(data);
42  return data;
43  }
44 
45  inline static Attribute dumpAttribute(File& file,
46  const std::string& path,
47  const std::string& key,
48  const T& data,
49  const DumpOptions& options) {
50  Attribute attribute = initScalarAttribute(file, path, key, data, options);
51  attribute.write(data);
52  if (options.flush()) {
53  file.flush();
54  }
55  return attribute;
56  }
57 
58  inline static T loadAttribute(const File& file,
59  const std::string& path,
60  const std::string& key) {
61  DataSet dataset = file.getDataSet(path);
62  Attribute attribute = dataset.getAttribute(key);
63  T data;
64  attribute.read(data);
65  return data;
66  }
67 
68  inline static DataSet dump_extend(File& file,
69  const std::string& path,
70  const T& data,
71  const std::vector<size_t>& idx,
72  const DumpOptions& options) {
73  std::vector<size_t> ones(idx.size(), 1);
74 
75  if (file.exist(path)) {
76  DataSet dataset = file.getDataSet(path);
77  std::vector<size_t> dims = dataset.getDimensions();
78  std::vector<size_t> shape = dims;
79  if (dims.size() != idx.size()) {
80  throw detail::error(file, path,
81  "H5Easy::dump: Dimension of the index and the existing field do not match");
82  }
83  for (size_t i = 0; i < dims.size(); ++i) {
84  shape[i] = std::max(dims[i], idx[i] + 1);
85  }
86  if (shape != dims) {
87  dataset.resize(shape);
88  }
89  dataset.select(idx, ones).write(data);
90  if (options.flush()) {
91  file.flush();
92  }
93  return dataset;
94  }
95 
96  std::vector<size_t> shape = idx;
97  const size_t unlim = DataSpace::UNLIMITED;
98  std::vector<size_t> unlim_shape(idx.size(), unlim);
99  std::vector<hsize_t> chunks(idx.size(), 10);
100  if (options.isChunked()) {
101  chunks = options.getChunkSize();
102  if (chunks.size() != idx.size()) {
103  throw error(file, path, "H5Easy::dump: Incorrect dimension ChunkSize");
104  }
105  }
106  for (size_t& i : shape) {
107  i++;
108  }
109  DataSpace dataspace = DataSpace(shape, unlim_shape);
110  DataSetCreateProps props;
111  props.add(Chunking(chunks));
112  DataSet dataset = file.createDataSet(path, dataspace, AtomicType<T>(), props, {}, true);
113  dataset.select(idx, ones).write(data);
114  if (options.flush()) {
115  file.flush();
116  }
117  return dataset;
118  }
119 
120  inline static T load_part(const File& file,
121  const std::string& path,
122  const std::vector<size_t>& idx) {
123  std::vector<size_t> ones(idx.size(), 1);
124  DataSet dataset = file.getDataSet(path);
125  T data;
126  dataset.select(idx, ones).read(data);
127  return data;
128  }
129 };
130 
131 } // namespace detail
132 } // namespace H5Easy
133 
134 #endif // H5EASY_BITS_SCALAR_HPP
static const size_t UNLIMITED
Definition: H5DataSpace.hpp:42
Read/dump DataSets or Attribute using a minimalistic syntax. To this end, the functions are templated...
Definition: H5Easy.hpp:60
DataSet dump(File &file, const std::string &path, const T &data, DumpMode mode=DumpMode::Create)
Write object (templated) to a (new) DataSet in an open HDF5 file.
Definition: H5Easy_public.hpp:115
T loadAttribute(const File &file, const std::string &path, const std::string &key)
Load a Attribute in an open HDF5 file to an object (templated).
Definition: H5Easy_public.hpp:185
Attribute dumpAttribute(File &file, const std::string &path, const std::string &key, const T &data, DumpMode mode=DumpMode::Create)
Write object (templated) to a (new) Attribute in an open HDF5 file.
Definition: H5Easy_public.hpp:167
T load(const File &file, const std::string &path, const std::vector< size_t > &idx)
Load entry {i, j, ...} from a DataSet in an open HDF5 file to a scalar.
Definition: H5Easy_public.hpp:157
PropertyList< PropertyType::DATASET_CREATE > DataSetCreateProps
Definition: H5PropertyList.hpp:93