# Group attributes with JLD2

**URL:** <https://discourse.julialang.org/t/group-attributes-with-jld2/114302>\
**Category:** General Usage\
**Tags:** jld2\
**Created:** [May 15, 2024, 2:20pm UTC](https://discourse.julialang.org/t/group-attributes-with-jld2/114302 "2024-05-15T14:20:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmxam](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@bmxam](https://discourse.julialang.org/u/bmxam)\
**Post date:** [May 15, 2024, 2:20pm UTC](https://discourse.julialang.org/t/group-attributes-with-jld2/114302/1 "2024-05-15T14:20:58Z")

</div>

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.

```julia
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
close(f)

```

and the output

```julia
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 !

---

<div class="post-metadata">

**Author:** ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)\
**Post date:** [May 15, 2024, 2:53pm UTC](https://discourse.julialang.org/t/group-attributes-with-jld2/114302/2 "2024-05-15T14:53:48Z")

</div>

> [@bmxam](#):
>
> 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 somethin

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.

```julia
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)
end

```

---

<div class="post-metadata">

**Author:** ![bmxam](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@bmxam](https://discourse.julialang.org/u/bmxam)\
**Post date:** [May 15, 2024, 2:56pm UTC](https://discourse.julialang.org/t/group-attributes-with-jld2/114302/3 "2024-05-15T14:56:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)\
**Post date:** [May 21, 2024, 7:16am UTC](https://discourse.julialang.org/t/group-attributes-with-jld2/114302/4 "2024-05-21T07:16:53Z")

</div>

I added this to JLD2 in the latest release.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 21, 2024, 8:07am UTC](https://discourse.julialang.org/t/group-attributes-with-jld2/114302/5 "2024-05-21T08:07:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bmxam](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@bmxam](https://discourse.julialang.org/u/bmxam)\
**Post date:** [May 21, 2024, 9:54am UTC](https://discourse.julialang.org/t/group-attributes-with-jld2/114302/6 "2024-05-21T09:54:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bmxam](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@bmxam](https://discourse.julialang.org/u/bmxam)\
**Post date:** [May 21, 2024, 1:08pm UTC](https://discourse.julialang.org/t/group-attributes-with-jld2/114302/7 "2024-05-21T13:08:05Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)\
**Post date:** [May 21, 2024, 1:26pm UTC](https://discourse.julialang.org/t/group-attributes-with-jld2/114302/8 "2024-05-21T13:26:51Z")

</div>

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.
