GammaLib 2.2.0.dev
Loading...
Searching...
No Matches
GHdf5.hpp
Go to the documentation of this file.
1/***************************************************************************
2 * GHdf5.hpp - HDF5 file handling class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2026 by Juergen Knoedlseder *
5 * ----------------------------------------------------------------------- *
6 * *
7 * This program is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21/**
22 * @file GHdf5.hpp
23 * @brief HDF5 file handling class definition
24 * @author Juergen Knoedlseder
25 */
26
27#ifndef GHDF5_HPP
28#define GHDF5_HPP
29
30/* __ Includes ___________________________________________________________ */
31#include <cstdio> // std::fopen, std::fgets, std::fclose, etc...
32#include <stdint.h> // uint64_t
33#include "GBase.hpp"
34#include "GXml.hpp"
35#include "GFilename.hpp"
36
37/* __ Forward declaration ________________________________________________ */
38class GXmlElement;
39
40/* __ Constants __________________________________________________________ */
41
42/* __ Prototypes ________________________________________________________ */
43namespace gammalib { namespace hdf5 {
44 std::string fread_data(FILE* fptr, const int& nbytes);
45 std::string fread_data(FILE* fptr,
46 const GXmlElement* dataspace,
47 const GXmlElement* datatype,
48 const GXmlElement* datalayout,
49 const GXmlElement* datafilter = NULL);
50 std::string fread_data_chunk(FILE* fptr,
51 const GXmlElement* chunk,
52 const GXmlElement* dataspace,
53 const GXmlElement* datatype,
54 const GXmlElement* datalayout,
55 const GXmlElement* datafilter = NULL);
56 int fread_int(FILE* fptr, const int& nbytes);
57 uint32_t fread_uint32(FILE* fptr, const int& nbytes);
58 uint64_t fread_uint64(FILE* fptr, const int& nbytes);
59 std::string fread_string(FILE* fptr, const int& nbytes);
60 std::string fread_data_as_string(FILE* fptr, const GXmlElement* datatype);
61 void fread_zero(FILE* fptr, const int& nbytes);
62 int data_to_int(const char* data,
63 const GXmlElement* datatype);
64 double data_to_double(const char* data,
65 const GXmlElement* datatype);
66 std::string data_to_string(const char* data,
67 const GXmlElement* datatype);
68 std::string data_filter(const std::string& data,
69 const GXmlElement* datafilter);
70 std::string data_filter_bitshuffle(const std::string& data,
71 const GXmlElement* filter);
72 const GXmlElement* xml_msg_type(const GXmlElement* header,
73 const std::string& type,
74 const int& number = 0);
75 bool xml_has_msg_type(const GXmlElement* header,
76 const std::string& type,
77 const int& number = 0);
78 int xml_msg_attributes(const GXmlElement* header,
79 const std::string& name = "");
80 const GXmlElement* xml_msg_attribute(const GXmlElement* header,
81 const std::string& name,
82 const int& number = 0);
83 bool xml_has_msg_attribute(const GXmlElement* header,
84 const std::string& name,
85 const int& number = 0);
86 std::string xml_msg_attribute(const GXmlElement* header,
87 const std::string& name,
88 const std::string& key);
89 const GXmlElement* xml_msg_dataspace(const GXmlElement* header);
90 const GXmlElement* xml_msg_datatype(const GXmlElement* header);
91 const GXmlElement* xml_msg_datalayout(const GXmlElement* header);
92 const GXmlElement* xml_msg_datafilter(const GXmlElement* header);
93 std::string classname(const int& cls);
94} }
95
96
97/***********************************************************************//**
98 * @class GHdf5
99 *
100 * @brief COSI instrument response function class
101 *
102 * The COSI instrument response function class defines the function
103 * that translates from physical quantities to measured quantities.
104 *
105 * @todo Complete the class description.
106 ***************************************************************************/
107class GHdf5 : public GBase {
108
109public:
110 // Constructors and destructors
111 GHdf5(void);
112 GHdf5(const GFilename& filename);
113 GHdf5(const GHdf5& file);
114 virtual ~GHdf5(void);
115
116 // Operators
117 GHdf5& operator=(const GHdf5 & file);
118
119 // Methods
120 void clear(void);
121 GHdf5* clone(void) const;
122 std::string classname(void) const;
123 bool is_empty(void) const;
124 void load(const GFilename& filename);
125 void read(FILE* fptr);
126 const GXml& xml(void) const;
127 const GXmlElement* xml_hdf5_entry(const std::string& name) const;
128 std::string print(const GChatter& chatter = NORMAL) const;
129
130private:
131 // Private methods
132 void init_members(void);
133 void copy_members(const GHdf5& file);
134 void free_members(void);
135
136 // Private methods used to read HDF5 files
137 void read_superblock(FILE* fptr);
138 void read_symbol_table_node(FILE* fptr,
139 const uint64_t& address,
140 const uint64_t& offset,
141 const std::string& heap,
142 GXmlElement* table,
143 const int& indent);
144 void read_symbol_table_entry(FILE* fptr,
145 const uint64_t& address,
146 const uint64_t& offset,
147 const std::string& heap,
148 GXmlElement* entry,
149 const int& indent);
150 void read_group(FILE* fptr,
151 const uint64_t& address_btree,
152 const uint64_t& address_heap,
153 GXmlElement* group,
154 const int& indent);
155 void read_btree_chunked(FILE* fptr,
156 const uint64_t& address,
157 const int& dimensions,
158 GXmlElement* btree);
159 void read_object_header(FILE* fptr,
160 const uint64_t& address,
161 GXmlElement* header,
162 const int& indent);
163 void read_object_header_v1(FILE* fptr,
164 GXmlElement* header,
165 const int& indent);
166 void read_object_header_v2(FILE* fptr,
167 GXmlElement* header,
168 const int& indent);
169 void read_object_header_message(FILE* fptr,
170 const int& type,
171 GXmlElement* message,
172 const int& indent);
173 void read_message_dataspace(FILE* fptr,
174 GXmlElement* message,
175 std::string& msg);
176 void read_message_datatype(FILE* fptr,
177 GXmlElement* message,
178 std::string& msg);
179 void read_message_layout(FILE* fptr,
180 GXmlElement* message,
181 std::string& msg);
182 void read_message_filter(FILE* fptr,
183 GXmlElement* message,
184 std::string& msg);
185 void read_message_attribute(FILE* fptr,
186 GXmlElement* message,
187 std::string& msg);
188 std::string global_heap_string(FILE* fptr,
189 const uint64_t& address,
190 const int& id,
191 const uint32_t& length);
192
193 // Private support methods
194 void debug_msg(const std::string& msg, const int& indent);
195 std::string hexaddress(const uint64_t& address);
196 std::string extract(const std::string& string, const int& pos);
197 int pad2eight(const int& nbytes);
198
199 // Private data members
200 GFilename m_filename; // File name from which data were loaded
201 int m_offset; // Size of Offsets
202 int m_length; // Size of Lengths
203 GXml m_xml; // Metadata parsed from HDF5 file
204};
205
206
207/***********************************************************************//**
208 * @brief Return class name
209 *
210 * @return String containing the class name ("GHdf5").
211 ***************************************************************************/
212inline
213std::string GHdf5::classname(void) const
214{
215 return ("GHdf5");
216}
217
218
219/***********************************************************************//**
220 * @brief Signals if HDF5 instance is empty
221 *
222 * @return True if HDF5 instance is empty.
223 ***************************************************************************/
224inline
225bool GHdf5::is_empty(void) const
226{
227 return (m_xml.is_empty());
228}
229
230
231/***********************************************************************//**
232 * @brief Return HDF5 metadata as XML object
233 *
234 * @return HDF5 metadata XML object.
235 *
236 * Returns the XML object that holds the HDF5 file metadata that is gathered
237 * when loading a COSI response file. This method is mainly used for
238 * diagnostic purposes.
239 ***************************************************************************/
240inline
241const GXml& GHdf5::xml(void) const
242{
243 return (m_xml);
244}
245
246
247/***********************************************************************//**
248 * @brief Return "Dataspace" message XML element
249 *
250 * @param[in] header Pointer to XML header element.
251 * @return Pointer to "Dataspace" message XML element.
252 *
253 * Returns a XML pointer to the first header message of type "Dataspace".
254 * If no "Dataspace" header message exists a NULL pointer is returned.
255 ***************************************************************************/
256inline
258{
259 const GXmlElement* element = NULL;
260 if (xml_has_msg_type(header, "Dataspace", 0)) {
261 element = xml_msg_type(header, "Dataspace", 0);
262 }
263 return (element);
264}
265
266
267/***********************************************************************//**
268 * @brief Return "Datatype" message XML element
269 *
270 * @param[in] header Pointer to XML header element.
271 * @return Pointer to "Datatype" message XML element.
272 *
273 * Returns a XML pointer to the first header message of type "Datatype".
274 * If no "Datatype" header message exists a NULL pointer is returned.
275 ***************************************************************************/
276inline
278{
279 const GXmlElement* element = NULL;
280 if (xml_has_msg_type(header, "Datatype", 0)) {
281 element = xml_msg_type(header, "Datatype", 0);
282 }
283 return (element);
284}
285
286
287/***********************************************************************//**
288 * @brief Return "DataStorageLayout" message XML element
289 *
290 * @param[in] header Pointer to XML header element.
291 * @return Pointer to "DataStorageLayout" message XML element.
292 *
293 * Returns a XML pointer to the first header message of type
294 * "DataStorageLayout". If no "DataStorageLayout" header message exists a
295 * NULL pointer is returned.
296 ***************************************************************************/
297inline
299{
300 const GXmlElement* element = NULL;
301 if (xml_has_msg_type(header, "DataStorageLayout", 0)) {
302 element = xml_msg_type(header, "DataStorageLayout", 0);
303 }
304 return (element);
305}
306
307
308/***********************************************************************//**
309 * @brief Return "DataStorageFilterPipeline" message XML element
310 *
311 * @param[in] header Pointer to XML header element.
312 * @return Pointer to "DataStorageFilterPipeline" message XML element.
313 *
314 * Returns a XML pointer to the first header message of type
315 * "DataStorageFilterPipeline". If no "DataStorageFilterPipeline" header
316 * message exists a NULL pointer is returned.
317 ***************************************************************************/
318inline
320{
321 const GXmlElement* element = NULL;
322 if (xml_has_msg_type(header, "DataStorageFilterPipeline", 0)) {
323 element = xml_msg_type(header, "DataStorageFilterPipeline", 0);
324 }
325 return (element);
326}
327
328#endif /* GHDF5_HPP */
Definition of interface for all GammaLib classes.
Filename class interface definition.
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
XML class interface definition.
Interface class for all GammaLib classes.
Definition GBase.hpp:52
Filename class.
Definition GFilename.hpp:62
COSI instrument response function class.
Definition GHdf5.hpp:107
GHdf5 * clone(void) const
Clone instance.
Definition GHdf5.cpp:285
GHdf5 & operator=(const GHdf5 &file)
Assignment operator.
Definition GHdf5.cpp:234
void read_group(FILE *fptr, const uint64_t &address_btree, const uint64_t &address_heap, GXmlElement *group, const int &indent)
Read and handle HDF5 group.
Definition GHdf5.cpp:923
void read_object_header_v2(FILE *fptr, GXmlElement *header, const int &indent)
Read object header version 2.
Definition GHdf5.cpp:1356
void read_object_header_message(FILE *fptr, const int &type, GXmlElement *message, const int &indent)
Read one object header message.
Definition GHdf5.cpp:1380
GXml m_xml
Definition GHdf5.hpp:203
const GXml & xml(void) const
Return HDF5 metadata as XML object.
Definition GHdf5.hpp:241
std::string print(const GChatter &chatter=NORMAL) const
Print HDF5 file.
Definition GHdf5.cpp:407
std::string global_heap_string(FILE *fptr, const uint64_t &address, const int &id, const uint32_t &length)
Get string from global heap.
Definition GHdf5.cpp:2684
void debug_msg(const std::string &msg, const int &indent)
Show debug message.
Definition GHdf5.cpp:2874
const GXmlElement * xml_hdf5_entry(const std::string &name) const
Return XML element to entry tag.
Definition GHdf5.cpp:371
void read_message_attribute(FILE *fptr, GXmlElement *message, std::string &msg)
Read Attribute message.
Definition GHdf5.cpp:2491
void read_symbol_table_node(FILE *fptr, const uint64_t &address, const uint64_t &offset, const std::string &heap, GXmlElement *table, const int &indent)
Read HDF5 symbol table node.
Definition GHdf5.cpp:720
GHdf5(void)
Void constructor.
Definition GHdf5.cpp:156
void read_symbol_table_entry(FILE *fptr, const uint64_t &address, const uint64_t &offset, const std::string &heap, GXmlElement *entry, const int &indent)
Read and handle HDF5 symbol table entry.
Definition GHdf5.cpp:809
void read_message_datatype(FILE *fptr, GXmlElement *message, std::string &msg)
Read datatype message.
Definition GHdf5.cpp:1938
int m_length
Definition GHdf5.hpp:202
std::string extract(const std::string &string, const int &pos)
Extract string from position until next null termination.
Definition GHdf5.cpp:2916
void read_message_layout(FILE *fptr, GXmlElement *message, std::string &msg)
Read Data Storage - Layout message.
Definition GHdf5.cpp:2203
int pad2eight(const int &nbytes)
Pad number of Bytes to 8 Bytes.
Definition GHdf5.cpp:2948
GFilename m_filename
Definition GHdf5.hpp:200
void load(const GFilename &filename)
Load data from HDF5 file into instance.
Definition GHdf5.cpp:301
void read_message_filter(FILE *fptr, GXmlElement *message, std::string &msg)
Read Data Storage - Filter pipeline message.
Definition GHdf5.cpp:2378
void read_object_header_v1(FILE *fptr, GXmlElement *header, const int &indent)
Read object header version 1.
Definition GHdf5.cpp:1249
int m_offset
Definition GHdf5.hpp:201
void copy_members(const GHdf5 &file)
Copy class members.
Definition GHdf5.cpp:464
void clear(void)
Clear instance.
Definition GHdf5.cpp:267
void read(FILE *fptr)
Read data from HDF5 file into instance.
Definition GHdf5.cpp:338
void read_object_header(FILE *fptr, const uint64_t &address, GXmlElement *header, const int &indent)
Read object header.
Definition GHdf5.cpp:1207
void read_btree_chunked(FILE *fptr, const uint64_t &address, const int &dimensions, GXmlElement *btree)
Read HDF5 B-tree for chunked data.
Definition GHdf5.cpp:1073
bool is_empty(void) const
Signals if HDF5 instance is empty.
Definition GHdf5.hpp:225
virtual ~GHdf5(void)
Destructor.
Definition GHdf5.cpp:207
void read_superblock(FILE *fptr)
Read HDF5 file superblock.
Definition GHdf5.cpp:516
std::string hexaddress(const uint64_t &address)
Format address for debugging.
Definition GHdf5.cpp:2897
void free_members(void)
Delete class members.
Definition GHdf5.cpp:480
std::string classname(void) const
Return class name.
Definition GHdf5.hpp:213
void init_members(void)
Initialise class members.
Definition GHdf5.cpp:446
void read_message_dataspace(FILE *fptr, GXmlElement *message, std::string &msg)
Read dataspace message.
Definition GHdf5.cpp:1760
XML element node class.
XML class.
Definition GXml.hpp:172
bool is_empty(void) const
Signals if document has no child nodes.
Definition GXml.hpp:279
std::string data_filter_bitshuffle(const std::string &data, const GXmlElement *filter)
Filter data according to Bitshuffle algorithm.
Definition GHdf5.cpp:3903
const GXmlElement * xml_msg_datalayout(const GXmlElement *header)
Return "DataStorageLayout" message XML element.
Definition GHdf5.hpp:298
const GXmlElement * xml_msg_type(const GXmlElement *header, const std::string &type, const int &number=0)
Return specific occurence of header message of given type.
Definition GHdf5.cpp:4026
std::string fread_data_chunk(FILE *fptr, const GXmlElement *chunk, const GXmlElement *dataspace, const GXmlElement *datatype, const GXmlElement *datalayout, const GXmlElement *datafilter=NULL)
Read data chunk.
Definition GHdf5.cpp:3172
int data_to_int(const char *data, const GXmlElement *datatype)
Convert data into integer value.
Definition GHdf5.cpp:3558
const GXmlElement * xml_msg_datafilter(const GXmlElement *header)
Return "DataStorageFilterPipeline" message XML element.
Definition GHdf5.hpp:319
std::string fread_data(FILE *fptr, const int &nbytes)
Read data from HDF5 file.
Definition GHdf5.cpp:2984
int fread_int(FILE *fptr, const int &nbytes)
Read data as integer from HDF5 file.
Definition GHdf5.cpp:3256
int xml_msg_attributes(const GXmlElement *header, const std::string &name="")
Return number of "Attribute" messages in header.
Definition GHdf5.cpp:4138
void fread_zero(FILE *fptr, const int &nbytes)
Read zero data from HDF5 file.
Definition GHdf5.cpp:3498
double data_to_double(const char *data, const GXmlElement *datatype)
Convert data into double precision floating point value.
Definition GHdf5.cpp:3639
bool xml_has_msg_type(const GXmlElement *header, const std::string &type, const int &number=0)
Checks if specific occurence of header message of given type exists.
Definition GHdf5.cpp:4089
std::string classname(const int &cls)
Convert datatype class into classname.
Definition GHdf5.cpp:4365
const GXmlElement * xml_msg_datatype(const GXmlElement *header)
Return "Datatype" message XML element.
Definition GHdf5.hpp:277
std::string data_to_string(const char *data, const GXmlElement *datatype)
Convert data into string.
Definition GHdf5.cpp:3717
std::string data_filter(const std::string &data, const GXmlElement *datafilter)
Filter data.
Definition GHdf5.cpp:3822
std::string fread_string(FILE *fptr, const int &nbytes)
Read string data from HDF5 file.
Definition GHdf5.cpp:3399
const GXmlElement * xml_msg_attribute(const GXmlElement *header, const std::string &name, const int &number=0)
Return "Attribute" message XML element with specified name.
Definition GHdf5.cpp:4193
const GXmlElement * xml_msg_dataspace(const GXmlElement *header)
Return "Dataspace" message XML element.
Definition GHdf5.hpp:257
uint32_t fread_uint32(FILE *fptr, const int &nbytes)
Read data as unsiged 32 Bit integer from HDF5 file.
Definition GHdf5.cpp:3302
bool xml_has_msg_attribute(const GXmlElement *header, const std::string &name, const int &number=0)
Checks if "Attribute" message XML element with specified name exists.
Definition GHdf5.cpp:4261
uint64_t fread_uint64(FILE *fptr, const int &nbytes)
Read data as unsiged 64 Bit integer from HDF5 file.
Definition GHdf5.cpp:3348
std::string fread_data_as_string(FILE *fptr, const GXmlElement *datatype)
Read data with any data type as a string.
Definition GHdf5.cpp:3456
std::string number(const std::string &noun, const int &number)
Convert singular noun into number noun.
Definition GTools.cpp:1160