A CrystFEL .geom (current and old syntax) is parsed for panels (pixel ranges, fs/ss, corners), clen/coffset (a number, or an HDF5 path read as mm), res, adu_per_eV/adu_per_photon, max_adu, no_index, bad regions (panel, whole-frame or lab-plane) and the HDF5 mask (mask/mask_file, mask_good/mask_bad). JFJochCrystFELReader reads the frames the geometry's data path names - a 3D stack (Cheetah CXI), one 2D frame per file, or one group per shot (/%/data) - from a file or a directory, puts the panels on one flat image at their rounded positions with gaps and bad regions masked, and converts to photons where a gain is stated (otherwise the pre-scan measures it). One wavelength and one camera length per run: values taken from the files are averaged over the first file. Panel positions are used as given. Frames stored as one deflate(+shuffle) chunk are read raw under the HDF5 lock and decoded outside it. Replaces the MPCCD-specific reader of the previous commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
139 lines
5.5 KiB
C++
139 lines
5.5 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include "CrystFELGeometry.h"
|
|
|
|
#include <fstream>
|
|
#include <map>
|
|
#include <regex>
|
|
|
|
#include "../common/JFJochException.h"
|
|
|
|
namespace crystfel {
|
|
|
|
namespace {
|
|
|
|
std::string Trim(const std::string &s) {
|
|
const auto b = s.find_first_not_of(" \t\r");
|
|
if (b == std::string::npos)
|
|
return "";
|
|
return s.substr(b, s.find_last_not_of(" \t\r") - b + 1);
|
|
}
|
|
|
|
// "+0.000390x -1.000000y", "-x", "0.5x+0.5y": the in-plane direction of a panel axis.
|
|
void ParseDirection(const std::string &txt, double &x, double &y) {
|
|
x = 0;
|
|
y = 0;
|
|
static const std::regex term(R"(([+-]?)\s*([0-9.]*(?:[eE][+-]?[0-9]+)?)\s*([xyz]))");
|
|
for (auto it = std::sregex_iterator(txt.begin(), txt.end(), term); it != std::sregex_iterator(); ++it) {
|
|
double v = (*it)[2].str().empty() ? 1.0 : std::stod((*it)[2].str());
|
|
if ((*it)[1] == "-")
|
|
v = -v;
|
|
if ((*it)[3] == "x")
|
|
x = v;
|
|
else if ((*it)[3] == "y")
|
|
y = v;
|
|
}
|
|
}
|
|
|
|
// Sets one panel property; false if the key is not one.
|
|
bool SetPanel(Panel &p, const std::string &key, const std::string &value) {
|
|
if (key == "min_fs") p.min_fs = std::stoll(value);
|
|
else if (key == "max_fs") p.max_fs = std::stoll(value);
|
|
else if (key == "min_ss") p.min_ss = std::stoll(value);
|
|
else if (key == "max_ss") p.max_ss = std::stoll(value);
|
|
else if (key == "fs") ParseDirection(value, p.fs_x, p.fs_y);
|
|
else if (key == "ss") ParseDirection(value, p.ss_x, p.ss_y);
|
|
else if (key == "corner_x") p.corner_x = std::stod(value);
|
|
else if (key == "corner_y") p.corner_y = std::stod(value);
|
|
else if (key == "res") p.res = std::stod(value);
|
|
else if (key == "clen") p.clen = value;
|
|
else if (key == "coffset") p.coffset = std::stod(value);
|
|
else if (key == "adu_per_eV") p.adu_per_eV = std::stod(value);
|
|
else if (key == "adu_per_photon") p.adu_per_photon = std::stod(value);
|
|
else if (key == "max_adu") p.max_adu = std::stod(value);
|
|
else if (key == "no_index") p.no_index = std::stoi(value) != 0;
|
|
else
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Geometry Read(const std::string &filename) {
|
|
std::ifstream in(filename);
|
|
if (!in)
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Cannot open " + filename);
|
|
|
|
Geometry geom;
|
|
// Global panel keys hold for every panel that appears after them, as in the old syntax; a file in
|
|
// the current syntax states them first, so this reads both the same.
|
|
Panel defaults;
|
|
std::map<std::string, size_t> panel_index;
|
|
std::map<std::string, size_t> bad_index;
|
|
|
|
std::string line;
|
|
while (std::getline(in, line)) {
|
|
line = line.substr(0, line.find(';'));
|
|
const auto eq = line.find('=');
|
|
if (eq == std::string::npos)
|
|
continue;
|
|
const std::string key = Trim(line.substr(0, eq));
|
|
const std::string value = Trim(line.substr(eq + 1));
|
|
const auto slash = key.find('/');
|
|
|
|
if (slash == std::string::npos) {
|
|
if (key == "data") geom.data = value;
|
|
else if (key == "photon_energy") geom.photon_energy = value;
|
|
else if (key == "mask") geom.mask = value;
|
|
else if (key == "mask_file") geom.mask_file = value;
|
|
else if (key == "mask_good") geom.mask_good = std::stoul(value, nullptr, 0);
|
|
else if (key == "mask_bad") geom.mask_bad = std::stoul(value, nullptr, 0);
|
|
else SetPanel(defaults, key, value); // anything else (rigid groups, dim, ...) is not needed
|
|
continue;
|
|
}
|
|
|
|
const std::string name = key.substr(0, slash);
|
|
const std::string prop = key.substr(slash + 1);
|
|
|
|
if (name.starts_with("bad")) {
|
|
if (!bad_index.contains(name)) {
|
|
bad_index[name] = geom.bad.size();
|
|
geom.bad.push_back({});
|
|
}
|
|
auto &b = geom.bad[bad_index[name]];
|
|
if (prop == "panel") b.panel = value;
|
|
else if (prop == "min_x") { b.in_lab = true; b.min_x = std::stod(value); }
|
|
else if (prop == "max_x") { b.in_lab = true; b.max_x = std::stod(value); }
|
|
else if (prop == "min_y") { b.in_lab = true; b.min_y = std::stod(value); }
|
|
else if (prop == "max_y") { b.in_lab = true; b.max_y = std::stod(value); }
|
|
else if (prop == "min_fs") b.min_fs = std::stoll(value);
|
|
else if (prop == "max_fs") b.max_fs = std::stoll(value);
|
|
else if (prop == "min_ss") b.min_ss = std::stoll(value);
|
|
else if (prop == "max_ss") b.max_ss = std::stoll(value);
|
|
continue;
|
|
}
|
|
|
|
if (!panel_index.contains(name)) {
|
|
panel_index[name] = geom.panels.size();
|
|
geom.panels.push_back(defaults);
|
|
geom.panels.back().name = name;
|
|
}
|
|
SetPanel(geom.panels[panel_index[name]], prop, value);
|
|
}
|
|
|
|
if (geom.panels.empty())
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, filename + " defines no panels");
|
|
for (const auto &p: geom.panels) {
|
|
if (!(p.res > 0))
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
filename + ": panel " + p.name + " has no pixel size (res)");
|
|
if (p.clen.empty())
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
filename + ": panel " + p.name + " has no camera length (clen)");
|
|
}
|
|
return geom;
|
|
}
|
|
|
|
} // namespace crystfel
|