Build Packages / Create release (push) Successful in 21s
Build Packages / build:rugnux-tgz (x86_64) (push) Successful in 9m40s
Build Packages / build:rugnux:aarch64 (cross) (push) Successful in 9m49s
Build Packages / build:viewer-tgz:cpu (push) Successful in 11m37s
Build Packages / build:viewer-tgz:cuda (push) Successful in 12m40s
Build Packages / build:windows:nocuda (push) Successful in 17m44s
Build Packages / build:windows:cuda (push) Successful in 20m13s
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 14m41s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 25m59s
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 15m5s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 14m35s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 15m53s
Build Packages / build:rugnux:windows (push) Successful in 11m29s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 18m51s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 18m43s
Build Packages / Generate python client (push) Successful in 51s
Build Packages / build:rpm (rocky8) (push) Successful in 18m51s
Build Packages / Build documentation (push) Successful in 1m21s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 18m38s
Build Packages / build:rpm (ubuntu2404) (push) Successful in 18m24s
Build Packages / build:rpm (rocky9) (push) Successful in 19m19s
Build Packages / Unit tests (push) Successful in 1h37m15s
* Building Jungfraujoch no longer needs zlib or Eigen installed on the machine, and the dependencies the build fetches are pinned and updated to current releases. * rugnux: improvements in indexing, lattice selection and geometry post-refinement, which index crystals that previously returned no lattice and keep the better of the two geometries a run measures. * rugnux: improvements in beam-centre measurement, beam-stop detection and space-group determination. * rugnux: the unit cell reported with a determined space group now obeys that group - a cell whose symmetry was confirmed from the intensities is re-refined under it, and a cell the group cannot describe is reported with a warning rather than as it stands. * rugnux drops the stretches of a rotation sweep whose removal measurably improves the merged intensities and reports what became of every frame, and decides the resolution cut on the crystal's own diffraction rather than on its ice rings. * The rugnux results report is machine-readable - every line that is not `KEY= value` data starts with `#` - and states the build it was written by, its authorship and its terms of use (`REPORT_VERSION= 8`). * `jfjoch_viewer`: improvements in the file manager (CBF frames beside HDF5 datasets, a remembered root), the dataset plots, the inspector and the image statistics, plus a settable font size, a view of the rugnux results report, usable performance over a remote display (`ssh -X`) and a reset of all settings to defaults; the reciprocal-space window is removed. * Broker fixes around DECTRIS collections and dark-mask calibration: re-initialising after a run that never started no longer freezes the broker, a cancelled calibration is abandoned instead of reported as done, and a collection whose start message never arrives ends by itself. Reviewed-on: #79 Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
228 lines
8.4 KiB
C++
228 lines
8.4 KiB
C++
// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include <filesystem>
|
|
#include <iostream>
|
|
|
|
#include "HDF5DataFile.h"
|
|
#include "../compression/JFJochCompressor.h"
|
|
|
|
#include "HDF5DataFilePluginAzInt.h"
|
|
#include "HDF5DataFilePluginMX.h"
|
|
#include "HDF5DataFilePluginXFEL.h"
|
|
#include "HDF5DataFilePluginDetector.h"
|
|
#include "HDF5DataFilePluginROI.h"
|
|
#include "HDF5DataFilePluginPerformance.h"
|
|
#include "HDF5DataFilePluginImageStats.h"
|
|
#include "HDF5DataFilePluginReflection.h"
|
|
#include "../include/spdlog/fmt/fmt.h"
|
|
#include "HDF5NXmx.h"
|
|
#include "../common/time_utc.h"
|
|
|
|
HDF5DataFile::HDF5DataFile(const StartMessage &msg, uint64_t file_number, const std::string &filename) :
|
|
filename(filename),
|
|
file_number(file_number),
|
|
write_images(msg.write_images.value_or(true)),
|
|
declared_bit_depth(msg.bit_depth_image),
|
|
declared_pixel_signed(msg.pixel_signed) {
|
|
if (msg.overwrite.has_value())
|
|
overwrite = msg.overwrite.value();
|
|
|
|
xpixel = 0;
|
|
ypixel = 0;
|
|
max_image_number = 0;
|
|
nimages = 0;
|
|
|
|
if (msg.file_format == FileWriterFormat::NXmxIntegrated) {
|
|
image_low = 0;
|
|
images_per_file = msg.number_of_images;
|
|
} else {
|
|
image_low = file_number * msg.images_per_file;
|
|
images_per_file = msg.images_per_file;
|
|
}
|
|
|
|
timestamp.reserve(images_per_file);
|
|
exptime.reserve(images_per_file);
|
|
number.reserve(images_per_file);
|
|
|
|
uint64_t tmp_suffix = std::chrono::system_clock::now().time_since_epoch().count();
|
|
try {
|
|
if (!msg.arm_date.empty())
|
|
tmp_suffix = parse_UTC_to_ms(msg.arm_date);
|
|
} catch (...) {
|
|
tmp_suffix = std::chrono::system_clock::now().time_since_epoch().count();
|
|
}
|
|
tmp_filename = fmt::format("{}.{:08x}.tmp", filename, tmp_suffix);
|
|
plugins.emplace_back(std::make_unique<HDF5DataFilePluginROI>());
|
|
plugins.emplace_back(std::make_unique<HDF5DataFilePluginDetector>(msg));
|
|
plugins.emplace_back(std::make_unique<HDF5DataFilePluginAzInt>(msg));
|
|
plugins.emplace_back(std::make_unique<HDF5DataFilePluginXFEL>());
|
|
plugins.emplace_back(std::make_unique<HDF5DataFilePluginMX>(msg));
|
|
plugins.emplace_back(std::make_unique<HDF5DataFilePluginImageStats>());
|
|
plugins.emplace_back(std::make_unique<HDF5DataFilePluginReflection>());
|
|
plugins.emplace_back(std::make_unique<HDF5DataFilePluginPerformance>());
|
|
}
|
|
|
|
std::optional<HDF5DataFileStatistics> HDF5DataFile::Close() {
|
|
if (!data_file)
|
|
return {};
|
|
|
|
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);
|
|
|
|
if (data_set) {
|
|
data_set->SetExtent({max_image_number + 1, ypixel, xpixel});
|
|
data_set
|
|
->Attr("image_nr_low", (int32_t) (image_low + 1))
|
|
.Attr("image_nr_high", (int32_t) (image_low + 1 + max_image_number));
|
|
data_set->Close();
|
|
data_set.reset();
|
|
}
|
|
|
|
if (manage_file ) {
|
|
data_file->Close();
|
|
data_file.reset();
|
|
|
|
if (std::filesystem::exists(filename) && !overwrite)
|
|
throw JFJochException(JFJochExceptionCategory::FileWriteError, "File already exists");
|
|
std::error_code ec;
|
|
std::filesystem::rename(tmp_filename, filename, ec);
|
|
if (ec)
|
|
throw JFJochException(JFJochExceptionCategory::FileWriteError,
|
|
"Cannot rename temporary HDF5 file " + tmp_filename +
|
|
" to " + filename + ": " + ec.message());
|
|
} else {
|
|
data_file.reset();
|
|
}
|
|
|
|
closed = true;
|
|
|
|
HDF5DataFileStatistics ret;
|
|
ret.max_image_number = max_image_number;
|
|
ret.total_images = nimages;
|
|
ret.filename = filename;
|
|
ret.file_number = file_number + 1;
|
|
|
|
return ret;
|
|
}
|
|
|
|
HDF5DataFile::~HDF5DataFile() {
|
|
if (data_file) {
|
|
try {
|
|
data_set.reset();
|
|
data_file.reset();
|
|
if (manage_file) {
|
|
std::error_code ec;
|
|
std::filesystem::remove(tmp_filename, ec);
|
|
}
|
|
} catch (const std::exception &e) {
|
|
std::cerr << "HDF5DataFile::~HDF5DataFile: " << e.what() << std::endl;
|
|
} catch (...) {
|
|
std::cerr << "HDF5DataFile::~HDF5DataFile: Unknown error " << std::endl;
|
|
}
|
|
}
|
|
}
|
|
|
|
void HDF5DataFile::CreateFile(const DataMessage& msg, std::shared_ptr<HDF5File> in_data_file) {
|
|
data_file = in_data_file;
|
|
|
|
HDF5Group(*data_file, "/entry").NXClass("NXentry");
|
|
if (write_images) {
|
|
HDF5Dcpl dcpl;
|
|
|
|
// The only point where the two independent statements of the pixel format meet: the images
|
|
// carry their own type (a CBOR tag per image, which is what the data files are written with)
|
|
// while the master is typed from the START message. Nothing else compares them, so a stream
|
|
// whose header contradicts its images produced data files of one type under a master
|
|
// declaring another - and with NXmxVDS, HDF5 then converts silently on every read.
|
|
if (msg.image.GetByteDepth() * 8 != declared_bit_depth
|
|
|| msg.image.IsSigned() != declared_pixel_signed)
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Image is " + std::to_string(msg.image.GetByteDepth() * 8)
|
|
+ "-bit " + (msg.image.IsSigned() ? "signed" : "unsigned")
|
|
+ ", but the start message declares "
|
|
+ std::to_string(declared_bit_depth) + "-bit "
|
|
+ (declared_pixel_signed ? "signed" : "unsigned")
|
|
+ " - the master file would not describe the data it links to");
|
|
|
|
HDF5DataType data_type(msg.image.GetMode());
|
|
|
|
xpixel = msg.image.GetWidth();
|
|
ypixel = msg.image.GetHeight();
|
|
|
|
dcpl.SetCompression(msg.image.GetCompressionAlgorithm(),
|
|
JFJochBitShuffleCompressor::BlockSize(msg.image.GetCompressionAlgorithm(),
|
|
msg.image.GetByteDepth()));
|
|
dcpl.SetChunking( {1, ypixel, xpixel});
|
|
|
|
H5Pset_fill_time(dcpl.GetID(), H5D_FILL_TIME_NEVER);
|
|
H5Pset_alloc_time(dcpl.GetID(), H5D_ALLOC_TIME_INCR);
|
|
|
|
switch (msg.image.GetMode()) {
|
|
case CompressedImageMode::Int8:
|
|
dcpl.SetFillValue8(INT8_MIN);
|
|
break;
|
|
case CompressedImageMode::Int16:
|
|
dcpl.SetFillValue16(INT16_MIN);
|
|
break;
|
|
case CompressedImageMode::Int32:
|
|
dcpl.SetFillValue32(INT32_MIN);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
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);
|
|
data_set->SetExtent({images_per_file, ypixel, xpixel});
|
|
}
|
|
|
|
for (auto &p: plugins)
|
|
p->OpenFile(*data_file, msg, images_per_file);
|
|
}
|
|
|
|
void HDF5DataFile::Write(const DataMessage &msg, uint64_t image_number) {
|
|
if (closed)
|
|
throw JFJochException(JFJochExceptionCategory::FileWriteError,
|
|
"Trying to write to already closed file");
|
|
if (image_number >= images_per_file)
|
|
throw JFJochException(JFJochExceptionCategory::FileWriteError,
|
|
"Image number out of bounds");
|
|
|
|
if (!data_file) {
|
|
manage_file = true;
|
|
CreateFile(msg, std::make_shared<HDF5File>(tmp_filename));
|
|
}
|
|
|
|
if (new_file || (static_cast<int64_t>(image_number) > max_image_number)) {
|
|
max_image_number = image_number;
|
|
timestamp.resize(max_image_number + 1);
|
|
exptime.resize(max_image_number + 1);
|
|
number.resize(max_image_number + 1);
|
|
new_file = false;
|
|
}
|
|
|
|
nimages++;
|
|
if (data_set)
|
|
data_set->WriteDirectChunk(msg.image.GetCompressed(), msg.image.GetCompressedSize(), {image_number, 0, 0});
|
|
|
|
for (auto &p: plugins)
|
|
p->Write(msg, image_number);
|
|
|
|
timestamp[image_number] = msg.timestamp;
|
|
exptime[image_number] = msg.exptime;
|
|
number[image_number] = (msg.original_number) ? msg.original_number.value() : msg.number;
|
|
}
|
|
|
|
size_t HDF5DataFile::GetNumImages() const {
|
|
return nimages;
|
|
}
|