HighFive 2.3.1
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#ifndef H5EASY_BITS_XTENSOR_HPP
10#define H5EASY_BITS_XTENSOR_HPP
11
12#include "../H5Easy.hpp"
13#include "H5Easy_misc.hpp"
14#include "H5Easy_scalar.hpp"
15
16#ifdef H5_USE_XTENSOR
17
18namespace H5Easy {
19
20namespace detail {
21
22template <typename T>
23struct io_impl<T, typename std::enable_if<xt::is_xexpression<T>::value>::type> {
24
25 inline static std::vector<size_t> shape(const T& data) {
26 return std::vector<size_t>(data.shape().cbegin(), data.shape().cend());
27 }
28
29 inline static DataSet dump(File& file,
30 const std::string& path,
31 const T& data,
32 const DumpOptions& options) {
33 using value_type = typename std::decay_t<T>::value_type;
34 DataSet dataset = initDataset<value_type>(file, path, shape(data), options);
35 dataset.write_raw(data.data());
36 if (options.flush()) {
37 file.flush();
38 }
39 return dataset;
40 }
41
42 inline static T load(const File& file, const std::string& path) {
43 static_assert(xt::has_data_interface<T>::value,
44 "Cannot load to xt::xfunction or xt::xgenerator, use e.g. xt::xtensor or xt::xarray");
45 DataSet dataset = file.getDataSet(path);
46 std::vector<size_t> dims = dataset.getDimensions();
47 T data = T::from_shape(dims);
48 dataset.read(data.data());
49 return data;
50 }
51
52 inline static Attribute dumpAttribute(File& file,
53 const std::string& path,
54 const std::string& key,
55 const T& data,
56 const DumpOptions& options) {
57 using value_type = typename std::decay_t<T>::value_type;
58 Attribute attribute = initAttribute<value_type>(file, path, key, shape(data), options);
59 attribute.write_raw(data.data());
60 if (options.flush()) {
61 file.flush();
62 }
63 return attribute;
64 }
65
66 inline static T loadAttribute(const File& file,
67 const std::string& path,
68 const std::string& key) {
69 static_assert(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(data.data());
77 return data;
78 }
79};
80
81} // namespace detail
82} // namespace H5Easy
83
84#endif // H5_USE_XTENSOR
85#endif // H5EASY_BITS_XTENSOR_HPP
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