9#ifndef H5SLICE_TRAITS_MISC_HPP
10#define H5SLICE_TRAITS_MISC_HPP
21#include <boost/multi_array.hpp>
22#include <boost/numeric/ublas/matrix.hpp>
23#include <boost/serialization/vector.hpp>
29#include "H5ReadWrite_misc.hpp"
30#include "H5Converter_misc.hpp"
39inline const DataSet& get_dataset(
const Selection& sel) {
40 return sel.getDataset();
43inline const DataSet& get_dataset(
const DataSet& ds) {
50inline hid_t get_memspace_id(
const Selection& ptr) {
51 return ptr.getMemSpace().getId(
false);
54inline hid_t get_memspace_id(
const DataSet&) {
63 :
ElementSet(std::vector<std::vector<std::size_t>>(list)) {}
66 : _ids(element_ids) {}
69 for (
const auto& vec : element_ids) {
70 std::copy(vec.begin(), vec.end(), std::back_inserter(_ids));
75template <
typename Derivate>
77 const std::vector<size_t>& count,
78 const std::vector<size_t>& stride,
79 const std::vector<size_t>& block)
const {
82 const auto& slice =
static_cast<const Derivate&
>(*this);
83 std::vector<hsize_t> offset_local(offset.size());
84 std::vector<hsize_t> count_local(count.size());
85 std::vector<hsize_t> stride_local(stride.size());
86 std::vector<hsize_t> block_local(block.size());
87 std::copy(offset.begin(), offset.end(), offset_local.begin());
88 std::copy(count.begin(), count.end(), count_local.begin());
89 std::copy(stride.begin(), stride.end(), stride_local.begin());
90 std::copy(block.begin(), block.end(), block_local.begin());
93 if (H5Sselect_hyperslab(space.
getId(
false), H5S_SELECT_SET, offset_local.data(),
94 stride.empty() ? NULL : stride_local.data(),
96 block.empty() ? NULL : block_local.data()) < 0) {
97 HDF5ErrMapper::ToException<DataSpaceException>(
"Unable to select hyperslap");
104 std::vector<size_t> count_block(block.size());
105 for (
size_t i = 0; i < count_block.size(); i++){
106 count_block[i] = count[i]*block[i];
108 return Selection(DataSpace(count_block), space, details::get_dataset(slice));
111template <
typename Derivate>
113 const auto& slice =
static_cast<const Derivate&
>(*this);
114 const hsize_t* data =
nullptr;
116 const std::size_t length = elements._ids.size();
119 "should be a multiple of the dimensions.");
122 std::vector<hsize_t> raw_elements;
126 if (std::is_same<std::size_t, hsize_t>::value) {
128 data =
reinterpret_cast<const hsize_t*
>(&(elements._ids[0]));
130 raw_elements.resize(length);
131 std::copy(elements._ids.begin(), elements._ids.end(), raw_elements.begin());
132 data = raw_elements.data();
135 if (H5Sselect_elements(space.
getId(
false), H5S_SELECT_SET, num_elements, data) < 0) {
136 HDF5ErrMapper::ToException<DataSpaceException>(
"Unable to select elements");
139 return Selection(DataSpace(num_elements), space, details::get_dataset(slice));
143template <
typename Derivate>
145 const std::vector<size_t>& ind,
size_t offset,
size_t count)
const {
146 const auto& slice =
static_cast<const Derivate&
>(*this);
147 const DataSpace& space = slice.getSpace();
148 const DataSet& dataset = details::get_dataset(slice);
150 dims[0] = ind.size();
151 std::vector<hsize_t> counts(dims.size());
152 std::copy(dims.begin(), dims.end(), counts.begin());
154 std::vector<hsize_t> offsets(dims.size(), 0);
156 if (offset != 0 && offsets.size() > 1){
160 count = dims[1] - offset;
163 if (count != 0 && counts.size() > 1){
168 H5Sselect_none(space.
getId(
false));
170 for (
const auto& i : ind) {
173 if (H5Sselect_hyperslab(space.
getId(
false), H5S_SELECT_OR, offsets.data(), 0,
174 counts.data(), 0) < 0) {
175 HDF5ErrMapper::ToException<DataSpaceException>(
"Unable to select hyperslap");
182template <
typename Derivate>
184 const std::vector<size_t>& ind,
size_t offset,
size_t count)
const {
185 const auto& slice =
static_cast<const Derivate&
>(*this);
186 const DataSpace& space = slice.getSpace();
187 const DataSet& dataset = details::get_dataset(slice);
189 dims[dims.size() - 1] = ind.size();
190 std::vector<hsize_t> counts(dims.size());
191 std::copy(dims.begin(), dims.end(), counts.begin());
192 counts[dims.size() - 1] = 1;
193 std::vector<hsize_t> offsets(dims.size(), 0);
195 if (offset != 0 && offsets.size() > 0){
199 count = dims[0] - offset;
202 if (count != 0 && counts.size() > 0){
208 H5Sselect_none(space.
getId(
false));
210 for (
const auto& i : ind) {
211 offsets[offsets.size() - 1] = i;
213 if (H5Sselect_hyperslab(space.
getId(
false), H5S_SELECT_OR, offsets.data(), 0,
214 counts.data(), 0) < 0) {
215 HDF5ErrMapper::ToException<DataSpaceException>(
"Unable to select hyperslap");
222template <
typename Derivate>
225 const auto& slice =
static_cast<const Derivate&
>(*this);
226 const DataSpace& mem_space = slice.getMemSpace();
229 if (!details::checkDimensions(mem_space, buffer_info.n_dimensions)) {
230 std::ostringstream ss;
231 ss <<
"Impossible to read DataSet of dimensions "
233 << buffer_info.n_dimensions;
237 if (buffer_info.data_type.
getClass() == DataTypeClass::Compound){
240 read(converter.transform_read(array), buffer_info.data_type);
242 converter.process_result(array);
246template <
typename Derivate>
255 static_assert(!std::is_const<T>::value,
256 "read() requires a non-const structure to read data into");
257 const auto& slice =
static_cast<const Derivate&
>(*this);
258 using element_type =
typename details::inspector<T>::base_type;
262 dtype.
empty() ? create_and_check_datatype<element_type>() : dtype;
264 if (H5Dread(details::get_dataset(slice).getId(
false),
265 mem_datatype.
getId(
false),
266 details::get_memspace_id(slice),
267 slice.getSpace().getId(
false), H5P_DEFAULT,
static_cast<void*
>(array)) < 0) {
268 HDF5ErrMapper::ToException<DataSetException>(
"Error during HDF5 Read: ");
273template <
typename Derivate>
276 const auto& slice =
static_cast<const Derivate&
>(*this);
277 const DataSpace& mem_space = slice.getMemSpace();
280 if (!details::checkDimensions(mem_space, buffer_info.n_dimensions)) {
281 std::ostringstream ss;
282 ss <<
"Impossible to write buffer of dimensions " << buffer_info.n_dimensions
287 write_raw(converter.transform_write(buffer), buffer_info.data_type);
291template <
typename Derivate>
294 using element_type =
typename details::inspector<T>::base_type;
295 const auto& slice =
static_cast<const Derivate&
>(*this);
296 const auto& mem_datatype =
297 dtype.
empty() ? create_and_check_datatype<element_type>() : dtype;
299 if (H5Dwrite(details::get_dataset(slice).getId(
false),
300 mem_datatype.getId(
false),
301 details::get_memspace_id(slice),
302 slice.getSpace().getId(
false), H5P_DEFAULT,
303 static_cast<const void*
>(buffer)) < 0) {
304 HDF5ErrMapper::ToException<DataSetException>(
"Error during HDF5 Write: ");
Class representing a dataset.
Definition H5DataSet.hpp:28
Exception specific to h5gt DataSpace interface.
Definition H5Exception.hpp:99
Class representing the space (dimensions) of a dataset.
Definition H5DataSpace.hpp:37
DataSpace clone() const
Definition H5Dataspace_misc.hpp:86
size_t getNumberDimensions() const
getNumberDimensions
Definition H5Dataspace_misc.hpp:94
std::vector< size_t > getDimensions() const
getDimensions
Definition H5Dataspace_misc.hpp:103
HDF5 Data Type.
Definition H5DataType.hpp:48
bool empty() const noexcept
Check the DataType was default constructed. Such value might represent auto-detection of the datatype...
Definition H5DataType_misc.hpp:28
DataTypeClass getClass() const
Return the fundamental type.
Definition H5DataType_misc.hpp:32
Definition H5Slice_traits.hpp:20
ElementSet(std::initializer_list< std::size_t > list)
Create a list of points of N-dimension for selection.
Definition H5Slice_traits_misc.hpp:59
hid_t getId(const bool &increaseRefCount=false) const noexcept
getId
Definition H5Object_misc.hpp:172
Selection: represent a view on a slice/part of a dataset.
Definition H5Selection.hpp:23
Selection select_rows(const std::vector< size_t > &ind, size_t offset=0, size_t count=0) const
Select a set of rows in the first dimension of this dataset. NOTE: Selection is done in memory layout...
Definition H5Slice_traits_misc.hpp:144
void write(const T &buffer)
Definition H5Slice_traits_misc.hpp:275
void write_raw(const T *buffer, const DataType &dtype=DataType())
Definition H5Slice_traits_misc.hpp:293
void read(T &array) const
Definition H5Slice_traits_misc.hpp:224
Selection select_cols(const std::vector< size_t > &ind, size_t offset=0, size_t count=0) const
Select a set of columns in the last dimension of this dataset. NOTE: Selection is done in memory layo...
Definition H5Slice_traits_misc.hpp:183
Selection select(const std::vector< size_t > &offset, const std::vector< size_t > &count, const std::vector< size_t > &stride=std::vector< size_t >(), const std::vector< size_t > &block=std::vector< size_t >()) const
Select a region in the current Slice/Dataset of count points at offset separated by stride....
Definition H5Slice_traits_misc.hpp:76
Definition H5ReadWrite_misc.hpp:18
Definition H5Converter_misc.hpp:124