// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only #include "CrystFELGeometry.h" #include #include #include #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 panel_index; std::map 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