diff --git a/include/aare/JungfrauDataFile.hpp b/include/aare/JungfrauDataFile.hpp index bba5403..9b1bc48 100644 --- a/include/aare/JungfrauDataFile.hpp +++ b/include/aare/JungfrauDataFile.hpp @@ -49,6 +49,7 @@ class JungfrauDataFile : public FileInterface { size_t total_frames() const override; size_t rows() const override; size_t cols() const override; + std::array shape() const; size_t n_files() const; //!< get the number of files in the series. // Extra functions needed for FileInterface @@ -81,13 +82,6 @@ class JungfrauDataFile : public FileInterface { */ void read_into(NDArray* image, JungfrauDataHeader* header = nullptr); - /** - * @brief Read a single frame from the file. Allocated a new NDArray for the output data - * @param header pointer to a JungfrauDataHeader or nullptr to skip header) - * @return NDArray with the image data - */ - NDArray read_frame(JungfrauDataHeader* header = nullptr); - JungfrauDataHeader read_header(); std::filesystem::path current_file() const { return fpath(m_current_file_index+m_offset); } diff --git a/include/aare/NDView.hpp b/include/aare/NDView.hpp index f53f758..4442324 100644 --- a/include/aare/NDView.hpp +++ b/include/aare/NDView.hpp @@ -72,6 +72,7 @@ template class NDView : public ArrayExpr(size_); } size_t total_bytes() const { return size_ * sizeof(T); } std::array strides() const noexcept { return strides_; } diff --git a/src/Interpolator.cpp b/src/Interpolator.cpp index 7f82533..0fc708c 100644 --- a/src/Interpolator.cpp +++ b/src/Interpolator.cpp @@ -6,8 +6,8 @@ namespace aare { Interpolator::Interpolator(NDView etacube, NDView xbins, NDView ybins, NDView ebins) : m_ietax(etacube), m_ietay(etacube), m_etabinsx(xbins), m_etabinsy(ybins), m_energy_bins(ebins) { - if (etacube.shape(0) != xbins.size() || etacube.shape(1) != ybins.size() || - etacube.shape(2) != ebins.size()) { + if (etacube.shape(0) != xbins.ssize() || etacube.shape(1) != ybins.ssize() || + etacube.shape(2) != ebins.ssize()) { throw std::invalid_argument( "The shape of the etacube does not match the shape of the bins"); } @@ -68,19 +68,14 @@ std::vector Interpolator::interpolate(const ClusterVector& clus photon.y = cluster.y; photon.energy = eta.sum; - // auto ie = nearest_index(m_energy_bins, photon.energy)-1; - // auto ix = nearest_index(m_etabinsx, eta.x)-1; - // auto iy = nearest_index(m_etabinsy, eta.y)-1; + //Finding the index of the last element that is smaller //should work fine as long as we have many bins auto ie = last_smaller(m_energy_bins, photon.energy); auto ix = last_smaller(m_etabinsx, eta.x); auto iy = last_smaller(m_etabinsy, eta.y); - - // fmt::print("ex: {}, ix: {}, iy: {}\n", ie, ix, iy); - double dX, dY; - int ex, ey; + double dX{}, dY{}; // cBottomLeft = 0, // cBottomRight = 1, // cTopLeft = 2, diff --git a/src/JungfrauDataFile.cpp b/src/JungfrauDataFile.cpp index 6e1ccd6..8f1f904 100644 --- a/src/JungfrauDataFile.cpp +++ b/src/JungfrauDataFile.cpp @@ -37,8 +37,9 @@ Frame JungfrauDataFile::read_frame(size_t frame_number){ std::vector JungfrauDataFile::read_n(size_t n_frames) { std::vector frames; - throw std::runtime_error(LOCATION + - "Not implemented yet"); + for(size_t i = 0; i < n_frames; ++i){ + frames.push_back(read_frame()); + } return frames; } @@ -54,6 +55,10 @@ size_t JungfrauDataFile::frame_number(size_t frame_index) { return read_header().framenum; } +std::array JungfrauDataFile::shape() const { + return {static_cast(rows()), static_cast(cols())}; +} + DetectorType JungfrauDataFile::detector_type() const { return DetectorType::Jungfrau; } std::string JungfrauDataFile::base_name() const { return m_base_name; } @@ -198,22 +203,13 @@ void JungfrauDataFile::read_into(std::byte *image_buf, size_t n_frames, } void JungfrauDataFile::read_into(NDArray* image, JungfrauDataHeader* header) { - if(!(rows() == image->shape(0) && cols() == image->shape(1))){ + if(image->shape()!=shape()){ throw std::runtime_error(LOCATION + "Image shape does not match file size: " + std::to_string(rows()) + "x" + std::to_string(cols())); } read_into(reinterpret_cast(image->data()), header); } -NDArray JungfrauDataFile::read_frame(JungfrauDataHeader* header) { - Shape<2> shape{rows(), cols()}; - NDArray image(shape); - - read_into(reinterpret_cast(image.data()), - header); - - return image; -} JungfrauDataHeader JungfrauDataFile::read_header() { JungfrauDataHeader header; diff --git a/src/JungfrauDataFile.test.cpp b/src/JungfrauDataFile.test.cpp index 626a318..ce51168 100644 --- a/src/JungfrauDataFile.test.cpp +++ b/src/JungfrauDataFile.test.cpp @@ -28,7 +28,8 @@ TEST_CASE("Open a Jungfrau data file", "[.files]") { //Check that the frame number and buch id is read correctly for (size_t i = 0; i < 24; ++i) { JungfrauDataHeader header; - auto image = f.read_frame(&header); + aare::NDArray image(f.shape()); + f.read_into(&image, &header); REQUIRE(header.framenum == i + 1); REQUIRE(header.bunchid == (i + 1) * (i + 1)); REQUIRE(image.shape(0) == 512); @@ -58,7 +59,8 @@ TEST_CASE("Seek in a JungfrauDataFile", "[.files]"){ REQUIRE(h3.framenum == 59+1); JungfrauDataHeader h4; - auto image = f.read_frame(&h4); + aare::NDArray image(f.shape()); + f.read_into(&image, &h4); REQUIRE(h4.framenum == 59+1); //now we should be on the next frame @@ -91,4 +93,22 @@ TEST_CASE("Open a Jungfrau data file with non zero file index", "[.files]"){ REQUIRE(f.current_file().stem() == "AldoJF65k_000003"); +} + +TEST_CASE("Read into throws if size doesn't match", "[.files]"){ + auto fpath = test_data_path() / "dat" / "AldoJF65k_000000.dat"; + REQUIRE(std::filesystem::exists(fpath)); + + JungfrauDataFile f(fpath); + + aare::NDArray image({39, 85}); + JungfrauDataHeader header; + + REQUIRE_THROWS(f.read_into(&image, &header)); + REQUIRE_THROWS(f.read_into(&image, nullptr)); + REQUIRE_THROWS(f.read_into(&image)); + + REQUIRE(f.tell() == 0); + + } \ No newline at end of file