Group attributes with JLD2

I’m starting to use the JLD2.jl package to read an HDF5 file. I am bit confused about the way attributes of Group should be accessed. It seems to me that the parent node of the group is always needed to access the group attributes. Is it really the case or am I doing something wrong? Below is a MWE, but I don’t know how to upload the input file so I just display the result.

f = jldopen("toto-mesh.cgns", "r")
@show JLD2.load_attributes(f, "") # attributes of the top level node
@show JLD2.load_attributes(f, "mesh.cgns") # attributes of the 'child' group "mesh.cgns"
@show JLD2.load_attributes(f["mesh.cgns"], "") # I would expect same result as above, but it errors

and the output

JLDFile toto-mesh.cgns (read-only)
 ├─🔢  format
 ├─🔢  hdf5version
 ├─📂 CGNSLibraryVersion
 │  └─🔢  data
 └─📂 mesh.cgns
    ├─🔢  data
    ├─📂 About
    │  └─🔢  data
    ├─📂 mesh_Part0 (10 entries)
    └─ ⋯ (5 more entries)
JLD2.load_attributes(f, "") = [:name => "HDF5 MotherNode\0Root Node of HDF5", :label => "Root Node of HDF5 File\0No Error\0T", :type => "MT\0"]
JLD2.load_attributes(f, "mesh.cgns") = Pair{Symbol}[:name => "mesh.cgns\0yVersion\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", :label => "CGNSBase_t\0Invalid input:  nijk[%", :type => "I4\0", :flags => Int32[1]]
ERROR: LoadError: ArgumentError: did not find a group or dataset named ""

Thanks !

Hi @bmxam,

the load_attributes is a new-ish feature. JLD2 itself does not use attributes much. (e.g. currently you cannot write them yourself)
In particular, group objects don’t store the attributes in their metadata.
Therefore, the parent object is needed to load them.

Here’s a potential new function that would add the missing feature.

function JLD2.load_attributes(g::JLD2.Group)
           # find reloffset of this group
           # get file handle from group
           f = g.f
           # get offset of group in the file (file handle keeps track)
           reloffset = findfirst(==(g), f.loaded_groups)
           # load attributes using file handle and offset
           JLD2.load_attributes(f, reloffset)

Alright, thank you very much for the (very fast) reply!

I added this to JLD2 in the latest release.

I am curious why you would use JLD2.jl rather than HDF5.jl to open an arbitrary HDF5 file.

Very nice, thank you. I’ll try it soon. I haven’t looked at how the equality between two groups is implemented in JLD2, but is reloffset = findfirst(==(g), f.loaded_groups) safe when multiple groups share the same name?

I was seduced by the lack of dependency to the hdf5 library. From what I understand JLD2.jl is written in pure Julia and suits my need : I don’t need to rely on an external binary library that may not behave correctly on my system (I had troubles with PETSC_jll for instance). Nevertheless, I am sure HDF5.jl has everything I need (and even more with parrallel IO).

It is not implemented specifically, but since JLD2.Group are mutable structs the generic implementation will test for object identitity. So this is independent of the group name.

Side note: groups and other datasets don’t actually have names.
Groups use names to identify child- groups/datasets.