HighFive 2.10.0
HighFive - Header-only C++ HDF5 interface
Loading...
Searching...
No Matches
H5Easy_xtensor.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#include "H5Easy_misc.hpp"
13#include "H5Easy_scalar.hpp"
14
15#ifdef H5_USE_XTENSOR
16
17namespace H5Easy {
18
19namespace detail {
20
21template <typename T>
22struct io_impl<T, typename std::enable_if<xt::is_xexpression<T>::value>::type> {
23 inline static std::vector<size_t> shape(const T& data) {
24 return std::vector<size_t>(data.shape().cbegin(), data.shape().cend());
25 }
26
27 inline static DataSet dump(File& file,
28 const std::string& path,
29 const T& data,
30 const DumpOptions& options) {
31 using value_type = typename std::decay_t<T>::value_type;
32 DataSet dataset = initDataset<value_type>(file, path, shape(data), options);
33 dataset.write_raw(data.data());
34 if (options.flush()) {
35 file.flush();
36 }
37 return dataset;
38 }
39
40 inline static T load(const File& file, const std::string& path) {
41 static_assert(
42 xt::has_data_interface<T>::value,
43 "Cannot load to xt::xfunction or xt::xgenerator, use e.g. xt::xtensor or xt::xarray");
44 DataSet dataset = file.getDataSet(path);
45 std::vector<size_t> dims = dataset.getDimensions();
46 T data = T::from_shape(dims);
47 dataset.read_raw(data.data());
48 return data;
49 }
50
51 inline static Attribute dumpAttribute(File& file,
52 const std::string& path,
53 const std::string& key,
54 const T& data,
55 const DumpOptions& options) {
56 using value_type = typename std::decay_t<T>::value_type;
57 Attribute attribute = initAttribute<value_type>(file, path, key, shape(data), options);
58 attribute.write_raw(data.data());
59 if (options.flush()) {
60 file.flush();
61 }
62 return attribute;
63 }
64
65 inline static T loadAttribute(const File& file,
66 const std::string& path,
67 const std::string& key) {
68 static_assert(
69 xt::has_data_interface<T>::value,
70 "Cannot load to xt::xfunction or xt::xgenerator, use e.g. xt::xtensor or xt::xarray");
71 DataSet dataset = file.getDataSet(path);
72 Attribute attribute = dataset.getAttribute(key);
73 DataSpace dataspace = attribute.getSpace();
74 std::vector<size_t> dims = dataspace.getDimensions();
75 T data = T::from_shape(dims);
76 attribute.read_raw(data.data());
77 return data;
78 }
79};
80
81} // namespace detail
82} // namespace H5Easy
83
84#endif // H5_USE_XTENSOR
Read/dump DataSets or Attribute using a minimalistic syntax. To this end, the functions are templated...
Definition H5Easy.hpp:59
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:99
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:166
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:148
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:138