works for hdf5, needs refactoring

This commit is contained in:
2024-12-04 00:52:36 +01:00
parent 4233509615
commit b23e697e26
2 changed files with 190 additions and 62 deletions

View File

@@ -8,6 +8,66 @@
namespace aare {
struct H5Handles {
std::string file_name{};
std::string dataset_name{};
H5::H5File file;
H5::DataSet dataset;
H5::DataSpace dataspace;
H5::DataType datatype;
std::unique_ptr<H5::DataSpace> memspace{nullptr};
std::vector<hsize_t>dims;
std::vector<hsize_t> count;
std::vector<hsize_t> offset;
H5Handles(const std::string& fname, const std::string& dname, int rank):
file_name(fname),
dataset_name(dname),
file(fname, H5F_ACC_RDONLY),
dataset(file.openDataSet(dname)),
dataspace(dataset.getSpace()),
datatype(dataset.getDataType())
{
if (dataspace.getSimpleExtentNdims() != rank) {
throw std::runtime_error(LOCATION + "Expected rank of " + dname + " dataset to be 1. Got " + std::to_string(rank));
}
dims.resize(rank);
dataspace.getSimpleExtentDims(dims.data(), nullptr);
// to slice 1 frame with hyperslab
count.push_back(1);
offset.push_back(0);
// header datasets
if (rank == 1) {
memspace = std::make_unique<H5::DataSpace>(H5S_SCALAR);
}
// data dataset
else {
hsize_t dimsm[2] = {dims[1], dims[2]};
memspace = std::make_unique<H5::DataSpace>(2, dimsm);
count.push_back(dims[1]);
count.push_back(dims[2]);
offset.push_back(0);
offset.push_back(0);
}
};
void seek(size_t frame_index) {
if (frame_index >= dims[0]) {
throw std::runtime_error(LOCATION + "Invalid frame number");
}
offset[0] = static_cast<hsize_t>(frame_index);
};
void get_frame_into(size_t frame_index, std::byte *frame_buffer) {
seek(frame_index);
dataspace.selectHyperslab(H5S_SELECT_SET, count.data(), offset.data());
dataset.read(frame_buffer, datatype, *memspace, dataspace);
};
};
/**
* @brief Class to read .h5 files. The class will parse the master file
* to find the correct geometry for the frames.
@@ -21,11 +81,6 @@ class Hdf5File : public FileInterface {
size_t m_total_frames{};
size_t m_rows{};
size_t m_cols{};
H5::DataType m_datatype{};
std::unique_ptr<H5::H5File> file{nullptr};
std::unique_ptr<H5::DataSet> dataset{nullptr};
std::unique_ptr<H5::DataSpace> dataspace{nullptr};
public:
/**
@@ -71,10 +126,23 @@ class Hdf5File : public FileInterface {
* @param frame_number frame number to read
* @param image_buf buffer to store the frame
*/
void get_frame_into(size_t frame_index, std::byte *frame_buffer,
DetectorHeader *header = nullptr);
/**
* @brief read the frame at the given frame index into the image buffer
* @param frame_index frame number to read
* @param frame_buffer buffer to store the frame
*/
void get_data_into(size_t frame_index, std::byte *frame_buffer);
/**
* @brief read the header at the given frame index into the header buffer
* @param frame_index frame number to read
* @param header buffer to store the header
*/
void get_header_into(size_t frame_index, DetectorHeader *header);
/**
* @brief get the frame at the given frame index
* @param frame_number frame number to read
@@ -90,7 +158,13 @@ class Hdf5File : public FileInterface {
static DetectorHeader read_header(const std::filesystem::path &fname);
static const std::string metadata_group_name;
void open_file();
static const std::vector<std::string> header_dataset_names;
std::unique_ptr<H5Handles> m_data_file{nullptr};
std::vector<std::unique_ptr<H5Handles>> m_header_files;
void open_data_file();
void open_header_files();
};
} // namespace aare