@@ -74,6 +74,50 @@ inline H5::PredType get_hdf5_standard_type() {
7474 }
7575}
7676
77+ inline H5::PredType get_type (H5::DataSet& dataset) {
78+ H5T_class_t type_class = dataset.getTypeClass ();
79+
80+ if (type_class == H5T_INTEGER) {
81+ H5::IntType intype = dataset.getIntType ();
82+
83+ H5std_string order_string;
84+ H5T_order_t order = intype.getOrder (order_string);
85+
86+ assert (order == H5T_ORDER_LE);
87+
88+ size_t size = intype.getSize ();
89+
90+ if (intype.getSign () == H5T_SGN_NONE &&
91+ size == sizeof (std::uint64_t )) {
92+ return H5::PredType::STD_U64LE;
93+ } else if (intype.getSign () == H5T_SGN_2 &&
94+ size == sizeof (std::int64_t )) {
95+ return H5::PredType::STD_I64LE;
96+ } else {
97+ assert (false );
98+ }
99+ } else if (type_class == H5T_FLOAT) {
100+ H5::FloatType floatype = dataset.getFloatType ();
101+
102+ H5std_string order_string;
103+ H5T_order_t order = floatype.getOrder (order_string);
104+
105+ assert (order == H5T_ORDER_LE);
106+
107+ size_t size = floatype.getSize ();
108+
109+ if (size == sizeof (float )) {
110+ return H5::PredType::IEEE_F32LE;
111+ } else if (size == sizeof (double )) {
112+ return H5::PredType::IEEE_F64LE;
113+ } else {
114+ assert (false );
115+ }
116+ } else {
117+ assert (false );
118+ }
119+ }
120+
77121template <std::ranges::contiguous_range R>
78122void write_dataset (H5::H5File& f, const std::string& label, R&& r) {
79123 using T = std::ranges::range_value_t <R>;
@@ -89,15 +133,17 @@ void write_dataset(H5::H5File& f, const std::string& label, R&& r) {
89133template <typename T, typename Allocator>
90134std::span<T> read_dataset (H5::H5File& f, const std::string& label, Allocator&& alloc) {
91135 H5::DataSet dataset = f.openDataSet (label.c_str ());
136+
92137 H5::DataSpace space = dataset.getSpace ();
93138 hsize_t ndims = space.getSimpleExtentNdims ();
94139 assert (ndims == 1 );
95140 hsize_t dims;
96141 space.getSimpleExtentDims (&dims, &ndims);
142+ space.close ();
143+
97144 T* data = alloc.allocate (dims);
98145 dataset.read (data, get_hdf5_native_type<T>());
99146 dataset.close ();
100- space.close ();
101147 return std::span<T>(data, dims);
102148}
103149
@@ -106,4 +152,11 @@ std::span<T> read_dataset(H5::H5File& f, const std::string& label) {
106152 return read_dataset<T>(f, label, std::allocator<T>{});
107153}
108154
155+ inline H5::PredType dataset_type (H5::H5File& f, const std::string& label) {
156+ H5::DataSet dataset = f.openDataSet (label.c_str ());
157+ auto type = get_type (dataset);
158+ dataset.close ();
159+ return type;
160+ }
161+
109162} // end hdf_tools
0 commit comments