Files
Jungfraujoch/reader/MiniCBF.h
T
leonarski_fandClaude Opus 5 367eba55b1 reader: take a miniCBF rotation axis from the goniometer the header states
Every miniCBF sweep was handed the same hardcoded axis regardless of what its header said, and
Chi/Kappa/Phi/Omega were not parsed at all - there were no members for them. A sweep collected on a
tilted chi cradle therefore ran with an axis that is 54.7 degrees wrong.

The header angles and their increments are now read, the scanned axis is identified from the
non-zero increment (the name is consulted only when no increment is stated, which is what absorbs the
five different spellings the corpus contains, including one file that states no axis name at all),
and a phi scan composes the head chain. An omega scan returns the base axis untouched, because a
fixed chi cannot tilt the axis it hangs from.

-9999 is a sentinel meaning "not set", not an angle. It is treated as absent, so it can never reach
the geometry.

The direction and sense are not invented: these files append an imgCIF _axis loop stating their own
vectors, and SOURCE with GRAVITY fix the imgCIF-to-internal transform, which independently reproduces
the transform this repository already documents for NXmx. Under it the file's own stated phi axis is
exactly the composed one, to four decimals.

Driving the real reader over all 39 corpus sweeps, 37 return the previous axis bit-identically -
including every sweep carrying a large fixed chi, every sentinel header and every axis-name spelling.
Only the two genuine phi scans move, and an unrelated rotation dataset is unchanged end to end.

This is necessary but not sufficient for the one dataset that motivates it: with the axis corrected
it still does not index, because that detector is also mounted rotated 90 degrees in its own plane,
which the reader does not yet read. Compensating both takes its phi sweep from no indexed validation
frames to 90.89% indexed and a complete merge, which is what shows this half is load-bearing. The
detector mount and the two-theta swing belong to the detector-frame work.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Lc5JG6kJqZoCWaoZ43JGTW
2026-08-29 19:58:50 +02:00

79 lines
3.6 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <cstdint>
#include <optional>
#include <string>
#include <vector>
// PILATUS miniCBF: an ASCII header, four separator bytes, then one byte-offset compressed image.
// No CIF parser and no libcbf are needed - every value is on a "# " comment line or a MIME line.
namespace minicbf {
// The four bytes that end the MIME header and begin the binary section.
inline constexpr unsigned char BINARY_SEPARATOR[4] = {0x0c, 0x1a, 0x04, 0xd5};
struct Header {
std::string detector; // "PILATUS3 6M, S/N 60-0136"
int64_t nx = 0; // fast dimension (columns)
int64_t ny = 0; // slow dimension (rows)
int64_t nelem = 0; // pixel count declared by the MIME header
double pixel_x_m = 0;
double pixel_y_m = 0;
double thickness_m = 0;
std::string material = "Si"; // NORMALISED: the file says "Silicon", the rest of the code wants "Si"
double distance_m = 0;
double beam_x_px = 0;
double beam_y_px = 0;
double wavelength_A = 0;
double start_angle_deg = 0;
double angle_increment_deg = 0;
double two_theta_deg = 0;
// Goniometer head angles and the per-image increment of each, in degrees. A writer spells "this
// head has no such axis" as -9999, which is a sentinel and not an angle, so an axis the header
// does not really carry is absent here rather than 9999 degrees away from zero.
std::optional<double> chi_deg;
std::optional<double> omega_deg;
double chi_increment_deg = 0;
double phi_increment_deg = 0;
double omega_increment_deg = 0;
double exposure_s = 0;
double period_s = 0;
int64_t count_cutoff = 0; // saturation
std::string axis_name = "omega";
bool byte_offset = false; // the only conversion supported
};
// Whether the sweep turns PHI rather than the base (omega) axis. What moved is the axis with a
// non-zero increment - a header routinely carries a "# Phi" angle for a phi that stands still - so
// the name is consulted only when no increment is stated at all. It is a poor discriminator: some
// writers put a direction convention there ("X.CW") instead of an axis name.
bool ScansPhi(const Header &h);
// Byte offset of the binary section (just past the separator), or nothing if there is none.
std::optional<size_t> FindBinarySection(const uint8_t *data, size_t size);
// Parse the ASCII header. Pass the bytes BEFORE the separator; headers are not a fixed size (one
// Diamond I24 set carries 6335 bytes, well past a 4 kB guess), so never parse a fixed prefix.
Header ParseHeader(const char *data, size_t size);
// x-CBF_BYTE_OFFSET -> int32. Deltas against a running value, smallest container first: int8,
// escaping to int16 via -128, to int32 via -32768, to int64 via INT32_MIN. Little-endian, packed.
// Throws if the stream ends before n_pixels are produced. out must hold n_pixels.
void DecodeByteOffset(const uint8_t *data, size_t size, int32_t *out, size_t n_pixels);
// Header + pixels of one file, read from disk.
Header Read(const std::string &path, std::vector<int32_t> &out);
// The same, decoding into memory the caller already has (one read of the file, no extra copy).
// Throws if the image does not fit in capacity pixels.
Header ReadInto(const std::string &path, int32_t *out, size_t capacity);
// Header only - reads just enough of the file to reach the separator. Cheap enough to call per
// frame for the rotation angles.
Header ReadHeader(const std::string &path);
} // namespace minicbf