Skip to content
Snippets Groups Projects
Verified Commit 19e2abdc authored by Tamas Gal's avatar Tamas Gal :speech_balloon:
Browse files

Add addmeta() for HDF5 structures

parent 27ee41c8
No related branches found
No related tags found
No related merge requests found
......@@ -38,6 +38,7 @@ SummaryFrame
```@docs
H5File
H5CompoundDataset
addmeta
create_dataset
flush
```
......
......@@ -18,7 +18,7 @@ import UnROOT
using HDF5
export ROOTFile
export H5File, H5CompoundDataset, create_dataset
export H5File, H5CompoundDataset, create_dataset, addmeta
export Direction, Position, UTMPosition, Location, Quaternion, Track, AbstractCalibratedHit
export Detector, DetectorModule, PMT, Tripod, Hydrophone, center, isbasemodule
......
......@@ -16,6 +16,7 @@ struct H5CompoundDataset{T}
end
Base.length(c::H5CompoundDatasetCache) = length(c.buffer)
isfull(c::H5CompoundDatasetCache) = length(c) >= c.size
HDF5.read_attribute(cdset::H5CompoundDataset, name::AbstractString) = HDF5.read_attribute(cdset.dset, name)
"""
......@@ -66,6 +67,7 @@ function Base.write(f::H5File, path::AbstractString, data)
write_dataset(f._h5f, path, data)
end
Base.getindex(f::H5File, args...) = getindex(f._h5f, args...)
HDF5.read_attribute(f::H5File, name::AbstractString) = HDF5.read_attribute(f._h5f, name)
"""
......@@ -88,3 +90,17 @@ function Base.push!(d::H5CompoundDataset{T}, element::T) where T
push!(d.cache.buffer, element)
isfull(d.cache) && flush(d)
end
"""
Attaches key-value-pair meta entries to an HDF5 instance for each field of the
given object.
"""
function addmeta(dset::Union{HDF5.Dataset, HDF5.File, HDF5.Group, HDF5.Datatype}, object::T) where T
for fieldname in fieldnames(T)
attributes(dset)[string(fieldname)] = getfield(object, fieldname)
end
end
addmeta(cdset::H5CompoundDataset, object) = addmeta(cdset.dset, object)
addmeta(f::H5File, object) = addmeta(f._h5f, object)
......@@ -39,5 +39,18 @@ end
write(f, "directly_written_bars", bars)
@test bars == reinterpret(Bar, f["directly_written_bars"][:])
addmeta(d, Bar(1, 2.3))
@test read_attribute(d, "a") == 1
@test read_attribute(d, "b") == 2.3
d2 = create_dataset(f, "foo", Int32; cache_size=1000)
addmeta(d2.dset, Bar(3, 4.5))
@test read_attribute(d2, "a") == 3
@test read_attribute(d2, "b") == 4.5
addmeta(f, Bar(6, 7.8))
@test read_attribute(f, "a") == 6
@test read_attribute(f, "b") == 7.8
close(f)
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment