OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
Hdf5Util.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2//
3// Copyright 2026 Caleb Buahin
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
39
44
45#ifndef OPENSWMM_HDF5_UTIL_HPP
46#define OPENSWMM_HDF5_UTIL_HPP
47
48#include <hdf5.h>
49
50#include <cstdint>
51#include <string>
52#include <vector>
53
54namespace openswmm::h5io {
55
62class Handle {
63public:
64 enum class Kind { File, Group, Dataset, Space, Type, Attr, Plist };
65
69 Handle() = default;
77 Handle(hid_t id, Kind kind) : id_(id), kind_(kind) {}
78 Handle(const Handle&) = delete;
79 Handle& operator=(const Handle&) = delete;
84 Handle(Handle&& o) noexcept : id_(o.id_), kind_(o.kind_) { o.id_ = -1; }
91 Handle& operator=(Handle&& o) noexcept {
92 if (this != &o) {
93 close();
94 id_ = o.id_;
95 kind_ = o.kind_;
96 o.id_ = -1;
97 }
98 return *this;
99 }
100
104 ~Handle() { close(); }
105
111 hid_t get() const noexcept { return id_; }
116 bool ok() const noexcept { return id_ >= 0; }
121 explicit operator bool() const noexcept { return ok(); }
122
129
136 void close() noexcept;
137
138private:
139 hid_t id_ = -1;
140 Kind kind_ = Kind::File;
141};
142
144
151
154
159Handle open_file_readonly(const std::string& path);
160
162
169std::string column_units(hid_t group, const std::string& column);
170
172
179Handle make_group(hid_t loc, const std::string& name);
186Handle open_group(hid_t loc, const std::string& name);
187
189
197bool has_link(hid_t loc, const std::string& name);
198
199// ---------------------------------------------------------------------------
200// Attributes โ€” scalars on a group or on the file root.
201// ---------------------------------------------------------------------------
211bool write_attr(hid_t loc, const std::string& name, const std::string& value);
221bool write_attr(hid_t loc, const std::string& name, double value);
231bool write_attr(hid_t loc, const std::string& name, int value);
232
243bool read_attr(hid_t loc, const std::string& name, std::string& out);
254bool read_attr(hid_t loc, const std::string& name, double& out);
265bool read_attr(hid_t loc, const std::string& name, int& out);
266
267// ---------------------------------------------------------------------------
268// Columns โ€” equal-length 1-D datasets inside a group.
269//
270// `units` and `description` ride along as dataset attributes so the file
271// explains itself without this repository (STRATEGY.md ยง2). Pass an empty
272// string to omit either.
273//
274// An EMPTY column is still written, as a zero-length dataset: a group whose
275// columns disagree on length is the one corruption a columnar reader cannot
276// recover from, so shape is always explicit.
277// ---------------------------------------------------------------------------
292bool write_column(hid_t group, const std::string& name, const std::vector<double>& values,
293 const std::string& units = "", const std::string& description = "",
294 int compression = 4);
295
310bool write_column(hid_t group, const std::string& name, const std::vector<int>& values,
311 const std::string& units = "", const std::string& description = "",
312 int compression = 4);
313
327bool write_column(hid_t group, const std::string& name, const std::vector<std::string>& values,
328 const std::string& description = "", int compression = 4);
329
343bool read_column(hid_t group, const std::string& name, std::vector<double>& out);
357bool read_column(hid_t group, const std::string& name, std::vector<int>& out);
371bool read_column(hid_t group, const std::string& name, std::vector<std::string>& out);
372
374
382std::size_t column_length(hid_t group, const std::string& probe);
383
384} // namespace openswmm::h5io
385
386#endif // OPENSWMM_HDF5_UTIL_HPP
Owns an hid_t and closes it with the right H5*close on scope exit.
Definition Hdf5Util.hpp:62
bool ok() const noexcept
Test whether this owner stores a nonnegative identifier.
Definition Hdf5Util.hpp:116
~Handle()
Close any owned identifier using its stored HDF5 object kind.
Definition Hdf5Util.hpp:104
void close() noexcept
Close any owned identifier and mark this owner empty.
Definition Hdf5Util.cpp:126
Handle()=default
Construct an empty owner with identifier -1 and file close kind.
Handle & operator=(const Handle &)=delete
hid_t get() const noexcept
Inspect the owned identifier without transferring ownership.
Definition Hdf5Util.hpp:111
Handle(const Handle &)=delete
Kind
Definition Hdf5Util.hpp:64
@ Group
Definition Hdf5Util.hpp:64
@ File
Definition Hdf5Util.hpp:64
@ Dataset
Definition Hdf5Util.hpp:64
@ Attr
Definition Hdf5Util.hpp:64
@ Type
Definition Hdf5Util.hpp:64
@ Space
Definition Hdf5Util.hpp:64
@ Plist
Definition Hdf5Util.hpp:64
Handle & operator=(Handle &&o) noexcept
Close this handle and transfer ownership from another.
Definition Hdf5Util.hpp:91
Handle(hid_t id, Kind kind)
Adopt an HDF5 identifier with its matching close operation.
Definition Hdf5Util.hpp:77
Handle(Handle &&o) noexcept
Transfer identifier ownership from another handle.
Definition Hdf5Util.hpp:84
Definition Hdf5InputPlugin.cpp:34
Handle open_file_readonly(const std::string &path)
Open an existing HDF5 file for read-only access.
Definition Hdf5Util.cpp:156
Handle make_group(hid_t loc, const std::string &name)
Create (or open) a group, creating intermediate groups as needed.
Definition Hdf5Util.cpp:182
Handle open_group(hid_t loc, const std::string &name)
Open an existing HDF5 group.
Definition Hdf5Util.cpp:195
bool write_attr(hid_t loc_in, const std::string &name, const std::string &value)
Create or replace a scalar std::string attribute.
Definition Hdf5Util.cpp:226
bool read_attr(hid_t loc_in, const std::string &name, std::string &out)
Read a scalar attribute as std::string.
Definition Hdf5Util.cpp:283
std::size_t column_length(hid_t group, const std::string &probe)
Row count of a group's columns โ€” the length of probe, or 0 if absent.
Definition Hdf5Util.cpp:465
bool write_column(hid_t group, const std::string &name, const std::vector< double > &values, const std::string &units, const std::string &description, int compression)
Create a one-dimensional double column with optional metadata.
Definition Hdf5Util.cpp:347
std::string column_units(hid_t group, const std::string &column)
A column's units attribute, or "" when it has none.
Definition Hdf5Util.cpp:161
bool has_link(hid_t loc, const std::string &name)
True when loc contains name.
Definition Hdf5Util.cpp:200
Handle make_string_type()
A variable-length UTF-8 string type, for name columns.
Definition Hdf5Util.cpp:172
bool read_column(hid_t group, const std::string &name, std::vector< double > &out)
Read a one-dimensional column into a double vector.
Definition Hdf5Util.cpp:414