From 2ee1a5583e48eade5b52b613830c362f42afcac0 Mon Sep 17 00:00:00 2001 From: froejdh_e Date: Tue, 12 Nov 2024 09:27:01 +0100 Subject: [PATCH] WIP --- include/aare/RawFile.hpp | 14 ++--- include/aare/RawMasterFile.hpp | 3 +- python/examples/play.py | 10 ++-- python/src/file.hpp | 72 +++----------------------- python/src/module.cpp | 4 ++ python/src/raw_file.hpp | 93 ++++++++++++++++++++++++++++++++++ python/src/raw_master_file.hpp | 58 +++++++++++++++++++++ src/RawFile.cpp | 23 ++++++--- src/RawMasterFile.cpp | 6 +++ 9 files changed, 199 insertions(+), 84 deletions(-) create mode 100644 python/src/raw_file.hpp create mode 100644 python/src/raw_master_file.hpp diff --git a/include/aare/RawFile.hpp b/include/aare/RawFile.hpp index 5c28481..b3fa7cc 100644 --- a/include/aare/RawFile.hpp +++ b/include/aare/RawFile.hpp @@ -62,32 +62,28 @@ class RawFile : public FileInterface { //TODO! do we need to adapt the API? void read_into(std::byte *image_buf, DetectorHeader *header); - + void read_into(std::byte *image_buf, size_t n_frames, DetectorHeader *header); size_t frame_number(size_t frame_index) override; size_t bytes_per_frame() override; size_t pixels_per_frame() override; + size_t bytes_per_pixel() const; void seek(size_t frame_index) override; size_t tell() override; size_t total_frames() const override; size_t rows() const override; size_t cols() const override; size_t bitdepth() const override; - size_t bytes_per_pixel() const; + xy geometry(); size_t n_mod() const; + + RawMasterFile master() const; DetectorType detector_type() const override; private: - /** - * @brief check if the file is a master file - * @param fpath path to the file - */ - static bool is_master_file(const std::filesystem::path &fpath); - - // TODO! Deal with fast quad and missing files /** * @brief read the frame at the given frame index into the image buffer diff --git a/include/aare/RawMasterFile.hpp b/include/aare/RawMasterFile.hpp index 04eb1c9..268de31 100644 --- a/include/aare/RawMasterFile.hpp +++ b/include/aare/RawMasterFile.hpp @@ -56,6 +56,7 @@ class ScanParameters { int step() const; const std::string &dac() const; bool enabled() const; + void increment_stop(); }; @@ -67,7 +68,7 @@ struct ROI{ size_t height() const { return ymax - ymin; } size_t width() const { return xmax - xmin; } -}; +}__attribute__((packed)); /** * @brief Class for parsing a master file either in our .json format or the old diff --git a/python/examples/play.py b/python/examples/play.py index ceeb1d3..7e4eb57 100644 --- a/python/examples/play.py +++ b/python/examples/play.py @@ -78,11 +78,15 @@ base = Path('/mnt/sls_det_storage/matterhorn_data/aare_test_data/ci/aare_test_da # # cols = 1024-roi.xmin # # sf = RawSubFile(base / raw, DetectorType.Jungfrau, rows, cols, 16) -from aare import RawFile -base = Path('/mnt/sls_det_storage/matterhorn_data/aare_test_data/Jungfrau10/Jungfrau_DoubleModule_1UDP_ROI/SideBySide/') -fname = base / Path('241019_JF_12keV_Si_FF_GaAs_FF_7p88mmFilter_PedestalStart_ZPos_5.5_master_0.json') + +from aare import RawFile, File + +base = Path('/mnt/sls_det_storage/matterhorn_data/aare_test_data/Jungfrau10/Jungfrau_DoubleModule_1UDP_ROI/') +fname = base / Path('SideBySide/241019_JF_12keV_Si_FF_GaAs_FF_7p88mmFilter_PedestalStart_ZPos_5.5_master_0.json') # fname = Path(base / 'jungfrau/jungfrau_single_master_0.json') +# fname = base / 'Stacked/241024_JF10_m450_m367_KnifeEdge_TestBesom_9keV_750umFilter_PedestalStart_ZPos_-6_master_0.json' + f = RawFile(fname) h,img = f.read_frame() print(f'{h["frameNumber"]}') diff --git a/python/src/file.hpp b/python/src/file.hpp index c7cb445..a7c3a9c 100644 --- a/python/src/file.hpp +++ b/python/src/file.hpp @@ -169,39 +169,7 @@ void define_file_io_bindings(py::module &m) { }); - py::class_(m, "RawMasterFile") - .def(py::init()) - .def("data_fname", &RawMasterFile::data_fname) - .def_property_readonly("version", &RawMasterFile::version) - .def_property_readonly("detector_type", &RawMasterFile::detector_type) - .def_property_readonly("timing_mode", &RawMasterFile::timing_mode) - .def_property_readonly("image_size_in_bytes", - &RawMasterFile::image_size_in_bytes) - .def_property_readonly("frames_in_file", &RawMasterFile::frames_in_file) - .def_property_readonly("pixels_y", &RawMasterFile::pixels_y) - .def_property_readonly("pixels_x", &RawMasterFile::pixels_x) - .def_property_readonly("max_frames_per_file", - &RawMasterFile::max_frames_per_file) - .def_property_readonly("bitdepth", &RawMasterFile::bitdepth) - .def_property_readonly("frame_padding", &RawMasterFile::frame_padding) - .def_property_readonly("frame_discard_policy", - &RawMasterFile::frame_discard_policy) - - .def_property_readonly("total_frames_expected", - &RawMasterFile::total_frames_expected) - .def_property_readonly("geometry", &RawMasterFile::geometry) - .def_property_readonly("analog_samples", &RawMasterFile::analog_samples) - .def_property_readonly("digital_samples", - &RawMasterFile::digital_samples) - - .def_property_readonly("transceiver_samples", - &RawMasterFile::transceiver_samples) - .def_property_readonly("number_of_rows", &RawMasterFile::number_of_rows) - .def_property_readonly("quad", &RawMasterFile::quad) - .def_property_readonly("scan_parameters", - &RawMasterFile::scan_parameters) - .def_property_readonly("roi", &RawMasterFile::roi); - + py::class_(m, "ScanParameters") .def(py::init()) .def(py::init()) @@ -221,39 +189,15 @@ void define_file_io_bindings(py::module &m) { .def_readwrite("ymax", &ROI::ymax) .def("__str__", [](const ROI& self){ return fmt::format("ROI: xmin: {} xmax: {} ymin: {} ymax: {}", self.xmin, self.xmax, self.ymin, self.ymax); + }) + .def("__repr__", [](const ROI& self){ + return fmt::format("", self.xmin, self.xmax, self.ymin, self.ymax); + }) + .def("__iter__", [](const ROI &self) { + return py::make_iterator(&self.xmin, &self.ymax+1); }); - py::class_(m, "RawFile") - .def(py::init()) - .def("read_frame", - [](RawFile &self) { - size_t image_size = self.bytes_per_frame(); - const uint8_t item_size = self.bytes_per_pixel(); - py::array image; - std::vector shape; - shape.reserve(2); - shape.push_back(self.rows()); - shape.push_back(self.cols()); - - - //return headers from all subfiles - py::array_t header(self.n_mod()); - - if (item_size == 1) { - image = py::array_t(shape); - } else if (item_size == 2) { - image = py::array_t(shape); - } else if (item_size == 4) { - image = py::array_t(shape); - } - fmt::print("item_size: {} rows: {} cols: {}\n", item_size, self.rows(), self.cols()); - - self.read_into( - reinterpret_cast(image.mutable_data()), - header.mutable_data()); - - return py::make_tuple(header, image); - }); + py::class_(m, "RawSubFile") .def(py::init(m, "Pedestal"); diff --git a/python/src/raw_file.hpp b/python/src/raw_file.hpp new file mode 100644 index 0000000..3ddbe9b --- /dev/null +++ b/python/src/raw_file.hpp @@ -0,0 +1,93 @@ +#include "aare/CtbRawFile.hpp" +#include "aare/File.hpp" +#include "aare/Frame.hpp" +#include "aare/RawFile.hpp" +#include "aare/RawMasterFile.hpp" +#include "aare/RawSubFile.hpp" + +#include "aare/defs.hpp" +// #include "aare/fClusterFileV2.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace py = pybind11; +using namespace ::aare; + +void define_raw_file_io_bindings(py::module &m) { + py::class_(m, "RawFile") + .def(py::init()) + .def("read_frame", [](RawFile &self) { + size_t image_size = self.bytes_per_frame(); + py::array image; + std::vector shape; + shape.reserve(2); + shape.push_back(self.rows()); + shape.push_back(self.cols()); + + // return headers from all subfiles + py::array_t header(self.n_mod()); + + const uint8_t item_size = self.bytes_per_pixel(); + if (item_size == 1) { + image = py::array_t(shape); + } else if (item_size == 2) { + image = py::array_t(shape); + } else if (item_size == 4) { + image = py::array_t(shape); + } + self.read_into(reinterpret_cast(image.mutable_data()), + header.mutable_data()); + + return py::make_tuple(header, image); + }) + .def("read_n", [](RawFile &self, size_t n_frames) { + py::array image; + std::vector shape; + shape.reserve(3); + shape.push_back(n_frames); + shape.push_back(self.rows()); + shape.push_back(self.cols()); + + // return headers from all subfiles + py::array_t header({self.n_mod(),n_frames}); + + const uint8_t item_size = self.bytes_per_pixel(); + if (item_size == 1) { + image = py::array_t(shape); + } else if (item_size == 2) { + image = py::array_t(shape); + } else if (item_size == 4) { + image = py::array_t(shape); + } + self.read_into(reinterpret_cast(image.mutable_data()),n_frames, + header.mutable_data()); + + return py::make_tuple(header, image); + + }) + .def("frame_number", &RawFile::frame_number) + .def_property_readonly("bytes_per_frame", &RawFile::bytes_per_frame) + .def_property_readonly("pixels_per_frame", &RawFile::pixels_per_frame) + .def_property_readonly("bytes_per_pixel", &RawFile::bytes_per_pixel) + .def("seek", &RawFile::seek, R"( + Seek to a specific frame number. + )") + .def("tell", &RawFile::tell) + .def_property_readonly("total_frames", &RawFile::total_frames) + .def_property_readonly("rows", &RawFile::rows) + .def_property_readonly("cols", &RawFile::cols) + .def_property_readonly("bitdepth", &RawFile::bitdepth) + .def_property_readonly("geometry", &RawFile::geometry) + .def_property_readonly("n_mod", &RawFile::n_mod) + .def_property_readonly("detector_type", &RawFile::detector_type) + .def_property_readonly("master", &RawFile::master); + + +} \ No newline at end of file diff --git a/python/src/raw_master_file.hpp b/python/src/raw_master_file.hpp new file mode 100644 index 0000000..1411c6a --- /dev/null +++ b/python/src/raw_master_file.hpp @@ -0,0 +1,58 @@ + +#include "aare/CtbRawFile.hpp" +#include "aare/File.hpp" +#include "aare/Frame.hpp" +#include "aare/RawFile.hpp" +#include "aare/RawMasterFile.hpp" +#include "aare/RawSubFile.hpp" + +#include "aare/defs.hpp" +// #include "aare/fClusterFileV2.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace py = pybind11; +using namespace ::aare; + +void define_raw_master_file_bindings(py::module &m) { +py::class_(m, "RawMasterFile") + .def(py::init()) + .def("data_fname", &RawMasterFile::data_fname) + .def_property_readonly("version", &RawMasterFile::version) + .def_property_readonly("detector_type", &RawMasterFile::detector_type) + .def_property_readonly("timing_mode", &RawMasterFile::timing_mode) + .def_property_readonly("image_size_in_bytes", + &RawMasterFile::image_size_in_bytes) + .def_property_readonly("frames_in_file", &RawMasterFile::frames_in_file) + .def_property_readonly("pixels_y", &RawMasterFile::pixels_y) + .def_property_readonly("pixels_x", &RawMasterFile::pixels_x) + .def_property_readonly("max_frames_per_file", + &RawMasterFile::max_frames_per_file) + .def_property_readonly("bitdepth", &RawMasterFile::bitdepth) + .def_property_readonly("frame_padding", &RawMasterFile::frame_padding) + .def_property_readonly("frame_discard_policy", + &RawMasterFile::frame_discard_policy) + + .def_property_readonly("total_frames_expected", + &RawMasterFile::total_frames_expected) + .def_property_readonly("geometry", &RawMasterFile::geometry) + .def_property_readonly("analog_samples", &RawMasterFile::analog_samples) + .def_property_readonly("digital_samples", + &RawMasterFile::digital_samples) + + .def_property_readonly("transceiver_samples", + &RawMasterFile::transceiver_samples) + .def_property_readonly("number_of_rows", &RawMasterFile::number_of_rows) + .def_property_readonly("quad", &RawMasterFile::quad) + .def_property_readonly("scan_parameters", + &RawMasterFile::scan_parameters) + .def_property_readonly("roi", &RawMasterFile::roi); + +} diff --git a/src/RawFile.cpp b/src/RawFile.cpp index a82b29b..c1b9c52 100644 --- a/src/RawFile.cpp +++ b/src/RawFile.cpp @@ -49,10 +49,20 @@ void RawFile::read_into(std::byte *image_buf) { void RawFile::read_into(std::byte *image_buf, DetectorHeader *header) { - return get_frame_into(m_current_frame++, image_buf, header); }; +void RawFile::read_into(std::byte *image_buf, size_t n_frames, DetectorHeader *header) { + // return get_frame_into(m_current_frame++, image_buf, header); + + for (size_t i = 0; i < n_frames; i++) { + this->get_frame_into(m_current_frame++, image_buf, header); + image_buf += bytes_per_frame(); + if(header) + header+=n_mod(); + } +}; + size_t RawFile::n_mod() const { return n_subfile_parts; } @@ -117,10 +127,7 @@ DetectorHeader RawFile::read_header(const std::filesystem::path &fname) { return h; } -bool RawFile::is_master_file(const std::filesystem::path &fpath) { - std::string const stem = fpath.stem().string(); - return stem.find("_master_") != std::string::npos; -} + int RawFile::find_number_of_subfiles() { int n_files = 0; @@ -135,6 +142,8 @@ int RawFile::find_number_of_subfiles() { } +RawMasterFile RawFile::master() const { return m_master; } + void RawFile::find_geometry() { uint16_t r{}; uint16_t c{}; @@ -208,8 +217,8 @@ void RawFile::update_geometry_with_roi() { if (m.y + m.height < roi.ymin) { m.height = 0; } else { - if (roi.ymin < m.y + m.height) { - m.height -= roi.ymin; + if ((roi.ymin > m.y) && (roi.ymin < m.y + m.height)) { + m.height -= roi.ymin - m.y; } if (roi.ymax < m.y + m.height) { m.height -= m.y + original_height - roi.ymax; diff --git a/src/RawMasterFile.cpp b/src/RawMasterFile.cpp index ae2caa1..0a5d146 100644 --- a/src/RawMasterFile.cpp +++ b/src/RawMasterFile.cpp @@ -72,6 +72,9 @@ ScanParameters::ScanParameters(const std::string& par){ int ScanParameters::start() const { return m_start; } int ScanParameters::stop() const { return m_stop; } +void ScanParameters::increment_stop(){ + m_stop += 1; +}; int ScanParameters::step() const { return m_step; } const std::string &ScanParameters::dac() const { return m_dac; } bool ScanParameters::enabled() const { return m_enabled; } @@ -240,6 +243,9 @@ void RawMasterFile::parse_json(const std::filesystem::path &fpath) { try{ std::string scan_parameters = j.at("Scan Parameters"); m_scan_parameters = ScanParameters(scan_parameters); + if(v<7.21){ + m_scan_parameters.increment_stop(); //adjust for endpoint being included + } }catch (const json::out_of_range &e) { // not a scan }