decoding of old Moench03

This commit is contained in:
Erik Fröjdh 2024-10-31 11:53:24 +01:00
parent ae1166b908
commit 563c39c0dd
6 changed files with 66 additions and 15 deletions

View File

@ -1,8 +1,11 @@
#pragma once
#include "aare/Frame.hpp"
#include "aare/NDArray.hpp" //for pixel map
#include "aare/FileInterface.hpp"
#include "aare/SubFile.hpp"
#include <optional>
namespace aare {
struct ModuleConfig {
@ -37,6 +40,7 @@ class RawFile : public FileInterface {
//Stuff that we might need with Ctb files
uint32_t m_analog_samples{};
uint32_t m_digital_samples{};
public:
/**

View File

@ -5,7 +5,8 @@
#include <cstdint>
#include <filesystem>
#include <map>
#include <variant>
#include <optional>
namespace aare {
@ -72,6 +73,8 @@ class SubFile {
std::string m_mode;
size_t n_frames{};
int m_sub_file_index_{};
DetectorType m_detector_type;
std::optional<NDArray<size_t, 2>> pixel_map;
};
} // namespace aare

View File

@ -1,3 +1,4 @@
# Make the compiled classes that live in _aare available from aare.
from ._aare import File
from ._aare import VarClusterFinder
from ._aare import VarClusterFinder
from ._aare import GenerateMoench03PixelMap

View File

@ -5,10 +5,17 @@ plt.ion()
import aare
from pathlib import Path
p = Path('/Users/erik/data/aare_test_data/jungfrau/jungfrau_single_master_0.json')
# p = Path('/Users/erik/data/aare_test_data/jungfrau/jungfrau_single_master_0.json')
f = aare.File(p)
# f = aare.File(p)
# frame = f.read_frame()
# fig, ax = plt.subplots()
# im = ax.imshow(frame, cmap='viridis')
fpath = Path('/Users/erik/data/Moench03old/test_034_irradiated_noise_g4_hg_exptime_2000us_master_0.json')
f = aare.File(fpath)
frame = f.read_frame()
fig, ax = plt.subplots()
im = ax.imshow(frame, cmap='viridis')
plt.imshow(frame)

View File

@ -1,6 +1,7 @@
#include "aare/RawFile.hpp"
#include "aare/defs.hpp"
#include "aare/json.hpp"
#include "aare/PixelMap.hpp"
#include <fmt/format.h>
#include <nlohmann/json.hpp>
@ -236,12 +237,15 @@ void RawFile::parse_json_metadata() {
}
//Update detector type for Moench
if (m_type == DetectorType::Moench && m_analog_samples == 0 && m_rows == 400) {
//TODO! How does this work with old .raw master files?
if (m_type == DetectorType::Moench && m_analog_samples == 0 && subfile_rows == 400) {
m_type = DetectorType::Moench03;
}else if (m_type == DetectorType::Moench && m_rows == 400 && m_analog_samples == 5000) {
}else if (m_type == DetectorType::Moench && subfile_rows == 400 && m_analog_samples == 5000) {
m_type = DetectorType::Moench03_old;
}
// only Eiger had quad
if (m_type == DetectorType::Eiger) {
@ -321,10 +325,6 @@ Frame RawFile::get_frame(size_t frame_index) {
auto f = Frame(this->m_rows, this->m_cols, Dtype::from_bitdepth(this->m_bitdepth));
std::byte *frame_buffer = f.data();
get_frame_into(frame_index, frame_buffer);
//here would be the place to run a transform before returning the frame
if (m_type == DetectorType::Moench03_old)
fmt::print("Moench03_old\n");
return f;
}
@ -375,6 +375,8 @@ void RawFile::get_frame_into(size_t frame_index, std::byte *frame_buffer) {
auto bytes_per_part = this->subfile_rows * this->subfile_cols * this->m_bitdepth / 8;
auto *part_buffer = new std::byte[bytes_per_part];
//TODO! if we have many submodules we should reorder them on the module level
for (size_t part_idx = 0; part_idx != this->n_subfile_parts; ++part_idx) {
auto corrected_idx = frame_indices[part_idx];
auto subfile_id = corrected_idx / this->max_frames_per_file;
@ -391,6 +393,17 @@ void RawFile::get_frame_into(size_t frame_index, std::byte *frame_buffer) {
}
delete[] part_buffer;
}
//TODO! deal with ROI!
// if (m_type == DetectorType::Moench03_old) {
// auto *data = reinterpret_cast<uint16_t *>(frame_buffer);
// for (size_t i = 0; i < m_rows * m_cols; i++) {
// data[i] = pixel_map[data[i]];
// }
// }
}
void RawFile::write(Frame &frame, sls_detector_header header) {
@ -417,6 +430,7 @@ std::vector<Frame> RawFile::read_n(size_t n_frames) {
}
void RawFile::read_into(std::byte *image_buf, size_t n_frames) {
// TODO: implement this in a more efficient way
for (size_t i = 0; i < n_frames; i++) {
this->get_frame_into(this->current_frame++, image_buf);
image_buf += this->bytes_per_frame();

View File

@ -1,5 +1,5 @@
#include "aare/SubFile.hpp"
#include "aare/PixelMap.hpp"
#include <cstring> // memcpy
#include <fmt/core.h>
#include <iostream>
@ -9,7 +9,12 @@ namespace aare {
SubFile::SubFile(const std::filesystem::path &fname, DetectorType detector, size_t rows, size_t cols, size_t bitdepth,
const std::string &mode)
: m_bitdepth(bitdepth), m_fname(fname), m_rows(rows), m_cols(cols), m_mode(mode) {
: m_bitdepth(bitdepth), m_fname(fname), m_rows(rows), m_cols(cols), m_mode(mode), m_detector_type(detector) {
if (m_detector_type == DetectorType::Moench03_old) {
pixel_map = GenerateMoench03PixelMap();
}
if (std::filesystem::exists(fname)) {
n_frames = std::filesystem::file_size(fname) / (sizeof(sls_detector_header) + rows * cols * bitdepth / 8);
@ -39,7 +44,24 @@ size_t SubFile::get_part(std::byte *buffer, size_t frame_index) {
}
fseek(fp, sizeof(sls_detector_header) + (sizeof(sls_detector_header) + bytes_per_part()) * frame_index, // NOLINT
SEEK_SET);
return fread(buffer, this->bytes_per_part(), 1, this->fp);
if (pixel_map){
// read into a temporary buffer and then copy the data to the buffer
// in the correct order
auto part_buffer = new std::byte[bytes_per_part()];
auto wc = fread(part_buffer, bytes_per_part(), 1, fp);
auto *data = reinterpret_cast<uint16_t *>(buffer);
auto *part_data = reinterpret_cast<uint16_t *>(part_buffer);
for (size_t i = 0; i < pixels_per_part(); i++) {
data[i] = part_data[(*pixel_map)(i)];
}
delete[] part_buffer;
return wc;
}else{
// read directly into the buffer
return fread(buffer, this->bytes_per_part(), 1, this->fp);
}
}
size_t SubFile::write_part(std::byte *buffer, sls_detector_header header, size_t frame_index) {
if (frame_index > n_frames) {