Here’s my version of how I write this taking advantage of multiple dispatch.
julia> function write_dicts_to_hdf5_groups(
parent::Union{HDF5.File, HDF5.Group},
dict::Dict
)
for (key, value) in dict
write_dicts_to_hdf5_groups(parent, key, value)
end
end
write_dicts_to_hdf5_groups (generic function with 3 methods)
julia> function write_dicts_to_hdf5_groups(
parent::Union{HDF5.File, HDF5.Group},
key::AbstractString,
dict::Dict
)
group = create_group(parent, key)
write_dicts_to_hdf5_groups(group, dict)
end
write_dicts_to_hdf5_groups (generic function with 3 methods)
julia> function write_dicts_to_hdf5_groups(
parent::Union{HDF5.File, HDF5.Group},
key::AbstractString,
value
)
write(parent, key, value)
end
write_dicts_to_hdf5_groups (generic function with 3 methods)
julia> h5open("allthestuff.h5", "w") do h5f
write_dicts_to_hdf5_groups(h5f, allthestuff)
end
julia> run(`h5ls -r allthestuff.h5`)
/ Group
/a Dataset {9}
/e Group
/e/medium Group
/e/medium/epsilon Dataset {SCALAR}
/e/medium/mu Dataset {SCALAR}
/e/particle Group
/e/particle/epsilon Dataset {SCALAR}
/e/particle/mu Dataset {SCALAR}
/g Group
/g/a Dataset {SCALAR}
/g/c Dataset {SCALAR}
/g/shape Dataset {SCALAR}
/m Dataset {9, 30, 30}
Process(`h5ls -r allthestuff.h5`, ProcessExited(0))
Another approach here would be to encode the dict as a compound datatype. Basically convert the Dict to a NamedTuple and then write that.
Dicts are hard to read and write since we do not know the type of the members up front. A NamedTuple is fully typed.
A big question is if there is a large array somewhere in the tree then the current approach would be better.