Files
Jungfraujoch/writer/HDF5DataFile.cpp

114 lines
3.7 KiB
C++

// Copyright (2019-2023) Paul Scherrer Institute
#include <sys/stat.h>
#include "HDF5DataFile.h"
#include "../compression/JFJochCompressor.h"
#include "HDF5DataFilePluginAzInt.h"
#include "HDF5DataFilePluginMX.h"
#include "HDF5DataFilePluginXFEL.h"
#include "HDF5DataFilePluginJUNGFRAU.h"
#include "HDF5DataFilePluginResEstimation.h"
HDF5DataFile::HDF5DataFile(const std::string &in_filename,
const std::vector<float>& in_rad_int_bin_to_q,
size_t in_max_spots) {
xpixel = 0;
ypixel = 0;
max_image_number = 0;
nimages = 0;
filename = in_filename;
plugins.emplace_back(std::make_unique<HDF5DataFilePluginJUNGFRAU>());
plugins.emplace_back(std::make_unique<HDF5DataFilePluginAzInt>(in_rad_int_bin_to_q));
plugins.emplace_back(std::make_unique<HDF5DataFilePluginXFEL>());
plugins.emplace_back(std::make_unique<HDF5DataFilePluginResEstimation>());
plugins.emplace_back(std::make_unique<HDF5DataFilePluginMX>(in_max_spots));
}
HDF5DataFile::~HDF5DataFile() {
if (data_file) {
HDF5Group group_exp(*data_file, "/entry/detector");
group_exp.NXClass("NXcollection");
group_exp.SaveVector("timestamp", timestamp);
group_exp.SaveVector("exptime", exptime);
group_exp.SaveVector("number", number);
for (auto &p: plugins)
p->WriteFinal(*data_file);
data_file.reset();
std::string old_filename = filename + ".tmp";
std::rename(old_filename.c_str(), filename.c_str());
}
}
void HDF5DataFile::CreateFile(const DataMessage& msg) {
HDF5Dcpl dcpl;
HDF5DataType data_type(msg.image.pixel_depth_bytes, msg.image.pixel_is_signed);
xpixel = msg.image.xpixel;
ypixel = msg.image.ypixel;
dcpl.SetCompression(msg.image.algorithm, msg.image.pixel_depth_bytes, JFJochBitShuffleCompressor::DefaultBlockSize);
dcpl.SetChunking( {1, ypixel, xpixel});
if (msg.image.pixel_is_signed) {
if (msg.image.pixel_depth_bytes == 2)
dcpl.SetFillValue16(INT16_MIN);
else
dcpl.SetFillValue32(INT32_MIN);
}
data_file = std::make_unique<HDF5File>(filename + ".tmp");
chmod(filename.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); // default permissions
HDF5Group(*data_file, "/entry").NXClass("NXentry");
HDF5Group(*data_file, "/entry/data").NXClass("NXdata");
HDF5DataSpace data_space({1, ypixel, xpixel}, {H5S_UNLIMITED, ypixel, xpixel});
data_set = std::make_unique<HDF5DataSet>(*data_file, "/entry/data/data", data_type, data_space, dcpl);
for (auto &p: plugins)
p->OpenFile(*data_file, msg);
}
void HDF5DataFile::Write(const DataMessage &msg, uint64_t image_number) {
std::lock_guard<std::mutex> lock(hdf5_mutex);
bool new_file = false;
if (!data_file) {
CreateFile(msg);
new_file = true;
}
if (new_file || (static_cast<int64_t>(image_number) > max_image_number)) {
max_image_number = image_number;
data_set->SetExtent({max_image_number+1, ypixel, xpixel});
timestamp.resize(max_image_number + 1);
exptime.resize(max_image_number + 1);
number.resize(max_image_number + 1);
}
nimages++;
data_set->WriteDirectChunk(msg.image.data, msg.image.size, {image_number, 0, 0});
for (auto &p: plugins)
p->Write(msg);
timestamp[image_number] = msg.timestamp;
exptime[image_number] = msg.exptime;
number[image_number] = msg.number;
}
HDF5DataFileStatistics HDF5DataFile::GetStatistics() const {
HDF5DataFileStatistics ret;
ret.max_image_number = max_image_number;
ret.total_images = nimages;
ret.filename = filename;
return ret;
}
size_t HDF5DataFile::GetNumImages() const {
return nimages;
}