opatIO-cpp 0.3.0a
Open Parametrized Array Table
Loading...
Searching...
No Matches
opatIO.h
Go to the documentation of this file.
1/* ***********************************************************************
2//
3// Copyright (C) 2025 -- The 4D-STAR Collaboration
4// File Author: Emily Boudreaux
5// Last Modified: March 07, 2025
6//
7// 4DSSE is free software; you can use it and/or modify
8// it under the terms and restrictions the GNU General Library Public
9// License version 3 (GPLv3) as published by the Free Software Foundation.
10//
11// 4DSSE is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14// See the GNU Library General Public License for more details.
15//
16// You should have received a copy of the GNU Library General Public License
17// along with this software; if not, write to the Free Software
18// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19//
20// *********************************************************************** */
21
48
49#ifndef OPATIO_H
50#define OPATIO_H
51
52#include <fstream>
53#include <memory>
54#include <string>
55#include <sys/types.h>
56#include <vector>
57#include <utility>
58#include <cstdint>
59#include <unordered_map>
60#include <limits>
61
62#include "indexVector.h"
63
64namespace opat {
65
72#pragma pack(1)
73struct Header {
74 char magic[4];
75 uint16_t version;
76 uint32_t numTables;
77 uint32_t headerSize;
78 uint64_t indexOffset;
79 char creationDate[16];
80 char sourceInfo[64];
81 char comment[128];
82 uint16_t numIndex;
83 uint8_t hashPrecision;
84 char reserved[23];
85
92 friend std::ostream& operator<<(std::ostream& os, const Header& header);
93
94 void print() const;
95};
96#pragma pack()
97
104#pragma pack(1)
106 char magic[4];
107 uint32_t numTables;
108 uint32_t headerSize;
109 uint64_t indexOffset;
110 uint64_t cardSize;
111 char comment[128];
112 char reserved[100];
113
120 friend std::ostream& operator<<(std::ostream& os, const CardHeader& header);
121};
122#pragma pack()
123
132 uint64_t byteStart;
133 uint64_t byteEnd;
134 char sha256[32];
135
142 friend std::ostream& operator<<(std::ostream& os, const CardCatalogEntry& entry);
143};
144
151 std::unordered_map<FloatIndexVector, CardCatalogEntry> tableIndex;
152
159 friend std::ostream& operator<<(std::ostream& os, const CardCatalog& catalog);
160};
161
162#pragma pack(1)
169 char tag[8];
170 uint64_t byteStart;
171 uint64_t byteEnd;
172 uint16_t numColumns;
173 uint16_t numRows;
174 char columnName[8];
175 char rowName[8];
176 uint64_t size;
177 char reserved[12];
178
185 friend std::ostream& operator<<(std::ostream& os, const TableIndexEntry& entry);
186};
187#pragma pack()
188
195 std::unordered_map<std::string, TableIndexEntry> tableIndex;
196
203 friend std::ostream& operator<<(std::ostream& os, const TableIndex& index);
204
211 [[nodiscard]] const TableIndexEntry& get(const std::string& tag) const;
212
219 const TableIndexEntry& operator[](const std::string& tag) const;
220};
221
239struct Slice {
240 uint32_t start;
241 uint32_t end;
242
249 friend std::ostream& operator<<(std::ostream& os, const Slice& slice);
250};
251
252
259struct OPATTable {
260 std::unique_ptr<double[]> rowValues;
261 std::unique_ptr<double[]> columnValues;
262 std::unique_ptr<double[]> data;
263
264 uint32_t N_R;
265 uint32_t N_C;
266 uint64_t m_vsize;
267
272 [[nodiscard]] std::pair<double, double> size() const { return std::make_pair(N_R, N_C); }
273
281 [[nodiscard]] int vsize() const { return static_cast<int>(m_vsize); }
282
292 const double& operator()() const;
293
303 OPATTable operator()(uint32_t row, uint32_t column) const;
304
313 double operator()(uint32_t row, uint32_t column, uint64_t zdepth) const;
314
322 OPATTable operator()(const Slice& rowSlice, const Slice& colSlice) const;
323
333 [[nodiscard]] OPATTable getData(uint32_t row, uint32_t column) const;
334
343 [[nodiscard]] double getData(uint32_t row, uint32_t column, uint64_t zdepth) const;
344
351 [[nodiscard]] OPATTable getRow(uint32_t row) const;
352
359 [[nodiscard]] OPATTable getColumn(uint32_t column) const;
360
365 [[nodiscard]] OPATTable getRowValues() const;
366
371 [[nodiscard]] OPATTable getColumnValues() const;
372
379 [[nodiscard]] const double* getRawData() const;
380
388 [[nodiscard]] OPATTable slice(const Slice& rowSlice, const Slice& colSlice) const;
389
394 [[nodiscard]] std::string ascii() const;
395
399 void print() const;
400
401 friend std::ostream& operator<<(std::ostream& os, const OPATTable& table);
402};
403
409struct DataCard {
412 std::unordered_map<std::string, OPATTable> tableData;
413
420 friend std::ostream& operator<<(std::ostream& os, const DataCard& card);
421
428 [[nodiscard]] const OPATTable& get(const std::string& tag) const;
429
436 const OPATTable& operator[](const std::string& tag) const;
437
444 const OPATTable& operator[](const char* tag) const;
445
452 const OPATTable& operator[](std::string_view tag) const;
453
469 [[nodiscard]] std::vector<std::string> getKeys() const;
470};
471
485struct Bounds {
486 double min = std::numeric_limits<double>::max();
487 double max = std::numeric_limits<double>::min();
494 friend std::ostream& operator<<(std::ostream& os, const Bounds& bounds);
495};
496
502struct OPAT {
505 std::unordered_map<FloatIndexVector, DataCard> cards;
506
513 friend std::ostream& operator<<(std::ostream& os, const OPAT& opat);
514
521 [[nodiscard]] const DataCard& get(const FloatIndexVector& index) const;
522
538 [[nodiscard]] const DataCard& get(const std::vector<double>& index) const {
539 return get(FloatIndexVector(index));
540 }
541
548 const DataCard& operator[](const FloatIndexVector& index) const;
549
565 const DataCard& operator[](const std::vector<double>& index) const {
566 return get(FloatIndexVector(index));
567 }
568
584 [[nodiscard]] std::vector<Bounds> getBounds() const;
585};
586
603OPAT readOPAT(const std::string& filename);
604
622Header readHeader(std::ifstream &file);
623
645CardCatalogEntry readCardCatalogEntry(std::ifstream &file, uint64_t offset, uint16_t numIndex, uint8_t hashPrecision);
646
666CardCatalog readCardCatalog(std::ifstream &file, const Header &header);
667
688std::unordered_map<FloatIndexVector, DataCard> readDataCards(std::ifstream &file, const Header &header, const CardCatalog &cardCatalog);
689
707DataCard readDataCard(std::ifstream &file, const CardCatalogEntry &entry);
708
727CardHeader readDataCardHeader(std::ifstream &file, const CardCatalogEntry &entry);
728
749TableIndex readTableIndex(std::ifstream &file, const CardCatalogEntry &entry, const CardHeader &header);
750
771OPATTable readOPATTable(std::ifstream &file, const CardCatalogEntry &cardEntry, const TableIndexEntry &tableEntry);
772
790bool hasMagic(const std::string& filename);
791
807bool is_big_endian();
808
825template <typename T>
826T swap_bytes(T value) {
827 static_assert(std::is_trivially_copyable_v<T>, "swap_bytes only supports trivial types.");
828 T result;
829 auto src = reinterpret_cast<uint8_t*>(&value);
830 auto* dest = reinterpret_cast<uint8_t*>(&result);
831 for (size_t i = 0; i < sizeof(T); i++) {
832 dest[i] = src[sizeof(T) - 1 - i];
833 }
834 return result;
835}
836
837} // namespace opat
838
839#endif
Definition indexVector.h:38
Header file defining the FloatIndexVector class for handling floating-point index vectors.
Definition opatIO.cpp:37
TableIndex readTableIndex(std::ifstream &file, const CardCatalogEntry &entry, const CardHeader &header)
Reads the TableIndex from a DataCard.
Definition opatIO.cpp:209
bool hasMagic(const std::string &filename)
Checks if a file has the correct magic number for an OPAT file.
Definition opatIO.cpp:46
CardHeader readDataCardHeader(std::ifstream &file, const CardCatalogEntry &entry)
Reads the header of a DataCard from the file.
Definition opatIO.cpp:192
OPATTable readOPATTable(std::ifstream &file, const CardCatalogEntry &cardEntry, const TableIndexEntry &tableEntry)
Reads an OPATTable from the file.
Definition opatIO.cpp:231
CardCatalogEntry readCardCatalogEntry(std::ifstream &file, uint64_t offset, uint16_t numIndex, uint8_t hashPrecision)
Reads a CardCatalogEntry from the file.
Definition opatIO.cpp:127
bool is_big_endian()
Determines if the system is big-endian.
Definition opatIO.cpp:40
CardCatalog readCardCatalog(std::ifstream &file, const Header &header)
Reads the CardCatalog from the file.
Definition opatIO.cpp:149
T swap_bytes(T value)
Swaps the byte order of a value.
Definition opatIO.h:826
DataCard readDataCard(std::ifstream &file, const CardCatalogEntry &entry)
Reads a single DataCard from the file.
Definition opatIO.cpp:173
OPAT readOPAT(const std::string &filename)
Reads an OPAT file and returns its contents as an OPAT structure.
Definition opatIO.cpp:76
Header readHeader(std::ifstream &file)
Reads the header of an OPAT file.
Definition opatIO.cpp:109
std::unordered_map< FloatIndexVector, DataCard > readDataCards(std::ifstream &file, const Header &header, const CardCatalog &cardCatalog)
Reads all DataCards from the file.
Definition opatIO.cpp:163
Structure to hold the minimum and maximum values for a dimension.
Definition opatIO.h:485
friend std::ostream & operator<<(std::ostream &os, const Bounds &bounds)
Stream insertion operator for printing the Bounds.
Definition opatIO.cpp:584
double max
The maximum value.
Definition opatIO.h:487
double min
The minimum value.
Definition opatIO.h:486
Structure to hold the index information of a table in an OPAT file.
Definition opatIO.h:130
friend std::ostream & operator<<(std::ostream &os, const CardCatalogEntry &entry)
Stream insertion operator for printing the catalog entry.
Definition opatIO.cpp:526
char sha256[32]
SHA-256 hash of the table data for integrity verification.
Definition opatIO.h:134
uint64_t byteStart
Byte start position of the table in the file.
Definition opatIO.h:132
uint64_t byteEnd
Byte end position of the table in the file.
Definition opatIO.h:133
FloatIndexVector index
Index vector for the associated table.
Definition opatIO.h:131
Structure to hold the catalog of tables in a DataCard.
Definition opatIO.h:150
friend std::ostream & operator<<(std::ostream &os, const CardCatalog &catalog)
Stream insertion operator for printing the card catalog.
Definition opatIO.cpp:541
std::unordered_map< FloatIndexVector, CardCatalogEntry > tableIndex
Map of index vectors to catalog entries.
Definition opatIO.h:151
Structure to hold the header information of a DataCard in an OPAT file.
Definition opatIO.h:105
uint32_t numTables
Number of tables in the card.
Definition opatIO.h:107
uint32_t headerSize
Size of the card header in bytes.
Definition opatIO.h:108
uint64_t cardSize
Total size of the card in bytes.
Definition opatIO.h:110
char reserved[100]
Reserved for future use.
Definition opatIO.h:112
char comment[128]
User-defined comment section.
Definition opatIO.h:111
char magic[4]
Magic number to identify the card type.
Definition opatIO.h:106
uint64_t indexOffset
Offset to the index section within the card.
Definition opatIO.h:109
friend std::ostream & operator<<(std::ostream &os, const CardHeader &header)
Stream insertion operator for printing the card header.
Definition opatIO.cpp:516
Structure to hold a DataCard, which contains multiple tables.
Definition opatIO.h:409
CardHeader header
Header of the DataCard.
Definition opatIO.h:410
std::unordered_map< std::string, OPATTable > tableData
Map of table tags to their data.
Definition opatIO.h:412
TableIndex tableIndex
Index of tables within the DataCard.
Definition opatIO.h:411
std::vector< std::string > getKeys() const
Retrieves a list of all table tags (keys) present in this DataCard.
Definition opatIO.cpp:302
const OPATTable & operator[](const std::string &tag) const
Accesses a table from the DataCard by tag.
Definition opatIO.cpp:292
friend std::ostream & operator<<(std::ostream &os, const DataCard &card)
Stream insertion operator for printing the DataCard.
Definition opatIO.cpp:569
const OPATTable & get(const std::string &tag) const
Retrieves a table from the DataCard by tag.
Definition opatIO.cpp:284
Structure to hold the header information of an OPAT file.
Definition opatIO.h:73
char reserved[23]
Reserved for future use.
Definition opatIO.h:84
char creationDate[16]
Creation date of the file (e.g., "YYYY-MM-DD HH:MM").
Definition opatIO.h:79
char magic[4]
Magic number to identify the file type.
Definition opatIO.h:74
uint32_t headerSize
Size of the header in bytes.
Definition opatIO.h:77
uint16_t numIndex
Size of the index vector per table.
Definition opatIO.h:82
uint8_t hashPrecision
Precision of the hash used for table validation.
Definition opatIO.h:83
uint16_t version
Version of the OPAT file format.
Definition opatIO.h:75
char sourceInfo[64]
Source information (e.g., software version or author).
Definition opatIO.h:80
uint64_t indexOffset
Offset to the index section.
Definition opatIO.h:78
uint32_t numTables
Number of tables in the file.
Definition opatIO.h:76
friend std::ostream & operator<<(std::ostream &os, const Header &header)
Stream insertion operator for printing the header.
Definition opatIO.cpp:507
char comment[128]
User-defined comment section.
Definition opatIO.h:81
void print() const
Prints the header information to the console.
Definition opatIO.cpp:257
Structure to hold the entire OPAT file.
Definition opatIO.h:502
Header header
Header of the OPAT file.
Definition opatIO.h:503
std::unordered_map< FloatIndexVector, DataCard > cards
Map of index vectors to DataCards.
Definition opatIO.h:505
const DataCard & get(const std::vector< double > &index) const
Retrieves a DataCard from the OPAT structure by a standard vector of doubles. This is a convenience o...
Definition opatIO.h:538
CardCatalog cardCatalog
Catalog of DataCards in the file.
Definition opatIO.h:504
const DataCard & operator[](const std::vector< double > &index) const
Accesses a DataCard from the OPAT structure by a standard vector of doubles. This is a convenience ov...
Definition opatIO.h:565
std::vector< Bounds > getBounds() const
Calculates and returns the bounds (min and max values) for each dimension of the index vectors in the...
Definition opatIO.cpp:59
friend std::ostream & operator<<(std::ostream &os, const OPAT &opat)
Stream insertion operator for printing the OPAT structure.
Definition opatIO.cpp:574
const DataCard & get(const FloatIndexVector &index) const
Retrieves a DataCard from the OPAT structure by index.
Definition opatIO.cpp:271
const DataCard & operator[](const FloatIndexVector &index) const
Accesses a DataCard from the OPAT structure by index.
Definition opatIO.cpp:280
Structure to hold the data of an OPAT table.
Definition opatIO.h:259
const double * getRawData() const
Retrieves the raw data of the table.
Definition opatIO.cpp:451
void print() const
Prints the table to the standard output.
Definition opatIO.cpp:502
uint32_t N_R
Number of rows in the table.
Definition opatIO.h:264
friend std::ostream & operator<<(std::ostream &os, const OPATTable &table)
Definition opatIO.cpp:564
std::pair< double, double > size() const
Returns the size of the table as a pair of rows and columns.
Definition opatIO.h:272
OPATTable slice(const Slice &rowSlice, const Slice &colSlice) const
Extracts a slice of the table.
Definition opatIO.cpp:461
std::string ascii() const
Converts the table to an ASCII representation.
Definition opatIO.cpp:491
OPATTable getColumn(uint32_t column) const
Extracts a single column from the table.
Definition opatIO.cpp:386
OPATTable getRowValues() const
Retrieves all row values of the table.
Definition opatIO.cpp:407
std::unique_ptr< double[]> columnValues
Array of column values.
Definition opatIO.h:261
OPATTable getColumnValues() const
Retrieves all column values of the table.
Definition opatIO.cpp:429
const double & operator()() const
Accesses the first value in the table's data array.
Definition opatIO.cpp:332
std::unique_ptr< double[]> rowValues
Array of row values.
Definition opatIO.h:260
uint32_t N_C
Number of columns in the table.
Definition opatIO.h:265
uint64_t m_vsize
Vector size of each cell.
Definition opatIO.h:266
std::unique_ptr< double[]> data
Array of table data.
Definition opatIO.h:262
OPATTable getRow(uint32_t row) const
Extracts a single row from the table.
Definition opatIO.cpp:370
OPATTable getData(uint32_t row, uint32_t column) const
Retrieves a table value by row and column.
Definition opatIO.cpp:349
int vsize() const
Retrieves the vector size of each cell in the table.
Definition opatIO.h:281
Structure to represent a slice of data.
Definition opatIO.h:239
uint32_t start
Start index of the slice.
Definition opatIO.h:240
uint32_t end
End index of the slice.
Definition opatIO.h:241
friend std::ostream & operator<<(std::ostream &os, const Slice &slice)
Stream insertion operator for printing the slice.
Definition opatIO.cpp:579
Structure to hold the index information of a table within a DataCard.
Definition opatIO.h:168
char columnName[8]
Name of the columns (optional).
Definition opatIO.h:174
friend std::ostream & operator<<(std::ostream &os, const TableIndexEntry &entry)
Stream insertion operator for printing the table index entry.
Definition opatIO.cpp:546
uint64_t byteStart
Byte start position of the table in the card.
Definition opatIO.h:170
char reserved[12]
Reserved for future use.
Definition opatIO.h:177
uint16_t numColumns
Number of columns in the table.
Definition opatIO.h:172
uint64_t size
Vector size of each cell.
Definition opatIO.h:176
char tag[8]
Tag identifying the table.
Definition opatIO.h:169
uint16_t numRows
Number of rows in the table.
Definition opatIO.h:173
char rowName[8]
Name of the rows (optional).
Definition opatIO.h:175
uint64_t byteEnd
Byte end position of the table in the card.
Definition opatIO.h:171
Structure to hold the index of tables within a DataCard.
Definition opatIO.h:194
std::unordered_map< std::string, TableIndexEntry > tableIndex
Map of table tags to index entries.
Definition opatIO.h:195
const TableIndexEntry & get(const std::string &tag) const
Retrieves a TableIndexEntry by tag.
Definition opatIO.cpp:311
friend std::ostream & operator<<(std::ostream &os, const TableIndex &index)
Stream insertion operator for printing the table index.
Definition opatIO.cpp:557
const TableIndexEntry & operator[](const std::string &tag) const
Accesses a TableIndexEntry by tag.
Definition opatIO.cpp:319