diff --git a/include/aare/RawFile.hpp b/include/aare/RawFile.hpp index 2e81796..8dcf826 100644 --- a/include/aare/RawFile.hpp +++ b/include/aare/RawFile.hpp @@ -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 + 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: /** diff --git a/include/aare/SubFile.hpp b/include/aare/SubFile.hpp index 16bdc41..745fb80 100644 --- a/include/aare/SubFile.hpp +++ b/include/aare/SubFile.hpp @@ -5,7 +5,8 @@ #include #include #include -#include +#include + 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> pixel_map; }; } // namespace aare \ No newline at end of file diff --git a/python/aare/__init__.py b/python/aare/__init__.py index c58d25c..ffdacb9 100644 --- a/python/aare/__init__.py +++ b/python/aare/__init__.py @@ -1,3 +1,4 @@ # Make the compiled classes that live in _aare available from aare. from ._aare import File -from ._aare import VarClusterFinder \ No newline at end of file +from ._aare import VarClusterFinder +from ._aare import GenerateMoench03PixelMap \ No newline at end of file diff --git a/python/examples/play.py b/python/examples/play.py index 68efe94..733aa43 100644 --- a/python/examples/play.py +++ b/python/examples/play.py @@ -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') \ No newline at end of file +plt.imshow(frame) \ No newline at end of file diff --git a/src/RawFile.cpp b/src/RawFile.cpp index 5b55460..030bf48 100644 --- a/src/RawFile.cpp +++ b/src/RawFile.cpp @@ -1,6 +1,7 @@ #include "aare/RawFile.hpp" #include "aare/defs.hpp" #include "aare/json.hpp" +#include "aare/PixelMap.hpp" #include #include @@ -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(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 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(); diff --git a/src/SubFile.cpp b/src/SubFile.cpp index 9c6c64a..6c1d0cc 100644 --- a/src/SubFile.cpp +++ b/src/SubFile.cpp @@ -1,5 +1,5 @@ #include "aare/SubFile.hpp" - +#include "aare/PixelMap.hpp" #include // memcpy #include #include @@ -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(buffer); + auto *part_data = reinterpret_cast(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) {