STIR 6.5.0
HDF5_utilities.h
Go to the documentation of this file.
1/*
2 Copyright 2026 University College London
3
4 SPDX-License-Identifier: Apache-2.0
5 See STIR/LICENSE.txt for details
6 */
15
16#ifndef __stir_IO_HDF5_utils_H__
17#define __stir_IO_HDF5_utils_H__
18
19#include "stir/common.h"
20#include <string>
21#include <utility>
22#include "H5Cpp.h"
23#include "H5Exception.h"
24
25START_NAMESPACE_STIR
26
32
35{
36typedef std::pair<H5E_auto2_t, void*> HDF5_error_handler_t;
37
38inline HDF5_error_handler_t
39HDF5_turn_off_error_handling()
40{
41 HDF5_error_handler_t h;
42 H5::Exception::getAutoPrint(h.first, &h.second);
43 H5::Exception::dontPrint();
44 return h;
45}
46
47inline void
48HDF5_set_error_handler(HDF5_error_handler_t& h)
49{
50 H5::Exception::setAutoPrint(h.first, h.second);
51}
52
53inline bool
54HDF5_dataset_exists(const H5::H5File& file, const std::string& dataset_name)
55{
56 auto h = HDF5_turn_off_error_handling();
57 const auto ret = H5Lexists(file.getId(), dataset_name.c_str(), H5P_DEFAULT);
58 HDF5_set_error_handler(h);
59 return ret > 0;
60}
61
62inline bool
63HDF5_group_exists(const H5::H5File& file, const std::string& group_name)
64{
65 // call same C function
66 return HDF5_dataset_exists(file, group_name);
67}
68
69inline H5::Group
70get_HDF5_group(const H5::H5File& file, const std::string& group_name)
71{
72 if (!HDF5_group_exists(file, group_name))
73 {
74 H5::Group group;
75 return group;
76 }
77 return file.openGroup(group_name);
78}
79
80inline bool
81is_valid(const H5::H5Object& obj)
82{
83 return obj.getId() != H5I_INVALID_HID;
84}
85
86} // namespace HDF5_utilities
87
89
90END_NAMESPACE_STIR
91
92#endif
basic configuration include file
Various utility functions for easier HDF5 file processing.
Definition HDF5_utilities.h:35