rugnux reads marCCD sweeps natively
A decade of deposited CCD data is archived as marCCD - what Rayonix MX-series and mar Mosaic detectors write - and rugnux could not open any of it. reader/ had two formats, NXmx/HDF5 and PILATUS miniCBF, and rugnux_cli dispatched on the one CanRead(); this adds the third. The format needs no new dependency: a marCCD file is an ordinary uncompressed TIFF whose 3072-byte instrument header sits in the gap between the TIFF header and the pixels, so libtiff - already fetched for JFJochPreview in every build mode - reads the image, and the header is a fixed-offset block of little-endian int32. Two things differ from the miniCBF path and are worth naming: * The pixel size is NOT rounded to whole micrometres. A PILATUS pixel is exactly 172 um so the existing reader can afford lround(); a MAR300 pixel is 73.242 um, and rounding it to 73 is a 0.33% scale error on every cell edge reported. * The sweep template is the last run of digits in the whole file name rather than in the stem, which covers both schemes these detectors use - a numbered stem (xtal_1_00042.mccd) and the frame number as the extension (D1.042). A CCD frame marks no untrusted pixels, so the sweep starts with nothing masked, and the format has nowhere to state the rotation axis' direction, so the run settles its sign from the data exactly as it does for a miniCBF carrying no axis table. Measured on one deposited 300-frame Rayonix MX-300 sweep, de novo with no flags: 100% indexing, the deposited point group, cell within 0.045%, 99.5% complete at multiplicity 3.4, in 26 s. The chosen sweep is confirmed against the instrument header before it is opened, so a directory of ordinary TIFFs is refused rather than read with a pixel size of zero - a unit test covers that, both naming schemes, and the geometry conversion. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
cb5a2f032a
commit
a1889c45e9
9 files changed
+850
-8
No files matched your search
@@ -8,9 +8,11 @@
|
||||
#include "../writer/FileWriter.h"
|
||||
#include "../reader/JFJochHDF5Reader.h"
|
||||
#include "../reader/JFJochCBFReader.h"
|
||||
#include "../reader/JFJochMarCCDReader.h"
|
||||
#include "../reader/MiniCBF.h"
|
||||
#include "../compression/JFJochCompressor.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <future>
|
||||
#include <iomanip>
|
||||
@@ -4113,3 +4115,179 @@ TEST_CASE("JFJochCBFReader_incomplete_header_is_refused_not_misread", "[HDF5][Fu
|
||||
RemoveMiniCBFSweep("cbfnocut");
|
||||
}
|
||||
}
|
||||
|
||||
// marCCD: a TIFF whose instrument header sits in the gap between the TIFF header and the pixels.
|
||||
// The fixtures below are written byte for byte rather than through libtiff, because the layout IS
|
||||
// what the reader has to cope with - the private tag that states where the instrument header
|
||||
// begins, and pixels that start well past the end of the IFD.
|
||||
namespace {
|
||||
constexpr size_t MARCCD_HEADER_OFF = 1024;
|
||||
constexpr size_t MARCCD_DATA_OFF = 4096;
|
||||
|
||||
void PutU16(std::vector<uint8_t> &b, size_t off, uint16_t v) {
|
||||
b[off] = v & 0xff; b[off + 1] = (v >> 8) & 0xff;
|
||||
}
|
||||
void PutU32(std::vector<uint8_t> &b, size_t off, uint32_t v) {
|
||||
for (int i = 0; i < 4; i++) b[off + i] = (v >> (8 * i)) & 0xff;
|
||||
}
|
||||
void PutI32(std::vector<uint8_t> &b, size_t off, int32_t v) {
|
||||
PutU32(b, off, static_cast<uint32_t>(v));
|
||||
}
|
||||
|
||||
struct MarCCDFields {
|
||||
int32_t start_phi_mdeg = 0;
|
||||
int32_t end_phi_mdeg = 200;
|
||||
int32_t rotation_range_mdeg = 200;
|
||||
int32_t two_theta_mdeg = 0;
|
||||
int32_t distance_um = 300000;
|
||||
int32_t beam_x_mpx = 12000; // thousandths of a pixel
|
||||
int32_t beam_y_mpx = 8000;
|
||||
int32_t pixel_nm = 73242;
|
||||
int32_t wavelength_e5A = 97872;
|
||||
int32_t saturated = 65535;
|
||||
int32_t depth = 2;
|
||||
bool write_instrument_header = true;
|
||||
};
|
||||
|
||||
// One frame. The image is all zeros: the reader is being asked about geometry, and a pixel
|
||||
// pattern would say nothing extra about that.
|
||||
void WriteMarCCDFrame(const std::string &name, int64_t nx, int64_t ny, const MarCCDFields &f) {
|
||||
const size_t npixel = static_cast<size_t>(nx) * static_cast<size_t>(ny);
|
||||
std::vector<uint8_t> file(MARCCD_DATA_OFF + npixel * 2, 0);
|
||||
|
||||
// TIFF header: little-endian, IFD at byte 8.
|
||||
file[0] = 'I'; file[1] = 'I'; PutU16(file, 2, 42); PutU32(file, 4, 8);
|
||||
struct Entry { uint16_t tag; uint16_t type; uint32_t count; uint32_t value; };
|
||||
const std::vector<Entry> ifd = {
|
||||
{256, 4, 1, static_cast<uint32_t>(nx)}, // ImageWidth
|
||||
{257, 4, 1, static_cast<uint32_t>(ny)}, // ImageLength
|
||||
{258, 3, 1, 16}, // BitsPerSample
|
||||
{259, 3, 1, 1}, // Compression: none
|
||||
{262, 3, 1, 1}, // Photometric: min-is-black
|
||||
{273, 4, 1, MARCCD_DATA_OFF}, // StripOffsets
|
||||
{277, 3, 1, 1}, // SamplesPerPixel
|
||||
{278, 4, 1, static_cast<uint32_t>(ny)}, // RowsPerStrip: one strip
|
||||
{279, 4, 1, static_cast<uint32_t>(npixel * 2)}, // StripByteCounts
|
||||
{34710, 4, 1, MARCCD_HEADER_OFF}, // where the instrument header begins
|
||||
};
|
||||
PutU16(file, 8, static_cast<uint16_t>(ifd.size()));
|
||||
size_t e = 10;
|
||||
for (const auto &t : ifd) {
|
||||
PutU16(file, e, t.tag); PutU16(file, e + 2, t.type); PutU32(file, e + 4, t.count);
|
||||
// A SHORT value sits in the first two bytes of the value field, a LONG fills it.
|
||||
if (t.type == 3) PutU16(file, e + 8, static_cast<uint16_t>(t.value));
|
||||
else PutU32(file, e + 8, t.value);
|
||||
e += 12;
|
||||
}
|
||||
PutU32(file, e, 0); // no next IFD
|
||||
|
||||
if (f.write_instrument_header) {
|
||||
const size_t h = MARCCD_HEADER_OFF;
|
||||
std::memcpy(file.data() + h + 4, "MMX", 3);
|
||||
PutI32(file, h + 80, static_cast<int32_t>(nx)); // nfast
|
||||
PutI32(file, h + 84, static_cast<int32_t>(ny)); // nslow
|
||||
PutI32(file, h + 88, f.depth);
|
||||
PutI32(file, h + 104, f.saturated);
|
||||
PutI32(file, h + 640, f.distance_um);
|
||||
PutI32(file, h + 644, f.beam_x_mpx);
|
||||
PutI32(file, h + 648, f.beam_y_mpx);
|
||||
PutI32(file, h + 656, 1000); // exposure, ms
|
||||
PutI32(file, h + 668, f.two_theta_mdeg); // start_twotheta
|
||||
PutI32(file, h + 684, f.start_phi_mdeg); // start_phi
|
||||
PutI32(file, h + 700, f.two_theta_mdeg); // end_twotheta
|
||||
PutI32(file, h + 716, f.end_phi_mdeg); // end_phi
|
||||
PutI32(file, h + 736, f.rotation_range_mdeg);
|
||||
PutI32(file, h + 772, f.pixel_nm);
|
||||
PutI32(file, h + 776, f.pixel_nm);
|
||||
PutI32(file, h + 908, f.wavelength_e5A);
|
||||
}
|
||||
|
||||
std::ofstream out(name, std::ios::binary);
|
||||
out.write(reinterpret_cast<const char *>(file.data()),
|
||||
static_cast<std::streamsize>(file.size()));
|
||||
}
|
||||
|
||||
// Two frames 0.2 deg apart, under whatever naming scheme the caller asks for.
|
||||
void WriteMarCCDSweep(const std::string &prefix, const std::string &suffix,
|
||||
int64_t nx, int64_t ny, MarCCDFields f = {}) {
|
||||
for (int frame = 1; frame <= 2; frame++) {
|
||||
f.start_phi_mdeg = (frame - 1) * 200;
|
||||
f.end_phi_mdeg = frame * 200;
|
||||
std::ostringstream name;
|
||||
name << prefix << std::setfill('0') << std::setw(3) << frame << suffix;
|
||||
WriteMarCCDFrame(name.str(), nx, ny, f);
|
||||
}
|
||||
}
|
||||
|
||||
void RemoveMarCCDSweep(const std::string &prefix, const std::string &suffix) {
|
||||
for (int frame = 1; frame <= 2; frame++) {
|
||||
std::ostringstream name;
|
||||
name << prefix << std::setfill('0') << std::setw(3) << frame << suffix;
|
||||
remove(name.str().c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("JFJochMarCCDReader_Geometry", "[HDF5][Full]") {
|
||||
const int64_t nx = 24, ny = 16;
|
||||
|
||||
SECTION("the header's geometry reaches the experiment in the units the rest of the code uses") {
|
||||
WriteMarCCDSweep("marccd_", ".mccd", nx, ny);
|
||||
REQUIRE(JFJochMarCCDReader::CanRead("marccd_001.mccd"));
|
||||
JFJochMarCCDReader reader;
|
||||
REQUIRE_NOTHROW(reader.ReadFiles("marccd_001.mccd"));
|
||||
CHECK(reader.GetNumberOfImages() == 2);
|
||||
|
||||
const auto x = reader.GetDataset()->experiment;
|
||||
CHECK(x.GetDetectorDistance_mm() == Catch::Approx(300.0));
|
||||
CHECK(x.GetBeamX_pxl() == Catch::Approx(12.0));
|
||||
CHECK(x.GetBeamY_pxl() == Catch::Approx(8.0));
|
||||
CHECK(x.GetPixelSize_mm() == Catch::Approx(0.073242).epsilon(1e-4));
|
||||
CHECK(x.GetWavelength_A() == Catch::Approx(0.97872).epsilon(1e-5));
|
||||
REQUIRE(x.GetGoniometer().has_value());
|
||||
// The circle that moved between start and end is the scanned one, and the step comes from
|
||||
// the two frames rather than from the header's nominal range.
|
||||
CHECK(x.GetGoniometer()->GetName() == "phi");
|
||||
CHECK(x.GetGoniometer()->GetIncrement_deg() == Catch::Approx(0.2));
|
||||
CHECK(x.GetGoniometer()->GetStart_deg() == Catch::Approx(0.0));
|
||||
reader.Close();
|
||||
RemoveMarCCDSweep("marccd_", ".mccd");
|
||||
}
|
||||
|
||||
SECTION("a frame number as the file extension names a sweep just as well") {
|
||||
// What the mar software writes by default: D1.001, D1.002, ... The frame number is the last
|
||||
// number in the name either way, which is the one rule the sweep template uses.
|
||||
WriteMarCCDSweep("marccdnum.", "", nx, ny);
|
||||
REQUIRE(JFJochMarCCDReader::CanRead("marccdnum.001"));
|
||||
JFJochMarCCDReader reader;
|
||||
REQUIRE_NOTHROW(reader.ReadFiles("marccdnum.001"));
|
||||
CHECK(reader.GetNumberOfImages() == 2);
|
||||
reader.Close();
|
||||
RemoveMarCCDSweep("marccdnum.", "");
|
||||
}
|
||||
|
||||
SECTION("the pixels come back as the int32 the rest of the code reads") {
|
||||
WriteMarCCDSweep("marccdpix_", ".mccd", nx, ny);
|
||||
JFJochMarCCDReader reader;
|
||||
REQUIRE_NOTHROW(reader.ReadFiles("marccdpix_001.mccd"));
|
||||
JFJochReaderRawImage image;
|
||||
REQUIRE(reader.ReadRawImage(0, image));
|
||||
CHECK(image.image.GetWidth() == nx);
|
||||
CHECK(image.image.GetHeight() == ny);
|
||||
CHECK(image.image.GetMode() == CompressedImageMode::Int32);
|
||||
reader.Close();
|
||||
RemoveMarCCDSweep("marccdpix_", ".mccd");
|
||||
}
|
||||
|
||||
SECTION("a TIFF that is not marCCD is refused, not misread") {
|
||||
// The name rule alone matches any numbered TIFF in the directory, so a file with no
|
||||
// instrument header must not be opened with a pixel size and a distance of zero.
|
||||
MarCCDFields f;
|
||||
f.write_instrument_header = false;
|
||||
WriteMarCCDFrame("marccdplain_001.tif", nx, ny, f);
|
||||
CHECK_FALSE(JFJochMarCCDReader::CanRead("marccdplain_001.tif"));
|
||||
JFJochMarCCDReader reader;
|
||||
CHECK_THROWS(reader.ReadFiles("marccdplain_001.tif"));
|
||||
remove("marccdplain_001.tif");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user