better file structure for better separation

This commit is contained in:
Bechir
2024-03-06 21:08:52 +01:00
parent 1b7ea707ae
commit 22fb8763be
24 changed files with 29 additions and 43 deletions

57
include/common/defs.hpp Normal file
View File

@@ -0,0 +1,57 @@
#pragma once
#include <array>
#include <vector>
#include <sys/types.h>
#include <string_view>
#include <stdexcept>
#include <fmt/format.h>
#include <variant>
typedef struct {
uint64_t frameNumber;
uint32_t expLength;
uint32_t packetNumber;
uint64_t bunchId;
uint64_t timestamp;
uint16_t modId;
uint16_t row;
uint16_t column;
uint16_t reserved;
uint32_t debug;
uint16_t roundRNumber;
uint8_t detType;
uint8_t version;
uint8_t packetMask[64];
}__attribute__((packed)) sls_detector_header;
struct xy {
int row;
int col;
};
// using image_shape = std::array<ssize_t, 2>;
using dynamic_shape = std::vector<ssize_t>;
enum class DetectorType { Jungfrau, Eiger, Mythen3, Moench };
enum class TimingMode {Auto, Trigger};
template<class T>
T StringTo(std::string sv){
return T(sv);
}
template<class T>
std::string toString(T sv){
return T(sv);
}
template <> DetectorType StringTo(std::string);
template <> std::string toString(DetectorType type);
template <> TimingMode StringTo(std::string);
using DataTypeVariants = std::variant<uint16_t, uint32_t>;

37
include/core/Frame.hpp Normal file
View File

@@ -0,0 +1,37 @@
#pragma once
#include <cstddef>
#include <sys/types.h>
#include <cstdint>
#include <bits/unique_ptr.h>
#include <vector>
#include "common/defs.hpp"
/**
* @brief Frame class to represent a single frame of data
* model class
* should be able to work with streams coming from files or network
*/
template <class DataType> class Frame{
public:
ssize_t rows;
ssize_t cols;
DataType* data;
ssize_t bitdepth = sizeof(DataType)*8;
Frame(std::byte* fp, ssize_t rows, ssize_t cols);
DataType get(int row, int col);
~Frame(){
delete[] data;
}
};
typedef Frame<uint16_t> Frame16;
typedef Frame<uint8_t> Frame8;
typedef Frame<uint32_t> Frame32;

79
include/file_io/File.hpp Normal file
View File

@@ -0,0 +1,79 @@
#pragma once
#include "common/defs.hpp"
#include "core/Frame.hpp"
#include "SubFile.hpp"
#include <filesystem>
#include <fmt/core.h>
#include <iostream>
struct RawFileConfig {
int module_gap_row{};
int module_gap_col{};
bool operator==(const RawFileConfig &other) const {
if (module_gap_col != other.module_gap_col)
return false;
if (module_gap_row != other.module_gap_row)
return false;
return true;
}
};
template <DetectorType detector, typename DataType>
class File {
public:
virtual Frame<DataType>* get_frame(int frame_number) = 0;
private:
using config = RawFileConfig;
public:
std::vector<SubFile*> subfiles;
std::filesystem::path fname;
std::filesystem::path base_path;
std::string base_name, ext;
int findex, n_subfiles;
size_t total_frames{};
size_t max_frames_per_file{};
std::string version;
DetectorType type;
TimingMode timing_mode;
int subfile_rows, subfile_cols;
bool quad{false};
ssize_t rows{};
ssize_t cols{};
uint8_t bitdepth{};
using data_type = uint16_t;
std::vector<xy> positions;
config cfg{0, 0};
// File();
~File();
inline size_t bytes_per_frame() const { return rows * cols * bitdepth / 8; }
inline size_t pixels() const { return rows * cols; }
inline void set_config(int row, int col) {
cfg.module_gap_row = row;
cfg.module_gap_col = col;
}
// TODO! Deal with fast quad and missing files
void find_number_of_subfiles() {
int n_mod = 0;
while (std::filesystem::exists(data_fname(++n_mod, 0)))
;
n_subfiles = n_mod;
}
inline std::filesystem::path master_fname() {
return base_path / fmt::format("{}_master_{}{}", base_name, findex, ext);
}
inline std::filesystem::path data_fname(int mod_id, int file_id) {
return base_path / fmt::format("{}_d{}_f{}_{}.raw", base_name, file_id, mod_id, findex);
}
// size_t total_frames();
};

