Skip to content

Commit 43bea69

Browse files
committed
add a simple H5 writer
1 parent ade40ff commit 43bea69

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

cpp/purify/h5reader.h

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class H5Handler {
160160
size_t _datalen, _slicepos, _slicelen, _batchpos;
161161
};
162162

163-
/// @brief Reads an HDF5 file with u,v visibilities, constructs a vis_params objects and returns it.
163+
/// @brief Reads an HDF5 file with u,v visibilities, constructs a vis_params object and returns it.
164164
///
165165
/// @note vis_name: name of input HDF5 file containing [u, v, real(V), imag(V)].
166166
utilities::vis_params read_visibility(const std::string& vis_name, const bool w_term) {
@@ -240,6 +240,50 @@ utilities::vis_params stochread_visibility(H5Handler& file, const size_t N, cons
240240
return uv_vis;
241241
}
242242

243+
244+
/// @brief Write an HDF5 file with u,v visibilities from a vis_params object.
245+
void write_visibility(const utilities::vis_params& uv_vis, const std::string& h5name,
246+
const bool w_term, cons int chunksize = -1) {
247+
// Set up HDF5 file
248+
HighFive::File h5file(h5name, HighFive::File::OpenOrCreate | HighFive::File::Truncate);
249+
// Set up file properies, such as chunking and compression
250+
// Note: the I/O is minimised if compression is disabeld (obvs)
251+
// If using decompressed data is not an option, then the
252+
// I/O performance can be optiomised by chunking the data
253+
// in such a way that each MPI rank only has to decompressi
254+
// its allocated segment (or a subset thereof)
255+
HighFive::DataSetCreateProps props;
256+
if (uv_vis.u.size()) {
257+
if (chunkize > 0) {
258+
props.add(HighFive::Chunking(std::vector<hsize_t>{(size_t)chunksize}));
259+
}
260+
else {
261+
props.add(HighFive::Chunking(std::vector<hsize_t>{uv_vis.u.size()}));
262+
}
263+
props.add(HighFive::Deflate(9)); // maximal compression
264+
}
265+
// Create the H5 datasets
266+
h5file.createDataSet("u", std::vector<t_real>(uv_vis.u.data(), uv_vis.u.data() + uv_vis.u.size()), props);
267+
h5file.createDataSet("v", std::vector<t_real>(uv_vis.v.data(), uv_vis.v.data() + uv_vis.v.size()), props);
268+
if (w_term) {
269+
h5file.createDataSet("w", std::vector<t_real>(uv_vis.w.data(), uv_vis.w.data() + uv_vis.w.size()), props);
270+
}
271+
272+
vector<t_real> redata, imdata, sigma;
273+
redata.reserve(uv_vis.vis.size());
274+
imdata.reserve(uv_vis.vis.size());
275+
sigma.reserve(uv_vis.weights.size());
276+
for (size_t i = 0; i < uv_vis.vis.size(); ++i) {
277+
redata.push_back(uv_vis.vis(i).real());
278+
imdata.push_back(uv_vis.vis(i).imag());
279+
sigma.push_back(1 / uv_vis.weights(i));
280+
}
281+
h5file.createDataSet("re", std::move(redata), props);
282+
h5file.createDataSet("im", std::move(imdata), props);
283+
h5file.createDataSet("sigma", std::move(imdata), props);
284+
}
285+
286+
243287
} // namespace purify::H5
244288

245289
#endif

0 commit comments

Comments
 (0)