29 void readPulseData(
const std::string& name, std::vector<ComplexType>& data)
31 if (!std::filesystem::exists(name))
33 LOG(Level::FATAL,
"File '{}' not found", name);
34 throw std::runtime_error(
"File " + name +
" not found.");
37 LOG(Level::TRACE,
"Opening file '{}'", name);
38 const HighFive::File file(name, HighFive::File::ReadOnly);
41 auto read_dataset = [&file](
const std::string& groupName, std::vector<double>& buffer) ->
size_t
43 const auto group = file.getGroup(
"/" + groupName);
45 const auto dataset = group.getDataSet(
"value");
47 const auto dimensions = dataset.getSpace().getDimensions();
48 const auto size = dimensions[0];
56 LOG(Level::TRACE,
"Reading dataset 'I' from file '{}'", name);
57 std::vector<double> buffer_i;
58 const auto size = read_dataset(
"I", buffer_i);
60 std::vector<double> buffer_q;
61 LOG(Level::TRACE,
"Reading dataset 'Q' from file '{}'", name);
62 if (read_dataset(
"Q", buffer_q) != size)
64 LOG(Level::FATAL,
"Dataset 'Q' is not the same size as dataset 'I' in file '{}'", name);
65 throw std::runtime_error(R
"(Dataset "Q" is not the same size as dataset "I" in file )" + name);
69 for (
size_t i = 0; i < size; ++i)
73 LOG(Level::TRACE,
"Read dataset successfully");
77 const RealType fullscale,
const unsigned count)
79 const unsigned size = data.size();
81 const std::string base_chunk_name =
"chunk_" + std::format(
"{:06}", count);
82 const std::string i_chunk_name = base_chunk_name +
"_I";
83 const std::string q_chunk_name = base_chunk_name +
"_Q";
85 std::vector<RealType> i(size), q(size);
86 std::ranges::transform(data, i.begin(), [](
const ComplexType& c) { return c.real(); });
87 std::ranges::transform(data, q.begin(), [](
const ComplexType& c) { return c.imag(); });
89 auto write_chunk = [&](
const std::string& chunkName,
const std::vector<RealType>& chunkData)
93 HighFive::DataSet dataset =
94 file.createDataSet<
RealType>(chunkName, HighFive::DataSpace::From(chunkData));
95 dataset.write(chunkData);
97 catch (
const HighFive::Exception& err)
99 LOG(Level::FATAL,
"Error while writing data to HDF5 file: {}", err.what());
100 throw std::runtime_error(
"Error while writing data to HDF5 file: " + chunkName +
" - " + err.what());
104 auto set_chunk_attributes = [&](
const std::string& chunkName)
108 HighFive::DataSet dataset = file.getDataSet(chunkName);
109 dataset.createAttribute(
"time", time);
111 dataset.createAttribute(
"fullscale", fullscale);
113 catch (
const HighFive::Exception& err)
115 LOG(Level::FATAL,
"Error while setting attributes on chunk: {}", err.what());
116 throw std::runtime_error(
"Error while setting attributes on chunk: " + chunkName +
" - " + err.what());
120 write_chunk(i_chunk_name, i);
121 write_chunk(q_chunk_name, q);
123 set_chunk_attributes(i_chunk_name);
124 set_chunk_attributes(q_chunk_name);
127 std::vector<std::vector<RealType>>
readPattern(
const std::string& name,
const std::string& datasetName)
131 LOG(Level::TRACE,
"Reading dataset '{}' from file '{}'", datasetName, name);
132 const HighFive::File file(name, HighFive::File::ReadOnly);
134 const auto dataset = file.getDataSet(datasetName);
136 const auto dataspace = dataset.getSpace();
137 const auto dims = dataspace.getDimensions();
139 if (dims.size() != 2)
141 LOG(Level::FATAL,
"Invalid dataset dimensions for '{}' in file '{}'", datasetName, name);
142 throw std::runtime_error(
143 std::format(R
"(Invalid dataset dimensions for "{}" in file "{}")", datasetName, name));
146 LOG(Level::TRACE, "Reading dataset with dimensions {}x{}", dims[0], dims[1]);
148 std::vector data(dims[0], std::vector<RealType>(dims[1]));
151 LOG(Level::TRACE,
"Read dataset successfully");
155 catch (
const HighFive::Exception& err)
157 LOG(Level::FATAL,
"Error handling HDF5 file: {}", err.what());
158 throw std::runtime_error(
"Error handling HDF5 file: " + std::string(err.what()));
void addChunkToFile(HighFive::File &file, const std::vector< ComplexType > &data, const RealType time, const RealType fullscale, const unsigned count)
Adds a chunk of data to an HDF5 file.