Thanks to @dan7geo's helpful hint to ExportStructuredHDF5
exporting to HDF5 is easier than I thought.
Take for instance
data = <|
"group1" -> <|"subgroup1" -> "group1/subgroup1",
"subgroup2" -> "group1/subgroup2",
"subgroup3" ->
<|
"subsubgroup1" -> "group1/subgroup3/subsubgroup1",
"subsubgroup2" -> "group1/subgroup3/subsubgroup2"
|>
|>,
"group2" -> <|"subgroup1" -> "group2/subgroup1"|>|>
Create a temporary file and export data
file = CreateFile[];
ExportStructuredHDF5[file, data];
Importing the file with Import[file]
shows a list of datasets names within the file.
{"/group1/subgroup1", "/group1/subgroup2",
"/group1/subgroup3/subsubgroup1", "/group1/subgroup3/subsubgroup2",
"/group2/subgroup1"}
Note that these are the internal names of the datasets and not the content (I choose the content of data
to match these names for testing).
A neat feature is to import the file's structure with Import["/Users/Sascha/Desktop/temp.h5", "StructureGraph"]
Note that layout isn't too smart and the root node is the lightblue dot on the left.
Importing the actual data back as an association is done with
imported = Import[file, "Data"]
<|"/group1/subgroup1" -> "group1/subgroup1",
"/group1/subgroup2" -> "group1/subgroup2",
"/group1/subgroup3/subsubgroup1" -> "group1/subgroup3/subsubgroup1",
"/group1/subgroup3/subsubgroup2" -> "group1/subgroup3/subsubgroup2",
"/group2/subgroup1" -> "group2/subgroup1"|>
As you see the importer isn't too smart either and just gives a flat associaton even though Import
is aware of the file's structure as can be seen from the graph above.
To get back the original structure one can use ImportStructuredHDF5
instead.
ImportStructuredHDF5["file"]
reads an HDF5 file previously created by ExportStructuredHDF5
.
The option "ImportAsRawArray"
controls whether arrays of numbers are returned as RawArrays.