moved reading of metadata into NumpyFile

This commit is contained in:
Erik Frojdh
2024-04-02 15:01:56 +02:00
parent 390ceb02d8
commit a6c0879de7
5 changed files with 82 additions and 126 deletions

View File

@ -4,8 +4,13 @@
namespace aare{
NumpyFile::NumpyFile(const std::filesystem::path& fname) {
//TODO! add opts to constructor
m_fname = fname;
fp = fopen(m_fname.c_str(), "rb");
if (!fp) {
throw std::runtime_error(fmt::format("Could not open: {} for reading", m_fname.c_str()));
}
load_metadata();
}
NumpyFile::NumpyFile(FileConfig config, header_t header) {
mode = "w";
@ -102,4 +107,77 @@ NumpyFile::~NumpyFile() {
}
}
void NumpyFile::load_metadata(){
// auto file = dynamic_cast<NumpyFile *>(_file);
// // open ifsteam to file
// f = std::ifstream(file->m_fname, std::ios::binary);
// // check if file exists
// if (!f.is_open()) {
// throw std::runtime_error(fmt::format("Could not open: \"{}\" for reading", file->m_fname.c_str()));
// }
// read magic number
std::array<char, 6> tmp{};
fread(tmp.data(), tmp.size(), 1, fp);
if (tmp != aare::NumpyHelpers::magic_str) {
for (auto item : tmp)
fmt::print("{}, ", int(item));
fmt::print("\n");
throw std::runtime_error("Not a numpy file");
}
// read version
fread(reinterpret_cast<char *>(&major_ver_),sizeof(major_ver_), 1,fp);
fread(reinterpret_cast<char *>(&minor_ver_), sizeof(minor_ver_),1,fp);
if (major_ver_ == 1) {
header_len_size = 2;
} else if (major_ver_ == 2) {
header_len_size = 4;
} else {
throw std::runtime_error("Unsupported numpy version");
}
// read header length
fread(reinterpret_cast<char *>(&header_len), header_len_size,1, fp);
header_size = aare::NumpyHelpers::magic_string_length + 2 + header_len_size + header_len;
if (header_size % 16 != 0) {
fmt::print("Warning: header length is not a multiple of 16\n");
}
// read header
auto buf_v = std::vector<char>(header_len);
fread(buf_v.data(), header_len,1,fp);
std::string header(buf_v.data(), header_len);
// parse header
std::vector<std::string> keys{"descr", "fortran_order", "shape"};
aare::logger::debug("original header: \"header\"");
auto dict_map = aare::NumpyHelpers::parse_dict(header, keys);
if (dict_map.size() == 0)
throw std::runtime_error("invalid dictionary in header");
std::string descr_s = dict_map["descr"];
std::string fortran_s = dict_map["fortran_order"];
std::string shape_s = dict_map["shape"];
std::string descr = aare::NumpyHelpers::parse_str(descr_s);
aare::DType dtype = aare::NumpyHelpers::parse_descr(descr);
// convert literal Python bool to C++ bool
bool fortran_order = aare::NumpyHelpers::parse_bool(fortran_s);
// parse the shape tuple
auto shape_v = aare::NumpyHelpers::parse_tuple(shape_s);
shape_t shape;
for (auto item : shape_v) {
auto dim = static_cast<unsigned long>(std::stoul(item));
shape.push_back(dim);
}
m_header = {dtype, fortran_order, shape};
}
} // namespace aare