View File

@@ -0,0 +1,25 @@
#pragma once
#include <filesystem>
#include "file_io/File.hpp"
template <DetectorType detector,typename DataType>
class FileFactory{
// Class that will be used to create File objects
// follows the factory pattern
protected:
std::filesystem::path fpath;
public:
static FileFactory<detector,DataType>* get_factory(std::filesystem::path);
// virtual int deleteFile() = 0;
virtual File<detector,DataType>* load_file()=0;//TODO: add option to load all file to memory or keep it on disk
virtual void parse_metadata(File<detector,DataType>*)=0;
void find_geometry(File<detector,DataType>*);
void parse_fname(File<detector,DataType>*);
sls_detector_header read_header(const std::filesystem::path &fname);
};

View File

@@ -0,0 +1,28 @@
#include <filesystem>
#include "file_io/FileFactory.hpp"
#include "file_io/File.hpp"
template <DetectorType detector,typename DataType>
class FileHandler{
private:
std::filesystem::path fpath;
FileFactory<detector,DataType>* fileFactory;
File<detector,DataType>* f;
public:
FileHandler<detector,DataType>(std::filesystem::path fpath){
this->fpath = fpath;
this->fileFactory= FileFactory<detector,DataType>::get_factory(fpath);
this->f= fileFactory->load_file();
delete fileFactory;
}
Frame<DataType>* get_frame(int index){
return f->get_frame(index);
}
~FileHandler(){
delete f;
}
};

View File

@@ -0,0 +1,9 @@
#pragma once
#include "file_io/File.hpp"
#include "core/Frame.hpp"
#include "common/defs.hpp"
template <DetectorType detector, typename DataType>
class JsonFile : public File<detector, DataType> {
Frame<DataType> *get_frame(int frame_number);
};

View File

@@ -0,0 +1,16 @@
#include "file_io/FileFactory.hpp"
template <DetectorType detector,typename DataType>
class JsonFileFactory: public FileFactory<detector,DataType>
{
private:
/* data */
public:
File<detector,DataType>* load_file() override;
void parse_metadata(File<detector,DataType>*) override;
JsonFileFactory<detector,DataType>(std::filesystem::path fpath);
void open_subfiles(File<detector,DataType>*);
};

View File

@@ -0,0 +1,9 @@
#include "file_io/File.hpp"
#include <filesystem>
template<DetectorType detector,typename DataType>
class RawFileFactory{
public:
// RawFileFactory();
// ~RawFileFactory();
File<detector,DataType> loadFile(std::filesystem::path fpath);
};

View File

@@ -0,0 +1,32 @@
#pragma once
#include "common/defs.hpp"
#include <cstdint>
#include <filesystem>
#include <variant>
class SubFile {
protected:
FILE *fp = nullptr;
uint16_t bitdepth;
public:
// pointer to a read_impl function. pointer will be set to the appropriate read_impl function in the constructor
size_t (SubFile::*read_impl)(std::byte *buffer) = nullptr;
size_t read_impl_normal(std::byte *buffer);
template <typename DataType> size_t read_impl_flip(std::byte *buffer);
template <typename DataType> size_t read_impl_reorder(std::byte *buffer);
SubFile(std::filesystem::path fname,DetectorType detector, ssize_t rows, ssize_t cols, uint16_t bitdepth);
size_t get_frame(std::byte *buffer, int frame_number);
// TODO: define the inlines as variables and assign them in constructor
inline size_t bytes_per_frame() { return (bitdepth / 8) * rows * cols; }
inline size_t pixels_per_frame() { return rows * cols; }
std::filesystem::path fname;
ssize_t rows{};
ssize_t cols{};
ssize_t n_frames{};
int sub_file_index_{};
};

View File

@@ -0,0 +1,8 @@
#pragma once
#include "file_io/File.hpp"
#include <filesystem>
#include <fmt/core.h>
bool is_master_file(std::filesystem::path fpath);