diff --git a/include/aare/Hdf5File.hpp b/include/aare/Hdf5File.hpp index 1c9a73d..1cd4230 100644 --- a/include/aare/Hdf5File.hpp +++ b/include/aare/Hdf5File.hpp @@ -8,7 +8,7 @@ namespace aare { -struct H5Handles { +class H5Handles { std::string file_name; std::string dataset_name; H5::H5File file; @@ -20,6 +20,7 @@ struct H5Handles { std::vector count; std::vector offset; + public: H5Handles(const std::string& fname, const std::string& dname): file_name(fname), dataset_name(dname), @@ -31,6 +32,10 @@ struct H5Handles { initialize_memspace(); } + std::vector get_dims() const { + return dims; + } + void seek(size_t frame_index) { if (frame_index >= dims[0]) { throw std::runtime_error(LOCATION + "Invalid frame number"); diff --git a/include/aare/Hdf5MasterFile.hpp b/include/aare/Hdf5MasterFile.hpp index 1894c0e..6b073b0 100644 --- a/include/aare/Hdf5MasterFile.hpp +++ b/include/aare/Hdf5MasterFile.hpp @@ -6,18 +6,6 @@ #include #include -namespace fmt { -template struct formatter> : formatter { - template - auto format(const std::optional &opt, FormatContext &ctx) { - if (opt) { - return formatter::format(*opt, ctx); - } else { - return format_to(ctx.out(), "nullopt"); - } - } -}; -} // namespace fmt namespace aare { @@ -29,7 +17,7 @@ class Hdf5FileNameComponents { std::filesystem::path m_base_path{}; std::string m_base_name{}; std::string m_ext{}; - int m_file_index{}; // TODO! is this measurement_index? + int m_file_index{}; public: Hdf5FileNameComponents(const std::filesystem::path &fname); @@ -50,39 +38,6 @@ class Hdf5FileNameComponents { void set_old_scheme(bool old_scheme); }; -/* -class ScanParameters { - bool m_enabled = false; - std::string m_dac; - int m_start = 0; - int m_stop = 0; - int m_step = 0; - // TODO! add settleTime, requires string to time conversion - - public: - ScanParameters(const std::string &par); - ScanParameters() = default; - ScanParameters(const ScanParameters &) = default; - ScanParameters &operator=(const ScanParameters &) = default; - ScanParameters(ScanParameters &&) = default; - int start() const; - int stop() const; - int step() const; - const std::string &dac() const; - bool enabled() const; - void increment_stop(); -}; - -struct ROI { - int64_t xmin{}; - int64_t xmax{}; - int64_t ymin{}; - int64_t ymax{}; - - int64_t height() const { return ymax - ymin; } - int64_t width() const { return xmax - xmin; } -}; -*/ /** * @brief Class for parsing a master file either in our .json format or the old @@ -104,7 +59,7 @@ class Hdf5MasterFile { xy m_geometry{}; size_t m_max_frames_per_file{}; - // uint32_t m_adc_mask{}; // TODO! implement reading + uint32_t m_adc_mask{}; // TODO! implement reading FrameDiscardPolicy m_frame_discard_policy{}; size_t m_frame_padding{}; @@ -113,7 +68,7 @@ class Hdf5MasterFile { uint8_t m_digital_flag{}; uint8_t m_transceiver_flag{}; - // ScanParameters m_scan_parameters; + ScanParameters m_scan_parameters; std::optional m_analog_samples; std::optional m_digital_samples; @@ -121,7 +76,7 @@ class Hdf5MasterFile { std::optional m_number_of_rows; std::optional m_quad; - // std::optional m_roi; + std::optional m_roi; public: Hdf5MasterFile(const std::filesystem::path &fpath); @@ -151,9 +106,9 @@ class Hdf5MasterFile { std::optional number_of_rows() const; std::optional quad() const; - // std::optional roi() const; + std::optional roi() const; - // ScanParameters scan_parameters() const; + ScanParameters scan_parameters() const; private: static const std::string metadata_group_name; diff --git a/include/aare/RawMasterFile.hpp b/include/aare/RawMasterFile.hpp index 4d143a6..2f2c8f6 100644 --- a/include/aare/RawMasterFile.hpp +++ b/include/aare/RawMasterFile.hpp @@ -39,28 +39,6 @@ class RawFileNameComponents { void set_old_scheme(bool old_scheme); }; -class ScanParameters { - bool m_enabled = false; - std::string m_dac; - int m_start = 0; - int m_stop = 0; - int m_step = 0; - //TODO! add settleTime, requires string to time conversion - - public: - ScanParameters(const std::string &par); - ScanParameters() = default; - ScanParameters(const ScanParameters &) = default; - ScanParameters &operator=(const ScanParameters &) = default; - ScanParameters(ScanParameters &&) = default; - int start() const; - int stop() const; - int step() const; - const std::string &dac() const; - bool enabled() const; - void increment_stop(); -}; - /** * @brief Class for parsing a master file either in our .json format or the old @@ -132,7 +110,6 @@ class RawMasterFile { std::optional roi() const; - ScanParameters scan_parameters() const; private: diff --git a/include/aare/defs.hpp b/include/aare/defs.hpp index 127e2e1..913e7ab 100644 --- a/include/aare/defs.hpp +++ b/include/aare/defs.hpp @@ -12,7 +12,8 @@ #include #include #include - +#include +#include /** * @brief LOCATION macro to get the current location in the code @@ -225,6 +226,46 @@ struct ROI{ using dynamic_shape = std::vector; + +class ScanParameters { + bool m_enabled = false; + std::string m_dac; + int m_start = 0; + int m_stop = 0; + int m_step = 0; + //TODO! add settleTime, requires string to time conversion + + public: + // "[enabled\ndac dac 4\nstart 500\nstop 2200\nstep 5\nsettleTime 100us\n]" + ScanParameters(const std::string &par) { + std::istringstream iss(par.substr(1, par.size()-2)); + std::string line; + while(std::getline(iss, line)){ + if(line == "enabled"){ + m_enabled = true; + }else if(line.find("dac") != std::string::npos){ + m_dac = line.substr(4); + }else if(line.find("start") != std::string::npos){ + m_start = std::stoi(line.substr(6)); + }else if(line.find("stop") != std::string::npos){ + m_stop = std::stoi(line.substr(5)); + }else if(line.find("step") != std::string::npos){ + m_step = std::stoi(line.substr(5)); + } + } + }; + ScanParameters() = default; + ScanParameters(const ScanParameters &) = default; + ScanParameters &operator=(const ScanParameters &) = default; + ScanParameters(ScanParameters &&) = default; + int start() const { return m_start; }; + int stop() const { return m_stop; }; + int step() const { return m_step; }; + const std::string &dac() const { return m_dac; }; + bool enabled() const { return m_enabled; }; + void increment_stop() { m_stop += 1; }; +}; + //TODO! Can we uniform enums between the libraries? /** @@ -266,6 +307,8 @@ template <> std::string ToString(TimingMode arg); template <> FrameDiscardPolicy StringTo(const std::string & /*mode*/); template <> std::string ToString(FrameDiscardPolicy arg); +template <> std::string ToString(ScanParameters arg); + using DataTypeVariants = std::variant; template @@ -280,5 +323,14 @@ std::ostream &operator<<(std::ostream &os, const std::vector &vec) { return os; } +template +std::ostream &operator<<(std::ostream &os, const std::optional &opt) { + if (opt) + os << *opt; + else + os << "nullopt"; + return os; +} + } // namespace aare \ No newline at end of file diff --git a/src/Hdf5File.cpp b/src/Hdf5File.cpp index 765545e..f6a3854 100644 --- a/src/Hdf5File.cpp +++ b/src/Hdf5File.cpp @@ -193,9 +193,9 @@ void Hdf5File::open_data_file() { try { m_data_file = std::make_unique(m_master.master_fname().string(), metadata_group_name + "/data"); - m_total_frames = m_data_file->dims[0]; - m_rows = m_data_file->dims[1]; - m_cols = m_data_file->dims[2]; + m_total_frames = m_data_file->get_dims()[0]; + m_rows = m_data_file->get_dims()[1]; + m_cols = m_data_file->get_dims()[2]; //fmt::print("Data Dataset dimensions: frames = {}, rows = {}, cols = {}\n", // m_total_frames, m_rows, m_cols); } catch (const H5::Exception &e) { diff --git a/src/Hdf5MasterFile.cpp b/src/Hdf5MasterFile.cpp index c61fab3..f75d757 100644 --- a/src/Hdf5MasterFile.cpp +++ b/src/Hdf5MasterFile.cpp @@ -1,4 +1,5 @@ #include "aare/Hdf5MasterFile.hpp" +#include "aare/logger.hpp" #include #include namespace aare { @@ -62,32 +63,6 @@ const std::string &Hdf5FileNameComponents::base_name() const { const std::string &Hdf5FileNameComponents::ext() const { return m_ext; } int Hdf5FileNameComponents::file_index() const { return m_file_index; } -// "[enabled\ndac dac 4\nstart 500\nstop 2200\nstep 5\nsettleTime 100us\n]" -/*ScanParameters::ScanParameters(const std::string &par) { - std::istringstream iss(par.substr(1, par.size() - 2)); - std::string line; - while (std::getline(iss, line)) { - if (line == "enabled") { - m_enabled = true; - } else if (line.find("dac") != std::string::npos) { - m_dac = line.substr(4); - } else if (line.find("start") != std::string::npos) { - m_start = std::stoi(line.substr(6)); - } else if (line.find("stop") != std::string::npos) { - m_stop = std::stoi(line.substr(5)); - } else if (line.find("step") != std::string::npos) { - m_step = std::stoi(line.substr(5)); - } - } -} - -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; } -*/ Hdf5MasterFile::Hdf5MasterFile(const std::filesystem::path &fpath) : m_fnc(fpath) { if (!std::filesystem::exists(fpath)) { @@ -131,18 +106,12 @@ size_t Hdf5MasterFile::total_frames_expected() const { return m_total_frames_expected; } -std::optional Hdf5MasterFile::number_of_rows() const { - return m_number_of_rows; -} - xy Hdf5MasterFile::geometry() const { return m_geometry; } size_t Hdf5MasterFile::n_modules() const { return m_geometry.row * m_geometry.col; } -std::optional Hdf5MasterFile::quad() const { return m_quad; } - // optional values, these may or may not be present in the master file // and are therefore modeled as std::optional std::optional Hdf5MasterFile::analog_samples() const { @@ -156,13 +125,17 @@ std::optional Hdf5MasterFile::transceiver_samples() const { return m_transceiver_samples; } -/* +std::optional Hdf5MasterFile::number_of_rows() const { + return m_number_of_rows; +} + +std::optional Hdf5MasterFile::quad() const { return m_quad; } + +std::optional Hdf5MasterFile::roi() const { return m_roi; } + ScanParameters Hdf5MasterFile::scan_parameters() const { return m_scan_parameters; } -*/ - -// std::optional Hdf5MasterFile::roi() const { return m_roi; } const std::string Hdf5MasterFile::metadata_group_name = "/entry/instrument/detector/"; @@ -202,59 +175,59 @@ void Hdf5MasterFile::parse_acquisition_metadata( H5::H5File file(fpath, H5F_ACC_RDONLY); // Attribute - version + double dVersion{0.0}; { H5::Attribute attr = file.openAttribute("version"); H5::DataType attr_type = attr.getDataType(); - double value{0.0}; - attr.read(attr_type, &value); + attr.read(attr_type, &dVersion); std::ostringstream oss; - oss << std::fixed << std::setprecision(1) << value; + oss << std::fixed << std::setprecision(1) << dVersion; m_version = oss.str(); - // fmt::print("Version: {}\n", m_version); + LOG(logDEBUG) << "Version: " << m_version; } // Scalar Dataset // Detector Type m_type = StringTo(h5_get_scalar_dataset( file, std::string(metadata_group_name + "Detector Type"))); - // fmt::print("Detector Type: {}\n", (ToString(m_type))); + LOG(logDEBUG) << "Detector Type: " << ToString(m_type); // Timing Mode m_timing_mode = StringTo(h5_get_scalar_dataset( file, std::string(metadata_group_name + "Timing Mode"))); - // fmt::print("Timing Mode: {}\n", (ToString(m_timing_mode))); + LOG(logDEBUG) << "Timing Mode: " << ToString(m_timing_mode); // Geometry m_geometry.row = h5_get_scalar_dataset( file, std::string(metadata_group_name + "Geometry in y axis")); m_geometry.col = h5_get_scalar_dataset( file, std::string(metadata_group_name + "Geometry in x axis")); - // fmt::print("Geometry: {}\n", m_geometry.to_string()); + LOG(logDEBUG) << "Geometry: " << m_geometry.to_string(); // Image Size m_image_size_in_bytes = h5_get_scalar_dataset( file, std::string(metadata_group_name + "Image Size")); - // fmt::print("Image size: {}\n", m_image_size_in_bytes); + LOG(logDEBUG) << "Image size: {}\n" << m_image_size_in_bytes; // Frames in File m_frames_in_file = h5_get_scalar_dataset( file, std::string(metadata_group_name + "Frames in File")); - // fmt::print("Frames in File: {}\n", m_frames_in_file); + LOG(logDEBUG) << "Frames in File: " << m_frames_in_file; // Pixels m_pixels_y = h5_get_scalar_dataset( file, std::string(metadata_group_name + "Number of pixels in y axis")); - // fmt::print("Pixels in y: {}\n", m_pixels_y); + LOG(logDEBUG) << "Pixels in y: " << m_pixels_y; m_pixels_x = h5_get_scalar_dataset( file, std::string(metadata_group_name + "Number of pixels in x axis")); - // fmt::print("Pixels in x: {}\n", m_pixels_x); + LOG(logDEBUG) << "Pixels in x: " << m_pixels_x; // Max Frames per File m_max_frames_per_file = h5_get_scalar_dataset( file, std::string(metadata_group_name + "Maximum frames per file")); - // fmt::print("Max frames per File: {}\n", m_max_frames_per_file); + LOG(logDEBUG) << "Max frames per File: " << m_max_frames_per_file; // Bit Depth // Not all detectors write the bitdepth but in case @@ -266,27 +239,26 @@ void Hdf5MasterFile::parse_acquisition_metadata( } catch (H5::FileIException &e) { m_bitdepth = 16; } - // fmt::print("Bit Depth: {}\n", m_bitdepth); + LOG(logDEBUG) << "Bit Depth: " << m_bitdepth; H5Eset_auto(H5E_DEFAULT, reinterpret_cast(H5Eprint2), stderr); // Total Frames m_total_frames_expected = h5_get_scalar_dataset( file, std::string(metadata_group_name + "Total Frames")); - // fmt::print("Total Frames: {}\n", m_total_frames_expected); + LOG(logDEBUG) << "Total Frames: " << m_total_frames_expected; // Frame Padding m_frame_padding = h5_get_scalar_dataset( file, std::string(metadata_group_name + "Frame Padding")); - // fmt::print("Frame Padding: {}\n", m_frame_padding); + LOG(logDEBUG) << "Frame Padding: " << m_frame_padding; // Frame Discard Policy m_frame_discard_policy = StringTo(h5_get_scalar_dataset( file, std::string(metadata_group_name + "Frame Discard Policy"))); - // fmt::print("Frame Discard Policy: {}\n", - // (ToString(m_frame_discard_policy))); + LOG(logDEBUG) << "Frame Discard Policy: " << ToString(m_frame_discard_policy); // Number of rows // Not all detectors write the Number of rows but in case @@ -297,7 +269,7 @@ void Hdf5MasterFile::parse_acquisition_metadata( } catch (H5::FileIException &e) { // keep the optional empty } - // fmt::print("Number of rows: {}\n", m_number_of_rows); + LOG(logDEBUG) << "Number of rows: " << m_number_of_rows; H5Eset_auto(H5E_DEFAULT, reinterpret_cast(H5Eprint2), stderr); @@ -313,7 +285,7 @@ void Hdf5MasterFile::parse_acquisition_metadata( // to try to decode analog samples (Old Moench03) m_analog_flag = 1; } - // fmt::print("Analog Flag: {}\n", m_analog_flag); + LOG(logDEBUG) << "Analog Flag: " << m_analog_flag; H5Eset_auto(H5E_DEFAULT, reinterpret_cast(H5Eprint2), stderr); @@ -331,7 +303,7 @@ void Hdf5MasterFile::parse_acquisition_metadata( } H5Eset_auto(H5E_DEFAULT, reinterpret_cast(H5Eprint2), stderr); - // fmt::print("Analog Samples: {}\n", m_analog_samples); + LOG(logDEBUG) << "Analog Samples: " << m_analog_samples; //----------------------------------------------------------------- // Quad @@ -342,23 +314,23 @@ void Hdf5MasterFile::parse_acquisition_metadata( } catch (H5::FileIException &e) { // keep the optional empty } - // fmt::print("Quad: {}\n", m_quad); + LOG(logDEBUG) << "Quad: " << m_quad; H5Eset_auto(H5E_DEFAULT, reinterpret_cast(H5Eprint2), stderr); // ADC Mask - // H5::Exception::dontPrint(); - // try { - // m_adc_mask = h5_get_scalar_dataset( - // file, std::string(metadata_group_name + "ADC - // Mask")); - // } catch (H5::FileIException &e) { - // m_adc_mask = 0; - // } - // fmt::print("ADC Mask: {}\n", m_adc_mask); - // H5Eset_auto(H5E_DEFAULT, - // reinterpret_cast(H5Eprint2), - // stderr); + H5::Exception::dontPrint(); + try { + m_adc_mask = h5_get_scalar_dataset( + file, std::string(metadata_group_name + "ADC + Mask")); + } catch (H5::FileIException &e) { + m_adc_mask = 0; + } + LOG(logDEBUG) << "ADC Mask: " << m_adc_mask; + H5Eset_auto(H5E_DEFAULT, reinterpret_cast(H5Eprint2), + stderr); + // Digital Flag, Digital Samples H5::Exception::dontPrint(); @@ -372,8 +344,8 @@ void Hdf5MasterFile::parse_acquisition_metadata( } catch (H5::FileIException &e) { // keep the optional empty } - // fmt::print("Digital Flag: {}\n", m_digital_flag); - // fmt::print("Digital Samples: {}\n", m_digital_samples); + LOG(logDEBUG) << "Digital Flag: " << m_digital_flag; + LOG(logDEBUG) << "Digital Samples: " << m_digital_samples; H5Eset_auto(H5E_DEFAULT, reinterpret_cast(H5Eprint2), stderr); @@ -390,23 +362,23 @@ void Hdf5MasterFile::parse_acquisition_metadata( } catch (H5::FileIException &e) { // keep the optional empty } - // fmt::print("Transceiver Flag: {}\n", m_transceiver_flag); - // fmt::print("Transceiver Samples: {}\n", - // m_transceiver_samples); + LOG(logDEBUG) << "Transceiver Flag: " << m_transceiver_flag; + LOG(logDEBUG) << "Transceiver Samples: ",m_transceiver_samples; H5Eset_auto(H5E_DEFAULT, reinterpret_cast(H5Eprint2), stderr); // scan parameters - /*try{ - std::string scan_parameters = j.at("Scan Parameters"); + try { + std::string scan_parameters = h5_get_scalar_dataset( + file, std::string(metadata_group_name + "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 + if (dVersion < 7.21){ + m_scan_parameters.increment_stop(); //adjust for endpoint being included + } + } catch (H5::FileIException &e) { + // keep the optional empty } + LOG(logDEBUG) << "Scan Parameters: " << ToString(m_scan_parameters); try{ ROI tmp_roi; diff --git a/src/RawMasterFile.cpp b/src/RawMasterFile.cpp index 8a2db87..1a44d96 100644 --- a/src/RawMasterFile.cpp +++ b/src/RawMasterFile.cpp @@ -64,34 +64,6 @@ const std::string &RawFileNameComponents::base_name() const { const std::string &RawFileNameComponents::ext() const { return m_ext; } int RawFileNameComponents::file_index() const { return m_file_index; } -// "[enabled\ndac dac 4\nstart 500\nstop 2200\nstep 5\nsettleTime 100us\n]" -ScanParameters::ScanParameters(const std::string& par){ - std::istringstream iss(par.substr(1, par.size()-2)); - std::string line; - while(std::getline(iss, line)){ - if(line == "enabled"){ - m_enabled = true; - }else if(line.find("dac") != std::string::npos){ - m_dac = line.substr(4); - }else if(line.find("start") != std::string::npos){ - m_start = std::stoi(line.substr(6)); - }else if(line.find("stop") != std::string::npos){ - m_stop = std::stoi(line.substr(5)); - }else if(line.find("step") != std::string::npos){ - m_step = std::stoi(line.substr(5)); - } - } -} - -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; } - RawMasterFile::RawMasterFile(const std::filesystem::path &fpath) : m_fnc(fpath) { diff --git a/src/defs.cpp b/src/defs.cpp index 43d2c67..2e839eb 100644 --- a/src/defs.cpp +++ b/src/defs.cpp @@ -1,6 +1,7 @@ #include "aare/defs.hpp" #include #include +#include #include namespace aare { @@ -153,6 +154,31 @@ template <> FrameDiscardPolicy StringTo(const std::string &arg) { throw std::runtime_error("Could not decode frame discard policy from: \"" + arg + "\""); } +/** + * @brief Convert a ScanParameters to a string + * @param type ScanParameters + * @return string representation of the ScanParameters + */ +template <> std::string ToString(ScanParameters arg) { + std::ostringstream oss; + oss << '['; + if (arg.enable) { + oss << "enabled" << std::endl + << "dac " << ToString(arg.dacInd) << std::endl + << "start " << arg.startOffset << std::endl + << "stop " << arg.stopOffset << std::endl + << "step " << arg.stepSize << std::endl + << "settleTime " + << ToString(std::chrono::nanoseconds{arg.dacSettleTime_ns}) + << std::endl; + } else { + oss << "disabled"; + } + oss << ']'; + return oss.str(); +} + + // template <> TimingMode StringTo(std::string mode); } // namespace aare \ No newline at end of file