added CtbRawFile

This commit is contained in:
Erik Fröjdh
2024-11-05 14:36:18 +01:00
parent b8a4498379
commit 80a39415de
24 changed files with 687 additions and 262 deletions

View File

@ -1,19 +1,37 @@
#pragma once
#include "aare/FileInterface.hpp"
#include "aare/file_utils.hpp"
#include "aare/RawMasterFile.hpp"
#include "aare/Frame.hpp"
#include <filesystem>
#include <fstream>
namespace aare{
class CtbRawFile{
FileNameComponents m_fnc{};
RawMasterFile m_master;
std::ifstream m_file;
size_t m_current_frame{0};
size_t m_current_subfile{0};
size_t m_num_subfiles{0};
public:
CtbRawFile(const std::filesystem::path &fname);
void read_into(std::byte *image_buf, DetectorHeader* header = nullptr);
void seek(size_t frame_index); //!< seek to the given frame index
size_t tell() const; //!< get the frame index of the file pointer
// in the specific class we can expose more functionality
size_t image_size_in_bytes() const { return m_master.image_size_in_bytes(); }
private:
void find_subfiles();
size_t sub_file_index(size_t frame_index) const {
return frame_index / m_master.max_frames_per_file();
}
void open_data_file(size_t subfile_index);
};

View File

@ -180,9 +180,9 @@ class RawFile : public FileInterface {
/**
* @brief read the header of the file
* @param fname path to the data subfile
* @return sls_detector_header
* @return DetectorHeader
*/
static sls_detector_header read_header(const std::filesystem::path &fname);
static DetectorHeader read_header(const std::filesystem::path &fname);
/**
* @brief open the subfiles

View File

@ -0,0 +1,96 @@
#pragma once
#include "aare/defs.hpp"
#include <filesystem>
#include <fmt/format.h>
#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace aare {
class RawFileNameComponents {
std::filesystem::path m_base_path{};
std::string m_base_name{};
std::string m_ext{};
int m_file_index{}; // TODO! is this measurement_index?
public:
RawFileNameComponents(const std::filesystem::path &fname);
std::filesystem::path master_fname() const {
return m_base_path /
fmt::format("{}_master_{}{}", m_base_name, m_file_index, m_ext);
}
std::filesystem::path data_fname(size_t mod_id, size_t file_id) {
return m_base_path / fmt::format("{}_d{}_f{}_{}.raw", m_base_name,
mod_id, file_id, m_file_index);
}
const std::filesystem::path &base_path() const { return m_base_path; }
const std::string &base_name() const { return m_base_name; }
const std::string &ext() const { return m_ext; }
int file_index() const { return m_file_index; }
};
class RawMasterFile {
RawFileNameComponents m_fnc;
std::string m_version;
DetectorType m_type;
TimingMode m_timing_mode;
size_t m_image_size_in_bytes;
size_t m_frames_in_file;
size_t m_pixels_y;
size_t m_pixels_x;
size_t m_bitdepth;
size_t m_max_frames_per_file;
uint32_t m_adc_mask;
FrameDiscardPolicy m_frame_discard_policy;
size_t m_frame_padding;
std::optional<size_t> m_analog_samples;
std::optional<size_t> m_digital_samples;
public:
RawMasterFile(const std::filesystem::path &fpath) : m_fnc(fpath) {
if (!std::filesystem::exists(fpath)) {
throw std::runtime_error(LOCATION + " File does not exist");
}
if (m_fnc.ext() == ".json") {
parse_json(fpath);
} else if (m_fnc.ext() == ".raw") {
parse_raw(fpath);
} else {
throw std::runtime_error(LOCATION + "Unsupported file type");
}
}
const std::string &version() const { return m_version; }
const DetectorType &detector_type() const { return m_type; }
const TimingMode &timing_mode() const { return m_timing_mode; }
size_t image_size_in_bytes() const { return m_image_size_in_bytes; }
size_t frames_in_file() const { return m_frames_in_file; }
size_t pixels_y() const { return m_pixels_y; }
size_t pixels_x() const { return m_pixels_x; }
size_t max_frames_per_file() const { return m_max_frames_per_file; }
size_t bitdepth() const { return m_bitdepth; }
size_t frame_padding() const { return m_frame_padding; }
const FrameDiscardPolicy &frame_discard_policy() const {
return m_frame_discard_policy;
}
std::optional<size_t> analog_samples() const { return m_analog_samples; }
std::optional<size_t> digital_samples() const { return m_digital_samples; }
std::filesystem::path data_fname(size_t mod_id, size_t file_id) {
return m_fnc.data_fname(mod_id, file_id);
}
private:
void parse_json(const std::filesystem::path &fpath);
void parse_raw(const std::filesystem::path &fpath);
};
} // namespace aare

View File

@ -15,7 +15,7 @@ namespace aare {
*/
class SubFile {
public:
size_t write_part(std::byte *buffer, sls_detector_header header, size_t frame_index);
size_t write_part(std::byte *buffer, DetectorHeader header, size_t frame_index);
/**
* @brief SubFile constructor
* @param fname path to the subfile

View File

@ -111,7 +111,7 @@ class Cluster {
/**
* @brief header contained in parts of frames
*/
struct sls_detector_header {
struct DetectorHeader {
uint64_t frameNumber;
uint32_t expLength;
uint32_t packetNumber;
@ -166,6 +166,7 @@ using xy = t_xy<uint32_t>;
using dynamic_shape = std::vector<int64_t>;
//TODO! Can we uniform enums between the libraries?
enum class DetectorType {
Jungfrau,
Eiger,
@ -178,6 +179,7 @@ enum class DetectorType {
};
enum class TimingMode { Auto, Trigger };
enum class FrameDiscardPolicy { NoDiscard, Discard };
template <class T> T StringTo(const std::string &arg) { return T(arg); }
@ -188,6 +190,8 @@ template <> std::string ToString(DetectorType arg);
template <> TimingMode StringTo(const std::string & /*mode*/);
template <> FrameDiscardPolicy StringTo(const std::string & /*mode*/);
using DataTypeVariants = std::variant<uint16_t, uint32_t>;
} // namespace aare

View File

@ -1,119 +0,0 @@
#pragma once
#include "aare/defs.hpp"
#include <filesystem>
#include <fmt/format.h>
#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace aare {
bool is_master_file(const std::filesystem::path &fpath);
struct FileNameComponents {
std::filesystem::path base_path{};
std::string base_name{};
std::string ext{};
int findex{};
bool valid{false}; // TODO! how do we do error handling?
std::filesystem::path master_fname() const {
return base_path /
fmt::format("{}_master_{}{}", base_name, findex, ext);
}
std::filesystem::path data_fname(size_t mod_id, size_t file_id) {
return base_path / fmt::format("{}_d{}_f{}_{}.raw", base_name, file_id,
mod_id, findex);
}
};
FileNameComponents parse_fname(const std::filesystem::path &fname);
class MasterFile {
FileNameComponents m_fnc;
std::string m_version;
DetectorType m_type;
TimingMode m_timing_mode;
size_t m_total_frames;
size_t m_subfile_rows;
size_t m_subfile_cols;
size_t m_bitdepth;
size_t m_analog_samples;
size_t m_digital_samples;
size_t m_max_frames_per_file;
uint32_t m_adc_mask;
public:
MasterFile(const std::filesystem::path &fpath) {
m_fnc = parse_fname(fpath);
std::ifstream ifs(fpath);
json j;
ifs >> j;
double v = j["Version"];
m_version = fmt::format("{:.1f}", v);
m_type = StringTo<DetectorType>(j["Detector// Type"].get<std::string>());
m_timing_mode =
StringTo<TimingMode>(j["Timing Mode"].get<std::string>());
m_total_frames = j["Frames in File"];
m_subfile_rows = j["Pixels"]["y"];
m_subfile_cols = j["Pixels"]["x"];
m_max_frames_per_file = j["Max Frames Per File"];
try {
m_bitdepth = j.at("Dynamic Range");
} catch (const json::out_of_range &e) {
m_bitdepth = 16;
}
try {
m_analog_samples = j.at("Analog Samples");
}catch (const json::out_of_range &e) {
m_analog_samples = 0;
}
try{
std::string adc_mask = j.at("ADC Mask");
m_adc_mask = std::stoul(adc_mask, nullptr, 16);
}catch (const json::out_of_range &e) {
m_adc_mask = 0;
}
try {
m_digital_samples = j.at("Digital Samples");
}catch (const json::out_of_range &e) {
m_digital_samples = 0;
}
//Update detector type for Moench
//TODO! How does this work with old .raw master files?
if (m_type == DetectorType::Moench && m_analog_samples == 0 &&
m_subfile_rows == 400) {
m_type = DetectorType::Moench03;
}else if (m_type == DetectorType::Moench && m_subfile_rows == 400 &&
m_analog_samples == 5000) {
m_type = DetectorType::Moench03_old;
}
// //Here we know we have a ChipTestBoard file update the geometry?
// //TODO! Carry on information about digtial, and transceivers
// if (m_type == DetectorType::ChipTestBoard) {
// subfile_rows = 1;
// subfile_cols = m_analog_samples*__builtin_popcount(m_adc_mask);
// }
// // only Eiger had quad
// if (m_type == DetectorType::Eiger) {
// quad = (j["Quad"] == 1);
// }
// m_geometry = {j["Geometry"]["y"], j["Geometry"]["x"]};
}
};
} // namespace aare