Update a variable in an HDF5 file

Simple problem, but it’s driving me mad. How do I update a dataset in an HDF5 file?


julia> tp2[“callParams”][“width”]
:1234: HDF5.Dataset: /callParams/width (file: testproj2.h5 xfer_mode: 0)

julia> read(tp2[“callParams/width”])

julia> tp2[“callParams”][“width”] = 256
ERROR: cannot create dataset: object “width” already exists at /callParams

julia> write(tp2,“callParams/width”,256)
ERROR: cannot create dataset: object “callParams/width” already exists at /

julia> HDF5.h5open(“testproj.h5”,“r+”) do fid
cp[“width”] = 256
ERROR: cannot create dataset: object “width” already exists at /callParams

Maybe you can check the key:

julia> file = tempname() * ".h5";

julia> h5write(file, "A/B", 1.1);

julia> h5read(file, "A/B")

julia> h5open(file, "r+") do f
           if haskey(f, "A/B")
               delete_object(f, "A/B")
           f["A/B"] = 2.2

Yes. That works. Thank you.

Surprised there is not an inbuilt way.

HDF5.h5open(“testproj.h5”,“w”) do fid

Would the above work?
Edit: Sorry, I think you meant to preserve (other) contents of the file, just replace one variable. This will overwrite the file.

Yes, and it also refuses to overwrite because the file exists.

Be aware that for older versions of HDF5 this didn’t actually free any space (see for example Reclaim storage space when deleting a large dataset in hdf5 file - HDF5 - HDF Forum). One had to explicitly h5repack the file afterwards. From the top of my head, I think they changed this in version 1.10.

1 Like

FWIW you can do this in h5py, I don’t know if it is deleting the object under the hood. If you made it a length 1 dataset you could change the content of the dataset like you can with larger ones.